-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgelmock.go
98 lines (85 loc) · 1.74 KB
/
gelmock.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
package contraption
import (
"fmt"
"io"
"github.com/neputevshina/geom"
)
// Segment is a segment of a vector path.
// It is based on golang.org/x/image/font/sfnt.Segment.
type Segment struct {
// Op is the operator.
// 'M' — move, one coordinate
// 'L' — line, one coordinate
// 'Q' — quadratic bezier, two coordinates
// 'C' — cubic bezier, three coordinates
Op byte
// Args is up to three (x, y) coordinates. The Y axis increases down.
Args [3]geom.Point
}
func (s Segment) LastComponent() geom.Point {
switch s.Op {
case 'M':
return s.Args[0]
case 'L':
return s.Args[0]
case 'Q':
return s.Args[1]
case 'C':
return s.Args[2]
default:
panic(`malformed Segment`)
}
}
func (s Segment) String() string {
switch s.Op {
case 'M':
return fmt.Sprint("{M ", s.Args[0], "}")
case 'L':
return fmt.Sprint("{L ", s.Args[0], "}")
case 'Q':
return fmt.Sprint("{Q ", s.Args[0], " ", s.Args[1], "}")
case 'C':
return fmt.Sprint("{Q ", s.Args[0], " ", s.Args[1], " ", s.Args[2], "}")
default:
return "{invalid op “" + string(s.Op) + "”}"
}
}
type (
point = geom.Point
rectangle = geom.Rectangle
)
var pt = geom.Pt
func validSize(r []rune) bool {
return sizeRegexp.MatchReader(untoRuneScanner(r))
}
type runeBuf struct {
buf []rune
off int
}
func untoRuneScanner(b []rune) io.RuneScanner {
return &runeBuf{buf: b}
}
func (s *runeBuf) ReadRune() (r rune, size int, err error) {
if s.off >= len(s.buf) {
err = io.EOF
return
}
r = s.buf[s.off]
s.off++
size = 1
err = nil
return
}
func (s *runeBuf) UnreadRune() error {
if s.off > 0 {
s.off--
return nil
}
panic("unread at 0")
}
func ApplySegment(g geom.Geom, s Segment) Segment {
for i := range s.Args {
s.Args[i] = g.ApplyPt(s.Args[i])
}
return s
}