|
| 1 | +// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) |
| 2 | +// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace Stride.Graphics.Font; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Provides runtime registration and loading of fonts from the file system. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// <para>This provider allows loading custom TrueType fonts (.ttf) at runtime without going through |
| 15 | +/// the content pipeline. Fonts registered through this provider are immediately available for use |
| 16 | +/// with <see cref="FontSystem.LoadRuntimeFont"/>.</para> |
| 17 | +/// <para><strong>Memory Management:</strong> All registered fonts remain in memory for the lifetime |
| 18 | +/// of the application. There is no mechanism to unload individual fonts once registered. For applications |
| 19 | +/// with dynamic font requirements or memory constraints, consider the total memory footprint of all |
| 20 | +/// registered fonts.</para> |
| 21 | +/// </remarks> |
| 22 | +public class RuntimeFontProvider |
| 23 | +{ |
| 24 | + private readonly FontSystem fontSystem; |
| 25 | + private readonly Dictionary<string, RuntimeFontInfo> registeredFonts = []; |
| 26 | + |
| 27 | + internal RuntimeFontProvider(FontSystem fontSystem) |
| 28 | + { |
| 29 | + this.fontSystem = fontSystem; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Registers a font file for runtime loading. |
| 34 | + /// </summary> |
| 35 | + /// <remarks> |
| 36 | + /// <para>Once registered, fonts are loaded into memory and cached for the lifetime of the font system. |
| 37 | + /// Individual fonts cannot be unregistered or unloaded - they remain in memory until the application exits |
| 38 | + /// or the font system is disposed.</para> |
| 39 | + /// <para>Attempting to register the same font name and style with a different file path will throw an exception.</para> |
| 40 | + /// </remarks> |
| 41 | + /// <param name="fontName">The name to use when loading the font (e.g., "MyFont").</param> |
| 42 | + /// <param name="filePath">The absolute or relative path to the .ttf file.</param> |
| 43 | + /// <param name="style">The font style.</param> |
| 44 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="fontName"/> is null or empty.</exception> |
| 45 | + /// <exception cref="FileNotFoundException">Thrown when the font file does not exist.</exception> |
| 46 | + /// <exception cref="InvalidOperationException">Thrown when attempting to register the same font name and style with a different file path.</exception> |
| 47 | + public void RegisterFont(string fontName, string filePath, FontStyle style = FontStyle.Regular) |
| 48 | + { |
| 49 | + ArgumentException.ThrowIfNullOrWhiteSpace(fontName); |
| 50 | + |
| 51 | + if (!File.Exists(filePath)) |
| 52 | + throw new FileNotFoundException($"Font file not found: {filePath}", filePath); |
| 53 | + |
| 54 | + var key = FontHelper.GetFontPath(fontName, style); |
| 55 | + |
| 56 | + if (registeredFonts.TryGetValue(key, out var existing)) |
| 57 | + { |
| 58 | + if (existing.FilePath == filePath) return; |
| 59 | + |
| 60 | + throw new InvalidOperationException( |
| 61 | + $"Font '{fontName}' with style '{style}' is already registered with path '{existing.FilePath}'. " + |
| 62 | + $"Cannot register a different path '{filePath}' for the same font name and style."); |
| 63 | + } |
| 64 | + |
| 65 | + fontSystem.FontManager.LoadFontFromFileSystem(fontName, filePath, style); |
| 66 | + |
| 67 | + registeredFonts[key] = new RuntimeFontInfo(fontName, filePath, style); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Checks if a font is registered for runtime loading. |
| 72 | + /// </summary> |
| 73 | + /// <param name="fontName">The font name.</param> |
| 74 | + /// <param name="style">The font style.</param> |
| 75 | + /// <returns>True if registered, false otherwise.</returns> |
| 76 | + public bool IsRegistered(string fontName, FontStyle style = FontStyle.Regular) |
| 77 | + { |
| 78 | + return registeredFonts.ContainsKey(FontHelper.GetFontPath(fontName, style)); |
| 79 | + } |
| 80 | + |
| 81 | + private record RuntimeFontInfo(string FontName, string FilePath, FontStyle Style); |
| 82 | +} |
0 commit comments