-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate.go
More file actions
199 lines (185 loc) · 4.48 KB
/
generate.go
File metadata and controls
199 lines (185 loc) · 4.48 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// decoder/generator for hershey fonts
// reads hershey.dat and creates hershey.go amd hersheyheights.go
// uses fontids.go for font generation
package hershey
import (
"fmt"
"io/ioutil"
"sort"
"strconv"
"strings"
)
func GenerateHershey() (out string, idToIndex map[int]int, err error) {
idToIndex = make(map[int]int)
name := "hershey"
data, err := ioutil.ReadFile(name + ".dat")
if err != nil {
return
}
fmt.Println("Decoding: ", name)
s := strings.ReplaceAll(string(data), "\n", "")
out = "//Coordinates: \n\nvar " + name + " = []FontPath{\n"
i := 0
for {
if len(s) < 5 {
break
}
// hershey character id
id := s[0:5]
nid, err := strconv.Atoi(strings.Trim(id, " "))
if err != nil {
return "", map[int]int{}, err
}
idToIndex[nid] = i
//number of coordinate pairs
n, err := strconv.Atoi(strings.Trim(s[5:8], " "))
if err != nil {
return "", map[int]int{}, err
}
s = s[8:]
//number of characters
cn := 2 * n
ss := s[:cn]
//left position
lt := int(ss[0]) - int('R')
//right position
rt := int(ss[1]) - int('R')
out += fmt.Sprintf("// %d: %s\n", i, id)
out += fmt.Sprintf("{%d, %d, [][][]int{", lt, rt)
i++
//paths & coordinates
if n > 1 {
out += "{"
}
for i := 2; i < len(ss); i++ {
if ss[i] == ' ' { //new path
out += fmt.Sprintf("},{")
i += 2
}
//coords
dx := int(ss[i]) - int('R')
dy := int(ss[i+1]) - int('R')
i++
out += fmt.Sprintf("{%d, %d},", dx, dy)
}
out = strings.TrimRight(out, ",")
if n > 1 {
out += "}"
}
out += fmt.Sprintf("}},\n")
s = s[cn:]
}
out = strings.TrimRight(out, ",\n")
out += "}\n\n"
return
}
func GenerateFonts(idToIndex map[int]int) (out string) {
out = "//Fonts: \n\nvar Fonts = map[string][]int{"
for name, _ := range Id_Fonts {
out += "\"" + name + "\": " + name + ",\n"
}
out += "}\n\n"
for name, font := range Id_Fonts {
out += "var " + name + " = []int{\n"
for i, v := range font {
out += fmt.Sprintf("%d, ", idToIndex[v])
if (i+1)%10 == 0 {
out += "\n"
}
}
out += "}\n\n"
}
return
}
func GenerateTranslators(idToIndex map[int]int) (out string) {
out1 := "//Id translation\n\nvar IdToIndex = map[int]int {\n"
out2 := "\n\nvar IndexToId = map[int]int {\n"
i := 1
for k, v := range IdToIndex {
out1 += fmt.Sprintf("%d: %d,", k, v)
out2 += fmt.Sprintf("%d: %d,", v, k)
if i%10 == 0 {
out1 += "\n"
out2 += "\n"
}
i++
}
out1 += "}\n\n"
out2 += "}\n\n"
out = out1 + out2
return
}
func Generate() (err error) {
packageName := "hershey"
out0 := "// generated file, do not edit - see generate.go\n"
out0 += "package " + packageName + "\n\n" + "type FontPath struct {\n Lt int\n Rt int\n Coords [][][]int\n}\n\n"
out1, idToIndex, err := GenerateHershey()
if err != nil {
return
}
out2 := GenerateFonts(idToIndex)
out3 := GenerateTranslators(idToIndex)
err = ioutil.WriteFile(packageName+".go", []byte(out0+out1+out2+out3), 0644)
// SymbolTables(IdToIndex)
return
}
//helper function to print symbol tables for any unused chars, (already added to fontids.go)
func SymbolTables(IdToIndex map[int]int) {
var symbols []int
for id, _ := range IdToIndex {
found := false
for _, font := range Id_Fonts {
for _, fid := range font {
if fid == id {
found = true
break
}
}
}
if !found {
symbols = append(symbols, id)
// fmt.Println("Unique Id: ", id)
}
}
sort.Slice(symbols, func(i, j int) bool {
return symbols[i] < symbols[j]
})
fmt.Println("Unused Symbols ", len(symbols))
i := 0
for _, v := range symbols {
fmt.Printf("%d,", v)
if (i+1)%10 == 0 {
println()
}
if (i+1)%96 == 0 {
println("\n===================")
i = -1
}
i++
}
}
//generate min/max heights for every font into hersheyheights.go, used by DrawStringLines()
func GenerateHeights() (err error) {
name := "hersheyheights"
packageTxt := "hershey"
fmt.Println("Creating: ", name)
out1 := "// generated file, do not edit - see generate.go\n"
out1 += "package " + packageTxt + "\n\n"
out1 += "//Height: \n\nvar Height = map[string][]int{"
for fontname, f := range Fonts {
fmt.Println(" Processing Font: ", fontname)
var minX, minY, maxX, maxY int
haveFirst := false
for i, _ := range f {
var x, y int
err = DrawChar(rune(i+32), fontname, 1, &x, &y, Bounds, Bounds, &haveFirst, &minX, &minY, &maxX, &maxY)
if err != nil {
return
}
}
out1 += fmt.Sprintf("\"%s\": {%d,%d},\n", fontname, minY, maxY)
}
out1 += "}\n\n"
err = ioutil.WriteFile(name+".go", []byte(out1), 0644)
return
}