-
Notifications
You must be signed in to change notification settings - Fork 2
/
svgparser.c
473 lines (427 loc) · 13 KB
/
svgparser.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//------------------------------------------------------------------------
// SVG tag dispatch table
//
void SVGparser::vtable_init() {
vtable["path"] = &SVGparser::parse_path_;
vtable["rect"] = &SVGparser::parse_rect_;
vtable["circle"] = &SVGparser::parse_circle_;
vtable["radialGradient"] = &SVGparser::parse_radial_gradient_;
vtable["linearGradient"] = &SVGparser::parse_linear_gradient_;
}
//------------------------------------------------------------------------
// SVG tag handlers
//
void SVGparser::parse_path_(XMLNode node) {
p = (char *) node.getAttribute("d");
path = new Path;
pos.Set(0,0);
while(parse_cmd());
path->finish();
}
//------------------------------------------------------------------------
void SVGparser::parse_rect_(XMLNode node) {
float32 x,y,w,h;
x = (float32) atof(node.getAttribute("x"));
y = (float32) atof(node.getAttribute("y"));
w = (float32) atof(node.getAttribute("width"));
h = (float32) atof(node.getAttribute("height"));
// NOTE rx,ry (corner radius) attributes exist in our Inkscape SVGs, but they seem to be negligible.
path = new Path;
path->push(b2Vec2(x,y));
path->push(b2Vec2(x+w,y));
path->push(b2Vec2(x+w,y+h));
path->push(b2Vec2(x,y+h));
path->close();
}
void SVGparser::parse_circle_(XMLNode node) {
const int segments = 24;
const float increment = 2 * M_PI / segments;
const float c = cos(increment);
const float s = sin(increment);
float cx,cy,r, x,y, t;
cx = atof(node.getAttribute("cx"));
cy = atof(node.getAttribute("cy"));
r = atof(node.getAttribute("r"));
// Bresenham circle algorithm?
x = r;
y = 0;
path = new Path;
for(int i=segments; i>0; i--) {
path->push(b2Vec2(cx+x, cy+y));
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
path->close();
}
void SVGparser::color_style_(XMLNode node) {
// Parse style; default method
const char *style = node.getAttribute("style");
if(style) {
parse_style_(style);
return;
}
// Older formats support individual style settings
const char *opacity = node.getAttribute("opacity");
const char *fill = node.getAttribute("fill");
const char *stroke = node.getAttribute("stroke");
const char *fill_opacity = node.getAttribute("fill-opacity");
const char *stroke_opacity = node.getAttribute("stroke-opacity");
if(fill) parse_color_(std::string(fill), path->fill);
if(stroke) parse_color_(std::string(stroke), path->stroke);
}
void SVGparser::init_gradient_(XMLNode node, Gradient *gradient) {
// Parse gradient stops
int num_stops = node.nChildNode();
for(int i = 0; i < num_stops; i++) {
XMLNode child = node.getChildNode(i);
const char *name = child.getName();
const char *style = child.getAttribute("style");
GLubyte *stop = new GLubyte[4];
float32 offset = atof(std::string(child.getAttribute("offset")).c_str());
if(style) {
parse_stop_style_(style, stop);
} else {
const char *color = child.getAttribute("stop-color");
parse_color_(color, stop);
stop[3] = 255 * atof(std::string(child.getAttribute("stop-opacity")).c_str());
}
gradient->stops[offset] = stop;
}
// Parse gradient transform
const char *transform = node.getAttribute("gradientTransform");
// Use identity matrix if not defined
Matrix *m = transform ? new Matrix(std::string(transform)) : new Matrix(1, 0, 0, 1, 0, 0);
gradient->inv_transform = m->inverse();
delete m;
// Inherite gradient stops
const char *xlink = node.getAttribute("xlink:href");
if(xlink) {
std::string href = std::string(xlink);
href.erase(0,1);
gradient->stops = gradients_[href]->stops;
}
// TODO: gradientUnits
}
void SVGparser::parse_radial_gradient_(XMLNode node) {
const char* id = node.getAttribute("id");
// TODO: Add subclass
RadialGradient *gradient = new RadialGradient(id);
init_gradient_(node, gradient);
// Parse attributes
const char* cx = node.getAttribute("cx");
const char* cy = node.getAttribute("cy");
const char* r = node.getAttribute("r");
const char* fx = node.getAttribute("fx");
const char* fy = node.getAttribute("fy");
if(cx) gradient->cx = atof(std::string(cx).c_str());
if(cy) gradient->cy = atof(std::string(cy).c_str());
if(r) gradient->r = atof(std::string(r).c_str());
//if(fx) gradient->fx = atof(std::string(fx).c_str());
//if(fy) gradient->fy = atof(std::string(fy).c_str());
gradients_[std::string(id)] = gradient;
}
void SVGparser::parse_linear_gradient_(XMLNode node) {
const char* id = node.getAttribute("id");
// TODO: Add subclass
LinearGradient *gradient = new LinearGradient(id);
init_gradient_(node, gradient);
// Parse attributes
const char* x1 = node.getAttribute("x1");
const char* y1 = node.getAttribute("y1");
const char* x2 = node.getAttribute("x2");
const char* y2 = node.getAttribute("y2");
if(x1) gradient->x1 = atof(std::string(x1).c_str());
if(y1) gradient->y1 = atof(std::string(y1).c_str());
if(x2) gradient->x2 = atof(std::string(x2).c_str());
if(y2) gradient->y2 = atof(std::string(y2).c_str());
gradients_[std::string(id)] = gradient;
}
//------------------------------------------------------------------------
// Parse a drawing command...
//
// TODO implement all commands
//
bool SVGparser::parse_cmd() {
// Relative offset
b2Vec2 offset;
parse_ws_();
switch (char c = *p++) {
// End of input
case '\0':
return false;
case 'M':
//move, absolute
while(parse_coord_pair_(&pos))
path->push(pos);
return true;
case 'm':
//move, relative
while(parse_coord_pair_(&offset)) {
pos += offset;
path->push(pos);
}
return true;
case 'L':
//Line, absolute
while (parse_coord_pair_(&pos))
path->push(pos);
return true;
case 'l':
// Line, relative
while (parse_coord_pair_(&offset)) {
pos += offset;
path->push(pos);
}
return true;
case 'A':
parse_arc_(false);
return true;
case 'a':
parse_arc_(true);
return true;
case 'C':
// cubic Bézier curve, absolute
curve_to_(false);
return true;
case 'c':
// cubic Bézier curve, relative
curve_to_(true);
return true;
case 'H': {
float32 coord;
// Horizontal lineto, absolute
while (parse_coord_(&coord)) {
pos.x = coord;
path->push(pos);
}
return true;
}
case 'h': {
float32 coord;
// Horizontal lineto, relative
while (parse_coord_(&coord)) {
pos.x += coord;
path->push(pos);
}
return true;
}
case 'V': {
float32 coord;
// Verticle lineto, absolute
while (parse_coord_(&coord)) {
pos.y = coord;
path->push(pos);
}
return true;
}
case 'v': {
float32 coord;
// Verticle lineto, relative
while (parse_coord_(&coord)) {
pos.y += coord;
path->push(pos);
}
return true;
}
case 'Z':
// closepath
// Fall through
case 'z':
// closepath
path->close();
return true;
default:
if(svg_debug) printf("Unimplemented/unknown SVGpath command '%c'\n", c);
while(*p && !isalpha(*p)) p++; // Skip the commmand
return true;
//return false;
}
}
void SVGparser::parse_arc_(bool offset) {
std::vector<float32> num;
float32 n;
while(parse_coord_(&n)) {
num.push_back(n);
}
int k = 0;
for(int j = 1; j <= num.size()/7; j++) {
b2Vec2 radii = b2Vec2(num[k], num[k+1]);
float32 phi = num[k+2];
bool large_arc = num[k+3] == 1 ? true : false;
bool sweep = num[k+4] == 1 ? true : false;
b2Vec2 off = offset ? pos : b2Vec2(0,0);
b2Vec2 end = off + b2Vec2(num[k+5], num[k+6]);
arc_to_(radii, phi, large_arc, sweep, end);
k+=7;
}
}
void SVGparser::curve_to_(bool offset) {
// Bezier points
Vec2Vec bez;
b2Vec2 point;
while(parse_coord_pair_(&point)) {
bez.push_back(point);
}
for(int i = 0; i < bez.size(); i+=3) {
b2Vec2 off = offset ? pos : b2Vec2(0.0, 0.0);
for (int j = 0; j < kBezierPoints; j++ ) {
float32 t = (float32)j/(kBezierPoints - 1);
cubic_bezier_(point, t, pos, bez[i]+off, bez[i+1]+off, bez[i+2]+off);
path->push(point);
}
line_to_(bez[i+2].x+off.x, bez[i+2].y+off.y);
}
}
/*
* Elliptical arc
* http://www.w3.org/TR/2003/REC-SVG11-20030114/implnote.html#ArcImplementationNotes
* Ported from Squirtle
*/
void SVGparser::arc_to_(b2Vec2 radii, float32 phi, bool large_arc, bool sweep, b2Vec2 end) {
float32 rx = radii.x;
float32 ry = radii.y;
float32 x1 = pos.x;
float32 y1 = pos.y;
float32 x2 = end.x;
float32 y2 = end.y;
float32 cp = cosf(phi);
float32 sp = sinf(phi);
float32 dx = .5 * (x1 - x2);
float32 dy = .5 * (y1 - y2);
float32 x_ = cp * dx + sp * dy;
float32 y_ = -sp * dx + cp * dy;
float32 r2 = (pow(rx * ry, 2) - pow(rx * y_,2) - pow(ry * x_,2)) /
(pow(rx * y_, 2) + pow(ry * x_,2));
if (r2 < 0) r2 = 0;
float32 r = sqrt(r2);
if (large_arc == sweep) r = -r;
float32 cx_ = r * rx * y_ / ry;
float32 cy_ = -r * ry * x_ / rx;
float32 cx = cp * cx_ - sp * cy_ + .5 * (x1 + x2);
float32 cy = sp * cx_ + cp * cy_ + .5 * (y1 + y2);
float32 psi = angle_(b2Vec2(1,0), b2Vec2((x_ - cx_)/rx, (y_ - cy_)/ry));
float32 delta = angle_(b2Vec2((x_ - cx_)/rx, (y_ - cy_)/ry),
b2Vec2((-x_ - cx_)/rx, (-y_ - cy_)/ry));
if (sweep && delta < 0) delta += b2_pi * 2;
if (!sweep && delta > 0) delta -= b2_pi * 2;
int n_points = b2Max(int(b2Abs(kCirclePoints * delta / (2 * b2_pi))), 1);
for(int i = 0; i < n_points; i++) {
float32 theta = psi + i * delta / n_points;
float32 ct = cos(theta);
float32 st = sin(theta);
line_to_(cp * rx * ct - sp * ry * st + cx,
sp * rx * ct + cp * ry * st + cy);
}
}
void SVGparser::parse_stop_style_(const char *style, GLubyte *stop) {
std::string k,v; // key, value
const char *p, *start;
p = start = style;
while (*p) {
if(*p == ':') {
k = std::string(start, p - start);
start = ++p;
while (*p && *p!=';') ++p;
v = std::string(start, p-start);
if(k=="stop-color") {
parse_color_(v, stop);
} else if(k=="stop-opacity") {
stop[3] = 255 * atof(v.c_str());
}
start = ++p;
continue;
}
++p;
}
}
//------------------------------------------------------------------------
bool SVGparser::parse_style_(const char *style) {
std::string k,v; // key, value
const char *p, *start;
p = start = style;
while (*p) {
if(*p == ':') {
k = std::string(start, p-start);
start = ++p;
while (*p && *p!=';') ++p;
v = std::string(start, p-start);
if(k=="fill") {
parse_color_(v, path->fill);
} else if(k=="stroke") {
parse_color_(v, path->stroke);
} else if(k=="fill-opacity") {
path->fill[3] = 255 * atof(v.c_str());
}
start = ++p;
continue;
}
++p;
}
return true;
}
void SVGparser::parse_color_(std::string s, GLubyte *color) {
GLubyte r, g, b = 0;
if(s=="none") {
return;
} else if(s[0]=='#') {
s = s.substr(1);
int clr = strtol(s.c_str(), NULL, 16);
switch(s.size()) {
case 3:
b = clr & 0xff;
clr >>= 8;
g = clr & 0xff;
clr >>= 8;
r = clr & 0xff;
break;
case 6:
b = clr & 0xff;
clr >>= 8;
g = clr & 0xff;
clr >>= 8;
r = clr & 0xff;
break;
default:
std::cerr << "Incorrect hex color length: '" << s << "'\n";
return;
}
} else if(s[0] == 'u') {
// Gradient color
s.erase(0, 5);
s.erase(s.size()-1, s.size()-1);
//std::cout << s << std::endl;
path->gradient = gradients_[s];
}
color[0] = r; color[1] = g; color[2] = b; color[3] = 255;
}
//------------------------------------------------------------------------
// Parse a single coordinate
//
bool SVGparser::parse_coord_(float32 *coord) {
char *endpos;
parse_ws_();
*coord = strtod(p, &endpos);
// If p is not a number then it must be a command, so exit
if(p == endpos) return false;
p = endpos;
return true;
}
//------------------------------------------------------------------------
// Parse a coordinate pair
//
bool SVGparser::parse_coord_pair_(b2Vec2 *pos) {
char *endpos;
float32 x,y;
parse_ws_();
x = strtod(p, &endpos);
// If p is not a number then it must be a command, so exit
if(p == endpos) return false;
p = endpos;
parse_ws_();
y = strtod(p, &endpos);
p = endpos;
pos->Set(x,y);
return true;
}
//------------------------------------------------------------------------