258 lines
7.6 KiB
Go
258 lines
7.6 KiB
Go
package base64Captcha
|
|
|
|
import (
|
|
"github.com/golang/freetype"
|
|
"github.com/golang/freetype/truetype"
|
|
"math/rand"
|
|
)
|
|
|
|
var fontsSimple = loadFontsByNames([]string{
|
|
"fonts/3Dumb.ttf",
|
|
"fonts/ApothecaryFont.ttf",
|
|
"fonts/Comismsh.ttf",
|
|
"fonts/DENNEthree-dee.ttf",
|
|
"fonts/DeborahFancyDress.ttf",
|
|
"fonts/Flim-Flam.ttf",
|
|
"fonts/RitaSmith.ttf",
|
|
"fonts/actionj.ttf",
|
|
"fonts/chromohv.ttf",
|
|
})
|
|
|
|
//var fontemoji = loadFontByName("fonts/seguiemj.ttf")
|
|
var fontsAll = append(fontsSimple, fontChinese)
|
|
var fontChinese = loadFontByName("fonts/wqy-microhei.ttc")
|
|
|
|
func loadFontByName(name string) *truetype.Font {
|
|
fontBytes, err := Asset(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 loadFontsByNames(assetFontNames []string) []*truetype.Font {
|
|
fonts := make([]*truetype.Font, 0)
|
|
for _, assetName := range assetFontNames {
|
|
f := loadFontByName(assetName)
|
|
fonts = append(fonts, f)
|
|
}
|
|
return fonts
|
|
}
|
|
|
|
//randFontFrom choose random font family.选择随机的字体
|
|
func randFontFrom(fonts []*truetype.Font) *truetype.Font {
|
|
fontCount := len(fonts)
|
|
index := rand.Intn(fontCount)
|
|
return fonts[index]
|
|
}
|
|
|
|
var digitFontData = [][]byte{
|
|
{ // 0
|
|
0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
|
|
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
|
|
0,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