Debian Codegraph @

converter/other/svgtopam.c

raw
1 /*============================================================================
2 svgtopam
3 ==============================================================================
4 This is not useful today. It is merely a stub from which someone who
5 cares about SVG can build a full converter.
6
7 The framework is all there; it should be just a matter of coding to
8 add each of the SVG features to it.
9
10 Today, the program works fine on an image that consists solely of
11 <path> elements, which use only the "M", "L", and "z" commands.
12
13 By Bryan Henderson, San Jose, California. May 2006
14
15 Contributed to the public domain.
16
17 ==============================================================================
18
19 Implementation notes:
20
21 A look at Libxml2 Subversion source code change history says the type
22 'xmlReaderTypes' was added (in <libxml/xmlreader.h>) in 2.5.9. But a MacOS
23 10.3.9 user reports in April 2007 that he has 2.6.16 installed and it
24 doesn't have xmlReaderTypes. Another MacOS user reported that in December
25 2008. Apparently that OS has a broken libxml2 installation.
26
27 ============================================================================*/
28
29 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
30 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
31 #define _BSD_SOURCE /* Make sure strdup() is in <string.h> */
32 #define _POSIX_SOURCE /* Make sure fileno() is in <stdio.h> */
33 #include <assert.h>
34 #include <string.h>
35 #include <stdio.h>
36
37 #include <libxml/xmlreader.h>
38
39 #include "pm_c_util.h"
40 #include "mallocvar.h"
41 #include "nstring.h"
42 #include "shhopt.h"
43 #include "pam.h"
44 #include "ppm.h"
45 #include "ppmdraw.h"
46
47
48 static bool traceDraw;
49
50 struct cmdlineInfo {
51 const char * inputFileName;
52 unsigned int trace;
53 };
54
55
56
57 static void
58 parseCommandLine(int argc,
59 char ** argv,
60 struct cmdlineInfo * const cmdlineP) {
61 /* --------------------------------------------------------------------------
62 Parse program command line described in Unix standard form by argc
63 and argv. Return the information in the options as *cmdlineP.
64
65 If command line is internally inconsistent (invalid options, etc.),
66 issue error message to stderr and abort program.
67
68 Note that the strings we return are stored in the storage that
69 was passed to us as the argv array. We also trash *argv.
70 --------------------------------------------------------------------------*/
71 optEntry *option_def;
72 /* Instructions to pm_optParseOptions3 on how to parse our options. */
73 optStruct3 opt;
74
75 unsigned int option_def_index;
76
77 MALLOCARRAY_NOFAIL(option_def, 100);
78
79 option_def_index = 0; /* incremented by OPTENT3 */
80 OPTENT3(0, "trace", OPT_FLAG, NULL,
81 &cmdlineP->trace, 0);
82
83 opt.opt_table = option_def;
84 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
85 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
86
87 pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
88 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
89
90 if (argc-1 < 1)
91 cmdlineP->inputFileName = "-";
92 else {
93 cmdlineP->inputFileName = argv[1];
94
95 if (argc-1 > 1)
96 pm_error("Too many arguments (%u). The only non-option argument "
97 "is the input file name.", argc-1);
98 }
99 }
100
101
102
103 /*============================================================================
104 Wrappers for libxml2 routines.
105
106 The difference is that these use conventional C data types, have shorter
107 names, and abort the program instead of returning a special value when they
108 fail.
109 =============================================================================*/
110
111 static const char *
112 getAttribute(xmlTextReaderPtr const xmlReaderP,
113 const char * const attributeName) {
114
115 const char * const rc = (const char *)
116 xmlTextReaderGetAttribute(xmlReaderP, (const xmlChar *)attributeName);
117
118 if (rc == NULL)
119 pm_error("xmlTextReaderGetAttribute(\"%.256s\") failed. ",
120 attributeName);
121
122 return rc;
123 }
124
125
126
127 static const char *
128 currentNodeName(xmlTextReaderPtr const xmlReaderP) {
129
130 const char * const rc = (const char *)
131 xmlTextReaderConstName(xmlReaderP);
132
133 if (rc == NULL)
134 pm_error("xmlTextReaderConstName() failed. ");
135
136 return rc;
137 }
138
139
140
141 /*===========================================================================*/
142
143 #define OUTPUT_MAXVAL 255
144
145 typedef struct {
146 unsigned int width;
147 unsigned int height;
148 pixel ** pixels;
149 pixval maxval;
150 } Canvas;
151
152 typedef struct {
153 pixel fillColor;
154 } Style;
155
156
157
158 typedef struct {
159 const char * pathText;
160 /* This is e.g. "M0 0 L1 1 L9 8 Z" */
161 Style style;
162 /* This is the style as given by a 'style' attribute of <path> */
163 unsigned int pathTextLength;
164 /* This is the length in characters of 'pathText'. It's redundant
165 with 'pathText' and exists for convenience.
166 */
167 } Path;
168
169 static void
170 createPath(const char * const pathText,
171 Style const style,
172 Path ** const pathPP) {
173 /*----------------------------------------------------------------------------
174 Create a path as described by a <path> element whose "style" attribute
175 indicates style 'style' and whose "d" attribute indicates path data
176 'pathText'.
177 -----------------------------------------------------------------------------*/
178 bool error;
179 Path * pathP;
180
181 MALLOCVAR(pathP);
182 if (pathP == NULL)
183 error = TRUE;
184 else {
185 pathP->style = style;
186
187 pathP->pathText = strdup(pathText);
188 if (pathP->pathText == NULL)
189 error = TRUE;
190 else {
191 pathP->pathTextLength = strlen(pathP->pathText);
192
193 error = FALSE;
194 }
195 if (error)
196 free(pathP);
197 }
198 if (error )
199 *pathPP = NULL;
200 else
201 *pathPP = pathP;
202 }
203
204
205
206 static void
207 destroyPath(Path * const pathP) {
208
209 assert(pathP->pathTextLength == strlen(pathP->pathText));
210
211 pm_strfree(pathP->pathText);
212
213 free(pathP);
214 }
215
216
217
218 typedef struct {
219 unsigned int x;
220 unsigned int y;
221 } Point;
222
223 static Point
224 makePoint(unsigned int const x,
225 unsigned int const y) {
226
227 Point p;
228
229 p.x = x;
230 p.y = y;
231
232 return p;
233 }
234
235
236
237 static ppmd_point
238 makePpmdPoint(Point const arg) {
239
240 ppmd_point p;
241
242 p.x = arg.x;
243 p.y = arg.y;
244
245 return p;
246 }
247
248
249
250 typedef enum {
251 PATH_MOVETO,
252 PATH_LINETO,
253 PATH_CLOSEPATH,
254 PATH_CUBIC
255 } PathCommandVerb;
256
257 typedef struct {
258 Point dest;
259 } PathMovetoArgs;
260
261 typedef struct {
262 /* Draw a line segment from current point to 'dest' */
263 Point dest;
264 } PathLinetoArgs;
265
266 typedef struct {
267 /* Draw a cubic spline from current point to 'dest' with control points
268 'ctl1' at the beginning of the curve and 'ctl2' at the end.
269
270 I.e. it's a section of a cubic curve which passes through the
271 current point and 'dest' and whose slope at the current point
272 is that of the line through the current point and 'ctl1' and
273 whose slope at 'dest' is that of the line through 'dest' and
274 'ctl2';
275
276 A cubic curve is a plot of a polynomial equation of degree 3
277 (or less, for our purposes).
278 */
279 Point dest;
280 Point ctl1;
281 Point ctl2;
282 } PathCubicArgs;
283
284 typedef struct {
285 PathCommandVerb verb;
286 union {
287 PathMovetoArgs moveto;
288 PathLinetoArgs lineto;
289 PathCubicArgs cubic;
290 } args;
291 } PathCommand;
292
293
294
295 typedef struct {
296 /*----------------------------------------------------------------------------
297 This is an object for reading through a path from beginning to end.
298 -----------------------------------------------------------------------------*/
299 Path * pathP;
300 unsigned int cursor;
301 } PathReader;
302
303 static void
304 pathReader_create(Path * const pathP,
305 PathReader ** const pathReaderPP) {
306
307 PathReader * pathReaderP;
308
309 MALLOCVAR_NOFAIL(pathReaderP);
310
311 pathReaderP->pathP = pathP;
312 pathReaderP->cursor = 0;
313
314 *pathReaderPP = pathReaderP;
315 }
316
317
318
319 static void
320 pathReader_destroy(PathReader * const pathReaderP) {
321 free(pathReaderP);
322 }
323
324
325
326 static const char *
327 pathReader_context(PathReader * const pathReaderP) {
328
329 const char * retval;
330
331 pm_asprintf(&retval, "Character position %u (starting at 0) in '%s'",
332 pathReaderP->cursor, pathReaderP->pathP->pathText);
333
334 return retval;
335 }
336
337
338
339 static void
340 pathReader_skipWhiteSpace(PathReader * const pathReaderP) {
341 /*----------------------------------------------------------------------------
342 Move the cursor over any white space where it now points.
343 -----------------------------------------------------------------------------*/
344 const Path * const pathP = pathReaderP->pathP;
345
346 while (isspace(pathP->pathText[pathReaderP->cursor]) &&
347 pathReaderP->cursor < pathP->pathTextLength)
348 ++pathReaderP->cursor;
349 }
350
351
352
353 static void
354 pathReader_getNumber(PathReader * const pathReaderP,
355 unsigned int * const numberP) {
356
357 const Path * const pathP = pathReaderP->pathP;
358 const char * const pathText = pathP->pathText;
359 size_t const pathTextLength = pathP->pathTextLength;
360
361 assert(!isspace(pathText[pathReaderP->cursor]));
362
363 if (pathReaderP->cursor >= pathTextLength)
364 pm_error("Path description ends where a number was expected.");
365 else if (!isdigit(pathText[pathReaderP->cursor])) {
366 pm_error("Character '%c' instead of a digit where number expected",
367 pathText[pathReaderP->cursor]);
368 } else {
369 unsigned int number;
370
371 number = 0; /* initial value */
372
373 while (pathReaderP->cursor < pathTextLength &&
374 isdigit(pathText[pathReaderP->cursor])) {
375 number = 10 * number + (pathText[pathReaderP->cursor] - '0');
376 ++pathReaderP->cursor;
377 }
378 if (pathText[pathReaderP->cursor] == '.')
379 pm_error("Number contains decimal point. This program does not "
380 "know how to deal with fractional positions");
381
382 *numberP = number;
383 }
384 }
385
386
387
388 static void
389 pathReader_getNextCommand(PathReader * const pathReaderP,
390 PathCommand * const pathCommandP,
391 bool * const endOfPathP) {
392
393 const Path * const pathP = pathReaderP->pathP;
394 const char * const pathText = pathP->pathText;
395 size_t const pathTextLength = pathP->pathTextLength;
396
397 pathReader_skipWhiteSpace(pathReaderP);
398
399 if (pathReaderP->cursor >= pathTextLength)
400 *endOfPathP = true;
401 else {
402 switch (pathText[pathReaderP->cursor++]) {
403 case 'M':
404 pathCommandP->verb = PATH_MOVETO;
405 pathReader_skipWhiteSpace(pathReaderP);
406 pathReader_getNumber(pathReaderP,
407 &pathCommandP->args.moveto.dest.x);
408 pathReader_skipWhiteSpace(pathReaderP);
409 pathReader_getNumber(pathReaderP,
410 &pathCommandP->args.moveto.dest.y);
411 break;
412 case 'L':
413 pathCommandP->verb = PATH_LINETO;
414 pathReader_skipWhiteSpace(pathReaderP);
415 pathReader_getNumber(pathReaderP,
416 &pathCommandP->args.lineto.dest.x);
417 pathReader_skipWhiteSpace(pathReaderP);
418 pathReader_getNumber(pathReaderP,
419 &pathCommandP->args.lineto.dest.y);
420 break;
421 case 'C':
422 pathCommandP->verb = PATH_CUBIC;
423 pathReader_skipWhiteSpace(pathReaderP);
424 pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl1.x);
425 pathReader_skipWhiteSpace(pathReaderP);
426 pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl1.y);
427 pathReader_skipWhiteSpace(pathReaderP);
428 pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl2.x);
429 pathReader_skipWhiteSpace(pathReaderP);
430 pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.ctl2.y);
431 pathReader_skipWhiteSpace(pathReaderP);
432 pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.dest.x);
433 pathReader_skipWhiteSpace(pathReaderP);
434 pathReader_getNumber(pathReaderP, &pathCommandP->args.cubic.dest.y);
435 break;
436 case 'z':
437 pathCommandP->verb = PATH_CLOSEPATH;
438 break;
439 default: {
440 const char * const context = pathReader_context(pathReaderP);
441
442 pm_errormsg("Unrecognized command in <path>: '%c'. %s",
443 pathText[pathReaderP->cursor++], context);
444
445 pm_strfree(context);
446
447 pm_longjmp();
448 }
449 }
450 }
451 }
452
453
454
455 static void
456 outlineObject(Path * const pathP,
457 struct fillobj * const fillObjP) {
458 /*----------------------------------------------------------------------------
459 Create a fill object, which contains and outline of the object and
460 can be used with ppmd_fill() to fill the figure. The outline is as
461 described by *pathP.
462 -----------------------------------------------------------------------------*/
463 PathReader * pathReaderP;
464 bool endOfPath;
465 Point currentPos;
466 Point subpathStart;
467 /* Point at which the current subpath starts */
468
469 endOfPath = false;
470 subpathStart = makePoint(0,0);
471 currentPos = subpathStart;
472
473 pathReader_create(pathP, &pathReaderP);
474
475 while (!endOfPath) {
476 PathCommand pathCommand;
477 pathReader_getNextCommand(pathReaderP, &pathCommand, &endOfPath);
478 if (!endOfPath) {
479 switch (pathCommand.verb) {
480 case PATH_MOVETO:
481 if (traceDraw)
482 pm_message("Moving to (%u, %u)",
483 pathCommand.args.moveto.dest.x,
484 pathCommand.args.moveto.dest.y);
485 subpathStart = pathCommand.args.moveto.dest;
486 currentPos = subpathStart;
487 break;
488 case PATH_LINETO: {
489 Point const dest = pathCommand.args.lineto.dest;
490 if (traceDraw)
491 pm_message("Lining to (%u, %u)", dest.x, dest.y);
492 ppmd_line(NULL, 0, 0, 0,
493 currentPos.x, currentPos.y, dest.x, dest.y,
494 ppmd_fill_drawproc, fillObjP);
495 currentPos = dest;
496 } break;
497 case PATH_CLOSEPATH:
498 if (traceDraw)
499 pm_message("Closing.");
500 ppmd_line(NULL, 0, 0, 0,
501 currentPos.x, currentPos.y,
502 subpathStart.x, subpathStart.y,
503 ppmd_fill_drawproc, fillObjP);
504 currentPos = subpathStart;
505 break;
506 case PATH_CUBIC: {
507 Point const dest = pathCommand.args.cubic.dest;
508 Point const ctl1 = pathCommand.args.cubic.ctl1;
509 Point const ctl2 = pathCommand.args.cubic.ctl2;
510 if (traceDraw)
511 pm_message("Doing cubic spline to (%u, %u)",
512 dest.x, dest.y);
513 pm_error("SVG image contains a cubic spline path. "
514 "This program cannot process cubic splines.");
515 /* We need to write ppmd_spline4() */
516 ppmd_spline4p(NULL, 0, 0, 0,
517 makePpmdPoint(currentPos),
518 makePpmdPoint(dest),
519 makePpmdPoint(ctl1),
520 makePpmdPoint(ctl2),
521 ppmd_fill_drawprocp, fillObjP);
522 currentPos = dest;
523 } break;
524 }
525 }
526 }
527 pathReader_destroy(pathReaderP);
528 }
529
530
531
532 static void
533 drawPath(Canvas * const canvasP,
534 Path * const pathP) {
535 /*----------------------------------------------------------------------------
536 Draw the path 'pathP' on the canvas 'canvasP'.
537 -----------------------------------------------------------------------------*/
538 struct fillobj * fillObjP;
539
540 if (traceDraw)
541 pm_message("Drawing path '%s' with fill color (%u, %u, %u)",
542 pathP->pathText,
543 pathP->style.fillColor.r,
544 pathP->style.fillColor.g,
545 pathP->style.fillColor.b);
546
547 fillObjP = ppmd_fill_create();
548
549 outlineObject(pathP, fillObjP);
550
551 ppmd_fill(canvasP->pixels, canvasP->width, canvasP->height,
552 canvasP->maxval,
553 fillObjP,
554 PPMD_NULLDRAWPROC, &pathP->style.fillColor);
555
556 ppmd_fill_destroy(fillObjP);
557 }
558
559
560
561 static Style
562 interpretStyle(const char * const styleAttr) {
563
564 Style style;
565
566 char * buffer;
567
568 char * p;
569
570 buffer = strdup(styleAttr);
571 if (buffer == NULL)
572 pm_error("Could not get memory for a buffer to parse style attribute");
573
574 p = &buffer[0];
575
576 while (p) {
577 const char * const token = pm_strsep(&p, ";");
578 const char * strippedToken;
579 const char * p;
580 char * buffer;
581
582 for (p = &token[0]; isspace(*p); ++p);
583
584 strippedToken = p;
585
586 buffer = strdup(strippedToken);
587
588 if (strlen(strippedToken) > 0) {
589 char * const colonPos = strchr(buffer, ':');
590
591 if (colonPos == NULL)
592 pm_error("There is no colon in the attribute specification "
593 "'%s' in the 'style' attribute of a <path> "
594 "element.", strippedToken);
595 else {
596 const char * const value = colonPos + 1;
597 const char * const name = &buffer[0];
598
599 *colonPos = '\0';
600
601 if (streq(name, "fill")) {
602 style.fillColor = ppm_parsecolor(value, OUTPUT_MAXVAL);
603 } else if (streq(name, "stroke")) {
604 if (!streq(value, "none"))
605 pm_error("Value of 'stroke' attribute in the 'style' "
606 "attribute of a <path> element is '%s'. We "
607 "understand only 'none'", value);
608 } else
609 pm_error("Unrecognized attribute '%s' "
610 "in the 'style' attribute "
611 "of a <path> element", name);
612 }
613 }
614 free(buffer);
615 }
616
617 free(buffer);
618
619 return style;
620 }
621
622
623
624 static void
625 getPathAttributes(xmlTextReaderPtr const xmlReaderP,
626 Style * const styleP,
627 const char ** const pathP) {
628
629 const char * const style = getAttribute(xmlReaderP, "style");
630 const char * const d = getAttribute(xmlReaderP, "d");
631
632 *styleP = interpretStyle(style);
633 *pathP = d;
634 }
635
636
637
638 static void
639 processSubPathNode(xmlTextReaderPtr const xmlReaderP,
640 bool * const endOfPathP) {
641
642 /* See comment above about xmlReaderTypes not being defined */
643
644 xmlReaderTypes const nodeType = xmlTextReaderNodeType(xmlReaderP);
645
646 *endOfPathP = FALSE; /* initial assumption */
647
648 switch (nodeType) {
649 case XML_READER_TYPE_ELEMENT:
650 pm_error("<path> contains a <%s> element. <path> should have "
651 "no contents", currentNodeName(xmlReaderP));
652 break;
653 case XML_READER_TYPE_END_ELEMENT: {
654 const char * nodeName = currentNodeName(xmlReaderP);
655 if (streq(nodeName, "path"))
656 *endOfPathP = TRUE;
657 else
658 pm_error("</%s> found where </path> expected", nodeName);
659 } break;
660 default:
661 /* Just ignore whatever this is. Contents of <path> are
662 meaningless; all the information is in the attributes
663 */
664 break;
665 }
666 }
667
668
669
670 static void
671 processPathElement(xmlTextReaderPtr const xmlReaderP,
672 Canvas * const canvasP) {
673
674 Style style;
675 const char * pathData;
676 Path * pathP;
677 bool endOfPath;
678
679 assert(xmlTextReaderNodeType(xmlReaderP) == XML_READER_TYPE_ELEMENT);
680 assert(streq(currentNodeName(xmlReaderP), "path"));
681
682 getPathAttributes(xmlReaderP, &style, &pathData);
683
684 createPath(pathData, style, &pathP);
685
686 if (pathP) {
687 drawPath(canvasP, pathP);
688 destroyPath(pathP);
689 }
690
691 endOfPath = xmlTextReaderIsEmptyElement(xmlReaderP);
692
693 while (!endOfPath) {
694 int rc;
695
696 rc = xmlTextReaderRead(xmlReaderP);
697
698 switch (rc) {
699 case 1:
700 processSubPathNode(xmlReaderP, &endOfPath);
701 break;
702 case 0:
703 pm_error("Input file ends in the middle of a <path>");
704 break;
705 default:
706 pm_error("xmlTextReaderRead() failed, rc=%d", rc);
707 }
708 }
709 }
710
711
712
713 static void
714 getSvgAttributes(xmlTextReaderPtr const xmlReaderP,
715 unsigned int * const colsP,
716 unsigned int * const rowsP) {
717
718 const char * const width = getAttribute(xmlReaderP, "width");
719 const char * const height = getAttribute(xmlReaderP, "height");
720
721 const char * error;
722
723 pm_string_to_uint(width, colsP, &error);
724 if (error) {
725 pm_error("'width' attribute of <svg> has invalid value '%s'. %s",
726 width, error);
727 pm_strfree(error);
728 }
729 pm_string_to_uint(height, rowsP, &error);
730 if (error) {
731 pm_error("'height' attribute of <svg> has invalid value '%s'. %s",
732 height, error);
733 pm_strfree(error);
734 }
735 }
736
737
738
739 static void
740 processSubSvgElement(xmlTextReaderPtr const xmlReaderP,
741 Canvas * const canvasP) {
742
743 const char * const nodeName = currentNodeName(xmlReaderP);
744
745 assert(xmlTextReaderNodeType(xmlReaderP) == XML_READER_TYPE_ELEMENT);
746
747 if (streq(nodeName, "path"))
748 processPathElement(xmlReaderP, canvasP);
749 else
750 pm_error("This image contains a <%s> element. This program "
751 "understands only <path>!", nodeName);
752 }
753
754
755
756 static void
757 processSubSvgNode(xmlTextReaderPtr const xmlReaderP,
758 Canvas * const canvasP,
759 bool * const endOfSvgP) {
760
761 xmlReaderTypes const nodeType = xmlTextReaderNodeType(xmlReaderP);
762
763 *endOfSvgP = FALSE; /* initial assumption */
764
765 switch (nodeType) {
766 case XML_READER_TYPE_ELEMENT:
767 processSubSvgElement(xmlReaderP, canvasP);
768 break;
769 case XML_READER_TYPE_END_ELEMENT: {
770 const char * const nodeName = currentNodeName(xmlReaderP);
771 if (streq(nodeName, "svg"))
772 *endOfSvgP = TRUE;
773 else
774 pm_error("</%s> found where </svg> expected", nodeName);
775 } break;
776 default:
777 /* Just ignore whatever this is */
778 break;
779 }
780 }
781
782
783
784 static void
785 createCanvas(unsigned int const width,
786 unsigned int const height,
787 pixval const maxval,
788 Canvas ** const canvasPP) {
789
790 Canvas * canvasP;
791
792 MALLOCVAR_NOFAIL(canvasP);
793
794 canvasP->width = width;
795 canvasP->height = height;
796 canvasP->pixels = ppm_allocarray(width, height);
797 canvasP->maxval = maxval;
798
799 *canvasPP = canvasP;
800 }
801
802
803
804 static void
805 destroyCanvas(Canvas * const canvasP) {
806
807 ppm_freearray(canvasP->pixels, canvasP->height);
808
809 free(canvasP);
810 }
811
812
813
814 static void
815 writePam(FILE * const ofP,
816 Canvas * const canvasP) {
817
818 unsigned int row;
819 struct pam pam;
820 tuple * tuplerow;
821
822 pam.size = sizeof(pam);
823 pam.len = PAM_STRUCT_SIZE(tuple_type);
824 pam.file = ofP;
825 pam.format = PAM_FORMAT;
826 pam.plainformat = 0;
827 pam.width = canvasP->width;
828 pam.height = canvasP->height;
829 pam.depth = 3;
830 pam.maxval = OUTPUT_MAXVAL;
831 strcpy(pam.tuple_type, PAM_PPM_TUPLETYPE);
832
833 pnm_writepaminit(&pam);
834
835 tuplerow = pnm_allocpamrow(&pam);
836
837 for (row = 0; row < (unsigned)pam.height; ++row) {
838 pixel * const pixelrow = canvasP->pixels[row];
839 unsigned int col;
840
841 for (col = 0; col < (unsigned)pam.width; ++col) {
842 pixel const thisPixel = pixelrow[col];
843 assert(pam.depth >= 3);
844
845 tuplerow[col][PAM_RED_PLANE] = PPM_GETR(thisPixel);
846 tuplerow[col][PAM_GRN_PLANE] = PPM_GETG(thisPixel);
847 tuplerow[col][PAM_BLU_PLANE] = PPM_GETB(thisPixel);
848 }
849 pnm_writepamrow(&pam, tuplerow);
850 }
851 pnm_freepamrow(tuplerow);
852 }
853
854
855
856 static void
857 processSvgElement(xmlTextReaderPtr const xmlReaderP,
858 FILE * const ofP) {
859
860 unsigned int width, height;
861 bool endOfSvg;
862 Canvas * canvasP;
863
864 assert(xmlTextReaderNodeType(xmlReaderP) == XML_READER_TYPE_ELEMENT);
865 assert(streq(currentNodeName(xmlReaderP), "svg"));
866
867 getSvgAttributes(xmlReaderP, &width, &height);
868
869 createCanvas(width, height, 255, &canvasP);
870
871 endOfSvg = xmlTextReaderIsEmptyElement(xmlReaderP);
872
873 while (!endOfSvg) {
874 int rc;
875
876 rc = xmlTextReaderRead(xmlReaderP);
877
878 switch (rc) {
879 case 1:
880 processSubSvgNode(xmlReaderP, canvasP, &endOfSvg);
881 break;
882 case 0:
883 pm_error("Input file ends in the middle of a <svg>");
884 break;
885 default:
886 pm_error("xmlTextReaderRead() failed, rc=%d", rc);
887 }
888 }
889 writePam(ofP, canvasP);
890
891 destroyCanvas(canvasP);
892 }
893
894
895
896 static void
897 processTopLevelElement(xmlTextReaderPtr const xmlReaderP,
898 FILE * const ofP) {
899
900 const char * const nodeName = currentNodeName(xmlReaderP);
901
902 assert(xmlTextReaderNodeType(xmlReaderP) == XML_READER_TYPE_ELEMENT);
903 assert(xmlTextReaderDepth(xmlReaderP) == 0);
904
905 if (!streq(nodeName, "svg"))
906 pm_error("Not an SVG image. This XML document consists of "
907 "a <%s> element, whereas an SVG image is an <svg> "
908 "element.", nodeName);
909 else
910 processSvgElement(xmlReaderP, ofP);
911 }
912
913
914
915 static void
916 processTopLevelNode(xmlTextReaderPtr const xmlReaderP,
917 FILE * const ofP) {
918
919 assert(xmlTextReaderDepth(xmlReaderP) == 0);
920
921 switch (xmlTextReaderNodeType(xmlReaderP)) {
922 case XML_READER_TYPE_ELEMENT:
923 processTopLevelElement(xmlReaderP, ofP);
924 break;
925 default:
926 /* Just ignore whatever this is */
927 break;
928 }
929 }
930
931
932
933 static void
934 processDocument(xmlTextReaderPtr const xmlReaderP,
935 FILE * const ofP) {
936
937 bool eof;
938
939 eof = false;
940
941 while (!eof) {
942 int rc;
943
944 rc = xmlTextReaderRead(xmlReaderP);
945
946 switch (rc) {
947 case 1:
948 processTopLevelNode(xmlReaderP, ofP);
949 break;
950 case 0:
951 eof = true;
952 break;
953 default:
954 pm_error("xmlTextReaderRead() failed, rc=%d", rc);
955 }
956 }
957 }
958
959
960
961 int
962 main(int argc, char **argv) {
963
964 struct cmdlineInfo cmdline;
965 FILE * ifP;
966 xmlTextReaderPtr xmlReaderP;
967
968 pnm_init(&argc, argv);
969
970 xmlInitParser();
971
972 LIBXML_TEST_VERSION;
973
974 parseCommandLine(argc, argv, &cmdline);
975
976 traceDraw = cmdline.trace;
977
978 ifP = pm_openr(cmdline.inputFileName);
979
980 xmlReaderP = xmlReaderForFd(fileno(ifP), "SVG_IMAGE", NULL, 0);
981 /* 'xmlReaderForFd' issues an error message to Standard Error if it
982 cannot interpret the XML document on *ifP; that message identifies
983 the document as "SVG_IMAGE". It issues the bogus message
984 "Extra content at the end of the document" if the document is
985 empty. It issues the bogus message "Document is empty" if the
986 file starts with something other than an XML element.
987 libxml2 2.9.10 23.10.09
988 */
989
990 if (xmlReaderP) {
991 processDocument(xmlReaderP, stdout);
992
993 /* xmlTextReaderIsValid() does not appear to work. It always says
994 the document is invalid
995 */
996
997 xmlFreeTextReader(xmlReaderP);
998 } else
999 pm_error("Failed to create xmlReader");
1000
1001 xmlCleanupParser();
1002
1003 return 0;
1004 }
1005
1006
1007