using System; using System.Linq; using Amphora; using Tessera.Typesetting; using Tessera.UnitTests.Editing; using UnitTestSharp; using Tessera.Unicode; using Tessera.UnitTests.Unicode; namespace Tessera.UnitTests.Typesetting { public class GlyphMetricsTableTests : TestFixture { public const int CodePoints = char.MaxValue + 1; /// /// A distinct instance for every code point, so no two pages are the same. /// public static GlyphMetrics[] DistinctMetrics() { var returnMe = new GlyphMetrics[CodePoints]; for (int i = 0; i < returnMe.Length; ++i) { returnMe[i] = new GlyphMetrics { Width = i }; } return returnMe; } public class CtorTests : TestFixture { public void NullMetricsFor_Throws() { CheckThrow(typeof(ArgumentNullException)); new GlyphMetricsTable(null); } public void AsksForEachCodePointOnce() { var asked = new int[CodePoints]; var shared = new GlyphMetrics(); new GlyphMetricsTable(glyph => { ++asked[glyph]; return shared; }); CheckEqual(-1, Array.FindIndex(asked, count => count != 1)); } } public class PageCountTests : TestFixture { public void SameMetricsEverywhere_IsOnePage() { var shared = new GlyphMetrics(); CheckEqual(1, new GlyphMetricsTable(glyph => shared).PageCount); } public void OneDifferentEntry_SetsItsPageApart() { var shared = new GlyphMetrics(); var different = new GlyphMetrics(); CheckEqual(2, new GlyphMetricsTable(glyph => glyph == 'A' ? different : shared).PageCount); } public void RepeatedPages_AreStoredOnce() { GlyphMetrics[] metrics = DistinctMetrics(); CheckEqual(1, new GlyphMetricsTable(glyph => metrics[glyph % GlyphMetricsTable.PageSize]).PageCount); } public void EqualButDistinctMetrics_AreNotShared() { CheckEqual(CodePoints / GlyphMetricsTable.PageSize, new GlyphMetricsTable(glyph => new GlyphMetrics()).PageCount); } } public class AsciiTests : TestFixture { public void HoldsWhatTheIndexerFindsForEachAsciiCodePoint() { GlyphMetrics[] metrics = DistinctMetrics(); var table = new GlyphMetricsTable(glyph => metrics[glyph]); CheckEqual(metrics.Take(GlyphMetricsTable.AsciiLength), table.Ascii); } } public class IndexerTests : TestFixture { public void EveryCodePoint_FindsItsOwnMetrics() { GlyphMetrics[] metrics = DistinctMetrics(); var table = new GlyphMetricsTable(glyph => metrics[glyph]); CheckEqual(-1, Array.FindIndex(metrics, entry => !ReferenceEquals(entry, table[(char)entry.Width]))); } public void SharedPage_FindsTheEntryAtEachPosition() { GlyphMetrics[] metrics = DistinctMetrics(); var table = new GlyphMetricsTable(glyph => metrics[glyph % GlyphMetricsTable.PageSize]); CheckEqual(metrics[1], table[(char)1]); CheckEqual(metrics[1], table[(char)(GlyphMetricsTable.PageSize + 1)]); CheckEqual(metrics[GlyphMetricsTable.PageSize - 1], table[char.MaxValue]); } } public class NeedsNextVisibleGlyphTests : TestFixture { public void EveryCodeUnit_IsTrueForExactlySurrogatesAndTheSoftHyphen() { CheckEqual("", CodeUnitSamples.SurrogateMismatches(GlyphMetricsTable.NeedsNextVisibleGlyph, character => char.IsSurrogate(character) || character == LineBreakIterator.SoftHyphen)); } } public class NextVisibleGlyphTests : TestFixture { /// /// Each code point's metrics are as wide as its value, so a glyph says which code point it came from. /// public static readonly GlyphMetricsTable Table = BuildTable(); public static GlyphMetricsTable BuildTable() { GlyphMetrics[] metrics = DistinctMetrics(); return new GlyphMetricsTable(glyph => metrics[glyph]); } /// /// The step from `position` as "visible? glyph width, position after". /// public static string Step(string text, int position) { bool visible = Table.NextVisibleGlyph(text, ref position, text.Length, out GlyphMetrics glyph); return $"{visible} {glyph?.Width} {position}"; } public void PlainCharacter_IsItsOwnGlyph() { CheckEqual("True 97 1", Step("ab", 0)); } public void SoftHyphen_DrawsNothing() { CheckEqual("False 1", Step($"{LineBreakIterator.SoftHyphen}a", 0)); } public void AstralCharacter_IsTheInvalidGlyph_AndStepsPastBothHalves() { CheckEqual($"True {(int)GlyphMetricsTable.LoneSurrogate} 2", Step(TextSamples.Emoji, 0)); } public void AstralDefaultIgnorable_DrawsNothing_AndStepsPastBothHalves() { CheckEqual("False 2", Step(TextSamples.LanguageTag, 0)); } public void HighSurrogateAtTheEnd_IsItsOwnGlyph() { CheckEqual($"True {0xD83D} 2", Step($"a{(char)0xD83D}", 1)); } public void HighSurrogateBeforeANonSurrogate_IsItsOwnGlyph() { CheckEqual($"True {0xD83D} 1", Step($"{(char)0xD83D}a", 0)); } public void LoneLowSurrogate_IsItsOwnGlyph() { CheckEqual($"True {0xDE00} 1", Step($"{(char)0xDE00}a", 0)); } public void Position_IsRelativeToTheView() { int position = 1; Table.NextVisibleGlyph(new StringView("xab", 1, 3), ref position, 2, out GlyphMetrics glyph); CheckEqual((int)'b', glyph.Width); } } } }