/
structure.go
227 lines (202 loc) · 5.3 KB
/
structure.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
package opentype
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
"time"
)
// Fixed is a 32-bit signed fixed-point number (16.16)
type Fixed int32
// LongDateTime is a Date represented in number of seconds since 12:00 midnight, January 1, 1904. The value is represented as a signed 64-bit integer.
type LongDateTime int64
func (t LongDateTime) value() time.Time {
return time.Date(1904, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(t) * time.Second)
}
// Tag is a array of four uint8s (length = 32 bits) used to identify a script, language system, feature, or baseline
type Tag uint32
func (t Tag) String() string {
bytes := []byte{
byte(t >> 24 & 0xFF),
byte(t >> 16 & 0xFF),
byte(t >> 8 & 0xFF),
byte(t & 0xFF),
}
return string(bytes)
}
// String2Tag creates Tag from string.
func String2Tag(s string) Tag {
bytes := []byte(s)
return Tag(uint32(bytes[0])<<24 + uint32(bytes[1])<<16 + uint32(bytes[2])<<8 + uint32(bytes[3]))
}
const (
// SfntVersionTrueTypeOpenType : OpenType fonts that contain TrueType outlines.
SfntVersionTrueTypeOpenType = Tag(0x00010000)
// SfntVersionCFFOpenType : OpenType fonts containing CFF data.
SfntVersionCFFOpenType = Tag(0x4F54544F) // OTTO
// SfntVersionAppleTrueType : The Apple specification for TrueType font.
SfntVersionAppleTrueType = Tag(0x74727565) // true
// SfntVersionAppleType1 : The Apple specification for the old style of PostScript font.
SfntVersionAppleType1 = Tag(0x74797031) // typ1
// SfntVersionTTCHeader : The header of TTC format file.
SfntVersionTTCHeader = Tag(0x74746366) // ttcf
)
func parseSfntVersion(f *os.File, offset int64) (t Tag, err error) {
f.Seek(offset, 0)
err = binary.Read(f, binary.BigEndian, &t)
f.Seek(offset, 0)
return
}
// OffsetTable is the first table of OpenType font file.
type OffsetTable struct {
SfntVersion Tag
// Number of tables.
NumTables uint16
// (Maximum power of 2 <= numTables) x 16.
SearchRange uint16
// Log2(maximum power of 2 <= numTables).
EntrySelector uint16
// NumTables x 16-searchRange.
RangeShift uint16
}
func parseOffsetTable(f *os.File) (ot *OffsetTable, err error) {
ot = &OffsetTable{}
r := newErrReader(f)
r.read(ot)
return ot, r.errorf("failed to parse offset table: %s")
}
func createOffsetTable(sfntVersion Tag, numTables uint16) *OffsetTable {
ot := &OffsetTable{
SfntVersion: sfntVersion,
NumTables: numTables,
}
ot.refreshField()
return ot
}
// Length returns the size(byte) of this table.
func (ot *OffsetTable) Length() uint32 {
return uint32(12)
}
func (ot *OffsetTable) refreshField() {
es := uint16(0)
sr := uint16(1)
for {
if 1<<(es+1) > ot.NumTables {
break
}
es = es + 1
sr = sr * 2
}
ot.SearchRange = sr * 16
ot.EntrySelector = es
ot.RangeShift = ot.NumTables*16 - ot.SearchRange
}
// TableRecord is a OpenType table record.
type TableRecord struct {
Tag Tag
CheckSum uint32
// Offset from beginning of TrueType font file.
Offset uint32
Length uint32
}
const (
// TableRecordLength : size of TableRecord
TableRecordLength = uint32(16)
)
func parseTableRecord(f *os.File, numTables uint16) (trs map[string]*TableRecord, err error) {
trs = make(map[string]*TableRecord)
for i := uint16(0); i < numTables; i++ {
tr := &TableRecord{}
err = binary.Read(f, binary.BigEndian, tr)
if err != nil {
err = fmt.Errorf("failed to parse table record: %s", err)
return
}
trs[tr.Tag.String()] = tr
}
for _, tr := range trs {
err = tr.validate(f)
if err != nil {
return
}
}
return
}
func createTableRecord(t Table, offset uint32) (tr *TableRecord, err error) {
checkSum, err := t.CheckSum()
if err != nil {
return
}
tr = &TableRecord{
Tag: t.Tag(),
CheckSum: checkSum,
Offset: offset,
Length: t.Length(),
}
return
}
func (tr *TableRecord) validate(f *os.File) (err error) {
if "head" == tr.Tag.String() {
return
}
_, err = f.Seek((int64)(tr.Offset), 0)
if err != nil {
return
}
checkSum, err := calcCheckSum(f, tr.Length)
if checkSum != tr.CheckSum {
err = fmt.Errorf("Table %s has invalid checksum, expected:%d actual:%d", tr.Tag, tr.CheckSum, checkSum)
}
return
}
// Table is a OpenType table.
type Table interface {
// Tag is table name.
Tag() Tag
// Length returns the size(byte) of this table.
Length() uint32
// CheckSum for this table.
CheckSum() (uint32, error)
// Exists returns true if this is not nil.
Exists() bool
// store writes binary expression of this table.
store(w *errWriter)
}
func simpleCheckSum(t Table) (checkSum uint32, err error) {
b := bytes.NewBuffer([]byte{})
w := newErrWriter(b)
t.store(w)
if w.hasErr() {
err = w.errorf("failed to calculate checksum: %s")
return
}
return calcCheckSum(b, t.Length())
}
func calcCheckSum(r io.Reader, length uint32) (checkSum uint32, err error) {
blocks := make([]uint32, padBlocks(length))
err = binary.Read(r, binary.BigEndian, blocks)
if err != nil {
return
}
for _, block := range blocks {
checkSum += block
}
return
}
func padBlocks(length uint32) uint32 {
q := length / 4
r := length % 4
if 0 == r {
return q
}
return q + 1
}
func padLength(length uint32) uint32 {
return padBlocks(length) * 4
}
// All tables must begin on four-byte boundries, and any remaining space between tables is padded with zeros.
func padSpace(w *errWriter, length uint32) {
pad := padLength(length) - length
w.write(make([]uint8, pad))
}