/
otf.go
executable file
·382 lines (340 loc) · 9.52 KB
/
otf.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
378
379
380
381
382
package pdfjet
/**
* otf.go
*
Copyright 2023 Innovatics Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"bytes"
"compress/zlib"
"fmt"
"io"
"log"
"strings"
"unicode/utf16"
"github.com/edragoev1/pdfjet/src/contents"
)
// FontTable is used to construct font table objects.
type FontTable struct {
name string
checkSum uint32
offset int
length int
}
// OTF is used to construct TTF and OTF font objects.
type OTF struct {
fontName string
fontInfo string
buf []byte
index int
compressed bytes.Buffer
unitsPerEm int
bBoxLLx int16
bBoxLLy int16
bBoxURx int16
bBoxURy int16
ascent int16
descent int16
advanceWidth []uint16
glyphWidth []uint16
firstChar rune
lastChar rune
capHeight int16
postVersion uint32
italicAngle uint32
underlinePosition int16
underlineThickness int16
cff bool
cffOff int
cffLen int
unicodeToGID []int
format int
count int
stringOffset int
}
// NewOTF is the constructor for TTF and OTF fonts.
func NewOTF(reader io.Reader) *OTF {
otf := new(OTF)
otf.buf = contents.GetFromReader(reader)
otf.unicodeToGID = make([]int, 0x10000)
// Extract the OTF metadata
version := readUint32(otf)
if version == 0x00010000 || // Win OTF
version == 0x74727565 || // Mac TTF
version == 0x4F54544F { // CFF OTF
// We should be able to read this font.
} else {
fmt.Println("OTF version == " + fmt.Sprint(version) + " is not supported.")
}
numOfTables := int(readUint16(otf))
readUint16(otf) // Skip the search range.
readUint16(otf) // Skip the entry selector.
readUint16(otf) // Skip the range shift.
var cmapTable *FontTable
for i := 0; i < numOfTables; i++ {
table := new(FontTable)
table.name = string(readNBytes(otf, 4))
table.checkSum = readUint32(otf)
table.offset = int(readUint32(otf))
table.length = int(readUint32(otf))
k := otf.index // Save the current index
if table.name == "head" {
getHeadTable(otf, table)
} else if table.name == "hhea" {
getHheaTable(otf, table)
} else if table.name == "OS/2" {
getOs2Table(otf, table)
} else if table.name == "name" {
getNameTable(otf, table)
} else if table.name == "hmtx" {
getHmtxTable(otf, table)
} else if table.name == "post" {
getPostTable(otf, table)
} else if table.name == "CFF " {
getCffTable(otf, table)
} else if table.name == "cmap" {
cmapTable = table
}
otf.index = k // Restore the index
}
// This table must be processed last
getCmapTable(otf, cmapTable)
writer := zlib.NewWriter(&otf.compressed)
if otf.cff {
_, err := writer.Write(otf.buf[otf.cffOff : otf.cffOff+otf.cffLen])
if err != nil {
log.Fatal(err)
}
} else {
_, err := writer.Write(otf.buf)
if err != nil {
log.Fatal(err)
}
}
writer.Close()
return otf
}
func getHeadTable(otf *OTF, table *FontTable) {
otf.index = table.offset + 16
_ = readUint16(otf) // Skip the flags
otf.unitsPerEm = int(readUint16(otf))
otf.index += 16
otf.bBoxLLx = readInt16(otf)
otf.bBoxLLy = readInt16(otf)
otf.bBoxURx = readInt16(otf)
otf.bBoxURy = readInt16(otf)
}
func getHheaTable(otf *OTF, table *FontTable) {
otf.index = table.offset + 4
otf.ascent = readInt16(otf)
otf.descent = readInt16(otf)
otf.index += 26
otf.advanceWidth = make([]uint16, readUint16(otf))
}
func getOs2Table(otf *OTF, table *FontTable) {
otf.index = table.offset + 64
otf.firstChar = rune(readUint16(otf))
otf.lastChar = rune(readUint16(otf))
otf.index += 20
otf.capHeight = int16(readUint16(otf))
}
func getNameTable(otf *OTF, table *FontTable) {
otf.index = table.offset
otf.format = int(readUint16(otf))
otf.count = int(readUint16(otf))
otf.stringOffset = int(readUint16(otf))
var macFontInfo strings.Builder
var winFontInfo strings.Builder
for r := 0; r < otf.count; r++ {
platformID := readUint16(otf)
encodingID := readUint16(otf)
languageID := readUint16(otf)
nameID := readUint16(otf)
length := int(readUint16(otf))
offset := int(readUint16(otf))
index1 := table.offset + otf.stringOffset + offset
index2 := index1 + length
buffer := otf.buf[index1:index2]
if platformID == 1 && encodingID == 0 && languageID == 0 {
// Macintosh
if nameID == 6 {
otf.fontName = string(buffer)
} else {
macFontInfo.WriteString(otf.fontName)
macFontInfo.WriteString("\n")
}
} else if platformID == 3 && encodingID == 1 && languageID == 0x409 {
// Windows
w := make([]uint16, len(buffer)/2)
for i := 0; i < len(w); i++ {
w[i] = uint16(buffer[2*i])<<8 | uint16(buffer[2*i+1])
}
str := string(utf16.Decode(w))
if nameID == 6 {
otf.fontName = str
} else {
winFontInfo.WriteString(str)
winFontInfo.WriteString("\n")
}
}
}
otf.fontInfo = winFontInfo.String()
if otf.fontInfo == "" {
otf.fontInfo = macFontInfo.String()
}
}
func getCmapTable(otf *OTF, table *FontTable) {
otf.index = table.offset
tableOffset := otf.index
otf.index += 2
numRecords := int(readUint16(otf))
// Process the encoding records
format4subtable := false
subtableOffset := 0
for i := 0; i < numRecords; i++ {
platformID := readUint16(otf)
encodingID := readUint16(otf)
subtableOffset = int(readUint32(otf))
if platformID == 3 && encodingID == 1 {
format4subtable = true
break
}
}
if !format4subtable {
log.Fatal("Format 4 subtable not found in this font.")
}
otf.index = tableOffset + subtableOffset
readUint16(otf) // Skip the format
tableLen := readUint16(otf)
readUint16(otf) // Skip the language
segCount := int(readUint16(otf) / 2)
otf.index += 6 // Skip to the endCount[]
endCount := make([]uint16, segCount)
for i := 0; i < segCount; i++ {
endCount[i] = readUint16(otf)
}
otf.index += 2 // Skip the reservedPad
startCount := make([]uint16, segCount)
for i := 0; i < segCount; i++ {
startCount[i] = readUint16(otf)
}
idDelta := make([]uint16, segCount)
for i := 0; i < segCount; i++ {
idDelta[i] = readUint16(otf)
}
idRangeOffset := make([]uint16, segCount)
for i := 0; i < segCount; i++ {
idRangeOffset[i] = readUint16(otf)
}
glyphIDArray := make([]uint16, (int(tableLen)-(16+8*segCount))/2)
for i := 0; i < len(glyphIDArray); i++ {
glyphIDArray[i] = readUint16(otf)
}
otf.glyphWidth = make([]uint16, otf.lastChar+1)
for i := 0; i < len(otf.glyphWidth); i++ {
otf.glyphWidth[i] = otf.advanceWidth[0]
}
for ch := otf.firstChar; ch <= otf.lastChar; ch++ {
seg := getSegmentFor(ch, startCount, endCount, segCount)
if seg != -1 {
gid := 0
offset := int(idRangeOffset[seg])
if offset == 0 {
gid = (int(idDelta[seg]) + int(ch)) % 65536
} else {
offset /= 2
offset -= segCount - seg
gid = int(glyphIDArray[offset+(int(ch)-int(startCount[seg]))])
if gid != 0 {
gid += int(idDelta[seg]) % 65536
}
}
if gid < len(otf.advanceWidth) {
otf.glyphWidth[ch] = otf.advanceWidth[gid]
}
otf.unicodeToGID[ch] = gid
}
}
}
func getHmtxTable(otf *OTF, table *FontTable) {
otf.index = table.offset
for i := 0; i < len(otf.advanceWidth); i++ {
otf.advanceWidth[i] = readUint16(otf)
otf.index += 2
}
}
func getPostTable(otf *OTF, table *FontTable) {
otf.index = table.offset
otf.postVersion = readUint32(otf)
otf.italicAngle = readUint32(otf)
otf.underlinePosition = readInt16(otf)
otf.underlineThickness = readInt16(otf)
}
func getCffTable(otf *OTF, table *FontTable) {
otf.cff = true
otf.cffOff = table.offset
otf.cffLen = table.length
}
func getSegmentFor(ch rune, startCount, endCount []uint16, segCount int) int {
segment := -1
for i := 0; i < segCount; i++ {
if uint16(ch) <= endCount[i] && uint16(ch) >= startCount[i] {
segment = i
break
}
}
return segment
}
func readInt16(otf *OTF) int16 {
value := int16(otf.buf[otf.index]) << 8
otf.index++
value |= int16(otf.buf[otf.index])
otf.index++
return value
}
func readUint8(otf *OTF) uint8 {
value := otf.buf[otf.index]
otf.index++
return value
}
func readUint16(otf *OTF) uint16 {
value := uint16(otf.buf[otf.index]) << 8
otf.index++
value |= uint16(otf.buf[otf.index])
otf.index++
return value
}
func readUint32(otf *OTF) uint32 {
value := uint32(otf.buf[otf.index]) << 24
otf.index++
value |= uint32(otf.buf[otf.index]) << 16
otf.index++
value |= uint32(otf.buf[otf.index]) << 8
otf.index++
value |= uint32(otf.buf[otf.index])
otf.index++
return value
}
func readNBytes(otf *OTF, n int) []byte {
bytes := make([]byte, 0)
for i := 0; i < n; i++ {
bytes = append(bytes, otf.buf[otf.index])
otf.index++
}
return bytes
}