Skip to content

Commit ff2a018

Browse files
committed
refactor(font): 重构字体相关代码并重命名文件夹
- 将 fonts 文件夹重命名为 font - 更新相关文件的 package 名称 - 修改字体路径常量名 - 删除 SetStore 方法,
1 parent e3d1761 commit ff2a018

21 files changed

+28
-35
lines changed

captcha.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ func NewCaptcha(d Driver, s ...store.Store) *Captcha {
2828
return &Captcha{Driver: d, Store: s[0]}
2929
}
3030

31-
// SetStore sets captcha store
32-
func (c *Captcha) SetStore(s store.Store) *Captcha {
33-
c.Store = s
34-
return c
35-
}
36-
3731
// Generate generates a random id, base64 image string or an error if any
3832
func (c *Captcha) Generate() (id, src, answer string, err error) {
3933
id, content, answer := c.Driver.GenerateIdQuestionAnswer()

captcha_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestCaptcha_GenerateB64s(t *testing.T) {
2727
}
2828
for _, tt := range tests {
2929
t.Run(tt.name, func(t *testing.T) {
30-
c := NewCaptcha(tt.fields.Driver).SetStore(tt.fields.Store)
30+
c := NewCaptcha(tt.fields.Driver, tt.fields.Store)
3131
gotId, b64s, _, err := c.Generate()
3232
if (err != nil) != tt.wantErr {
3333
t.Errorf("Captcha.Generate() error = %v, wantErr %v", err, tt.wantErr)
@@ -88,7 +88,7 @@ func TestNewCaptcha(t *testing.T) {
8888
}
8989
for _, tt := range tests {
9090
t.Run(tt.name, func(t *testing.T) {
91-
if got := NewCaptcha(tt.args.driver).SetStore(tt.args.store); !reflect.DeepEqual(got, tt.want) {
91+
if got := NewCaptcha(tt.args.driver, tt.args.store); !reflect.DeepEqual(got, tt.want) {
9292
t.Errorf("NewCaptcha() = %v, want %v", got, tt.want)
9393
}
9494
})

driver_chinese.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"image/color"
55
"strings"
66

7-
fontLoader "github.com/golang-module/base64Captcha/fonts"
7+
"github.com/golang-module/base64Captcha/font"
88
"github.com/golang/freetype/truetype"
99
)
1010

@@ -37,7 +37,7 @@ type DriverChinese struct {
3737

3838
// NewDriverChinese creates a driver of Chinese characters
3939
func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fonts []string) *DriverChinese {
40-
defaultSource := fontLoader.DefaultSource
40+
defaultSource := font.DefaultSource
4141
fontsArray := defaultSource.LoadFonts(fonts)
4242
if len(fontsArray) == 0 {
4343
fontsArray = defaultSource.LoadAll()
@@ -47,7 +47,7 @@ func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int
4747

4848
// ConvertFonts loads fonts by names
4949
func (d *DriverChinese) ConvertFonts() *DriverChinese {
50-
defaultSource := fontLoader.DefaultSource
50+
defaultSource := font.DefaultSource
5151
fontsArray := defaultSource.LoadFonts(d.Fonts)
5252
if len(fontsArray) == 0 {
5353
fontsArray = defaultSource.LoadAll()
@@ -59,7 +59,6 @@ func (d *DriverChinese) ConvertFonts() *DriverChinese {
5959
// GenerateIdQuestionAnswer generates captcha content and its answer
6060
func (d *DriverChinese) GenerateIdQuestionAnswer() (id, content, answer string) {
6161
id = RandomId()
62-
6362
ss := strings.Split(d.Source, ",")
6463
length := len(ss)
6564
if length == 1 {

driver_language.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"image/color"
55
"log"
66

7-
fontLoader "github.com/golang-module/base64Captcha/fonts"
7+
"github.com/golang-module/base64Captcha/font"
88
"github.com/golang/freetype/truetype"
99
)
1010

@@ -100,7 +100,7 @@ func (d *DriverLanguage) DrawCaptcha(content string) (item Item, err error) {
100100
itemChar.drawSineLine()
101101
}
102102

103-
defaultSource := fontLoader.DefaultSource
103+
defaultSource := font.DefaultSource
104104

105105
// draw noise
106106
if d.NoiseCount > 0 {

driver_language_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"reflect"
66
"testing"
77

8-
fontLoader "github.com/golang-module/base64Captcha/fonts"
8+
"github.com/golang-module/base64Captcha/font"
99
"github.com/golang/freetype/truetype"
1010
)
1111

1212
func TestDriverLanguage_DrawCaptcha(t *testing.T) {
13-
defaultSource := fontLoader.DefaultSource
13+
defaultSource := font.DefaultSource
1414
fontChinese := defaultSource.LoadChinese()
1515
ds := NewDriverLanguage(80, 240, 5, OptionShowSineLine|OptionShowSlimeLine|OptionShowHollowLine, 5, nil, []*truetype.Font{fontChinese}, "emotion")
1616

driver_math.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"math/rand"
77
"strings"
88

9-
fontLoader "github.com/golang-module/base64Captcha/fonts"
9+
"github.com/golang-module/base64Captcha/font"
1010
"github.com/golang/freetype/truetype"
1111
)
1212

@@ -34,7 +34,7 @@ type DriverMath struct {
3434

3535
// NewDriverMath creates a driver of math
3636
func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, bgColor *color.RGBA, fonts []string) *DriverMath {
37-
defaultSource := fontLoader.DefaultSource
37+
defaultSource := font.DefaultSource
3838
fontsArray := defaultSource.LoadFonts(fonts)
3939
if len(fontsArray) == 0 {
4040
fontsArray = defaultSource.LoadAll()
@@ -44,7 +44,7 @@ func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, b
4444

4545
// ConvertFonts loads fonts from names
4646
func (d *DriverMath) ConvertFonts() *DriverMath {
47-
defaultSource := fontLoader.DefaultSource
47+
defaultSource := font.DefaultSource
4848
fontsArray := defaultSource.LoadFonts(d.Fonts)
4949
if len(fontsArray) == 0 {
5050
fontsArray = defaultSource.LoadAll()
@@ -98,7 +98,7 @@ func (d *DriverMath) DrawCaptcha(question string) (item Item, err error) {
9898
// 背景有文字干扰
9999
if d.NoiseCount > 0 {
100100
noise := RandText(d.NoiseCount, strings.Repeat(TxtNumbers, d.NoiseCount))
101-
err = itemChar.drawNoise(noise, fontLoader.DefaultSource.LoadAll())
101+
err = itemChar.drawNoise(noise, font.DefaultSource.LoadAll())
102102
if err != nil {
103103
return
104104
}

driver_string.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"image/color"
55
"strings"
66

7-
fontLoader "github.com/golang-module/base64Captcha/fonts"
7+
"github.com/golang-module/base64Captcha/font"
88
"github.com/golang/freetype/truetype"
99
)
1010

@@ -38,7 +38,7 @@ type DriverString struct {
3838

3939
// NewDriverString creates driver
4040
func NewDriverString(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fonts []string) *DriverString {
41-
defaultSource := fontLoader.DefaultSource
41+
defaultSource := font.DefaultSource
4242
fontsArray := defaultSource.LoadFonts(fonts)
4343
if len(fontsArray) == 0 {
4444
fontsArray = defaultSource.LoadAll()
@@ -48,7 +48,7 @@ func NewDriverString(height int, width int, noiseCount int, showLineOptions int,
4848

4949
// ConvertFonts loads fonts by names
5050
func (d *DriverString) ConvertFonts() *DriverString {
51-
defaultSource := fontLoader.DefaultSource
51+
defaultSource := font.DefaultSource
5252
fontsArray := defaultSource.LoadFonts(d.Fonts)
5353
if len(fontsArray) == 0 {
5454
fontsArray = defaultSource.LoadAll()

driver_string_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"reflect"
66
"testing"
77

8-
fontLoader "github.com/golang-module/base64Captcha/fonts"
8+
"github.com/golang-module/base64Captcha/font"
99
"github.com/golang/freetype/truetype"
1010
)
1111

@@ -24,7 +24,7 @@ func TestDriverString_DrawCaptcha(t *testing.T) {
2424
content string
2525
}
2626

27-
defaultSource := fontLoader.DefaultSource
27+
defaultSource := font.DefaultSource
2828
fontsAll := defaultSource.LoadAll()
2929
tests := []struct {
3030
name string

fonts/default.go renamed to font/default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fonts
1+
package font
22

33
import "embed"
44

fonts/loader.go renamed to font/loader.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fonts
1+
package font
22

33
import (
44
"embed"
@@ -8,7 +8,7 @@ import (
88
"github.com/golang/freetype/truetype"
99
)
1010

11-
const FontsPath = "sources"
11+
const FontPath = "sources"
1212

1313
type Source struct {
1414
fs embed.FS
@@ -20,8 +20,8 @@ func NewSource(fs embed.FS) *Source {
2020

2121
// LoadFont import font from file.
2222
func (s *Source) LoadFont(name string) *truetype.Font {
23-
fontsPath := strings.TrimLeft(FontsPath, "/")
24-
fontBytes, err := s.fs.ReadFile(fontsPath + "/" + name)
23+
fontPath := strings.TrimLeft(FontPath, "/")
24+
fontBytes, err := s.fs.ReadFile(fontPath + "/" + name)
2525
if err != nil {
2626
panic(err)
2727
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

item_char.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"log"
1414
"math"
1515

16-
fontLoader "github.com/golang-module/base64Captcha/fonts"
16+
"github.com/golang-module/base64Captcha/font"
1717
"github.com/golang/freetype"
1818
"github.com/golang/freetype/truetype"
19-
"golang.org/x/image/font"
19+
imageFont "golang.org/x/image/font"
2020
)
2121

2222
// ItemChar captcha item of unicode characters
@@ -184,7 +184,7 @@ func (item *ItemChar) drawNoise(noiseText string, fonts []*truetype.Font) error
184184

185185
c.SetClip(item.nrgba.Bounds())
186186
c.SetDst(item.nrgba)
187-
c.SetHinting(font.HintingFull)
187+
c.SetHinting(imageFont.HintingFull)
188188
rawFontSize := float64(item.height) / (1 + float64(randIntn(7))/float64(10))
189189

190190
for _, char := range noiseText {
@@ -209,7 +209,7 @@ func (item *ItemChar) drawText(text string, fonts []*truetype.Font) error {
209209
c.SetDPI(imageStringDpi)
210210
c.SetClip(item.nrgba.Bounds())
211211
c.SetDst(item.nrgba)
212-
c.SetHinting(font.HintingFull)
212+
c.SetHinting(imageFont.HintingFull)
213213

214214
if len(text) == 0 {
215215
return errors.New("text must not be empty, there is nothing to draw")
@@ -263,7 +263,7 @@ func randFontFrom(fonts []*truetype.Font) *truetype.Font {
263263

264264
if fontCount == 0 {
265265
// loading default fonts
266-
fonts = fontLoader.DefaultSource.LoadAll()
266+
fonts = font.DefaultSource.LoadAll()
267267
fontCount = len(fonts)
268268
}
269269
index := randIntn(fontCount)

0 commit comments

Comments
 (0)