-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_hhea.go
106 lines (89 loc) · 2.29 KB
/
table_hhea.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
/*
* 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 (
"github.com/sirupsen/logrus"
)
// hheaTable represents the horizontal header table (hhea).
// This table contains information for horizontal layout.
// https://docs.microsoft.com/en-us/typography/opentype/spec/hhea
type hheaTable struct {
majorVersion uint16
minorVersion uint16
ascender fword
descender fword
lineGap fword
advanceWidthMax ufword
minLeftSideBearing fword
minRightSideBearing fword
xMaxExtent fword
caretSlopeRise int16
caretSlopeRun int16
caretOffset int16
metricDataFormat int16
numberOfHMetrics uint16 // Number of hMetric entries in 'hmtx' table.
}
func (f *font) parseHhea(r *byteReader) (*hheaTable, error) {
_, has, err := f.seekToTable(r, "hhea")
if err != nil {
return nil, err
}
if !has {
logrus.Debug("hhea table absent")
return nil, nil
}
t := &hheaTable{}
err = r.read(&t.majorVersion, &t.minorVersion)
if err != nil {
return nil, err
}
err = r.read(&t.ascender, &t.descender, &t.lineGap)
if err != nil {
return nil, err
}
err = r.read(&t.advanceWidthMax, &t.minLeftSideBearing, &t.minRightSideBearing, &t.xMaxExtent)
if err != nil {
return nil, err
}
err = r.read(&t.caretSlopeRise, &t.caretSlopeRun, &t.caretOffset)
if err != nil {
return nil, err
}
// Skip over reserved bytes.
err = r.Skip(4 * 2)
if err != nil {
return nil, err
}
return t, r.read(&t.metricDataFormat, &t.numberOfHMetrics)
}
func (f *font) writeHhea(w *byteWriter) error {
if f.hhea == nil {
logrus.Debug("hhea is nil - nothing to write")
return nil
}
t := f.hhea
err := w.write(t.majorVersion, t.minorVersion)
if err != nil {
return err
}
err = w.write(t.ascender, t.descender, t.lineGap)
if err != nil {
return err
}
err = w.write(t.advanceWidthMax, t.minLeftSideBearing, t.minRightSideBearing, t.xMaxExtent)
if err != nil {
return err
}
err = w.write(t.caretSlopeRise, t.caretSlopeRun, t.caretOffset)
if err != nil {
return err
}
reserved := int16(0)
err = w.write(reserved, reserved, reserved, reserved)
if err != nil {
return err
}
return w.write(t.metricDataFormat, t.numberOfHMetrics)
}