-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
68 lines (60 loc) · 1.26 KB
/
font.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
package font
import (
"image/color"
"strings"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text"
"github.com/hajimehoshi/bitmapfont"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
const LineHeight = 16
func tokens(str string) []string {
tokens := []string{}
for {
var strs []string
switch len(tokens) % 2 {
case 0:
strs = strings.SplitN(str, "<red>", 2)
case 1:
strs = strings.SplitN(str, "</red>", 2)
}
if len(strs) >= 1 {
tokens = append(tokens, strs[0])
}
if len(strs) == 2 {
str = strs[1]
} else {
break
}
}
return tokens
}
func Width(str string) int {
w := fixed.I(0)
for _, t := range tokens(str) {
w += font.MeasureString(bitmapfont.Gothic12r, t)
}
return w.Round()
}
func Height(str string) int {
return (strings.Count(str, "\n") + 1) * LineHeight
}
var red = color.RGBA{0xe4, 0x32, 0x60, 0xff}
func DrawText(target *ebiten.Image, str string, x, y int, clr color.Color) {
// Adjust position for 'dot' position.
const (
dx = 3
dy = 12
)
fx := fixed.I(x)
fy := fixed.I(y)
for i, t := range tokens(str) {
clr := clr
if i%2 == 1 {
clr = red
}
text.Draw(target, t, bitmapfont.Gothic12r, fx.Round()+dx, fy.Round()+dy, clr)
fx += font.MeasureString(bitmapfont.Gothic12r, t)
}
}