using System; using System.Collections.Generic; using Amphora; using Tessera.Typesetting; using UnitTestSharp; using Tessera.Unicode; namespace Tessera.UnitTests.Typesetting { public class TextWidthTests : TestFixture { public class CalculateTests : TestFixture { public void NullFont_Throws() { CheckThrow(typeof(ArgumentNullException)); TextWidth.Calculate(null, "ab"); } public void EmptyText_IsZero() { CheckEqual(0, TextWidth.Calculate(WordMeasurerTests.SharedFont, "")); } public void OneWord_KernsItsInterior() { CheckEqual(19, TextWidth.Calculate(WordMeasurerTests.SharedFont, "ab")); } public void KerningFlowsThroughSpaces_UnlikeAWordBoundary() { CheckEqual(28, TextWidth.Calculate(WordMeasurerTests.SharedFont, "ab c")); } public void SoftHyphen_TakesNoWidthAndKerningCarriesAcross() { CheckEqual(15, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"a{LineBreakIterator.SoftHyphen}c")); } public void AstralCharacter_MeasuresAsOneInvalidGlyph() { CheckEqual(LayoutFonts.InvalidGlyphWidth, TextWidth.Calculate(WordMeasurerTests.SharedFont, WordMeasurerTests.Emoji)); } public void AstralDefaultIgnorable_TakesNoWidthAndKerningCarriesAcross() { CheckEqual(15, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"a{WordMeasurerTests.LanguageTag}c")); } public void LoneSurrogate_MeasuresAsTheInvalidGlyph() { CheckEqual(LayoutFonts.InvalidGlyphWidth, TextWidth.Calculate(WordMeasurerTests.SharedFont, GlyphMetricsTable.LoneSurrogate.ToString())); } public void UnpairedHighSurrogateBeforeACharacter_MeasuresAsTheInvalidGlyph() { char high = WordMeasurer.FirstHighSurrogate; CheckEqual(LayoutFonts.InvalidGlyphWidth + 10, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"{high}a")); CheckEqual(10 + LayoutFonts.InvalidGlyphWidth + 10, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"a{high}a")); } public void TwoUnpairedHighSurrogates_EachMeasuresAsTheInvalidGlyph() { char high = WordMeasurer.FirstHighSurrogate; CheckEqual(10 + LayoutFonts.InvalidGlyphWidth + 10 + LayoutFonts.InvalidGlyphWidth, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"a{high}a{high}")); } public void UnpairedLowSurrogate_MeasuresAsTheInvalidGlyph() { CheckEqual(LayoutFonts.InvalidGlyphWidth + 10, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"{WordMeasurer.FirstLowSurrogate}a")); } public void NonAsciiBmpCharacter_MeasuresItsOwnGlyph() { var glyphs = new Dictionary { ['é'] = new GlyphMetrics { Width = 12 } }; MetricsFont font = WordMeasurerTests.TestFont(glyphs); CheckEqual(12, TextWidth.Calculate(font, "é")); } public void LongAsciiRun_BatchesInPairsWithoutMiscounting() { CheckEqual(100, TextWidth.Calculate(WordMeasurerTests.SharedFont, new string('a', 10))); } public void OddLengthAsciiRun_HandlesTheLeftoverCharacter() { CheckEqual(90, TextWidth.Calculate(WordMeasurerTests.SharedFont, new string('a', 9))); } public void View_PositionsAreRelativeToItsStart() { CheckEqual(19, TextWidth.Calculate(WordMeasurerTests.SharedFont, ((StringView)"xxabxx")[2, 4])); } public void TwoAstralCharacters_EachPairIsWithinItsOwnWindow() { string text = WordMeasurerTests.Emoji + WordMeasurerTests.Emoji; CheckEqual(2 * LayoutFonts.InvalidGlyphWidth, TextWidth.Calculate(WordMeasurerTests.SharedFont, text)); } public void AstralCharacterAfterAnOddNumberOfCharacters_StraddlesTwoWindowsButStillCombines() { CheckEqual(10 + LayoutFonts.InvalidGlyphWidth + 10, TextWidth.Calculate(WordMeasurerTests.SharedFont, $"a{WordMeasurerTests.Emoji}a")); } public void TwoNonAsciiBmpCharacters_TakeTheGeneralPairPath() { var glyphs = new Dictionary { ['é'] = new GlyphMetrics { Width = 12 } }; MetricsFont font = WordMeasurerTests.TestFont(glyphs); CheckEqual(24, TextWidth.Calculate(font, "éé")); } // U+3000 ORed with a genuine high surrogate reads as 0xF800 rather than 0xD800: a mask-based check // would miss the surrogate here and later throw in ConvertToUtf32 for want of a high half. U+3000 is // zero width in this font, so only the astral pair's own width should show. public void BmpCharacterThatHidesAHighSurrogateWhenOred_StillCombinesTheAstralPair() { var glyphs = new Dictionary { [(char)0x3000] = new GlyphMetrics { Width = 0 } }; string text = $" {WordMeasurerTests.Emoji} "; CheckEqual(LayoutFonts.InvalidGlyphWidth, TextWidth.Calculate(WordMeasurerTests.TestFont(glyphs), text)); } } } }