-
Notifications
You must be signed in to change notification settings - Fork 3
/
font.go
201 lines (183 loc) · 5.69 KB
/
font.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
package font
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"math"
"reflect"
"sort"
"github.com/golang/freetype/truetype"
"github.com/tdewolff/parse/v2"
"golang.org/x/image/font/sfnt"
)
// MediaType returns the media type (MIME) for a given font.
func MediaType(b []byte) (string, error) {
if len(b) < 4 {
return "", fmt.Errorf("empty font file")
}
tag := string(b[:4])
if tag == "wOFF" {
return "font/woff", nil
} else if tag == "wOF2" {
return "font/woff2", nil
} else if tag == "true" || binary.BigEndian.Uint32(b[:4]) == 0x00010000 || tag == "ttcf" {
return "font/truetype", nil
} else if tag == "OTTO" {
return "font/opentype", nil
} else if 36 < len(b) && binary.LittleEndian.Uint16(b[34:36]) == 0x504C {
return "application/vnd.ms-fontobject", nil
}
return "", fmt.Errorf("unrecognized font file format")
}
// Extension returns the file extension for a given font. An empty string is returned when the font is not recognized.
func Extension(b []byte) string {
mediatype, err := MediaType(b)
if err != nil {
return ""
}
switch mediatype {
case "font/truetype":
return ".ttf"
case "font/opentype":
return ".otf"
case "font/woff":
return ".woff"
case "font/woff2":
return ".woff2"
case "application/vnd.ms-fontobject":
return ".eot"
}
return ""
}
// ToSFNT takes a byte slice and transforms it into an SFNT byte slice. That is, given TTF/OTF/WOFF/WOFF2/EOT input, it will return TTF/OTF output.
func ToSFNT(b []byte) ([]byte, error) {
mediatype, err := MediaType(b)
if err != nil {
return nil, err
}
switch mediatype {
case "font/truetype":
return b, nil
case "font/opentype":
return b, nil
case "font/woff":
if b, err = ParseWOFF(b); err != nil {
return nil, fmt.Errorf("WOFF: %w", err)
}
return b, nil
case "font/woff2":
if b, err = ParseWOFF2(b); err != nil {
return nil, fmt.Errorf("WOFF2: %w", err)
}
return b, nil
case "application/vnd.ms-fontobject":
if b, err = ParseEOT(b); err != nil {
return nil, fmt.Errorf("EOT: %w", err)
}
return b, nil
}
return nil, fmt.Errorf("unrecognized font file format")
}
// NewSFNTReader takes an io.Reader and transforms it into an SFNT reader. That is, given TTF/OTF/WOFF/WOFF2/EOT input, it will return TTF/OTF output.
func NewSFNTReader(r io.Reader) (*bytes.Reader, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
if b, err = ToSFNT(b); err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
// ParseFont parses a byte slice and of a TTF, OTF, WOFF, WOFF2, or EOT font format. It will return the parsed font and its mimetype.
func ParseFont(b []byte, index int) (*SFNT, error) {
sfntBytes, err := ToSFNT(b)
if err != nil {
return nil, err
}
return ParseSFNT(sfntBytes, index)
}
// FromGoFreetype parses a structure from truetype.Font to a valid SFNT byte slice.
func FromGoFreetype(font *truetype.Font) []byte {
v := reflect.ValueOf(*font)
tables := map[string][]byte{}
tables["cmap"] = v.FieldByName("cmap").Bytes()
tables["cvt "] = v.FieldByName("cvt").Bytes()
tables["fpgm"] = v.FieldByName("fpgm").Bytes()
tables["glyf"] = v.FieldByName("glyf").Bytes()
tables["hdmx"] = v.FieldByName("hdmx").Bytes()
tables["head"] = v.FieldByName("head").Bytes()
tables["hhea"] = v.FieldByName("hhea").Bytes()
tables["hmtx"] = v.FieldByName("hmtx").Bytes()
tables["kern"] = v.FieldByName("kern").Bytes()
tables["loca"] = v.FieldByName("loca").Bytes()
tables["maxp"] = v.FieldByName("maxp").Bytes()
tables["name"] = v.FieldByName("name").Bytes()
tables["OS/2"] = v.FieldByName("os2").Bytes()
tables["prep"] = v.FieldByName("prep").Bytes()
tables["vmtx"] = v.FieldByName("vmtx").Bytes()
// reconstruct missing post table
post := parse.NewBinaryWriter([]byte{})
post.WriteUint32(0x00030000) // version
post.WriteUint32(0) // italicAngle
post.WriteInt16(0) // underlinePosition
post.WriteInt16(0) // underlineThickness
post.WriteUint32(0) // isFixedPitch
post.WriteUint32(0) // minMemType42
post.WriteUint32(0) // maxMemType42
post.WriteUint32(0) // minMemType1
post.WriteUint32(0) // maxMemType1
tables["post"] = post.Bytes()
// remove empty tables
tags := []string{}
for tag, table := range tables {
if 0 < len(table) {
tags = append(tags, tag)
}
}
sort.Strings(tags)
w := parse.NewBinaryWriter([]byte{})
w.WriteUint32(0x00010000) // sfntVersion
numTables := uint16(len(tags))
entrySelector := uint16(math.Log2(float64(numTables)))
searchRange := uint16(1 << (entrySelector + 4))
w.WriteUint16(numTables) // numTables
w.WriteUint16(searchRange) // searchRange
w.WriteUint16(entrySelector) // entrySelector
w.WriteUint16(numTables<<4 - searchRange) // rangeShift
// write table directory
var checksumAdjustmentPos uint32
offset := w.Len() + 16*uint32(numTables)
for _, tag := range tags {
length := uint32(len(tables[tag]))
padding := (4 - length&3) & 3
for j := 0; j < int(padding); j++ {
tables[tag] = append(tables[tag], 0x00)
}
if tag == "head" {
checksumAdjustmentPos = offset + 8
binary.BigEndian.PutUint32(tables[tag][8:], 0x00000000)
}
checksum := calcChecksum(tables[tag])
w.WriteString(tag)
w.WriteUint32(checksum)
w.WriteUint32(offset)
w.WriteUint32(length)
offset += length + padding
}
// write tables
for _, tag := range tags {
w.WriteBytes(tables[tag])
}
buf := w.Bytes()
binary.BigEndian.PutUint32(buf[checksumAdjustmentPos:], 0xB1B0AFBA-calcChecksum(buf))
return buf
}
// FromGoSFNT parses a structure from sfnt.Font to a valid SFNT byte slice.
func FromGoSFNT(font *sfnt.Font) []byte {
v := reflect.ValueOf(*font)
buf := v.FieldByName("src").FieldByName("b").Bytes()
return buf
}