-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshapePath.go
More file actions
136 lines (112 loc) · 2.85 KB
/
shapePath.go
File metadata and controls
136 lines (112 loc) · 2.85 KB
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
package gorough
import (
"github.com/NovikovRoman/gorough/data_parser"
"regexp"
)
type path struct {
options *PathOptions
operations []operation
}
func (p path) Name() string {
return shapePoligon
}
func (p path) Operations() []operation {
return p.operations
}
func (p path) Attributes() Attributes {
return map[string]string{
"stroke": p.options.Styles.Stroke,
"stroke-width": data_parser.FloatToString(p.options.Styles.StrokeWidth),
"fill": p.options.Styles.Fill,
"fill-weight": data_parser.FloatToString(p.options.Styles.FillWeight),
}
}
func (p path) Styles() *Styles {
return p.options.Styles
}
var (
reShapePathLt = regexp.MustCompile(`(!s)\n`)
reShapePathHyphen = regexp.MustCompile(`(!s)-\s`)
reShapePathDoubleSpace = regexp.MustCompile(`(!s)\s\s`)
)
func NewPath(d string, opt *PathOptions) (*path, error) {
if opt == nil {
opt = &PathOptions{}
}
if opt.PenOptions == nil {
opt.PenOptions = PenOptionsDefault()
}
if opt.Styles == nil {
opt.Styles = StylesDefault()
}
hasFill := opt.Styles.Fill != None && opt.Styles.Fill != "transparent"
if (!hasFill || opt.Styles.Stroke != "") && opt.Styles.StrokeWidth == 0 {
opt.Styles.StrokeWidth = 1
}
var (
operations []operation
combined []Point
)
operations = []operation{}
p := &path{
options: opt,
operations: operations,
}
if d == "" {
return p, nil
}
d = reShapePathLt.ReplaceAllString(d, " ")
d = reShapePathHyphen.ReplaceAllString(d, "-")
d = reShapePathDoubleSpace.ReplaceAllString(d, " ")
simplified := opt.Simplification < 1
distance := opt.PenOptions.Roughness / 2
if simplified {
distance = 4 - 4*opt.Simplification
}
points, err := PointsOnPath(d, 1, distance)
if err != nil {
return nil, err
}
if hasFill {
if opt.CombineNestedSvgPaths {
combined = []Point{}
for _, pp := range points {
combined = append(combined, pp...)
}
if opt.Styles.Filler == nil {
operations = append(operations, solidFillPolygon(combined, opt.PenOptions))
} else {
operations = append(operations, patternFillPolygon(combined, &LineOptions{
PenOptions: opt.PenOptions,
Styles: opt.Styles,
}))
}
} else {
for _, pp := range points {
if opt.Styles.Filler == nil {
operations = append(operations, solidFillPolygon(pp, opt.PenOptions))
} else {
operations = append(operations, patternFillPolygon(pp, &LineOptions{
PenOptions: opt.PenOptions,
Styles: opt.Styles,
}))
}
}
}
}
if opt.Styles.Stroke != None {
if simplified {
for _, pp := range points {
operations = append(operations, linearPathOperation(pp, false, opt.PenOptions))
}
} else {
var op operation
if op, err = svgPath(d, opt.PenOptions); err != nil {
return nil, err
}
operations = append(operations, op)
}
}
p.operations = operations
return p, nil
}