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 ")); } }
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 ")); } }
public void LoadInfoFromFullUrl() { var path = RootUrl; using (var reader = new TypefaceReader()) { path = path + UrlPath; var info = reader.ReadTypeface(path); ValidateHelvetica.AssertInfo(info, path, 5); } }
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); } }
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 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 FailLoadInfoFromFullUrl() { var path = RootUrl; using (var reader = new TypefaceReader()) { path = path + FailingUrlPath; Assert.ThrowsException <AggregateException>(() => { var info = reader.ReadTypeface(path); }); } }
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); } } }
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 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); } }); } }
public void ValidGetFontHelveticaUrlInfoAndIndex() { var path = new Uri(ValidateHelvetica.RootUrl); using (var reader = new TypefaceReader(path)) { var file = new Uri(ValidateHelvetica.UrlPath, UriKind.Relative); var info = reader.ReadTypeface(file); var face = reader.GetFont(info, 0); Assert.IsNotNull(face); ValidateHelvetica.AssertTypeface(face); } }
public void ValidGetFontHelveticaUrlAndReference() { var path = new Uri(ValidateHelvetica.RootUrl); using (var reader = new TypefaceReader(path)) { var uri = new Uri(ValidateHelvetica.UrlPath, UriKind.Relative); var info = reader.ReadTypeface(uri); var fref = info.Fonts[0]; //Load from a known url and a font reference var face = reader.GetFont(uri, fref); Assert.IsNotNull(face); ValidateHelvetica.AssertTypeface(face); } }
public void ValidGetFontGillSansSemiBoldItalicFileInfoAndReference() { var path = new DirectoryInfo(System.Environment.CurrentDirectory); using (var reader = new TypefaceReader(path)) { var file = new FileInfo(ValidateGillSans.UrlPath); var index = ValidateGillSans.SemiBoldItalicIndex; var info = reader.ReadTypeface(file); var fref = info.Fonts[index]; //Load from a known file and a font reference var face = reader.GetFont(file, fref); Assert.IsNotNull(face); ValidateGillSans.AssertMatches(ValidateGillSans.FontTypefaces[index], face); } }
public void ValidGetFontHelveticaFileInfoAndIndex() { var path = new DirectoryInfo(System.Environment.CurrentDirectory); using (var reader = new TypefaceReader(path)) { var file = new FileInfo(ValidateHelvetica.UrlPath); var info = reader.ReadTypeface(file); //Get the font with the info and an index of 0 var face = reader.GetFont(info, 0); Assert.IsNotNull(face); ValidateHelvetica.AssertTypeface(face); } }
public async Task GetFontsAsyncFromInfo() { ITypefaceInfo gill; var path = new DirectoryInfo(System.Environment.CurrentDirectory); var file = new FileInfo(System.IO.Path.Combine(path.FullName, ValidateGillSans.UrlPath)); using (var reader = new TypefaceReader()) { gill = reader.ReadTypeface(file); } //Clean reader with a typeface info using (var reader = new TypefaceReader()) { var faces = await reader.GetFontsAsync(gill); ValidateGillSans.AssertTypefaces(faces.ToArray()); } }
public void FailLoadInfoFromPartialUrl() { var path = RootUrl; TypefaceReader reader; StreamLoader loader; using (reader = new TypefaceReader(new Uri(path))) { path = FailingUrlPath; Assert.ThrowsException <AggregateException>(() => { var info = reader.ReadTypeface(path); }); 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 disposed and set to null"); }
public void ValidGetFontGillSansBoldInfoAndRef() { var path = new DirectoryInfo(System.Environment.CurrentDirectory); using (var reader = new TypefaceReader(path)) { var file = new FileInfo(ValidateGillSans.UrlPath); var index = ValidateGillSans.BoldRegularIndex; var info = reader.ReadTypeface(file); var fref = info.Fonts[index]; //Get the font with the info and the font reference var face = reader.GetFont(info, fref); Assert.IsNotNull(face); //Make sure the style matches var expected = ValidateGillSans.FontTypefaces[index]; ValidateGillSans.AssertMatches(expected, face); } }
public void LoadInfoFromPartialUrl() { var path = RootUrl; TypefaceReader reader; StreamLoader loader; using (reader = new TypefaceReader(new Uri(path))) { path = UrlPath; var info = reader.ReadTypeface(path); ValidateHelvetica.AssertInfo(info, path, 6); //check http is set Assert.IsNotNull(reader.Loader.Client, "The loader should have a client as it was not provided, but needed"); Assert.IsTrue(reader.Loader.OwnsClient, "The loader should own the client as it was not 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 disposed and set to null"); }
private async static Task MeasureStringFor(FontDownload loader, string path) { byte[] data = null; data = await loader.LoadFrom(path); #if UseOpenFont ZlibDecompressStreamFunc zipfunc = (byte[] dataIn, byte[] output) => { using (var streamIn = new MemoryStream(dataIn)) { #if NET6_0 ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream input = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(streamIn); using (var streamOut = new MemoryStream(output)) input.CopyTo(streamOut); #endif return(true); } }; WoffDefaultZlibDecompressFunc.DecompressHandler = zipfunc; BrotliDecompressStreamFunc brotlifunc = (byte[] dataIn, Stream dataOut) => { using (var streamIn = new MemoryStream(dataIn)) { System.IO.Compression.BrotliStream deflate = new System.IO.Compression.BrotliStream(streamIn, System.IO.Compression.CompressionMode.Decompress); deflate.CopyTo(dataOut); return(true); } }; Woff2DefaultBrotliDecompressFunc.DecompressHandler = brotlifunc; using (var ms = new System.IO.MemoryStream(data)) { var reader = new Typography.OpenFont.OpenFontReader(); var preview = reader.ReadPreview(ms); Console.WriteLine("Loaded font reference " + preview.Name); } using (var ms = new System.IO.MemoryStream(data)) { var reader = new Typography.OpenFont.OpenFontReader(); var full = reader.Read(ms); string text = "This is the text to measure"; var encoding = Scryber.OpenType.SubTables.CMapEncoding.WindowsUnicode; int fitted; var size = full.MeasureString(text, 0, 12, 10000, true, out fitted); Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length); } #else TypefaceReader tfreader = new TypefaceReader(); ITypefaceInfo info; using (var ms = new System.IO.MemoryStream(data)) { info = tfreader.ReadTypeface(ms, path); if (null == info) { ExitClean("Could not read the info from the font file"); return; } else if (info.FontCount == 0) { ExitClean("No fonts could be read from the data: " + info.ErrorMessage ?? "Unknown error"); return; } else { Console.WriteLine("Read " + info.FontCount + " typefaces from the font file " + info.Source); foreach (var reference in info.Fonts) { Console.WriteLine(" " + reference.FamilyName + " (weight: " + reference.FontWeight.ToString() + ", width: " + reference.FontWidth + ", restrictions : " + reference.Restrictions + ", selections : " + reference.Selections.ToString() + ")"); } } } TypeMeasureOptions options = new TypeMeasureOptions(); using (var ms = new System.IO.MemoryStream(data)) { foreach (var fref in info.Fonts) { Console.WriteLine(); ms.Position = 0; var typeface = tfreader.GetFont(ms, info.Source, fref); if (null == typeface || typeface.IsValid == false) { ExitClean("The font " + fref.ToString() + " was not valid"); return; } else { Console.WriteLine("Loaded font reference " + typeface.ToString()); } var metrics = typeface.GetMetrics(options); if (null != metrics) { var line = metrics.MeasureLine("This is the text to measure", 0, 12.0, 10000, options); Console.WriteLine("Measured the string to " + line.RequiredWidth + ", " + line.RequiredHeight + " points, and fitted " + line.CharsFitted + " chars" + (line.OnWordBoudary ? " breaking on the word boundary." : ".")); } else { ExitClean("No metrics were returned for the font " + typeface.ToString()); return; } var ttfData = typeface.GetFileData(DataFormat.TTF); if (null == ttfData) { ExitClean("No data was returned in TTF format for the font " + typeface.ToString()); } else { Console.WriteLine("TrueType font data was extracted at " + ttfData.Length + " bytes from the original data of " + data.Length); } var name = typeface.FamilyName; if (typeface.FontWeight != WeightClass.Normal) { if (name.IndexOf(typeface.FontWeight.ToString()) < 0) { name += " " + typeface.FontWeight.ToString(); } } if ((typeface.Selections & FontSelection.Italic) > 0) { if (name.IndexOf("Italic") < 0) { name += " Italic"; } } loader.SaveToLocal("Output", name + ".ttf", ttfData); } } #if Legacy var ttf = new Scryber.OpenType.TTFFile(data, 0); Console.WriteLine("Loaded font file " + ttf.ToString()); var encoding = Scryber.OpenType.SubTables.CMapEncoding.WindowsUnicode; string text = "This is the text to measure"; int fitted; var size = ttf.MeasureString(encoding, text, 0, 12, 1000, true, out fitted); Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length); //Measure 90 wiout word boundary size = ttf.MeasureString(encoding, text, 0, 12, 90, false, out fitted); Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length + " to string '" + text.Substring(0, fitted) + "'"); //Measure 90 with word boundary size = ttf.MeasureString(encoding, text, 0, 12, 90, true, out fitted); Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length + " to string '" + text.Substring(0, fitted) + "'"); #endif #if Performance var stopWatch = System.Diagnostics.Stopwatch.StartNew(); MeasureStringMeasurer(ttf); stopWatch.Stop(); Console.WriteLine("To measure 4 different strings " + maxRepeat + " times took " + stopWatch.Elapsed.TotalMilliseconds + "ms"); #endif #endif }