namespace Spectre.Console; /// /// Represents a FIGlet font. /// public sealed class FigletFont { private const string StandardFont = "Spectre.Console/Widgets/Figlet/Fonts/Standard.flf"; private readonly Dictionary _characters; private static readonly Lazy _standard; /// /// Gets the number of characters in the font. /// public int Count => _characters.Count; /// /// Gets the height of the font. /// public int Height { get; } /// /// Gets the font's baseline. /// public int Baseline { get; } /// /// Gets the font's maximum width. /// public int MaxWidth { get; } /// /// Gets the default FIGlet font. /// public static FigletFont Default => _standard.Value; static FigletFont() { _standard = new Lazy(() => Parse( ResourceReader.ReadManifestData(StandardFont))); } internal FigletFont(IEnumerable characters, FigletHeader header) { _characters = new Dictionary(); foreach (var character in characters) { if (_characters.ContainsKey(character.Code)) { throw new InvalidOperationException("Character already exist"); } _characters[character.Code] = character; } Height = header.Height; Baseline = header.Baseline; MaxWidth = header.MaxLength; } /// /// Loads a FIGlet font from the specified stream. /// /// The stream to load the FIGlet font from. /// The loaded FIGlet font. public static FigletFont Load(Stream stream) { using (var reader = new StreamReader(stream)) { return Parse(reader.ReadToEnd()); } } /// /// Loads a FIGlet font from disk. /// /// The path of the FIGlet font to load. /// The loaded FIGlet font. public static FigletFont Load(string path) { return Parse(File.ReadAllText(path)); } /// /// Parses a FIGlet font from the specified . /// /// The FIGlet font source. /// The parsed FIGlet font. public static FigletFont Parse(string source) { return FigletFontParser.Parse(source); } internal int GetWidth(string text) { var width = 0; foreach (var character in text) { width += GetCharacter(character)?.Width ?? 0; } return width; } internal FigletCharacter? GetCharacter(char character) { _characters.TryGetValue(character, out var result); return result; } internal IEnumerable GetCharacters(string text) { if (text is null) { throw new ArgumentNullException(nameof(text)); } var result = new List(); foreach (var character in text) { if (_characters.TryGetValue(character, out var figletCharacter)) { result.Add(figletCharacter); } } return result; } }