-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
57 lines (52 loc) · 1.39 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package captcha
import (
"image/color"
"os"
"github.com/virzz/logger"
"golang.org/x/image/font"
)
type Option func(cb *CaptchaBase)
func WithSize(width, hight int) Option {
return func(cb *CaptchaBase) { cb.height, cb.width = hight, width }
}
func WithBackground(color color.Color) Option {
return func(cb *CaptchaBase) { cb.background = color }
}
func WithFront(color color.Color) Option {
return func(cb *CaptchaBase) { cb.front = color }
}
func WithExpire(t int64) Option {
return func(cb *CaptchaBase) { cb.expire = t }
}
func WithLine(n int) Option {
return func(cb *CaptchaBase) { cb.line = n }
}
func WithPoint(n int) Option {
return func(cb *CaptchaBase) { cb.point = n }
}
func WithFontSize(size float64) Option {
return func(cb *CaptchaBase) { cb.fontSize = size }
}
func WithFont(fontFace font.Face, size float64) Option {
return func(cb *CaptchaBase) { cb.fontFace, cb.fontSize = fontFace, size }
}
func WithFontName(fontName string, fontSize float64, dpi ...float64) Option {
return func(cb *CaptchaBase) {
buf, err := os.ReadFile(fontName)
if err != nil {
logger.Error(err)
} else {
WithFontByte(buf, fontSize, dpi...)(cb)
}
}
}
func WithFontByte(buf []byte, fontSize float64, dpi ...float64) Option {
return func(cb *CaptchaBase) {
fontFace, err := getFontFace(buf, fontSize, dpi...)
if err != nil {
logger.Error(err)
return
}
WithFont(fontFace, fontSize)(cb)
}
}