]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - io-svg.c
io-svg.c: more work towards incremental loading (not functional yet).
[libsvgtiny-pixbuf.git] / io-svg.c
1 #include <stdio.h> /* fopen, fprintf, fread, printf */
2 #include <stdlib.h> /* malloc */
3
4 #include <cairo.h>
5 #include <gdk/gdk.h>
6 #include <gdk-pixbuf/gdk-pixbuf.h>
7 #include <svgtiny.h>
8
9 /*
10 * The width and height of the viewport that we'll render the SVG
11 * into. The final "picture" may not actually be this size; based on
12 * the height, width, viewBox, and preserveAspectRatio attributes in
13 * the SVG itself, libsvgtiny may scale, stretch, offset, etc. the
14 * paths to make them fit nicely into the viewport.
15 */
16 #define VIEWPORT_WIDTH 512
17 #define VIEWPORT_HEIGHT 512
18
19 #define SVG_BUFFER_INCREMENT (size_t)4194304
20
21 /* Convenient typedefs for libsvgtiny */
22 typedef struct svgtiny_diagram diagram_t;
23 typedef struct svgtiny_shape shape_t;
24
25
26 typedef struct {
27 GdkPixbufModuleUpdatedFunc updated_func;
28 GdkPixbufModulePreparedFunc prepared_func;
29 GdkPixbufModuleSizeFunc size_func;
30 gpointer user_data;
31
32 /* The "file" */
33 char* svg_data;
34 size_t svg_data_size;
35 size_t svg_data_max;
36
37 } SvgTinyContext;
38
39
40 /**
41 * @brief Render an svgtiny path using cairo.
42 *
43 * @param cr
44 * A pointer to a valid cairo context.
45 *
46 * @param path
47 * A pointer to an svgtiny shape that will be rendered on the
48 * cairo context's target surface.
49 */
50 static void render_path(cairo_t* cr, shape_t* path) {
51 unsigned int j;
52
53 cairo_new_path(cr);
54 for (j = 0; j != path->path_length; ) {
55 switch ((int) path->path[j]) {
56 case svgtiny_PATH_MOVE:
57 cairo_move_to(cr,
58 path->path[j + 1],
59 path->path[j + 2]);
60 j += 3;
61 break;
62 case svgtiny_PATH_CLOSE:
63 cairo_close_path(cr);
64 j += 1;
65 break;
66 case svgtiny_PATH_LINE:
67 cairo_line_to(cr,
68 path->path[j + 1],
69 path->path[j + 2]);
70 j += 3;
71 break;
72 case svgtiny_PATH_BEZIER:
73 cairo_curve_to(cr,
74 path->path[j + 1],
75 path->path[j + 2],
76 path->path[j + 3],
77 path->path[j + 4],
78 path->path[j + 5],
79 path->path[j + 6]);
80 j += 7;
81 break;
82 default:
83 fprintf(stderr, "error: unmatched case in render_path\n");
84 j += 1;
85 }
86 }
87 if (path->fill != svgtiny_TRANSPARENT) {
88 cairo_set_source_rgba(cr,
89 svgtiny_RED(path->fill) / 255.0,
90 svgtiny_GREEN(path->fill) / 255.0,
91 svgtiny_BLUE(path->fill) / 255.0,
92 1);
93 cairo_fill_preserve(cr);
94 }
95 if (path->stroke != svgtiny_TRANSPARENT) {
96 cairo_set_source_rgba(cr,
97 svgtiny_RED(path->stroke) / 255.0,
98 svgtiny_GREEN(path->stroke) / 255.0,
99 svgtiny_BLUE(path->stroke) / 255.0,
100 1);
101 cairo_set_line_width(cr, path->stroke_width);
102 cairo_stroke_preserve(cr);
103 }
104 }
105
106 /**
107 * @brief Parse an SVG file into a diagram_t structure.
108 *
109 * @param fp
110 * A pointer to an open file stream.
111 *
112 * @return If successful, a pointer to a @c diagram_t structure is
113 * returned; if not, @c NULL is returned. You are expected to @c
114 * svgtiny_free the result if it is valid.
115 */
116 static diagram_t* svgtiny_diagram_from_buffer(char* buffer,
117 size_t bytecount,
118 int width,
119 int height) {
120 diagram_t* diagram;
121 svgtiny_code code;
122
123 diagram = svgtiny_create();
124 if (!diagram) {
125 fprintf(stderr, "svgtiny_create() failed\n");
126 return NULL;
127 }
128
129 code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
130 free(buffer);
131
132 if (code != svgtiny_OK) {
133 fprintf(stderr, "svgtiny_parse failed with ");
134 switch (code) {
135 case svgtiny_OUT_OF_MEMORY:
136 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
137 break;
138 case svgtiny_LIBDOM_ERROR:
139 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
140 break;
141 case svgtiny_NOT_SVG:
142 fprintf(stderr, "svgtiny_NOT_SVG");
143 break;
144 case svgtiny_SVG_ERROR:
145 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
146 diagram->error_line,
147 diagram->error_message);
148 break;
149 default:
150 fprintf(stderr, "unknown svgtiny_code %i", code);
151 break;
152 }
153 fprintf(stderr, "\n");
154 return NULL;
155 }
156
157 return diagram;
158 }
159
160 /**
161 * @brief Create a cairo context from a libsvgtiny diagram.
162 *
163 * @param diagram
164 * A pointer to a valid libsvgtiny diagram.
165 *
166 * @return If successful, a pointer to a @c cairo_t context structure
167 * is returned; if not, @c NULL is returned. You are expected to @c
168 * cairo_destroy the result if it is valid.
169 */
170 static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
171 cairo_t* cr;
172 cairo_surface_t* surface;
173 cairo_status_t crs;
174 unsigned int i;
175
176 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
177 diagram->width,
178 diagram->height);
179
180 crs = cairo_surface_status(surface);
181 if (crs != CAIRO_STATUS_SUCCESS) {
182 fprintf(stderr,
183 "cairo_image_surface_create failed: %s\n",
184 cairo_status_to_string(crs));
185 cairo_surface_destroy(surface);
186 return NULL;
187 }
188
189 cr = cairo_create(surface);
190 crs = cairo_status(cr);
191
192 /* Immediately destroy the surface which is now accessible as
193 cr->target */
194 cairo_surface_destroy(surface);
195
196 if (crs != CAIRO_STATUS_SUCCESS) {
197 fprintf(stderr,
198 "cairo_create failed: %s\n",
199 cairo_status_to_string(crs));
200 cairo_destroy(cr);
201 return NULL;
202 }
203
204 cairo_set_source_rgba(cr, 0, 0, 0, 0);
205 cairo_paint(cr);
206
207 /* Loop through the shapes in the diagram... */
208 for (i = 0; i != diagram->shape_count; i++) {
209
210 /* If this shape is a path, just render it. */
211 if (diagram->shape[i].path) {
212 render_path(cr, &diagram->shape[i]);
213 }
214
215 /* If this shape is text... */
216 if (diagram->shape[i].text) {
217 /* Figure out what color to use from the R/G/B components of the
218 shape's stroke. */
219 cairo_set_source_rgba(cr,
220 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
221 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
222 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0,
223 1);
224 /* Then move to the actual position of the text within the
225 shape... */
226 cairo_move_to(cr,
227 diagram->shape[i].text_x,
228 diagram->shape[i].text_y);
229
230 /* and draw it. */
231 cairo_show_text(cr, diagram->shape[i].text);
232 }
233 }
234
235
236 /* Check the status again just for good measure? */
237 crs = cairo_status(cr);
238 if (crs != CAIRO_STATUS_SUCCESS) {
239 fprintf(stderr,
240 "cairo error: %s\n",
241 cairo_status_to_string(crs));
242 cairo_destroy(cr);
243 return NULL;
244 }
245
246 return cr;
247 }
248
249 static GdkPixbuf* gdk_pixbuf_from_svg_buffer(char* buffer,
250 size_t bytecount,
251 GError** error) {
252 diagram_t* diagram;
253 cairo_t* cr = 0;
254 GdkPixbuf* pb;
255
256 diagram = svgtiny_diagram_from_buffer(buffer,
257 bytecount,
258 VIEWPORT_WIDTH,
259 VIEWPORT_HEIGHT);
260 if (!diagram) {
261 g_set_error_literal(error,
262 GDK_PIXBUF_ERROR,
263 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
264 "Could not parse SVG diagram from file");
265 return NULL;
266 }
267
268 cr = cairo_context_from_diagram(diagram);
269 if (!cr) {
270 svgtiny_free(diagram);
271 g_set_error_literal(error,
272 GDK_PIXBUF_ERROR,
273 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
274 "Could not create Cairo surface from SVG diagram");
275 return NULL;
276 }
277
278
279 /* We're using the viewport width and height and not the diagram
280 * width/height for the image. The diagram can be of a different
281 * size and aspect ratio than the viewport, and our main use case is
282 * for icons that are generally square and reasonably sized. If the
283 * diagram is "small," then we want to scale it up until it fits
284 * nicely in the viewport before rendering it. That's as opposed to
285 * rendering the image small, and letting GDK scale it up. Of course
286 * this reasoning makes the assumption that the viewport is usually
287 * larger than the diagram.
288 */
289 pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
290 0,
291 0,
292 VIEWPORT_WIDTH,
293 VIEWPORT_HEIGHT);
294
295
296 if (!pb) {
297 g_set_error_literal(error,
298 GDK_PIXBUF_ERROR,
299 GDK_PIXBUF_ERROR_FAILED,
300 "Failed to obtain a GdkPixbuf from Cairo surface");
301 }
302
303 return pb;
304 }
305
306 static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError** error) {
307 size_t bytecount, bytesread;
308 char* buffer;
309
310 /* Find the size of the file stream */
311 fseek(fp, 0L, SEEK_END);
312 bytecount = ftell(fp);
313 rewind(fp);
314
315 buffer = malloc(bytecount);
316 if (!buffer) {
317 fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount);
318 return NULL;
319 }
320
321 bytesread = fread(buffer, 1, bytecount, fp);
322 if (bytesread != bytecount) {
323 fprintf(stderr, "Read only %zd of %zd bytes from stream\n",
324 bytesread,
325 bytecount);
326 }
327 fclose(fp);
328
329 return gdk_pixbuf_from_svg_buffer(buffer, bytecount, error);
330 }
331
332
333 static gpointer gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func,
334 GdkPixbufModulePreparedFunc prep_func,
335 GdkPixbufModuleUpdatedFunc updated_func,
336 gpointer user_data,
337 GError **error) {
338
339 SvgTinyContext* context = g_new0(SvgTinyContext, 1);
340
341 context->size_func = size_func;
342 context->prepared_func = prep_func;
343 context->updated_func = updated_func;
344 context->user_data = user_data;
345
346 context->svg_data = malloc(SVG_BUFFER_INCREMENT);
347 if (context->svg_data) {
348 context->svg_data_size = 0;
349 context->svg_data_max = SVG_BUFFER_INCREMENT;
350 }
351 else {
352 fprintf(stderr, "Unable to allocate %zd bytes\n", SVG_BUFFER_INCREMENT);
353 return NULL;
354 }
355
356 return context;
357 }
358
359 static gboolean gdk_pixbuf_load_increment(gpointer data,
360 const guchar* buf,
361 guint size,
362 GError** error) {
363 SvgTinyContext* context = (SvgTinyContext*)data;
364
365 if (context->svg_data_size + size > context->svg_data_max) {
366 size_t increment = 0;
367 if (size > SVG_BUFFER_INCREMENT) {
368 increment = size;
369 }
370 else {
371 increment = SVG_BUFFER_INCREMENT;
372 }
373 context->svg_data = realloc(context->svg_data,
374 context->svg_data_max + increment);
375 if (context->svg_data) {
376 context->svg_data_max += increment;
377 }
378 else {
379 fprintf(stderr, "Unable to (re)allocate %zd bytes\n", increment);
380 return FALSE;
381 }
382 }
383 memcpy(context->svg_data + context->svg_data_size, buf, size);
384 context->svg_data_size += size;
385
386 return TRUE;
387 }
388
389 static void emit_updated(SvgTinyContext* context, GdkPixbuf* pixbuf) {
390 if (context->updated_func != NULL) {
391 (*context->updated_func)(pixbuf,
392 0,
393 0,
394 gdk_pixbuf_get_width(pixbuf),
395 gdk_pixbuf_get_height(pixbuf),
396 context->user_data);
397 }
398 }
399
400 static void emit_prepared(SvgTinyContext* context, GdkPixbuf* pixbuf) {
401 if (context->prepared_func != NULL) {
402 (*context->prepared_func)(pixbuf, NULL, context->user_data);
403 }
404 }
405
406
407 /*
408 static void emit_size(SvgTinyContext* context, GdkPixbuf* pixbuf) {
409
410 int w = gdk_pixbuf_get_width(pixbuf);
411 int h = gdk_pixbuf_get_height(pixbuf);
412 if (context->size_func != NULL) {
413 (*context->size_func)(&w, &h, context->user_data);
414 }
415 }
416 */
417
418 static gboolean gdk_pixbuf_stop_load(gpointer data, GError **error) {
419 SvgTinyContext* context = (SvgTinyContext*)data;
420 GdkPixbuf* pixbuf = NULL;
421 gboolean result = TRUE;
422
423 pixbuf = gdk_pixbuf_from_svg_buffer(context->svg_data,
424 context->svg_data_size,
425 error);
426
427 if (pixbuf != NULL) {
428 /* emit_size(context, pixbuf); */
429 emit_prepared(context, pixbuf);
430 emit_updated(context, pixbuf);
431 g_object_unref(pixbuf);
432 }
433 else {
434 result = FALSE;
435 }
436 g_free (context);
437
438 return result;
439 }
440
441
442 G_MODULE_EXPORT void fill_vtable(GdkPixbufModule* module);
443 void fill_vtable(GdkPixbufModule* module) {
444 module->begin_load = gdk_pixbuf_begin_load;
445 module->load_increment = gdk_pixbuf_load_increment;
446 module->stop_load = gdk_pixbuf_stop_load;
447 module->load = gdk_pixbuf_from_svg_file_stream;
448 }
449
450 G_MODULE_EXPORT void fill_info(GdkPixbufFormat *info);
451 void fill_info(GdkPixbufFormat* info) {
452 /* Borrowed from librsvg-2.40.21 */
453 static const GdkPixbufModulePattern signature[] = {
454 { " <svg", "* ", 100 },
455 { " <!DOCTYPE svg", "* ", 100 },
456 { NULL, NULL, 0 }
457 };
458
459 /* I'm not sure if we should support gzipped svg here? */
460 static const gchar *mime_types[] = {
461 "image/svg+xml",
462 "image/svg",
463 "image/svg-xml",
464 "image/vnd.adobe.svg+xml",
465 "text/xml-svg",
466 "image/svg+xml-compressed",
467 NULL
468 };
469
470 static const gchar *extensions[] = {
471 "svg",
472 "svgz",
473 "svg.gz",
474 NULL
475 };
476
477 info->name = "svg";
478 info->signature = (GdkPixbufModulePattern*)signature;
479 info->description = "Scalable Vector Graphics";
480 info->mime_types = (gchar**)mime_types;
481 info->extensions = (gchar**)extensions;
482 info->flags = GDK_PIXBUF_FORMAT_SCALABLE;
483 info->license = "AGPL3";
484 }
485
486
487 int main(int argc, char** argv) {
488 char* svgpath;
489 char* pngpath;
490 FILE* fp;
491 GError* err = NULL;
492 GdkPixbuf* pb;
493
494 /* Parse arguments, and maybe print usage */
495 if (argc < 3) {
496 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
497 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
498 return 2;
499 }
500
501 svgpath = argv[1];
502 pngpath = argv[2];
503
504 fp = fopen(svgpath, "rb");
505 if (!fp) {
506 perror(svgpath);
507 return 1;
508 }
509
510 pb = gdk_pixbuf_from_svg_file_stream(fp, &err);
511 if (!pb) {
512 fprintf(stderr,
513 "Error %d in gdk_pixbuf_from_svg_file_stream: %s\n",
514 err->code,
515 err->message);
516 g_error_free(err);
517 return 1;
518 }
519
520 gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
521 g_object_unref(pb);
522 return 0;
523 }