Three small APIs write fonts. They exist for programs that carry the glyphs of another format onto a web page (a PDF's fonts as web fonts, for example): the glyph shapes, advances and the text each glyph stands for come from the caller, and the result is an ordinary font that browsers and any OpenType reader accept.
Build a font
var fb = new FontBuilder(unitsPerEm: 1000) { FamilyName = "Page 1 Font", Ascent = 880, Descent = -120 }; var path = new GlyphOutlineBuilder(); // cubic and quadratic segments, y up, font units path.MoveTo(50, 0); path.LineTo(450, 0); path.CubicTo(500, 200, 500, 500, 450, 700); path.Close(); int a = fb.AddGlyph(path.ToOutline(), advance: 500); fb.Map('a', a); // code point to glyph (BMP and supplementary) fb.MapSequence(['f', 'f', 'i'], fb.AddGlyph(ffiOutline, 1100)); // "ffi" shows one glyph fb.SetVerticalMetrics(a, advanceHeight: 1000, originY: 880); // only for vertical text byte[] otf = fb.Build(); // OpenType, CFF outlines byte[] woff2 = WebFont.ToWoff2(otf);
- Outlines are stored as Type 2 charstrings at 1/65536 font unit, so cubic input is exact; quadratic segments are raised to cubics, also exact. Coordinates must lie within ±16383 units. There is no hinting and no glyph names. The font is CID-keyed with CIDs equal to glyph ids.
- Advances are the integers given. Pick
unitsPerEmso the source widths fit (1000 for PDF widths). - Sequences go through a GSUB ligature lookup (type 4) registered under
ccmpandrligfor the default script. Those two are applied by browsers and other shaping engines in every case:font-variant-ligatures: noneswitches offligaandclig, and CSS says optional ligatures are not applied whenletter-spacingis non-zero, but neither touches composition (ccmp) or required ligatures (rlig). So the text a reader sees, selects and copies keeps its clusters. Longer sequences win over shorter ones that start the same way. A sequence's code points must each have a glyph for the browser to find the lookup; components nobody mapped get a shared empty glyph with no advance. Very large sets are split over extension subtables; thousands of sequences starting with the same glyph are refused. - Vertical metrics write
vhea,vmtxandVORG. Browsers use them forwriting-mode: vertical-*withtext-orientation: upright. - Deterministic: tables are written in a fixed order with fixed dates, so the same calls give the same bytes.
Subset a font
var s = new FontSubsetter(FontFace.Load(bytes)); s.Map('H', face.GetGlyphIndex('H')); // keep the glyph and map a code point to it s.MapSequence([0x66, 0x69], face.GetGlyphIndex(0xFB01)); byte[] subset = s.Build(); int newId = s.GetSubsetGlyphId(face.GetGlyphIndex('H'));
Outlines, hinting programs and advances are copied as they are; glyph ids are renumbered in their original order (glyph 0 stays .notdef); composite TrueType glyphs bring their components. The result keeps head, hhea, maxp, hmtx, OS/2, name, post (names dropped), gasp, cvt, fpgm, prep, the vertical tables, and a new cmap (and GSUB when sequences were given). Everything else is dropped, notably the original GSUB, GPOS, GDEF, kern, colour and variation tables; variable fonts are cut at their default instance. CFF2 fonts are not supported (NotSupportedException). In CFF fonts, subroutines are kept whole, and a glyph built with the old accent form of endchar (seac) keeps its charstring, so its accent is not followed (the same as FontFace.GetOutline).
The input is untrusted. All reads are bounds-checked, work is bounded by the font's size, and malformed fonts raise FontFormatException. The font's embedding permissions (OS/2 fsType) are copied, not enforced: honouring them is the caller's job.
A browser's automatic hinting calibrates on the characters a font maps, so a hinted subset can differ from its source in a few pixels at small sizes; unhinted drawing is identical.
Pack as a web font
WebFont.ToWoff2(byte[] sfnt) writes WOFF 2.0 (W3C Recommendation): the tables, unchanged, in one Brotli stream at quality 11 (the format's null transform for glyf and loca; no table is rewritten), without DSIG, and with bit 11 of the head flags set as encoders must. WebFont.ToWoff writes WOFF 1.0 with a zlib stream per table. Both reject collections and malformed table directories with FontFormatException. Brotli comes from the .NET base class library; the output is the same for the same input on one runtime version.
Checked against a browser
tools/WebFontCheck writes built, subset and packed fonts, then check.py loads each (raw, WOFF and WOFF2) in headless Chrome as an @font-face and checks that: the browser reports the font loaded; the drawn glyphs overlap the outlines OneFont reads back from the same font (about 0.93 to 0.99 for unhinted outlines); widths equal the sum of the advances; selecting the text gives the page text; ligature sequences still apply with font-variant-ligatures: none and letter-spacing; vertical text uses the vertical advances; and subset fonts draw the same as the font they were cut from. fontTools opens every output as a second reader.