-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtable_head.go
93 lines (80 loc) · 2.34 KB
/
table_head.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package unitype
import (
"errors"
"github.com/sirupsen/logrus"
)
// Font header.
// https://docs.microsoft.com/en-us/typography/opentype/spec/head
type headTable struct {
majorVersion uint16 // 00 01
minorVersion uint16 // 00 00
fontRevision fixed // 00 01 CA 3D
checksumAdjustment uint32 // 00 00 00 00
magicNumber uint32 // 5F 0F 3C F5
flags uint16
unitsPerEm uint16
created longdatetime
modified longdatetime
xMin int16
yMin int16
xMax int16
yMax int16
macStyle uint16
lowestRecPPEM uint16
fontDirectionHint int16
indexToLocFormat int16
glyphDataFormat int16
}
// parse the font's *head* table from `r` in the context of `f`.
// TODO(gunnsth): Read the table as bytes first and then process? Probably easier in terms of checksumming etc.
func (f *font) parseHead(r *byteReader) (*headTable, error) {
_, has, err := f.seekToTable(r, "head")
if err != nil {
return nil, err
}
if !has {
// Does not have head.
return nil, nil
}
t := &headTable{}
err = r.read(&t.majorVersion, &t.minorVersion, &t.fontRevision)
if err != nil {
return nil, err
}
err = r.read(&t.checksumAdjustment, &t.magicNumber)
if err != nil {
return nil, err
}
if t.magicNumber != 0x5F0F3CF5 {
logrus.Debugf("Error: got magic number 0x%X", t.magicNumber)
return nil, errors.New("magic number mismatch")
}
err = r.read(&t.flags, &t.unitsPerEm, &t.created, &t.modified)
if err != nil {
return nil, err
}
err = r.read(&t.xMin, &t.yMin, &t.xMax, &t.yMax)
if err != nil {
return nil, err
}
return t, r.read(&t.macStyle, &t.lowestRecPPEM, &t.fontDirectionHint, &t.indexToLocFormat, &t.glyphDataFormat)
}
func (f *font) writeHead(w *byteWriter) error {
if f.head == nil {
return errRequiredField
}
t := f.head
err := w.write(t.majorVersion, t.minorVersion, t.fontRevision, t.checksumAdjustment, t.magicNumber)
if err != nil {
return err
}
err = w.write(t.flags, t.unitsPerEm, t.created, t.modified, t.xMin, t.yMin, t.xMax, t.yMax)
if err != nil {
return err
}
return w.write(t.macStyle, t.lowestRecPPEM, t.fontDirectionHint, t.indexToLocFormat, t.glyphDataFormat)
}