-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
63 lines (48 loc) · 1.16 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package text2picture
import (
"image"
"image/color"
"golang.org/x/image/math/fixed"
)
// Color can be generated using text2picture.NewColor()
func (p *picture) DrawWithColor(color color.Color, text string) *picture {
// font color
p.c.SetSrc(image.NewUniform(color))
p.handleText(&text)
return p
}
func (p *picture) DrawWithBlack(text string) *picture {
// font color
p.c.SetSrc(Black)
p.handleText(&text)
return p
}
func (p *picture) DrawWithWhite(text string) *picture {
// font color
p.c.SetSrc(White)
p.handleText(&text)
return p
}
func (p *picture) handleText(text *string) {
for _, x := range *text {
w, _ := p.face.GlyphAdvance(x)
if x == '\n' {
p.newline()
continue
} else if x == '\t' {
x = ' '
} else if p.font.Index(x) == 0 {
continue
} else if p.pt.X.Round()+w.Round() > p.rgba.Bounds().Max.X-p.padding {
p.newline()
}
p.pt, _ = p.c.DrawString(string(x), p.pt)
}
}
func (p *picture) newline() {
p.pt.X = fixed.Int26_6(p.padding) << 6
p.pt.Y += p.c.PointToFixed(p.fontSize + float64(p.line_spacing))
}
func (p *picture) NextLineDistance() int {
return p.c.PointToFixed(p.fontSize + float64(p.line_spacing)).Round()
}