Table of Contents

Class FontLoader

Namespace
Egami.Fonts
Assembly
Egami.Fonts.dll

Tells what font data holds and loads every face of a collection.

public static class FontLoader
Inheritance
FontLoader
Inherited Members

Examples

// One face from a file; a collection takes an index. The bytes are read once and every table is parsed when first needed.
Typeface face = Typeface.FromFile(FontFile);
Console.WriteLine($"{face.FamilyName} {face.SubfamilyName}, {face.GlyphCount} glyphs, {face.UnitsPerEm} units per em");

// What a file holds, from its header alone.
FontProbe probe = FontLoader.Probe(File.ReadAllBytes(FontFile));
Console.WriteLine($"{probe.Kind}, {probe.FaceCount} face(s), {probe.Outlines} outlines");

// Damaged fonts load leniently with diagnostics; Strict throws instead.
var seen = new List<EgamiDiagnostic>();
var options = new FontLoadOptions { Context = EgamiContext.Default with { Diagnostics = seen.Add } };
Typeface lenient = Typeface.FromMemory(File.ReadAllBytes(FontFile), 0, options);

Methods

LoadAll(ReadOnlyMemory<byte>, FontLoadOptions?)

Loads every face of font data held in memory, which is kept and must not change afterwards.

public static IReadOnlyList<Typeface> LoadAll(ReadOnlyMemory<byte> data, FontLoadOptions? options = null)

Parameters

data ReadOnlyMemory<byte>

The font data.

options FontLoadOptions

How to load, or null for Default.

Returns

IReadOnlyList<Typeface>

The faces, sharing the memory.

Examples

// One face from a file; a collection takes an index. The bytes are read once and every table is parsed when first needed.
Typeface face = Typeface.FromFile(FontFile);
Console.WriteLine($"{face.FamilyName} {face.SubfamilyName}, {face.GlyphCount} glyphs, {face.UnitsPerEm} units per em");

// What a file holds, from its header alone.
FontProbe probe = FontLoader.Probe(File.ReadAllBytes(FontFile));
Console.WriteLine($"{probe.Kind}, {probe.FaceCount} face(s), {probe.Outlines} outlines");

// Damaged fonts load leniently with diagnostics; Strict throws instead.
var seen = new List<EgamiDiagnostic>();
var options = new FontLoadOptions { Context = EgamiContext.Default with { Diagnostics = seen.Add } };
Typeface lenient = Typeface.FromMemory(File.ReadAllBytes(FontFile), 0, options);

Exceptions

UnsupportedFormatException

The data is not a font Egami reads.

CorruptDataException

A face has no readable glyph data, or is damaged and strict loading is set.

LimitExceededException

The data is larger than the context's MaxTotalBytes.

LoadAll(string, FontLoadOptions?)

Loads every face of a font file: one for a single font or CFF, all of them for a collection.

public static IReadOnlyList<Typeface> LoadAll(string path, FontLoadOptions? options = null)

Parameters

path string

The file.

options FontLoadOptions

How to load, or null for Default.

Returns

IReadOnlyList<Typeface>

The faces, sharing the file's memory.

Examples

// One face from a file; a collection takes an index. The bytes are read once and every table is parsed when first needed.
Typeface face = Typeface.FromFile(FontFile);
Console.WriteLine($"{face.FamilyName} {face.SubfamilyName}, {face.GlyphCount} glyphs, {face.UnitsPerEm} units per em");

// What a file holds, from its header alone.
FontProbe probe = FontLoader.Probe(File.ReadAllBytes(FontFile));
Console.WriteLine($"{probe.Kind}, {probe.FaceCount} face(s), {probe.Outlines} outlines");

// Damaged fonts load leniently with diagnostics; Strict throws instead.
var seen = new List<EgamiDiagnostic>();
var options = new FontLoadOptions { Context = EgamiContext.Default with { Diagnostics = seen.Add } };
Typeface lenient = Typeface.FromMemory(File.ReadAllBytes(FontFile), 0, options);

Exceptions

UnsupportedFormatException

The file is not a font Egami reads.

CorruptDataException

A face has no readable glyph data, or is damaged and strict loading is set.

LimitExceededException

The file is larger than the context's MaxTotalBytes.

Probe(ReadOnlySpan<byte>)

Says whether the bytes are a single TrueType or OpenType font, a collection (and how many faces), a WOFF or WOFF2 file (and, for a WOFF2 collection, how many faces) or a bare CFF stream, from the headers alone, without parsing glyphs or decompressing.

public static FontProbe Probe(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The start of the data; the first 4 KiB are enough for every header Egami reads, except a WOFF2 file with a long table directory, whose face count and outlines need the whole directory (0 and null without it).

Returns

FontProbe

What the data is.