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.");
            }
        }
Beispiel #2
0
        public void FailLoadInfoWithHttp()
        {
            var            path = RootUrl;
            TypefaceReader reader;
            StreamLoader   loader;

            using (var http = new System.Net.Http.HttpClient())
            {
                using (reader = new TypefaceReader(http))
                {
                    Assert.IsNotNull(reader.Loader.Client, "The loader SHOULD have a client as it was provided");

                    path += FailingUrlPath;

                    Assert.ThrowsException <AggregateException>(() =>
                    {
                        var info = reader.ReadTypeface(path);
                    });

                    //check http is set
                    Assert.IsNotNull(reader.Loader.Client, "The loader should STILL have a client as it was provided");
                    Assert.IsFalse(reader.Loader.OwnsClient, "The loader should NOT own the client as it was  provided");

                    loader = reader.Loader;
                }
                //check clean up
                Assert.IsNull(reader.Loader, "The readers loader should have been set to null");
                Assert.IsNull(loader.Client, "The loaders http should have been set to null, but not disposed");

                //Simple check to make sure we are still able to use the http client
                var data = http.GetStringAsync(CheckAliveUrl).Result;
                Assert.IsNotNull(data);
                Assert.IsTrue(data.StartsWith("<Project "));
            }
        }
Beispiel #3
0
        public void TryLoadFromUrlStream()
        {
            ITypefaceInfo info;
            bool          result;

            using (var reader = new TypefaceReader())
            {
                var path = RootUrl;

                //valid path
                path = path + UrlPath;

                using (var http = new HttpClient())
                {
                    //We need a seekable stream, so download to a buffer and use that.
                    var data = http.GetByteArrayAsync(path).Result;

                    using (var stream = new MemoryStream(data))
                        result = reader.TryReadTypeface(stream, path, out info);

                    Assert.IsTrue(result, "Reported as false, to read the input from a stream");
                    Assert.IsNotNull(info, "Info was not returned");
                    Assert.IsTrue(string.IsNullOrEmpty(info.ErrorMessage), "An Error message was returned");
                    //check the info
                    ValidateHelvetica.AssertInfo(info, path, 3);
                }
            }
        }
Beispiel #4
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)");
            }
        }
Beispiel #5
0
        public void TryFailLoadFromUrlStream()
        {
            ITypefaceInfo info;
            bool          result;

            using (var reader = new TypefaceReader())
            {
                var path = RootUrl;

                //valid path
                path = path + UrlPath;

                using (var http = new HttpClient())
                {
                    //We need a seekable stream, this will fail.

                    using (var stream = http.GetStreamAsync(path).Result)
                        result = reader.TryReadTypeface(stream, path, out info);

                    Assert.IsFalse(result, "Reported as true, to read the input from a seekable stream");
                    Assert.IsNotNull(info, "Info was not returned");
                    Assert.IsFalse(string.IsNullOrEmpty(info.ErrorMessage), "No Error message was returned");
                }
            }
        }
Beispiel #6
0
        public void ParameterUriClient()
        {
            var root = new Uri("https://fonts.gstatic.com/s/");

            Utility.RootedUriStreamLoader loader;

            using (var client = new System.Net.Http.HttpClient())
            {
                using (var reader = new TypefaceReader(root, client))
                {
                    Assert.IsNotNull(reader);
                    Assert.IsNotNull(reader.Loader);
                    Assert.IsInstanceOfType(reader.Loader, typeof(Utility.RootedUriStreamLoader), "The uri constructor should have an rooted uri stream loader");

                    loader = reader.Loader as Utility.RootedUriStreamLoader;
                    Assert.IsNotNull(loader);
                    Assert.IsNotNull(loader.Client, "The client was not set");
                    Assert.AreSame(loader.Client, client);
                    Assert.IsFalse(loader.OwnsClient, "The loader should not own the http client");

                    Assert.AreEqual(root, loader.BaseUri, "The base uri's do not match");
                }
                //This should be removed if the reader is disposed.
                Assert.IsNull(loader.Client);
            }
        }
Beispiel #7
0
        public void LoadInfoFromPartialUrlWithHttp()
        {
            var            path = RootUrl;
            TypefaceReader reader;
            StreamLoader   loader;

            using (var http = new System.Net.Http.HttpClient())
            {
                using (reader = new TypefaceReader(new Uri(path), http))
                {
                    Assert.IsNotNull(reader.Loader.Client, "The loader SHOULD have a client as it was provided");

                    path = UrlPath;
                    var info = reader.ReadTypeface(path);
                    ValidateHelvetica.AssertInfo(info, path, 7);

                    //check http is set
                    Assert.IsNotNull(reader.Loader.Client, "The loader should STILL have a client as it was provided");
                    Assert.IsFalse(reader.Loader.OwnsClient, "The loader should NOT own the client as it was  provided");

                    loader = reader.Loader;
                }
                //check clean up
                Assert.IsNull(reader.Loader, "The readers loader should have been set to null");
                Assert.IsNull(loader.Client, "The loaders http should have been set to null, but not disposed");

                //Simple check to make sure we are still able to use the http client
                var data = http.GetStringAsync(CheckAliveUrl).Result;
                Assert.IsNotNull(data);
                Assert.IsTrue(data.StartsWith("<Project "));
            }
        }
Beispiel #8
0
        public void ParameterLess()
        {
            var reader = new TypefaceReader();

            Assert.IsNotNull(reader);
            Assert.IsNotNull(reader.Loader);
            Assert.IsInstanceOfType(reader.Loader, typeof(Utility.UnRootedStreamLoader), "The parameterless constructor should have an unrooted stream loader");
        }
Beispiel #9
0
        public void LoadInfoFromFilePath()
        {
            using (var reader = new TypefaceReader())
            {
                var path = System.Environment.CurrentDirectory;
                path = Path.Combine(path, PartialFilePath);

                var info = reader.ReadTypeface(path);
                ValidateHelvetica.AssertInfo(info, path, 2);
            }
        }
Beispiel #10
0
        public void LoadInfoFromFullUrl()
        {
            var path = RootUrl;

            using (var reader = new TypefaceReader())
            {
                path = path + UrlPath;
                var info = reader.ReadTypeface(path);
                ValidateHelvetica.AssertInfo(info, path, 5);
            }
        }
Beispiel #11
0
        public void ParameterCustomLoader()
        {
            CustomLoader loader = new CustomLoader();

            using (var reader = new TypefaceReader(loader))
            {
                Assert.IsNotNull(reader);
                Assert.IsNotNull(reader.Loader);
                Assert.IsInstanceOfType(reader.Loader, typeof(CustomLoader), "The custom constructor should be set so it can be used");
            }
        }
Beispiel #12
0
        public void GetGillSansBlackMetrics()
        {
            var path      = new FileInfo(Path.Combine(System.Environment.CurrentDirectory, ValidateGillSans.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.GetFont(path, ValidateGillSans.BlackRegularIndex);

                metrics = font.GetMetrics(options);


                ValidateGillSans.AssertBlackMetrics(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(199.46, Math.Round(size.RequiredWidth, 2), "The width of the string was not as statically caclulated");

                //checked height of a line to 12
                Assert.AreEqual(14.95, Math.Round(size.RequiredHeight, 2), "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 t
                Assert.AreEqual(13, size.CharsFitted);
                Assert.AreEqual(86.06, Math.Round(size.RequiredWidth, 2), "The width of the restricted string was not as statically calculated");
                Assert.AreEqual(14.95, Math.Round(size.RequiredHeight, 2), "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
                Assert.AreEqual(11, size.CharsFitted);
                Assert.AreEqual(75.30, Math.Round(size.RequiredWidth, 2), "The width of the restricted string was not as statically calculated");
                Assert.AreEqual(14.95, Math.Round(size.RequiredHeight, 2), "The height of the string was not as statically calculated");
            }
        }
Beispiel #13
0
        public void LoadInfoFromDirectoryAndPath()
        {
            var path = System.Environment.CurrentDirectory;
            var di   = new System.IO.DirectoryInfo(path);

            using (var reader = new TypefaceReader(di))
            {
                path = PartialFilePath;
                var info = reader.ReadTypeface(path);
                ValidateHelvetica.AssertInfo(info, path, 4);
            }
        }
        public async Task GetFontsAsyncFromFile()
        {
            var path = new DirectoryInfo(System.Environment.CurrentDirectory);
            var file = new FileInfo(System.IO.Path.Combine(path.FullName, ValidateGillSans.UrlPath));


            using (var reader = new TypefaceReader())
            {
                var faces = await reader.GetFontsAsync(file);

                ValidateGillSans.AssertTypefaces(faces.ToArray());
            }
        }
        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 async Task GetFontsAsyncFromUri()
        {
            var path = new Uri(ValidateGillSans.RootUrl + ValidateGillSans.UrlPath);

            //Clean reader with a file stream

            using (var reader = new TypefaceReader())
            {
                var faces = await reader.GetFontsAsync(path);

                ValidateGillSans.AssertTypefaces(faces.ToArray());
            }
        }
Beispiel #18
0
        public void FailLoadInfoFromFilePath()
        {
            using (var reader = new TypefaceReader())
            {
                var path = System.Environment.CurrentDirectory;
                path = Path.Combine(path, FailingPartialFilePath);

                Assert.ThrowsException <TypefaceReadException>(() =>
                {
                    var info = reader.ReadTypeface(path);
                });
            }
        }
        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);
            }
        }
Beispiel #21
0
        public void LoadInfoFromFileStream()
        {
            using (var reader = new TypefaceReader())
            {
                var path = System.Environment.CurrentDirectory;
                path = Path.Combine(path, PartialFilePath);

                using (var stream = new FileStream(path, FileMode.Open))
                {
                    var info = reader.ReadTypeface(stream, path);
                    ValidateHelvetica.AssertInfo(info, path, 1);
                }
            }
        }
Beispiel #22
0
        public void FailLoadInfoFromFullUrl()
        {
            var path = RootUrl;

            using (var reader = new TypefaceReader())
            {
                path = path + FailingUrlPath;

                Assert.ThrowsException <AggregateException>(() =>
                {
                    var info = reader.ReadTypeface(path);
                });
            }
        }
        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 ValidGetFontHelveticaPathAndIndex()
        {
            var path = new DirectoryInfo(System.Environment.CurrentDirectory);

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

                var face = reader.GetFont(file, 0);
                Assert.IsNotNull(face);


                ValidateHelvetica.AssertTypeface(face);
            }
        }
Beispiel #25
0
        public void ParameterUri()
        {
            var root   = new Uri("https://fonts.gstatic.com/s/");
            var reader = new TypefaceReader(root);

            Assert.IsNotNull(reader);

            Assert.IsNotNull(reader.Loader);
            Assert.IsInstanceOfType(reader.Loader, typeof(Utility.RootedUriStreamLoader), "The uri constructor should have an rooted uri stream loader");
            var loader = reader.Loader as Utility.RootedUriStreamLoader;

            Assert.IsNotNull(loader);
            Assert.IsNull(loader.Client, "An unused loader should not have an existing client");
            Assert.AreEqual(root, loader.BaseUri, "The base uri's do not match");
        }
        public async Task ValidGetFirstFontHelveticaPath()
        {
            var path = new DirectoryInfo(System.Environment.CurrentDirectory);

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

                var face = await reader.GetFirstFontAsync(file);

                Assert.IsNotNull(face);

                ValidateHelvetica.AssertTypeface(face);
            }
        }
        public void ValidGetFontGillSansBold()
        {
            var path = new DirectoryInfo(System.Environment.CurrentDirectory);

            using (var reader = new TypefaceReader(path))
            {
                var file  = new FileInfo(ValidateGillSans.UrlPath);
                var index = ValidateGillSans.BoldRegularIndex;

                var face = reader.GetFont(file, index);
                Assert.IsNotNull(face);

                ValidateGillSans.AssertMatches(ValidateGillSans.FontTypefaces[index], face);
            }
        }
Beispiel #28
0
        public void FailLoadInfoFromDirectoryAndPath()
        {
            var path = System.Environment.CurrentDirectory;
            var di   = new System.IO.DirectoryInfo(path);

            using (var reader = new TypefaceReader(di))
            {
                path = FailingPartialFilePath;

                Assert.ThrowsException <TypefaceReadException>(() =>
                {
                    var info = reader.ReadTypeface(path);
                });
            }
        }
        public async Task ValidGetFontHelveticaUrlAndIndex()
        {
            var path = new Uri(ValidateHelvetica.RootUrl);

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

                var face = await reader.GetFontAsync(url, 0);

                Assert.IsNotNull(face);

                ValidateHelvetica.AssertTypeface(face);
            }
        }
Beispiel #30
0
        public void FailLoadFromFileStream()
        {
            using (var reader = new TypefaceReader())
            {
                var path = System.Environment.CurrentDirectory;
                path = Path.Combine(path, FailingPartialFilePath);

                Assert.ThrowsException <DirectoryNotFoundException>(() =>
                {
                    using (var stream = new FileStream(path, FileMode.Open))
                    {
                        var info = reader.ReadTypeface(stream, path);
                    }
                });
            }
        }