Use UnwrapWebFont to recover the font inside a WOFF2 file; the first four bytes of the unpacked font tell you whether it’s TTF, OTF, or TTC. The zip includes a Noto Sans web font.
go get recursiveai.org/go/onefontpackage main import ( "fmt" "log" "os" "recursiveai.org/go/onefont" ) func main() { input := "NotoSans-Subset-Regular.woff2" data, err := os.ReadFile(input) if err != nil { log.Fatal(err) } font, err := onefont.UnwrapWebFont(data, onefont.WithMaxFontSize(8<<20)) if err != nil { log.Fatal(err) } var ext string switch string(font[:4]) { case "\x00\x01\x00\x00", "true": ext = ".ttf" case "OTTO": ext = ".otf" case "ttcf": ext = ".ttc" default: log.Fatalf("unknown SFNT flavor: %x", font[:4]) } output := "unpacked" + ext err = os.WriteFile(output, font, 0644) if err != nil { log.Fatal(err) } fmt.Println("Wrote", output) }
Wrote unpacked.ttfUnwrapWebFont returns an error for damaged input or a font that exceeds the unpacked-size limit. The program writes no file in either case.
Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onefont, then go run .; or unzip the example and go run ..
Requires Go 1.24 or later.
Variations
Unpack a CFF web font. Use an input that contains CFF outlines, such as the pinned cff.woff2 example. Save the raw file beside main.go and change the input:
input := "cff.woff2"Wrote unpacked.otfUnpack a collection. Download the pinned collection.woff2 example the same way:
input := "collection.woff2"Wrote unpacked.ttcRelated recipes
The short answer
Call onefont.UnwrapWebFont on the file’s bytes, then write the returned slice as a font file. Choose .ttf, .otf, or .ttc from the signature of the unpacked font. The .woff2 suffix only tells you about the wrapper.
Can you convert WOFF2 to TTF?
Yes, if the web font wraps a font with TrueType outlines. The OpenType file specification gives 00 01 00 00 as the sfnt version for those outlines. The sample has that signature, so the program writes unpacked.ttf. An sfnt is a font container built from named tables; the program checks its first four bytes rather than looking for a family name or a filename extension.
Unwrapping is not an outline conversion. If the source is a web subset with only the characters needed by a site, the output is still that subset. A .ttf extension will not restore missing characters, weights, or tables. And the output need not be byte-for-byte identical to the font before compression: onefont-go reverses WOFF2 table transforms and recomputes the table checksums. It skips the wrapper’s optional metadata and private data.
How can I convert a WOFF2 file to OTF?
Use the same call. An unpacked OTTO signature means the OpenType font contains CFF or CFF2 outlines; give it the .otf extension. If you get ttcf instead, keep the result as a .ttc collection. It can hold multiple faces, not just one. If a downstream tool accepts only TrueType outlines, changing the extension of an OTTO result will not make it compatible.
How can I convert a WOFF2 file without using a web service?
UnwrapWebFont runs locally. It also accepts WOFF 1.0. Use IsWebFont if one code path needs to handle both already-unpacked and wrapped fonts: it checks the signature only, not whether the remaining data is valid. When you only want to inspect or shape a face, pass the unpacked bytes to Parse without writing a copy first.
A compressed file can be small even when the unpacked tables are large. The example sets an 8 MiB output limit with WithMaxFontSize; omitting it uses the library’s 64 MiB default. If a known font needs a higher limit than your application chose, raise it for that input rather than inferring an output limit from the file size.