1 #include <stdio.h> /* fopen, fprintf, fread, 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 #define SVG_BUFFER_INCREMENT (size_t)4194304
21 /* Convenient typedefs for libsvgtiny */
22 typedef struct svgtiny_diagram diagram_t
;
23 typedef struct svgtiny_shape shape_t
;
27 GdkPixbufModuleUpdatedFunc updated_func
;
28 GdkPixbufModulePreparedFunc prepared_func
;
29 GdkPixbufModuleSizeFunc size_func
;
41 * @brief Render an svgtiny path using cairo.
44 * A pointer to a valid cairo context.
47 * A pointer to an svgtiny shape that will be rendered on the
48 * cairo context's target surface.
50 static void render_path(cairo_t
* cr
, shape_t
* path
) {
54 for (j
= 0; j
!= path
->path_length
; ) {
55 switch ((int) path
->path
[j
]) {
56 case svgtiny_PATH_MOVE
:
62 case svgtiny_PATH_CLOSE
:
66 case svgtiny_PATH_LINE
:
72 case svgtiny_PATH_BEZIER
:
83 if (path
->fill
!= svgtiny_TRANSPARENT
) {
84 cairo_set_source_rgba(cr
,
85 svgtiny_RED(path
->fill
) / 255.0,
86 svgtiny_GREEN(path
->fill
) / 255.0,
87 svgtiny_BLUE(path
->fill
) / 255.0,
89 cairo_fill_preserve(cr
);
91 if (path
->stroke
!= svgtiny_TRANSPARENT
) {
92 cairo_set_source_rgba(cr
,
93 svgtiny_RED(path
->stroke
) / 255.0,
94 svgtiny_GREEN(path
->stroke
) / 255.0,
95 svgtiny_BLUE(path
->stroke
) / 255.0,
97 cairo_set_line_width(cr
, path
->stroke_width
);
98 cairo_stroke_preserve(cr
);
103 * @brief Parse a buffer of SVG data into a diagram_t structure.
106 * The buffer containing the SVG document.
109 * The number of bytes in @c buffer.
111 * @return If successful, a pointer to a @c diagram_t structure is
112 * returned; if not, @c NULL is returned. You are expected to @c
113 * svgtiny_free the result if it is valid.
115 static diagram_t
* svgtiny_diagram_from_buffer(char* buffer
,
123 diagram
= svgtiny_create();
125 g_set_error_literal(error
,
127 GDK_PIXBUF_ERROR_FAILED
,
128 "svgtiny_create() failed");
132 code
= svgtiny_parse(diagram
, buffer
, bytecount
, "", width
, height
);
135 if (code
!= svgtiny_OK
) {
137 case svgtiny_OUT_OF_MEMORY
:
138 g_set_error_literal(error
,
141 "out of memory in svgtiny_parse()");
143 case svgtiny_LIBDOM_ERROR
:
144 g_set_error_literal(error
,
146 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
147 "libdom error in svgtiny_parse()");
149 case svgtiny_NOT_SVG
:
150 g_set_error_literal(error
,
152 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
153 "encountered svgtiny_NOT_SVG in svgtiny_parse()");
155 case svgtiny_SVG_ERROR
:
158 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
159 "SVG error in svgtiny_parse() on line %i: %s",
161 diagram
->error_message
);
171 * @brief Create a cairo context from a libsvgtiny diagram.
174 * A pointer to a valid libsvgtiny diagram.
176 * @return If successful, a pointer to a @c cairo_t context structure
177 * is returned; if not, @c NULL is returned. You are expected to @c
178 * cairo_destroy the result if it is valid.
180 static cairo_t
* cairo_context_from_diagram(diagram_t
* diagram
) {
182 cairo_surface_t
* surface
;
186 surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,
190 crs
= cairo_surface_status(surface
);
191 if (crs
!= CAIRO_STATUS_SUCCESS
) {
193 "cairo_image_surface_create failed: %s\n",
194 cairo_status_to_string(crs
));
195 cairo_surface_destroy(surface
);
199 cr
= cairo_create(surface
);
200 crs
= cairo_status(cr
);
202 /* Immediately destroy the surface which is now accessible as
204 cairo_surface_destroy(surface
);
206 if (crs
!= CAIRO_STATUS_SUCCESS
) {
208 "cairo_create failed: %s\n",
209 cairo_status_to_string(crs
));
214 cairo_set_source_rgba(cr
, 0, 0, 0, 0);
217 /* Loop through the shapes in the diagram... */
218 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
220 /* If this shape is a path, just render it. */
221 if (diagram
->shape
[i
].path
) {
222 render_path(cr
, &diagram
->shape
[i
]);
225 /* If this shape is text... */
226 if (diagram
->shape
[i
].text
) {
227 /* Figure out what color to use from the R/G/B components of the
229 cairo_set_source_rgba(cr
,
230 svgtiny_RED(diagram
->shape
[i
].stroke
) / 255.0,
231 svgtiny_GREEN(diagram
->shape
[i
].stroke
) / 255.0,
232 svgtiny_BLUE(diagram
->shape
[i
].stroke
) / 255.0,
234 /* Then move to the actual position of the text within the
237 diagram
->shape
[i
].text_x
,
238 diagram
->shape
[i
].text_y
);
241 cairo_show_text(cr
, diagram
->shape
[i
].text
);
246 /* Check the status again just for good measure? */
247 crs
= cairo_status(cr
);
248 if (crs
!= CAIRO_STATUS_SUCCESS
) {
251 cairo_status_to_string(crs
));
260 * @brief Create a GdkPixbuf from a buffer of SVG data.
263 * The buffer containing the SVG document.
266 * The number of bytes in @c buffer.
269 * The address of a @c GError pointer that we use to return errors.
271 * @return If successful, a valid pointer to a @c GdkPixbuf is
272 * returned; if not, @c NULL is returned and @c error is populated.
274 static GdkPixbuf
* gdk_pixbuf_from_svg_buffer(char* buffer
,
280 GError
* sub_error
= NULL
;
282 diagram
= svgtiny_diagram_from_buffer(buffer
,
288 g_propagate_error(error
, sub_error
);
292 cr
= cairo_context_from_diagram(diagram
);
294 svgtiny_free(diagram
);
295 g_set_error_literal(error
,
297 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
298 "could not create Cairo surface from SVG diagram");
303 /* We're using the viewport width and height and not the diagram
304 * width/height for the image. The diagram can be of a different
305 * size and aspect ratio than the viewport, and our main use case is
306 * for icons that are generally square and reasonably sized. If the
307 * diagram is "small," then we want to scale it up until it fits
308 * nicely in the viewport before rendering it. That's as opposed to
309 * rendering the image small, and letting GDK scale it up. Of course
310 * this reasoning makes the assumption that the viewport is usually
311 * larger than the diagram.
313 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
321 g_set_error_literal(error
,
323 GDK_PIXBUF_ERROR_FAILED
,
324 "failed to obtain a GdkPixbuf from Cairo surface");
332 * @brief Create a GdkPixbuf from an SVG filestream.
334 * This is essentially a wrapper around @gdk_pixbuf_from_svg_buffer
335 * that reads a @c FILE pointer into a buffer.
338 * A pointer to a @c FILE containing the SVG document.
341 * The address of a @c GError pointer that we use to return errors.
343 * @return If successful, a valid pointer to a @c GdkPixbuf is
344 * returned; if not, @c NULL is returned and @c error is populated.
346 static GdkPixbuf
* gdk_pixbuf_from_svg_file_stream(FILE *fp
, GError
** error
) {
347 size_t bytecount
, bytesread
;
350 /* Find the size of the file stream */
351 fseek(fp
, 0L, SEEK_END
);
352 bytecount
= ftell(fp
);
355 /* YOLO, no error checking */
356 buffer
= g_malloc(bytecount
);
358 bytesread
= fread(buffer
, 1, bytecount
, fp
);
359 if (bytesread
!= bytecount
) {
363 "read only %zd of %zd bytes from stream",
369 return gdk_pixbuf_from_svg_buffer(buffer
, bytecount
, error
);
373 static gpointer
gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func
,
374 GdkPixbufModulePreparedFunc prep_func
,
375 GdkPixbufModuleUpdatedFunc updated_func
,
379 SvgTinyContext
* context
= g_new(SvgTinyContext
, 1);
381 context
->size_func
= size_func
;
382 context
->prepared_func
= prep_func
;
383 context
->updated_func
= updated_func
;
384 context
->user_data
= user_data
;
386 /* YOLO, no error checking */
387 context
->svg_data
= g_malloc(SVG_BUFFER_INCREMENT
);
388 context
->svg_data_size
= 0;
393 static gboolean
gdk_pixbuf_load_increment(gpointer data
,
397 size_t increment
= 0;
398 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
400 if (context
->svg_data_size
+ size
> context
->svg_data_max
) {
401 if (size
> SVG_BUFFER_INCREMENT
) {
405 increment
= SVG_BUFFER_INCREMENT
;
408 /* YOLO, no error checking */
409 context
->svg_data
= g_realloc(context
->svg_data
,
410 context
->svg_data_max
+ increment
);
412 context
->svg_data_max
+= increment
;
415 memcpy(context
->svg_data
+ context
->svg_data_size
, buf
, size
);
416 context
->svg_data_size
+= size
;
421 static void emit_updated(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
422 if (context
->updated_func
!= NULL
) {
423 (*context
->updated_func
)(pixbuf
,
426 gdk_pixbuf_get_width(pixbuf
),
427 gdk_pixbuf_get_height(pixbuf
),
432 static void emit_prepared(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
433 if (context
->prepared_func
!= NULL
) {
434 (*context
->prepared_func
)(pixbuf
, NULL
, context
->user_data
);
440 static void emit_size(SvgTinyContext* context, GdkPixbuf* pixbuf) {
441 int w = gdk_pixbuf_get_width(pixbuf);
442 int h = gdk_pixbuf_get_height(pixbuf);
443 if (context->size_func != NULL) {
444 (*context->size_func)(&w, &h, context->user_data);
450 static gboolean
gdk_pixbuf_stop_load(gpointer data
, GError
**error
) {
451 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
452 GdkPixbuf
* pixbuf
= NULL
;
453 gboolean result
= TRUE
;
454 GError
* sub_error
= NULL
;
456 pixbuf
= gdk_pixbuf_from_svg_buffer(context
->svg_data
,
457 context
->svg_data_size
,
460 if (pixbuf
!= NULL
) {
461 /*emit_size(context, pixbuf);*/
462 emit_prepared(context
, pixbuf
);
463 emit_updated(context
, pixbuf
);
464 g_object_unref(pixbuf
);
467 g_propagate_error(error
, sub_error
);
476 G_MODULE_EXPORT
void fill_vtable(GdkPixbufModule
* module);
477 void fill_vtable(GdkPixbufModule
* module) {
478 module->begin_load
= gdk_pixbuf_begin_load
;
479 module->load_increment
= gdk_pixbuf_load_increment
;
480 module->stop_load
= gdk_pixbuf_stop_load
;
481 module->load
= gdk_pixbuf_from_svg_file_stream
;
484 G_MODULE_EXPORT
void fill_info(GdkPixbufFormat
*info
);
485 void fill_info(GdkPixbufFormat
* info
) {
486 /* Borrowed from librsvg-2.40.21 */
487 static const GdkPixbufModulePattern signature
[] = {
488 { " <svg", "* ", 100 },
489 { " <!DOCTYPE svg", "* ", 100 },
493 /* I'm not sure if we should support gzipped svg here? */
494 static const gchar
*mime_types
[] = {
498 "image/vnd.adobe.svg+xml",
500 "image/svg+xml-compressed",
504 static const gchar
*extensions
[] = {
512 info
->signature
= (GdkPixbufModulePattern
*)signature
;
513 info
->description
= "Scalable Vector Graphics";
514 info
->mime_types
= (gchar
**)mime_types
;
515 info
->extensions
= (gchar
**)extensions
;
516 info
->flags
= GDK_PIXBUF_FORMAT_SCALABLE
;
517 info
->license
= "AGPL3";
521 int main(int argc
, char** argv
) {
528 /* Parse arguments, and maybe print usage */
530 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
531 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
538 fp
= fopen(svgpath
, "rb");
544 pb
= gdk_pixbuf_from_svg_file_stream(fp
, &err
);
547 "Error %d in gdk_pixbuf_from_svg_file_stream: %s\n",
554 gdk_pixbuf_save(pb
, pngpath
, "png", NULL
, NULL
);