-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttf.go
377 lines (299 loc) · 6.86 KB
/
ttf.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package ttf
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
)
type tableType uint32
var (
INVALID = tableType(0)
HEAD = tableTypeString("head")
CMAP = tableTypeString("cmap")
GLYF = tableTypeString("glyf")
HHEA = tableTypeString("hhea")
HMTX = tableTypeString("hmtx")
LOCA = tableTypeString("loca")
MAXP = tableTypeString("maxp")
NAME = tableTypeString("name")
POST = tableTypeString("post")
)
func tableTypeString(s string) (ret tableType) {
if len(s) != 4 {
return
}
buf := bytes.NewBufferString(s)
binary.Read(buf, binary.BigEndian, &ret)
return
}
func (t tableType) String() string {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(t))
return "tableType(" + buf.String() + ")"
}
type tableData struct {
Tag, Checksum, Offset, N uint32
}
type readerAt interface {
io.Reader
io.ReaderAt
}
type TTF struct {
file readerAt
tables map[tableType]tableData
mapper mapper
}
const (
headerSize = 4 + 4*2
tableEntrySize = 4 * 4
)
func (ttf *TTF) readTableEntry(i int) error {
var data tableData
offset := headerSize + int64(i)*tableEntrySize
tableEntry := io.NewSectionReader(ttf.file, offset, tableEntrySize)
err := binary.Read(tableEntry, binary.BigEndian, &data)
if err != nil {
return err
}
ttf.tables[tableType(data.Tag)] = data
return nil
}
type header struct {
Filetype uint32
TablesNum uint16
// 3 uint16 ignored
}
func (ttf *TTF) readTableDir() (err error) {
var data header
header := io.NewSectionReader(ttf.file, 0, headerSize)
err = binary.Read(header, binary.BigEndian, &data)
if err != nil {
return
}
if data.Filetype != 0x74727565 && data.Filetype != 0x00010000 {
return errors.New("Invalid magic number")
}
for i := 0; i < int(data.TablesNum); i++ {
ttf.readTableEntry(i)
}
return
}
func Read(file readerAt) (*TTF, error) {
ttf := &TTF{file, make(map[tableType]tableData), nil}
err := ttf.readTableDir()
return ttf, err
}
func (ttf *TTF) TablesNum() int {
return len(ttf.tables)
}
func checksum(r io.Reader) (sum uint32) {
for {
var i uint32
err := binary.Read(r, binary.BigEndian, &i)
if err != nil {
return
}
sum += i
}
panic("Reached unreachable path!?")
return
}
var requiredTables = []tableType{CMAP, GLYF, HEAD, HHEA, HMTX, LOCA, MAXP, NAME, POST}
func (ttf *TTF) fontChecksum() (checksum uint32, err error) {
table, ok := ttf.tables[HEAD]
if !ok {
return 0, errors.New("table 'head' not found")
}
sect := io.NewSectionReader(ttf.file, int64(table.Offset+8), 4)
err = binary.Read(sect, binary.BigEndian, &checksum)
return
}
func (ttf *TTF) tableReader(ttype tableType) (*io.SectionReader, error) {
table, ok := ttf.tables[ttype]
if !ok {
return nil, fmt.Errorf("Table %v not found", ttype)
}
return io.NewSectionReader(ttf.file, int64(table.Offset), int64(table.N)), nil
}
func (ttf *TTF) checkTableChecksum(ttype tableType, table tableData) error {
N := 4 * ((3 + table.N) / 4)
sect := io.NewSectionReader(ttf.file, int64(table.Offset), int64(N))
sum := checksum(sect)
if ttype == HEAD {
fontSum, err := ttf.fontChecksum()
if err != nil {
return err
}
sum -= fontSum
}
if table.Checksum != sum {
return fmt.Errorf("Table '%v' checksum failed.", ttype)
}
return nil
}
func (ttf *TTF) checkFontChecksum() error {
font := io.NewSectionReader(ttf.file, 0, math.MaxInt64)
fsum, err := ttf.fontChecksum()
if err != nil {
return err
}
sum := 0xB1B0AFBA - checksum(font) + fsum
if fsum != sum {
return errors.New("Font checksum failed")
}
return nil
}
func (ttf *TTF) Check() error {
for _, reqType := range requiredTables {
if _, ok := ttf.tables[reqType]; !ok {
return fmt.Errorf("Missing required table %v", reqType)
}
}
for ttype, table := range ttf.tables {
if err := ttf.checkTableChecksum(ttype, table); err != nil {
return err
}
}
err := ttf.checkFontChecksum()
return err
}
type cmapIndex struct {
Version, TablesNum uint16
}
type cmapSubtable struct {
Platform platformType
Specific uint16
Offset uint32
}
type cmapSubtableHeader struct {
Type, Len uint16
}
type cmapSubtable4 struct {
Type, Len, Lang, Count2, X0, X1, X2 uint16
}
type mapper interface {
Map(r rune) (int, error)
}
type mapper4 struct {
Ranges []range4
Direct map[uint16]uint16
}
type range4 struct {
Start, End, Delta uint16
}
func newMapper4(s *io.SectionReader) (map4 *mapper4, err error) {
var submap cmapSubtable4
err = binary.Read(s, binary.BigEndian, &submap)
if err != nil {
return
}
if submap.Type != 4 {
panic("newMapper4 called with submap.Type != 4")
}
count := int(submap.Count2 / 2)
end := make([]uint16, count)
start := make([]uint16, count)
delta := make([]uint16, count)
offset := make([]uint16, count)
err = binary.Read(s, binary.BigEndian, &end)
if err != nil {
return
}
var X uint16
err = binary.Read(s, binary.BigEndian, &X)
if err != nil {
return
}
err = binary.Read(s, binary.BigEndian, &start)
if err != nil {
return
}
err = binary.Read(s, binary.BigEndian, &delta)
if err != nil {
return
}
err = binary.Read(s, binary.BigEndian, &offset)
if err != nil {
return
}
ranges := make([]range4, 0)
direct := make(map[uint16]uint16)
for i := 0; i < count; i++ {
if offset[i] == 0 {
ranges = append(ranges, range4{start[i], end[i], delta[i]})
} else {
//Error: range offset not implemented
panic("Range offset mapping not implemented")
}
}
return &mapper4{ranges, direct}, nil
}
func (m *mapper4) Map(r rune) (int, error) {
if r > 0xFFFF {
return 0, errors.New("Unicode rune out of range")
}
r16 := uint16(r)
g, ok := m.Direct[r16]
if ok {
return int(g), nil
}
for _, rng := range m.Ranges {
if rng.End < r16 {
continue
}
if rng.Start > r16 {
return 0, nil
}
return int(rng.Delta + r16), nil
}
return 0, nil
}
func (ttf *TTF) initMap(platform platformType) error {
cmap, err := ttf.tableReader(CMAP)
if err != nil {
return err
}
var index cmapIndex
err = binary.Read(cmap, binary.BigEndian, &index)
if err != nil {
return err
}
var subtable cmapSubtable
for i := 0; i < int(index.TablesNum); i++ {
err = binary.Read(cmap, binary.BigEndian, &subtable)
if err != nil {
return err
}
if subtable.Platform == platform {
break
}
}
submap := io.NewSectionReader(cmap, int64(subtable.Offset), 4)
var header cmapSubtableHeader
err = binary.Read(submap, binary.BigEndian, &header)
if err != nil {
return err
}
submap = io.NewSectionReader(cmap, int64(subtable.Offset), int64(header.Len))
switch header.Type {
case 4:
ttf.mapper, err = newMapper4(submap)
default:
return fmt.Errorf("Unsupported cmap subtable type %v", header.Type)
}
return err
}
type platformType uint16
var (
UNICODE platformType = 0
MAC = 1
WINDOWS = 4
)
func (ttf *TTF) MapGlyph(r rune) (int, error) {
if ttf.mapper == nil {
ttf.initMap(UNICODE)
}
return ttf.mapper.Map(r)
}