-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfont.go
More file actions
58 lines (54 loc) · 1.68 KB
/
font.go
File metadata and controls
58 lines (54 loc) · 1.68 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
58
package gg
import (
"os"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
// LoadFontFace is a helper function to load the specified font file with
// the specified point size. Note that the returned `font.Face` objects
// are not thread safe and cannot be used in parallel across goroutines.
// You can usually just use the Context.LoadFontFace function instead of
// this package-level function.
//
// LoadFontFace 是一个辅助函数,用于加载指定点大小的指定字体文件。
// 请注意,返回的 `font.Face` 对象不是线程安全的,不能跨 goroutine 并行使用。
// 您通常可以只使用 Context.LoadFontFace 函数而不是这个包级函数。
func LoadFontFace(path string, points, dpi float64) (face font.Face, err error) {
fontBytes, err := os.ReadFile(path)
if err != nil {
return
}
f, err := opentype.ParseCollection(fontBytes)
if err != nil {
return
}
fnf, err := f.Font(0)
if err != nil {
return
}
face, err = opentype.NewFace(fnf, &opentype.FaceOptions{
Size: points,
DPI: dpi,
// Hinting: font.HintingFull,
})
return
}
// ParseFontFace 是一个辅助函数,用于加载指定点大小的指定字体文件。
// 请注意,返回的 `font.Face` 对象不是线程安全的,不能跨 goroutine 并行使用。
// 您通常可以只使用 Context.LoadFontFace 函数而不是这个包级函数。
func ParseFontFace(b []byte, points float64) (face font.Face, err error) {
f, err := opentype.ParseCollection(b)
if err != nil {
return
}
fnf, err := f.Font(0)
if err != nil {
return
}
face, err = opentype.NewFace(fnf, &opentype.FaceOptions{
Size: points,
DPI: 72,
// Hinting: font.HintingFull,
})
return
}