~eliasnaur/gio

0dfd8c3da6297a23e13b0fba1c85cba9624fe356 — Chris Waldon 1 year, 5 months ago ccf24c0
font/gofont: allow loading just the regular Go font

This commit introduces a special mechanism to load only the regular version
of the Go font. This is useful for Gio to load a font for drawing window
decorations without forcing applications to load every Go font.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 21 insertions(+), 1 deletions(-)

M font/gofont/gofont.go
M font/gofont/gofont.go => font/gofont/gofont.go +21 -1
@@ 29,13 29,33 @@ import (
)

var (
	regOnce    sync.Once
	reg        []font.FontFace
	once       sync.Once
	collection []font.FontFace
)

func loadRegular() {
	regOnce.Do(func() {
		face, err := opentype.Parse(goregular.TTF)
		if err != nil {
			panic(fmt.Errorf("failed to parse font: %v", err))
		}
		reg = []font.FontFace{{Font: font.Font{Typeface: "Go"}, Face: face}}
		collection = append(collection, reg[0])
	})
}

// Regular returns a collection of only the Go regular font face.
func Regular() []font.FontFace {
	loadRegular()
	return reg
}

// Regular returns a collection of all available Go font faces.
func Collection() []font.FontFace {
	loadRegular()
	once.Do(func() {
		register(font.Font{}, goregular.TTF)
		register(font.Font{Style: font.Italic}, goitalic.TTF)
		register(font.Font{Weight: font.Bold}, gobold.TTF)
		register(font.Font{Style: font.Italic, Weight: font.Bold}, gobolditalic.TTF)