package theme import ( "embed" "fmt" "gioui.org/font" "gioui.org/font/gofont" "gioui.org/font/opentype" "gioui.org/text" ) // The UI face, Noto Sans, is embedded: systems commonly install it as a // variable font, from which Gio can only use one weight. // //go:embed fonts/NotoSans-*.ttf var fontFiles embed.FS // Fonts returns the embedded font faces. func Fonts() []font.FontFace { entries, err := fontFiles.ReadDir("fonts") if err != nil { panic(err) } var faces []font.FontFace for _, e := range entries { data, err := fontFiles.ReadFile("fonts/" + e.Name()) if err != nil { panic(err) } parsed, err := opentype.ParseCollection(data) if err != nil { panic(fmt.Errorf("embedded font %s: %w", e.Name(), err)) } for _, f := range parsed { // A family name of its own, so that the system's variable // Noto Sans, which offers only its default weight, can't be // chosen instead. f.Font.Typeface = uiFamily faces = append(faces, f) } } return faces } // uiFamily is what the embedded Noto Sans faces are called. const uiFamily = "100gex Noto Sans" // NewShaper returns a shaper with the embedded fonts and Gio's Go fonts. The // system's fonts, such as Fraunces and Iosevka, load by name, and are // fallbacks for characters these lack, such as CJK and emoji. func NewShaper() *text.Shaper { return text.NewShaper(text.WithCollection(append(gofont.Collection(), Fonts()...))) }