-
Notifications
You must be signed in to change notification settings - Fork 5
/
table_name.go
334 lines (294 loc) · 8.43 KB
/
table_name.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package unitype
import (
"bytes"
"strconv"
"unicode"
"unicode/utf8"
"github.com/sirupsen/logrus"
"golang.org/x/text/encoding/charmap"
"github.com/unidoc/unitype/internal/strutils"
)
// nameTable represents the Naming table (name).
// The naming table allows multilingual strings to be associated with the font.
// These strings can represent copyright notices, font names, family names, style names, and so on.
type nameTable struct {
// format >= 0
format uint16
count uint16
stringOffset offset16
nameRecords []*nameRecord // len = count.
// format = 1 adds
langTagCount uint16
langTagRecords []*langTagRecord // len = langTagCount
}
type langTagRecord struct {
length uint16
offset offset16
data []byte // actual string data (UTF-16BE format).
}
// Each string in the string storage is referenced by a name record.
type nameRecord struct {
platformID uint16
encodingID uint16
languageID uint16
nameID uint16
length uint16
offset offset16
data []byte // actual string data.
}
// GetNameByID returns the first entry according to the name table with `nameID`.
// An empty string is returned otherwise (nothing found).
func (f *font) GetNameByID(nameID int) string {
if f == nil || f.name == nil {
logrus.Debug("ERROR: Font or name not set")
return ""
}
for _, nr := range f.name.nameRecords {
if int(nr.nameID) == nameID {
return nr.Decoded()
}
}
return ""
}
// GetNameRecords returns name records as map of language ID
// that contais name ID and it's value.
func (f *font) GetNameRecords() map[uint16]map[uint16]string {
var nameRecords = make(map[uint16]map[uint16]string, 0)
for _, nr := range f.name.nameRecords {
nameRec, ok := nameRecords[nr.languageID]
if ok {
nameRec[nr.nameID] = nr.Decoded()
} else {
nameRec = map[uint16]string{nr.nameID: nr.Decoded()}
}
nameRecords[nr.languageID] = nameRec
}
return nameRecords
}
// numPrintables returns the number of printable runes in `str`
func numPrintables(str string) int {
printables := 0
for _, r := range str {
if unicode.IsPrint(r) || r == '\n' {
printables++
}
}
return printables
}
// makePrintable replaces unprintable runes with quotes runes, returning printable string.
func makePrintable(str string) string {
var buf bytes.Buffer
for _, r := range str {
if unicode.IsPrint(r) || r == '\n' {
buf.WriteRune(r)
} else {
buf.WriteString(strconv.QuoteRune(r))
}
}
return buf.String()
}
// Decoded attempts to decode the underlying data and convert to a string.
// NOTE: Works in many cases but often has some -garbage- around texts.
func (nr nameRecord) Decoded() string {
switch nr.platformID {
case 0: // unicode
// TODO(gunnsth): Untested as have not encountered this yet.
dup := make([]byte, len(nr.data))
copy(dup, nr.data)
var decoded bytes.Buffer
for len(dup) > 0 {
r, size := utf8.DecodeRune(dup)
dup = dup[size:]
decoded.WriteRune(r)
}
return makePrintable(decoded.String())
case 1: // macintosh
var decoded bytes.Buffer
for _, val := range nr.data {
decoded.WriteRune(charmap.Macintosh.DecodeByte(val))
}
macs := decoded.String()
// Following may be needed in rare cases:
/*
utf16s := strutils.UTF16ToString([]byte(macs))
if numPrintables(utf16s) > numPrintables(macs) {
return makePrintable(utf16s)
}
*/
return makePrintable(macs)
case 3: // windows
// When building a Unicode font for Windows, the platform ID should be 3 and the encoding ID should be 1,
// and the referenced string data must be encoded in UTF-16BE. When building a symbol font for Windows,
// the platform ID should be 3 and the encoding ID should be 0, and the referenced string data must be
// encoded in UTF-16BE. (https://docs.microsoft.com/en-us/typography/opentype/spec/name).
if nr.encodingID == 0 || nr.encodingID == 1 {
if len(nr.data) > 0 {
decoded := strutils.UTF16ToString(nr.data)
return makePrintable(decoded)
}
}
}
return makePrintable(string(nr.data))
}
func (f *font) parseNameTable(r *byteReader) (*nameTable, error) {
tr, has, err := f.seekToTable(r, "name")
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
logrus.Debugf("TR: %+v", tr)
t := &nameTable{}
err = r.read(&t.format, &t.count, &t.stringOffset)
if err != nil {
return nil, err
}
logrus.Debugf("format/count/stringOffset: %v/%v/%v", t.format, t.count, t.stringOffset)
logrus.Debugf("-- name string offset: %d", t.stringOffset)
if t.format > 1 {
logrus.Debugf("ERROR: format > 1 (%d)", t.format)
return nil, errRangeCheck
}
for i := 0; i < int(t.count); i++ {
var nr nameRecord
err = r.read(&nr.platformID, &nr.encodingID, &nr.languageID, &nr.nameID, &nr.length, &nr.offset)
if err != nil {
return nil, err
}
logrus.Debugf("name record %d: %v/%v/%v/%v/%v/%v", i, nr.platformID, nr.encodingID, nr.languageID, nr.nameID,
nr.length, nr.offset)
t.nameRecords = append(t.nameRecords, &nr)
}
if t.format == 1 {
err = r.read(&t.langTagCount)
if err != nil {
return nil, err
}
for i := 0; i < int(t.langTagCount); i++ {
var ltr langTagRecord
err = r.read(<r.length, <r.offset)
if err != nil {
return nil, err
}
logrus.Debugf("ltr name record %d: %v/%v", i, ltr.offset, ltr.length)
t.langTagRecords = append(t.langTagRecords, <r)
}
}
// Get the actual string data.
for _, nr := range t.nameRecords {
if int(t.stringOffset)+int(nr.offset)+int(nr.length) > int(tr.length) {
logrus.Debugf("%v> %v", int(t.stringOffset)+int(nr.offset)+int(nr.length), int(tr.length))
logrus.Debug("name string offset outside table")
return nil, errRangeCheck
}
err = r.SeekTo(int64(t.stringOffset) + int64(tr.offset) + int64(nr.offset))
if err != nil {
logrus.Debugf("Error: %v", err)
return nil, err
}
err = r.readBytes(&nr.data, int(nr.length))
if err != nil {
logrus.Debugf("Error: %v", err)
return nil, err
}
}
for _, ltr := range t.langTagRecords {
if int(t.stringOffset)+int(ltr.offset)+int(ltr.length) > int(tr.length) {
logrus.Debug("lang tag string offset outside table")
return nil, errRangeCheck
}
err = r.SeekTo(int64(t.stringOffset) + int64(tr.offset) + int64(ltr.offset))
if err != nil {
logrus.Debugf("Error: %v", err)
return nil, err
}
err = r.readBytes(<r.data, int(ltr.length))
if err != nil {
logrus.Debugf("Error: %v", err)
return nil, err
}
}
logrus.Debugf("Name records: %d", len(t.nameRecords))
for _, nr := range t.nameRecords {
logrus.Debugf("%d %d %d - '%s' (%d)", nr.platformID, nr.encodingID, nr.nameID, nr.Decoded(), len(nr.data))
}
return t, nil
}
func (f *font) writeNameTable(w *byteWriter) error {
if f.name == nil {
logrus.Debug("name is nil")
return nil
}
t := f.name
// Preprocess: Write to buffer and update offsets.
var buf bytes.Buffer
{
bufw := newByteWriter(&buf)
for _, nr := range t.nameRecords {
nr.offset = offset16(bufw.bufferedLen())
nr.length = uint16(len(nr.data))
err := bufw.writeSlice(nr.data)
if err != nil {
return err
}
}
for _, ltr := range t.langTagRecords {
ltr.offset = offset16(bufw.bufferedLen())
ltr.length = uint16(len(ltr.data))
err := bufw.writeSlice(ltr.data)
if err != nil {
return err
}
}
err := bufw.flush()
if err != nil {
return err
}
}
logrus.Debugf("Buffer length: %d", buf.Len())
// Update count and stringOffsets (calculated).
t.count = uint16(len(t.nameRecords))
t.langTagCount = uint16(len(t.langTagRecords))
// 2+2+2+count*(6*2) + (format=1) 2+langTagCount*2
t.stringOffset = 6 + offset16(t.count)*12
if t.format == 1 {
t.stringOffset += 2 + offset16(t.langTagCount)*4
}
logrus.Debugf("w @ %d", w.bufferedLen())
err := w.write(t.format, t.count, t.stringOffset)
if err != nil {
return err
}
for _, nr := range t.nameRecords {
err = w.write(nr.platformID, nr.encodingID, nr.languageID, nr.nameID, nr.length, nr.offset)
if err != nil {
return err
}
}
logrus.Debugf("w @ %d", w.bufferedLen())
if t.format == 1 {
err = w.write(t.langTagCount)
if err != nil {
return err
}
for _, ltr := range t.langTagRecords {
err = w.write(ltr.length, ltr.offset)
if err != nil {
return err
}
}
}
logrus.Debugf("w @ %d", w.bufferedLen())
// Write the buffered data.
err = w.writeBytes(buf.Bytes())
if err != nil {
return err
}
logrus.Debugf("w @ %d", w.bufferedLen())
return nil
}