-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtable_os2.go
102 lines (88 loc) · 2.11 KB
/
table_os2.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
package truetype
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
)
type TableOS2Version0 struct {
Version uint16
XAvgCharWidth uint16
USWeightClass uint16
USWidthClass uint16
FSType uint16
YSubscriptXSize int16
YSubscriptYSize int16
YSubscriptXOffset int16
YSubscriptYOffset int16
YSuperscriptXSize int16
YSuperscriptYSize int16
YSuperscriptXOffset int16
YSuperscriptYOffset int16
YStrikeoutSize int16
YStrikeoutPosition int16
SFamilyClass int16
Panose [10]byte
UlCharRange [4]uint32
AchVendID Tag
FsSelection uint16
USFirstCharIndex uint16
USLastCharIndex uint16
STypoAscender int16
STypoDescender int16
STypoLineGap int16
UsWinAscent uint16
UsWinDescent uint16
}
type TableOS2Version1 struct {
TableOS2Version0
UlCodePageRange1 uint32
UlCodePageRange2 uint32
}
// TableOS2Version4 is the OS2 format for versions 2,3 and 4
type TableOS2Version4 struct {
TableOS2Version1
SxHeigh int16
SCapHeight int16
UsDefaultChar uint16
UsBreakChar uint16
UsMaxContext uint16
}
type TableOS2 struct {
TableOS2Version4
UsLowerPointSize uint16
UsUpperPointSize uint16
}
func parseTableOS2(buf []byte) (*TableOS2, error) {
if len(buf) < 2 {
return nil, errors.New("invalid 'os2' table (EOF)")
}
var (
dst interface{}
out TableOS2
)
version := binary.BigEndian.Uint16(buf)
switch version {
case 0:
dst = &out.TableOS2Version0
case 1, 2, 3:
dst = &out.TableOS2Version1
case 4:
dst = &out.TableOS2Version4
case 5:
dst = &out
default:
return nil, fmt.Errorf("unsupported 'os2' table version: %d", version)
}
if err := binary.Read(bytes.NewReader(buf), binary.BigEndian, dst); err != nil {
return nil, fmt.Errorf("invalid 'os2' table: %s", err)
}
return &out, nil
}
func (t *TableOS2) useTypoMetrics() bool {
const useTypoMetrics = 1 << 7
return t.FsSelection&useTypoMetrics != 0
}
func (t *TableOS2) hasData() bool {
return t.USWeightClass != 0 || t.USWidthClass != 0 || t.USFirstCharIndex != 0 || t.USLastCharIndex != 0
}