forked from mojocn/base64Captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_string.go
130 lines (102 loc) · 2.99 KB
/
driver_string.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package base64Captcha
import (
"image/color"
"strings"
"github.com/golang/freetype/truetype"
)
//DriverChar captcha config for captcha-engine-characters.
type DriverString struct {
// Height png height in pixel.
Height int
// Width Captcha png width in pixel.
Width int
//NoiseCount text noise count.
NoiseCount int
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
ShowLineOptions int
//Length random string length.
Length int
//Source is a unicode which is the rand string from.
Source string
//BgColor captcha image background color (optional)
BgColor *color.RGBA
//fontsStorage font storage (optional)
fontsStorage FontsStorage
//Fonts loads by name see fonts.go's comment
Fonts []string
fontsArray []*truetype.Font
}
//NewDriverString creates driver
func NewDriverString(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverString {
if fontsStorage == nil {
fontsStorage = DefaultEmbeddedFonts
}
tfs := []*truetype.Font{}
for _, fff := range fonts {
tf := fontsStorage.LoadFontByName("fonts/" + fff)
tfs = append(tfs, tf)
}
if len(tfs) == 0 {
tfs = fontsAll
}
return &DriverString{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, Length: length, Source: source, BgColor: bgColor, fontsStorage: fontsStorage, fontsArray: tfs, Fonts: fonts}
}
//ConvertFonts loads fonts by names
func (d *DriverString) ConvertFonts() *DriverString {
if d.fontsStorage == nil {
d.fontsStorage = DefaultEmbeddedFonts
}
tfs := []*truetype.Font{}
for _, fff := range d.Fonts {
tf := d.fontsStorage.LoadFontByName("fonts/" + fff)
tfs = append(tfs, tf)
}
if len(tfs) == 0 {
tfs = fontsAll
}
d.fontsArray = tfs
return d
}
//GenerateIdQuestionAnswer creates id,content and answer
func (d *DriverString) GenerateIdQuestionAnswer() (id, content, answer string) {
id = RandomId()
content = RandText(d.Length, d.Source)
return id, content, content
}
//DrawCaptcha draws captcha item
func (d *DriverString) DrawCaptcha(content string) (item Item, err error) {
var bgc color.RGBA
if d.BgColor != nil {
bgc = *d.BgColor
} else {
bgc = RandLightColor()
}
itemChar := NewItemChar(d.Width, d.Height, bgc)
//draw hollow line
if d.ShowLineOptions&OptionShowHollowLine == OptionShowHollowLine {
itemChar.drawHollowLine()
}
//draw slime line
if d.ShowLineOptions&OptionShowSlimeLine == OptionShowSlimeLine {
itemChar.drawSlimLine(3)
}
//draw sine line
if d.ShowLineOptions&OptionShowSineLine == OptionShowSineLine {
itemChar.drawSineLine()
}
//draw noise
if d.NoiseCount > 0 {
source := TxtNumbers + TxtAlphabet + ",.[]<>"
noise := RandText(d.NoiseCount, strings.Repeat(source, d.NoiseCount))
err = itemChar.drawNoise(noise, d.fontsArray)
if err != nil {
return
}
}
//draw content
err = itemChar.drawText(content, d.fontsArray)
if err != nil {
return
}
return itemChar, nil
}