-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
171 lines (151 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
package tempura
import (
"image/color"
"fmt"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text"
"golang.org/x/image/font"
)
// Align is used to specify text alignment
type Align uint8
const (
// AlignLeft specified to draw the left edge of text at the specified x coordinate
AlignLeft Align = iota
// AlignRight specifies to draw the text horizontally centered at the specified x coordinate
AlignCenter
// AlignRight specifies to draw the right edge of text at the specified x coordinate
AlignRight
)
type Text struct {
Face font.Face
Color color.Color
Text string
W int
H int
Advance int
}
func NewText(face font.Face, color color.Color, text string) Text {
w, h, advance := MeasureText(text, face)
return Text{
Face: face,
Color: color,
Text: text,
W: w,
H: h,
Advance: advance,
}
}
func NewTextf(face font.Face, color color.Color, text string, args ...interface{}) Text {
if len(args) == 0 {
return NewText(face, color, text)
}
return NewText(face, color, fmt.Sprintf(text, args...))
}
func (t Text) Draw(image *ebiten.Image, x, y int, align Align) {
// TODO(explodes): do not draw if text is not in bounds of image
switch align {
case AlignCenter:
x = x - t.W/2
case AlignRight:
x = x - t.W
}
text.Draw(image, t.Text, t.Face, x, y, t.Color)
}
type Texts []Text
func NewTexts(face font.Face, color color.Color, texts []string) Texts {
measured := make(Texts, len(texts))
for i, t := range texts {
measured[i] = NewText(face, color, t)
}
return measured
}
func (ts *Texts) Push(face font.Face, color color.Color, text string) {
next := append(*ts, NewText(face, color, text))
*ts = next
}
func (ts *Texts) Pushf(face font.Face, color color.Color, text string, args ...interface{}) {
next := append(*ts, NewTextf(face, color, text, args...))
*ts = next
}
func (ts Texts) DrawSingleLine(image *ebiten.Image, x, y int, align Align) {
switch align {
case AlignLeft:
for _, t := range ts {
t.Draw(image, x, y, AlignLeft)
x += t.Advance
}
case AlignCenter:
width := ts.SingleLineWidth()
for _, t := range ts {
t.Draw(image, x-width/2, y, AlignLeft)
x += t.Advance
}
case AlignRight:
width := ts.SingleLineWidth()
for _, t := range ts {
t.Draw(image, x-width, y, AlignLeft)
x += t.Advance
}
}
}
func (ts Texts) SingleLineWidth() int {
width := 0
for _, t := range ts {
width += t.Advance
}
return width
}
func (ts Texts) SingleLineHeight() int {
height := 0
for _, t := range ts {
if t.H > height {
height = t.H
}
}
return height
}
func (ts Texts) MultiLineWidth() int {
width := 0
for _, t := range ts {
if t.Advance > width {
width = t.Advance
}
}
return width
}
func (ts Texts) MultiLineHeight(addSpace int) int {
height := 0
for _, t := range ts {
height += t.H + addSpace
}
return height
}
func (ts Texts) DrawLines(image *ebiten.Image, addSpace, x, y int, align Align) {
switch align {
case AlignLeft:
for _, t := range ts {
t.Draw(image, x, y, AlignLeft)
y += t.H + addSpace
}
case AlignCenter:
for _, t := range ts {
t.Draw(image, x-t.W/2, y, AlignLeft)
y += t.H + addSpace
}
case AlignRight:
for _, t := range ts {
t.Draw(image, x-t.W, y, AlignLeft)
y += t.H + addSpace
}
}
}
// MeasureText measures the size of a string with a given font face.
// This is probably best computed the fewest amount of times
// necessary, use MeasuredText for convenience.
func MeasureText(text string, face font.Face) (width, height, advance int) {
bounds, a := font.BoundString(face, text)
width = (bounds.Max.X - bounds.Min.X).Ceil()
height = (bounds.Max.Y - bounds.Min.Y).Ceil()
advance = a.Ceil()
return
}