]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - example.c
example.c: improve GError handling.
[libsvgtiny-pixbuf.git] / example.c
1 #include <stdio.h> /* fopen, fprintf, fread, printf */
2 #include <stdlib.h> /* malloc */
3
4 #include <cairo.h>
5 #include <gdk/gdk.h>
6 #include <gdk-pixbuf/gdk-pixbuf.h>
7 #include <svgtiny.h>
8
9 /*
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.
15 */
16 #define VIEWPORT_WIDTH 512
17 #define VIEWPORT_HEIGHT 512
18
19 /* Convenient typedefs for libsvgtiny */
20 typedef struct svgtiny_diagram diagram_t;
21 typedef struct svgtiny_shape shape_t;
22
23
24 /**
25 * @brief Render an svgtiny path using cairo.
26 *
27 * @param cr
28 * A pointer to a valid cairo context.
29 *
30 * @param path
31 * A pointer to an svgtiny shape that will be rendered on the
32 * cairo context's target surface.
33 */
34 static void render_path(cairo_t* cr, shape_t* path) {
35 unsigned int j;
36
37 cairo_new_path(cr);
38 for (j = 0; j != path->path_length; ) {
39 switch ((int) path->path[j]) {
40 case svgtiny_PATH_MOVE:
41 cairo_move_to(cr,
42 path->path[j + 1],
43 path->path[j + 2]);
44 j += 3;
45 break;
46 case svgtiny_PATH_CLOSE:
47 cairo_close_path(cr);
48 j += 1;
49 break;
50 case svgtiny_PATH_LINE:
51 cairo_line_to(cr,
52 path->path[j + 1],
53 path->path[j + 2]);
54 j += 3;
55 break;
56 case svgtiny_PATH_BEZIER:
57 cairo_curve_to(cr,
58 path->path[j + 1],
59 path->path[j + 2],
60 path->path[j + 3],
61 path->path[j + 4],
62 path->path[j + 5],
63 path->path[j + 6]);
64 j += 7;
65 break;
66 default:
67 fprintf(stderr, "error: unmatched case in render_path\n");
68 j += 1;
69 }
70 }
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,
76 1);
77 cairo_fill_preserve(cr);
78 }
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,
84 1);
85 cairo_set_line_width(cr, path->stroke_width);
86 cairo_stroke_preserve(cr);
87 }
88 }
89
90 /**
91 * @brief Parse an SVG file into a diagram_t structure.
92 *
93 * @param fp
94 * A pointer to an open file stream.
95 *
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.
99 */
100 static diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
101 diagram_t* diagram;
102
103 size_t bytecount;
104 char* buffer;
105 size_t bytesread;
106 svgtiny_code code;
107
108 /* Find the size of the file stream */
109 fseek(fp, 0L, SEEK_END);
110 bytecount = ftell(fp);
111 rewind(fp);
112
113 buffer = malloc(bytecount);
114 if (!buffer) {
115 fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount);
116 return NULL;
117 }
118
119 bytesread = fread(buffer, 1, bytecount, fp);
120 if (bytesread != bytecount) {
121 fprintf(stderr, "Read only %zd of %zd bytes from stream\n",
122 bytesread,
123 bytecount);
124 }
125 fclose(fp);
126
127 diagram = svgtiny_create();
128 if (!diagram) {
129 fprintf(stderr, "svgtiny_create() failed\n");
130 return NULL;
131 }
132
133 code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
134 free(buffer);
135
136 if (code != svgtiny_OK) {
137 fprintf(stderr, "svgtiny_parse failed with ");
138 switch (code) {
139 case svgtiny_OUT_OF_MEMORY:
140 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
141 break;
142 case svgtiny_LIBDOM_ERROR:
143 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
144 break;
145 case svgtiny_NOT_SVG:
146 fprintf(stderr, "svgtiny_NOT_SVG");
147 break;
148 case svgtiny_SVG_ERROR:
149 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
150 diagram->error_line,
151 diagram->error_message);
152 break;
153 default:
154 fprintf(stderr, "unknown svgtiny_code %i", code);
155 break;
156 }
157 fprintf(stderr, "\n");
158 return NULL;
159 }
160
161 return diagram;
162 }
163
164 /**
165 * @brief Create a cairo context from a libsvgtiny diagram.
166 *
167 * @param diagram
168 * A pointer to a valid libsvgtiny diagram.
169 *
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.
173 */
174 static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
175 cairo_t* cr;
176 cairo_surface_t* surface;
177 cairo_status_t crs;
178 unsigned int i;
179
180 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
181 diagram->width,
182 diagram->height);
183
184 crs = cairo_surface_status(surface);
185 if (crs != CAIRO_STATUS_SUCCESS) {
186 fprintf(stderr,
187 "cairo_image_surface_create failed: %s\n",
188 cairo_status_to_string(crs));
189 cairo_surface_destroy(surface);
190 return NULL;
191 }
192
193 cr = cairo_create(surface);
194 crs = cairo_status(cr);
195
196 /* Immediately destroy the surface which is now accessible as
197 cr->target */
198 cairo_surface_destroy(surface);
199
200 if (crs != CAIRO_STATUS_SUCCESS) {
201 fprintf(stderr,
202 "cairo_create failed: %s\n",
203 cairo_status_to_string(crs));
204 cairo_destroy(cr);
205 return NULL;
206 }
207
208 cairo_set_source_rgba(cr, 0, 0, 0, 0);
209 cairo_paint(cr);
210
211 /* Loop through the shapes in the diagram... */
212 for (i = 0; i != diagram->shape_count; i++) {
213
214 /* If this shape is a path, just render it. */
215 if (diagram->shape[i].path) {
216 render_path(cr, &diagram->shape[i]);
217 }
218
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
222 shape's stroke. */
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,
227 1);
228 /* Then move to the actual position of the text within the
229 shape... */
230 cairo_move_to(cr,
231 diagram->shape[i].text_x,
232 diagram->shape[i].text_y);
233
234 /* and draw it. */
235 cairo_show_text(cr, diagram->shape[i].text);
236 }
237 }
238
239
240 /* Check the status again just for good measure? */
241 crs = cairo_status(cr);
242 if (crs != CAIRO_STATUS_SUCCESS) {
243 fprintf(stderr,
244 "cairo error: %s\n",
245 cairo_status_to_string(crs));
246 cairo_destroy(cr);
247 return NULL;
248 }
249
250 return cr;
251 }
252
253 static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError** error) {
254 diagram_t* diagram;
255 cairo_t* cr = 0;
256 GdkPixbuf* pb;
257
258 diagram = svgtiny_diagram_from_file(fp, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
259 if (!diagram) {
260 g_set_error_literal(error,
261 GDK_PIXBUF_ERROR,
262 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
263 "Could not parse SVG diagram from file");
264 return NULL;
265 }
266
267 cr = cairo_context_from_diagram(diagram);
268 if (!cr) {
269 svgtiny_free(diagram);
270 g_set_error_literal(error,
271 GDK_PIXBUF_ERROR,
272 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
273 "Could not create Cairo surface from SVG diagram");
274 return NULL;
275 }
276
277
278 /* We're using the viewport width and height and not the diagram
279 * width/height for the image. The diagram can be of a different
280 * size and aspect ratio than the viewport, and our main use case is
281 * for icons that are generally square and reasonably sized. If the
282 * diagram is "small," then we want to scale it up until it fits
283 * nicely in the viewport before rendering it. That's as opposed to
284 * rendering the image small, and letting GDK scale it up. Of course
285 * this reasoning makes the assumption that the viewport is usually
286 * larger than the diagram.
287 */
288 pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
289 0,
290 0,
291 VIEWPORT_WIDTH,
292 VIEWPORT_HEIGHT);
293
294
295 if (!pb) {
296 g_set_error_literal(error,
297 GDK_PIXBUF_ERROR,
298 GDK_PIXBUF_ERROR_FAILED,
299 "Failed to obtain a GdkPixbuf from Cairo surface");
300 }
301
302 return pb;
303 }
304
305
306 G_MODULE_EXPORT void fill_vtable(GdkPixbufModule* module);
307 void fill_vtable(GdkPixbufModule* module) {
308 module->load = gdk_pixbuf_from_svg_file_stream;
309 }
310
311 G_MODULE_EXPORT void fill_info(GdkPixbufFormat *info);
312 void fill_info(GdkPixbufFormat* info) {
313 /* Borrowed from librsvg-2.40.21 */
314 static const GdkPixbufModulePattern signature[] = {
315 { " <svg", "* ", 100 },
316 { " <!DOCTYPE svg", "* ", 100 },
317 { NULL, NULL, 0 }
318 };
319
320 /* I'm not sure if we should support gzipped svg here? */
321 static const gchar *mime_types[] = {
322 "image/svg+xml",
323 "image/svg",
324 "image/svg-xml",
325 "image/vnd.adobe.svg+xml",
326 "text/xml-svg",
327 "image/svg+xml-compressed",
328 NULL
329 };
330
331 static const gchar *extensions[] = {
332 "svg",
333 "svgz",
334 "svg.gz",
335 NULL
336 };
337
338 info->name = "svg";
339 info->signature = (GdkPixbufModulePattern*) signature;
340 info->description = "Scalable Vector Graphics";
341 info->mime_types = (gchar**) mime_types;
342 info->extensions = (gchar**) extensions;
343 info->flags = GDK_PIXBUF_FORMAT_SCALABLE;
344 info->license = "AGPL3";
345 }
346
347
348 int main(int argc, char** argv) {
349 char* svgpath;
350 char* pngpath;
351 FILE* fp;
352 GError* err = NULL;
353 GdkPixbuf* pb;
354
355 /* Parse arguments, and maybe print usage */
356 if (argc < 3) {
357 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
358 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
359 return 2;
360 }
361
362 svgpath = argv[1];
363 pngpath = argv[2];
364
365 fp = fopen(svgpath, "rb");
366 if (!fp) {
367 perror(svgpath);
368 return 1;
369 }
370
371 pb = gdk_pixbuf_from_svg_file_stream(fp, &err);
372 if (!pb) {
373 fprintf(stderr,
374 "Error %d in gdk_pixbuf_from_svg_file_stream: %s\n",
375 err->code,
376 err->message);
377 g_error_free(err);
378 return 1;
379 }
380
381 gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
382 g_object_unref(pb);
383 return 0;
384 }