-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont.go
More file actions
57 lines (46 loc) · 1.06 KB
/
Copy pathfont.go
File metadata and controls
57 lines (46 loc) · 1.06 KB
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
package main
import (
_ "embed"
"fmt"
"golang.org/x/image/font/basicfont"
"sync"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
var (
// Embed the font file directly into the binary
//go:embed NotoSans-Regular.ttf
notoSansFontBytes []byte
fontCache = make(map[float64]*truetype.Font)
fontCacheLock sync.RWMutex
)
func GetFont(fontSize float64) (*truetype.Font, error) {
fontCacheLock.RLock()
cachedFont, exists := fontCache[fontSize]
fontCacheLock.RUnlock()
if exists {
return cachedFont, nil
}
// Parse the font
ft, err := truetype.Parse(notoSansFontBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse font: %w", err)
}
fontCacheLock.Lock()
fontCache[fontSize] = ft
fontCacheLock.Unlock()
return ft, nil
}
func CreateFontFace(fontSize float64) (font.Face, error) {
ft, err := GetFont(fontSize)
if err != nil {
return nil, err
}
return truetype.NewFace(ft, &truetype.Options{
Size: fontSize,
DPI: 72,
}), nil
}
func FallbackFontFace(size float64) font.Face {
return basicfont.Face7x13
}