public void TypeMeasureOptions_FontUnitTypo()
        {
            var options = TypeMeasureOptions.BreakOnWords;

            options.IgnoreStartingWhiteSpace = true;
            options.FontUnits = FontUnitType.UseTypographicMetrics;

            Assert.IsFalse(options.CharacterSpacing.HasValue);
            Assert.IsFalse(options.WordSpacing.HasValue);

            Assert.IsTrue(options.BreakOnWordBoundaries, "The static break on words should have this flag as true");
            Assert.IsTrue(options.IgnoreStartingWhiteSpace, "The starting white space should be ignored");
            Assert.AreEqual(FontUnitType.UseTypographicMetrics, options.FontUnits, "The font units should be the header metrics");


            using (var reader = new TypefaceReader())
            {
                ITypefaceFont font = reader.GetFirstFont(path);

                Assert.IsNotNull(font);
                Assert.IsTrue(font.FamilyName == ValidateHelvetica.FamilyName);

                var metrics = font.GetMetrics(options);
                Assert.IsNotNull(metrics);

                var size = metrics.MeasureLine(TextToMeasure, 0, fontSize, availableWidth, options);

                Assert.AreEqual(TextToMeasure.Length, size.CharsFitted, "Should have fitted all the characters");
                Assert.IsFalse(size.OnWordBoudary, "Should not be breaking on a word boundary, as eveything fitted");
                Assert.AreEqual(11.4, Math.Round(size.RequiredHeight, 1), "Should use the typographic size, rather than header.");
            }
        }
Example #2
0
        public void GetHelveticaMetrics()
        {
            var path      = new FileInfo(Path.Combine(System.Environment.CurrentDirectory, ValidateHelvetica.UrlPath));
            var words     = TextToMeasure;
            var offset    = 0;
            var fontSize  = 12.0;
            var available = 1000; //fit all characters
            var options   = TypeMeasureOptions.Default;

            IFontMetrics metrics;

            using (var reader = new TypefaceReader())
            {
                var font = reader.GetFirstFont(path);

                metrics = font.GetMetrics(options);


                ValidateHelvetica.AssertMetrics(metrics);
                var size = metrics.MeasureLine(words, offset, fontSize, available, options);


                Assert.AreEqual(12.0, fontSize, "The measurements are for a point size of 12");
                Assert.AreEqual(words.Length, size.CharsFitted, "Should be able to fit everything in the first measurement");

                //checked to fit all at 140.53125
                Assert.AreEqual(140.53, Math.Round(size.RequiredWidth, 2), "The width of the string was not as statically caclulated");

                //checked height of a line to 12
                Assert.AreEqual(12.0, size.RequiredHeight, "The height of the string was not as statically calculated");

                // check 2
                //reduce the size so not all characters can fit.
                //expected 90.50 and fitted 19
                available = 90;
                size      = metrics.MeasureLine(words, offset, fontSize, available, options);

                //This is the text t
                Assert.AreEqual(18, size.CharsFitted);
                Assert.AreEqual(83.84, Math.Round(size.RequiredWidth, 2), "The width of the restricted string was not as statically calculated");
                Assert.AreEqual(12.0, size.RequiredHeight, "The height of the string was not as statically calculated");


                // check 3
                //now set breaking on words only
                options.BreakOnWordBoundaries = true;

                size = metrics.MeasureLine(words, offset, fontSize, available, options);
                //This is the text
                Assert.AreEqual(16, size.CharsFitted);
                Assert.AreEqual(77.17, Math.Round(size.RequiredWidth, 2), "The width of the restricted string was not as statically calculated");
                Assert.AreEqual(12.0, size.RequiredHeight, "The height of the string was not as statically calculated");

                //check 4
                //set the offset to last fitted and measure the rest
                offset = size.CharsFitted;
                size   = metrics.MeasureLine(words, offset, fontSize, available, options);
                Assert.AreEqual(words.Length - offset, size.CharsFitted, "Expected to fit all the remaining characters on the next measure (line)");
            }
        }
        public void ValidGetFirstFontGillSansUrl()
        {
            var path = new Uri(ValidateGillSans.RootUrl);

            using (var reader = new TypefaceReader(path))
            {
                var url = new Uri(ValidateGillSans.UrlPath, UriKind.Relative);

                var face = reader.GetFirstFont(url);

                ValidateGillSans.AssertMatches(ValidateGillSans.FontTypefaces[0], face);
            }
        }
        public void ValidGetFirstFontGillSansPath()
        {
            var path = new DirectoryInfo(System.Environment.CurrentDirectory);

            using (var reader = new TypefaceReader(path))
            {
                var url = new FileInfo(ValidateGillSans.UrlPath);

                var face = reader.GetFirstFont(url);

                ValidateGillSans.AssertMatches(ValidateGillSans.FontTypefaces[0], face);
            }
        }
        public void ValidGetTypefacesHachiFile()
        {
            var path = new DirectoryInfo(System.Environment.CurrentDirectory);

            using (var reader = new TypefaceReader(path))
            {
                var file = new FileInfo(ValidateHachi.UrlPath);

                var face = reader.GetFirstFont(file);
                Assert.IsNotNull(face);

                ValidateHachi.AssertTypeface(face);
            }
        }
        public void ValidGetFirstFontHelveticaUrl()
        {
            var path = new Uri(ValidateHelvetica.RootUrl);

            using (var reader = new TypefaceReader(path))
            {
                var url = new Uri(ValidateHelvetica.UrlPath, UriKind.Relative);

                var face = reader.GetFirstFont(url);


                ValidateHelvetica.AssertTypeface(face);
            }
        }
        public void TypeMeasureOptions_IgnoreStartingWhitespace()
        {
            var options = TypeMeasureOptions.BreakOnWords;

            options.IgnoreStartingWhiteSpace = true;

            Assert.IsFalse(options.CharacterSpacing.HasValue);
            Assert.IsFalse(options.WordSpacing.HasValue);

            Assert.IsTrue(options.BreakOnWordBoundaries, "The static break on words should have this flag as true");
            Assert.IsTrue(options.IgnoreStartingWhiteSpace, "The starting white space should be ignored");
            Assert.AreEqual(FontUnitType.UseFontPreference, options.FontUnits);


            using (var reader = new TypefaceReader())
            {
                ITypefaceFont font = reader.GetFirstFont(path);

                Assert.IsNotNull(font);
                Assert.IsTrue(font.FamilyName == ValidateHelvetica.FamilyName);

                var metrics = font.GetMetrics(options);
                Assert.IsNotNull(metrics);

                var size = metrics.MeasureLine(TextToMeasure, 0, fontSize, availableWidth, options);

                Assert.AreEqual(TextToMeasure.Length, size.CharsFitted, "Should have fitted all the characters");
                Assert.IsFalse(size.OnWordBoudary, "Should not be breaking on a word boundary, as eveything fitted");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");


                //Check the smaller size

                // "This is the text"
                // "to measure"

                size = metrics.MeasureLine(TextToMeasure, 0, fontSize, smallWidth, options);

                Assert.AreEqual(16, size.CharsFitted, "Can only fit 16 chars on smaller width with word break");
                Assert.IsTrue(size.OnWordBoudary, "Breaking on word boundary should be true");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");

                //Check the remaining text
                size = metrics.MeasureLine(TextToMeasure, size.CharsFitted, fontSize, smallWidth, options);

                Assert.AreEqual(17, size.FirstCharacter, "The space should be ignored and the first character should be the 't' at index 17");
                Assert.AreEqual(TextToMeasure.Length - size.FirstCharacter, size.CharsFitted, "Should have been able to fit the remaining text");
            }
        }
        public void TypeMeasureOptions_Default()
        {
            var options = TypeMeasureOptions.Default;

            Assert.IsFalse(options.CharacterSpacing.HasValue);
            Assert.IsFalse(options.WordSpacing.HasValue);

            Assert.IsFalse(options.BreakOnWordBoundaries);
            Assert.IsFalse(options.IgnoreStartingWhiteSpace);
            Assert.AreEqual(FontUnitType.UseFontPreference, options.FontUnits);


            using (var reader = new TypefaceReader())
            {
                ITypefaceFont font = reader.GetFirstFont(path);

                Assert.IsNotNull(font);
                Assert.IsTrue(font.FamilyName == ValidateHelvetica.FamilyName);

                var metrics = font.GetMetrics(options);
                Assert.IsNotNull(metrics);

                var size = metrics.MeasureLine(TextToMeasure, 0, fontSize, availableWidth, options);

                Assert.AreEqual(TextToMeasure.Length, size.CharsFitted, "Should have fitted all the characters");
                Assert.IsFalse(size.OnWordBoudary, "Should not be breaking on a word boundary");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");


                //Check the smaller size

                // "This is the text t"
                // "o measure"

                size = metrics.MeasureLine(TextToMeasure, 0, fontSize, smallWidth, options);

                Assert.AreEqual(18, size.CharsFitted, "Can only fit 18 chars on smaller width");
                Assert.IsFalse(size.OnWordBoudary, "Breaking on word boundary should be false");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");
            }
        }
        public void TypeMeasureOptions_CharacterAndWordSpacing()
        {
            var options = TypeMeasureOptions.Default;

            Assert.IsFalse(options.CharacterSpacing.HasValue);
            Assert.IsFalse(options.WordSpacing.HasValue);

            Assert.IsFalse(options.BreakOnWordBoundaries);
            Assert.IsFalse(options.IgnoreStartingWhiteSpace);
            Assert.AreEqual(FontUnitType.UseFontPreference, options.FontUnits);


            using (var reader = new TypefaceReader())
            {
                ITypefaceFont font = reader.GetFirstFont(path);

                Assert.IsNotNull(font);
                Assert.IsTrue(font.FamilyName == ValidateHelvetica.FamilyName);

                var metrics = font.GetMetrics(options);
                Assert.IsNotNull(metrics);

                //Without spacing should be 140.53 wide
                var size = metrics.MeasureLine(TextToMeasure, 0, fontSize, availableWidth, options);

                Assert.AreEqual(TextToMeasure.Length, size.CharsFitted, "Should have fitted all the characters");
                Assert.IsFalse(size.OnWordBoudary, "Should not be breaking on a word boundary");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");
                Assert.AreEqual(140.53, Math.Round(size.RequiredWidth, 2), "The width of the string was not as statically caclulated");

                options.CharacterSpacing = 2.0;
                options.WordSpacing      = 5.0;
                var extra = (options.CharacterSpacing * 22.0) + (options.WordSpacing * 5); // 22 character spaces + 5 word spaces in the text to measure


                size = metrics.MeasureLine(TextToMeasure, 0, fontSize, availableWidth, options);

                Assert.AreEqual(TextToMeasure.Length, size.CharsFitted, "Should have fitted all the characters");
                Assert.IsFalse(size.OnWordBoudary, "Should not be breaking on a word boundary");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");
                Assert.AreEqual(140.53 + extra, Math.Round(size.RequiredWidth, 2), "The width of the string did not match with the expected word and character spacing");


                //Check the smaller size with the extra spacing

                // "This is the tex"
                // "t to measure"

                size = metrics.MeasureLine(TextToMeasure, 0, fontSize, smallWidth, options);

                Assert.AreEqual(11, size.CharsFitted, "Can only fit 11 chars on smaller width with extra character and word space");
                Assert.IsFalse(size.OnWordBoudary, "Breaking on word boundary should be false");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");


                //Check the smaller size with the extra spacing and break on words

                // "This is the"
                // " text to measure"

                options.BreakOnWordBoundaries = true;

                size = metrics.MeasureLine(TextToMeasure, 0, fontSize, smallWidth, options);

                Assert.AreEqual(11, size.CharsFitted, "Can only fit 11 chars on smaller width with extra character space and breaking on words");
                Assert.IsTrue(size.OnWordBoudary, "Breaking on word boundary should be true");
                Assert.AreEqual(12.0, size.RequiredHeight, "Helvetica font preferences should use the font header size, rather than typographic.");
            }
        }