Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Captcha) Generate() (id, b64s, answer string, err error) {
// You may want to call `store.Verify` method instead.
func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {
vv := c.Store.Get(id, clear)
vv = strings.TrimSpace(vv)
answer = strings.TrimSpace(answer)
return strings.EqualFold(vv, answer)
vv = strings.TrimSpace(vv)
answer = strings.TrimSpace(answer)
return strings.EqualFold(vv, answer)
}
4 changes: 2 additions & 2 deletions driver_chinese.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int
}

if len(tfs) == 0 {
tfs = fontsAll
tfs = getFontsAll()
}

return &DriverChinese{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, Length: length, Source: source, BgColor: bgColor, fontsStorage: fontsStorage, fontsArray: tfs}
Expand All @@ -69,7 +69,7 @@ func (d *DriverChinese) ConvertFonts() *DriverChinese {
tfs = append(tfs, tf)
}
if len(tfs) == 0 {
tfs = fontsAll
tfs = getFontsAll()
}
d.fontsArray = tfs

Expand Down
2 changes: 1 addition & 1 deletion driver_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestDriverLanguage_DrawCaptcha(t *testing.T) {
ds := NewDriverLanguage(80, 240, 5, OptionShowSineLine|OptionShowSlimeLine|OptionShowHollowLine, 5, nil, nil, []*truetype.Font{fontChinese}, "emotion")
ds := NewDriverLanguage(80, 240, 5, OptionShowSineLine|OptionShowSlimeLine|OptionShowHollowLine, 5, nil, nil, []*truetype.Font{getFontChinese()}, "emotion")

for i := 0; i < 40; i++ {
_, q, _ := ds.GenerateIdQuestionAnswer()
Expand Down
6 changes: 3 additions & 3 deletions driver_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, b
}

if len(tfs) == 0 {
tfs = fontsAll
tfs = getFontsAll()
}

return &DriverMath{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, fontsArray: tfs, BgColor: bgColor, Fonts: fonts}
Expand All @@ -65,7 +65,7 @@ func (d *DriverMath) ConvertFonts() *DriverMath {
tfs = append(tfs, tf)
}
if len(tfs) == 0 {
tfs = fontsAll
tfs = getFontsAll()
}
d.fontsArray = tfs

Expand Down Expand Up @@ -118,7 +118,7 @@ func (d *DriverMath) DrawCaptcha(question string) (item Item, err error) {
//背景有文字干扰
if d.NoiseCount > 0 {
noise := RandText(d.NoiseCount, strings.Repeat(TxtNumbers, d.NoiseCount))
err = itemChar.drawNoise(noise, fontsAll)
err = itemChar.drawNoise(noise, d.fontsArray)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions driver_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewDriverString(height int, width int, noiseCount int, showLineOptions int,
}

if len(tfs) == 0 {
tfs = fontsAll
tfs = getFontsSimple()
}

return &DriverString{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, Length: length, Source: source, BgColor: bgColor, fontsStorage: fontsStorage, fontsArray: tfs, Fonts: fonts}
Expand All @@ -69,7 +69,7 @@ func (d *DriverString) ConvertFonts() *DriverString {
tfs = append(tfs, tf)
}
if len(tfs) == 0 {
tfs = fontsAll
tfs = getFontsSimple()
}

d.fontsArray = tfs
Expand Down
2 changes: 1 addition & 1 deletion driver_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDriverString_DrawCaptcha(t *testing.T) {
wantItem Item
wantErr bool
}{
{"string", fields{80, 240, 20, 100, 2, 5, nil, fontsAll}, args{"45Ad8"}, nil, false},
{"string", fields{80, 240, 20, 100, 2, 5, nil, getFontsAll()}, args{"45Ad8"}, nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
80 changes: 64 additions & 16 deletions fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,82 @@ package base64Captcha

import (
"math/rand"
"sync"

"github.com/golang/freetype/truetype"
)

var fontsSimple = DefaultEmbeddedFonts.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",
})
// Available fonts
const (
Font3Dumb = "3Dumb.ttf"
FontApothecaryFont = "ApothecaryFont.ttf"
FontComismsh = "Comismsh.ttf"
FontDENNEthreeDee = "DENNEthree-dee.ttf"
FontDeborahFancyDress = "DeborahFancyDress.ttf"
FontFlimFlam = "Flim-Flam.ttf"
FontRitaSmith = "RitaSmith.ttf"
FontActionj = "actionj.ttf"
FontChromohv = "chromohv.ttf"
FontWqyMicrohei = "wqy-microhei.ttc"
)

const FontPrefix = "fonts/"

// var fontemoji = loadFontByName("fonts/seguiemj.ttf")
var fontsAll = append(fontsSimple, fontChinese)
var fontChinese = DefaultEmbeddedFonts.LoadFontByName("fonts/wqy-microhei.ttc")
// Private variables for lazy loading
var (
fontsSimple []*truetype.Font
fontChinese *truetype.Font
fontsAll []*truetype.Font
fontsSimpleOnce sync.Once
fontChineseOnce sync.Once
fontsAllOnce sync.Once
)

// getFontsSimple returns the simple font collection (without Chinese fonts).
// Fonts are loaded lazily on first call.
func getFontsSimple() []*truetype.Font {
fontsSimpleOnce.Do(func() {
fontsSimple = DefaultEmbeddedFonts.LoadFontsByNames([]string{
FontPrefix + Font3Dumb,
FontPrefix + FontApothecaryFont,
FontPrefix + FontComismsh,
FontPrefix + FontDENNEthreeDee,
FontPrefix + FontDeborahFancyDress,
FontPrefix + FontFlimFlam,
FontPrefix + FontRitaSmith,
FontPrefix + FontActionj,
FontPrefix + FontChromohv,
})
})
return fontsSimple
}

// getFontChinese returns the Chinese font.
// Font is loaded lazily on first call.
func getFontChinese() *truetype.Font {
fontChineseOnce.Do(func() {
fontChinese = DefaultEmbeddedFonts.LoadFontByName(FontPrefix + FontWqyMicrohei)
})
return fontChinese
}

// getFontsAll returns all fonts (simple fonts + Chinese font).
// Fonts are loaded lazily on first call.
func getFontsAll() []*truetype.Font {
fontsAllOnce.Do(func() {
fontsAll = append(getFontsSimple(), getFontChinese())
})
return fontsAll
}

// randFontFrom choose random font family.选择随机的字体
func randFontFrom(fonts []*truetype.Font) *truetype.Font {
fontCount := len(fonts)

if fontCount == 0 {
//loading default fonts
fonts = fontsAll
fontCount = len(fontsAll)
fonts = getFontsAll()
fontCount = len(fonts)
}
index := rand.Intn(fontCount)
return fonts[index]
Expand Down
2 changes: 1 addition & 1 deletion fonts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Test_loadFontsByNames(t *testing.T) {
}

func Test_randFontFrom(t *testing.T) {
f := randFontFrom(fontsAll)
f := randFontFrom(getFontsAll())
if f == nil {
t.Error("failed")
}
Expand Down
2 changes: 1 addition & 1 deletion store_sync_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ func (s StoreSyncMap) Get(id string, clear bool) string {
// Verify check a string value
func (s StoreSyncMap) Verify(id, answer string, clear bool) bool {
vv := s.Get(id, clear)
return strings.EqualFold(vv, answer)
return strings.EqualFold(vv, answer)
}