/
font.go
45 lines (40 loc) · 839 Bytes
/
font.go
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
package imageutil
import (
"errors"
"github.com/golang/freetype/truetype"
"github.com/llgcode/draw2d"
)
// Font font info
type Font struct {
// Cache FontCache
Cache draw2d.FontCache
// Size font size
Size float64 `json:"size,omitempty"`
// Data font setting
Data *draw2d.FontData `json:"data,omitempty"`
// Font
Font *truetype.Font `json:"-"`
}
// Load font from font cache
func (f *Font) Load(cache draw2d.FontCache) error {
if f.Font != nil {
return nil
}
if f.Data == nil {
return nil
}
if cache == nil {
return errors.New("missing font cache")
}
ft, err := cache.Load(*f.Data)
if err != nil {
return err
}
f.Cache = cache
f.Font = ft
return nil
}
// NewFontCache load font cache
func NewFontCache(fontFolder string) *draw2d.SyncFolderFontCache {
return draw2d.NewSyncFolderFontCache(fontFolder)
}