-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.go
110 lines (99 loc) · 2.46 KB
/
shape.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
package hvif
import (
"encoding/binary"
"fmt"
"io"
)
type (
shapeType uint8
shapeFlag uint8
)
const (
shapePathSource shapeType = 0xa
)
const (
shapeFlagTransform shapeFlag = 1 << (1 + iota)
shapeFlagHinting
shapeFlagLodScale
shapeFlagHasTransformers
shapeFlagTranslation
)
type Shape struct {
Hinting bool
styleID *uint8
pathIDs []uint8
Transforms []Transformer
}
func readShape(r io.Reader) (Shape, error) {
var shape Shape
var stype shapeType
err := binary.Read(r, binary.LittleEndian, &stype)
if err != nil {
return shape, fmt.Errorf("reading type: %w", err)
}
if stype == shapePathSource {
var styleID uint8
err := binary.Read(r, binary.LittleEndian, &styleID)
if err != nil {
return shape, fmt.Errorf("reading style id: %w", err)
}
shape.styleID = &styleID
var pathCount uint8
err = binary.Read(r, binary.LittleEndian, &pathCount)
if err != nil {
return shape, fmt.Errorf("reading path count: %w", err)
}
for i := byte(0); i < pathCount; i++ {
var pathID uint8
err := binary.Read(r, binary.LittleEndian, &pathID)
if err != nil {
return shape, fmt.Errorf("reading path [%d] id: %w", i, err)
}
shape.pathIDs = append(shape.pathIDs, pathID)
}
var flags shapeFlag
err = binary.Read(r, binary.LittleEndian, &flags)
if err != nil {
return shape, fmt.Errorf("reading flags: %w", err)
}
if flags&shapeFlagTransform != 0 {
t, err := readAffine(r)
if err != nil {
return shape, fmt.Errorf("reading affine transformer: %w", err)
}
shape.Transforms = append(shape.Transforms, &t)
}
if flags&shapeFlagTranslation != 0 {
t, err := readTranslation(r)
if err != nil {
return shape, fmt.Errorf("reading translation %w", err)
}
shape.Transforms = append(shape.Transforms, &t)
}
if flags&shapeFlagLodScale != 0 {
t, err := readLodScale(r)
if err != nil {
return shape, fmt.Errorf("reading lod scale: %w", err)
}
shape.Transforms = append(shape.Transforms, &t)
}
if flags&shapeFlagHasTransformers != 0 {
var count uint8
err := binary.Read(r, binary.LittleEndian, &count)
if err != nil {
return shape, fmt.Errorf("reading transformers count: %w", err)
}
for i := range count {
t, err := readTransformer(r)
if err != nil {
return shape, fmt.Errorf("reading transformer [%d]: %w", i, err)
}
shape.Transforms = append(shape.Transforms, t)
}
}
if flags&shapeFlagHinting != 0 {
shape.Hinting = true
}
}
return shape, nil
}