forked from russross/warddirectory
/
font.go
258 lines (230 loc) · 6.78 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// Font metric files
// Code to parse and represent font metrics
//
package main
import (
"bytes"
"compress/zlib"
"errors"
"fmt"
"log"
"strings"
)
// metrics for a single glyph from a font
type GlyphMetrics struct {
Code rune
Width int
Name string
BBoxLeft int
BBoxBottom int
BBoxRight int
BBoxTop int
Ligatures map[string]string
Kerning map[string]int
}
// metrics for an entire font
type FontMetrics struct {
Name string
Label string
Glyphs map[string]*GlyphMetrics
File []byte
CompressedFile []byte
CapHeight int
FirstChar rune
LastChar rune
Flags int
BBoxLeft int
BBoxBottom int
BBoxRight int
BBoxTop int
ItalicAngle int
Ascent int
Descent int
StemV int
NameToCode map[string]string
CodePointToName map[rune]string
}
// a single chunk of text made up of glyphs
type Box struct {
Font *FontMetrics
Original string
Width float64
Command string
JoinNext bool
Penalty int
}
type fontdata struct {
Metrics string
FontFile string
Label string
StemV int
}
var FontList map[string]*FontMetrics
var roman, bold, courier, lmtt, lmvtt *FontMetrics
var unicodeToGlyph map[rune]string
func init() {
loadDataFiles()
var FontSourceList = map[string]*fontdata{
"times-roman": {string(dataFiles["Times-Roman.afm"]), "", "FR", -1},
"times-bold": {string(dataFiles["Times-Bold.afm"]), "", "FB", -1},
"courier": {string(dataFiles["Courier.afm"]), "", "FT", -1},
"lmtt": {string(dataFiles["lmtt10.afm"]), string(dataFiles["lmtt10.pfb"]), "FT", 69},
"lmvtt": {string(dataFiles["lmvtt10.afm"]), string(dataFiles["lmvtt10.pfb"]), "FT", 69},
}
var err error
// first load the fonts
FontList = make(map[string]*FontMetrics)
for name, font := range FontSourceList {
FontList[name] = loadFont(font)
}
// get the complete list of glyphs we know about
if unicodeToGlyph, err = GlyphMapping(FontList, string(dataFiles["glyphlist.txt"])); err != nil {
log.Fatal("loading glyph metrics: ", err)
}
}
func loadFont(f *fontdata) (font *FontMetrics) {
var err error
if font, err = ParseFontMetricsFile(f.Metrics, f.Label); err != nil {
log.Fatalf("loading font metrics: %v", err)
}
if f.StemV > 0 && font.StemV <= 0 {
font.StemV = f.StemV
}
if len(f.FontFile) > 0 {
font.File = []byte(f.FontFile)
var buf bytes.Buffer
var writer *zlib.Writer
if writer, err = zlib.NewWriterLevel(&buf, zlib.BestCompression); err != nil {
log.Fatal("Setting up zlib compressor: ", err)
}
if _, err = writer.Write(font.File); err != nil {
log.Fatal("Writing to zlib compressor: ", err)
}
if err = writer.Close(); err != nil {
log.Fatal("Closing zlib compressor: ", err)
}
font.CompressedFile = buf.Bytes()
}
return
}
// parse a single glyph metric line from a .afm file
func (font *FontMetrics) ParseGlyph(in string) error {
// sample: C 102 ; WX 333 ; N f ; B 20 0 383 683 ; L i fi ; L l fl ;
glyph := &GlyphMetrics{Ligatures: make(map[string]string), Kerning: make(map[string]int)}
for _, elt := range strings.Split(in, ";") {
elt = strings.TrimSpace(elt)
var a, b, c, d int
var r rune
var u, v string
if n, err := fmt.Sscanf(elt, "C %d", &r); n == 1 && err == nil {
glyph.Code = r
} else if n, err := fmt.Sscanf(elt, "CH %x", &r); n == 1 && err == nil {
glyph.Code = r
} else if n, err := fmt.Sscanf(elt, "WX %d", &a); n == 1 && err == nil {
glyph.Width = a
} else if n, err := fmt.Sscanf(elt, "N %s", &u); n == 1 && err == nil {
glyph.Name = u
} else if n, err := fmt.Sscanf(elt, "B %d %d %d %d", &a, &b, &c, &d); n == 4 && err == nil {
glyph.BBoxLeft, glyph.BBoxBottom, glyph.BBoxRight, glyph.BBoxTop = a, b, c, d
} else if n, err := fmt.Sscanf(elt, "L %s %s", &u, &v); n == 2 && err == nil {
glyph.Ligatures[u] = v
} else if elt == "" {
} else {
return errors.New("Unknown glyph metric field: [" + elt + "] from [" + in + "]")
}
}
if glyph.Name == "" {
return errors.New("No glyph name found in metric line: [" + in + "]")
}
if other, present := font.Glyphs[glyph.Name]; present && 0x20 <= other.Code && other.Code < 0x80 {
// keep the one with an ascii code
} else {
font.Glyphs[glyph.Name] = glyph
}
return nil
}
// parse a single glyph kerning line from a .afm file
func (font *FontMetrics) ParseKerning(in string) error {
// sample: KPX f i -20
var a int
var u, v string
if n, err := fmt.Sscanf(in, "KPX %s %s %d", &u, &v, &a); n == 3 && err == nil {
glyph, present := font.Glyphs[u]
if !present {
return errors.New("Kerning found for unknown glyph: [" + in + "]")
}
glyph.Kerning[v] = a
} else {
return errors.New("Unknown kerning line: [" + in + "]")
}
return nil
}
// parse and entire .afm file
func ParseFontMetricsFile(file string, label string) (font *FontMetrics, err error) {
font = &FontMetrics{
Glyphs: make(map[string]*GlyphMetrics),
Label: label,
Flags: 1<<1 | 1<<5,
}
lines := strings.Split(file, "\n")
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
a, b, c, d, count := 0, 0, 0, 0, 0
n := 0
s := ""
if n, err = fmt.Sscanf(line, "CapHeight %d", &a); n == 1 && err == nil {
font.CapHeight = a
} else if n, err = fmt.Sscanf(line, "FontBBox %d %d %d %d", &a, &b, &c, &d); n == 4 && err == nil {
font.BBoxLeft = a
font.BBoxBottom = b
font.BBoxRight = c
font.BBoxTop = d
} else if n, err = fmt.Sscanf(line, "ItalicAngle %d", &a); n == 1 && err == nil {
font.ItalicAngle = a
} else if n, err = fmt.Sscanf(line, "Ascender %d", &a); n == 1 && err == nil {
font.Ascent = a
} else if n, err = fmt.Sscanf(line, "Descender %d", &a); n == 1 && err == nil {
font.Descent = a
} else if n, err = fmt.Sscanf(line, "StdVW %d", &a); n == 1 && err == nil {
font.StemV = a
} else if n, err = fmt.Sscanf(line, "IsFixedPitch %s", &s); n == 1 && err == nil {
if s == "true" {
font.Flags |= 1
}
} else if n, err = fmt.Sscanf(line, "StartCharMetrics %d", &count); n == 1 && err == nil {
i += 1
for j := 0; j < count && i < len(lines); j, i = j+1, i+1 {
line := strings.TrimSpace(lines[i])
if err = font.ParseGlyph(line); err != nil {
return
}
}
} else if n, err = fmt.Sscanf(line, "StartKernPairs %d", &count); n == 1 && err == nil {
i += 1
for j := 0; j < count && i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if line == "" {
continue
}
if err = font.ParseKerning(line); err != nil {
return
}
j++
}
} else if n, err = fmt.Sscanf(line, "FontName %s", &s); n == 1 && err == nil {
font.Name = s
}
err = nil
}
return
}
func (font *FontMetrics) Copy() *FontMetrics {
elt := new(FontMetrics)
*elt = *font
elt.NameToCode = make(map[string]string)
elt.CodePointToName = make(map[rune]string)
elt.FirstChar = 0
elt.LastChar = 0
return elt
}