Add FIGlet text support

Closes #97
This commit is contained in:
Patrik Svensson
2020-11-20 00:15:13 +01:00
committed by Patrik Svensson
parent bde61cc6ff
commit a59e0dcb21
24 changed files with 3718 additions and 15 deletions

View File

@ -0,0 +1,27 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="FigletText"/>.
/// </summary>
public static class FigletTextExtensions
{
/// <summary>
/// Sets the color of the FIGlet text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static FigletText Color(this FigletText text, Color? color)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
text.Color = color ?? Console.Color.Default;
return text;
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Spectre.Console
{
internal sealed class FigletCharacter
{
public int Code { get; }
public int Width { get; }
public int Height { get; }
public IReadOnlyList<string> Lines { get; }
public FigletCharacter(int code, IEnumerable<string> lines)
{
Code = code;
Lines = new List<string>(lines ?? throw new ArgumentNullException(nameof(lines)));
var min = Lines.Min(x => x.Length);
var max = Lines.Max(x => x.Length);
if (min != max)
{
throw new InvalidOperationException($"Figlet character #{code} has varying width");
}
Width = max;
Height = Lines.Count;
}
}
}

View File

@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.IO;
using Spectre.Console.Internal;
namespace Spectre.Console
{
/// <summary>
/// Represents a FIGlet font.
/// </summary>
public sealed class FigletFont
{
private readonly Dictionary<int, FigletCharacter> _characters;
private static readonly Lazy<FigletFont> _standard;
/// <summary>
/// Gets the number of characters in the font.
/// </summary>
public int Count => _characters.Count;
/// <summary>
/// Gets the height of the font.
/// </summary>
public int Height { get; }
/// <summary>
/// Gets the font's baseline.
/// </summary>
public int Baseline { get; }
/// <summary>
/// Gets the font's maximum width.
/// </summary>
public int MaxWidth { get; }
/// <summary>
/// Gets the default FIGlet font.
/// </summary>
public static FigletFont Default => _standard.Value;
static FigletFont()
{
_standard = new Lazy<FigletFont>(() => Parse(ResourceReader.ReadManifestData("Spectre.Console/Figlet/Fonts/Standard.flf")));
}
internal FigletFont(IEnumerable<FigletCharacter> characters, FigletHeader header)
{
_characters = new Dictionary<int, FigletCharacter>();
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;
}
/// <summary>
/// Loads a FIGlet font from the specified stream.
/// </summary>
/// <param name="stream">The stream to load the FIGlet font from.</param>
/// <returns>The loaded FIGlet font.</returns>
public static FigletFont Load(Stream stream)
{
using (var reader = new StreamReader(stream))
{
return Parse(reader.ReadToEnd());
}
}
/// <summary>
/// Loads a FIGlet font from disk.
/// </summary>
/// <param name="path">The path of the FIGlet font to load.</param>
/// <returns>The loaded FIGlet font.</returns>
public static FigletFont Load(string path)
{
return Parse(File.ReadAllText(path));
}
/// <summary>
/// Parses a FIGlet font from the specified <see cref="string"/>.
/// </summary>
/// <param name="source">The FIGlet font source.</param>
/// <returns>The parsed FIGlet font.</returns>
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<FigletCharacter> GetCharacters(string text)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
var result = new List<FigletCharacter>();
foreach (var character in text)
{
if (_characters.TryGetValue(character, out var figletCharacter))
{
result.Add(figletCharacter);
}
}
return result;
}
}
}

View File

@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Spectre.Console
{
internal static class FigletFontParser
{
public static FigletFont Parse(string source)
{
var lines = source.SplitLines();
var header = ParseHeader(lines.FirstOrDefault());
var characters = new List<FigletCharacter>();
var index = 32;
var indexOverridden = false;
var hasOverriddenIndex = false;
var buffer = new List<string>();
foreach (var line in lines.Skip(header.CommentLines + 1))
{
if (!line.EndsWith("@", StringComparison.Ordinal))
{
var words = line.SplitWords();
if (words.Length > 0 && TryParseIndex(words[0], out var newIndex))
{
index = newIndex;
indexOverridden = true;
hasOverriddenIndex = true;
continue;
}
continue;
}
if (hasOverriddenIndex && !indexOverridden)
{
throw new InvalidOperationException("Unknown index for FIGlet character");
}
buffer.Add(line.Replace('$', ' ').ReplaceExact("@", string.Empty));
if (line.EndsWith("@@", StringComparison.Ordinal))
{
characters.Add(new FigletCharacter(index, buffer));
buffer.Clear();
if (!hasOverriddenIndex)
{
index++;
}
// Reset the flag so we know if we're trying to parse
// a character that wasn't prefixed with an ASCII index.
indexOverridden = false;
}
}
return new FigletFont(characters, header);
}
private static bool TryParseIndex(string index, out int result)
{
var style = NumberStyles.Integer;
if (index.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
// TODO: ReplaceExact should not be used
index = index.ReplaceExact("0x", string.Empty).ReplaceExact("0x", string.Empty);
style = NumberStyles.HexNumber;
}
return int.TryParse(index, style, CultureInfo.InvariantCulture, out result);
}
private static FigletHeader ParseHeader(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
throw new InvalidOperationException("Invalid Figlet font");
}
var parts = text.SplitWords(StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 6)
{
throw new InvalidOperationException("Invalid Figlet font header");
}
if (!IsValidSignature(parts[0]))
{
throw new InvalidOperationException("Invalid Figlet font header signature");
}
return new FigletHeader
{
Hardblank = parts[0][5],
Height = int.Parse(parts[1], CultureInfo.InvariantCulture),
Baseline = int.Parse(parts[2], CultureInfo.InvariantCulture),
MaxLength = int.Parse(parts[3], CultureInfo.InvariantCulture),
OldLayout = int.Parse(parts[4], CultureInfo.InvariantCulture),
CommentLines = int.Parse(parts[5], CultureInfo.InvariantCulture),
};
}
private static bool IsValidSignature(string signature)
{
return signature.Length == 6
&& signature[0] == 'f' && signature[1] == 'l'
&& signature[2] == 'f' && signature[3] == '2'
&& signature[4] == 'a';
}
}
}

View File

@ -0,0 +1,12 @@
namespace Spectre.Console
{
internal sealed class FigletHeader
{
public char Hardblank { get; set; }
public int Height { get; set; }
public int Baseline { get; set; }
public int MaxLength { get; set; }
public int OldLayout { get; set; }
public int CommentLines { get; set; }
}
}

View File

@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents text rendered with a FIGlet font.
/// </summary>
public sealed class FigletText : Renderable, IAlignable
{
private readonly FigletFont _font;
private readonly string _text;
/// <summary>
/// Gets or sets the color of the text.
/// </summary>
public Color? Color { get; set; }
/// <inheritdoc/>
public Justify? Alignment { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="FigletText"/> class.
/// </summary>
/// <param name="text">The text.</param>
public FigletText(string text)
: this(FigletFont.Default, text)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FigletText"/> class.
/// </summary>
/// <param name="font">The FIGlet font to use.</param>
/// <param name="text">The text.</param>
public FigletText(FigletFont font, string text)
{
_font = font ?? throw new ArgumentNullException(nameof(font));
_text = text ?? throw new ArgumentNullException(nameof(text));
}
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
var style = new Style(Color ?? Console.Color.Default);
var alignment = Alignment ?? Justify.Left;
foreach (var row in GetRows(maxWidth))
{
for (var index = 0; index < _font.Height; index++)
{
var line = new Segment(string.Concat(row.Select(x => x.Lines[index])), style);
var lineWidth = line.CellCount(context);
if (alignment == Justify.Left)
{
yield return line;
if (lineWidth < maxWidth)
{
yield return new Segment(new string(' ', maxWidth - lineWidth));
}
}
else if (alignment == Justify.Center)
{
var left = (maxWidth - lineWidth) / 2;
var right = left + ((maxWidth - lineWidth) % 2);
yield return new Segment(new string(' ', left));
yield return line;
yield return new Segment(new string(' ', right));
}
else if (alignment == Justify.Right)
{
if (lineWidth < maxWidth)
{
yield return new Segment(new string(' ', maxWidth - lineWidth));
}
yield return line;
}
yield return Segment.LineBreak;
}
}
}
private List<List<FigletCharacter>> GetRows(int maxWidth)
{
var result = new List<List<FigletCharacter>>();
var words = _text.SplitWords(StringSplitOptions.None);
var totalWidth = 0;
var line = new List<FigletCharacter>();
foreach (var word in words)
{
// Does the whole word fit?
var width = _font.GetWidth(word);
if (width + totalWidth < maxWidth)
{
// Add it to the line
line.AddRange(_font.GetCharacters(word));
totalWidth += width;
}
else
{
// Does it fit on it's own line?
if (width < maxWidth)
{
// Flush the line
result.Add(line);
line = new List<FigletCharacter>();
totalWidth = 0;
line.AddRange(_font.GetCharacters(word));
totalWidth += width;
}
else
{
// We need to split it up.
var queue = new Queue<FigletCharacter>(_font.GetCharacters(word));
while (queue.Count > 0)
{
var current = queue.Dequeue();
if (totalWidth + current.Width > maxWidth)
{
// Flush the line
result.Add(line);
line = new List<FigletCharacter>();
totalWidth = 0;
}
line.Add(current);
totalWidth += current.Width;
}
}
}
}
if (line.Count > 0)
{
result.Add(line);
}
return result;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,32 @@
using System;
using System.IO;
namespace Spectre.Console.Internal
{
internal static class ResourceReader
{
public static string ReadManifestData(string resourceName)
{
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
var assembly = typeof(ResourceReader).Assembly;
resourceName = resourceName.ReplaceExact("/", ".");
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd().NormalizeLineEndings();
}
}
}
}
}

View File

@ -5,8 +5,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="Figlet\Fonts\Standard.flf" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\stylecop.json" Link="Properties/stylecop.json" />
<EmbeddedResource Include="Figlet\Fonts\Standard.flf" />
<None Include="../../resources/gfx/small-logo.png" Pack="true" PackagePath="\" Link="Properties/small-logo.png" />
</ItemGroup>
@ -20,20 +25,6 @@
<PackageReference Include="Wcwidth" Version="0.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="BoxBorder.Known.cs">
<DependentUpon>BoxBorder.cs</DependentUpon>
</Compile>
<Compile Update="TableBorder.Known.cs">
<DependentUpon>TableBorder.cs</DependentUpon>
</Compile>
<Compile Update="Extensions\AnsiConsoleExtensions.Markup.cs">
<DependentUpon>AnsiConsoleExtensions.cs</DependentUpon>
</Compile>
<Compile Update="Extensions\AnsiConsoleExtensions.Rendering.cs">
<DependentUpon>AnsiConsoleExtensions.cs</DependentUpon>
</Compile>
</ItemGroup>
<PropertyGroup>
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>