generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmath.go
86 lines (71 loc) · 1.64 KB
/
math.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package drivers
import (
"image/color"
"clevergo.tech/captchas"
"github.com/mojocn/base64Captcha"
)
// MathOption is a function that receives a pointer of math driver.
type MathOption func(*Math)
// MathHeight sets height.
func MathHeight(height int) MathOption {
return func(m *Math) {
m.height = height
}
}
// MathWidth sets width.
func MathWidth(width int) MathOption {
return func(m *Math) {
m.width = width
}
}
// MathNoiseCount sets noise count.
func MathNoiseCount(count int) MathOption {
return func(m *Math) {
m.noiseCount = count
}
}
// MathBGColor sets background color.
func MathBGColor(color *color.RGBA) MathOption {
return func(m *Math) {
m.bgColor = color
}
}
// MathFonts sets fonts.
func MathFonts(fonts []string) MathOption {
return func(m *Math) {
m.fonts = fonts
}
}
// Math is a math driver.
type Math struct {
*driver
// captcha png height in pixel.
height int
// captcha png width in pixel.
width int
// text noise count.
noiseCount int
showLineOptions int
// background color.
bgColor *color.RGBA
fonts []string
}
var _ captchas.Driver = NewMath()
// NewMath return a math driver.
func NewMath(opts ...MathOption) *Math {
d := &Math{
driver: &driver{htmlTag: htmlTagIMG},
height: 80,
width: 220,
noiseCount: 0,
fonts: []string{"wqy-microhei.ttc"},
}
for _, f := range opts {
f(d)
}
d.driver.driver = base64Captcha.NewDriverMath(d.height, d.width, d.noiseCount, d.showLineOptions, d.bgColor, d.fonts)
return d
}