forked from hanguofeng/gocaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontmanager.go
61 lines (49 loc) · 1.46 KB
/
fontmanager.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2013 hanguofeng. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gocaptcha
import (
"github.com/hanguofeng/freetype-go-mirror/freetype/truetype"
"io/ioutil"
"math/rand"
"time"
)
//FontManager is a Font Manager the manage font list
type FontManager struct {
fontFiles []string
fontObjects map[string]*truetype.Font
randObject *rand.Rand
}
//CreateFontManager will create a new Font Manager
func CreateFontManager() *FontManager {
fm := new(FontManager)
fm.fontFiles = []string{}
fm.fontObjects = make(map[string]*truetype.Font)
fm.randObject = rand.New(rand.NewSource(time.Now().UnixNano()))
return fm
}
//AddFont will add a new font file to the list
func (fm *FontManager) AddFont(pathToFontFile string) error {
fontBytes, err := ioutil.ReadFile(pathToFontFile)
if err != nil {
return err
}
font, err := truetype.Parse(fontBytes)
if err != nil {
return err
}
fm.fontFiles = append(fm.fontFiles, pathToFontFile)
fm.fontObjects[pathToFontFile] = font
return nil
}
//GetFont will return a Font struct by path
func (fm *FontManager) GetFont(pathToFontFile string) *truetype.Font {
return fm.fontObjects[pathToFontFile]
}
//GetRandomFont will return a random Font struct
func (fm *FontManager) GetRandomFont() *truetype.Font {
randomIndex := fm.randObject.Intn(len(fm.fontFiles))
fontFile := fm.fontFiles[randomIndex]
rst := fm.GetFont(fontFile)
return rst
}