using System; using System.Collections.Generic; using Amphora; using Tessera.Typesetting; using UnitTestSharp; namespace Tessera.UnitTests.Typesetting { public class CounterBoxTests : TestFixture { public const int SpaceWidth = 4; public const int CommaWidth = 3; public const int PeriodWidth = 2; public const int InvalidWidth = LayoutFonts.InvalidGlyphWidth; public static readonly int[] ProportionalDigits = { 8, 4, 7, 7, 7, 7, 7, 6, 7, 7 }; public static MetricsFont FontOf(int[] digitWidths, int zeroDigit = '0', Dictionary<(short, short), short> kernings = null, Dictionary letterWidths = null) { var glyphs = new Dictionary(); if (letterWidths != null) { foreach (KeyValuePair letter in letterWidths) { glyphs[letter.Key] = new GlyphMetrics { Width = letter.Value }; } } for (int i = 0; i < digitWidths.Length; ++i) { glyphs[(char)(zeroDigit + i)] = new GlyphMetrics { Width = digitWidths[i] }; } glyphs[' '] = new GlyphMetrics { Width = SpaceWidth }; glyphs[','] = new GlyphMetrics { Width = CommaWidth }; glyphs['.'] = new GlyphMetrics { Width = PeriodWidth }; glyphs['-'] = new GlyphMetrics { Width = 5 }; return new MetricsFont(new MetricsFont.Data { Glyphs = glyphs, InvalidGlyph = new GlyphMetrics { Width = InvalidWidth }, Kernings = kernings ?? new Dictionary<(short, short), short>(), Baseline = 8, LineHeight = 10, }); } public class WidestDigitTests : TestFixture { public void IsTheWidestOfTheTen() { MetricsFont font = FontOf(new[] { 5, 3, 9, 4, 4, 4, 4, 4, 4, 6 }); CheckEqual(9, CounterBox.WidestDigit(font, '0').Width); CheckTrue(ReferenceEquals(font.MetricsTable['2'], CounterBox.WidestDigit(font, '0'))); } public void OfEquallyWideDigits_IsTheFirst() { MetricsFont font = FontOf(new[] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }); CheckTrue(ReferenceEquals(font.MetricsTable['0'], CounterBox.WidestDigit(font, '0'))); } public void OfAnotherSystem_ReadsThatSystemsDigits() { MetricsFont font = FontOf(new[] { 2, 3, 4, 5, 6, 7, 8, 9, 1, 2 }, zeroDigit: 0x0660); CheckEqual(9, CounterBox.WidestDigit(font, 0x0660).Width); } public void OfASystemPastTheBasicPlane_IsTheGlyphAnyOfItsCodePointsDraws() { MetricsFont font = FontOf(ProportionalDigits); CheckTrue(ReferenceEquals(font.MetricsTable[GlyphMetricsTable.LoneSurrogate], CounterBox.WidestDigit(font, 0x1E950))); } public void NullFont_Throws() { CheckThrow(typeof(ArgumentNullException)); CounterBox.WidestDigit(null, '0'); } } public class WidthOfTests : TestFixture { public void EveryDigit_IsAsWideAsTheWidest() { MetricsFont font = FontOf(ProportionalDigits); CheckEqual(8, CounterBox.WidthOf(font, "1", '0')); } public void Separators_AddTheirOwnWidths() { MetricsFont font = FontOf(ProportionalDigits); CheckEqual(5 * 8 + CommaWidth, CounterBox.WidthOf(font, "99,999", '0')); CheckEqual(3 * 8 + PeriodWidth + 2 * 8, CounterBox.WidthOf(font, "999.99", '0')); } public void Text_WithNoDigits_IsMeasuredAsTextWidthDoes() { MetricsFont font = FontOf(ProportionalDigits); CheckEqual(TextWidth.Calculate(font, "-,. ,"), CounterBox.WidthOf(font, "-,. ,", '0')); } public void NoText_IsNoWidth() { CheckEqual(0, CounterBox.WidthOf(FontOf(ProportionalDigits), "", '0')); } public void KerningAgainstTheWidestDigit_Counts() { var kernings = new Dictionary<(short, short), short> { [LayoutFonts.Pair('3', ',')] = -2 }; MetricsFont font = FontOf(new[] { 5, 5, 5, 6, 5, 5, 5, 5, 5, 5 }, kernings: kernings); CheckEqual(6 - 2 + CommaWidth, CounterBox.WidthOf(font, "1,", '0')); } public void DigitsOfAnotherSystem_AreSubstitutedWhenThatSystemIsAsked() { MetricsFont font = FontOf(new[] { 2, 3, 4, 5, 6, 7, 8, 9, 1, 2 }, zeroDigit: 0x0660); string text = new string(new[] { (char)0x0661, (char)0x0662 }); CheckEqual(2 * 9, CounterBox.WidthOf(font, text, 0x0660)); } public void DigitsOfOtherSystems_AreMeasuredAsTheyAre() { MetricsFont font = FontOf(ProportionalDigits); CheckEqual(4, CounterBox.WidthOf(font, "1", 0x0660)); } public void DigitsPastTheBasicPlane_AreEachOneGlyph() { MetricsFont font = FontOf(ProportionalDigits); string text = char.ConvertFromUtf32(0x1E951) + char.ConvertFromUtf32(0x1E952); CheckEqual(2 * InvalidWidth, CounterBox.WidthOf(font, text, 0x1E950)); } public void ACodePointJustPastTheSystem_IsNotADigit() { MetricsFont font = FontOf(ProportionalDigits); string text = char.ConvertFromUtf32(0x1E95A); CheckEqual(InvalidWidth, CounterBox.WidthOf(font, text, 0x1E950)); } public void ALoneSurrogate_IsNotADigit() { MetricsFont font = FontOf(ProportionalDigits); CheckEqual(InvalidWidth, CounterBox.WidthOf(font, new string((char)0xD83A, 1), 0x1E950)); } public void NullFont_Throws() { CheckThrow(typeof(ArgumentNullException)); CounterBox.WidthOf(null, "1", '0'); } } } }