Recursive AI

Choose an emoji fallback font in Go

onefont-go is a free, open-source font and text-shaping library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

Use WithEmojiPresentation to prefer a color face for keycaps and other emoji sequences. Both sample fonts are in the zip.

Terminal
go get recursiveai.org/go/onefont
GO
package main
 
import (
	"fmt"
	"log"
	"os"
 
	"recursiveai.org/go/onefont"
)
 
func main() {
	textFont := load("NotoSans-Subset-Regular.ttf")
	emojiFont := load("Noto-Emoji-Keycap.ttf")
	chain := onefont.NewFallbackChain(textFont, emojiFont).
		WithEmojiPresentation()
 
	text := "1\uFE0E 1\uFE0F\u20E3"
	for run, err := range onefont.ShapeText(text, chain) {
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("bytes %d:%d, font %d, %d glyphs\n",
			run.Start, run.End, run.Font, run.Run.Len())
	}
}
 
func load(path string) *onefont.Face {
	data, err := os.ReadFile(path)
	if err != nil {
		log.Fatal(err)
	}
	face, err := onefont.Parse(data)
	if err != nil {
		log.Fatal(err)
	}
	return face
}
Text
bytes 0:5, font 0, 3 glyphs
bytes 5:12, font 1, 1 glyphs

1\uFE0E asks for text presentation. 1\uFE0F\u20E3 is an emoji keycap; the latter uses font 1, the emoji face. The space stays with font 0.

Paste the code above into main.go, put both .ttf files from the zip next to it, 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

Use first covering font instead. Drop WithEmojiPresentation from the chain declaration:

GO
chain := onefont.NewFallbackChain(textFont, emojiFont)
Text
bytes 0:12, font 0, 6 glyphs

The short answer

Load your primary text face and a color emoji face. Pass them in that order to NewFallbackChain, then call WithEmojiPresentation() on the chain and pass the result to ShapeText. The runs report a font index as well as glyphs. In the sample, both faces have a glyph for 1, but the presentation request makes the keycap use the second face.

How do I change the emoji font without changing the text font?

Keep your preferred text face first and put your emoji face after it. Without the presentation option, the first font that covers the first code point in each grapheme cluster wins. For emoji sequences recognized by WithEmojiPresentation, onefont-go instead looks for the first covering face that has a CBDT, COLR, sbix, or SVG table. If no such face covers the base, choice falls back to the normal order.

This lets you change the emoji face without putting it ahead of your text face for ordinary text. For example, moving the emoji face to the front simply to fix keycaps can also change the face used for plain digits. The option returns a new chain; pass that chain, not the original, to ShapeText.

How do I force text or emoji presentation?

The Unicode emoji standard defines U+FE0E (VS15) for text presentation and U+FE0F (VS16) for emoji presentation. onefont-go uses its ordinary order for a cluster with VS15. It prefers a color face for a cluster with VS16 or a recognized emoji sequence such as a keycap or a flag made from two regional indicators. A standalone emoji without a selector follows ordinary font order in this release. A selector is therefore a request used in font choice, not a guarantee that a specific font or glyph will appear.

Why does an emoji still show separate glyphs or no color?

Font choice and drawing are different. The fallback chain tests coverage of the cluster’s first code point, not whether the entire sequence maps to a single glyph in that font. A color table also doesn’t guarantee that the chosen glyph has color data. Use fonts that contain the sequences you expect and a renderer that supports the chosen font’s color format. ShapeText provides the glyphs and positions; it doesn’t rasterize them. If you retain glyphs after advancing its iterator, copy them: the run storage is reused.

All onefont Go recipes