-
Notifications
You must be signed in to change notification settings - Fork 61
/
text.go
173 lines (141 loc) · 3.66 KB
/
text.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gocaptcha
import (
"errors"
"image"
"image/draw"
"math"
"math/rand"
"time"
"github.com/golang/freetype"
"golang.org/x/image/font"
)
var (
ErrNilCanvas = errors.New("canvas is nil")
ErrNilText = errors.New("text is nil")
)
// TextDrawer is a text drawer interface.
type TextDrawer interface {
DrawString(canvas draw.Image, text string) error
}
type textDrawer struct {
dpi float64
r *rand.Rand
}
// DrawString draws a string on the canvas.
func (t *textDrawer) DrawString(canvas draw.Image, text string) error {
if len(text) == 0 {
return ErrNilText
}
if canvas == nil {
return ErrNilCanvas
}
c := freetype.NewContext()
if t.dpi <= 0 {
t.dpi = 72
}
c.SetDPI(t.dpi)
c.SetClip(canvas.Bounds())
c.SetDst(canvas)
c.SetHinting(font.HintingFull)
fontWidth := canvas.Bounds().Dx() / len(text)
for i, s := range text {
fontSize := float64(canvas.Bounds().Dy()) / (1 + float64(t.r.Intn(7))/float64(9))
c.SetSrc(image.NewUniform(RandDeepColor()))
c.SetFontSize(fontSize)
f, err := DefaultFontFamily.Random()
if err != nil {
return err
}
c.SetFont(f)
x := (fontWidth)*i + (fontWidth)/int(fontSize)
y := 5 + t.r.Intn(canvas.Bounds().Dy()/2) + int(fontSize/2)
pt := freetype.Pt(x, y)
_, err = c.DrawString(string(s), pt)
if err != nil {
return err
}
}
return nil
}
// NewTextDrawer returns a new text drawer.
func NewTextDrawer(dpi float64) TextDrawer {
return &textDrawer{
dpi: dpi,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
type twistTextDrawer struct {
dpi float64
r *rand.Rand
amplitude float64
frequency float64
}
// DrawString draws a string on the canvas.
func (t *twistTextDrawer) DrawString(canvas draw.Image, text string) error {
if len(text) == 0 {
return ErrNilText
}
if canvas == nil {
return ErrNilCanvas
}
// 创建一个新的画布用于存储扭曲后的图像
textCanvas := image.NewRGBA(image.Rect(0, 0, canvas.Bounds().Dx(), canvas.Bounds().Dy()))
draw.Draw(textCanvas, textCanvas.Bounds(), image.Transparent, image.Point{}, draw.Src)
c := freetype.NewContext()
if t.dpi <= 0 {
t.dpi = 72
}
c.SetDPI(t.dpi)
c.SetClip(canvas.Bounds())
c.SetDst(textCanvas)
c.SetHinting(font.HintingFull)
fontWidth := canvas.Bounds().Dx() / len(text)
for i, s := range text {
fontSize := float64(canvas.Bounds().Dy()) / (1 + float64(t.r.Intn(7))/float64(9))
c.SetSrc(image.NewUniform(RandDeepColor()))
c.SetFontSize(fontSize)
f, err := DefaultFontFamily.Random()
if err != nil {
return err
}
c.SetFont(f)
x := (fontWidth)*i + (fontWidth)/int(fontSize)
y := 5 + t.r.Intn(canvas.Bounds().Dy()/2) + int(fontSize/2)
pt := freetype.Pt(x, y)
_, err = c.DrawString(string(s), pt)
if err != nil {
return err
}
}
return t.twistEffect(textCanvas, canvas)
}
func (t *twistTextDrawer) twistEffect(src image.Image, dst draw.Image) error {
width := src.Bounds().Dx()
height := src.Bounds().Dy()
// 遍历源图像像素
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// 计算扭曲后的坐标
dx := int(t.amplitude * math.Sin(t.frequency*float64(y)))
newX := x + dx
newY := y
// 如果新坐标在目标图像范围内,设置像素
if newX >= 0 && newX < width && newY >= 0 && newY < height {
_, _, _, a := src.At(x, y).RGBA()
if a != 0 {
dst.Set(newX, newY, src.At(x, y))
}
}
}
}
return nil
}
// NewTwistTextDrawer returns a new text drawer with twist effect.
func NewTwistTextDrawer(dpi float64, amplitude float64, frequency float64) TextDrawer {
return &twistTextDrawer{
dpi: dpi,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
amplitude: amplitude,
frequency: frequency,
}
}