Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fonts are not loaded by default #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
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 = FontsAll
}

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 = FontsAll
}
d.fontsArray = tfs

Expand Down
4 changes: 2 additions & 2 deletions driver_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ 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
}
}

//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
}
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{FontChinese}, "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 = FontsAll
}

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 = FontsAll
}
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, FontsAll)
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 = FontsAll
}

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 = FontsAll
}

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, FontsAll}, args{"45Ad8"}, nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
19 changes: 4 additions & 15 deletions fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,19 @@ 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 {
fontCount := len(fonts)

if fontCount == 0 {
//loading default fonts
fonts = fontsAll
fontCount = len(fontsAll)
fonts = FontsAll
}
index := rand.Intn(fontCount)
return fonts[index]
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/DENNEthree-dee/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dennnethree_dee

import (
_ "embed"
)

//go:embed DENNEthree-dee.ttf
var FontBytes []byte
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/actionj/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package actionj

import (
_ "embed"
)

//go:embed actionj.ttf
var FontBytes []byte
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/apothecary/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package apothecary

import (
_ "embed"
)

//go:embed ApothecaryFont.ttf
var FontBytes []byte
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/chromohv/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package chromohv

import (
_ "embed"
)

//go:embed chromohv.ttf
var FontBytes []byte
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/comismsh/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package comismsh

import (
_ "embed"
)

//go:embed Comismsh.ttf
var FontBytes []byte
8 changes: 8 additions & 0 deletions fonts/deborahFancydress/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package deborah_fancydress

import (
_ "embed"
)

//go:embed DeborahFancyDress.ttf
var FontBytes []byte
44 changes: 44 additions & 0 deletions fonts/defaults/font.go
Original file line number Diff line number Diff line change
@@ -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)
}
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/flim-flam/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package flim_flam

import (
_ "embed"
)

//go:embed Flim-Flam.ttf
var FontBytes []byte
22 changes: 22 additions & 0 deletions fonts/fonts.go
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions fonts/fonts_embedded_default.go
Original file line number Diff line number Diff line change
@@ -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,
}
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/rita-smith/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package rita_smith

import (
_ "embed"
)

//go:embed RitaSmith.ttf
var FontBytes []byte
File renamed without changes.
8 changes: 8 additions & 0 deletions fonts/three-dumb/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package three_dumb

import (
_ "embed"
)

//go:embed 3Dumb.ttf
var FontBytes []byte
8 changes: 8 additions & 0 deletions fonts/wqy-microhei/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wqymicrohei

import (
_ "embed"
)

//go:embed wqy-microhei.ttc
var FontBytes []byte
File renamed without changes.
Loading