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 /* 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
;
33 * @brief Render an svgtiny path using cairo.
36 * A pointer to a valid cairo context.
39 * A pointer to an svgtiny shape that will be rendered on the
40 * cairo context's target surface.
42 static void render_path(cairo_t
* cr
, shape_t
* path
) {
46 for (j
= 0; j
!= path
->path_length
; ) {
47 switch ((int) path
->path
[j
]) {
48 case svgtiny_PATH_MOVE
:
54 case svgtiny_PATH_CLOSE
:
58 case svgtiny_PATH_LINE
:
64 case svgtiny_PATH_BEZIER
:
75 fprintf(stderr
, "error: unmatched case in render_path\n");
79 if (path
->fill
!= svgtiny_TRANSPARENT
) {
80 cairo_set_source_rgba(cr
,
81 svgtiny_RED(path
->fill
) / 255.0,
82 svgtiny_GREEN(path
->fill
) / 255.0,
83 svgtiny_BLUE(path
->fill
) / 255.0,
85 cairo_fill_preserve(cr
);
87 if (path
->stroke
!= svgtiny_TRANSPARENT
) {
88 cairo_set_source_rgba(cr
,
89 svgtiny_RED(path
->stroke
) / 255.0,
90 svgtiny_GREEN(path
->stroke
) / 255.0,
91 svgtiny_BLUE(path
->stroke
) / 255.0,
93 cairo_set_line_width(cr
, path
->stroke_width
);
94 cairo_stroke_preserve(cr
);
99 * @brief Parse an SVG file into a diagram_t structure.
102 * A pointer to an open file stream.
104 * @return If successful, a pointer to a @c diagram_t structure is
105 * returned; if not, @c NULL is returned. You are expected to @c
106 * svgtiny_free the result if it is valid.
108 static diagram_t
* svgtiny_diagram_from_file(FILE* fp
, int width
, int height
) {
116 /* Find the size of the file stream */
117 fseek(fp
, 0L, SEEK_END
);
118 bytecount
= ftell(fp
);
121 buffer
= malloc(bytecount
);
123 fprintf(stderr
, "Unable to allocate %zd bytes\n", bytecount
);
127 bytesread
= fread(buffer
, 1, bytecount
, fp
);
128 if (bytesread
!= bytecount
) {
129 fprintf(stderr
, "Read only %zd of %zd bytes from stream\n",
135 diagram
= svgtiny_create();
137 fprintf(stderr
, "svgtiny_create() failed\n");
141 code
= svgtiny_parse(diagram
, buffer
, bytecount
, "", width
, height
);
144 if (code
!= svgtiny_OK
) {
145 fprintf(stderr
, "svgtiny_parse failed with ");
147 case svgtiny_OUT_OF_MEMORY
:
148 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
150 case svgtiny_LIBDOM_ERROR
:
151 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
153 case svgtiny_NOT_SVG
:
154 fprintf(stderr
, "svgtiny_NOT_SVG");
156 case svgtiny_SVG_ERROR
:
157 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
159 diagram
->error_message
);
162 fprintf(stderr
, "unknown svgtiny_code %i", code
);
165 fprintf(stderr
, "\n");
173 * @brief Create a cairo context from a libsvgtiny diagram.
176 * A pointer to a valid libsvgtiny diagram.
178 * @return If successful, a pointer to a @c cairo_t context structure
179 * is returned; if not, @c NULL is returned. You are expected to @c
180 * cairo_destroy the result if it is valid.
182 static cairo_t
* cairo_context_from_diagram(diagram_t
* diagram
) {
184 cairo_surface_t
* surface
;
188 surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,
192 crs
= cairo_surface_status(surface
);
193 if (crs
!= CAIRO_STATUS_SUCCESS
) {
195 "cairo_image_surface_create failed: %s\n",
196 cairo_status_to_string(crs
));
197 cairo_surface_destroy(surface
);
201 cr
= cairo_create(surface
);
202 crs
= cairo_status(cr
);
204 /* Immediately destroy the surface which is now accessible as
206 cairo_surface_destroy(surface
);
208 if (crs
!= CAIRO_STATUS_SUCCESS
) {
210 "cairo_create failed: %s\n",
211 cairo_status_to_string(crs
));
216 cairo_set_source_rgba(cr
, 0, 0, 0, 0);
219 /* Loop through the shapes in the diagram... */
220 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
222 /* If this shape is a path, just render it. */
223 if (diagram
->shape
[i
].path
) {
224 render_path(cr
, &diagram
->shape
[i
]);
227 /* If this shape is text... */
228 if (diagram
->shape
[i
].text
) {
229 /* Figure out what color to use from the R/G/B components of the
231 cairo_set_source_rgba(cr
,
232 svgtiny_RED(diagram
->shape
[i
].stroke
) / 255.0,
233 svgtiny_GREEN(diagram
->shape
[i
].stroke
) / 255.0,
234 svgtiny_BLUE(diagram
->shape
[i
].stroke
) / 255.0,
236 /* Then move to the actual position of the text within the
239 diagram
->shape
[i
].text_x
,
240 diagram
->shape
[i
].text_y
);
243 cairo_show_text(cr
, diagram
->shape
[i
].text
);
248 /* Check the status again just for good measure? */
249 crs
= cairo_status(cr
);
250 if (crs
!= CAIRO_STATUS_SUCCESS
) {
253 cairo_status_to_string(crs
));
261 static GdkPixbuf
* gdk_pixbuf_from_svg_file_stream(FILE *fp
, GError
** error
) {
266 diagram
= svgtiny_diagram_from_file(fp
, VIEWPORT_WIDTH
, VIEWPORT_HEIGHT
);
268 g_set_error_literal(error
,
270 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
271 "Could not parse SVG diagram from file");
275 cr
= cairo_context_from_diagram(diagram
);
277 svgtiny_free(diagram
);
278 g_set_error_literal(error
,
280 GDK_PIXBUF_ERROR_CORRUPT_IMAGE
,
281 "Could not create Cairo surface from SVG diagram");
286 /* We're using the viewport width and height and not the diagram
287 * width/height for the image. The diagram can be of a different
288 * size and aspect ratio than the viewport, and our main use case is
289 * for icons that are generally square and reasonably sized. If the
290 * diagram is "small," then we want to scale it up until it fits
291 * nicely in the viewport before rendering it. That's as opposed to
292 * rendering the image small, and letting GDK scale it up. Of course
293 * this reasoning makes the assumption that the viewport is usually
294 * larger than the diagram.
296 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
304 g_set_error_literal(error
,
306 GDK_PIXBUF_ERROR_FAILED
,
307 "Failed to obtain a GdkPixbuf from Cairo surface");
314 static gpointer
gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func
,
315 GdkPixbufModulePreparedFunc prep_func
,
316 GdkPixbufModuleUpdatedFunc updated_func
,
320 SvgTinyContext
* context
= g_new0(SvgTinyContext
, 1);
322 context
->size_func
= size_func
;
323 context
->prepared_func
= prep_func
;
324 context
->updated_func
= updated_func
;
325 context
->user_data
= user_data
;
330 static gboolean
gdk_pixbuf_load_increment(gpointer data
,
334 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
336 if (!rsvg_handle_write (context->handle, buf, size, error)) {
337 rsvg_propagate_error (error, _("Error writing"), ERROR_WRITING);
344 static void emit_updated(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
345 if (context
->updated_func
!= NULL
) {
346 (*context
->updated_func
)(pixbuf
,
349 gdk_pixbuf_get_width(pixbuf
),
350 gdk_pixbuf_get_height(pixbuf
),
355 static void emit_prepared(SvgTinyContext
* context
, GdkPixbuf
* pixbuf
) {
356 if (context
->prepared_func
!= NULL
) {
357 (*context
->prepared_func
)(pixbuf
, NULL
, context
->user_data
);
361 static gboolean
gdk_pixbuf_stop_load(gpointer data
, GError
**error
) {
362 SvgTinyContext
* context
= (SvgTinyContext
*)data
;
363 GdkPixbuf
* pixbuf
= NULL
;
364 gboolean result
= TRUE
;
366 if (pixbuf
!= NULL
) {
367 emit_prepared(context
, pixbuf
);
368 emit_updated(context
, pixbuf
);
369 g_object_unref(pixbuf
);
380 G_MODULE_EXPORT
void fill_vtable(GdkPixbufModule
* module);
381 void fill_vtable(GdkPixbufModule
* module) {
382 module->begin_load
= gdk_pixbuf_begin_load
;
383 module->load_increment
= gdk_pixbuf_load_increment
;
384 module->stop_load
= gdk_pixbuf_stop_load
;
385 module->load
= gdk_pixbuf_from_svg_file_stream
;
388 G_MODULE_EXPORT
void fill_info(GdkPixbufFormat
*info
);
389 void fill_info(GdkPixbufFormat
* info
) {
390 /* Borrowed from librsvg-2.40.21 */
391 static const GdkPixbufModulePattern signature
[] = {
392 { " <svg", "* ", 100 },
393 { " <!DOCTYPE svg", "* ", 100 },
397 /* I'm not sure if we should support gzipped svg here? */
398 static const gchar
*mime_types
[] = {
402 "image/vnd.adobe.svg+xml",
404 "image/svg+xml-compressed",
408 static const gchar
*extensions
[] = {
416 info
->signature
= (GdkPixbufModulePattern
*) signature
;
417 info
->description
= "Scalable Vector Graphics";
418 info
->mime_types
= (gchar
**) mime_types
;
419 info
->extensions
= (gchar
**) extensions
;
420 info
->flags
= GDK_PIXBUF_FORMAT_SCALABLE
;
421 info
->license
= "AGPL3";
425 int main(int argc
, char** argv
) {
432 /* Parse arguments, and maybe print usage */
434 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
435 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
442 fp
= fopen(svgpath
, "rb");
448 pb
= gdk_pixbuf_from_svg_file_stream(fp
, &err
);
451 "Error %d in gdk_pixbuf_from_svg_file_stream: %s\n",
458 gdk_pixbuf_save(pb
, pngpath
, "png", NULL
, NULL
);