forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttfparser_cmap_other_format.go
127 lines (106 loc) · 2.3 KB
/
ttfparser_cmap_other_format.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
package core
import (
"bytes"
"errors"
)
//ParseCmapFormat12 parse cmap table format 12 https://www.microsoft.com/typography/otspec/cmap.htm
func (t *TTFParser) ParseCmapFormat12(fd *bytes.Reader) (bool, error) {
t.Seek(fd, "cmap")
t.Skip(fd, 2) //skip version
numTables, err := t.ReadUShort(fd)
if err != nil {
return false, err
}
var cEncodingSubtables []cmapFormat12EncodingSubtable
for i := 0; i < int(numTables); i++ {
platformID, err := t.ReadUShort(fd)
if err != nil {
return false, err
}
encodingID, err := t.ReadUShort(fd)
if err != nil {
return false, err
}
offset, err := t.ReadULong(fd)
if err != nil {
return false, err
}
var ce cmapFormat12EncodingSubtable
ce.platformID = platformID
ce.encodingID = encodingID
ce.offset = offset
cEncodingSubtables = append(cEncodingSubtables, ce)
}
isFound := false
offset := uint(0)
for _, ce := range cEncodingSubtables {
if ce.platformID == 3 && ce.encodingID == 10 {
offset = ce.offset
isFound = true
break
}
}
if !isFound {
return false, nil
}
_, err = fd.Seek(int64(t.tables["cmap"].Offset+offset), 0)
if err != nil {
return false, err
}
format, err := t.ReadUShort(fd)
if err != nil {
return false, err
}
if format != 12 {
return false, errors.New("format != 12")
}
reserved, err := t.ReadUShort(fd)
if err != nil {
return false, err
}
if reserved != 0 {
return false, errors.New("reserved != 0")
}
err = t.Skip(fd, 4) //skip length
if err != nil {
return false, err
}
err = t.Skip(fd, 4) //skip language
if err != nil {
return false, err
}
nGroups, err := t.ReadULong(fd)
if err != nil {
return false, err
}
g := uint(0)
for g < nGroups {
startCharCode, err := t.ReadULong(fd)
if err != nil {
return false, err
}
endCharCode, err := t.ReadULong(fd)
if err != nil {
return false, err
}
glyphID, err := t.ReadULong(fd)
if err != nil {
return false, err
}
var gTb CmapFormat12GroupingTable
gTb.StartCharCode = startCharCode
gTb.EndCharCode = endCharCode
gTb.GlyphID = glyphID
t.groupingTables = append(t.groupingTables, gTb)
g++
}
return true, nil
}
type cmapFormat12EncodingSubtable struct {
platformID uint
encodingID uint
offset uint
}
type CmapFormat12GroupingTable struct {
StartCharCode, EndCharCode, GlyphID uint
}