]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - example.c
example.c: factor out svgtiny_diagram_from_path().
[libsvgtiny-pixbuf.git] / example.c
1 #include <fcntl.h> /* open */
2 #include <stdio.h> /* printf, fprintf */
3 #include <stdlib.h> /* malloc */
4 #include <sys/stat.h> /* fstat */
5 #include <unistd.h> /* read */
6
7 #include <cairo.h>
8 #include <svgtiny.h>
9
10
11 /**
12 * Render an svgtiny path using cairo.
13 */
14 void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) {
15 unsigned int j;
16
17 cairo_new_path(cr);
18 for (j = 0; j != path->path_length; ) {
19 switch ((int) path->path[j]) {
20 case svgtiny_PATH_MOVE:
21 cairo_move_to(cr,
22 scale * path->path[j + 1],
23 scale * path->path[j + 2]);
24 j += 3;
25 break;
26 case svgtiny_PATH_CLOSE:
27 cairo_close_path(cr);
28 j += 1;
29 break;
30 case svgtiny_PATH_LINE:
31 cairo_line_to(cr,
32 scale * path->path[j + 1],
33 scale * path->path[j + 2]);
34 j += 3;
35 break;
36 case svgtiny_PATH_BEZIER:
37 cairo_curve_to(cr,
38 scale * path->path[j + 1],
39 scale * path->path[j + 2],
40 scale * path->path[j + 3],
41 scale * path->path[j + 4],
42 scale * path->path[j + 5],
43 scale * path->path[j + 6]);
44 j += 7;
45 break;
46 default:
47 printf("error ");
48 j += 1;
49 }
50 }
51 if (path->fill != svgtiny_TRANSPARENT) {
52 cairo_set_source_rgb(cr,
53 svgtiny_RED(path->fill) / 255.0,
54 svgtiny_GREEN(path->fill) / 255.0,
55 svgtiny_BLUE(path->fill) / 255.0);
56 cairo_fill_preserve(cr);
57 }
58 if (path->stroke != svgtiny_TRANSPARENT) {
59 cairo_set_source_rgb(cr,
60 svgtiny_RED(path->stroke) / 255.0,
61 svgtiny_GREEN(path->stroke) / 255.0,
62 svgtiny_BLUE(path->stroke) / 255.0);
63 cairo_set_line_width(cr, scale * path->stroke_width);
64 cairo_stroke_preserve(cr);
65 }
66 }
67
68 /*
69 * @brief Parse an SVG file into an svgtiny_diagram structure.
70 *
71 * @param path
72 * The path to the SVG file.
73 *
74 * @return If successful, a pointer to an @c svgtiny_diagram structure
75 * is returned; if not, @c NULL is returned. You are expected to @c
76 * svgtiny_free the result if it is valid.
77 */
78 struct svgtiny_diagram* svgtiny_diagram_from_path(char* path,
79 int width,
80 int height) {
81 struct svgtiny_diagram *diagram;
82
83 int fd;
84 size_t bytecount;
85 char *buffer;
86 size_t bytesread;
87 struct stat sb;
88 svgtiny_code code;
89
90 /* load file into memory buffer */
91 fd = open(path, O_RDONLY);
92 if (fd == -1) {
93 perror(path);
94 return NULL;
95 }
96
97 if (fstat(fd, &sb)) {
98 perror(path);
99 return NULL;
100 }
101 bytecount = sb.st_size;
102
103 buffer = malloc(bytecount);
104 if (!buffer) {
105 fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount);
106 return NULL;
107 }
108
109 bytesread = read(fd, buffer, bytecount);
110 if (bytesread != bytecount) {
111 perror(path);
112 return NULL;
113 }
114 close(fd);
115
116
117 /* create svgtiny object */
118 diagram = svgtiny_create();
119 if (!diagram) {
120 fprintf(stderr, "svgtiny_create() failed\n");
121 return NULL;
122 }
123
124 code = svgtiny_parse(diagram, buffer, bytecount, path, width, height);
125 free(buffer);
126
127 if (code != svgtiny_OK) {
128 fprintf(stderr, "svgtiny_parse failed: ");
129 switch (code) {
130 case svgtiny_OUT_OF_MEMORY:
131 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
132 break;
133 case svgtiny_LIBDOM_ERROR:
134 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
135 break;
136 case svgtiny_NOT_SVG:
137 fprintf(stderr, "svgtiny_NOT_SVG");
138 break;
139 case svgtiny_SVG_ERROR:
140 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
141 diagram->error_line,
142 diagram->error_message);
143 break;
144 default:
145 fprintf(stderr, "unknown svgtiny_code %i", code);
146 break;
147 }
148 fprintf(stderr, "\n");
149 return NULL;
150 }
151
152 return diagram;
153 }
154
155 int main(int argc, char** argv) {
156 int exit_code = 0;
157
158 char* svgpath;
159 char* pngpath;
160 int pngwidth = 1024, pngheight = 1024;
161
162 float scale = 1.0;
163 struct svgtiny_diagram *diagram;
164 cairo_surface_t *surface;
165 cairo_t *cr = 0;
166 cairo_status_t cr_status;
167
168 unsigned int i;
169
170 /* Parse arguments, and maybe print usage */
171 if (argc < 3) {
172 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
173 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
174 return 2;
175 }
176
177 svgpath = argv[1];
178 pngpath = argv[2];
179
180 diagram = svgtiny_diagram_from_path(svgpath, pngwidth, pngheight);
181
182 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
183 pngwidth,
184 pngheight);
185 if (!surface) {
186 fprintf(stderr, "cairo_image_surface_create failed\n");
187 exit_code = 1;
188 goto cleanup;
189 }
190
191 cr = cairo_create(surface);
192 cr_status = cairo_status(cr);
193 if (cr_status != CAIRO_STATUS_SUCCESS) {
194 fprintf(stderr,
195 "cairo_create failed: %s\n",
196 cairo_status_to_string(cr_status));
197 exit_code = 1;
198 goto cleanup;
199 }
200
201 cairo_set_source_rgb(cr, 1, 1, 1);
202 cairo_paint(cr);
203
204 /* Loop through the shapes in the diagram... */
205 for (i = 0; i != diagram->shape_count; i++) {
206
207 /* If this shape is a path, just render it. */
208 if (diagram->shape[i].path) {
209 render_path(cr, scale, &diagram->shape[i]);
210 }
211
212 /* If this shape is text... */
213 if (diagram->shape[i].text) {
214 /* Figure out what color to use from the R/G/B components of the
215 shape's stroke. */
216 cairo_set_source_rgb(cr,
217 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
218 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
219 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0);
220 /* Then move to the actual position of the text within the
221 shape... */
222 cairo_move_to(cr,
223 scale * diagram->shape[i].text_x,
224 scale * diagram->shape[i].text_y);
225
226 /* and draw it. */
227 cairo_show_text(cr, diagram->shape[i].text);
228 }
229 }
230
231 /* Check the status again. */
232 cr_status = cairo_status(cr);
233 if (cr_status != CAIRO_STATUS_SUCCESS) {
234 fprintf(stderr, "cairo error: %s\n",
235 cairo_status_to_string(cr_status));
236 exit_code = 1;
237 goto cleanup;
238 }
239
240 cr_status = cairo_surface_write_to_png(surface, pngpath);
241 if (cr_status != CAIRO_STATUS_SUCCESS) {
242 fprintf(stderr, "cairo error: %s\n",
243 cairo_status_to_string(cr_status));
244 exit_code = 1;
245 goto cleanup;
246 }
247
248 cleanup:
249 if (cr) {
250 cairo_destroy(cr);
251 }
252 cairo_surface_destroy(surface);
253 svgtiny_free(diagram);
254
255 return exit_code;
256 }