From 0dfd8c3da6297a23e13b0fba1c85cba9624fe356 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Thu, 20 Apr 2023 10:27:29 -0400 Subject: [PATCH] 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 --- font/gofont/gofont.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/font/gofont/gofont.go b/font/gofont/gofont.go index 1139399a..1e2818ad 100644 --- a/font/gofont/gofont.go +++ b/font/gofont/gofont.go @@ -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) -- 2.38.5