using System; using System.Collections.Generic; using System.Text; using Amphora; using Azimuth; using Tessera.Text; using Tessera.Typesetting; using UnitTestSharp; using LineBreakTestFile = Tessera.UnitTests.Unicode.LineBreakSamples.LineBreakTestFile; using Tessera.UnitTests.Unicode; using Tessera.Unicode; using static Tessera.UnitTests.Typesetting.LayoutFonts; namespace Tessera.UnitTests.Typesetting { public class GlyphPlacerTests : TestFixture { public const int Roomy = 1000; /// /// Letters 10 wide, a space 4, and lines apart. /// public static readonly MetricsFont Font = LayoutFonts.FontWithTenPixelLines; /// /// The glyph just placed as "index@x,y+advance". /// public static string Describe(in GlyphPlacer placer) { return $"{placer.Index}@{placer.X},{placer.Y}+{placer.Advance}"; } public static List StyleIndexes(StringView text, int containerWidth, IFontMetrics font = null, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null) { GlyphPlacer placer = Placer(text, containerWidth, font, overflow: overflow, maxLines: maxLines); var returnMe = new List(); while (placer.MoveNext()) { returnMe.Add(placer.StyleIndex); } return returnMe; } public static GlyphPlacer Placer(StringView text, int containerWidth, IFontMetrics font = null, Alignment.Horizontal alignment = Alignment.Horizontal.Left, int? maxHeight = null, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null, TrailingSpaceMode trailingSpaces = TrailingSpaceMode.Hang) { return new GlyphPlacer(font ?? Font, text, containerWidth, alignment, maxHeight, overflow, maxLines, trailingSpaces); } public static List Placements(StringView text, int containerWidth, IFontMetrics font = null, Alignment.Horizontal alignment = Alignment.Horizontal.Left, int? maxHeight = null, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null, TrailingSpaceMode trailingSpaces = TrailingSpaceMode.Hang) { GlyphPlacer placer = Placer(text, containerWidth, font, alignment, maxHeight, overflow, maxLines, trailingSpaces); var returnMe = new List(); while (placer.MoveNext()) { returnMe.Add(Describe(placer)); } return returnMe; } public static GlyphPlacer FacedPlacer(StringView text, int containerWidth, IFontMetrics baseFont, FaceRuns faces, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null) { return new GlyphPlacer(new MeasuredWords(baseFont, text, faces), containerWidth, Alignment.Horizontal.Left, maxHeight: null, overflow, maxLines); } public static List Drained(GlyphPlacer placer) { var returnMe = new List(); while (placer.MoveNext()) { returnMe.Add(Describe(placer)); } return returnMe; } public static List FacedPlacements(StringView text, int containerWidth, IFontMetrics baseFont, params (int start, int end, IFontMetrics font)[] runs) { return Drained(FacedPlacer(text, containerWidth, baseFont, FacedMeasuringTests.Runs(baseFont, runs))); } /// /// The placer once drained, described like a glyph. /// public static string End(StringView text, int containerWidth, IFontMetrics font = null, Alignment.Horizontal alignment = Alignment.Horizontal.Left, int? maxLines = null) { GlyphPlacer placer = Placer(text, containerWidth, font, alignment, maxLines: maxLines); while (placer.MoveNext()) { } return Describe(placer); } public const string Prose = "hello world, this wraps\nacross lines"; public static int Drain(string text, int containerWidth, TextOverflow overflow, int? maxLines = null, Alignment.Horizontal alignment = Alignment.Horizontal.Left, TrailingSpaceMode trailingSpaces = TrailingSpaceMode.Hang) { var placer = new GlyphPlacer(Font, text, containerWidth, alignment, maxHeight: null, overflow, maxLines, trailingSpaces); int count = 0; while (placer.MoveNext()) { ++count; } return count; } /// /// with wider glyphs and a taller line, to set some text in beside it. /// public static readonly MetricsFont OtherKernedFont = BuildKernedFont(widthOffset: 2, ascent: 11); /// /// Sets every other three characters of `text` in , which puts a face change /// inside most words. /// public static FaceRuns AlternatingFaces(string text) { var returnMe = new FaceRuns(KernedFont); for (int start = 0; start < text.Length; start += 6) { returnMe.Add(start, start + 3, OtherKernedFont); } return returnMe; } /// /// How 's pen disagrees with 's widths, or null if it /// doesn't. A line is as wide as the pen after its last non-space glyph, or after a space following it, since /// an emergency split can end a line inside content spaces rather than hanging ones. /// public static string WidthMismatch(string text, int maxWidth, TextOverflow overflow, int? maxLines, FaceRuns faces = null) { var words = new MeasuredWords(KernedFont, text, faces); var wrapper = default(LineWrapper); wrapper.Initialize(words, maxWidth, maxHeight: null, overflow, maxLines); var layout = new List(); while (wrapper.MoveNext()) { layout.Add(wrapper.Current); } var placer = new GlyphPlacer(words, maxWidth, Alignment.Horizontal.Left, maxHeight: null, overflow, maxLines); var candidates = new List[layout.Count]; for (int i = 0; i < candidates.Length; ++i) { candidates[i] = new List { 0 }; } while (placer.MoveNext()) { int line = placer.LineTop / LineSpacing; if (line >= layout.Count) { return $"a glyph on line {line}, past the layout's {layout.Count}"; } bool isSpace = placer.Index < layout[line].End && text[placer.Index] == ' '; if (!isSpace) { candidates[line].Clear(); } candidates[line].Add(placer.X + placer.Advance); } if (placer.LineCount != layout.Count) { return $"{placer.LineCount} lines placed, {layout.Count} laid out"; } for (int i = 0; i < layout.Count; ++i) { if (!candidates[i].Contains(layout[i].VisibleWidth)) { return $"line {i} is {layout[i].VisibleWidth} wide, pens {string.Join(",", candidates[i])}"; } } return null; } public class CtorTests : TestFixture { public void NullFont_Throws() { CheckThrow(typeof(ArgumentNullException)); new GlyphPlacer(null, "a", Roomy, Alignment.Horizontal.Left); } public void InfiniteWidth_NeverWraps() { CheckEqual(new[] { "0@0,0+10", "1@10,0+4", "2@14,0+10" }, Placements("a b", int.MaxValue)); } public void MeasuredWords_PlaceLikeMeasuringTheText() { var text = new StringView("x" + Prose, 1, Prose.Length + 1); var placer = new GlyphPlacer(new MeasuredWords(Font, text), containerWidth: 60, Alignment.Horizontal.Center, maxHeight: null, TextOverflow.Ellipsis, maxLines: 2); var placements = new List(); while (placer.MoveNext()) { placements.Add(Describe(placer)); } CheckEqual(Placements(text, containerWidth: 60, alignment: Alignment.Horizontal.Center, overflow: TextOverflow.Ellipsis, maxLines: 2), placements); } } public class MoveNextTests : TestFixture { public void EmptyText_PlacesNothing() { CheckEqual(new string[0], Placements("", Roomy)); } public void EachGlyph_SitsWhereTheKerningAndTheOneBeforeLeaveThePen() { CheckEqual(new[] { "0@0,0+10", "1@9,0+10" }, Placements("ab", Roomy, WordMeasurerTests.SharedFont)); } public void Spaces_ArePlacedLikeAnyGlyph_EvenHangingOnes() { CheckEqual(new[] { "0@0,0+10", "1@8,0+4", "2@9,0+10", "3@19,0+4" }, Placements("b c ", Roomy, WordMeasurerTests.SharedFont)); } public void WrappedLines_StackDownLineByLine() { CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "2@20,0+4", "3@0,10+10", "4@10,10+10" }, Placements("ab cd", 25)); } public void LineStart_CarriesNoKerningIn() { CheckEqual(new[] { "0@0,0+10", "1@8,0+4", "2@0,0+10" }, Placements("b c", 12, WordMeasurerTests.SharedFont)); } public void LineFeed_IsNotPlaced() { CheckEqual(new[] { "0@0,0+10", "2@0,10+10" }, Placements("a\nb", Roomy)); } public void CarriageReturnLineFeed_IsNotPlaced() { CheckEqual(new[] { "0@0,0+10", "3@0,10+10" }, Placements("a\r\nb", Roomy)); } public void OtherHardBreak_IsNotPlaced() { CheckEqual(new[] { "0@0,0+10", "2@0,10+10" }, Placements($"a{(char)0x85}b", Roomy)); } public void SoftHyphenNotTaken_IsNotPlaced_AndKerningCarriesAcrossIt() { CheckEqual(new[] { "0@0,0+10", "2@5,0+10" }, Placements($"a{LineBreakIterator.SoftHyphen}c", Roomy, WordMeasurerTests.SharedFont)); } public void SoftHyphenTaken_PlacesAKernedHyphenAtTheLinesEnd() { CheckEqual(new[] { "0@0,0+10", "2@9,0+3", "2@0,0+10" }, Placements( $"b{LineBreakIterator.SoftHyphen}c", 12, WordMeasurerTests.SharedFont)); } public void AstralCharacter_IsOneGlyph() { CheckEqual(new[] { "0@0,0+10", "1@10,0+7", "3@17,0+10" }, Placements($"a{WordMeasurerTests.Emoji}b", Roomy)); } public void InvisibleCharacter_IsNotPlaced() { CheckEqual(new[] { "0@0,0+10", "3@10,0+10" }, Placements($"a{WordMeasurerTests.LanguageTag}b", Roomy)); } public void EllipsisTruncatedLine_PlacesTheMarkerAfterTheKeptText() { CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "2@20,0+5" }, Placements("aaa bbb", 34, overflow: TextOverflow.Ellipsis, maxLines: 1)); } public void Marker_KernsAgainstTheLastKeptGlyph() { MetricsFont font = LayoutFonts.FontWithLineHeight( kernings: new Dictionary<(short, short), short> { [LayoutFonts.Pair('b', TruncationMarker.EllipsisGlyph)] = -1, }); CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "2@19,0+5" }, Placements("ab\nc", Roomy, font, overflow: TextOverflow.Ellipsis, maxLines: 1)); } public void ThreePeriodMarker_PlacesEveryPeriod() { CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "2@20,0+2", "2@22,0+2", "2@24,0+2" }, Placements("ab\ncd", Roomy, LayoutFonts.FontWithLineHeight(withEllipsis: false), overflow: TextOverflow.Ellipsis, maxLines: 1)); } private static readonly MetricsFont Narrow = FacedMeasuringTests.Narrow; private static readonly MetricsFont Wide = FacedMeasuringTests.Wide; private static readonly MetricsFont Tall = FacedMeasuringTests.Tall; private static readonly MetricsFont Short = FacedMeasuringTests.Short; public void FacedGlyphs_SitWhereTheirOwnWidthsLeaveThePen() { CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "2@20,0+4", "3@24,0+14", "4@38,0+14" }, FacedPlacements("ab cd", Roomy, Narrow, (3, 5, Wide))); } public void FacedSpace_IsPlacedInItsOwnFont() { CheckEqual(new[] { "0@0,0+10", "1@10,0+4", "2@14,0+6", "3@20,0+10" }, FacedPlacements("a b", Roomy, Narrow, (2, 3, Wide))); } public void FaceWithMoreAscent_StartsHigherSoTheBaselinesLineUp() { CheckEqual(new[] { "0@0,0+10", "1@10,-4+10" }, FacedPlacements("ab", Roomy, Narrow, (1, 2, Tall))); } public void FaceWithLessAscent_StartsLower() { CheckEqual(new[] { "0@0,0+10", "1@10,3+10" }, FacedPlacements("ab", Roomy, Narrow, (1, 2, Short))); } public void FaceOnALaterLine_IsAlignedToThatLinesBaseline() { CheckEqual(new[] { "0@0,0+10", "2@0,6+10" }, FacedPlacements("a\nb", Roomy, Narrow, (2, 3, Tall))); } public void WrappedLines_StackByTheBaseFontsSpacingWhateverFacesAreOnThem() { CheckEqual(new[] { "0@0,0+10", "1@10,0+4", "2@0,6+10" }, FacedPlacements("a b", 14, Narrow, (2, 3, Tall))); } public void KerningAcrossAFaceChange_IsNone() { CheckEqual(new[] { "0@0,0+10", "1@10,0+14" }, FacedPlacements("bc", Roomy, FacedMeasuringTests.Kerned, (1, 2, FacedMeasuringTests.KernedWide))); } public void KerningWithinTheBaseFont_IsItsOwn() { CheckEqual(new[] { "0@0,0+10", "1@6,0+10" }, FacedPlacements("bc", Roomy, FacedMeasuringTests.Kerned, (5, 6, FacedMeasuringTests.KernedWide))); } public void KerningInsideARun_UsesThatFontsTable() { CheckEqual(new[] { "0@0,0+14", "1@11,0+14" }, FacedPlacements("ab", Roomy, FacedMeasuringTests.Kerned, (0, 2, FacedMeasuringTests.KernedWide))); } public void LineStart_CarriesNoKerningIntoAFacedGlyph() { CheckEqual(new[] { "0@0,0+10", "1@8,0+4", "2@0,10+14" }, FacedPlacements("b c", 12, FacedMeasuringTests.Kerned, (2, 3, FacedMeasuringTests.KernedWide))); } public void FacedHyphen_IsPlacedInTheFontOfTheSoftHyphen() { string text = $"ab{LineBreakIterator.SoftHyphen}cd"; CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "3@20,0+5", "3@0,10+10", "4@10,10+10" }, FacedPlacements(text, 28, Narrow, (2, 3, Wide))); } public void HyphenInTheBaseFont_IsPlacedInIt() { string text = $"ab{LineBreakIterator.SoftHyphen}cd"; CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "3@20,0+3", "3@0,10+14", "4@14,10+14" }, FacedPlacements(text, 28, Narrow, (3, 5, Wide))); } public void FacedEllipsis_IsPlacedInTheFontOfTheLastKeptGlyph() { CheckEqual(new[] { "0@0,0+14", "1@14,0+8" }, Drained(FacedPlacer("aaa bbb", 34, Narrow, FacedMeasuringTests.Runs(Narrow, (0, 3, Wide)), TextOverflow.Ellipsis, maxLines: 1))); } public void EllipsisThatKeepsNothing_IsPlacedInTheFontOfTheTextItReplaces() { CheckEqual(new[] { "0@0,0+8" }, Drained(FacedPlacer("bbb", 6, Narrow, FacedMeasuringTests.Runs(Narrow, (0, 3, Wide)), TextOverflow.Ellipsis, maxLines: 1))); } public void FacedEllipsis_SitsOnTheBaselineOfItsFont() { CheckEqual(new[] { "0@0,-4+10", "1@10,-4+5" }, Drained(FacedPlacer("aaa bbb", 15, Narrow, FacedMeasuringTests.Runs(Narrow, (0, 3, Tall)), TextOverflow.Ellipsis, maxLines: 1))); } public void FacedWordWiderThanTheLine_SplitsByEachGlyphsWidth() { CheckEqual(new[] { "0@0,0+10", "1@10,0+10", "2@0,10+14", "3@14,10+14", "4@0,20+10", "5@10,20+10" }, FacedPlacements("abcdef", 30, Narrow, (2, 4, Wide))); } public void CharacterThatDrawsNothing_LeavesTheFontAlone() { string text = $"b{LineBreakIterator.SoftHyphen}c"; CheckEqual(new[] { "0@0,0+10", "2@6,0+10" }, FacedPlacements(text, Roomy, FacedMeasuringTests.Kerned, (1, 2, FacedMeasuringTests.KernedWide))); } public void FacedCenteredLine_IsAlignedByItsFacedWidth() { GlyphPlacer placer = new GlyphPlacer(new MeasuredWords(Narrow, "ab", FacedMeasuringTests.Runs(Narrow, (1, 2, Wide))), containerWidth: 40, Alignment.Horizontal.Center); CheckEqual(new[] { "0@8,0+10", "1@18,0+14" }, Drained(placer)); } public void RightAligned_EachLineAlignsByItsOwnWidth() { CheckEqual(new[] { "0@90,0+10", "2@70,10+10", "3@80,10+10", "4@90,10+10" }, Placements("a\nbbb", 100, alignment: Alignment.Horizontal.Right)); } public void Centered_CanLandOnAHalfPixel() { CheckEqual(new[] { "0@45,0+10" }, Placements("a", 101, alignment: Alignment.Horizontal.Center)); } public void WrapMode_SpacesShiftTheAlignment() { CheckEqual(new[] { "0@86,0+10", "1@96,0+4" }, Placements("a ", 100, alignment: Alignment.Horizontal.Right, trailingSpaces: TrailingSpaceMode.Wrap)); } public void HangingSpaces_DoNotShiftTheAlignment() { CheckEqual(new[] { "0@90,0+10", "1@100,0+4" }, Placements("a ", 100, alignment: Alignment.Horizontal.Right)); } public void LineWiderThanTheContainer_AnchorsLeftWhateverTheAlignment() { CheckEqual(new[] { "0@0,0+10" }, Placements("a", 5, alignment: Alignment.Horizontal.Right)); CheckEqual(new[] { "0@0,0+10" }, Placements("a", 5, alignment: Alignment.Horizontal.Center)); } public void InvalidAlignment_Throws() { CheckThrow(typeof(InvalidOperationException)); Placements("a", Roomy, alignment: (Alignment.Horizontal)999); } public void Drained_StaysDrained() { GlyphPlacer placer = Placer("a", Roomy); placer.MoveNext(); placer.MoveNext(); CheckFalse(placer.MoveNext()); } public void ViewIntoALongerString_PlacesLikeTheStandaloneString() { const string text = "ab cd\nef"; var view = new StringView("xx " + text + " yy", 3, 3 + text.Length); CheckEqual(Placements(text, 25), Placements(view, 25)); } [IgnoreTest] public void CheckPenMatchesLayout(string text, int maxWidth, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null) { CheckNull(WidthMismatch(text, maxWidth, overflow, maxLines)); } public void SoftWrappedWords_PenMatchesLayout() { CheckPenMatchesLayout("ab ab ab ab", maxWidth: 30); } public void HardBreaks_PenMatchesLayout() { CheckPenMatchesLayout("ab\n\nba.\n", maxWidth: 60); } public void SoftHyphenBreak_PenMatchesLayout() { CheckPenMatchesLayout($"aaa{LineBreakIterator.SoftHyphen}bbb", maxWidth: 25); } public void IdeographsBreakAnywhere_PenMatchesLayout() { CheckPenMatchesLayout(new string((char)0x4E00, 5), maxWidth: 25); } public void EmergencySplitInsideContentSpaces_PenMatchesLayout() { CheckPenMatchesLayout("a b", maxWidth: 12); } public void LongWordWithKerning_PenMatchesLayout() { CheckPenMatchesLayout("(ab).-ab).-ab", maxWidth: 20); } public void ClippedByMaxLines_PenMatchesLayout() { CheckPenMatchesLayout("ab ab ab ab ab", maxWidth: 20, maxLines: 2); } public void EllipsisByMaxLines_PenMatchesLayout() { CheckPenMatchesLayout("ab ab ab ab ab", maxWidth: 20, TextOverflow.Ellipsis, maxLines: 2); } public void EllipsisOnASingleLongLine_PenMatchesLayout() { CheckPenMatchesLayout("ab.ab.ab.ab.ab.", maxWidth: 40, TextOverflow.Ellipsis, maxLines: 1); } public void LeadingSoftHyphen_PenMatchesLayout() { CheckPenMatchesLayout($"{LineBreakIterator.SoftHyphen}ab", maxWidth: 30); } public void FreshLineStartingWithASoftHyphen_PenMatchesLayout() { CheckPenMatchesLayout($"ab {LineBreakIterator.SoftHyphen}ab", maxWidth: 25); CheckPenMatchesLayout($"ab ab{LineBreakIterator.SoftHyphen} ab", maxWidth: 25); } public void LeadingSoftHyphenBeforeAWordWiderThanTheLine_PenMatchesLayout() { CheckPenMatchesLayout($"{LineBreakIterator.SoftHyphen}(a", maxWidth: 5); CheckPenMatchesLayout($"{LineBreakIterator.SoftHyphen}{(char)0x4E00}", maxWidth: 5); } public void OnlySoftHyphen_PenMatchesLayout() { CheckPenMatchesLayout($"{LineBreakIterator.SoftHyphen}", maxWidth: 30); } public void SoftHyphenAloneOnALine_PenMatchesLayout() { CheckPenMatchesLayout($"ab\n{LineBreakIterator.SoftHyphen}\nab", maxWidth: 30); } public void LeadingZeroWidthSpace_PenMatchesLayout() { CheckPenMatchesLayout($"{(char)0x200B}ab", maxWidth: 30); CheckPenMatchesLayout($"ab\n{(char)0x200B}ab", maxWidth: 30); } public void EllipsisWithNoRoomForAnyText_PenMatchesLayout() { CheckPenMatchesLayout("ab ab", maxWidth: 5, TextOverflow.Ellipsis, maxLines: 1); } public void OnlyLineFeeds_PenMatchesLayout() { CheckPenMatchesLayout("\n\n", maxWidth: 30); CheckPenMatchesLayout("\n\n", maxWidth: 30, TextOverflow.Ellipsis, maxLines: 1); } public void EmptyText_PenMatchesLayout() { CheckPenMatchesLayout("", maxWidth: 20, TextOverflow.Ellipsis, maxLines: 1); } private static string FacedWidthMismatchOf(string text, int maxWidth, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null) { return WidthMismatch(text, maxWidth, overflow, maxLines, AlternatingFaces(text)); } public void FacedText_PenMatchesLayout() { var mismatches = new List(); foreach ((string text, int maxWidth, TextOverflow overflow, int? maxLines) in new[] { ("ab ab ab ab", 30, TextOverflow.Clip, (int?)null), ("ab\n\nba.\n", 60, TextOverflow.Clip, null), ($"aaa{LineBreakIterator.SoftHyphen}bbb", 25, TextOverflow.Clip, null), ("a b", 12, TextOverflow.Clip, null), ("(ab).-ab).-ab", 20, TextOverflow.Clip, null), ("ab ab ab ab ab", 20, TextOverflow.Clip, 2), ("ab ab ab ab ab", 20, TextOverflow.Ellipsis, 2), ("ab.ab.ab.ab.ab.", 40, TextOverflow.Ellipsis, 1), ($"{LineBreakIterator.SoftHyphen}ab", 30, TextOverflow.Clip, null), ($"ab {LineBreakIterator.SoftHyphen}ab", 25, TextOverflow.Clip, null), ($"ab\n{LineBreakIterator.SoftHyphen}\nab", 30, TextOverflow.Clip, null), ("ab ab", 5, TextOverflow.Ellipsis, 1), (Prose, 60, TextOverflow.Clip, null), (Prose, 60, TextOverflow.Ellipsis, 2), }) { string mismatch = FacedWidthMismatchOf(text, maxWidth, overflow, maxLines); if (mismatch != null) { mismatches.Add($"[{text.Replace("\n", "\\n")}] at {maxWidth}: {mismatch}"); } } CheckEqual("", string.Join("; ", mismatches)); } // Mismatches are collected rather than checked one by one, since the file holds thousands of texts. public void FacedText_PenMatchesLayoutWhereItDoesForPlainText_OnEveryLineBreakTestShape() { var mismatches = new List(); foreach (BoundaryTestFile.Case testCase in LineBreakTestFile.Shapes) { foreach (int width in new[] { 12, 40 }) { if (WidthMismatch(testCase.Text, width, TextOverflow.Clip, null) == null && FacedWidthMismatchOf(testCase.Text, width) != null && mismatches.Count < 10) { mismatches.Add($"[{LineBreakSamples.CodePointsOf(testCase.Text)}] at {width}"); } } } CheckEqual("", string.Join("; ", mismatches)); } public void FacedGlyphsInACopyOfTheFont_PlaceLikeThePlainGlyphs_OnEveryLineBreakTestShape() { IFontMetrics copy = BuildKernedFont(); var mismatches = new List(); foreach (BoundaryTestFile.Case testCase in LineBreakTestFile.Shapes) { var faces = new FaceRuns(KernedFont); faces.Add(0, testCase.Text.Length, copy); foreach (int width in new[] { 12, 40 }) { string plain = string.Join("|", Placements(testCase.Text, width, KernedFont)); string faced = string.Join("|", Drained(FacedPlacer(testCase.Text, width, KernedFont, faces))); if (plain != faced && mismatches.Count < 10) { mismatches.Add($"[{LineBreakSamples.CodePointsOf(testCase.Text)}] at {width}"); } } } CheckEqual("", string.Join("; ", mismatches)); } [AllocationTest] public void Wrapping_AllocatesNothing() { int count = Drain(Prose, 60, TextOverflow.Clip); CheckNoAllocations(); CheckEqual(Prose.Length - 1, count); } [AllocationTest] public void EllipsisTruncated_AllocatesNothing() { int count = Drain(Prose, 60, TextOverflow.Ellipsis, maxLines: 1); CheckNoAllocations(); CheckEqual("hello".Length + 1, count); } [AllocationTest] public void Centered_AllocatesNothing() { int count = Drain(Prose, 60, TextOverflow.Clip, alignment: Alignment.Horizontal.Center); CheckNoAllocations(); CheckEqual(Prose.Length - 1, count); } [AllocationTest] public void EmergencySplit_AllocatesNothing() { int count = Drain("abcdefgh", 25, TextOverflow.Clip); CheckNoAllocations(); CheckEqual(8, count); } [AllocationTest] public void WrapModeSpill_AllocatesNothing() { int count = Drain("a b", 25, TextOverflow.Clip, trailingSpaces: TrailingSpaceMode.Wrap); CheckNoAllocations(); CheckEqual(12, count); } /// /// Built once, since building it mid-test would count against an allocation test. /// public static readonly MeasuredWords AllocationWords = new MeasuredWords(Font, Prose); [AllocationTest] public void FromMeasuredWords_AllocatesNothing() { var placer = new GlyphPlacer(AllocationWords, containerWidth: 60, Alignment.Horizontal.Left); int count = 0; while (placer.MoveNext()) { ++count; } CheckNoAllocations(); CheckEqual(Prose.Length - 1, count); } } public class StyleIndexTests : TestFixture { public void Glyph_IsItsOwnCharacter() { CheckEqual(new[] { 0, 1, 2, 3 }, StyleIndexes("ab c", Roomy)); } public void TruncationMarker_IsTheLastKeptGlyph() { CheckEqual(new[] { 0, 1, 1 }, StyleIndexes("aaa bbb", 34, overflow: TextOverflow.Ellipsis, maxLines: 1)); } public void EveryGlyphOfAMultiGlyphMarker_IsTheLastKeptGlyph() { CheckEqual(new[] { 0, 1, 1, 1, 1 }, StyleIndexes("ab\ncd", Roomy, LayoutFonts.FontWithLineHeight(withEllipsis: false), TextOverflow.Ellipsis, maxLines: 1)); } public void TruncationMarkerOnALineThatKeptNothing_IsWhereTheLineEnds() { CheckEqual(new[] { 0, 1, 3 }, StyleIndexes("ab\n\ncd", Roomy, overflow: TextOverflow.Ellipsis, maxLines: 2)); } public void SoftHyphen_IsTheLastKeptGlyph() { CheckEqual(new[] { 0, 0, 2 }, StyleIndexes($"b{LineBreakIterator.SoftHyphen}c", 12, WordMeasurerTests.SharedFont)); } public void FacedTruncationMarker_IsTheLastKeptGlyph() { GlyphPlacer placer = FacedPlacer("aaa bbb", 34, FacedMeasuringTests.Narrow, FacedMeasuringTests.Runs(FacedMeasuringTests.Narrow, (0, 3, FacedMeasuringTests.Wide)), TextOverflow.Ellipsis, maxLines: 1); var returnMe = new List(); while (placer.MoveNext()) { returnMe.Add(placer.StyleIndex); } CheckEqual(new[] { 0, 0 }, returnMe); } public void PastTheLastGlyph_MatchesIndex() { GlyphPlacer placer = Placer("ab\nc", Roomy, overflow: TextOverflow.Ellipsis, maxLines: 1); while (placer.MoveNext()) { } CheckEqual(placer.Index, placer.StyleIndex); } } public class LineTopTests : TestFixture { private static List YAndLineTop(GlyphPlacer placer) { var returnMe = new List(); while (placer.MoveNext()) { returnMe.Add($"{placer.Y}/{placer.LineTop}"); } return returnMe; } public void PlainText_IsEachGlyphsY() { CheckEqual(new[] { "0/0", "0/0", "10/10", "10/10" }, YAndLineTop(Placer("ab\ncd", Roomy))); } public void FacedGlyphs_ShareTheirLinesTopWhateverTheirY() { FaceRuns faces = FacedMeasuringTests.Runs(FacedMeasuringTests.Narrow, (1, 2, FacedMeasuringTests.Tall), (4, 5, FacedMeasuringTests.Short)); CheckEqual(new[] { "0/0", "-4/0", "10/10", "13/10" }, YAndLineTop(FacedPlacer("ab\ncd", Roomy, FacedMeasuringTests.Narrow, faces))); } public void AfterTheLastGlyph_IsTheLastLinesTop() { GlyphPlacer placer = Placer("ab\ncd", Roomy); while (placer.MoveNext()) { } CheckEqual(10, placer.LineTop); } } public class XAndYTests : TestFixture { public void Drained_AfterTheLastGlyph_IsThePen() { CheckEqual("2@20,0+0", End("ab", Roomy)); } public void Drained_AfterHangingSpaces_IsPastThem() { CheckEqual("4@28,0+0", End("ab ", Roomy)); } public void Drained_AfterATrailingHardBreak_IsTheNextLinesStart() { CheckEqual("3@0,10+0", End("ab\n", Roomy)); } public void Drained_OnEmptyText_IsTheFirstLinesStart() { CheckEqual("0@0,0+0", End("", Roomy)); } public void Drained_OnEmptyText_HonorsTheAlignment() { CheckEqual("0@100,0+0", End("", 100, alignment: Alignment.Horizontal.Right)); } public void Drained_TruncatedAtAHardBreak_StaysOnTheLastLine() { CheckEqual("2@10,0+0", End("a\nb", Roomy, maxLines: 1)); } } public class GlyphTests : TestFixture { public void IsTheFontsOwnMetrics() { MetricsFont font = Font; GlyphPlacer placer = Placer("a", Roomy, font); placer.MoveNext(); CheckTrue(ReferenceEquals(font.MetricsTable['a'], placer.Glyph)); } public void Drained_IsNull() { GlyphPlacer placer = Placer("a", Roomy); placer.MoveNext(); placer.MoveNext(); CheckNull(placer.Glyph); } } public class LineCountTests : TestFixture { public void CountsTheLinesPlaced() { GlyphPlacer placer = Placer("a\nb\nc", Roomy); while (placer.MoveNext()) { } CheckEqual(3, placer.LineCount); } } public class HeightTests : TestFixture { public void StacksTheLinesPlaced() { GlyphPlacer placer = Placer("a\nb\nc", Roomy); while (placer.MoveNext()) { } CheckEqual(30, placer.Height); } } public class TruncationTests : TestFixture { public void TextThatFits_IsNull() { GlyphPlacer placer = Placer("ab", Roomy); while (placer.MoveNext()) { } CheckNull(placer.Truncation); } public void TextCutShort_SaysHow() { GlyphPlacer placer = Placer("aaa bbb", 34, overflow: TextOverflow.Ellipsis, maxLines: 1); while (placer.MoveNext()) { } CheckEqual(TextOverflow.Ellipsis, placer.Truncation); } } public class VerticalOffsetForTests : TestFixture { public void Top_IsAlwaysZero() { CheckEqual(0, GlyphPlacer.VerticalOffsetFor(Alignment.Vertical.Top, 200, 80)); } public void Bottom_SitsTheBlockFlushWithTheBottom() { CheckEqual(120, GlyphPlacer.VerticalOffsetFor(Alignment.Vertical.Bottom, 200, 80)); } public void Center_SplitsTheRemainingSpaceInHalf() { CheckEqual(60, GlyphPlacer.VerticalOffsetFor(Alignment.Vertical.Center, 200, 80)); } public void Center_OddRemainder_RoundsDownToAWholePixel() { CheckEqual(60, GlyphPlacer.VerticalOffsetFor(Alignment.Vertical.Center, 201, 80)); } public void Bottom_BlockTallerThanTheContainer_ClampsToZero() { CheckEqual(0, GlyphPlacer.VerticalOffsetFor(Alignment.Vertical.Bottom, 200, 260)); } public void Center_BlockTallerThanTheContainer_ClampsToZero() { CheckEqual(0, GlyphPlacer.VerticalOffsetFor(Alignment.Vertical.Center, 200, 260)); } public void InvalidAlignment_Throws() { CheckThrow(typeof(InvalidOperationException)); GlyphPlacer.VerticalOffsetFor((Alignment.Vertical)999, 200, 80); } } public class CounterTests : TestFixture { private static readonly MetricsFont Digits = CounterBoxTests.FontOf( CounterBoxTests.ProportionalDigits); private static CounterRun Counter(int start, int end, CounterAlignment alignment, int room = 40) { return new CounterRun(start, end, room, alignment, zeroDigit: '0', digitWidth: 8); } private static List Placed(StringView text, int containerWidth, MetricsFont font, params CounterRun[] counters) { var faces = new FaceRuns(font); foreach (CounterRun counter in counters) { faces.AddCounter(counter); } return Drained(FacedPlacer(text, containerWidth, font, faces)); } public void Right_PutsTheSpareRoomBeforeTheGlyphs() { CheckEqual(new[] { "0@29,0+4", "1@33,0+7", "2@40,0+7" }, Placed("12a", Roomy, Digits, Counter(0, 2, CounterAlignment.Right))); } public void Left_PutsTheSpareRoomAfterTheGlyphs() { CheckEqual(new[] { "0@0,0+4", "1@4,0+7", "2@40,0+7" }, Placed("12a", Roomy, Digits, Counter(0, 2, CounterAlignment.Left))); } public void Digit_CentersEachDigitInAWholeCellAndPutsTheSpareRoomBefore() { CheckEqual(new[] { "0@26,0+8", "1@32,0+8", "2@40,0+7" }, Placed("12a", Roomy, Digits, Counter(0, 2, CounterAlignment.Digit))); } public void Digit_LeavesASeparatorItsOwnWidth() { CheckEqual(new[] { "0@2,0+8", "1@8,0+3", "2@11,0+8" }, Placed("1,2", Roomy, Digits, Counter(0, 3, CounterAlignment.Digit, room: 0))); } public void ACounterWiderThanItsRoom_HasNoSpareRoom() { CheckEqual(new[] { "0@0,0+4", "1@4,0+7", "2@11,0+7" }, Placed("12a", Roomy, Digits, Counter(0, 2, CounterAlignment.Right, room: 5))); } public void ACounterInTheMiddleOfAWord_ShiftsOnlyItsOwnGlyphsAndWhatFollows() { CheckEqual(new[] { "0@0,0+7", "1@36,0+4", "2@40,0+7", "3@47,0+7" }, Placed("a12b", Roomy, Digits, Counter(1, 3, CounterAlignment.Right))); } public void TwoCounters_EachTakeTheirRoom() { CheckEqual(new[] { "0@29,0+4", "1@33,0+7", "2@40,0+4", "3@70,0+7", "4@77,0+7" }, Placed("12 34", Roomy, Digits, Counter(0, 2, CounterAlignment.Right), Counter(3, 5, CounterAlignment.Right))); } public void KerningIntoTheFirstGlyph_IsKeptInsideTheRoom() { MetricsFont font = CounterBoxTests.FontOf(CounterBoxTests.ProportionalDigits, kernings: new Dictionary<(short, short), short> { [LayoutFonts.Pair(' ', '1')] = -2 }); CheckEqual(new[] { "0@0,0+7", "1@7,0+4", "2@38,0+4", "3@42,0+7" }, Placed("a 12", Roomy, font, Counter(2, 4, CounterAlignment.Right))); } public void ACounterThatWraps_TakesItsRoomOnItsOwnLine() { CheckEqual(new[] { "0@0,0+7", "1@7,0+7", "2@14,0+4", "3@29,10+4", "4@33,10+7" }, Placed("ab 12", 45, Digits, Counter(3, 5, CounterAlignment.Right))); } public void ACounterWithARoomWiderThanTheContainer_IsPlacedWhereItsGlyphsFall() { CheckEqual(new[] { "0@0,0+4", "1@4,0+7" }, Placed("12", 20, Digits, Counter(0, 2, CounterAlignment.Right))); } public void ACounterTheWrapperSplitAcrossLines_IsPlacedAsPlainText() { CheckEqual(new[] { "0@0,0+4", "1@4,0+7", "2@11,0+7", "3@18,0+7", "4@0,10+7", "5@7,10+7", "6@14,10+6", "7@20,10+7", "8@0,20+7" }, Placed("123456789", 30, Digits, Counter(0, 9, CounterAlignment.Right, room: 10))); } public void TextAlignment_CentersTheCounterWithItsRoom() { var faces = new FaceRuns(Digits); faces.AddCounter(Counter(0, 2, CounterAlignment.Right)); var placer = new GlyphPlacer(new MeasuredWords(Digits, "12", faces), containerWidth: 100, Alignment.Horizontal.Center, maxHeight: null, TextOverflow.Clip, maxLines: null); CheckEqual(new[] { "0@59,0+4", "1@63,0+7" }, Drained(placer)); } } } }