1 #include <stdio.h> /* fopen, fprintf, fread, printf */
2 #include <stdlib.h> /* malloc */
6 #include <gdk-pixbuf/gdk-pixbuf.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 fprintf(stderr
, "error: unmatched case in render_path\n");
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,
93 cairo_fill_preserve(cr
);
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,
101 cairo_set_line_width(cr
, path
->stroke_width
);
102 cairo_stroke_preserve(cr
);
107 * @brief Parse an SVG file into a diagram_t structure.
110 * A pointer to an open file stream.
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
,
123 diagram
= svgtiny_create();
125 fprintf(stderr
, "svgtiny_create() failed\n");
129 code
= svgtiny_parse(diagram
, buffer
, bytecount
, "", width
, height
);
132 if (code
!= svgtiny_OK
) {
133 fprintf(stderr
, "svgtiny_parse failed with ");
135 case svgtiny_OUT_OF_MEMORY
:
136 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
138 case svgtiny_LIBDOM_ERROR
:
139 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
141 case svgtiny_NOT_SVG
:
142 fprintf(stderr
, "svgtiny_NOT_SVG");
144 case svgtiny_SVG_ERROR
:
145 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
147 diagram
->error_message
);
150 fprintf(stderr
, "unknown svgtiny_code %i", code
);
153 fprintf(stderr
, "\n");
161 * @brief Create a cairo context from a libsvgtiny diagram.
164 * A pointer to a valid libsvgtiny diagram.
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.
170 static cairo_t
* cairo_context_from_diagram(diagram_t
* diagram
) {
172 cairo_surface_t
* surface
;
176 surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,
180 crs
= cairo_surface_status(surface
);
181 if (crs
!= CAIRO_STATUS_SUCCESS
) {
183 "cairo_image_surface_create failed: %s\n",
184 cairo_status_to_string(crs
));
185 cairo_surface_destroy(surface
);
189 cr
= cairo_create(surface
);
190 crs
= cairo_status(cr
);
192 /* Immediately destroy the surface which is now accessible as
194 cairo_surface_destroy(surface
);
196 if (crs
!= CAIRO_STATUS_SUCCESS
) {
198 "cairo_create failed: %s\n",
199 cairo_status_to_string(crs
));
204 cairo_set_source_rgba(cr
, 0, 0, 0, 0);
207 /* Loop through the shapes in the diagram... */
208 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
210 /* If this shape is a path, just render it. */
211 if (diagram
->shape
[i
].path
) {
212 render_path(cr
, &diagram
->shape
[i
]);
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
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,
224 /* Then move to the actual position of the text within the
227 diagram
->shape
[i
].text_x
,
228 diagram
->shape
[i
].text_y
);
231 cairo_show_text(cr
, diagram
->shape
[i
].text
);
236 /* Check the status again just for good measure? */
237 crs
= cairo_status(cr
);
238 if (crs
!= CAIRO_STATUS_SUCCESS
) {
241 cairo_status_to_string(crs
));
249 static GdkPixbuf
* gdk_pixbuf_from_svg_buffer(char* buffer
,
256 diagram
= svgtiny_diagram_from_buffer(buffer
,
261 g_set_error_literal(error
,
263 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
264 "Could not parse SVG diagram from file");
268 cr
= cairo_context_from_diagram(diagram
);
270 svgtiny_free(diagram
);
271 g_set_error_literal(error
,
273 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
274 "Could not create Cairo surface from SVG diagram");
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.
289 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
297 g_set_error_literal(error
,
299 GDK_PIXBUF_ERROR_FAILED
,
300 "Failed to obtain a GdkPixbuf from Cairo surface");
306 static GdkPixbuf
* gdk_pixbuf_from_svg_file_stream(FILE *fp
, GError
** error
) {
307 size_t bytecount
, bytesread
;
310 /* Find the size of the file stream */
311 fseek(fp
, 0L, SEEK_END
);
312 bytecount
= ftell(fp
);
315 buffer
= malloc(bytecount
);
317 fprintf(stderr
, "Unable to allocate %zd bytes\n", bytecount
);
321 bytesread
= fread(buffer
, 1, bytecount
, fp
);
322 if (bytesread
!= bytecount
) {
323 fprintf(stderr
, "Read only %zd of %zd bytes from stream\n",
329 return gdk_pixbuf_from_svg_buffer(buffer
, bytecount
, error
);
333 static gpointer
gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func
,
334 GdkPixbufModulePreparedFunc prep_func
,
335 GdkPixbufModuleUpdatedFunc updated_func
,
339 SvgTinyContext
* context
= g_new0(SvgTinyContext
, 1);
341 context
->size_func
= size_func
;
342 context
->prepared_func
= prep_func
;
343 context
->updated_func
= updated_func
;
344 context
->user_data
= user_data
;
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
;
352 fprintf(stderr
, "Unable to allocate %zd bytes\n", SVG_BUFFER_INCREMENT
);
359 static gboolean
gdk_pixbuf_load_increment(gpointer data
,
363 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
365 if (context
->svg_data_size
+ size
> context
->svg_data_max
) {
366 size_t increment
= 0;
367 if (size
> SVG_BUFFER_INCREMENT
) {
371 increment
= SVG_BUFFER_INCREMENT
;
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
;
379 fprintf(stderr
, "Unable to (re)allocate %zd bytes\n", increment
);
383 memcpy(context
->svg_data
+ context
->svg_data_size
, buf
, size
);
384 context
->svg_data_size
+= size
;
389 static void emit_updated(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
390 if (context
->updated_func
!= NULL
) {
391 (*context
->updated_func
)(pixbuf
,
394 gdk_pixbuf_get_width(pixbuf
),
395 gdk_pixbuf_get_height(pixbuf
),
400 static void emit_prepared(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
401 if (context
->prepared_func
!= NULL
) {
402 (*context
->prepared_func
)(pixbuf
, NULL
, context
->user_data
);
408 static void emit_size(SvgTinyContext* context, GdkPixbuf* pixbuf) {
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);
418 static gboolean
gdk_pixbuf_stop_load(gpointer data
, GError
**error
) {
419 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
420 GdkPixbuf
* pixbuf
= NULL
;
421 gboolean result
= TRUE
;
423 pixbuf
= gdk_pixbuf_from_svg_buffer(context
->svg_data
,
424 context
->svg_data_size
,
427 if (pixbuf
!= NULL
) {
428 /* emit_size(context, pixbuf); */
429 emit_prepared(context
, pixbuf
);
430 emit_updated(context
, pixbuf
);
431 g_object_unref(pixbuf
);
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
;
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 },
459 /* I'm not sure if we should support gzipped svg here? */
460 static const gchar
*mime_types
[] = {
464 "image/vnd.adobe.svg+xml",
466 "image/svg+xml-compressed",
470 static const gchar
*extensions
[] = {
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";
487 int main(int argc
, char** argv
) {
494 /* Parse arguments, and maybe print usage */
496 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
497 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
504 fp
= fopen(svgpath
, "rb");
510 pb
= gdk_pixbuf_from_svg_file_stream(fp
, &err
);
513 "Error %d in gdk_pixbuf_from_svg_file_stream: %s\n",
520 gdk_pixbuf_save(pb
, pngpath
, "png", NULL
, NULL
);