Documentation
¶
Overview ¶
Package fonts is a registry of legible, permissively-licensed TrueType fonts, laid out one subpackage per family so your binary links only the families you actually import — the same lazy-at-compile-time model as golang.org/x/image/font/gofont.
The root package never bulk-embeds every family: [//go:embed] is eager per package, so a package that embedded all bundled fonts would link every one of them into any binary that imports it, whether or not it uses them. Instead each family lives in its own subpackage with its own //go:embed, and the root package holds only lightweight Family metadata plus a single embedded default.
Call All to enumerate every bundled family's metadata (name, Kind, license, and import path — no bytes), ByName to look one up by name, or MostLegible for the one family embedded directly in this package: the font the Braille Institute designed specifically for maximum legibility, including for low-vision readers. To use any other family, import its subpackage and read its exported TTF variable:
import ( "github.com/go-opentype/fonts" "github.com/go-opentype/fonts/inter" ) f, err := fonts.Parse(inter.TTF) face := f.NewFace(16)
Parse is a thin convenience wrapper around opentype.Parse.
Dozens of families are bundled; see the "Bundled fonts" table in README.md for the full list, and cmd/genfonts for the generator that ingests OFL-licensed families from github.com/google/fonts to produce new subpackages.
The Go code in this repository is BSD-3-Clause (see LICENSE). Each bundled font keeps its own upstream license — see the "Bundled fonts" table in README.md, each subpackage's doc comment, and the full texts under licenses/.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MostLegible ¶
func MostLegible() []byte
MostLegible returns Atkinson Hyperlegible, the bundled font with the strongest legibility credentials: it was designed by the Braille Institute specifically to maximize character distinction for readers with low vision, and it benefits every reader in the process. Prefer it whenever legibility, not brand voice, is the deciding factor.
MostLegible is the one family the root fonts package embeds directly — every other family requires importing its own subpackage. This keeps a sensible default usable with zero extra imports, without defeating the per-family lazy-linking model for everyone else.
Example ¶
ExampleMostLegible parses the one family embedded directly in the root package — Atkinson Hyperlegible — with zero extra imports.
package main
import (
"fmt"
"github.com/go-opentype/fonts"
)
func main() {
f, err := fonts.Parse(fonts.MostLegible())
if err != nil {
panic(err)
}
fmt.Println(f.NumGlyphs())
}
Output: 369
func Parse ¶
Parse decodes ttf (typically a subpackage's TTF variable, or the return value of MostLegible) into a *opentype.Font, ready for NewFace and rendering. It is a thin convenience wrapper around opentype.Parse:
f, err := fonts.Parse(fonts.MostLegible())
Example ¶
ExampleParse shows the per-family lazy-import model: importing a family's own subpackage (here fonts/inter) links only that family's bytes into the binary, and fonts.Parse is a thin wrapper over opentype.Parse for them.
package main
import (
"fmt"
"github.com/go-opentype/fonts"
"github.com/go-opentype/fonts/inter"
)
func main() {
f, err := fonts.Parse(inter.TTF)
if err != nil {
panic(err)
}
face := f.NewFace(16)
fmt.Println("glyphs:", f.NumGlyphs())
fmt.Println("Measure(Hi):", face.Measure("Hi"))
}
Output: glyphs: 2933 Measure(Hi): 16
Types ¶
type Family ¶
type Family struct {
Name string // display name, e.g. "Atkinson Hyperlegible"
Kind Kind
License string // SPDX identifier: "OFL-1.1" or "BSD-3-Clause"
ImportPath string // e.g. "github.com/go-opentype/fonts/inter"
}
Family describes one bundled font by metadata only: its display name, general style, the SPDX identifier of the license it ships under, and the import path of the subpackage that embeds its bytes.
Family deliberately has no []byte field. Enumerating All therefore does not link any font into your binary — only importing a specific subpackage (e.g. "github.com/go-opentype/fonts/inter") does that. This is the "lazy at compile time" model: your binary links exactly the families you import, nothing more.
func All ¶
func All() []Family
All returns every bundled Family, curated families first (in their original order), followed by cmd/genfonts output, in a stable order.
All returns metadata only — no font bytes. To use a family, import its ImportPath and read its exported TTF variable, e.g.:
import "github.com/go-opentype/fonts/inter" f, err := fonts.Parse(inter.TTF)
Example ¶
ExampleAll enumerates every bundled family's metadata without linking any font bytes: Family has no []byte field, so All is safe to call even if you only ever import one family's subpackage.
package main
import (
"fmt"
"github.com/go-opentype/fonts"
)
func main() {
all := fonts.All()
fmt.Println("bundled families:", len(all))
fmt.Println(all[0].Name, all[0].Kind, all[0].License)
}
Output: bundled families: 44 Atkinson Hyperlegible sans OFL-1.1
func ByName ¶
ByName looks up a bundled family by its Family.Name, case-insensitively, and returns its metadata. ok is false when no family matches. As with All, this returns metadata only; fetch bytes via the returned Family.ImportPath subpackage.
Example ¶
ExampleByName looks up a bundled family by name, case-insensitively, to discover its ImportPath before importing the subpackage.
package main
import (
"fmt"
"github.com/go-opentype/fonts"
)
func main() {
fam, ok := fonts.ByName("inter")
fmt.Println(ok, fam.Name, fam.ImportPath)
_, ok = fonts.ByName("Nonexistent Family")
fmt.Println(ok)
}
Output: true Inter github.com/go-opentype/fonts/inter false
Directories
¶
| Path | Synopsis |
|---|---|
|
Package arimo embeds Arimo, a variable font upstream (bundled at its default master).
|
Package arimo embeds Arimo, a variable font upstream (bundled at its default master). |
|
Package atkinsonhyperlegible embeds Atkinson Hyperlegible Regular, designed by the Braille Institute specifically to maximize character distinction for readers with low vision, benefiting every reader in the process.
|
Package atkinsonhyperlegible embeds Atkinson Hyperlegible Regular, designed by the Braille Institute specifically to maximize character distinction for readers with low vision, benefiting every reader in the process. |
|
Package bitter embeds Bitter, a variable font upstream (bundled at its default master).
|
Package bitter embeds Bitter, a variable font upstream (bundled at its default master). |
|
Package cabin embeds Cabin, a variable font upstream (bundled at its default master).
|
Package cabin embeds Cabin, a variable font upstream (bundled at its default master). |
|
cmd
|
|
|
genfonts
command
Command genfonts ingests OFL-licensed font families from github.com/google/fonts and generates a github.com/go-opentype/fonts subpackage for each one that survives validation: it fetches the family's .ttf and OFL.txt over plain net/http, confirms the .ttf decodes through github.com/go-opentype/opentype (skipping and logging any family that fails to parse, is missing its license file, or exceeds the size cap), and writes:
|
Command genfonts ingests OFL-licensed font families from github.com/google/fonts and generates a github.com/go-opentype/fonts subpackage for each one that survives validation: it fetches the family's .ttf and OFL.txt over plain net/http, confirms the .ttf decodes through github.com/go-opentype/opentype (skipping and logging any family that fails to parse, is missing its license file, or exceeds the size cap), and writes: |
|
Package cousine embeds Cousine.
|
Package cousine embeds Cousine. |
|
Package dmsans embeds DM Sans, a variable font upstream (bundled at its default master).
|
Package dmsans embeds DM Sans, a variable font upstream (bundled at its default master). |
|
Package firacode embeds Fira Code, a variable font upstream (bundled at its default master).
|
Package firacode embeds Fira Code, a variable font upstream (bundled at its default master). |
|
Package firasans embeds Fira Sans.
|
Package firasans embeds Fira Sans. |
|
Package gomono embeds Go Mono, the Go project's monospace companion to Go Regular.
|
Package gomono embeds Go Mono, the Go project's monospace companion to Go Regular. |
|
Package goregular embeds Go Regular, the Go project's screen sans-serif designed by Bigelow & Holmes.
|
Package goregular embeds Go Regular, the Go project's screen sans-serif designed by Bigelow & Holmes. |
|
Package ibmplexmono embeds IBM Plex Mono.
|
Package ibmplexmono embeds IBM Plex Mono. |
|
Package ibmplexsans embeds IBM Plex Sans, a variable font upstream (bundled at its default master).
|
Package ibmplexsans embeds IBM Plex Sans, a variable font upstream (bundled at its default master). |
|
Package inconsolata embeds Inconsolata.
|
Package inconsolata embeds Inconsolata. |
|
Package inter embeds Inter Regular, a UI sans-serif designed for computer screens.
|
Package inter embeds Inter Regular, a UI sans-serif designed for computer screens. |
|
Package jetbrainsmono embeds JetBrains Mono Regular, a monospace face designed for code.
|
Package jetbrainsmono embeds JetBrains Mono Regular, a monospace face designed for code. |
|
Package karla embeds Karla, a variable font upstream (bundled at its default master).
|
Package karla embeds Karla, a variable font upstream (bundled at its default master). |
|
Package lato embeds Lato.
|
Package lato embeds Lato. |
|
Package lora embeds Lora Regular, a contemporary serif with roots in calligraphy.
|
Package lora embeds Lora Regular, a contemporary serif with roots in calligraphy. |
|
Package manrope embeds Manrope, a variable font upstream (bundled at its default master).
|
Package manrope embeds Manrope, a variable font upstream (bundled at its default master). |
|
Package montserrat embeds Montserrat, a variable font upstream (bundled at its default master).
|
Package montserrat embeds Montserrat, a variable font upstream (bundled at its default master). |
|
Package mulish embeds Mulish, a variable font upstream (bundled at its default master).
|
Package mulish embeds Mulish, a variable font upstream (bundled at its default master). |
|
Package notosans embeds Noto Sans, a variable font upstream (bundled at its default master).
|
Package notosans embeds Noto Sans, a variable font upstream (bundled at its default master). |
|
Package notosansarabic embeds Noto Sans Arabic, a variable font upstream (bundled at its default master).
|
Package notosansarabic embeds Noto Sans Arabic, a variable font upstream (bundled at its default master). |
|
Package notosansarmenian embeds Noto Sans Armenian, a variable font upstream (bundled at its default master).
|
Package notosansarmenian embeds Noto Sans Armenian, a variable font upstream (bundled at its default master). |
|
Package notosansdevanagari embeds Noto Sans Devanagari, a variable font upstream (bundled at its default master).
|
Package notosansdevanagari embeds Noto Sans Devanagari, a variable font upstream (bundled at its default master). |
|
Package notosansegyptianhieroglyphs embeds Noto Sans Egyptian Hieroglyphs.
|
Package notosansegyptianhieroglyphs embeds Noto Sans Egyptian Hieroglyphs. |
|
Package notosansgeorgian embeds Noto Sans Georgian, a variable font upstream (bundled at its default master).
|
Package notosansgeorgian embeds Noto Sans Georgian, a variable font upstream (bundled at its default master). |
|
Package notosanshebrew embeds Noto Sans Hebrew, a variable font upstream (bundled at its default master).
|
Package notosanshebrew embeds Noto Sans Hebrew, a variable font upstream (bundled at its default master). |
|
Package notosanssc embeds Noto Sans SC, a variable font upstream (bundled at its default master).
|
Package notosanssc embeds Noto Sans SC, a variable font upstream (bundled at its default master). |
|
Package notosansthai embeds Noto Sans Thai, a variable font upstream (bundled at its default master).
|
Package notosansthai embeds Noto Sans Thai, a variable font upstream (bundled at its default master). |
|
Package nunito embeds Nunito, a variable font upstream (bundled at its default master).
|
Package nunito embeds Nunito, a variable font upstream (bundled at its default master). |
|
Package nunitosans embeds Nunito Sans, a variable font upstream (bundled at its default master).
|
Package nunitosans embeds Nunito Sans, a variable font upstream (bundled at its default master). |
|
Package opensans embeds Open Sans, a variable font upstream (bundled at its default master).
|
Package opensans embeds Open Sans, a variable font upstream (bundled at its default master). |
|
Package playfairdisplay embeds Playfair Display, a variable font upstream (bundled at its default master).
|
Package playfairdisplay embeds Playfair Display, a variable font upstream (bundled at its default master). |
|
Package poppins embeds Poppins.
|
Package poppins embeds Poppins. |
|
Package ptsans embeds PT Sans.
|
Package ptsans embeds PT Sans. |
|
Package roboto embeds Roboto, a variable font upstream (bundled at its default master).
|
Package roboto embeds Roboto, a variable font upstream (bundled at its default master). |
|
Package robotomono embeds Roboto Mono, a variable font upstream (bundled at its default master).
|
Package robotomono embeds Roboto Mono, a variable font upstream (bundled at its default master). |
|
Package rubik embeds Rubik, a variable font upstream (bundled at its default master).
|
Package rubik embeds Rubik, a variable font upstream (bundled at its default master). |
|
Package sourcecodepro embeds Source Code Pro, a variable font upstream (bundled at its default master).
|
Package sourcecodepro embeds Source Code Pro, a variable font upstream (bundled at its default master). |
|
Package sourcesans3 embeds Source Sans 3, a variable font upstream (bundled at its default master).
|
Package sourcesans3 embeds Source Sans 3, a variable font upstream (bundled at its default master). |
|
Package spacemono embeds Space Mono.
|
Package spacemono embeds Space Mono. |
|
Package titilliumweb embeds Titillium Web.
|
Package titilliumweb embeds Titillium Web. |
|
Package worksans embeds Work Sans, a variable font upstream (bundled at its default master).
|
Package worksans embeds Work Sans, a variable font upstream (bundled at its default master). |