]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - test/decode_svg.c
src/svgtiny_css.c: use our userdata handler in set_libcss_node_data()
[libsvgtiny.git] / test / decode_svg.c
1 /*
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>
6 */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include "svgtiny.h"
14
15
16 static void write_mvg(FILE *fh, float scale, struct svgtiny_diagram *diagram)
17 {
18 unsigned int i;
19
20 fprintf(fh, "viewbox 0 0 %g %g\n",
21 scale * diagram->width, scale * diagram->height);
22
23 for (i = 0; i != diagram->shape_count; i++) {
24 if (diagram->shape[i].fill == svgtiny_TRANSPARENT) {
25 fprintf(fh, "fill none ");
26 } else {
27 fprintf(fh, "fill #%.6x ", diagram->shape[i].fill);
28 }
29
30 if (diagram->shape[i].stroke == svgtiny_TRANSPARENT) {
31 fprintf(fh, "stroke none ");
32 } else {
33 fprintf(fh, "stroke #%.6x ", diagram->shape[i].stroke);
34 }
35 fprintf(fh, "stroke-width %g ",
36 scale * diagram->shape[i].stroke_width);
37
38 if (diagram->shape[i].path) {
39 unsigned int j;
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]);
47 j += 3;
48 break;
49
50 case svgtiny_PATH_CLOSE:
51 fprintf(fh, "Z ");
52 j += 1;
53 break;
54
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]);
59 j += 3;
60 break;
61
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]);
70 j += 7;
71 break;
72
73 default:
74 fprintf(fh, "error ");
75 j += 1;
76 }
77 }
78 fprintf(fh, "' ");
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);
84 }
85 fprintf(fh, "\n");
86 }
87 }
88
89 int main(int argc, char *argv[])
90 {
91 FILE *fd;
92 float scale = 1.0;
93 struct stat sb;
94 char *buffer;
95 size_t size;
96 size_t n;
97 struct svgtiny_diagram *diagram;
98 svgtiny_code code;
99 FILE *outf = stdout;
100
101 if (argc < 2 || argc > 4) {
102 fprintf(stderr, "Usage: %s FILE [SCALE] [out]\n", argv[0]);
103 return 1;
104 }
105
106 /* load file into memory buffer */
107 fd = fopen(argv[1], "rb");
108 if (!fd) {
109 perror(argv[1]);
110 return 1;
111 }
112
113 if (stat(argv[1], &sb)) {
114 perror(argv[1]);
115 return 1;
116 }
117 size = sb.st_size;
118
119 buffer = malloc(size);
120 if (!buffer) {
121 fprintf(stderr, "Unable to allocate %lld bytes\n",
122 (long long) size);
123 return 1;
124 }
125
126 n = fread(buffer, 1, size, fd);
127 if (n != size) {
128 perror(argv[1]);
129 return 1;
130 }
131
132 fclose(fd);
133
134 /* read scale argument */
135 if (argc > 2) {
136 scale = atof(argv[2]);
137 if (scale == 0)
138 scale = 1.0;
139 }
140
141 /* output file */
142 if (argc > 3) {
143 outf = fopen(argv[3], "w+");
144 if (outf == NULL) {
145 fprintf(stderr, "Unable to open %s for writing\n", argv[3]);
146 return 2;
147 }
148 }
149
150 /* create svgtiny object */
151 diagram = svgtiny_create();
152 if (!diagram) {
153 fprintf(stderr, "svgtiny_create failed\n");
154 return 1;
155 }
156
157 /* parse */
158 code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
159 if (code != svgtiny_OK) {
160 fprintf(stderr, "svgtiny_parse failed: ");
161 switch (code) {
162 case svgtiny_OUT_OF_MEMORY:
163 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
164 break;
165 case svgtiny_LIBDOM_ERROR:
166 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
167 break;
168 case svgtiny_NOT_SVG:
169 fprintf(stderr, "svgtiny_NOT_SVG");
170 break;
171 case svgtiny_SVG_ERROR:
172 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
173 diagram->error_line,
174 diagram->error_message);
175 break;
176 default:
177 fprintf(stderr, "unknown svgtiny_code %i", code);
178 break;
179 }
180 fprintf(stderr, "\n");
181 }
182
183 free(buffer);
184
185 write_mvg(outf, scale, diagram);
186
187 if (argc > 3) {
188 fclose(outf);
189 }
190
191 svgtiny_free(diagram);
192
193 return 0;
194 }