-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvg.c
455 lines (388 loc) · 11 KB
/
svg.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
// ======================================================================
// tom's SVG path parser 2010-04-03
//
// - Simple top-down parser/lexer in the style of 'META II' [Schorre 1964]
//
#include "svg.h"
//========================================================================
#include <ctype.h>
#include <stdio.h>
//#include "uthash/utstring.h"
#include "uthash/uthash.h"
// XXX CRUFT...
const float32 kMaxShipSize = 5; // Maximum ship size (meters)
const int kCirclePoints = 25; // Number of points in a circle
const int kBezierPoints = 20; // Number of points in a Bezier curve
//========================================================================
// Module-global state (nope, this isn't reentrant/threadsafe...)
FILE *input;
bool svg_debug = true;
float32 width, height;
// Little parser subroutines...
int fpeek(FILE *f) {
int c = fgetc(f);
ungetc(c, f);
return c;
}
void skip_whitespace() {
while( !feof(input) && isspace(fpeek(input)) )
fgetc(input);
}
bool is_sym(char c) {
return isalnum(c) || c=='"' || c=='\'' || c==':' || c=='.'
|| c=='*' || c=='/' || c=='%' || c=='+' || c=='-' || c=='_';
}
// Match a string
bool match(char *s) {
long pos = ftell(input);
int n = strlen(s);
int i;
for(i=0; i<n; i++) {
if( s[i] != fgetc(input) ) {
fseek(input, pos, SEEK_SET);
return false;
}
}
return true;
}
//========================================================================
// SVG tag parser subroutines
//========================================================================
//------------------------------------------------------------------------
// Traverse the XML element tree
//
void parse_svg() {
/*
//const char* name = node.getName();
//const char* transform = node.getAttribute("transform");
Matrix *old_transform = transform_;
//transform_ = mul(transform_, new Matrix(transform?std::string(transform):std::string("")));
// Parse current node
fnmap::iterator iter;
iter = vtable.find(name);
if(iter != vtable.end()) {
// Dispatch to the appropriate tag-handler
(this->*(iter->second))(node);
} else {
//TODO debug log...
if(svg_debug) printf("Ignoring '%s' element\n", name);
}
// Apply transform
if(path) {
path->transform = transform_;
path->apply_transform();
path->id = (char*) node.getAttribute("id");
color_style_(node);
path_list.push_back(path);
}
transform_ = old_transform;
// Process child nodes
int num_children = node.nChildNode();
for(int i = 0; i < num_children; i++) {
XMLNode child = node.getChildNode(i);
parse_svg(child, indent+1);
}
path = NULL; //TODO this is now 'curpath' in path.c
*/
}
//========================================================================
// Hash table
//========================================================================
typedef void(*fnptr)(); // Function Pointer
// hash map for looking up XML tag names
typedef struct {
char *name; // key (use HASH_ADD_KEYPTR)
fnptr fn; // value
UT_hash_handle hh;
} Fnmap;
Fnmap *fnmap = NULL;
void fnmap_add(char *name, fnptr fn) {
Fnmap *s = malloc(sizeof(Fnmap));
s->name = name;
s->fn = fn;
HASH_ADD_KEYPTR( hh, fnmap, s->name, strlen(s->name), s );
}
void init_fnmap() {
fnmap_add("svg", parse_svg);
//fnmap_add("path", parse_path_);
//fnmap_add("rect", parse_rect_);
//fnmap_add("circle", parse_circle_);
//fnmap_add("radialGradient", parse_radial_gradient_);
//fnmap_add("linearGradient", parse_linear_gradient_);
}
//========================================================================
// XML parsing
//========================================================================
bool parse_xml_comment() {
//comment = '<!--' ([^-] | '-' . [^-])* '-->' IGNORE
if(!match("<!--")) return false;
for(;;) {
int c = fgetc(input);
if(c=='-') {
if(match("->")) {
printf("PARSED A COMMENT\n");
return true;
}
}
}
}
//------------------------------------------------------------------------
char* parse_xml_name() {
char *buf;
size_t bufsize;
FILE *out = open_memstream(&buf, &bufsize);
if (out == NULL) {
perror("open_memstream");
exit(1);
}
int c = fgetc(input);
//printf("--> '%c'\n", c);
if (isalpha(c) || c=='_' || c==':') {
// Other chars (_ and : aren't
while (isalnum(c) || c=='_' || c==':' || c=='.' || c=='-') {
fputc(c, out);
c = fgetc(input);
//printf("--> '%c'\n", c);
}
ungetc(c, input);
fclose(out);
//printf("Parsed tag name <%s>\n", buf);
return buf;
}
else {
ungetc(c, input);
fclose(out);
free(buf);
return NULL;
}
}
// Parse over (skip) extraneous data
bool skip_attrs() {
skip_whitespace();
if(parse_xml_name() == NULL) {
return false;
}
skip_whitespace();
// '='
int c = fgetc(input);
skip_whitespace();
// '"'
c = fgetc(input);
for(;;) {
c = fgetc(input);
if (c == '"') break;
}
return true;
}
//------------------------------------------------------------------------
// Parse and return xml attribute name and value
bool parse_xml_attr(char **name, char **value) {
skip_whitespace();
// attribute name
char *k = parse_xml_name();
if(k == NULL) {
return false;
}
skip_whitespace();
// '='
int c = fgetc(input);
if (c != '=') {
printf("EXPECTED '='\n");
return false;
}
skip_whitespace();
char *buf;
size_t bufsize;
FILE *out = open_memstream(&buf, &bufsize);
if (out == NULL) {
perror("open_memstream");
exit(1);
}
// attribute value (quoted string)
c = fgetc(input);
switch (c) {
case '"':
for(;;) {
c = fgetc(input);
//TODO parse &...; XML entity refs
if (c == '"') break;
fputc(c, out);
}
break;
case '\'':
for(;;) {
c = fgetc(input);
//TODO parse &...; XML entity refs
if (c == '\'') break;
fputc(c, out);
}
break;
default:
fclose(out); free(buf);
printf("EXPECTED xml attr value (quoted string)\n");
return false;
}
fclose(out);
*name = k;
*value = buf;
return true;
}
bool parse_svg_path(Sprite *sprite, Path *path) {
char *name, *value;
while(parse_xml_attr(&name, &value));
}
//------------------------------------------------------------------------
bool parse_xml_attrs(Sprite *sprite, const char *element) {
char *name, *value;
// Extract height and width data from svg
if(!strcmp(element, "svg")) {
while(parse_xml_attr(&name, &value)) {
if(!strcmp(name, "width")) {
sprite->width = atof(value);
} else if (!strcmp(name, "height")) {
sprite->height = atof(value);
}
}
} else if(!strcmp(element, "g")) {
// Extract skeleton from group
// Maybe there's a smarter way to do the skeleton rather than manual
while(parse_xml_attr(&name, &value)) {
if(!strcmp(name, "id")) {
if(!strcmp(value, "skeleton")) {
printf("**** found the skeleton! ****\n");
}
}
}
} else if(!strcmp(element, "path")) {
Path *path = path_new();
parse_svg_path(sprite, path);
} else {
// Parse and skip everything else
while(skip_attrs());
}
return true;
}
//------------------------------------------------------------------------
bool parse_xml_pi() {
// Parse a program instruction ("<?... ?>") and IGNORE IT
if(!match("<?")) return false;
//printf("GOT <?\n");
free(parse_xml_name());
//printf("GOT name\n");
while(skip_attrs());
//printf("GOT attrs\n");
if(!match("?>")) return false;
//printf("GOT ?>\n");
return true;
}
//------------------------------------------------------------------------
bool parse_xml_misc() {
// Parse "misc" - comments and PIs
do skip_whitespace();
while(parse_xml_pi() || parse_xml_comment());
return true;
}
//------------------------------------------------------------------------
bool parse_xml_prolog() {
// <?xml ... ?> header
if (!parse_xml_pi()) return false;
// Comments and PIs
parse_xml_misc();
return true;
}
//------------------------------------------------------------------------
bool parse_xml_element(); // forward ref
bool parse_xml_content() {
for(;;) {
int c = fgetc(input);
if(c=='<') {
int c = fgetc(input);
fseek(input, -2, SEEK_CUR);
if(c=='/') {
return true;
}
parse_xml_element();
}
}
}
//------------------------------------------------------------------------
bool parse_xml_element(Sprite *sprite) {
if(fgetc(input) != '<') return false;
char *element = parse_xml_name();
printf("BEGIN <%s> TAG\n", element);
parse_xml_attrs(sprite, element);
if(match("/>")) {
printf("PARSED EMPTY <%s/> TAG\n", element);
return true;
}
if(!match(">")) {
printf("EXPECTED '>' TO CLOSE <%s> TAG\n", element);
return false;
}
printf("PARSING CONTENT OF <%s> TAG\n", element);
parse_xml_content();
if(! (match("</") && match(element) && match(">"))) {
printf("EXPECTED </%s> CLOSING TAG\n", element);
return false;
}
return true;
}
//------------------------------------------------------------------------
// Parses the given SVG file and stores it in the supplied Sprite.
// Returns true on success.
//
bool svg_load(const char *filename, float32 scale, Sprite *sprite) {
if(svg_debug) {
printf("===============================================================\n");
printf("Loading %s scale=%f\n", filename, scale);
printf("===============================================================\n");
}
//TODO new XML parser....
//XMLNode top = XMLNode::openFileHelper(filename).getChildNode("svg");
input = fopen(filename, "r");
if (!input) {
perror("unable to open input file");
return false;
}
if (!(parse_xml_prolog())) {
printf("XML prologue not parsed\n");
return false;
}
if (!parse_xml_element(sprite)) {
printf("FAILED to parse XML body (i.e. <svg>...</svg>\n");
return false;
}
printf("FINISHED PARSING SVG\n");
#if 0
const char *width_s = top.getAttribute("width");
const char *height_s = top.getAttribute("height");
double width = atof(width_s);
double height = atof(height_s);
// This flips from SVG cord. space to OpenGL/world cord. space
transform_ = new Matrix(1, 0, 0, -1, 0, height);
printf("w = %f, h = %f\n", width, height);
// Parse SVG paths into a temporary list
path_list.clear();
parse_svg(top, 0);
skin->path_list = path_list;
// Render to a GL display list
GLuint displist = glGenLists(1);
if(!displist) {
std::cerr << "WARNING: Could not allocate displist in SVGparser::load()\n";
}
else {
glNewList(displist, GL_COMPILE);
glRotatef(180, 0, 0, 1);
glScaled(scale, scale, scale);
PathList::iterator x = skin->path_list.begin();
while(x != skin->path_list.end()) {
(*x)->render();
++x;
}
glEndList();
}
skin->displist = displist;
#endif
return true;
}
//========================================================================