-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
200 lines (173 loc) · 4.2 KB
/
path.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
package hvif
import (
"encoding/binary"
"fmt"
"io"
"math"
)
const (
pathCommandSizeBits = 2
byteSizeBits = 8
)
type pathFlag uint8
const (
pathFlagClosed pathFlag = 1 << (1 + iota)
pathFlagUsesCommands
pathFlagNoCurves
)
type pathCommandType uint8
const (
pathCommandHLine pathCommandType = iota
pathCommandVLine
pathCommandLine
pathCommandCurve
)
type PathElement any // Point | HLine | VLine | Curve
type HLine struct {
X float32
}
type VLine struct {
Y float32
}
type Point struct {
X float32
Y float32
}
type Curve struct {
PointIn Point
Point Point
PointOut Point
}
type Path struct {
isClosed bool
Elements []PathElement
}
func readPoint(r io.Reader) (Point, error) {
var p Point
x, err := readFloatCoord(r)
if err != nil {
return p, fmt.Errorf("reading x coord: %w", err)
}
y, err := readFloatCoord(r)
if err != nil {
return p, fmt.Errorf("reading y coord: %w", err)
}
p.X = x
p.Y = y
return p, nil
}
func splitCommandTypes(rawTypes []uint8, count uint8) []pathCommandType {
pct := make([]pathCommandType, 0, count)
const pctsPerByte = (byteSizeBits / pathCommandSizeBits)
for i := range count {
segment := i / pctsPerByte
shift := i % pctsPerByte
commandType := (rawTypes[segment] >> (shift * pathCommandSizeBits)) & 0x3
pct = append(pct, pathCommandType(commandType))
}
return pct
}
func readPath(r io.Reader) (Path, error) {
var path Path
var flag pathFlag
err := binary.Read(r, binary.LittleEndian, &flag)
if err != nil {
return path, fmt.Errorf("reading flags: %w", err)
}
path.isClosed = flag&pathFlagClosed != 0
switch {
case flag&pathFlagNoCurves != 0:
var count uint8
err := binary.Read(r, binary.LittleEndian, &count)
if err != nil {
return path, fmt.Errorf("reading count for path no curves: %w", err)
}
var points []PathElement
for i := byte(0); i < count; i++ {
p, err := readPoint(r)
if err != nil {
return path, fmt.Errorf("reading point: %w", err)
}
points = append(points, p)
}
path.Elements = points
case flag&pathFlagUsesCommands != 0:
var count uint8
err := binary.Read(r, binary.LittleEndian, &count)
if err != nil {
return path, fmt.Errorf("reading count for path with commands: %w", err)
}
// Each command is 2 bits, aligned in a byte
bytesForCommandTypes := uint8(math.Ceil(pathCommandSizeBits * float64(count) / byteSizeBits))
pathRawCommandTypes := make([]uint8, bytesForCommandTypes)
err = binary.Read(r, binary.LittleEndian, &pathRawCommandTypes)
if err != nil {
return path, fmt.Errorf("reading commands: %w", err)
}
pathCommandTypes := splitCommandTypes(pathRawCommandTypes, count)
var points []PathElement
for i := byte(0); i < count; i++ {
var line interface{}
switch pathCommandTypes[i] {
case pathCommandHLine:
c, err := readFloatCoord(r)
if err != nil {
return path, fmt.Errorf("reading hline coord: %w", err)
}
line = &HLine{c}
case pathCommandVLine:
c, err := readFloatCoord(r)
if err != nil {
return path, fmt.Errorf("reading vline coord: %w", err)
}
line = &VLine{c}
case pathCommandLine:
p, err := readPoint(r)
if err != nil {
return path, fmt.Errorf("reading point: %w", err)
}
line = &p
case pathCommandCurve:
c, err := readCurve(r)
if err != nil {
return path, fmt.Errorf("reading curve: %w", err)
}
line = &c
}
points = append(points, line)
}
path.Elements = points
default:
var count uint8
err := binary.Read(r, binary.LittleEndian, &count)
if err != nil {
return path, fmt.Errorf("reading count for curves: %w", err)
}
var points []PathElement
for i := byte(0); i < count; i++ {
c, err := readCurve(r)
if err != nil {
return path, fmt.Errorf("reading curve: %w", err)
}
points = append(points, &c)
}
path.Elements = points
}
return path, nil
}
func readCurve(r io.Reader) (Curve, error) {
var c Curve
p1, err := readPoint(r)
if err != nil {
return c, fmt.Errorf("reading first point: %w", err)
}
p2, err := readPoint(r)
if err != nil {
return c, fmt.Errorf("reading second point: %w", err)
}
p3, err := readPoint(r)
if err != nil {
return c, fmt.Errorf("reading third point: %w", err)
}
return Curve{p1, p2, p3}, nil
}