1 #include <stdio.h> /* fprintf, printf */
2 #include <string.h> /* memcpy */
6 #include <gdk-pixbuf/gdk-pixbuf.h> /* includes glib.h */
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.
16 #define VIEWPORT_WIDTH 512
17 #define VIEWPORT_HEIGHT 512
19 /* Convenient typedefs for libsvgtiny */
20 typedef struct svgtiny_diagram diagram_t
;
21 typedef struct svgtiny_shape shape_t
;
25 GdkPixbufModuleUpdatedFunc updated_func
;
26 GdkPixbufModulePreparedFunc prepared_func
;
27 GdkPixbufModuleSizeFunc size_func
;
30 /* The SVG "file" that we're building in memory. */
33 /* How far into svg_data are we? This should always point to the
34 next empty byte. If (for example) svg_data_size is 2, then
35 svg_data[0] and svg_data[1] are used, but svg_data[2] is free. */
42 * @brief Render an svgtiny path using cairo.
45 * A pointer to a valid cairo context.
48 * A pointer to an svgtiny shape that will be rendered on the
49 * cairo context's target surface.
51 static void render_path(cairo_t
* cr
, shape_t
* path
) {
55 for (j
= 0; j
!= path
->path_length
; ) {
56 switch ((int) path
->path
[j
]) {
57 case svgtiny_PATH_MOVE
:
63 case svgtiny_PATH_CLOSE
:
67 case svgtiny_PATH_LINE
:
73 case svgtiny_PATH_BEZIER
:
84 if (path
->fill
!= svgtiny_TRANSPARENT
) {
85 cairo_set_source_rgba(cr
,
86 svgtiny_RED(path
->fill
) / 255.0,
87 svgtiny_GREEN(path
->fill
) / 255.0,
88 svgtiny_BLUE(path
->fill
) / 255.0,
90 cairo_fill_preserve(cr
);
92 if (path
->stroke
!= svgtiny_TRANSPARENT
) {
93 cairo_set_source_rgba(cr
,
94 svgtiny_RED(path
->stroke
) / 255.0,
95 svgtiny_GREEN(path
->stroke
) / 255.0,
96 svgtiny_BLUE(path
->stroke
) / 255.0,
98 cairo_set_line_width(cr
, path
->stroke_width
);
99 cairo_stroke_preserve(cr
);
104 * @brief Parse a buffer of SVG data into a diagram_t structure.
107 * The buffer containing the SVG document.
110 * The number of bytes in @c buffer.
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.
116 static diagram_t
* svgtiny_diagram_from_buffer(char* buffer
,
124 diagram
= svgtiny_create();
126 g_set_error_literal(error
,
128 GDK_PIXBUF_ERROR_FAILED
,
129 "svgtiny_create() failed");
133 code
= svgtiny_parse(diagram
, buffer
, bytecount
, "", width
, height
);
136 if (code
!= svgtiny_OK
) {
138 case svgtiny_OUT_OF_MEMORY
:
139 g_set_error_literal(error
,
142 "out of memory in svgtiny_parse()");
144 case svgtiny_LIBDOM_ERROR
:
145 g_set_error_literal(error
,
147 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
148 "libdom error in svgtiny_parse()");
150 case svgtiny_NOT_SVG
:
151 g_set_error_literal(error
,
153 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
154 "encountered svgtiny_NOT_SVG in svgtiny_parse()");
156 case svgtiny_SVG_ERROR
:
159 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
160 "SVG error in svgtiny_parse() on line %i: %s",
162 diagram
->error_message
);
172 * @brief Create a cairo context from a libsvgtiny diagram.
175 * A pointer to a valid libsvgtiny diagram.
177 * @return If successful, a pointer to a @c cairo_t context structure
178 * is returned; if not, @c NULL is returned. You are expected to @c
179 * cairo_destroy the result if it is valid.
181 static cairo_t
* cairo_context_from_diagram(diagram_t
* diagram
) {
183 cairo_surface_t
* surface
;
187 surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,
191 crs
= cairo_surface_status(surface
);
192 if (crs
!= CAIRO_STATUS_SUCCESS
) {
194 "cairo_image_surface_create failed: %s\n",
195 cairo_status_to_string(crs
));
196 cairo_surface_destroy(surface
);
200 cr
= cairo_create(surface
);
201 crs
= cairo_status(cr
);
203 /* Immediately destroy the surface which is now accessible as
205 cairo_surface_destroy(surface
);
207 if (crs
!= CAIRO_STATUS_SUCCESS
) {
209 "cairo_create failed: %s\n",
210 cairo_status_to_string(crs
));
215 cairo_set_source_rgba(cr
, 0, 0, 0, 0);
218 /* Loop through the shapes in the diagram... */
219 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
221 /* If this shape is a path, just render it. */
222 if (diagram
->shape
[i
].path
) {
223 render_path(cr
, &diagram
->shape
[i
]);
226 /* If this shape is text... */
227 if (diagram
->shape
[i
].text
) {
228 /* Figure out what color to use from the R/G/B components of the
230 cairo_set_source_rgba(cr
,
231 svgtiny_RED(diagram
->shape
[i
].stroke
) / 255.0,
232 svgtiny_GREEN(diagram
->shape
[i
].stroke
) / 255.0,
233 svgtiny_BLUE(diagram
->shape
[i
].stroke
) / 255.0,
235 /* Then move to the actual position of the text within the
238 diagram
->shape
[i
].text_x
,
239 diagram
->shape
[i
].text_y
);
242 cairo_show_text(cr
, diagram
->shape
[i
].text
);
247 /* Check the status again just for good measure? */
248 crs
= cairo_status(cr
);
249 if (crs
!= CAIRO_STATUS_SUCCESS
) {
252 cairo_status_to_string(crs
));
261 * @brief Create a GdkPixbuf from a buffer of SVG data.
264 * The buffer containing the SVG document.
267 * The number of bytes in @c buffer.
270 * The address of a @c GError pointer that we use to return errors.
272 * @return If successful, a valid pointer to a @c GdkPixbuf is
273 * returned; if not, @c NULL is returned and @c error is populated.
275 static GdkPixbuf
* gdk_pixbuf_from_svg_buffer(char* buffer
,
281 GError
* sub_error
= NULL
;
283 diagram
= svgtiny_diagram_from_buffer(buffer
,
289 g_propagate_error(error
, sub_error
);
293 cr
= cairo_context_from_diagram(diagram
);
295 svgtiny_free(diagram
);
296 g_set_error_literal(error
,
298 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
299 "could not create Cairo surface from SVG diagram");
304 /* I've gone back and forth on this about five times: we use the
305 * diagram width and height, and not the viewport width and height.
306 * This can ultimately render an image that's larger than the
307 * viewport size, but I think GDK will resize the final pixbuf
308 * anyway. More importantly, rendering small icons at a larger
309 * (viewport) size seems to make the whole thing go ape-shit.
310 * So for now I'm back in the diagram camp.
312 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
320 g_set_error_literal(error
,
322 GDK_PIXBUF_ERROR_FAILED
,
323 "failed to obtain a GdkPixbuf from Cairo surface");
330 static gpointer
gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func
,
331 GdkPixbufModulePreparedFunc prep_func
,
332 GdkPixbufModuleUpdatedFunc updated_func
,
336 SvgTinyContext
* context
= g_new(SvgTinyContext
, 1);
338 context
->size_func
= size_func
;
339 context
->prepared_func
= prep_func
;
340 context
->updated_func
= updated_func
;
341 context
->user_data
= user_data
;
343 /* YOLO, no error checking */
344 context
->svg_data
= g_malloc(0);
345 context
->svg_data_size
= 0;
350 static gboolean
gdk_pixbuf_load_increment(gpointer data
,
354 size_t increment
= 0;
355 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
357 /* YOLO, no error checking */
358 context
->svg_data
= g_realloc(context
->svg_data
,
359 context
->svg_data_size
+ buf_size
);
360 memcpy(context
->svg_data
+ context
->svg_data_size
, buf
, buf_size
);
361 context
->svg_data_size
+= buf_size
;
366 static void emit_updated(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
367 if (context
->updated_func
!= NULL
) {
368 (*context
->updated_func
)(pixbuf
,
371 gdk_pixbuf_get_width(pixbuf
),
372 gdk_pixbuf_get_height(pixbuf
),
377 static void emit_prepared(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
378 if (context
->prepared_func
!= NULL
) {
379 (*context
->prepared_func
)(pixbuf
, NULL
, context
->user_data
);
385 static void emit_size(SvgTinyContext* context, GdkPixbuf* pixbuf) {
386 int w = gdk_pixbuf_get_width(pixbuf);
387 int h = gdk_pixbuf_get_height(pixbuf);
388 if (context->size_func != NULL) {
389 (*context->size_func)(&w, &h, context->user_data);
395 static gboolean
gdk_pixbuf_stop_load(gpointer data
, GError
**error
) {
396 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
397 GdkPixbuf
* pixbuf
= NULL
;
398 gboolean result
= TRUE
;
399 GError
* sub_error
= NULL
;
401 pixbuf
= gdk_pixbuf_from_svg_buffer(context
->svg_data
,
402 context
->svg_data_size
,
405 if (pixbuf
!= NULL
) {
406 /*emit_size(context, pixbuf);*/
407 emit_prepared(context
, pixbuf
);
408 emit_updated(context
, pixbuf
);
409 g_object_unref(pixbuf
);
412 g_propagate_error(error
, sub_error
);
421 G_MODULE_EXPORT
void fill_vtable(GdkPixbufModule
* module);
422 void fill_vtable(GdkPixbufModule
* module) {
423 module->begin_load
= gdk_pixbuf_begin_load
;
424 module->load_increment
= gdk_pixbuf_load_increment
;
425 module->stop_load
= gdk_pixbuf_stop_load
;
428 G_MODULE_EXPORT
void fill_info(GdkPixbufFormat
*info
);
429 void fill_info(GdkPixbufFormat
* info
) {
430 /* Borrowed from librsvg-2.40.21 */
431 static const GdkPixbufModulePattern signature
[] = {
432 { " <svg", "* ", 100 },
433 { " <!DOCTYPE svg", "* ", 100 },
437 /* I'm not sure if we should support gzipped svg here? */
438 static const gchar
*mime_types
[] = {
442 "image/vnd.adobe.svg+xml",
444 "image/svg+xml-compressed",
448 static const gchar
*extensions
[] = {
456 info
->signature
= (GdkPixbufModulePattern
*)signature
;
457 info
->description
= "Scalable Vector Graphics";
458 info
->mime_types
= (gchar
**)mime_types
;
459 info
->extensions
= (gchar
**)extensions
;
460 info
->flags
= GDK_PIXBUF_FORMAT_SCALABLE
;
461 info
->license
= "AGPL3";
465 int main(int argc
, char** argv
) {
472 /* Parse arguments, and maybe print usage */
474 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
475 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
482 pb
= gdk_pixbuf_new_from_file(svgpath
, &err
);
485 "Error %d in gdk_pixbuf_new_from_file: %s\n",
492 gdk_pixbuf_save(pb
, pngpath
, "png", NULL
, NULL
);