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 * @brief Render an svgtiny path using cairo.
28 * A pointer to a valid cairo context.
31 * A pointer to an svgtiny shape that will be rendered on the
32 * cairo context's target surface.
34 static void render_path(cairo_t
* cr
, shape_t
* path
) {
38 for (j
= 0; j
!= path
->path_length
; ) {
39 switch ((int) path
->path
[j
]) {
40 case svgtiny_PATH_MOVE
:
46 case svgtiny_PATH_CLOSE
:
50 case svgtiny_PATH_LINE
:
56 case svgtiny_PATH_BEZIER
:
71 if (path
->fill
!= svgtiny_TRANSPARENT
) {
72 cairo_set_source_rgba(cr
,
73 svgtiny_RED(path
->fill
) / 255.0,
74 svgtiny_GREEN(path
->fill
) / 255.0,
75 svgtiny_BLUE(path
->fill
) / 255.0,
77 cairo_fill_preserve(cr
);
79 if (path
->stroke
!= svgtiny_TRANSPARENT
) {
80 cairo_set_source_rgba(cr
,
81 svgtiny_RED(path
->stroke
) / 255.0,
82 svgtiny_GREEN(path
->stroke
) / 255.0,
83 svgtiny_BLUE(path
->stroke
) / 255.0,
85 cairo_set_line_width(cr
, path
->stroke_width
);
86 cairo_stroke_preserve(cr
);
91 * @brief Parse an SVG file into a diagram_t structure.
94 * A pointer to an open file stream.
96 * @return If successful, a pointer to a @c diagram_t structure is
97 * returned; if not, @c NULL is returned. You are expected to @c
98 * svgtiny_free the result if it is valid.
100 static diagram_t
* svgtiny_diagram_from_file(FILE* fp
, int width
, int height
) {
108 /* Find the size of the file stream */
109 fseek(fp
, 0L, SEEK_END
);
110 bytecount
= ftell(fp
);
113 buffer
= malloc(bytecount
);
115 fprintf(stderr
, "Unable to allocate %zd bytes\n", bytecount
);
119 bytesread
= fread(buffer
, 1, bytecount
, fp
);
120 if (bytesread
!= bytecount
) {
121 fprintf(stderr
, "Read only %zd of %zd bytes from stream\n",
127 diagram
= svgtiny_create();
129 fprintf(stderr
, "svgtiny_create() failed\n");
133 code
= svgtiny_parse(diagram
, buffer
, bytecount
, "", width
, height
);
136 if (code
!= svgtiny_OK
) {
137 fprintf(stderr
, "svgtiny_parse failed: ");
139 case svgtiny_OUT_OF_MEMORY
:
140 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
142 case svgtiny_LIBDOM_ERROR
:
143 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
145 case svgtiny_NOT_SVG
:
146 fprintf(stderr
, "svgtiny_NOT_SVG");
148 case svgtiny_SVG_ERROR
:
149 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
151 diagram
->error_message
);
154 fprintf(stderr
, "unknown svgtiny_code %i", code
);
157 fprintf(stderr
, "\n");
165 * @brief Create a cairo context from a libsvgtiny diagram.
168 * A pointer to a valid libsvgtiny diagram.
170 * @return If successful, a pointer to a @c cairo_t context structure
171 * is returned; if not, @c NULL is returned. You are expected to @c
172 * cairo_destroy the result if it is valid.
174 static cairo_t
* cairo_context_from_diagram(diagram_t
* diagram
) {
176 cairo_surface_t
* surface
;
180 surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,
184 crs
= cairo_surface_status(surface
);
185 if (crs
!= CAIRO_STATUS_SUCCESS
) {
187 "cairo_image_surface_create failed: %s\n",
188 cairo_status_to_string(crs
));
189 cairo_surface_destroy(surface
);
193 cr
= cairo_create(surface
);
194 crs
= cairo_status(cr
);
196 /* Immediately destroy the surface which is now accessible as
198 cairo_surface_destroy(surface
);
200 if (crs
!= CAIRO_STATUS_SUCCESS
) {
202 "cairo_create failed: %s\n",
203 cairo_status_to_string(crs
));
208 cairo_set_source_rgba(cr
, 0, 0, 0, 0);
211 /* Loop through the shapes in the diagram... */
212 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
214 /* If this shape is a path, just render it. */
215 if (diagram
->shape
[i
].path
) {
216 render_path(cr
, &diagram
->shape
[i
]);
219 /* If this shape is text... */
220 if (diagram
->shape
[i
].text
) {
221 /* Figure out what color to use from the R/G/B components of the
223 cairo_set_source_rgba(cr
,
224 svgtiny_RED(diagram
->shape
[i
].stroke
) / 255.0,
225 svgtiny_GREEN(diagram
->shape
[i
].stroke
) / 255.0,
226 svgtiny_BLUE(diagram
->shape
[i
].stroke
) / 255.0,
228 /* Then move to the actual position of the text within the
231 diagram
->shape
[i
].text_x
,
232 diagram
->shape
[i
].text_y
);
235 cairo_show_text(cr
, diagram
->shape
[i
].text
);
240 /* Check the status again just for good measure? */
241 crs
= cairo_status(cr
);
242 if (crs
!= CAIRO_STATUS_SUCCESS
) {
245 cairo_status_to_string(crs
));
253 int main(int argc
, char** argv
) {
263 /* Parse arguments, and maybe print usage */
265 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
266 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
273 fp
= fopen(svgpath
, "rb");
279 diagram
= svgtiny_diagram_from_file(fp
, VIEWPORT_WIDTH
, VIEWPORT_HEIGHT
);
284 cr
= cairo_context_from_diagram(diagram
);
286 svgtiny_free(diagram
);
290 /* We're using the viewport width and height and not the diagram
291 * width/height for the image. The diagram can be of a different
292 * size and aspect ratio than the viewport, and our main use case is
293 * for icons that are generally square and reasonably sized. If the
294 * diagram is "small," then we want to scale it up until it fits
295 * nicely in the viewport before rendering it. That's as opposed to
296 * rendering the image small, and letting GDK scale it up. Of course
297 * this reasoning makes the assumption that the viewport is usually
298 * larger than the diagram.
300 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
308 gdk_pixbuf_save(pb
, pngpath
, "png", NULL
, NULL
);
316 static GdkPixbuf
* gdk_pixbuf_from_svg_file_stream(FILE *fp
, GError
**error
) {
322 diagram
= svgtiny_diagram_from_file(fp
, VIEWPORT_WIDTH
, VIEWPORT_HEIGHT
);
327 cr
= cairo_context_from_diagram(diagram
);
329 svgtiny_free(diagram
);
334 /* We're using the viewport width and height and not the diagram
335 * width/height for the image. The diagram can be of a different
336 * size and aspect ratio than the viewport, and our main use case is
337 * for icons that are generally square and reasonably sized. If the
338 * diagram is "small," then we want to scale it up until it fits
339 * nicely in the viewport before rendering it. That's as opposed to
340 * rendering the image small, and letting GDK scale it up. Of course
341 * this reasoning makes the assumption that the viewport is usually
342 * larger than the diagram.
344 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
352 fprintf(stderr
, "gdk_pixbuf_get_from_surface failed!\n");
359 G_MODULE_EXPORT
void fill_vtable(GdkPixbufModule
* module);
360 void fill_vtable(GdkPixbufModule
* module) {
361 module->load
= gdk_pixbuf_from_svg_file_stream
;
364 G_MODULE_EXPORT
void fill_info(GdkPixbufFormat
*info
);
365 void fill_info(GdkPixbufFormat
* info
) {
366 /* Borrowed from librsvg-2.40.21 */
367 static const GdkPixbufModulePattern signature
[] = {
368 { " <svg", "* ", 100 },
369 { " <!DOCTYPE svg", "* ", 100 },
373 /* I'm not sure if we should support gzipped svg here? */
374 static const gchar
*mime_types
[] = {
378 "image/vnd.adobe.svg+xml",
380 "image/svg+xml-compressed",
384 static const gchar
*extensions
[] = {
392 info
->signature
= (GdkPixbufModulePattern
*) signature
;
393 info
->description
= "Scalable Vector Graphics";
394 info
->mime_types
= (gchar
**) mime_types
;
395 info
->extensions
= (gchar
**) extensions
;
396 info
->flags
= GDK_PIXBUF_FORMAT_SCALABLE
;
397 info
->license
= "AGPL3";