using System; using System.Collections.Generic; using Amphora; using Tessera.Text; using Tessera.Typesetting; using Tessera.Unicode; using UnitTestSharp; namespace Glass.UnitTests.Text { public class WidestNumberFitTests : TestFixture { public class WriteShortFitsEveryNumberTests : TestFixture { private const int NarrowestLetter = 4; private const int DigitWidth = 8; private static readonly int[] Exponents = { -5, -4, -3, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 20, 50, 100, 200, 300, 307, }; private static readonly double[] Mantissas = { 1, 2, 5.5, 9.99 }; private Dictionary fontsByZeroDigit; private static void AddLetters(Dictionary glyphs, string text) { foreach (char letter in text) { glyphs[letter] = new SpriteFontDefinition.GlyphDefinition { Width = NarrowestLetter + letter % 9 }; } } // Every format here writes with the same digits, so each measures in this font as it would in a font of its // own letters and digits. A font per format would cost several milliseconds apiece. public static SpriteFontDefinition FontFor(IEnumerable formatsSharingDigits) { var glyphs = new Dictionary(); int zeroDigit = '0'; foreach (NumberFormat format in formatsSharingDigits) { zeroDigit = format.ZeroDigit; AddLetters(glyphs, format.MinusSign + format.DecimalSeparator + format.ExponentSymbol); ShortNotation notation = format.ShortNotation; for (int i = 0; i < notation.TierCount; ++i) { ShortTier tier = notation.TierOf(notation.FirstExponent + i); for (PluralCategory category = PluralCategory.Zero; category <= PluralCategory.Other; ++category) { AddLetters(glyphs, tier.PatternFor(category).Before + tier.PatternFor(category).After); } } } for (int digit = 0; digit < 10; ++digit) { glyphs[(char)(zeroDigit + digit)] = new SpriteFontDefinition.GlyphDefinition { Width = DigitWidth }; } return new SpriteFontDefinition(new SpriteFontDefinition.Data { GlyphDefinitions = glyphs, InvalidGlyph = new SpriteFontDefinition.GlyphDefinition { Width = DigitWidth }, Kernings = new Dictionary<(short, short), short>(), Baseline = 8, LineHeight = 10, }); } public static int RoomOf(NumberFormat format, SpriteFontDefinition font, int significant, bool negative) { var buffer = new char[format.MaxShortLength]; int room = 0; for (int variant = 0; variant < WidestNumber.ShortVariantCount(format); ++variant) { int end = WidestNumber.WriteShort(format, significant, negative, variant, buffer, 0); room = Math.Max(room, CounterBox.WidthOf(font, new StringView(buffer, 0, end), format.ZeroDigit)); } return room; } public static int WidestSample(NumberFormat format, SpriteFontDefinition font, int significant, bool negative) { var buffer = new char[format.MaxShortLength]; int widest = 0; foreach (int exponent in Exponents) { foreach (double mantissa in Mantissas) { double value = mantissa * Math.Pow(10, exponent); int end = format.WriteShortExponent(negative ? -value : value, significant, buffer, 0); int width = CounterBox.WidthOf(font, new StringView(buffer, 0, end), format.ZeroDigit); widest = Math.Max(widest, width); } } return widest; } public override void FixtureSetup() { var formatsByZeroDigit = new Dictionary>(); foreach (string tag in NumberFormat.LocaleTags()) { NumberFormat format = NumberFormat.For(tag); if (!formatsByZeroDigit.TryGetValue(format.ZeroDigit, out List formats)) { formats = new List(); formatsByZeroDigit[format.ZeroDigit] = formats; } formats.Add(format); } fontsByZeroDigit = new Dictionary(); foreach (KeyValuePair> group in formatsByZeroDigit) { fontsByZeroDigit[group.Key] = FontFor(group.Value); } } [IgnoreTest] public void CheckRoomHoldsEverySample(string[] tags, int firstTag, int step) { for (int i = firstTag; i < tags.Length; i += step) { NumberFormat format = NumberFormat.For(tags[i]); if (format.DigitWidth != 1) { continue; } SpriteFontDefinition font = fontsByZeroDigit[format.ZeroDigit]; foreach (int significant in new[] { 1, 3 }) { foreach (bool negative in new[] { false, true }) { CheckTrue(WidestSample(format, font, significant, negative) <= RoomOf(format, font, significant, negative)); } } } } public void English_HoldsEverySample() { CheckRoomHoldsEverySample(new[] { "en" }, 0, 1); } public void EveryLanguage_HoldsEverySample_First() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 0, 8); } public void EveryLanguage_HoldsEverySample_Second() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 1, 8); } public void EveryLanguage_HoldsEverySample_Third() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 2, 8); } public void EveryLanguage_HoldsEverySample_Fourth() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 3, 8); } public void EveryLanguage_HoldsEverySample_Fifth() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 4, 8); } public void EveryLanguage_HoldsEverySample_Sixth() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 5, 8); } public void EveryLanguage_HoldsEverySample_Seventh() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 6, 8); } public void EveryLanguage_HoldsEverySample_Eighth() { CheckRoomHoldsEverySample(NumberFormat.LocaleTags(), 7, 8); } } } }