~eliasnaur/gio

gio/font/gofont/gofont.go -rw-r--r-- 2.2 KiB
ef7b3e75Jack Mordaunt widget: delete whole words with key modifier 5 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: Unlicense OR MIT

// Package gofont exports the Go fonts as a text.Collection.
//
// See https://blog.golang.org/go-fonts for a description of the
// fonts, and the golang.org/x/image/font/gofont packages for the
// font data.
package gofont

import (
	"fmt"
	"sync"

	"gioui.org/font/opentype"
	"gioui.org/text"
	"golang.org/x/image/font/gofont/gobold"
	"golang.org/x/image/font/gofont/gobolditalic"
	"golang.org/x/image/font/gofont/goitalic"
	"golang.org/x/image/font/gofont/gomedium"
	"golang.org/x/image/font/gofont/gomediumitalic"
	"golang.org/x/image/font/gofont/gomono"
	"golang.org/x/image/font/gofont/gomonobold"
	"golang.org/x/image/font/gofont/gomonobolditalic"
	"golang.org/x/image/font/gofont/gomonoitalic"
	"golang.org/x/image/font/gofont/goregular"
	"golang.org/x/image/font/gofont/gosmallcaps"
	"golang.org/x/image/font/gofont/gosmallcapsitalic"
)

var (
	once       sync.Once
	collection []text.FontFace
)

func Collection() []text.FontFace {
	once.Do(func() {
		register(text.Font{}, goregular.TTF)
		register(text.Font{Style: text.Italic}, goitalic.TTF)
		register(text.Font{Weight: text.Bold}, gobold.TTF)
		register(text.Font{Style: text.Italic, Weight: text.Bold}, gobolditalic.TTF)
		register(text.Font{Weight: text.Medium}, gomedium.TTF)
		register(text.Font{Weight: text.Medium, Style: text.Italic}, gomediumitalic.TTF)
		register(text.Font{Variant: "Mono"}, gomono.TTF)
		register(text.Font{Variant: "Mono", Weight: text.Bold}, gomonobold.TTF)
		register(text.Font{Variant: "Mono", Weight: text.Bold, Style: text.Italic}, gomonobolditalic.TTF)
		register(text.Font{Variant: "Mono", Style: text.Italic}, gomonoitalic.TTF)
		register(text.Font{Variant: "Mono", Style: text.Italic}, gomonoitalic.TTF)
		register(text.Font{Variant: "Smallcaps"}, gosmallcaps.TTF)
		register(text.Font{Variant: "Smallcaps", Style: text.Italic}, gosmallcapsitalic.TTF)
		// Ensure that any outside appends will not reuse the backing store.
		n := len(collection)
		collection = collection[:n:n]
	})
	return collection
}

func register(fnt text.Font, ttf []byte) {
	face, err := opentype.Parse(ttf)
	if err != nil {
		panic(fmt.Errorf("failed to parse font: %v", err))
	}
	fnt.Typeface = "Go"
	collection = append(collection, text.FontFace{Font: fnt, Face: face})
}