/
drawer.go
145 lines (117 loc) · 2.87 KB
/
drawer.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
package entity
import (
"image"
"image/color"
"image/draw"
"image/jpeg"
"math"
"os"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
type Drawer interface {
DrawFace(rectangle image.Rectangle, name string)
drawLine(img draw.Image, x0, y0, x1, y1 int, color color.RGBA)
drawName(txt string, x, y int, h float64)
loadImage(path string) error
loadFont(path string) error
SaveImage(path string) error
}
type DrawerImpl struct {
img *image.Image
dst *image.RGBA
font *truetype.Font
color color.RGBA
}
func (d *DrawerImpl) DrawFace(rectangle image.Rectangle, name string) {
d.drawLine(d.dst, rectangle.Min.X, rectangle.Min.Y, rectangle.Max.X, rectangle.Min.Y, d.color)
d.drawLine(d.dst, rectangle.Max.X, rectangle.Min.Y, rectangle.Max.X, rectangle.Max.Y, d.color)
d.drawLine(d.dst, rectangle.Max.X, rectangle.Max.Y, rectangle.Min.X, rectangle.Max.Y, d.color)
d.drawLine(d.dst, rectangle.Min.X, rectangle.Max.Y, rectangle.Min.X, rectangle.Min.Y, d.color)
d.drawName(name, rectangle.Min.X, rectangle.Max.Y, float64(rectangle.Max.Y-rectangle.Min.Y))
}
func (d *DrawerImpl) drawLine(img draw.Image, x0, y0, x1, y1 int, color color.RGBA) {
dx := math.Abs(float64(x1 - x0))
dy := -math.Abs(float64(y1 - y0))
sx := 1
sy := 1
if x0 > x1 {
sx = -1
}
if y0 > y1 {
sy = -1
}
err := dx + dy
for {
img.Set(x0, y0, color)
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 >= dy {
err += dy
x0 += sx
}
if e2 <= dx {
err += dx
y0 += sy
}
}
}
func (d *DrawerImpl) drawName(txt string, x, y int, h float64) {
// Define the font size as a fraction of the face height
fontSize := h / 3
dr := &font.Drawer{
Dst: d.dst,
Src: image.NewUniform(d.color),
Face: truetype.NewFace(d.font, &truetype.Options{Size: fontSize, Hinting: font.HintingNone, DPI: 72.0}),
}
dr.Dot = fixed.Point26_6{
X: fixed.I(x),
Y: fixed.I(y + int(fontSize)),
}
dr.DrawString(txt)
}
func (d *DrawerImpl) loadFont(path string) error {
fontBytes, err := os.ReadFile(path)
if err != nil {
return err
}
f, err := truetype.Parse(fontBytes)
if err != nil {
return err
}
d.font = f
return nil
}
func (d *DrawerImpl) loadImage(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
img, err := jpeg.Decode(f)
if err != nil {
return err
}
d.img = &img
return nil
}
func (d *DrawerImpl) SaveImage(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return jpeg.Encode(f, d.dst, nil)
}
func NewDrawer(imagePath, fontPath string) Drawer {
drawer := &DrawerImpl{}
drawer.loadImage(imagePath)
drawer.loadFont(fontPath)
drawer.dst = image.NewRGBA((*drawer.img).Bounds())
draw.Draw(drawer.dst, drawer.dst.Bounds(), (*drawer.img), (*drawer.img).Bounds().Min, draw.Src)
drawer.color = color.RGBA{0, 255, 0, 255}
return drawer
}