-
Notifications
You must be signed in to change notification settings - Fork 110
/
draw.go
55 lines (44 loc) · 1.48 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
package rimage
import (
"image"
"image/color"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/goregular"
)
var font *truetype.Font
// init sets up the fonts we want to use.
func init() {
var err error
font, err = truetype.Parse(goregular.TTF)
if err != nil {
panic(err)
}
}
// Font returns the font we use for drawing.
func Font() *truetype.Font {
return font
}
// DrawString writes a string to the given context at a particular point.
func DrawString(dc *gg.Context, text string, p image.Point, c color.Color, size float64) {
dc.SetFontFace(truetype.NewFace(Font(), &truetype.Options{Size: size}))
dc.SetColor(c)
dc.DrawStringWrapped(text, float64(p.X), float64(p.Y), 0, 0, float64(dc.Width()), 1, 0)
}
// DrawRectangleEmpty draws the given rectangle into the context. The positions of the
// rectangle are used to place it within the context.
func DrawRectangleEmpty(dc *gg.Context, r image.Rectangle, c color.Color, width float64) {
dc.SetColor(c)
dc.DrawLine(float64(r.Min.X), float64(r.Min.Y), float64(r.Max.X), float64(r.Min.Y))
dc.SetLineWidth(width)
dc.Stroke()
dc.DrawLine(float64(r.Min.X), float64(r.Min.Y), float64(r.Min.X), float64(r.Max.Y))
dc.SetLineWidth(width)
dc.Stroke()
dc.DrawLine(float64(r.Max.X), float64(r.Min.Y), float64(r.Max.X), float64(r.Max.Y))
dc.SetLineWidth(width)
dc.Stroke()
dc.DrawLine(float64(r.Min.X), float64(r.Max.Y), float64(r.Max.X), float64(r.Max.Y))
dc.SetLineWidth(width)
dc.Stroke()
}