fonts

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

README

go-opentype/fonts

CI Go Reference Go Report Card License

36 legible, permissively-licensed TrueType fonts, one Go subpackage per family, each with its own //go:embed — no downloading, no sourcing a .ttf yourself, and your binary links only the families you import. Built for go-opentype/opentype, the pure-Go, stdlib-only TrueType engine, but the raw bytes work with any parser that accepts a .ttf.

import (
	"github.com/go-opentype/fonts"
	"github.com/go-opentype/fonts/inter"
)

f, err := fonts.Parse(inter.TTF) // *opentype.Font
face := f.NewFace(16)            // 16px face

Install

go get github.com/go-opentype/fonts

Pure Go, no cgo, no external assets to fetch at build or run time — every family is //go:embedded into its own subpackage.

The lazy-at-compile-time import model

//go:embed is eager per package: any package that embeds a font links that font's bytes into every binary that imports it, whether or not the binary ever uses it. A fonts package that bulk-embedded all 36 families would put all 36 into your binary the moment you imported it for anything at all.

So the root fonts package doesn't do that. Each family lives in its own subpackage — fonts/inter, fonts/roboto, fonts/jetbrainsmono, and so on — with its own //go:embed. Importing fonts/inter links only Inter. Importing ten subpackages links only those ten. This is the same pattern golang.org/x/image/font/gofont uses for Go's own bundled fonts.

The root fonts package holds two things only:

  1. Family metadata — name, Kind, license, and import path for every bundled font, returned by All and ByName. No []byte field: enumerating families never links any of them in.

  2. MostLegible — the one family embedded directly in the root package, so there's a sensible zero-extra-imports default. Every other family requires importing its own subpackage:

    import (
    	"github.com/go-opentype/fonts"
    	"github.com/go-opentype/fonts/robotomono"
    )
    
    f, err := fonts.Parse(robotomono.TTF)
    

Bundled fonts

Six families are hand-curated (present since v0.1.0); the other thirty were ingested by cmd/genfonts from google/fonts's ofl/ directory.

Name Kind License Import path
Atkinson Hyperlegible Sans OFL-1.1 github.com/go-opentype/fonts/atkinsonhyperlegible
Inter Sans OFL-1.1 github.com/go-opentype/fonts/inter
Go Sans BSD-3-Clause github.com/go-opentype/fonts/goregular
Lora Serif OFL-1.1 github.com/go-opentype/fonts/lora
Go Mono Mono BSD-3-Clause github.com/go-opentype/fonts/gomono
JetBrains Mono Mono OFL-1.1 github.com/go-opentype/fonts/jetbrainsmono
Arimo Sans OFL-1.1 github.com/go-opentype/fonts/arimo
Bitter Serif OFL-1.1 github.com/go-opentype/fonts/bitter
Cabin Sans OFL-1.1 github.com/go-opentype/fonts/cabin
Cousine Mono OFL-1.1 github.com/go-opentype/fonts/cousine
DM Sans Sans OFL-1.1 github.com/go-opentype/fonts/dmsans
Fira Code Mono OFL-1.1 github.com/go-opentype/fonts/firacode
Fira Sans Sans OFL-1.1 github.com/go-opentype/fonts/firasans
IBM Plex Mono Mono OFL-1.1 github.com/go-opentype/fonts/ibmplexmono
IBM Plex Sans Sans OFL-1.1 github.com/go-opentype/fonts/ibmplexsans
Inconsolata Mono OFL-1.1 github.com/go-opentype/fonts/inconsolata
Karla Sans OFL-1.1 github.com/go-opentype/fonts/karla
Lato Sans OFL-1.1 github.com/go-opentype/fonts/lato
Manrope Sans OFL-1.1 github.com/go-opentype/fonts/manrope
Montserrat Sans OFL-1.1 github.com/go-opentype/fonts/montserrat
Mulish Sans OFL-1.1 github.com/go-opentype/fonts/mulish
Noto Sans Sans OFL-1.1 github.com/go-opentype/fonts/notosans
Nunito Sans OFL-1.1 github.com/go-opentype/fonts/nunito
Nunito Sans Sans OFL-1.1 github.com/go-opentype/fonts/nunitosans
Open Sans Sans OFL-1.1 github.com/go-opentype/fonts/opensans
PT Sans Sans OFL-1.1 github.com/go-opentype/fonts/ptsans
Playfair Display Serif OFL-1.1 github.com/go-opentype/fonts/playfairdisplay
Poppins Sans OFL-1.1 github.com/go-opentype/fonts/poppins
Roboto Sans OFL-1.1 github.com/go-opentype/fonts/roboto
Roboto Mono Mono OFL-1.1 github.com/go-opentype/fonts/robotomono
Rubik Sans OFL-1.1 github.com/go-opentype/fonts/rubik
Source Code Pro Mono OFL-1.1 github.com/go-opentype/fonts/sourcecodepro
Source Sans 3 Sans OFL-1.1 github.com/go-opentype/fonts/sourcesans3
Space Mono Mono OFL-1.1 github.com/go-opentype/fonts/spacemono
Titillium Web Sans OFL-1.1 github.com/go-opentype/fonts/titilliumweb
Work Sans Sans OFL-1.1 github.com/go-opentype/fonts/worksans

Full license texts are bundled verbatim under licenses/, one file per family (SIL Open Font License 1.1 for every OFL-1.1 row, plus GoFonts-LICENSE.txt — BSD-3-Clause, Bigelow & Holmes — shared by Go and Go Mono).

Atkinson Hyperlegible is the standout pick when legibility itself is the goal: it was designed by the Braille Institute specifically to maximize character distinction for readers with low vision, benefiting every reader in the process. MostLegible() returns it, and it's the only family embedded directly in the root package.

Many families are variable fonts upstream (Inter, Lora, JetBrains Mono, and most of the cmd/genfonts-ingested set); each bundled .ttf is pinned at that family's default master (static instance). go-opentype has no variable-font support, so it always renders that default instance — OpenType Variations axes are not applied. Each affected subpackage's doc comment says so explicitly.

API

type Kind int

const (
	KindSans Kind = iota
	KindSerif
	KindMono
	KindDisplay
)

// Family is metadata only — no font bytes. Fetch bytes via ImportPath.
type Family struct {
	Name       string // e.g. "Inter"
	Kind       Kind
	License    string // SPDX id: "OFL-1.1" or "BSD-3-Clause"
	ImportPath string // e.g. "github.com/go-opentype/fonts/inter"
}

func All() []Family                        // every bundled family's metadata, stable order
func ByName(name string) (Family, bool)     // case-insensitive lookup by Family.Name
func MostLegible() []byte                   // Atkinson Hyperlegible bytes — the one family embedded here
func Parse(ttf []byte) (*opentype.Font, error) // convenience wrapper over opentype.Parse

Every other family's bytes live in its own subpackage as var TTF []byte, e.g. inter.TTF, roboto.TTF, jetbrainsmono.TTF — see the table above for the full list of import paths.

See the full package documentation on pkg.go.dev.

Generator: cmd/genfonts

cmd/genfonts ingests OFL-licensed families from google/fonts and generates a new subpackage for each one that survives validation. It:

  1. Reads the curated seed list in cmd/genfonts/seeds.go — family name, google/fonts ofl/<slug> directory, and the chosen .ttf file (a static Regular instance where one exists, otherwise the family's single default-master variable font).
  2. Fetches the .ttf and OFL.txt over plain net/http.
  3. Validates the .ttf through opentype.Parse and enforces a size cap (2.5 MB — excludes pathological multi-axis variable fonts and giant CJK-scale Noto variants).
  4. Skips and logs (not an error) any family that fails to fetch, fails to parse, has no OFL.txt to bundle, or exceeds the size cap.
  5. Writes <slug>/<slug>.ttf, <slug>/<slug>.go (embed + doc comment), <slug>/<slug>_test.go (an opentype.Parse smoke test), a license file under licenses/, and regenerates the top-level generated.go registry from every family that succeeded.

Run it from the module root:

GOWORK=off go run ./cmd/genfonts

The last run ingested 30 of 32 seeded families; Merriweather (its only shipped .ttf is a ~4.6 MB multi-axis variable font, over the size cap) and Tinos (its google/fonts directory currently ships no OFL.txt) were skipped. Ubuntu is deliberately not seeded at all: it ships under the Ubuntu Font License (UFL), not OFL, outside this generator's scope.

License

The Go source code in this repository (everything outside the bundled .ttf files and licenses/) is BSD-3-Clause — see LICENSE.

The bundled font files keep their own upstream licenses; see the Bundled fonts table above, each subpackage's doc comment, and licenses/ for the full texts, copyright notices, and (for the OFL fonts) their Reserved Font Names.

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

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.

func Parse

func Parse(ttf []byte) (*opentype.Font, error)

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())

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)

func ByName

func ByName(name string) (family Family, ok bool)

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.

type Kind

type Kind int

Kind classifies a Family by its general letterform style.

const (
	// KindSans is an upright, low-contrast sans-serif.
	KindSans Kind = iota
	// KindSerif is a text serif.
	KindSerif
	// KindMono is a fixed-width (monospace) face.
	KindMono
	// KindDisplay is a face intended for headlines and short text at large
	// sizes, rather than for body copy.
	KindDisplay
)

func (Kind) String

func (k Kind) String() string

String returns the lower-case name of k ("sans", "serif", "mono", "display"), or "unknown" for any other value.

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 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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL