using System;
using System.Collections.Generic;
using Amphora;
using Tessera.Text;
using Tessera.Typesetting;
using UnitTestSharp;
using Tessera.Unicode;
using static Tessera.UnitTests.Typesetting.LayoutFonts;
namespace Tessera.UnitTests.Typesetting
{
public class FacedMeasuringTests : TestFixture
{
///
/// Letters 10 wide with no kerning.
///
public static readonly MetricsFont Narrow = Face(letterWidth: 10);
///
/// Letters 14 wide with a wider space, hyphen and ellipsis than , and no kerning.
///
public static readonly MetricsFont Wide = Face(letterWidth: 14, spaceWidth: 6, hyphenWidth: 5,
ellipsisWidth: 8, invalidWidth: 9);
///
/// Letters 10 wide like , with 4 more pixels above the baseline.
///
public static readonly MetricsFont Tall = Face(letterWidth: 10, ascent: 12, descent: 2);
///
/// Letters 10 wide like , with 3 fewer pixels above the baseline.
///
public static readonly MetricsFont Short = Face(letterWidth: 10, ascent: 5, descent: 2);
///
/// with a kerning pair of each kind a test crosses a face change with.
///
public static readonly MetricsFont Kerned = Face(letterWidth: 10, kernings:
new Dictionary<(short, short), short>
{
[Pair('a', 'b')] = -1,
[Pair('b', 'c')] = -4,
[Pair('b', '-')] = -1,
[Pair('b', TruncationMarker.EllipsisGlyph)] = -1,
[Pair('b', ' ')] = -2,
[Pair(' ', 'c')] = -3,
[Pair(' ', ' ')] = 1,
});
///
/// with kerning that differs from 's for the same pairs.
///
public static readonly MetricsFont KernedWide = Face(letterWidth: 14, spaceWidth: 6, hyphenWidth: 5,
ellipsisWidth: 8, invalidWidth: 9, kernings: new Dictionary<(short, short), short>
{
[Pair('a', 'b')] = -3,
[Pair('b', 'c')] = -6,
[Pair('b', '-')] = -2,
[Pair('b', TruncationMarker.EllipsisGlyph)] = -2,
[Pair('b', ' ')] = -5,
[Pair(' ', 'c')] = -5,
[Pair(' ', ' ')] = 2,
});
public static FaceRuns Runs(IFontMetrics baseFont, params (int start, int end, IFontMetrics font)[] runs)
{
var returnMe = new FaceRuns(baseFont);
foreach ((int start, int end, IFontMetrics font) in runs)
{
returnMe.Add(start, end, font);
}
return returnMe;
}
public static string Describe(int start, in MeasuredWord word)
{
return $"{start}..{word.ContentEnd}..{word.End}{(word.IsMandatory ? "!" : "")} " +
$"lead {word.LeadingKerning} content {word.ContentWidth} end {word.EndKerning}";
}
public static List Words(StringView text, IFontMetrics baseFont,
params (int start, int end, IFontMetrics font)[] runs)
{
var measured = new MeasuredWords(baseFont, text, Runs(baseFont, runs));
var returnMe = new List();
while (measured.MoveNext())
{
returnMe.Add(Describe(measured.CurrentStart, measured.Current));
}
return returnMe;
}
///
/// The first word of `text` measured with `runs`, for a test of what ends it.
///
public static MeasuredWord FirstWord(StringView text, IFontMetrics baseFont,
params (int start, int end, IFontMetrics font)[] runs)
{
var measured = new MeasuredWords(baseFont, text, Runs(baseFont, runs));
measured.MoveNext();
return measured.Current;
}
public class MeasureTests : TestFixture
{
public void TextInTheBaseFont_MeasuresInIt()
{
CheckEqual(new[] { "0..2..3 lead 0 content 20 end 0", "3..5..5! lead 0 content 20 end 0" },
Words("ab cd", Narrow, (7, 8, Wide)));
}
public void WordInAnotherFont_MeasuresInIt()
{
CheckEqual(new[] { "0..2..3 lead 0 content 20 end 0", "3..5..5! lead 0 content 28 end 0" },
Words("ab cd", Narrow, (3, 5, Wide)));
}
public void WordStraddlingTwoFonts_IsTheSumOfItsGlyphs()
{
CheckEqual(new[] { "0..4..4! lead 0 content 48 end 0" }, Words("abcd", Narrow, (1, 3, Wide)));
}
public void KerningInsideARun_UsesThatFontsTable()
{
CheckEqual(new[] { "0..2..2! lead 0 content 19 end 0" }, Words("ab", KernedWide, (0, 2, Kerned)));
}
public void KerningAcrossAFaceChange_IsNone()
{
CheckEqual(new[] { "0..2..2! lead 0 content 24 end 0" }, Words("bc", Kerned, (1, 2, KernedWide)));
}
public void KerningWithinTheBaseFont_IsItsOwn()
{
CheckEqual(new[] { "0..2..2! lead 0 content 16 end 0" }, Words("bc", Kerned, (5, 6, KernedWide)));
}
public void LeadingKerningAcrossAFaceChange_IsNone()
{
CheckEqual(new[] { "0..1..2 lead 0 content 10 end -2", "2..3..3! lead 0 content 14 end 0" },
Words("b c", Kerned, (2, 3, KernedWide)));
}
public void LeadingKerningWithinTheBaseFont_IsItsOwn()
{
CheckEqual(new[] { "0..1..2 lead 0 content 10 end -2", "2..3..3! lead -3 content 10 end 0" },
Words("b c", Kerned, (5, 6, KernedWide)));
}
public void LeadingKerningAfterASpaceInAnotherFont_IsNone()
{
CheckEqual(new[] { "0..1..2 lead 0 content 10 end 0", "2..3..3! lead 0 content 10 end 0" },
Words("b c", Kerned, (1, 2, KernedWide)));
}
public void TrailingSpace_KernsAgainstTheContentInTheSameFont()
{
CheckEqual("0..1..2 lead 0 content 10 end -2",
Describe(0, FirstWord("b c", Kerned, (5, 6, KernedWide))));
}
public void TrailingSpaceInAnotherFont_KernsAsAtTheStartOfALine()
{
CheckEqual("0..1..2 lead 0 content 10 end 0",
Describe(0, FirstWord("b c", Kerned, (1, 2, KernedWide))));
}
public void SoftHyphenInTheSameFont_KernsAgainstTheContent()
{
string text = $"b{LineBreakIterator.SoftHyphen}c";
CheckEqual(new[] { "0..2..2 lead 0 content 10 end -1", "2..3..3! lead -4 content 10 end 0" },
Words(text, Kerned, (5, 6, KernedWide)));
}
public void SoftHyphenInAnotherFont_KernsAsAtTheStartOfALine()
{
string text = $"b{LineBreakIterator.SoftHyphen}c";
CheckEqual(new[] { "0..2..2 lead 0 content 10 end 0", "2..3..3! lead -4 content 10 end 0" },
Words(text, Kerned, (1, 2, KernedWide)));
}
public void SoftHyphenInAnotherFont_LeavesTheKerningBetweenTheGlyphsAround()
{
string text = $"a{LineBreakIterator.SoftHyphen}b";
CheckEqual(new[] { "0..2..2 lead 0 content 10 end 0", "2..3..3! lead -1 content 10 end 0" },
Words(text, Kerned, (1, 2, KernedWide)));
}
public void HardBreak_StartsKerningOver()
{
CheckEqual(new[] { "0..1..2! lead 0 content 10 end 0", "2..3..3! lead 0 content 10 end 0" },
Words("b\nc", Kerned, (5, 6, KernedWide)));
}
public void SurrogatePair_IsMeasuredInTheFontAtItsFirstCodeUnit()
{
CheckEqual(new[] { "0..2..2! lead 0 content 9 end 0" },
Words(WordMeasurerTests.Emoji, Narrow, (0, 1, Wide)));
}
public void SurrogatePairInTheBaseFont_IsMeasuredInIt()
{
CheckEqual(new[] { "0..2..2! lead 0 content 7 end 0" },
Words(WordMeasurerTests.Emoji, Narrow, (1, 2, Wide)));
}
public void CharacterThatDrawsNothing_AddsNoWidth()
{
string text = $"a{LineBreakIterator.SoftHyphen}{WordMeasurerTests.LanguageTag}b";
CheckEqual(new[] { "0..4..4 lead 0 content 10 end 0", "4..5..5! lead 0 content 10 end 0" },
Words(text, Narrow, (7, 8, Wide)));
}
public void EmptyText_HasNoWords()
{
CheckEqual(0, Words("", Narrow, (0, 1, Wide)).Count);
}
public void ReplacesWhatWordsHeld()
{
var words = new DynamicArray();
words.Add(MeasuredWordsTests.WordEndingAt(7));
FacedMeasuring.Measure("ab", Runs(Narrow, (0, 1, Wide)), words);
CheckEqual(1, words.Count);
CheckEqual(24, words[0].ContentWidth);
}
}
public class HyphenFaceOfTests : TestFixture
{
public void IsTheFontTheSoftHyphenIsSetIn()
{
var pen = new FacedPen(Runs(Narrow, (1, 2, Wide)));
var lineBreak = new LineBreak(2, isMandatory: false, ending: LineBreakEnding.SoftHyphen);
CheckTrue(ReferenceEquals(Wide, FacedMeasuring.HyphenFaceOf(ref pen, lineBreak)));
}
public void ASoftHyphenInTheBaseFont_IsTheBaseFont()
{
var pen = new FacedPen(Runs(Narrow, (0, 1, Wide)));
var lineBreak = new LineBreak(2, isMandatory: false, ending: LineBreakEnding.SoftHyphen);
CheckTrue(ReferenceEquals(Narrow, FacedMeasuring.HyphenFaceOf(ref pen, lineBreak)));
}
}
public class HyphenWidthOfTests : TestFixture
{
private static readonly string SoftHyphenated = $"b{LineBreakIterator.SoftHyphen}c";
public void InTheSameFont_IsItsHyphenKernedAgainstTheContent()
{
FaceRuns faces = Runs(Kerned, (5, 6, KernedWide));
MeasuredWord word = FirstWord(SoftHyphenated, Kerned, (5, 6, KernedWide));
CheckEqual(2, FacedMeasuring.HyphenWidthOf(faces, in word));
}
public void InAnotherFont_IsThatFontsHyphenWithNoKerning()
{
FaceRuns faces = Runs(Kerned, (1, 2, KernedWide));
MeasuredWord word = FirstWord(SoftHyphenated, Kerned, (1, 2, KernedWide));
CheckEqual(5, FacedMeasuring.HyphenWidthOf(faces, in word));
}
public void WordNotEndingAtASoftHyphen_IsZero()
{
FaceRuns faces = Runs(Narrow, (5, 6, Wide));
MeasuredWord word = FirstWord("ab cd", Narrow, (5, 6, Wide));
CheckEqual(0, FacedMeasuring.HyphenWidthOf(faces, in word));
}
public void WordEndingTheText_IsZeroEvenAtASoftHyphen()
{
string text = $"b{LineBreakIterator.SoftHyphen}";
FaceRuns faces = Runs(Narrow, (5, 6, Wide));
MeasuredWord word = FirstWord(text, Narrow, (5, 6, Wide));
CheckEqual(0, FacedMeasuring.HyphenWidthOf(faces, in word));
}
}
public class WidthOfSpacesTests : TestFixture
{
public void NoSpaces_IsZero()
{
CheckEqual(0, FacedMeasuring.WidthOfSpaces(Runs(Kerned, (7, 8, KernedWide)), start: 1, count: 0,
firstKerning: 3));
}
public void SpacesInTheBaseFont_AreKernedBetweenThemselves()
{
CheckEqual(14, FacedMeasuring.WidthOfSpaces(Runs(Kerned, (7, 8, KernedWide)), start: 1, count: 3,
firstKerning: 0));
}
public void FirstKerning_IsAddedBeforeTheFirstSpace()
{
CheckEqual(19, FacedMeasuring.WidthOfSpaces(Runs(Kerned, (7, 8, KernedWide)), start: 1, count: 3,
firstKerning: 5));
}
public void OnlyTheFirstCountSpaces_AreWidened()
{
CheckEqual(9, FacedMeasuring.WidthOfSpaces(Runs(Kerned, (7, 8, KernedWide)), start: 1, count: 2,
firstKerning: 0));
}
public void EachSpace_IsInItsOwnFont()
{
CheckEqual(14, FacedMeasuring.WidthOfSpaces(Runs(Kerned, (2, 3, KernedWide)), start: 1, count: 3,
firstKerning: 0));
}
public void SpacesInAnotherFont_AreItsWidthAndKerning()
{
CheckEqual(6 + 2 + 6 + 2 + 6, FacedMeasuring.WidthOfSpaces(Runs(Kerned, (1, 4, KernedWide)), start: 1,
count: 3, firstKerning: 0));
}
}
public class SpacesThatFitTests : TestFixture
{
// Three spaces from index 1, all in the base font unless a test says otherwise.
private static int Fit(FaceRuns faces, int room, int available = 3, int firstKerning = 0)
{
return FacedMeasuring.SpacesThatFit(faces, start: 1, available, room, firstKerning);
}
private static FaceRuns AllInTheBaseFont => Runs(Kerned, (7, 8, KernedWide));
public void RoomForNone_IsZero()
{
CheckEqual(0, Fit(AllInTheBaseFont, room: 3));
}
public void RoomForOne_IsOne()
{
CheckEqual(1, Fit(AllInTheBaseFont, room: 8));
}
public void RoomExactlyForTwo_IsTwo()
{
CheckEqual(2, Fit(AllInTheBaseFont, room: 9));
}
public void RoomForAll_IsAllAvailable()
{
CheckEqual(3, Fit(AllInTheBaseFont, room: 100));
}
public void RoomForMoreThanAvailable_IsAvailable()
{
CheckEqual(2, Fit(AllInTheBaseFont, room: 100, available: 2));
}
public void FirstKerning_TakesRoomFromTheFirstSpace()
{
CheckEqual(2, Fit(AllInTheBaseFont, room: 7, firstKerning: -2));
CheckEqual(1, Fit(AllInTheBaseFont, room: 6, firstKerning: -2));
}
public void EachSpace_IsInItsOwnFont()
{
FaceRuns middleOneWide = Runs(Kerned, (2, 3, KernedWide));
CheckEqual(1, Fit(middleOneWide, room: 9));
CheckEqual(2, Fit(middleOneWide, room: 10));
}
public void ASpaceThatDoesNotAdvance_AlwaysFits()
{
MetricsFont flat = Face(letterWidth: 10, spaceWidth: 0);
CheckEqual(3, FacedMeasuring.SpacesThatFit(Runs(flat, (7, 8, Wide)), start: 1, available: 3, room: -10,
firstKerning: 0));
}
}
public class WidthOfMarkerTests : TestFixture
{
public void Ellipsis_IsItsWidthInTheFont()
{
CheckEqual(8, FacedMeasuring.WidthOfMarker(Wide, TruncationMarker.HorizontalEllipsis, rowBefore: 0));
}
public void Ellipsis_KernsAgainstTheGlyphBeforeIt()
{
int row = Kerned.MetricsTable['b'].LeftKerningRow;
CheckEqual(4, FacedMeasuring.WidthOfMarker(Kerned, TruncationMarker.HorizontalEllipsis, row));
}
public void ThreePeriods_AreEachTheirWidth()
{
CheckEqual(3 * LayoutFonts.PeriodWidth,
FacedMeasuring.WidthOfMarker(Narrow, TruncationMarker.ThreePeriods, rowBefore: 0));
}
}
public class MeasureCounterTests : TestFixture
{
private static CounterRun Counter(int start, int end, int room,
CounterAlignment alignment = CounterAlignment.Right)
{
return new CounterRun(start, end, room, alignment, zeroDigit: '0', digitWidth: 8);
}
private static FaceRuns RunsWith(IFontMetrics font, params CounterRun[] counters)
{
var returnMe = new FaceRuns(font);
foreach (CounterRun counter in counters)
{
returnMe.AddCounter(counter);
}
return returnMe;
}
private static List WordsOf(StringView text, IFontMetrics font, params CounterRun[] counters)
{
var measured = new MeasuredWords(font, text, RunsWith(font, counters));
var returnMe = new List();
while (measured.MoveNext())
{
returnMe.Add(Describe(measured.CurrentStart, measured.Current));
}
return returnMe;
}
private static readonly MetricsFont Digits = CounterBoxTests.FontOf(
CounterBoxTests.ProportionalDigits);
public void ACounterNarrowerThanItsRoom_TakesTheRoom()
{
CheckEqual(new[] { "0..2..2! lead 0 content 40 end 0" },
WordsOf("12", Digits, Counter(0, 2, room: 40, CounterAlignment.Left)));
}
public void ACounterWiderThanItsRoom_TakesItsOwnWidth()
{
CheckEqual(new[] { "0..2..2! lead 0 content 11 end 0" }, WordsOf("12", Digits, Counter(0, 2, room: 5)));
}
public void TheAlignment_DoesNotChangeTheRoomTaken()
{
CheckEqual(new[] { "0..2..2! lead 0 content 40 end 0" },
WordsOf("12", Digits, Counter(0, 2, room: 40, CounterAlignment.Right)));
}
public void DigitsCentered_AreMeasuredAWholeCellEach()
{
CheckEqual(new[] { "0..2..2! lead 0 content 16 end 0" },
WordsOf("12", Digits, Counter(0, 2, room: 0, CounterAlignment.Digit)));
}
public void DigitsCentered_StillTakeTheRoomWhenItIsWider()
{
CheckEqual(new[] { "0..2..2! lead 0 content 40 end 0" },
WordsOf("12", Digits, Counter(0, 2, room: 40, CounterAlignment.Digit)));
}
public void ACounterInsideAWord_AddsItsRoomToTheGlyphsAround()
{
CheckEqual(new[] { "0..6..6! lead 0 content 68 end 0" },
WordsOf("ab12cd", Digits, Counter(2, 4, room: 40)));
}
public void ACounterInALaterWord_IsFound()
{
CheckEqual(new[] { "0..2..3 lead 0 content 14 end 0", "3..5..5! lead 0 content 40 end 0" },
WordsOf("ab 12", Digits, Counter(3, 5, room: 40)));
}
public void TwoCountersInOneWord_EachTakeTheirRoom()
{
CheckEqual(new[] { "0..5..5! lead 0 content 82 end 0" },
WordsOf("12.34", Digits, Counter(0, 2, room: 40), Counter(3, 5, room: 40)));
}
public void TheKerningIntoACountersFirstGlyph_IsTheWordsLeadingKerning()
{
MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits,
kernings: new Dictionary<(short, short), short> { [Pair(' ', '1')] = -2 });
CheckEqual(new[] { "0..2..3 lead 0 content 14 end 0", "3..5..5! lead -2 content 40 end 0" },
WordsOf("ab 12", font, Counter(3, 5, room: 40)));
}
public void KerningBetweenGlyphsInsideACounter_Counts()
{
MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits,
kernings: new Dictionary<(short, short), short> { [Pair('1', '2')] = -3 });
CheckEqual(new[] { "0..2..2! lead 0 content 8 end 0" }, WordsOf("12", font, Counter(0, 2, room: 0)));
}
public void KerningAfterACounter_UsesItsLastGlyph()
{
MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits,
kernings: new Dictionary<(short, short), short> { [Pair('2', ',')] = -2 });
CheckEqual(new[] { "0..3..3! lead 0 content 41 end 0" },
WordsOf("12,", font, Counter(0, 2, room: 40)));
}
public void ACounterWithABreakInsideIt_IsOneWord()
{
CheckEqual(new[] { "0..5..5! lead 0 content 40 end 0" }, WordsOf("12 34", Digits, Counter(0, 5, 40)));
}
public void ACounterWithABreakInsideIt_LeavesTheWordsAfterIt()
{
CheckEqual(new[] { "0..5..6 lead 0 content 40 end 0", "6..8..8! lead 0 content 14 end 0" },
WordsOf("12 34 ab", Digits, Counter(0, 5, 40)));
}
public void ACounterWithABreakInsideIt_NaturallyWiderThanItsRoom_TakesItsOwnWidth()
{
CheckEqual(new[] { "0..5..5! lead 0 content 29 end 0" }, WordsOf("12 34", Digits, Counter(0, 5, 10)));
}
public void ACounterThatRunsPastAHardBreak_Throws()
{
CheckThrow(typeof(InvalidOperationException));
WordsOf("12\n34", Digits, Counter(0, 5, room: 40));
}
public void ACounterThatRunsPastTheText_Throws()
{
CheckThrow(typeof(InvalidOperationException));
WordsOf("12", Digits, Counter(0, 9, room: 40));
}
public void RunsWithOnlyACounter_AreKept()
{
FaceRuns faces = RunsWith(Digits, Counter(0, 2, room: 40));
CheckTrue(ReferenceEquals(faces, new MeasuredWords(Digits, "12", faces).Faces));
}
}
}
}