]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - io-svg.c
COPYING: add one to state the "or later" bit
[libsvgtiny-pixbuf.git] / io-svg.c
1 #include <string.h> /* memcpy, memset */
2
3 #include <cairo.h>
4 #include <gdk-pixbuf/gdk-pixbuf.h> /* includes glib.h */
5 #include <glib/gprintf.h>
6 #include <libxml/parser.h>
7 #include <libxml/tree.h>
8 #include <svgtiny.h>
9
10 /* This "header" includes extra code that we've copy/pasted from GDK */
11 #include "gdk_pixbuf_get_from_surface.h"
12
13 /* Declare the two functions that we export. Their names aren't
14 * mentioned in gdk-pixbuf-io.h, but they are required by the
15 * implementation, as you can confirm from the API docs or in
16 * gdk-pixbuf-io.c. The G_MODULE_EXPORT macro is defined in
17 * glib's gmodule.h */
18 G_MODULE_EXPORT void fill_vtable(GdkPixbufModule* module);
19 G_MODULE_EXPORT void fill_info(GdkPixbufFormat *info);
20
21
22 /* The width and height of the viewport that we'll render the SVG
23 * into. The final "picture" may not actually be this size; based on
24 * the height, width, viewBox, and preserveAspectRatio attributes in
25 * the SVG itself, libsvgtiny may scale, stretch, offset, etc. the
26 * paths to make them fit nicely into the viewport. */
27 #define VIEWPORT_WIDTH 512
28 #define VIEWPORT_HEIGHT 512
29
30 /* Convenient typedefs for libsvgtiny */
31 typedef struct svgtiny_diagram diagram_t;
32 typedef struct svgtiny_shape shape_t;
33
34
35 /* Our main data structure. One of these is created when we start
36 * loading an SVG, and it persists while the SVG is being read. It
37 * contains a few boilerplate members, and the svg_data/svg_data_size
38 * fields that we use to keep track of the buffer where the SVG file
39 * lives. */
40 typedef struct {
41 GdkPixbufModuleUpdatedFunc updated_func;
42 GdkPixbufModulePreparedFunc prepared_func;
43 gpointer user_data;
44
45 /* The SVG "file" that we're building in memory. */
46 gchar* svg_data;
47
48 /* How far into svg_data are we? This should always point to the
49 next empty byte. If (for example) svg_data_size is 2, then
50 svg_data[0] and svg_data[1] are used, but svg_data[2] is free. */
51 size_t svg_data_size;
52
53 } SvgTinyContext;
54
55
56 /**
57 * @brief Render an svgtiny path using cairo.
58 *
59 * This was lovingly borrowed from @c examples/svgtiny_display_x11.c
60 * in libsvgtiny itself, and modified to use a transparent background.
61 *
62 * @param cr
63 * A pointer to a valid cairo context.
64 *
65 * @param path
66 * A pointer to an svgtiny shape that will be rendered on the
67 * cairo context's target surface.
68 */
69 static void render_path(cairo_t* cr, shape_t* path) {
70 unsigned int j;
71
72 cairo_new_path(cr);
73 for (j = 0; j != path->path_length; ) {
74 switch ((int) path->path[j]) {
75 case svgtiny_PATH_MOVE:
76 cairo_move_to(cr,
77 path->path[j + 1],
78 path->path[j + 2]);
79 j += 3;
80 break;
81 case svgtiny_PATH_CLOSE:
82 cairo_close_path(cr);
83 j += 1;
84 break;
85 case svgtiny_PATH_LINE:
86 cairo_line_to(cr,
87 path->path[j + 1],
88 path->path[j + 2]);
89 j += 3;
90 break;
91 case svgtiny_PATH_BEZIER:
92 cairo_curve_to(cr,
93 path->path[j + 1],
94 path->path[j + 2],
95 path->path[j + 3],
96 path->path[j + 4],
97 path->path[j + 5],
98 path->path[j + 6]);
99 j += 7;
100 }
101 }
102 if (path->fill != svgtiny_TRANSPARENT) {
103 cairo_set_source_rgba(cr,
104 svgtiny_RED(path->fill) / 255.0,
105 svgtiny_GREEN(path->fill) / 255.0,
106 svgtiny_BLUE(path->fill) / 255.0,
107 1);
108 cairo_fill_preserve(cr);
109 }
110 if (path->stroke != svgtiny_TRANSPARENT) {
111 cairo_set_source_rgba(cr,
112 svgtiny_RED(path->stroke) / 255.0,
113 svgtiny_GREEN(path->stroke) / 255.0,
114 svgtiny_BLUE(path->stroke) / 255.0,
115 1);
116 cairo_set_line_width(cr, path->stroke_width);
117 cairo_stroke_preserve(cr);
118 }
119 }
120
121 /**
122 * @brief Parse a buffer of SVG data into a diagram_t structure.
123 *
124 * @param buffer
125 * The buffer containing the SVG document.
126 *
127 * @param bytecount
128 * The number of bytes in @c buffer.
129 *
130 * @return If successful, a pointer to a @c diagram_t structure is
131 * returned; if not, @c NULL is returned. You are expected to @c
132 * svgtiny_free the result if it is valid.
133 */
134 static diagram_t* svgtiny_diagram_from_buffer(const gchar* buffer,
135 gsize bytecount,
136 guint width,
137 guint height,
138 GError** error) {
139 diagram_t* diagram;
140 svgtiny_code code;
141
142 diagram = svgtiny_create();
143 if (!diagram) {
144 g_set_error_literal(error,
145 G_FILE_ERROR,
146 G_FILE_ERROR_NOMEM,
147 "out of memory in svgtiny_create()");
148 return NULL;
149 }
150
151 g_assert((int)width >= 0);
152 g_assert((int)height >= 0);
153
154 /* There's a thread-safety issue in libwapcaplet that can cause
155 * svgtiny_parse() to crash if you load lots of SVGs at once:
156 *
157 * https://bugs.netsurf-browser.org/mantis/view.php?id=2857
158 *
159 * Putting a lock around svgtiny_parse() is a pretty simple solution
160 * and looks like it does the trick.
161 */
162 static GMutex mutex;
163 g_mutex_lock(&mutex);
164 code = svgtiny_parse(diagram,
165 buffer,
166 bytecount, "",
167 (int)width,
168 (int)height);
169 g_mutex_unlock (&mutex);
170
171 switch(code) {
172 case svgtiny_OK:
173 /* The one success case. */
174 return diagram;
175 case svgtiny_OUT_OF_MEMORY:
176 g_set_error_literal(error,
177 G_FILE_ERROR,
178 G_FILE_ERROR_NOMEM,
179 "out of memory in svgtiny_parse()");
180 break;
181 case svgtiny_LIBDOM_ERROR:
182 g_set_error_literal(error,
183 GDK_PIXBUF_ERROR,
184 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
185 "invalid XML DOM in svgtiny_parse()");
186 break;
187 case svgtiny_NOT_SVG:
188 g_set_error_literal(error,
189 GDK_PIXBUF_ERROR,
190 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
191 "missing <svg> element in svgtiny_parse()");
192 break;
193 case svgtiny_SVG_ERROR:
194 g_set_error(error,
195 GDK_PIXBUF_ERROR,
196 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
197 "SVG format error in svgtiny_parse() on line %i: %s",
198 diagram->error_line,
199 diagram->error_message);
200 break;
201 }
202
203 /* All other cases above are failure */
204 return NULL;
205 }
206
207 /**
208 * @brief Create a cairo context from a libsvgtiny diagram.
209 *
210 * @param diagram
211 * A pointer to a valid libsvgtiny diagram.
212 *
213 * @return If successful, a pointer to a @c cairo_t context structure
214 * is returned; if not, @c NULL is returned. You are expected to @c
215 * cairo_destroy the result if it is valid.
216 */
217 static cairo_t* cairo_context_from_diagram(const diagram_t* diagram) {
218 cairo_t* cr;
219 cairo_surface_t* surface;
220 cairo_status_t crs;
221 unsigned int i;
222
223 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
224 diagram->width,
225 diagram->height);
226
227 crs = cairo_surface_status(surface);
228 if (crs != CAIRO_STATUS_SUCCESS) {
229 g_fprintf(stderr,
230 "cairo_image_surface_create failed: %s\n",
231 cairo_status_to_string(crs));
232 cairo_surface_destroy(surface);
233 return NULL;
234 }
235
236 cr = cairo_create(surface);
237 crs = cairo_status(cr);
238
239 /* Immediately destroy the surface which is now accessible as
240 cr->target */
241 cairo_surface_destroy(surface);
242
243 if (crs != CAIRO_STATUS_SUCCESS) {
244 g_fprintf(stderr,
245 "cairo_create failed: %s\n",
246 cairo_status_to_string(crs));
247 cairo_destroy(cr);
248 return NULL;
249 }
250
251 cairo_set_source_rgba(cr, 0, 0, 0, 0);
252 cairo_paint(cr);
253
254 /* Loop through the shapes in the diagram... */
255 for (i = 0; i != diagram->shape_count; i++) {
256
257 /* If this shape is a path, just render it. */
258 if (diagram->shape[i].path) {
259 render_path(cr, &diagram->shape[i]);
260 }
261
262 /* If this shape is text... */
263 if (diagram->shape[i].text) {
264 /* Figure out what color to use from the R/G/B components of the
265 shape's stroke. */
266 cairo_set_source_rgba(cr,
267 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
268 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
269 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0,
270 1);
271 /* Then move to the actual position of the text within the
272 shape... */
273 cairo_move_to(cr,
274 diagram->shape[i].text_x,
275 diagram->shape[i].text_y);
276
277 /* and draw it. */
278 cairo_show_text(cr, diagram->shape[i].text);
279 }
280 }
281
282
283 /* Check the status again just for good measure? */
284 crs = cairo_status(cr);
285 if (crs != CAIRO_STATUS_SUCCESS) {
286 g_fprintf(stderr,
287 "cairo error: %s\n",
288 cairo_status_to_string(crs));
289 cairo_destroy(cr);
290 return NULL;
291 }
292
293 return cr;
294 }
295
296 /**
297 * @brief Create a GdkPixbuf from a buffer of SVG data.
298 *
299 * @param buffer
300 * The buffer containing the SVG document.
301 *
302 * @param bytecount
303 * The number of bytes in @c buffer.
304 *
305 * @param error
306 * The address of a @c GError pointer that we use to return errors.
307 *
308 * @return If successful, a valid pointer to a @c GdkPixbuf is
309 * returned; if not, @c NULL is returned and @c error is populated.
310 */
311 static GdkPixbuf* gdk_pixbuf_from_svg_buffer(const gchar* buffer,
312 gsize bytecount,
313 GError** error) {
314 diagram_t* diagram;
315 cairo_t* cr = 0;
316 GdkPixbuf* pb;
317 GError* sub_error = NULL;
318
319 diagram = svgtiny_diagram_from_buffer(buffer,
320 bytecount,
321 VIEWPORT_WIDTH,
322 VIEWPORT_HEIGHT,
323 &sub_error);
324 if (!diagram) {
325 g_propagate_error(error, sub_error);
326 return NULL;
327 }
328
329 cr = cairo_context_from_diagram(diagram);
330 if (!cr) {
331 svgtiny_free(diagram);
332 g_set_error_literal(error,
333 GDK_PIXBUF_ERROR,
334 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
335 "could not create Cairo surface from SVG diagram");
336 return NULL;
337 }
338
339
340 /* I've gone back and forth on this about five times: we use the
341 * diagram width and height, and not the viewport width and height.
342 * This can ultimately render an image that's larger than the
343 * viewport size, but I think GDK will resize the final pixbuf
344 * anyway. More importantly, rendering small icons at a larger
345 * (viewport) size seems to make the whole thing go ape-shit.
346 * So for now I'm back in the diagram camp.
347 */
348 pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
349 0,
350 0,
351 diagram->width,
352 diagram->height);
353
354
355 if (!pb) {
356 g_set_error_literal(error,
357 GDK_PIXBUF_ERROR,
358 GDK_PIXBUF_ERROR_FAILED,
359 "failed to obtain a GdkPixbuf from Cairo surface");
360 }
361
362 return pb;
363 }
364
365
366 /**
367 * @brief Our implementation of GdkPixbufModuleBeginLoadFunc, as
368 * defined in gdk-pixbuf-io.h.
369 */
370 static gpointer gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func,
371 GdkPixbufModulePreparedFunc prep_func,
372 GdkPixbufModuleUpdatedFunc updated_func,
373 gpointer user_data,
374 GError **error) {
375
376 SvgTinyContext* context = g_new(SvgTinyContext, 1);
377
378 context->prepared_func = prep_func;
379 context->updated_func = updated_func;
380 context->user_data = user_data;
381
382 context->svg_data = NULL;
383 context->svg_data_size = 0;
384
385 return context;
386 }
387
388
389 /**
390 * @brief Our implementation of GdkPixbufModuleIncrementLoadFunc, as
391 * defined in gdk-pixbuf-io.h.
392 */
393 static gboolean gdk_pixbuf_load_increment(gpointer data,
394 const guchar* buf,
395 guint buf_size,
396 GError** error) {
397 SvgTinyContext* context = (SvgTinyContext*)data;
398
399 if (buf_size == 0) {
400 return TRUE;
401 }
402
403 context->svg_data = g_realloc(context->svg_data,
404 context->svg_data_size + buf_size);
405 memcpy(context->svg_data + context->svg_data_size, buf, buf_size);
406 context->svg_data_size += buf_size;
407
408 return TRUE;
409 }
410
411 /**
412 * @brief Convenience function to execute the "updated" callback
413 * stored in our @c context.
414 */
415 static void emit_updated(SvgTinyContext* context, GdkPixbuf* pixbuf) {
416 if (context->updated_func != NULL) {
417 (*context->updated_func)(pixbuf,
418 0,
419 0,
420 gdk_pixbuf_get_width(pixbuf),
421 gdk_pixbuf_get_height(pixbuf),
422 context->user_data);
423 }
424 }
425
426 /**
427 * @brief Convenience function to execute the "prepared" callback
428 * stored in our @c context.
429 */
430 static void emit_prepared(SvgTinyContext* context, GdkPixbuf* pixbuf) {
431 if (context->prepared_func != NULL) {
432 (*context->prepared_func)(pixbuf, NULL, context->user_data);
433 }
434 }
435
436
437
438 /**
439 * @brief Replace one GTK <xi:include> element by its data.
440 *
441 * @param node
442 * A pointer to an <xi:include> element node.
443 *
444 * @return TRUE if we replaced the node, and FALSE otherwise.
445 *
446 */
447 static gboolean process_one_xinclude(xmlNode* node) {
448 xmlChar* href;
449
450 href = xmlGetProp(node, BAD_CAST "href");
451 if (href == NULL) {
452 /* We only process XIncludes with base64 data hrefs */
453 return FALSE;
454 }
455
456 if (xmlStrncmp(href, BAD_CAST "data:text/xml;base64,", 21)) {
457 /* We only process XIncludes with base64 data hrefs */
458 return FALSE;
459 }
460
461 xmlChar* hrefdata = href+21;
462
463 /* Verify that hrefdata is base64-encoded (and that it's safe to
464 cast to a signed gchar pointer). I'm assuming that everyone is
465 using the RFC 4648 encoding? */
466 for (unsigned int i=0; i < xmlStrlen(hrefdata); i++) {
467 if (hrefdata[i] > 'z') {
468 return FALSE;
469 }
470 if (hrefdata[i] < '0' && hrefdata[i] != '+' && hrefdata[i] != '/') {
471 return FALSE;
472 }
473 }
474
475 /* WARNING: the xmlChar and guchar types here are compatible, but
476 the decoded data is not necessarily NULL-terminated, while all of
477 the libxml2 functions that operate on a xmlChar pointer assume
478 that they are. */
479 gsize decoded_size;
480 xmlChar* decoded = g_base64_decode((const gchar*)hrefdata, &decoded_size);
481
482 /* This cast is safe because signed and unsigned chars are the same size,
483 and xmlReadMemory is going to treat the data as binary anyway. */
484 xmlDoc* xinc_doc = xmlReadMemory((const char*)decoded,
485 decoded_size,
486 "xinclude.xml",
487 NULL,
488 0);
489 g_free(decoded);
490
491 if (xinc_doc == NULL) {
492 return FALSE;
493 }
494
495 xmlNode* xinc_root = xmlDocGetRootElement(xinc_doc);
496 if (xinc_root == NULL || xmlStrcmp(xinc_root->name, BAD_CAST "svg")) {
497 return FALSE;
498 }
499
500 /* Replace the original xinclude "node" with the children of this
501 "svg" node. Do the order of the nodes in an SVG matter? I don't
502 know, but we go to a little bit of extra trouble here to ensure
503 that we put the replacement in the right place, i.e. after its
504 previous sibling (if there is one). */
505
506 xmlNode* p = xmlPreviousElementSibling(node);
507 xmlNode* cur_node;
508
509 /* If there is no previous sibling element, do one AddChild()
510 first. Then we're back to the case of a previous sibling. */
511 if (p) {
512 cur_node = xmlFirstElementChild(xinc_root);
513 }
514 else {
515 p = node->parent;
516 cur_node = xmlFirstElementChild(xinc_root);
517 if (cur_node) {
518 /* Without the xmlCopyNode, I get segfaults, and I don't care to
519 investigate why. */
520 p = xmlAddChild(p, xmlCopyNode(cur_node,1));
521 xmlReconciliateNs(p->doc, p);
522
523 cur_node = xmlNextElementSibling(cur_node);
524 }
525 }
526
527 g_assert(p != NULL); /* xmlAddChild didn't fail */
528
529 xmlUnlinkNode(node);
530 xmlFreeNode(node);
531
532 while (cur_node) {
533 p = xmlAddNextSibling(p, xmlCopyNode(cur_node,1));
534 xmlReconciliateNs(p->doc, p);
535 cur_node = xmlNextElementSibling(cur_node);
536 }
537
538 xmlFreeDoc(xinc_doc);
539
540 return TRUE;
541 }
542
543 /**
544 * @brief Replace all GTK <xi:include> elements in a tree by their data.
545 *
546 * @param node
547 * A node pointer, to the root of the tree.
548 *
549 * @return TRUE if we replaced any <xi:include> element nodes, and
550 * FALSE otherwise.
551 *
552 */
553 static gboolean process_child_xincludes(xmlNode* a_node) {
554 gboolean result = FALSE;
555 xmlNode* cur_node = a_node;
556 xmlNode* next_node;
557
558 g_assert(cur_node == NULL || cur_node->type == XML_ELEMENT_NODE);
559
560 while (cur_node) {
561 if (!xmlStrcmp(cur_node->name, BAD_CAST "include")) {
562 /* process_one_xinclude() clobbers this node, so we need
563 to get its successor before calling that function. */
564 next_node = xmlNextElementSibling(cur_node);
565 if (process_one_xinclude(cur_node)) {
566 result = TRUE;
567 }
568 cur_node = next_node;
569 continue;
570 }
571
572 if (process_child_xincludes(xmlFirstElementChild(cur_node))) {
573 result = TRUE;
574 }
575 cur_node = xmlNextElementSibling(cur_node);
576 }
577
578 return result;
579 }
580
581
582 /**
583 * @brief Process GTK <xi:include> elements in an SVG buffer.
584 *
585 * GTK is very cute. Its gtk-encode-symbolic-svg tool wraps your SVG
586 * in its own boilerplate, but then rather than including your SVG
587 * data verbatim, it includes it via a sketchy XInclude that looks
588 * like,
589 *
590 * <xi:include href="data:text/xml;base64,PD94bWwgd..."/>
591 *
592 * Librsvg knows how to parse that, but libxml2 doesn't (the latter
593 * can handle an XInclude, just not a base64-encoded data reference).
594 * Fortunately, you can read the source to gtk-encode-symbolic-svg,
595 * and just see what the format of that line will be. Here we're going
596 * to parse out the base64 data, decode it, strip out its opening
597 * <xml> and <svg> tags, and then replace the original <xi:include>
598 * element with the result.
599 *
600 * @param buffer
601 * A buffer containing SVG file data.
602 *
603 * @param buf_size
604 * The size of @c buffer (which may not be NULL-terminated).
605 *
606 * @param new_size
607 * A pointer to the size of the new buffer, valid only if the
608 * return value is non-NULL.
609 *
610 * @return A pointer to a buffer where the <xi:include> has been
611 * processed. If no replacements were made, the result will be @c
612 * NULL; otherwise, you are expected to @c free it when you are done.
613 */
614 static gchar* process_gtk_symbolic_svg_xinclude(const gchar* buffer,
615 gsize buf_size,
616 gsize* new_size) {
617
618 xmlDoc* doc = xmlReadMemory(buffer,buf_size,"symbolic.xml",NULL,0);
619 if (doc == NULL) {
620 return NULL;
621 }
622
623 xmlNode* root_element = xmlDocGetRootElement(doc);
624 if (root_element == NULL) {
625 return NULL;
626 }
627
628 gchar* result = NULL;
629 if (process_child_xincludes(root_element)) {
630 /* If we actually replaced something, we need to return the new
631 document in a buffer. */
632 xmlChar *xmlbuf;
633 int xmlbuf_size;
634 xmlDocDumpFormatMemory(doc, &xmlbuf, &xmlbuf_size, 1);
635 /* We're going to free() this later on with g_free() instead of
636 xmlFree(), so the two "byte" types had better be the same
637 size. */
638 g_assert(sizeof(xmlChar) == sizeof(gchar));
639 *new_size = (gsize)xmlbuf_size;
640 result = (gchar*)xmlbuf;
641 }
642
643 xmlFreeDoc(doc);
644 xmlCleanupParser();
645 return result;
646 }
647
648
649 /**
650 * @brief Our implementation of GdkPixbufModuleStopLoadFunc, as
651 * defined in gdk-pixbuf-io.h.
652 */
653 static gboolean gdk_pixbuf_stop_load(gpointer data, GError **error) {
654 SvgTinyContext* context = (SvgTinyContext*)data;
655 GdkPixbuf* pixbuf = NULL;
656 GError* sub_error = NULL;
657
658 g_assert(context != NULL);
659 if (context->svg_data == NULL || context->svg_data_size == 0) {
660 /* Is it possible to begin/stop with no increments in between?
661 * I sure don't know. Let's play it safe. */
662 return FALSE;
663 }
664
665 /* If we're inside of gtk-encode-symbolic-svg right now, we need to
666 process the insane librsvg-specific XInclude directive it hands
667 us before proceeding. */
668 gsize newsize;
669 gchar* newdata = process_gtk_symbolic_svg_xinclude(context->svg_data,
670 context->svg_data_size,
671 &newsize);
672 if (newdata != NULL) {
673 g_free(context->svg_data);
674 context->svg_data = newdata;
675 context->svg_data_size = newsize;
676 }
677
678 /* OK, we've got an SVG with no XIncludes now. */
679 pixbuf = gdk_pixbuf_from_svg_buffer(context->svg_data,
680 context->svg_data_size,
681 &sub_error);
682
683 if (pixbuf != NULL) {
684 emit_prepared(context, pixbuf);
685 emit_updated(context, pixbuf);
686 g_object_unref(pixbuf);
687 }
688 else {
689 g_propagate_error(error, sub_error);
690 return FALSE;
691 }
692 g_free(context->svg_data);
693 g_free(context);
694
695 return TRUE;
696 }
697
698
699 /**
700 * @brief Our implementation of GdkPixbufModuleFillVtableFunc, as
701 * defined in gdk-pixbuf-io.h.
702 */
703 void fill_vtable(GdkPixbufModule* module) {
704 module->begin_load = gdk_pixbuf_begin_load;
705 module->load_increment = gdk_pixbuf_load_increment;
706 module->stop_load = gdk_pixbuf_stop_load;
707 }
708
709 /**
710 * @brief Our implementation of GdkPixbufModuleFillInfoFunc, as
711 * defined in gdk-pixbuf-io.h.
712 */
713 void fill_info(GdkPixbufFormat* info) {
714 /* Borrowed from librsvg-2.40.21 */
715 static const GdkPixbufModulePattern signature[] = {
716 { " <svg", "* ", 100 },
717 { " <!DOCTYPE svg", "* ", 100 },
718 { NULL, NULL, 0 }
719 };
720
721 /* I'm not sure if we should support gzipped svg here? */
722 static const gchar *mime_types[] = {
723 "image/svg+xml",
724 "image/svg",
725 "image/svg-xml",
726 "image/vnd.adobe.svg+xml",
727 "text/xml-svg",
728 "image/svg+xml-compressed",
729 NULL
730 };
731
732 static const gchar *extensions[] = {
733 "svg",
734 "svgz",
735 "svg.gz",
736 NULL
737 };
738
739 info->name = "svg";
740 info->signature = (GdkPixbufModulePattern*)signature;
741 info->description = "Scalable Vector Graphics";
742 info->mime_types = (gchar**)mime_types;
743 info->extensions = (gchar**)extensions;
744 info->flags = GDK_PIXBUF_FORMAT_SCALABLE;
745 info->license = "AGPL3";
746 }
747
748
749 /**
750 * @brief Entry point of the svg2png test program.
751 */
752 int main(int argc, char** argv) {
753 char* svgpath;
754 char* pngpath;
755 GError* err = NULL;
756 GdkPixbuf* pb;
757
758 /* Parse arguments, and maybe print usage */
759 if (argc < 3) {
760 g_printf("Usage: %s INPUT OUTPUT\n", argv[0]);
761 g_printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
762 return 2;
763 }
764
765 svgpath = argv[1];
766 pngpath = argv[2];
767
768 pb = gdk_pixbuf_new_from_file(svgpath, &err);
769 if (!pb) {
770 g_fprintf(stderr,
771 "Error %d in gdk_pixbuf_new_from_file: %s\n",
772 err->code,
773 err->message);
774 g_error_free(err);
775 return 1;
776 }
777
778 gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
779 g_object_unref(pb);
780 return 0;
781 }