forked from mojocn/base64Captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts_embedded.go
44 lines (36 loc) · 940 Bytes
/
fonts_embedded.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
package base64Captcha
import (
"embed"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
type EmbeddedFontsStorage struct {
fs embed.FS
}
func (s *EmbeddedFontsStorage) LoadFontByName(name string) *truetype.Font {
fontBytes, err := s.fs.ReadFile(name)
if err != nil {
panic(err)
}
//font file bytes to trueTypeFont
trueTypeFont, err := freetype.ParseFont(fontBytes)
if err != nil {
panic(err)
}
return trueTypeFont
}
// LoadFontsByNames import fonts from dir.
// make the simple-font(RitaSmith.ttf) the first font of trueTypeFonts.
func (s *EmbeddedFontsStorage) LoadFontsByNames(assetFontNames []string) []*truetype.Font {
fonts := make([]*truetype.Font, 0)
for _, assetName := range assetFontNames {
f := s.LoadFontByName(assetName)
fonts = append(fonts, f)
}
return fonts
}
func NewEmbeddedFontsStorage(fs embed.FS) *EmbeddedFontsStorage {
return &EmbeddedFontsStorage{
fs: fs,
}
}