Pack one TTF or OTF for the web. NotoSans-Regular.ttf is included in the example ZIP. onefont writes WOFF and WOFF2 but doesn’t read them; start with the original TTF or OTF.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; const string name = "NotoSans-Regular"; byte[] font = File.ReadAllBytes(name + ".ttf"); File.WriteAllBytes(name + ".woff2", WebFont.ToWoff2(font)); File.WriteAllBytes(name + ".woff", WebFont.ToWoff(font)); Console.WriteLine( $"Wrote {name}.woff2 and {name}.woff");
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
If you paste the snippet into a new project, copy a .ttf next to Program.cs as NotoSans-Regular.ttf or change name to your font’s filename without .ttf.
Wrote NotoSans-Regular.woff2 and NotoSans-Regular.woffThe full program
using OneFont; string input = args.Length == 0 ? "NotoSans-Regular.ttf" : args[0]; string name = Path.GetFileNameWithoutExtension(input); byte[] sfnt = File.ReadAllBytes(input); byte[] woff2 = WebFont.ToWoff2(sfnt); byte[] woff = WebFont.ToWoff(sfnt); File.WriteAllBytes(name + ".woff2", woff2); File.WriteAllBytes(name + ".woff", woff); Console.WriteLine($"Wrote {name}.woff2 and {name}.woff");
Wrote NotoSans-Regular.woff2 and NotoSans-Regular.woffThe program takes an optional path and writes both files to the current directory. Both methods throw FontFormatException for a collection (.ttc or .otc) or an invalid table directory. Pass one standalone font, not an existing web font.
Variations
Convert an OTF file. Pass the path to any standalone .otf to the full program:
dotnet run -- MyFont.otfWrote MyFont.woff2 and MyFont.woffIf you don’t have an OTF handy, build Triangle.otf with the glyph outline example and pass that file instead.
Related recipes
The short answer
Read a standalone TTF or OTF into a byte array and pass it to WebFont.ToWoff2 or WebFont.ToWoff. Save the returned bytes with the matching .woff2 or .woff extension. Both methods pack the font as-is rather than selecting characters, drawing outlines, or changing a CFF outline into a TrueType outline.
What is the difference between WOFF and WOFF2 font formats?
Both are web containers for OpenType font data. The onefont docs explain how these two methods pack the source: ToWoff2 puts the tables in one Brotli stream and uses the null transform for the TrueType glyf and loca tables; ToWoff compresses each table separately with zlib. WOFF2 omits the DSIG table and sets the required flag in head. The outline format inside the container is still the one you started with.
Use WOFF2 if that’s what your supported browsers load. If you also need to support a browser that understands WOFF but not WOFF2, generate both from the original TTF or OTF. Don’t pass the .woff2 to ToWoff to generate the fallback. These are encoders, not readers for either web format.
Why use WOFF2 over TTF?
WOFF2 wraps the font for delivery with compression specified by WOFF File Format 2.0. You can keep the uncompressed original for future changes and publish the packed copy for the browser. Packing and subsetting solve different problems: packing compresses the stored font data, while subsetting reduces the character mappings and glyphs before you pack. If the font is meant to cover text you don’t know in advance, keep its coverage and pack it without subsetting.
How do you use a WOFF2 font in CSS?
Use @font-face. Put the example’s output files beside the stylesheet. List WOFF2 first and WOFF second so the browser can pick a source it supports. The URLs are relative to the stylesheet.
@font-face { font-family: "Site Sans"; src: url("NotoSans-Regular.woff2") format("woff2"), url("NotoSans-Regular.woff") format("woff"); } body { font-family: "Site Sans", sans-serif; }
The name Site Sans is the name your stylesheet uses, not a request to rename the font on disk. If you only publish the WOFF2 output, remove the WOFF entry. Before putting either file on a public server, check the source font’s license for web distribution. The ZIP’s Noto Sans input is distributed with its OFL license.