]>
gitweb.michael.orlitzky.com - libsvgtiny.git/blob - test/decode_svg.c
2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008 James Bursa <james@semichrome.net>
16 static void write_mvg(FILE *fh
, float scale
, struct svgtiny_diagram
*diagram
)
20 fprintf(fh
, "viewbox 0 0 %g %g\n",
21 scale
* diagram
->width
, scale
* diagram
->height
);
23 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
24 if (diagram
->shape
[i
].fill
== svgtiny_TRANSPARENT
) {
25 fprintf(fh
, "fill none ");
27 fprintf(fh
, "fill #%.6x ", diagram
->shape
[i
].fill
);
30 if (diagram
->shape
[i
].stroke
== svgtiny_TRANSPARENT
) {
31 fprintf(fh
, "stroke none ");
33 fprintf(fh
, "stroke #%.6x ", diagram
->shape
[i
].stroke
);
35 fprintf(fh
, "stroke-width %g ",
36 scale
* diagram
->shape
[i
].stroke_width
);
38 if (diagram
->shape
[i
].path
) {
40 fprintf(fh
, "path '");
41 for (j
= 0; j
!= diagram
->shape
[i
].path_length
; ) {
42 switch ((int) diagram
->shape
[i
].path
[j
]) {
43 case svgtiny_PATH_MOVE
:
44 fprintf(fh
, "M %g %g ",
45 scale
* diagram
->shape
[i
].path
[j
+ 1],
46 scale
* diagram
->shape
[i
].path
[j
+ 2]);
50 case svgtiny_PATH_CLOSE
:
55 case svgtiny_PATH_LINE
:
56 fprintf(fh
, "L %g %g ",
57 scale
* diagram
->shape
[i
].path
[j
+ 1],
58 scale
* diagram
->shape
[i
].path
[j
+ 2]);
62 case svgtiny_PATH_BEZIER
:
63 fprintf(fh
, "C %g %g %g %g %g %g ",
64 scale
* diagram
->shape
[i
].path
[j
+ 1],
65 scale
* diagram
->shape
[i
].path
[j
+ 2],
66 scale
* diagram
->shape
[i
].path
[j
+ 3],
67 scale
* diagram
->shape
[i
].path
[j
+ 4],
68 scale
* diagram
->shape
[i
].path
[j
+ 5],
69 scale
* diagram
->shape
[i
].path
[j
+ 6]);
74 fprintf(fh
, "error ");
79 } else if (diagram
->shape
[i
].text
) {
80 fprintf(fh
, "text %g %g '%s' ",
81 scale
* diagram
->shape
[i
].text_x
,
82 scale
* diagram
->shape
[i
].text_y
,
83 diagram
->shape
[i
].text
);
89 int main(int argc
, char *argv
[])
97 struct svgtiny_diagram
*diagram
;
101 if (argc
< 2 || argc
> 4) {
102 fprintf(stderr
, "Usage: %s FILE [SCALE] [out]\n", argv
[0]);
106 /* load file into memory buffer */
107 fd
= fopen(argv
[1], "rb");
113 if (stat(argv
[1], &sb
)) {
119 buffer
= malloc(size
);
121 fprintf(stderr
, "Unable to allocate %lld bytes\n",
126 n
= fread(buffer
, 1, size
, fd
);
134 /* read scale argument */
136 scale
= atof(argv
[2]);
143 outf
= fopen(argv
[3], "w+");
145 fprintf(stderr
, "Unable to open %s for writing\n", argv
[3]);
150 /* create svgtiny object */
151 diagram
= svgtiny_create();
153 fprintf(stderr
, "svgtiny_create failed\n");
158 code
= svgtiny_parse(diagram
, buffer
, size
, argv
[1], 1000, 1000);
159 if (code
!= svgtiny_OK
) {
160 fprintf(stderr
, "svgtiny_parse failed: ");
162 case svgtiny_OUT_OF_MEMORY
:
163 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
165 case svgtiny_LIBDOM_ERROR
:
166 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
168 case svgtiny_NOT_SVG
:
169 fprintf(stderr
, "svgtiny_NOT_SVG");
171 case svgtiny_SVG_ERROR
:
172 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
174 diagram
->error_message
);
177 fprintf(stderr
, "unknown svgtiny_code %i", code
);
180 fprintf(stderr
, "\n");
185 write_mvg(outf
, scale
, diagram
);
191 svgtiny_free(diagram
);