mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
Use file scoped namespace declarations
This commit is contained in:

committed by
Phil Scott

parent
1dbaf50935
commit
ec1188b837
@ -2,29 +2,28 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class FigletCharacter
|
||||
{
|
||||
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)
|
||||
{
|
||||
public int Code { get; }
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
public IReadOnlyList<string> Lines { get; }
|
||||
Code = code;
|
||||
Lines = new List<string>(lines ?? throw new ArgumentNullException(nameof(lines)));
|
||||
|
||||
public FigletCharacter(int code, IEnumerable<string> lines)
|
||||
var min = Lines.Min(x => x.Length);
|
||||
var max = Lines.Max(x => x.Length);
|
||||
if (min != max)
|
||||
{
|
||||
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;
|
||||
throw new InvalidOperationException($"Figlet character #{code} has varying width");
|
||||
}
|
||||
|
||||
Width = max;
|
||||
Height = Lines.Count;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,135 +2,134 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a FIGlet font.
|
||||
/// </summary>
|
||||
public sealed class FigletFont
|
||||
{
|
||||
private const string StandardFont = "Spectre.Console/Widgets/Figlet/Fonts/Standard.flf";
|
||||
|
||||
private readonly Dictionary<int, FigletCharacter> _characters;
|
||||
private static readonly Lazy<FigletFont> _standard;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a FIGlet font.
|
||||
/// Gets the number of characters in the font.
|
||||
/// </summary>
|
||||
public sealed class FigletFont
|
||||
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()
|
||||
{
|
||||
private const string StandardFont = "Spectre.Console/Widgets/Figlet/Fonts/Standard.flf";
|
||||
_standard = new Lazy<FigletFont>(() => Parse(
|
||||
ResourceReader.ReadManifestData(StandardFont)));
|
||||
}
|
||||
|
||||
private readonly Dictionary<int, FigletCharacter> _characters;
|
||||
private static readonly Lazy<FigletFont> _standard;
|
||||
internal FigletFont(IEnumerable<FigletCharacter> characters, FigletHeader header)
|
||||
{
|
||||
_characters = new Dictionary<int, FigletCharacter>();
|
||||
|
||||
/// <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()
|
||||
foreach (var character in characters)
|
||||
{
|
||||
_standard = new Lazy<FigletFont>(() => Parse(
|
||||
ResourceReader.ReadManifestData(StandardFont)));
|
||||
}
|
||||
|
||||
internal FigletFont(IEnumerable<FigletCharacter> characters, FigletHeader header)
|
||||
{
|
||||
_characters = new Dictionary<int, FigletCharacter>();
|
||||
|
||||
foreach (var character in characters)
|
||||
if (_characters.ContainsKey(character.Code))
|
||||
{
|
||||
if (_characters.ContainsKey(character.Code))
|
||||
{
|
||||
throw new InvalidOperationException("Character already exist");
|
||||
}
|
||||
|
||||
_characters[character.Code] = character;
|
||||
throw new InvalidOperationException("Character already exist");
|
||||
}
|
||||
|
||||
Height = header.Height;
|
||||
Baseline = header.Baseline;
|
||||
MaxWidth = header.MaxLength;
|
||||
_characters[character.Code] = character;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
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))
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
@ -3,112 +3,111 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal static class FigletFontParser
|
||||
{
|
||||
internal static class FigletFontParser
|
||||
public static FigletFont Parse(string source)
|
||||
{
|
||||
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))
|
||||
{
|
||||
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))
|
||||
{
|
||||
if (!line.EndsWith("@", StringComparison.Ordinal))
|
||||
var words = line.SplitWords();
|
||||
if (words.Length > 0 && TryParseIndex(words[0], out var newIndex))
|
||||
{
|
||||
var words = line.SplitWords();
|
||||
if (words.Length > 0 && TryParseIndex(words[0], out var newIndex))
|
||||
{
|
||||
index = newIndex;
|
||||
indexOverridden = true;
|
||||
hasOverriddenIndex = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
index = newIndex;
|
||||
indexOverridden = true;
|
||||
hasOverriddenIndex = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hasOverriddenIndex && !indexOverridden)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hasOverriddenIndex && !indexOverridden)
|
||||
{
|
||||
throw new InvalidOperationException("Unknown index for FIGlet character");
|
||||
}
|
||||
|
||||
buffer.Add(line.Replace(header.Hardblank, ' ').ReplaceExact("@", string.Empty));
|
||||
|
||||
if (line.EndsWith("@@", StringComparison.Ordinal))
|
||||
{
|
||||
characters.Add(new FigletCharacter(index, buffer));
|
||||
buffer.Clear();
|
||||
|
||||
if (!hasOverriddenIndex)
|
||||
{
|
||||
throw new InvalidOperationException("Unknown index for FIGlet character");
|
||||
index++;
|
||||
}
|
||||
|
||||
buffer.Add(line.Replace(header.Hardblank, ' ').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;
|
||||
}
|
||||
// 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';
|
||||
}
|
||||
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';
|
||||
}
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal sealed class FigletHeader
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
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; }
|
||||
}
|
@ -3,149 +3,148 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
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>
|
||||
/// Represents text rendered with a FIGlet font.
|
||||
/// Gets or sets the color of the text.
|
||||
/// </summary>
|
||||
public sealed class FigletText : Renderable, IAlignable
|
||||
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)
|
||||
{
|
||||
private readonly FigletFont _font;
|
||||
private readonly string _text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the color of the text.
|
||||
/// </summary>
|
||||
public Color? Color { get; set; }
|
||||
/// <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/>
|
||||
public Justify? Alignment { get; set; }
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
var style = new Style(Color ?? Console.Color.Default);
|
||||
var alignment = Alignment ?? Justify.Left;
|
||||
|
||||
/// <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)
|
||||
foreach (var row in GetRows(maxWidth))
|
||||
{
|
||||
}
|
||||
|
||||
/// <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++)
|
||||
{
|
||||
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();
|
||||
if (alignment == Justify.Left)
|
||||
{
|
||||
var line = new Segment(string.Concat(row.Select(x => x.Lines[index])), style);
|
||||
yield return line;
|
||||
|
||||
var lineWidth = line.CellCount();
|
||||
if (alignment == Justify.Left)
|
||||
if (lineWidth < maxWidth)
|
||||
{
|
||||
yield return line;
|
||||
|
||||
if (lineWidth < maxWidth)
|
||||
{
|
||||
yield return Segment.Padding(maxWidth - lineWidth);
|
||||
}
|
||||
yield return Segment.Padding(maxWidth - lineWidth);
|
||||
}
|
||||
else if (alignment == Justify.Center)
|
||||
{
|
||||
var left = (maxWidth - lineWidth) / 2;
|
||||
var right = left + ((maxWidth - lineWidth) % 2);
|
||||
|
||||
yield return Segment.Padding(left);
|
||||
yield return line;
|
||||
yield return Segment.Padding(right);
|
||||
}
|
||||
else if (alignment == Justify.Right)
|
||||
{
|
||||
if (lineWidth < maxWidth)
|
||||
{
|
||||
yield return Segment.Padding(maxWidth - lineWidth);
|
||||
}
|
||||
|
||||
yield return line;
|
||||
}
|
||||
|
||||
yield return Segment.LineBreak;
|
||||
}
|
||||
else if (alignment == Justify.Center)
|
||||
{
|
||||
var left = (maxWidth - lineWidth) / 2;
|
||||
var right = left + ((maxWidth - lineWidth) % 2);
|
||||
|
||||
yield return Segment.Padding(left);
|
||||
yield return line;
|
||||
yield return Segment.Padding(right);
|
||||
}
|
||||
else if (alignment == Justify.Right)
|
||||
{
|
||||
if (lineWidth < maxWidth)
|
||||
{
|
||||
yield return Segment.Padding(maxWidth - lineWidth);
|
||||
}
|
||||
|
||||
yield return line;
|
||||
}
|
||||
|
||||
yield return Segment.LineBreak;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<List<FigletCharacter>> GetRows(int maxWidth)
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// 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 its own line?
|
||||
if (width < maxWidth)
|
||||
{
|
||||
// Add it to the line
|
||||
// Flush the line
|
||||
result.Add(line);
|
||||
line = new List<FigletCharacter>();
|
||||
totalWidth = 0;
|
||||
|
||||
line.AddRange(_font.GetCharacters(word));
|
||||
totalWidth += width;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Does it fit on its own line?
|
||||
if (width < maxWidth)
|
||||
// We need to split it up.
|
||||
var queue = new Queue<FigletCharacter>(_font.GetCharacters(word));
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
if (line.Count > 0)
|
||||
{
|
||||
result.Add(line);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user