-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
215 lines (192 loc) · 4.99 KB
/
walk.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package svg
import (
"io"
"github.com/cdillond/gdf"
)
type svgRoot struct {
n *node
xContent gdf.XContent
Height, Width float64
HScale, WScale float64
defs map[string]*node
}
/*
SVGs may specify a height and width as well as a "viewBox". If no viewBox is given, it is assumed
to be coincident with the height and width. That is, the coordinates begin (at the top left of the page)
at (0, 0), and end at the bottom right of the page at (w, h). Any graphics drawn outside of this box
are not considered. If both a viewBox and height and width parameters are provided, then the height
and width are interpreted instead as scale values. The values of an SVG with a height and width of 5px
but a viewBox of 0 0 10 10 are scaled down by 2.
*/
func Decode(r io.Reader) (gdf.XContent, error) {
out := svgRoot{
n: new(node),
xContent: *gdf.NewXContent(nil, gdf.Rect{}),
HScale: 1,
WScale: 1,
defs: make(map[string]*node),
}
out.xContent.Filter = gdf.NoFilter // for now...
// the SVG gets unmarshalled into a tree of structs
unmarshalXML(out.n, r, out.defs)
out.n = out.n.children[0]
var h, w float64
if out.n.self.height != nil {
h = *out.n.self.height
out.Height = h
}
if out.n.self.width != nil {
w = *out.n.self.width
out.Width = w
}
if out.n.self.viewBox != nil {
out.Width = px * (out.n.self.viewBox[2] - out.n.self.viewBox[0])
/*if out.n.self.width == nil {
out.Width = w
}*/
out.Height = px * (out.n.self.viewBox[3] - out.n.self.viewBox[1])
/*if out.n.self.height == nil {
out.Height = h
}*/
}
if h != 0 {
out.HScale = out.Height / h
} else {
out.HScale = 1
}
if w != 0 {
out.WScale = out.Width / w
} else {
out.WScale = 1
}
if out.n.self.viewBox != nil {
out.xContent.BBox = gdf.Rect{
LLX: out.n.self.viewBox[0],
LLY: out.n.self.viewBox[1],
URX: out.n.self.viewBox[2],
URY: out.n.self.viewBox[3],
}
} else {
out.xContent.BBox.URY = out.Height
out.xContent.BBox.URX = out.Width
}
walk(out.n, &out.xContent, out.HScale, out.WScale, out.defs)
return out.xContent, nil
}
func walk(n *node, x *gdf.XContent, hScale, wScale float64, defs map[string]*node) {
if n == nil || n.k == defsKind || n.k == badKind {
return
}
style := merge(n.inherited, n.self)
if style.fill != nil && *style.fill != badColor {
if *style.fill == rgbBlack || n.inherited.fill == nil || *n.inherited.fill != *style.fill {
x.SetColor(*style.fill)
}
}
if style.stroke != nil && *style.stroke != badColor {
if n.inherited.stroke == nil || *n.inherited.stroke != *style.stroke {
x.SetColorStroke(*style.stroke)
}
}
if style.strokeWidth != nil {
x.SetLineWidth(*style.strokeWidth)
}
if n.k == useKind {
if len(*n.self.href) > 0 {
t1, ok := defs[(*n.self.href)[1:]]
if ok {
t2 := *t1
t2.self = merge(style, t2.self)
t2.transforms = append(t2.transforms, n.transforms...)
walk(&t2, x, hScale, wScale, defs)
}
return
}
}
if n.k == maskKind {
x.QSave()
//if n.self.maskFill != nil && *n.self.maskFill == rgbWhite {
//fmt.Println(n.children)
//x.SetAlphaConst(1)
//}
for _, c := range n.children {
walk(c, x, hScale, wScale, defs)
}
x.QRestore()
return
}
if n.self.mask != nil {
t1, ok := defs[(*n.self.mask)]
if ok {
t2 := *t1
t2.self = merge(style, t2.self)
t2.transforms = append(t2.transforms, n.transforms...)
walk(&t2, x, hScale, wScale, defs)
}
return
}
//if n.k != useKind {
cmds := resolvePathCmds(n.tmpCmds)
if n.k == circleKind {
cmds = append(cmds, pdfPathCmd{
op: circle,
args: []gdf.Point{{X: *n.self.cx, Y: *n.self.cy}, {X: *n.self.r, Y: 0}},
})
}
if n.k == rectKind {
cmds = append(cmds, pdfPathCmd{
op: rect,
args: []gdf.Point{{X: *n.self.x, Y: *n.self.y}, {X: *n.self.width, Y: *n.self.height}},
})
}
if n.k == ellipseKind {
x.QSave()
cmds = append(cmds, pdfPathCmd{
op: ellipse,
args: []gdf.Point{{X: *n.self.cx, Y: *n.self.cy}, {X: *n.self.rx, Y: *n.self.ry}},
})
}
for _, c := range cmds {
for i := range c.args {
c.args[i].X *= px
c.args[i].Y *= px
for j := range n.transforms {
c.args[i] = gdf.Transform(c.args[i], n.transforms[j])
}
c.args[i].Y *= hScale
c.args[i].Y = hScale*x.BBox.URY - c.args[i].Y
c.args[i].X *= wScale
}
writeCmd(&x.ContentStream, c)
}
if len(cmds) > 0 {
/*if style.clipRule != nil {
x.Clip(*style.clipRule)
}*/
//if style.fill != nil {
// fmt.Println(*style.fill)
//}
if style.fill != nil && *style.fill != badColor && style.stroke != nil && *style.stroke != badColor {
if style.fillRule != nil {
x.FillStroke(*style.fillRule)
} else {
x.FillStroke(gdf.NonZero)
}
} else if style.fill != nil && *style.fill != badColor {
if style.fillRule != nil {
x.Fill(*style.fillRule)
} else {
x.Fill(gdf.NonZero)
}
} else if style.stroke != nil && *style.stroke != badColor {
x.Stroke()
}
}
if n.k == ellipseKind {
x.QRestore()
}
// }
for _, c := range n.children {
walk(c, x, hScale, wScale, defs)
}
}