-
Notifications
You must be signed in to change notification settings - Fork 43
/
fnt.go
68 lines (55 loc) · 1.23 KB
/
fnt.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 main
import (
"image"
"regexp"
"strconv"
"strings"
)
func parseIntMap(str string) map[string]int {
ret := map[string]int{}
arr := strings.Split(str, " ")
for _, kv := range arr {
if kv == "" {
continue
}
kvarr := strings.Split(kv, "=")
i, err := strconv.Atoi(kvarr[1])
if err != nil {
panic(err)
}
ret[kvarr[0]] = i
}
return ret
}
func dumpFnt(c *DumpContext) error {
content := strings.ReplaceAll(string(c.FileContent), "\r\n", "\n")
lines := strings.Split(content, "\n")
var part *AtlasPart
for _, line := range lines {
if line == "" {
continue
}
n := strings.Index(line, " ")
switch line[:n] {
case "page":
regPage := regexp.MustCompile(`page\s+id=0\s+file="?([^"]+)"?`)
match := regPage.FindStringSubmatch(line)
part = c.AppendPart()
part.ImageFile = match[1]
case "char":
m := parseIntMap(line[n+1:])
k := string(rune(m["id"]))
switch k {
case ":", "/", "\\", " ":
k = strconv.Itoa(m["id"])
}
imgname := k + ".png"
part.Frames[imgname] = &Frame{
Rect: image.Rect(m["x"], m["y"], m["x"]+m["width"], m["y"]+m["height"]),
OriginalSize: image.Point{m["width"], m["height"]},
Offset: image.Point{0, 0},
}
}
}
return nil
}