Scryber.Core.OpenType
6.0.0-alpha.2
See the version list below for details.
dotnet add package Scryber.Core.OpenType --version 6.0.0-alpha.2
NuGet\Install-Package Scryber.Core.OpenType -Version 6.0.0-alpha.2
<PackageReference Include="Scryber.Core.OpenType" Version="6.0.0-alpha.2" />
paket add Scryber.Core.OpenType --version 6.0.0-alpha.2
#r "nuget: Scryber.Core.OpenType, 6.0.0-alpha.2"
// Install Scryber.Core.OpenType as a Cake Addin #addin nuget:?package=Scryber.Core.OpenType&version=6.0.0-alpha.2&prerelease // Install Scryber.Core.OpenType as a Cake Tool #tool nuget:?package=Scryber.Core.OpenType&version=6.0.0-alpha.2&prerelease
Scryber.Core.OpenType
This repository is the OpenType library capability for Scryber, but placed independently in case it helps others.
It allows the...
- Reading of multiple font files from various sources,
- Measuring lines of characters within available space using that font.
- The conversion from ttc or woff to open type byte arrays or streams.
- Both syncronous and asyncronous loading of typefaces and fonts.
This latest version (6.0.0-alpha at the time of writing) supports the following frameworks
- .NET Standard 2.0 (inc .NET Framework 4.8, dotnet core 3.1)
- .NET 5.0
Supported Typeface Formats
The Scryber.Core.OpenType
library supports reading typefaces and font programmes in the following formats:
- True Type fonts.
- Open Type fonts (including ccf).
- True Type collections
- Woff fonts (version 1)
Woff2 is coming, but need to resolve the glyph tables back into true type format first.
Using the library
If you want to use the library in your own projects or build on the base library, please feel free. And if you have any troublesome fonts, please just call them out. Happy to help, as it helps us too.
See the read me file at https://github.com/richard-scryber/Scryber.Core.OpenType for more usage information
In this document we use the terms 'Typeface' to refer to a single file or stream, and 'Font' to refer to the one or more font programmes in that file or stream
1. Create a TypefaceReader
The TypefaceReader (Scryber.OpenType.TypefaceReader
) is the primary entry point to the library and there are multiple constructors for the disposable class that can read a typeface from either a url, file, stream or custom StreamLoader (Scryber.OpenType.Utility.StreamLoader
).
The overloads also allow a base path to be defined and also an 'HttpClient or WebClient' to be passed rather than a new one be created on demand by each reader instance.
//using Scryber.OpenType;
using(var reader = new TypefaceReader())
{
}
2. Reading the information in a typeface file.
Once a reader is created it can be used to read one or more typefaces from a stream, file or url using the ReadTypeface
or ReadTypefaceAsync
variants.
//using Scryber.OpenType
using(var reader = new TypefaceReader())
{
var file = new FileInfo("[path to font]"); //new Uri("[absolute or relative url]");
var face = reader.ReadTypeface(file); //await reader.ReadTypefaceAsync(url);
}
The library uses the .net framework FileInfo
and Uri
classes along with the Stream classes to distinguish easily between file paths and urls, rather than trusting strings.
3. Reading font programmes
A TypefaceReader
also reads the font programmes within the the files or streams with the GetFont
and similar methods, allowing both synchronous and asyncronous loading of font programmes.
All methods return either a single or a collection of ITypefaceFont
(s) from a previous ITypefaceInfo or IFontInfo, or paths or streams.
using(var reader = new TypefaceReader())
{
var file = new FileInfo("[path to font]"); //new Uri("[absolute or relative url]");
var font = reader.GetFonts(file); //await reader.GetFontsAsync(url);
}
The returned ITypefaceFont
is also an IFontInfo
interface so can be used interchangeably but add functionality for extracting tables, data and getting the font metrics.
There are no TryGetFont methods as the info access should be used to check fonts, and any other failures would point to an invalid font (or an issue with the library).
3.1. The ITypefaceFont
The ITypefaceFont has a GetFileData
method that will return a byte[] for the data that can be saved to another stream matching the format requested, if that is supported
The CanGetFileData
will return true if the conversion would be supported.
It also supports the font metrics using the GetMetrics
method.
The returned instances can also support the IOpenTypeFont. This interface enables access to the inner tables in an open/true type font programme, along with any decoded SubTables
.
3.2. Extracting the font data
Primarily the ONLY fully supported format is DataFormat.TTF
, and will support the conversion if the original source was OpenType, TrueType, TrueTypeCollection, or Woff v1.
//using System.IO;
using(var reader = new TypefaceReader())
{
var file = new FileInfo("[path to font]"); //new Uri("[absolute or relative url]");
var font = reader.GetFont(file, 0); //await reader.GetFontsAsync(url);
if(font.CanGetFileData(DataFormat.TTF))
File.WriteAllBytes("[new path].ttf", font.GetFileData(DataFormat.TTF);
}
3.3 Supported conversion
The following table shows the input data type and supported output type for an ITypefaceFont.
Source Data | Output data types |
---|---|
True Type | TTF |
Open Type | TTF |
Collection | TTF or TTC |
Woff | TTF or WOFF |
It may be that we will add more in the future, but it meets the needs of our usage at the moment.
3.4 Measuring strings
The font metrics returned from the ITypefaceFont
enables the measurement of strings of characters in a font programme.
At the moment it is fairly basic, only supporting horizontal characters, word and character spacing, and word boundaries, nor it does not use the kerning tables.
However it is fast.
//using System.IO;
using(var reader = new TypefaceReader())
{
var file = new FileInfo("[path to font]"); //new Uri("[absolute or relative url]");
var font = reader.GetFont(file, 0); //await reader.GetFontsAsync(url);
var options = new TypeMeasureOptions() { BreakOnWordBoundaries = true };
//Get the standard font metrics for the font.
var metrics = font.GetMetrics(options);
var chars = "This is my line of text";
var start = 0;
var fontSize = 12.0;
var max = 200.0;
var size = metrics.MeasureLine(chars, start, fontSize, max, options);
Console.WriteLine("Fitted " + size.CharsFitted + " in "
+ size.RequiredWidth
+ (OnWordBoundary ? ", breaking on a word boundary" : ""));
}
The font metrics instance holds a reference to the font programme, but can also add lookup tables for common and used characters. A single instance can be held to measure any number of font sizes. If multiple fonts are used, they, and the data they contain will not be garbage collected if the measure is still referenced.
5. With thanks
We have referred to a lot of other open source software and libraries to get this code together, and it is with thanks to them that we can have been able to do this.
- iTextSharp in it's various releases,
- SharpZipLib for decompression where decomression is not available
- Typography.OpenFont for the Woff and Woff2 formats
These are brilliant libraries and assets to the open source community.
Happy coding.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- SharpZipLib (>= 1.3.3)
-
net5.0
- SharpZipLib (>= 1.3.3)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Scryber.Core.OpenType:
Package | Downloads |
---|---|
AuthScape.WhiteLabel
Package Description |
|
AuthScape.PrivateLabel
Package Description |
|
Scryber.Core.Experimental
The scryber pdf engine for dotnet 5 - Beautiful documents from templates made easy. Scryber is an advanced PDF generation engine based around HTML templates with CSS styles and SVG drawing. It includes full flowing pages, dynamic template binding on your object model and referenced external files, images, css and fonts. Easily create documents from your Apps, MVC sites, or Javascipt ajax calls. This framework is built entirely in .NET5 and is released under the LGPL licence so you can use to it in commercial applications. |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Scryber.Core.OpenType:
Repository | Stars |
---|---|
grandnode/grandnode2
Open-Source eCommerce Platform on .NET Core, MongoDB, AWS DocumentDB, Azure CosmosDB, LiteDB & Vue.js
|
|
richard-scryber/scryber.core
Scryber.Core is a dotnet 6 html to pdf engine written entirely in C# for creating beautiful flowing documents from html templates including css styles, object data binding and svg drawing.
|
Version | Downloads | Last updated |
---|---|---|
6.1.0-beta | 8,036 | 1/6/2024 |
6.0.3.3-beta | 2,206 | 1/2/2024 |
6.0.1-beta | 98 | 1/6/2024 |
6.0.0.7-beta | 116 | 1/2/2024 |
6.0.0.6-beta | 2,024 | 1/1/2024 |
6.0.0.5-beta | 30,653 | 9/16/2022 |
6.0.0-alpha.2 | 2,451 | 11/1/2021 |
6.0.0-alpha | 250 | 10/26/2021 |
5.0.3 | 72,280 | 1/18/2021 |
5.0.1 | 405 | 1/18/2021 |
5.0.0 | 422 | 1/16/2021 |
5.0.0-alpha | 1,395 | 12/22/2020 |
1.0.0 | 7,010 | 5/23/2020 |
Release for the 6.0.0-alpha of the core engine.
Now supports the otf, ttf, ttc and woff file formats for font reading and measuring.
Compatible with net standard 2.0 and net 5, along with 6.0 RC1 checks.
There is a change in usage to the Scryber.OpenType.TypefaceReader along with returning a LineSize struct, rather than a System.Drawing.SizeF struct.
This is the base type for reading OpenType, TTF and TTC font files used in scryber, but is independent from any PDF creation requirements.
See the read me at https://github.com/richard-scryber/Scryber.Core.OpenType for more usage information.