using System.Collections.Generic;
using Tessera.Typesetting;
namespace Tessera.UnitTests.Typesetting
{
///
/// Test fonts that the layout tests share with the tests of what's built on layout.
///
public static class LayoutFonts
{
public const int LetterWidth = 10;
public const int SpaceWidth = 4;
public const int HyphenWidth = 3;
public const int InvalidGlyphWidth = 7;
public const int EllipsisWidth = 5;
public const int PeriodWidth = 2;
public const int LineSpacing = 10;
public const int WideGlyph = 1000000;
public static (short, short) Pair(char left, char right) => ((short)left, (short)right);
///
/// Letters, a space, a period and an ellipsis, none kerned unless `kernings` says. It has a real line height
/// for tests of the maxHeight cutoff.
///
public static MetricsFont FontWithLineHeight(int ascent = 0, int descent = 0,
bool withEllipsis = true, Dictionary<(short, short), short> kernings = null)
{
var glyphs = new Dictionary();
for (char letter = 'a'; letter <= 'z'; ++letter)
{
glyphs[letter] = new GlyphMetrics { Width = LetterWidth };
}
glyphs[' '] = new GlyphMetrics { Width = SpaceWidth };
glyphs['.'] = new GlyphMetrics { Width = PeriodWidth };
if (withEllipsis)
{
glyphs[TruncationMarker.EllipsisGlyph] = new GlyphMetrics { Width = EllipsisWidth };
}
return new MetricsFont(new MetricsFont.Data
{
Glyphs = glyphs,
InvalidGlyph = new GlyphMetrics { Width = InvalidGlyphWidth },
Kernings = kernings ?? new Dictionary<(short, short), short>(),
Baseline = ascent,
LineHeight = ascent + descent,
});
}
///
/// A with lines 10 apart, built once for every test to share.
///
public static readonly MetricsFont FontWithTenPixelLines = FontWithLineHeight(ascent: 8, descent: 2);
///
/// Lowercase letters, a space, a hyphen, a period and an ellipsis, each the same width in every font that
/// doesn't say otherwise, with lines 10 apart.
///
public static MetricsFont Face(int letterWidth, int spaceWidth = 4, int hyphenWidth = 3,
int ellipsisWidth = 5, int invalidWidth = InvalidGlyphWidth, int ascent = 8,
int descent = 2, string page = null, Dictionary<(short, short), short> kernings = null)
{
var glyphs = new Dictionary();
for (char letter = 'a'; letter <= 'z'; ++letter)
{
glyphs[letter] = new GlyphMetrics { Width = letterWidth };
}
glyphs[' '] = new GlyphMetrics { Width = spaceWidth };
glyphs['-'] = new GlyphMetrics { Width = hyphenWidth };
glyphs['.'] = new GlyphMetrics { Width = PeriodWidth };
glyphs[TruncationMarker.EllipsisGlyph] = new GlyphMetrics { Width = ellipsisWidth };
return new MetricsFont(new MetricsFont.Data
{
Glyphs = glyphs,
InvalidGlyph = new GlyphMetrics { Width = invalidWidth },
Kernings = kernings ?? new Dictionary<(short, short), short>(),
Baseline = ascent,
LineHeight = ascent + descent,
Pages = page == null ? null : new[] { page },
});
}
/// How much wider than 's every glyph is.
/// How far above the baseline the font's lines reach.
public static MetricsFont BuildKernedFont(int widthOffset = 0, int ascent = 8)
{
var glyphs = new Dictionary();
for (char character = ' '; character <= '~'; ++character)
{
glyphs[character] = new GlyphMetrics { Width = character % 7 + 3 + widthOffset };
}
glyphs[TruncationMarker.EllipsisGlyph] = new GlyphMetrics { Width = 5 };
string kerned = $" ().-ab{TruncationMarker.EllipsisGlyph}";
var kernings = new Dictionary<(short, short), short>();
foreach (char left in kerned)
{
foreach (char right in kerned)
{
kernings[Pair(left, right)] = (short)((left * 3 + right) % 5 - 2);
}
}
return new MetricsFont(new MetricsFont.Data
{
Glyphs = glyphs,
InvalidGlyph = new GlyphMetrics { Width = InvalidGlyphWidth },
Kernings = kernings,
Baseline = ascent,
LineHeight = ascent + LineSpacing - 8,
});
}
///
/// Printable ASCII in assorted widths plus an ellipsis, kerned among the characters the width tests use most,
/// with lines apart.
///
public static readonly MetricsFont KernedFont = BuildKernedFont();
public static MetricsFont BuildWideFont(int glyphWidth = WideGlyph)
{
var glyphs = new Dictionary();
for (char letter = 'a'; letter <= 'z'; ++letter)
{
glyphs[letter] = new GlyphMetrics { Width = glyphWidth };
}
glyphs[' '] = new GlyphMetrics { Width = glyphWidth };
return new MetricsFont(new MetricsFont.Data
{
Glyphs = glyphs,
InvalidGlyph = new GlyphMetrics { Width = InvalidGlyphWidth },
Kernings = new Dictionary<(short, short), short>(),
Baseline = 8,
LineHeight = LineSpacing,
});
}
public static readonly MetricsFont WideFont = BuildWideFont();
}
}