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_rgb(cr
,
73 svgtiny_RED(path
->fill
) / 255.0,
74 svgtiny_GREEN(path
->fill
) / 255.0,
75 svgtiny_BLUE(path
->fill
) / 255.0);
76 cairo_fill_preserve(cr
);
78 if (path
->stroke
!= svgtiny_TRANSPARENT
) {
79 cairo_set_source_rgb(cr
,
80 svgtiny_RED(path
->stroke
) / 255.0,
81 svgtiny_GREEN(path
->stroke
) / 255.0,
82 svgtiny_BLUE(path
->stroke
) / 255.0);
83 cairo_set_line_width(cr
, path
->stroke_width
);
84 cairo_stroke_preserve(cr
);
89 * @brief Parse an SVG file into a diagram_t structure.
92 * A pointer to an open file stream.
94 * @return If successful, a pointer to a @c diagram_t structure is
95 * returned; if not, @c NULL is returned. You are expected to @c
96 * svgtiny_free the result if it is valid.
98 static diagram_t
* svgtiny_diagram_from_file(FILE* fp
, int width
, int height
) {
106 /* Find the size of the file stream */
107 fseek(fp
, 0L, SEEK_END
);
108 bytecount
= ftell(fp
);
111 buffer
= malloc(bytecount
);
113 fprintf(stderr
, "Unable to allocate %zd bytes\n", bytecount
);
117 bytesread
= fread(buffer
, 1, bytecount
, fp
);
118 if (bytesread
!= bytecount
) {
119 fprintf(stderr
, "Read only %zd of %zd bytes from stream\n",
125 diagram
= svgtiny_create();
127 fprintf(stderr
, "svgtiny_create() failed\n");
131 code
= svgtiny_parse(diagram
, buffer
, bytecount
, "", width
, height
);
134 if (code
!= svgtiny_OK
) {
135 fprintf(stderr
, "svgtiny_parse failed: ");
137 case svgtiny_OUT_OF_MEMORY
:
138 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
140 case svgtiny_LIBDOM_ERROR
:
141 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
143 case svgtiny_NOT_SVG
:
144 fprintf(stderr
, "svgtiny_NOT_SVG");
146 case svgtiny_SVG_ERROR
:
147 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
149 diagram
->error_message
);
152 fprintf(stderr
, "unknown svgtiny_code %i", code
);
155 fprintf(stderr
, "\n");
163 * @brief Create a cairo context from a libsvgtiny diagram.
166 * A pointer to a valid libsvgtiny diagram.
168 * @return If successful, a pointer to a @c cairo_t context structure
169 * is returned; if not, @c NULL is returned. You are expected to @c
170 * cairo_destroy the result if it is valid.
172 static cairo_t
* cairo_context_from_diagram(diagram_t
* diagram
) {
174 cairo_surface_t
* surface
;
178 surface
= cairo_image_surface_create(CAIRO_FORMAT_RGB24
,
182 crs
= cairo_surface_status(surface
);
183 if (crs
!= CAIRO_STATUS_SUCCESS
) {
185 "cairo_image_surface_create failed: %s\n",
186 cairo_status_to_string(crs
));
187 cairo_surface_destroy(surface
);
191 cr
= cairo_create(surface
);
192 crs
= cairo_status(cr
);
194 /* Immediately destroy the surface which is now accessible as
196 cairo_surface_destroy(surface
);
198 if (crs
!= CAIRO_STATUS_SUCCESS
) {
200 "cairo_create failed: %s\n",
201 cairo_status_to_string(crs
));
206 cairo_set_source_rgb(cr
, 1, 1, 1);
209 /* Loop through the shapes in the diagram... */
210 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
212 /* If this shape is a path, just render it. */
213 if (diagram
->shape
[i
].path
) {
214 render_path(cr
, &diagram
->shape
[i
]);
217 /* If this shape is text... */
218 if (diagram
->shape
[i
].text
) {
219 /* Figure out what color to use from the R/G/B components of the
221 cairo_set_source_rgb(cr
,
222 svgtiny_RED(diagram
->shape
[i
].stroke
) / 255.0,
223 svgtiny_GREEN(diagram
->shape
[i
].stroke
) / 255.0,
224 svgtiny_BLUE(diagram
->shape
[i
].stroke
) / 255.0);
225 /* Then move to the actual position of the text within the
228 diagram
->shape
[i
].text_x
,
229 diagram
->shape
[i
].text_y
);
232 cairo_show_text(cr
, diagram
->shape
[i
].text
);
237 /* Check the status again just for good measure? */
238 crs
= cairo_status(cr
);
239 if (crs
!= CAIRO_STATUS_SUCCESS
) {
242 cairo_status_to_string(crs
));
250 int main(int argc
, char** argv
) {
260 /* Parse arguments, and maybe print usage */
262 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
263 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
270 fp
= fopen(svgpath
, "rb");
276 diagram
= svgtiny_diagram_from_file(fp
, VIEWPORT_WIDTH
, VIEWPORT_HEIGHT
);
281 cr
= cairo_context_from_diagram(diagram
);
283 svgtiny_free(diagram
);
287 /* We're using the diagram width and height and not the viewport
288 * width/height for the image. This has the potential to create an
289 * image with a different size and aspect ratio than the viewport,
290 * but since our viewport is entirely made-up... I don't know. This
291 * relies on libsvgtiny being good at scaling/stretching/etc an SVG
292 * that may only have partial width/height/viewBox information.
294 pb
= gdk_pixbuf_get_from_surface(cairo_get_target(cr
),
302 gdk_pixbuf_save(pb
, pngpath
, "png", NULL
, NULL
);