/
draw.go
49 lines (40 loc) · 1.23 KB
/
draw.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
package toi
import (
"image"
"image/draw"
"github.com/abdullahdiaa/garabic"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
// Doodle represents a text doodle that can be drawn onto an image.
type Doodle struct {
Arabic bool // Indicates whether the text is Arabic or not.
Text string // The text to be drawn.
Dst draw.Image // The destination image onto which the text will be drawn.
Src image.Image // The source image from which the text color will be picked.
Face font.Face // The font face used for rendering the text.
X, Y int // The coordinates where the text will be drawn.
}
// Draw draws the text onto the destination image.
func (d *Doodle) Draw() {
drawer := &font.Drawer{
Dst: d.Dst,
Src: d.Src,
Face: d.Face,
}
// Shape the text if it's Arabic.
var text string
if d.Arabic {
text = garabic.Shape(d.Text)
} else {
text = d.Text
}
// Measure the width of the text.
width := drawer.MeasureString(text).Ceil()
// Calculate the starting position for drawing the text.
startX := d.X - width/2
startY := d.Y
// Set the drawing position and draw the text.
drawer.Dot = fixed.Point26_6{X: fixed.I(startX), Y: fixed.I(startY)}
drawer.DrawString(text)
}