/
ttc.go
78 lines (72 loc) · 1.67 KB
/
ttc.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
package opentype
import (
"encoding/binary"
"os"
)
// IsFontCollection returns true if the file is FontCollection format.
func IsFontCollection(f *os.File) (bool, error) {
sfntVersion, err := parseSfntVersion(f, 0)
if err != nil {
return false, err
}
if sfntVersion == SfntVersionTTCHeader {
return true, nil
}
return false, nil
}
// FontCollection packages multiple OpenType fonts in a single file structure.
type FontCollection struct {
header *ttcHeader
Fonts []*Font
}
// ParseFontCollections returns the FontCollection instance from the font file.
func ParseFontCollections(f *os.File) (fc *FontCollection, err error) {
fc = &FontCollection{}
fc.header, err = parseTTCHeader(f)
if err != nil {
return
}
fc.Fonts = make([]*Font, fc.header.numFonts)
for i, o := range fc.header.offsetTable {
fc.Fonts[i], err = parseFont(f, int64(o))
if err != nil {
return
}
}
return
}
// ttcHeader is The header of TTC format file.
type ttcHeader struct {
sfntVersion Tag
majorVersion uint16
minorVersion uint16
offsetTable []uint32
numFonts uint32
}
func parseTTCHeader(f *os.File) (h *ttcHeader, err error) {
h = &ttcHeader{}
err = binary.Read(f, binary.BigEndian, &(h.sfntVersion))
if err != nil {
return
}
err = binary.Read(f, binary.BigEndian, &(h.majorVersion))
if err != nil {
return
}
err = binary.Read(f, binary.BigEndian, &(h.minorVersion))
if err != nil {
return
}
err = binary.Read(f, binary.BigEndian, &(h.numFonts))
if err != nil {
return
}
h.offsetTable = make([]uint32, h.numFonts)
for i := uint32(0); i < h.numFonts; i++ {
err = binary.Read(f, binary.BigEndian, &(h.offsetTable[i]))
if err != nil {
return
}
}
return
}