diff --git a/README.md b/README.md index 864bf1a..8531fb3 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,48 @@ func main() { #### 2.3.7 Example Use Etcd as store [captcha with etcd database as store](captcha_with_etcd_exmaple.md) + +#### 2.3.8 Load Fonts +Because the font is loaded by default, resulting in unwanted data at compile time. Add compilation only when you use it.[#110](https://github.com/mojocn/base64Captcha/issues/110) + +```go +import ( + _ "github.com/mojocn/base64Captcha/fonts" // defaults load all embed fonts +) +... + +import ( + _ "github.com/mojocn/base64Captcha/fonts/defaults" // load fonts without chinese +) +... + +import ( + _ "embed" + dennnethree_dee "github.com/mojocn/base64Captcha/fonts/DENNEthree-dee" + "github.com/mojocn/base64Captcha/fonts/actionj" + "github.com/mojocn/base64Captcha/fonts/apothecary" +) + +//go:embed a.ttf +var your_fonts []byte + +var defaultfonts = map[string][]byte{ + "ApothecaryFont": apothecary.FontBytes, + "DENNEthree-dee": dennnethree_dee.FontBytes, + "actionj": actionj.FontBytes, + "your_name": your_fonts, +} + +base64Captcha.DefaultEmbeddedFonts = base64Captcha.NewEmbeddedFontsStorage(defaultfonts) + +base64Captcha.FontChinese = base64Captcha.DefaultEmbeddedFonts.LoadFontByName("your_name") +base64Captcha.FontsAll = append(base64Captcha.DefaultEmbeddedFonts.LoadFontsByNames([]string{ + "ApothecaryFont", + "DENNEthree-dee", + "actionj", +}), base64Captcha.FontChinese) +``` + ## 3. 🎨🎨🎨 Customization You can customize your captcha display image by implementing [interface driver](interface_driver.go) and [interface item](interface_item.go). diff --git a/driver_chinese.go b/driver_chinese.go index 892d406..0520e2a 100644 --- a/driver_chinese.go +++ b/driver_chinese.go @@ -51,7 +51,7 @@ func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int } if len(tfs) == 0 { - tfs = fontsAll + tfs = FontsAll } return &DriverChinese{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, Length: length, Source: source, BgColor: bgColor, fontsStorage: fontsStorage, fontsArray: tfs} @@ -69,7 +69,7 @@ func (d *DriverChinese) ConvertFonts() *DriverChinese { tfs = append(tfs, tf) } if len(tfs) == 0 { - tfs = fontsAll + tfs = FontsAll } d.fontsArray = tfs diff --git a/driver_language.go b/driver_language.go index 17e0d0b..76854e3 100644 --- a/driver_language.go +++ b/driver_language.go @@ -106,7 +106,7 @@ func (d *DriverLanguage) DrawCaptcha(content string) (item Item, err error) { //draw noise if d.NoiseCount > 0 { noise := RandText(d.NoiseCount, TxtNumbers+TxtAlphabet+",.[]<>") - err = itemChar.drawNoise(noise, fontsAll) + err = itemChar.drawNoise(noise, FontsAll) if err != nil { return } @@ -114,7 +114,7 @@ func (d *DriverLanguage) DrawCaptcha(content string) (item Item, err error) { //draw content //use font that match your language - err = itemChar.drawText(content, []*truetype.Font{fontChinese}) + err = itemChar.drawText(content, []*truetype.Font{FontChinese}) if err != nil { return } diff --git a/driver_language_test.go b/driver_language_test.go index 3a8a71b..3529a75 100644 --- a/driver_language_test.go +++ b/driver_language_test.go @@ -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{FontChinese}, "emotion") for i := 0; i < 40; i++ { _, q, _ := ds.GenerateIdQuestionAnswer() diff --git a/driver_math.go b/driver_math.go index 7fba315..5ab7e65 100644 --- a/driver_math.go +++ b/driver_math.go @@ -47,7 +47,7 @@ func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, b } if len(tfs) == 0 { - tfs = fontsAll + tfs = FontsAll } return &DriverMath{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, fontsArray: tfs, BgColor: bgColor, Fonts: fonts} @@ -65,7 +65,7 @@ func (d *DriverMath) ConvertFonts() *DriverMath { tfs = append(tfs, tf) } if len(tfs) == 0 { - tfs = fontsAll + tfs = FontsAll } d.fontsArray = tfs @@ -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, FontsAll) if err != nil { return } diff --git a/driver_string.go b/driver_string.go index d67d5f8..6f9929b 100644 --- a/driver_string.go +++ b/driver_string.go @@ -51,7 +51,7 @@ func NewDriverString(height int, width int, noiseCount int, showLineOptions int, } if len(tfs) == 0 { - tfs = fontsAll + tfs = FontsAll } return &DriverString{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, Length: length, Source: source, BgColor: bgColor, fontsStorage: fontsStorage, fontsArray: tfs, Fonts: fonts} @@ -69,7 +69,7 @@ func (d *DriverString) ConvertFonts() *DriverString { tfs = append(tfs, tf) } if len(tfs) == 0 { - tfs = fontsAll + tfs = FontsAll } d.fontsArray = tfs diff --git a/driver_string_test.go b/driver_string_test.go index d9ff843..23f6b28 100644 --- a/driver_string_test.go +++ b/driver_string_test.go @@ -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, FontsAll}, args{"45Ad8"}, nil, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/fonts.go b/fonts.go index 0556728..148abe0 100644 --- a/fonts.go +++ b/fonts.go @@ -6,21 +6,11 @@ import ( "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", -}) +var DefaultEmbeddedFonts FontsStorage // var fontemoji = loadFontByName("fonts/seguiemj.ttf") -var fontsAll = append(fontsSimple, fontChinese) -var fontChinese = DefaultEmbeddedFonts.LoadFontByName("fonts/wqy-microhei.ttc") +var FontsAll []*truetype.Font +var FontChinese *truetype.Font // randFontFrom choose random font family.选择随机的字体 func randFontFrom(fonts []*truetype.Font) *truetype.Font { @@ -28,8 +18,7 @@ func randFontFrom(fonts []*truetype.Font) *truetype.Font { if fontCount == 0 { //loading default fonts - fonts = fontsAll - fontCount = len(fontsAll) + fonts = FontsAll } index := rand.Intn(fontCount) return fonts[index] diff --git a/fonts/DENNEthree-dee.ttf b/fonts/DENNEthree-dee/DENNEthree-dee.ttf similarity index 100% rename from fonts/DENNEthree-dee.ttf rename to fonts/DENNEthree-dee/DENNEthree-dee.ttf diff --git a/fonts/DENNEthree-dee/font.go b/fonts/DENNEthree-dee/font.go new file mode 100644 index 0000000..2bd26e1 --- /dev/null +++ b/fonts/DENNEthree-dee/font.go @@ -0,0 +1,8 @@ +package dennnethree_dee + +import ( + _ "embed" +) + +//go:embed DENNEthree-dee.ttf +var FontBytes []byte diff --git a/fonts/actionj.ttf b/fonts/actionj/actionj.ttf similarity index 100% rename from fonts/actionj.ttf rename to fonts/actionj/actionj.ttf diff --git a/fonts/actionj/font.go b/fonts/actionj/font.go new file mode 100644 index 0000000..4f7baf8 --- /dev/null +++ b/fonts/actionj/font.go @@ -0,0 +1,8 @@ +package actionj + +import ( + _ "embed" +) + +//go:embed actionj.ttf +var FontBytes []byte diff --git a/fonts/ApothecaryFont.ttf b/fonts/apothecary/ApothecaryFont.ttf similarity index 100% rename from fonts/ApothecaryFont.ttf rename to fonts/apothecary/ApothecaryFont.ttf diff --git a/fonts/apothecary/font.go b/fonts/apothecary/font.go new file mode 100644 index 0000000..e8c1365 --- /dev/null +++ b/fonts/apothecary/font.go @@ -0,0 +1,8 @@ +package apothecary + +import ( + _ "embed" +) + +//go:embed ApothecaryFont.ttf +var FontBytes []byte diff --git a/fonts/chromohv.ttf b/fonts/chromohv/chromohv.ttf similarity index 100% rename from fonts/chromohv.ttf rename to fonts/chromohv/chromohv.ttf diff --git a/fonts/chromohv/font.go b/fonts/chromohv/font.go new file mode 100644 index 0000000..2df4c73 --- /dev/null +++ b/fonts/chromohv/font.go @@ -0,0 +1,8 @@ +package chromohv + +import ( + _ "embed" +) + +//go:embed chromohv.ttf +var FontBytes []byte diff --git a/fonts/Comismsh.ttf b/fonts/comismsh/Comismsh.ttf similarity index 100% rename from fonts/Comismsh.ttf rename to fonts/comismsh/Comismsh.ttf diff --git a/fonts/comismsh/font.go b/fonts/comismsh/font.go new file mode 100644 index 0000000..31c3498 --- /dev/null +++ b/fonts/comismsh/font.go @@ -0,0 +1,8 @@ +package comismsh + +import ( + _ "embed" +) + +//go:embed Comismsh.ttf +var FontBytes []byte diff --git a/fonts/DeborahFancyDress.ttf b/fonts/deborahFancydress/DeborahFancyDress.ttf similarity index 100% rename from fonts/DeborahFancyDress.ttf rename to fonts/deborahFancydress/DeborahFancyDress.ttf diff --git a/fonts/deborahFancydress/font.go b/fonts/deborahFancydress/font.go new file mode 100644 index 0000000..5901438 --- /dev/null +++ b/fonts/deborahFancydress/font.go @@ -0,0 +1,8 @@ +package deborah_fancydress + +import ( + _ "embed" +) + +//go:embed DeborahFancyDress.ttf +var FontBytes []byte diff --git a/fonts/defaults/font.go b/fonts/defaults/font.go new file mode 100644 index 0000000..08e7096 --- /dev/null +++ b/fonts/defaults/font.go @@ -0,0 +1,44 @@ +// load fonts without chinese font +package defaults + +import ( + "github.com/mojocn/base64Captcha" + dennnethree_dee "github.com/mojocn/base64Captcha/fonts/DENNEthree-dee" + "github.com/mojocn/base64Captcha/fonts/actionj" + "github.com/mojocn/base64Captcha/fonts/apothecary" + "github.com/mojocn/base64Captcha/fonts/chromohv" + "github.com/mojocn/base64Captcha/fonts/comismsh" + deborah_fancydress "github.com/mojocn/base64Captcha/fonts/deborahFancydress" + flim_flam "github.com/mojocn/base64Captcha/fonts/flim-flam" + rita_smith "github.com/mojocn/base64Captcha/fonts/rita-smith" + three_dumb "github.com/mojocn/base64Captcha/fonts/three-dumb" +) + +// defaultEmbeddedFontsFS Built-in font storage. +var defaultEmbeddedFontsFS = map[string][]byte{ + "3Dumb": three_dumb.FontBytes, + "ApothecaryFont": apothecary.FontBytes, + "Comismsh": comismsh.FontBytes, + "DENNEthree-dee": dennnethree_dee.FontBytes, + "DeborahFancyDress": deborah_fancydress.FontBytes, + "Flim-Flam": flim_flam.FontBytes, + "RitaSmith": rita_smith.FontBytes, + "actionj": actionj.FontBytes, + "chromohv": chromohv.FontBytes, +} + +func init() { + base64Captcha.DefaultEmbeddedFonts = base64Captcha.NewEmbeddedFontsStorage(defaultEmbeddedFontsFS) + var fontsSimple = base64Captcha.DefaultEmbeddedFonts.LoadFontsByNames([]string{ + "3Dumb", + "ApothecaryFont", + "Comismsh", + "DENNEthree-dee", + "DeborahFancyDress", + "Flim-Flam", + "RitaSmith", + "actionj", + "chromohv", + }) + base64Captcha.FontsAll = append(fontsSimple, base64Captcha.FontChinese) +} diff --git a/fonts/Flim-Flam.ttf b/fonts/flim-flam/Flim-Flam.ttf similarity index 100% rename from fonts/Flim-Flam.ttf rename to fonts/flim-flam/Flim-Flam.ttf diff --git a/fonts/flim-flam/font.go b/fonts/flim-flam/font.go new file mode 100644 index 0000000..e9353cd --- /dev/null +++ b/fonts/flim-flam/font.go @@ -0,0 +1,8 @@ +package flim_flam + +import ( + _ "embed" +) + +//go:embed Flim-Flam.ttf +var FontBytes []byte diff --git a/fonts/fonts.go b/fonts/fonts.go new file mode 100644 index 0000000..bca00f5 --- /dev/null +++ b/fonts/fonts.go @@ -0,0 +1,22 @@ +package fonts + +import ( + "github.com/mojocn/base64Captcha" +) + +func init() { + base64Captcha.DefaultEmbeddedFonts = base64Captcha.NewEmbeddedFontsStorage(defaultEmbeddedFontsFS) + var fontsSimple = base64Captcha.DefaultEmbeddedFonts.LoadFontsByNames([]string{ + "3Dumb", + "ApothecaryFont", + "Comismsh", + "DENNEthree-dee", + "DeborahFancyDress", + "Flim-Flam", + "RitaSmith", + "actionj", + "chromohv", + }) + base64Captcha.FontChinese = base64Captcha.DefaultEmbeddedFonts.LoadFontByName("wqy-microhei") + base64Captcha.FontsAll = append(fontsSimple, base64Captcha.FontChinese) +} diff --git a/fonts/fonts_embedded_default.go b/fonts/fonts_embedded_default.go new file mode 100644 index 0000000..34ecaaa --- /dev/null +++ b/fonts/fonts_embedded_default.go @@ -0,0 +1,28 @@ +package fonts + +import ( + dennnethree_dee "github.com/mojocn/base64Captcha/fonts/DENNEthree-dee" + "github.com/mojocn/base64Captcha/fonts/actionj" + "github.com/mojocn/base64Captcha/fonts/apothecary" + "github.com/mojocn/base64Captcha/fonts/chromohv" + "github.com/mojocn/base64Captcha/fonts/comismsh" + deborah_fancydress "github.com/mojocn/base64Captcha/fonts/deborahFancydress" + flim_flam "github.com/mojocn/base64Captcha/fonts/flim-flam" + rita_smith "github.com/mojocn/base64Captcha/fonts/rita-smith" + three_dumb "github.com/mojocn/base64Captcha/fonts/three-dumb" + wqymicrohei "github.com/mojocn/base64Captcha/fonts/wqy-microhei" +) + +// defaultEmbeddedFontsFS Built-in font storage. +var defaultEmbeddedFontsFS = map[string][]byte{ + "3Dumb": three_dumb.FontBytes, + "ApothecaryFont": apothecary.FontBytes, + "Comismsh": comismsh.FontBytes, + "DENNEthree-dee": dennnethree_dee.FontBytes, + "DeborahFancyDress": deborah_fancydress.FontBytes, + "Flim-Flam": flim_flam.FontBytes, + "RitaSmith": rita_smith.FontBytes, + "actionj": actionj.FontBytes, + "chromohv": chromohv.FontBytes, + "wqy-microhei": wqymicrohei.FontBytes, +} diff --git a/fonts/RitaSmith.ttf b/fonts/rita-smith/RitaSmith.ttf similarity index 100% rename from fonts/RitaSmith.ttf rename to fonts/rita-smith/RitaSmith.ttf diff --git a/fonts/rita-smith/font.go b/fonts/rita-smith/font.go new file mode 100644 index 0000000..5c946a5 --- /dev/null +++ b/fonts/rita-smith/font.go @@ -0,0 +1,8 @@ +package rita_smith + +import ( + _ "embed" +) + +//go:embed RitaSmith.ttf +var FontBytes []byte diff --git a/fonts/3Dumb.ttf b/fonts/three-dumb/3Dumb.ttf similarity index 100% rename from fonts/3Dumb.ttf rename to fonts/three-dumb/3Dumb.ttf diff --git a/fonts/three-dumb/font.go b/fonts/three-dumb/font.go new file mode 100644 index 0000000..50077d3 --- /dev/null +++ b/fonts/three-dumb/font.go @@ -0,0 +1,8 @@ +package three_dumb + +import ( + _ "embed" +) + +//go:embed 3Dumb.ttf +var FontBytes []byte diff --git a/fonts/wqy-microhei/font.go b/fonts/wqy-microhei/font.go new file mode 100644 index 0000000..5fef4d9 --- /dev/null +++ b/fonts/wqy-microhei/font.go @@ -0,0 +1,8 @@ +package wqymicrohei + +import ( + _ "embed" +) + +//go:embed wqy-microhei.ttc +var FontBytes []byte diff --git a/fonts/wqy-microhei.ttc b/fonts/wqy-microhei/wqy-microhei.ttc similarity index 100% rename from fonts/wqy-microhei.ttc rename to fonts/wqy-microhei/wqy-microhei.ttc diff --git a/fonts_embedded.go b/fonts_embedded.go index 19c75fe..5d902b8 100644 --- a/fonts_embedded.go +++ b/fonts_embedded.go @@ -1,20 +1,18 @@ package base64Captcha import ( - "embed" - "github.com/golang/freetype" "github.com/golang/freetype/truetype" ) type EmbeddedFontsStorage struct { - fs embed.FS + data map[string][]byte } func (s *EmbeddedFontsStorage) LoadFontByName(name string) *truetype.Font { - fontBytes, err := s.fs.ReadFile(name) - if err != nil { - panic(err) + fontBytes, ok := s.data[name] + if !ok { + panic("font not found.") } //font file bytes to trueTypeFont @@ -37,8 +35,8 @@ func (s *EmbeddedFontsStorage) LoadFontsByNames(assetFontNames []string) []*true return fonts } -func NewEmbeddedFontsStorage(fs embed.FS) *EmbeddedFontsStorage { +func NewEmbeddedFontsStorage(data map[string][]byte) *EmbeddedFontsStorage { return &EmbeddedFontsStorage{ - fs: fs, + data: data, } } diff --git a/fonts_embedded_default.go b/fonts_embedded_default.go deleted file mode 100644 index 0d26714..0000000 --- a/fonts_embedded_default.go +++ /dev/null @@ -1,11 +0,0 @@ -package base64Captcha - -import "embed" - -// defaultEmbeddedFontsFS Built-in font storage. -// -//go:embed fonts/*.ttf -//go:embed fonts/*.ttc -var defaultEmbeddedFontsFS embed.FS - -var DefaultEmbeddedFonts = NewEmbeddedFontsStorage(defaultEmbeddedFontsFS) diff --git a/fonts_test.go b/fonts_test.go index 16c4c60..b292546 100644 --- a/fonts_test.go +++ b/fonts_test.go @@ -45,7 +45,7 @@ func Test_loadFontsByNames(t *testing.T) { } func Test_randFontFrom(t *testing.T) { - f := randFontFrom(fontsAll) + f := randFontFrom(FontsAll) if f == nil { t.Error("failed") }