using System;
using System.Collections.Generic;
using Amphora;
using Azimuth;
using Tessera.Typesetting;
using UnitTestSharp;
using Tessera.Unicode;
using static Tessera.UnitTests.Typesetting.LayoutFonts;
namespace Tessera.UnitTests.Typesetting
{
public partial class LineWrapperTests : TestFixture
{
///
/// Letters and a space, all with zero line height, so height/maxLines cutoffs never trigger unless a test
/// opts into instead.
///
public static readonly MetricsFont Font = WordMeasurerTests.SharedFont;
// Line carries no Start of its own (like MeasuredWord), so the caller tracks it: it's wherever the line
// before it ended.
public static string Describe(int start, in Line line)
{
string flags = line.Flags == LineFlags.None ? "" : $" {line.Flags}";
return $"{start}..{line.End} width {line.VisibleWidth}{flags}";
}
public static LineWrapper Wrap(StringView text, int maxWidth, IFontMetrics font = null,
int? maxHeight = null, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null,
TrailingSpaceMode trailingSpaces = TrailingSpaceMode.Hang)
{
var words = new WordMeasurer(font ?? Font, new LineBreakIterator(text));
return new LineWrapper(words, maxWidth, maxHeight, overflow, maxLines, trailingSpaces);
}
public static List Lines(StringView text, int maxWidth, IFontMetrics font = null,
int? maxHeight = null, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null,
TrailingSpaceMode trailingSpaces = TrailingSpaceMode.Hang)
{
return Lines(Wrap(text, maxWidth, font, maxHeight, overflow, maxLines, trailingSpaces));
}
public static List Lines(LineWrapper wrapper)
{
var returnMe = new List();
int start = 0;
while (wrapper.MoveNext())
{
returnMe.Add(Describe(start, wrapper.Current));
start = wrapper.Current.End;
}
return returnMe;
}
public static TextOverflow? TruncationOf(StringView text, int maxWidth, IFontMetrics font = null,
int? maxHeight = null, TextOverflow overflow = TextOverflow.Clip, int? maxLines = null,
TrailingSpaceMode trailingSpaces = TrailingSpaceMode.Hang)
{
LineWrapper wrapper = Wrap(text, maxWidth, font, maxHeight, overflow, maxLines, trailingSpaces);
while (wrapper.MoveNext())
{
}
return wrapper.Truncation;
}
public class CtorTests : TestFixture
{
public void MaxLinesLessThanOne_Throws()
{
CheckThrow(typeof(ArgumentOutOfRangeException));
new LineWrapper(new WordMeasurer(Font, new LineBreakIterator("a")), maxWidth: 100, maxLines: 0);
}
public void ExposesItsWordMeasurersFontAndText()
{
MetricsFont font = Font;
const string backing = "xab cd";
LineWrapper wrapper = Wrap(new StringView(backing, 1, 6), maxWidth: 100, font);
CheckEqual(font, wrapper.Font);
CheckEqual(font.MetricsTable, wrapper.MetricsTable);
CheckEqual(font.KerningTable, wrapper.KerningTable);
CheckEqual("ab cd", wrapper.Text.ToString());
CheckTrue(wrapper.Text.IsOver(backing));
CheckEqual('a', wrapper.Reader[0]);
}
public void LineSpacing_IsTheFontsAscentDescentAndGap()
{
CheckEqual(10, Wrap("a", maxWidth: 100, FontWithTenPixelLines).LineSpacing);
}
}
public class InitializeTests : TestFixture
{
public void WrapsLikeTheConstructor()
{
var wrapper = default(LineWrapper);
wrapper.Initialize(Font, "ab cd", maxWidth: 25, maxHeight: null, TextOverflow.Clip, maxLines: null);
CheckEqual(Lines("ab cd", maxWidth: 25), Lines(wrapper));
}
public void AfterUse_StartsOverOnTheNewText()
{
LineWrapper wrapper = Wrap("xy zw\nq", maxWidth: 25);
while (wrapper.MoveNext())
{
}
wrapper.Initialize(Font, "ab cd", maxWidth: 25, maxHeight: null, TextOverflow.Clip, maxLines: null);
CheckEqual(Lines("ab cd", maxWidth: 25), Lines(wrapper));
}
public void MaxLinesLessThanOne_Throws()
{
var wrapper = default(LineWrapper);
CheckThrow(typeof(ArgumentOutOfRangeException));
wrapper.Initialize(Font, "a", maxWidth: 100, maxHeight: null, TextOverflow.Clip, maxLines: 0);
}
public void MeasuredWords_WrapLikeMeasuringTheText()
{
var text = new StringView("xabcdefgh ij kl mn op", 1, 21);
MetricsFont font = FontWithTenPixelLines;
var wrapper = default(LineWrapper);
wrapper.Initialize(new MeasuredWords(font, text), maxWidth: 25, maxHeight: null, TextOverflow.Ellipsis,
maxLines: 4);
CheckEqual(Lines(text, maxWidth: 25, font, overflow: TextOverflow.Ellipsis, maxLines: 4),
Lines(wrapper));
}
public void MeasuredWords_KeepTheTrailingSpaceMode()
{
const string text = "a b";
var wrapper = default(LineWrapper);
wrapper.Initialize(new MeasuredWords(Font, text), maxWidth: 25, maxHeight: null, TextOverflow.Clip,
maxLines: null, TrailingSpaceMode.Wrap);
CheckEqual(Lines(text, maxWidth: 25, trailingSpaces: TrailingSpaceMode.Wrap), Lines(wrapper));
}
public void MeasuredWords_MaxLinesLessThanOne_Throws()
{
var wrapper = default(LineWrapper);
CheckThrow(typeof(ArgumentOutOfRangeException));
wrapper.Initialize(new MeasuredWords(Font, "a"), maxWidth: 100, maxHeight: null, TextOverflow.Clip,
maxLines: 0);
}
public void MeasuredWordsWithFaces_WrapByThem()
{
var wrapper = default(LineWrapper);
var faces = FacedMeasuringTests.Runs(FacedMeasuringTests.Narrow, (3, 5, FacedMeasuringTests.Wide));
wrapper.Initialize(new MeasuredWords(FacedMeasuringTests.Narrow, "aa bb cc", faces), maxWidth: 50,
maxHeight: null, TextOverflow.Clip, maxLines: null);
CheckEqual(new[] { "0..3 width 20", "3..6 width 28", "6..8 width 20" }, Lines(wrapper));
}
public void TextAfterMeasuredWordsWithFaces_WrapsWithoutThem()
{
var wrapper = default(LineWrapper);
var faces = FacedMeasuringTests.Runs(FacedMeasuringTests.Narrow, (3, 5, FacedMeasuringTests.Wide));
wrapper.Initialize(new MeasuredWords(FacedMeasuringTests.Narrow, "aa bb cc", faces), maxWidth: 50,
maxHeight: null, TextOverflow.Clip, maxLines: null);
while (wrapper.MoveNext())
{
}
wrapper.Initialize(FacedMeasuringTests.Narrow, "aa bb cc", maxWidth: 50, maxHeight: null,
TextOverflow.Clip, maxLines: null);
CheckEqual(Lines("aa bb cc", maxWidth: 50, FacedMeasuringTests.Narrow), Lines(wrapper));
}
public void MeasuredWordsWithoutFacesAfterOnesWithThem_WrapWithoutThem()
{
var wrapper = default(LineWrapper);
var faces = FacedMeasuringTests.Runs(FacedMeasuringTests.Narrow, (3, 5, FacedMeasuringTests.Wide));
wrapper.Initialize(new MeasuredWords(FacedMeasuringTests.Narrow, "aa bb cc", faces), maxWidth: 50,
maxHeight: null, TextOverflow.Clip, maxLines: null);
wrapper.Initialize(new MeasuredWords(FacedMeasuringTests.Narrow, "aa bb cc"), maxWidth: 50,
maxHeight: null, TextOverflow.Clip, maxLines: null);
CheckEqual(Lines("aa bb cc", maxWidth: 50, FacedMeasuringTests.Narrow), Lines(wrapper));
}
}
///
/// Every glyph one pixel wide unless a subclass says otherwise, nothing kerned, and every glyph present.
///
public class OnePixelTestFont : IFontMetrics
{
public OnePixelTestFont()
{
// One instance per distinct width, so the table's pages repeat the way a real font's do.
var shared = new Dictionary();
MetricsTable = new GlyphMetricsTable(glyph =>
{
GlyphMetrics metrics = MetricsFor(glyph);
if (!shared.TryGetValue(metrics.Width, out GlyphMetrics sharedMetrics))
{
sharedMetrics = metrics;
shared[metrics.Width] = sharedMetrics;
}
return sharedMetrics;
});
}
public GlyphMetricsTable MetricsTable { get; }
public virtual GlyphMetrics MetricsFor(char glyph) => new GlyphMetrics { Width = 1 };
public KerningTable KerningTable { get; } =
KerningTable.Build(new Dictionary<(short, short), short>());
public bool HasGlyph(char glyph) => true;
public int LineAscent => 3;
public int LineDescent => 5;
public int LineGap => 11;
public int LineHeight => LineAscent + LineDescent;
public int Em => LineHeight;
public string Name => "OnePixelTestFont";
public IReadOnlyList Pages => null;
public FontFamily Family { get; set; }
}
///
/// Every glyph one pixel wide and nothing kerned, so a test's widths are its character counts.
///
public static readonly IFontMetrics OnePixelFont = new OnePixelTestFont();
///
/// Each line's text, with any hard break ending it left off.
///
public static List LineTexts(StringView text, int maxWidth)
{
LineWrapper wrapper = Wrap(text, maxWidth, OnePixelFont);
var returnMe = new List();
int start = 0;
while (wrapper.MoveNext())
{
int end = wrapper.Current.End;
int drawnEnd = end;
while (drawnEnd > start && IsHardBreak(text[drawnEnd - 1]))
{
--drawnEnd;
}
returnMe.Add(text[start, drawnEnd].ToString());
start = end;
}
return returnMe;
}
public static bool IsHardBreak(char character)
{
return character == '\n' || character == '\r' || character == (char)0x0B || character == (char)0x0C
|| character == (char)0x85 || character == (char)0x2028 || character == (char)0x2029;
}
public partial class MoveNextTests : TestFixture
{
public void NoBreakSpaces_AreNeverBrokenAt()
{
foreach (char space in new[] { (char)0x00A0, (char)0x202F, (char)0x2007 })
{
CheckEqual(new[] { "x ", $"aa{space}bb" }, LineTexts($"x aa{space}bb", maxWidth: 5));
}
}
public void SpaceBeforeClosingPunctuationOrSlash_IsNotABreak()
{
foreach (char punctuation in "!?)]}/")
{
CheckEqual(new[] { "x ", $"ab {punctuation}" }, LineTexts($"x ab {punctuation}", maxWidth: 5));
}
}
public void SpaceBeforeInfixPunctuation_IsNotABreak()
{
foreach (char punctuation in ",.:;")
{
CheckEqual(new[] { "x ", $"ab {punctuation}" }, LineTexts($"x ab {punctuation}", maxWidth: 5));
}
}
public void SpaceBeforeADecimalMark_IsABreak()
{
CheckEqual(new[] { "x ab ", ".5" }, LineTexts("x ab .5", maxWidth: 5));
}
public void GuillemetsWithInnerSpaces_StayWithTheirText()
{
CheckEqual(new[] { "x ", $"{(char)0x00AB} ab {(char)0x00BB}" },
LineTexts($"x {(char)0x00AB} ab {(char)0x00BB}", maxWidth: 6));
}
public void WordJoiners_OfferNoBreakEitherSide()
{
foreach (char joiner in new[] { (char)0x2060, (char)0xFEFF })
{
CheckEqual(new[] { "x ", $"aa{joiner}bb" }, LineTexts($"x aa{joiner}bb", maxWidth: 5));
}
}
public void HyphenBeforeADigit_OffersNoBreak()
{
CheckEqual(new[] { "x ", "ab-12" }, LineTexts("x ab-12", maxWidth: 5));
}
public void WordInitialHyphen_OffersNoBreakAfterIt()
{
CheckEqual(new[] { "x ", "-ab" }, LineTexts("x -ab", maxWidth: 3));
}
public void SlashBetweenDigits_OffersNoBreak()
{
CheckEqual(new[] { "x ", "12/34" }, LineTexts("x 12/34", maxWidth: 5));
}
public void CharacterWrapping_KeepsACombiningMarkWithItsBase()
{
CheckEqual(new[] { "ab", $"e{(char)0x0301}c", "d" }, LineTexts($"abe{(char)0x0301}cd", maxWidth: 3));
}
public void CharacterWrapping_BaseStartingTheChunk_TakesItsMarksAlong()
{
CheckEqual(new[] { $"e{(char)0x0301}{(char)0x0301}", "b" },
LineTexts($"e{(char)0x0301}{(char)0x0301}b", maxWidth: 1));
}
public void CharacterWrapping_KeepsAMarkWithAnAstralBase()
{
CheckEqual(new[] { "a", $"{WordMeasurerTests.Emoji}{(char)0x0301}", "b" },
LineTexts($"a{WordMeasurerTests.Emoji}{(char)0x0301}b", maxWidth: 2));
}
public void LineBreakCharactersBeyondCrAndLf_ForceABreak()
{
foreach (char lineBreak in new[] { (char)0x0B, (char)0x0C, (char)0x85, (char)0x2028, (char)0x2029 })
{
CheckEqual(new[] { "ab", "cd" }, LineTexts($"ab{lineBreak}cd", maxWidth: 100));
}
}
public void HyphensAndDashes_OfferABreakAfterThem()
{
foreach (char dash in new[] { '-', (char)0x2010, (char)0x2013, (char)0x2014 })
{
CheckEqual(new[] { $"well{dash}", "known" }, LineTexts($"well{dash}known", maxWidth: 6));
}
}
public void SoftHyphen_OffersABreak()
{
CheckEqual(new[] { $"well{LineBreakIterator.SoftHyphen}", "known" },
LineTexts($"well{LineBreakIterator.SoftHyphen}known", maxWidth: 6));
}
public void SoftHyphen_OnlyEndsALineWithRoomForTheHyphen()
{
CheckEqual(new[] { "x ", $"abc{LineBreakIterator.SoftHyphen}de" },
LineTexts($"x abc{LineBreakIterator.SoftHyphen}de", maxWidth: 5));
}
public void ZeroWidthSpace_OffersABreak()
{
CheckEqual(new[] { $"abc{(char)0x200B}", "def" }, LineTexts($"abc{(char)0x200B}def", maxWidth: 5));
}
public void Slash_OffersABreakAfterIt()
{
CheckEqual(new[] { "either/", "or" }, LineTexts("either/or", maxWidth: 8));
}
public void Ideographs_OfferABreakBetweenEachPair()
{
const char ideograph = (char)0x5B57;
CheckEqual(new[] { $"a {ideograph}{ideograph}", $"{ideograph}" },
LineTexts($"a {ideograph}{ideograph}{ideograph}", maxWidth: 4));
}
public void IdeographicFullStop_NeverStartsALine()
{
const char ideograph = (char)0x5B57;
CheckEqual(new[] { $"{ideograph}{ideograph}", $"{ideograph}{(char)0x3002}" },
LineTexts($"{ideograph}{ideograph}{ideograph}{(char)0x3002}", maxWidth: 3));
}
public void ViewIntoALongerString_WrapsLikeTheStandaloneString()
{
string standalone = "the quick" + "\n" + "brown foxes";
var view = new StringView("xx" + standalone + "yy", 2, 2 + standalone.Length);
CheckEqual(Lines(standalone, maxWidth: 3, OnePixelFont), Lines(view, maxWidth: 3, OnePixelFont));
CheckEqual(Lines(standalone, maxWidth: 3, OnePixelFont, maxHeight: 27, TextOverflow.Ellipsis),
Lines(view, maxWidth: 3, OnePixelFont, maxHeight: 27, TextOverflow.Ellipsis));
}
public void WrapMode_WordMovesDownWithItsSpacesWhenTheyDontFitAfterIt()
{
CheckEqual(new[] { "0..2 width 14", "2..6 width 28", "6..7 width 10" },
Lines("a bb c", maxWidth: 35, FontWithLineHeight(), trailingSpaces: TrailingSpaceMode.Wrap));
}
public void HangMode_SameText_LetsTheSpacesHangInstead()
{
CheckEqual(new[] { "0..6 width 34", "6..7 width 10" },
Lines("a bb c", maxWidth: 35, FontWithLineHeight()));
}
public void WrapMode_SpacesTooWideForAnyLine_SpillOntoLinesOfTheirOwn()
{
CheckEqual(new[] { "0..3 width 18", "3..8 width 20", "8..11 width 12", "11..12 width 10" },
Lines($"a{new string(' ', 10)}b", maxWidth: 20, FontWithLineHeight(),
trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_LastSpilledSpaces_LeaveRoomForTheNextWord()
{
CheckEqual(new[] { "0..4 width 22", "4..10 width 24", "10..12 width 14" },
Lines($"a{new string(' ', 10)}b", maxWidth: 25, FontWithLineHeight(),
trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_SpillEndingInAHardBreak_EndsTheLineThere()
{
CheckEqual(new[] { "0..3 width 18", "3..8 width 20", "8..12 width 12", "12..13 width 10" },
Lines($"a{new string(' ', 10)}\nb", maxWidth: 20, FontWithLineHeight(),
trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_SpacesBeforeAHardBreak_CountTowardTheWidth()
{
CheckEqual(new[] { "0..5 width 28", "5..6 width 10" },
Lines("ab \nc", maxWidth: 1000, FontWithLineHeight(), trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_FirstSpace_KernsAgainstTheContent()
{
CheckEqual(new[] { "0..3 width 12", "3..4 width 10" },
Lines("b \nc", maxWidth: 1000, trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_SplitWordsSpaces_SpillToo()
{
CheckEqual(new[] { "0..2 width 20", "2..6 width 22", "6..10 width 22" },
Lines($"aaa{new string(' ', 6)}b", maxWidth: 25, FontWithLineHeight(),
trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_LeadingSpacesWiderThanTheLine_StartSpillingRightAway()
{
CheckEqual(new[] { "0..1 width 4", "1..2 width 4" },
Lines(" ", maxWidth: 3, FontWithLineHeight(), trailingSpaces: TrailingSpaceMode.Wrap));
CheckEqual(TextOverflow.Clip, TruncationOf(" ", maxWidth: 3, FontWithLineHeight(),
trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_SpilledLineCutShort_GetsTheMarker()
{
CheckEqual(new[] { "0..1 width 15 EndsWithEllipsis" },
Lines($"a{new string(' ', 10)}b", maxWidth: 20, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1, trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_OnlySpacesCutOff_IsNoTruncation()
{
CheckNull(TruncationOf($"a{new string(' ', 10)}", maxWidth: 20, FontWithLineHeight(), maxLines: 1,
trailingSpaces: TrailingSpaceMode.Wrap));
}
public void WrapMode_WordCutOffAfterSpilledSpaces_IsTruncation()
{
CheckEqual(TextOverflow.Clip, TruncationOf($"a{new string(' ', 10)}b", maxWidth: 20,
FontWithLineHeight(), maxLines: 2, trailingSpaces: TrailingSpaceMode.Wrap));
}
public void EmptyText_HasNoLines()
{
CheckEqual(new string[0], Lines("", maxWidth: 100));
}
public void WordsThatFitTogether_StayOnOneLine()
{
CheckEqual(new[] { "0..5 width 38" }, Lines("ab cd", maxWidth: 1000));
}
public void WordsThatDoNotFitTogether_WrapToANewLine_AndTheSpaceHangs()
{
CheckEqual(new[] { "0..3 width 19", "3..5 width 20" }, Lines("ab cd", maxWidth: 25));
}
public void MandatoryBreak_EndsTheLineEvenWithRoomToSpare()
{
CheckEqual(new[] { "0..2 width 10", "2..4 width 20" }, Lines("a\nbb", maxWidth: 1000));
}
public void SoftHyphenNotTaken_ShowsNoHyphenAndKerningCarriesAcross()
{
CheckEqual(new[] { "0..3 width 15" }, Lines($"a{LineBreakIterator.SoftHyphen}c", maxWidth: 1000));
}
public void SoftHyphenTaken_ShowsTheHyphenAndItsWidth()
{
string text = $"a{LineBreakIterator.SoftHyphen}c";
CheckEqual(new[] { "0..2 width 13 EndsWithHyphen", "2..3 width 10" }, Lines(text, maxWidth: 12));
}
public void WordWiderThanMaxWidth_SplitsIntoCharacterFitChunks()
{
CheckEqual(new[] { "0..2 width 20", "2..4 width 20", "4..6 width 20" },
Lines("aaaaaa", maxWidth: 25));
}
public void SplitWordFollowedByMoreWords_TheLastChunkStaysOpenForThem()
{
CheckEqual(new[] { "0..2 width 20", "2..5 width 24" }, Lines("aaa a", maxWidth: 25));
}
public void OversizedWordAfterAnotherWord_ClosesTheLineBeforeSplitting()
{
CheckEqual(new[] { "0..2 width 10", "2..4 width 20", "4..6 width 20", "6..8 width 20" },
Lines("a aaaaaa", maxWidth: 25));
}
public void FreshLineStartingWithASoftHyphen_KernsTheNextWordAgainstNothing()
{
CheckEqual(new[] { "0..2 width 10", "2..4 width 10" },
Lines($"b {LineBreakIterator.SoftHyphen}c", maxWidth: 11));
}
public void FreshLineStartingWithInvisibleContentAndSpaces_KernsTheSpacesAgainstNothing()
{
CheckEqual(new[] { "0..2 width 10", "2..5 width 11" },
Lines($"b {LineBreakIterator.SoftHyphen} c", maxWidth: 11,
WordMeasurerTests.TestFont(kerningBetweenSpaces: -1)));
}
public void SplitWordEndingInAHardBreak_EndsItsLastChunksLine()
{
CheckEqual(new[] { "0..2 width 20", "2..4 width 10", "4..5 width 10" }, Lines("aaa\nb", maxWidth: 25));
}
public void SplitWord_NeverSplitsASurrogatePair()
{
string boldA = char.ConvertFromUtf32(0x1D400);
CheckEqual(new[] { "0..1 width 10", "1..3 width 7", "3..4 width 10" },
Lines($"a{boldA}a", maxWidth: 8));
}
public void SplitWord_LoneSurrogate_IsTheInvalidGlyph()
{
CheckEqual(new[] { "0..1 width 10", "1..2 width 7", "2..3 width 10" },
Lines($"a{GlyphMetricsTable.LoneSurrogate}a", maxWidth: 8));
}
public void SplitWord_InvisibleCharacterJoinsTheChunkBeforeIt()
{
CheckEqual(new[] { "0..3 width 10", "3..4 width 10" },
Lines($"a{WordMeasurerTests.LanguageTag}a", maxWidth: 8));
}
public void SplitWord_StartingWithAnInvisibleCharacter_KeepsItWithTheFirstGlyph()
{
CheckEqual(new[] { "0..3 width 10", "3..4 width 10" },
Lines($"{WordMeasurerTests.LanguageTag}aa", maxWidth: 8));
}
public void SplitWord_EndingAtASoftHyphen_ShowsTheHyphenAfterItsLastChunk()
{
CheckEqual(new[] { "0..1 width 10", "1..3 width 13 EndsWithHyphen", "3..4 width 10" },
Lines($"aa{LineBreakIterator.SoftHyphen}b", maxWidth: 15));
}
public void SoftHyphenEndingTheText_NeedsNoHyphen()
{
CheckEqual(new[] { "0..3 width 19" }, Lines($"ab{LineBreakIterator.SoftHyphen}", maxWidth: 1000));
}
public void SingleCharacterWiderThanMaxWidth_OverflowsSidewaysAndIsClipped()
{
CheckEqual(new[] { "0..1 width 10" }, Lines("a", maxWidth: 5));
CheckEqual(TextOverflow.Clip, TruncationOf("a", maxWidth: 5));
}
public void MaxLinesReached_StopsEmittingAndMarksClipped()
{
CheckEqual(new[] { "0..2 width 20", "2..4 width 20" },
Lines("aaaaaa", maxWidth: 25, maxLines: 2));
CheckEqual(TextOverflow.Clip, TruncationOf("aaaaaa", maxWidth: 25, maxLines: 2));
}
public void FirstLine_AlwaysShowsEvenWithNoHeightBudget()
{
MetricsFont font = FontWithTenPixelLines;
CheckEqual(new[] { "0..1 width 10" }, Lines("a", maxWidth: 1000, font, maxHeight: 0));
CheckNull(TruncationOf("a", maxWidth: 1000, font, maxHeight: 0));
}
public void ClipOverflow_AllowsAPartlyVisibleLineButNotOneEntirelyOutOfBounds()
{
MetricsFont font = FontWithTenPixelLines;
CheckEqual(new[] { "0..2 width 10", "2..4 width 10" },
Lines("a\nb\nc", maxWidth: 1000, font, maxHeight: 15, TextOverflow.Clip));
CheckEqual(TextOverflow.Clip,
TruncationOf("a\nb\nc", maxWidth: 1000, font, maxHeight: 15, TextOverflow.Clip));
}
public void EllipsisOverflow_NeedsTheWholeNextLineInsideTheBudget()
{
MetricsFont font = FontWithTenPixelLines;
CheckEqual(new[] { "0..1 width 15 EndsWithEllipsis" },
Lines("a\nb\nc", maxWidth: 1000, font, maxHeight: 15, TextOverflow.Ellipsis));
}
public void EllipsisOverflow_AllowsALineThatFitsEntirely()
{
MetricsFont font = FontWithTenPixelLines;
CheckEqual(new[] { "0..2 width 10", "2..3 width 15 EndsWithEllipsis" },
Lines("a\nb\nc", maxWidth: 1000, font, maxHeight: 20, TextOverflow.Ellipsis));
}
public void EllipsisOverflow_TextThatFits_GetsNoMarker()
{
CheckEqual(new[] { "0..2 width 20" },
Lines("ab", maxWidth: 100, FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
CheckNull(TruncationOf("ab", maxWidth: 100, FontWithLineHeight(), overflow: TextOverflow.Ellipsis,
maxLines: 1));
}
public void EllipsisOverflow_MarkerThatFits_KeepsTheWholeLine()
{
CheckEqual(new[] { "0..3 width 35 EndsWithEllipsis" }, Lines("aaa bbb", maxWidth: 35,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
CheckEqual(TextOverflow.Ellipsis, TruncationOf("aaa bbb", maxWidth: 35, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_GivesBackCharactersUntilTheMarkerFits()
{
CheckEqual(new[] { "0..2 width 25 EndsWithEllipsis" }, Lines("aaa bbb", maxWidth: 34,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_KeptTextNeverEndsInASpace()
{
CheckEqual(new[] { "0..1 width 15 EndsWithEllipsis" }, Lines("a b cccc", maxWidth: 28,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_HardBreakBeforeMoreText_StillGetsAMarker()
{
CheckEqual(new[] { "0..2 width 25 EndsWithEllipsis" }, Lines("ab\ncd", maxWidth: 100,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_EmptyLinesBeforeMoreText_StillGetAMarker()
{
CheckEqual(new[] { "0..2 width 25 EndsWithEllipsis" }, Lines("ab\n\ncd", maxWidth: 100,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_OverridesAnEarlierSidewaysClip()
{
CheckEqual(new[] { "0..2 width 10", "2..2 width 5 EndsWithEllipsis" }, Lines("a\nb\nc", maxWidth: 8,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 2));
CheckEqual(TextOverflow.Ellipsis, TruncationOf("a\nb\nc", maxWidth: 8, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 2));
}
public void EllipsisOverflow_OnlyHardBreaksLeft_GetsNoMarker()
{
CheckEqual(new[] { "0..4 width 20" }, Lines("ab\r\n", maxWidth: 100,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
CheckNull(TruncationOf("ab\n\n", maxWidth: 100, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_HangingSpaces_GetNoMarker()
{
CheckEqual(new[] { "0..5 width 20" }, Lines("ab ", maxWidth: 25,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
CheckNull(TruncationOf("ab ", maxWidth: 25, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_LineEndingAtASoftHyphen_ShowsTheMarkerInsteadOfTheHyphen()
{
CheckEqual(new[] { "0..2 width 25 EndsWithEllipsis" },
Lines($"ab{LineBreakIterator.SoftHyphen}cd", maxWidth: 30, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_AstralCharacter_IsKeptWhole()
{
CheckEqual(new[] { "0..3 width 22 EndsWithEllipsis" },
Lines($"a{WordMeasurerTests.Emoji}\nb", maxWidth: 22, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_InvisibleCharacter_NeverEndsTheKeptText()
{
CheckEqual(new[] { "0..1 width 15 EndsWithEllipsis" },
Lines($"a{WordMeasurerTests.LanguageTag}\nb", maxWidth: 100, FontWithLineHeight(),
overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_ShorterPrefixThatKernsWider_DoesNotStopALongerOneFitting()
{
MetricsFont font = FontWithLineHeight(kernings: new Dictionary<(short, short), short>
{
[LayoutFonts.Pair('a', TruncationMarker.EllipsisGlyph)] = 10,
[LayoutFonts.Pair('b', TruncationMarker.EllipsisGlyph)] = -1,
});
CheckEqual(new[] { "0..2 width 24 EndsWithEllipsis" },
Lines("ab\nc", maxWidth: 24, font, overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_FontWithoutAnEllipsis_MarksWithThreeKernedPeriods()
{
MetricsFont font = FontWithLineHeight(withEllipsis: false,
kernings: new Dictionary<(short, short), short> { [LayoutFonts.Pair('.', '.')] = -1 });
CheckEqual(new[] { "0..2 width 24 EndsWithEllipsis" },
Lines("ab\ncd", maxWidth: 100, font, overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_MarkerWiderThanTheLine_KeepsNoTextAndOverruns()
{
CheckEqual(new[] { "0..0 width 6 EndsWithEllipsis" }, Lines("ab\ncd", maxWidth: 5,
FontWithLineHeight(withEllipsis: false), overflow: TextOverflow.Ellipsis, maxLines: 1));
CheckEqual(TextOverflow.Ellipsis, TruncationOf("ab\ncd", maxWidth: 5,
FontWithLineHeight(withEllipsis: false), overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_SplitWordCutByTheLastLine_MarksTheChunk()
{
CheckEqual(new[] { "0..1 width 15 EndsWithEllipsis" }, Lines("aaaaaa", maxWidth: 24,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 1));
}
public void EllipsisOverflow_LastChunkOfASplitWord_CanBeMarked()
{
CheckEqual(new[] { "0..2 width 20", "2..3 width 15 EndsWithEllipsis" }, Lines("aaa bb", maxWidth: 25,
FontWithLineHeight(), overflow: TextOverflow.Ellipsis, maxLines: 2));
}
public void ClipOverflow_NeverMarks()
{
CheckEqual(new[] { "0..4 width 30" }, Lines("aaa bbb", maxWidth: 34,
FontWithLineHeight(), overflow: TextOverflow.Clip, maxLines: 1));
}
public void HeightBudget_CanRunOutMidSplit()
{
MetricsFont font = FontWithTenPixelLines;
CheckEqual(new[] { "0..2 width 20", "2..4 width 20", "4..6 width 20" },
Lines("aaaaaaaa", maxWidth: 25, font, maxHeight: 25));
CheckEqual(TextOverflow.Clip, TruncationOf("aaaaaaaa", maxWidth: 25, font, maxHeight: 25));
}
}
}
}