]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - io-svg.c
io-svg.c: whitespace cleanup.
[libsvgtiny-pixbuf.git] / io-svg.c
1 #include <stdio.h> /* fprintf, printf */
2 #include <string.h> /* memcpy */
3
4 #include <cairo.h>
5 #include <gdk/gdk.h>
6 #include <gdk-pixbuf/gdk-pixbuf.h> /* includes glib.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 typedef struct {
25 GdkPixbufModuleUpdatedFunc updated_func;
26 GdkPixbufModulePreparedFunc prepared_func;
27 GdkPixbufModuleSizeFunc size_func;
28 gpointer user_data;
29
30 /* The SVG "file" that we're building in memory. */
31 char* svg_data;
32
33 /* How far into svg_data are we? This should always point to the
34 next empty byte. If (for example) svg_data_size is 2, then
35 svg_data[0] and svg_data[1] are used, but svg_data[2] is free. */
36 size_t svg_data_size;
37
38 } SvgTinyContext;
39
40
41 /**
42 * @brief Render an svgtiny path using cairo.
43 *
44 * @param cr
45 * A pointer to a valid cairo context.
46 *
47 * @param path
48 * A pointer to an svgtiny shape that will be rendered on the
49 * cairo context's target surface.
50 */
51 static void render_path(cairo_t* cr, shape_t* path) {
52 unsigned int j;
53
54 cairo_new_path(cr);
55 for (j = 0; j != path->path_length; ) {
56 switch ((int) path->path[j]) {
57 case svgtiny_PATH_MOVE:
58 cairo_move_to(cr,
59 path->path[j + 1],
60 path->path[j + 2]);
61 j += 3;
62 break;
63 case svgtiny_PATH_CLOSE:
64 cairo_close_path(cr);
65 j += 1;
66 break;
67 case svgtiny_PATH_LINE:
68 cairo_line_to(cr,
69 path->path[j + 1],
70 path->path[j + 2]);
71 j += 3;
72 break;
73 case svgtiny_PATH_BEZIER:
74 cairo_curve_to(cr,
75 path->path[j + 1],
76 path->path[j + 2],
77 path->path[j + 3],
78 path->path[j + 4],
79 path->path[j + 5],
80 path->path[j + 6]);
81 j += 7;
82 }
83 }
84 if (path->fill != svgtiny_TRANSPARENT) {
85 cairo_set_source_rgba(cr,
86 svgtiny_RED(path->fill) / 255.0,
87 svgtiny_GREEN(path->fill) / 255.0,
88 svgtiny_BLUE(path->fill) / 255.0,
89 1);
90 cairo_fill_preserve(cr);
91 }
92 if (path->stroke != svgtiny_TRANSPARENT) {
93 cairo_set_source_rgba(cr,
94 svgtiny_RED(path->stroke) / 255.0,
95 svgtiny_GREEN(path->stroke) / 255.0,
96 svgtiny_BLUE(path->stroke) / 255.0,
97 1);
98 cairo_set_line_width(cr, path->stroke_width);
99 cairo_stroke_preserve(cr);
100 }
101 }
102
103 /**
104 * @brief Parse a buffer of SVG data into a diagram_t structure.
105 *
106 * @param buffer
107 * The buffer containing the SVG document.
108 *
109 * @param bytecount
110 * The number of bytes in @c buffer.
111 *
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.
115 */
116 static diagram_t* svgtiny_diagram_from_buffer(char* buffer,
117 size_t bytecount,
118 int width,
119 int height,
120 GError** error) {
121 diagram_t* diagram;
122 svgtiny_code code;
123
124 diagram = svgtiny_create();
125 if (!diagram) {
126 g_set_error_literal(error,
127 GDK_PIXBUF_ERROR,
128 GDK_PIXBUF_ERROR_FAILED,
129 "svgtiny_create() failed");
130 return NULL;
131 }
132
133 code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
134 free(buffer);
135
136 if (code != svgtiny_OK) {
137 switch (code) {
138 case svgtiny_OUT_OF_MEMORY:
139 g_set_error_literal(error,
140 G_FILE_ERROR,
141 G_FILE_ERROR_NOMEM,
142 "out of memory in svgtiny_parse()");
143 break;
144 case svgtiny_LIBDOM_ERROR:
145 g_set_error_literal(error,
146 GDK_PIXBUF_ERROR,
147 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
148 "libdom error in svgtiny_parse()");
149 break;
150 case svgtiny_NOT_SVG:
151 g_set_error_literal(error,
152 GDK_PIXBUF_ERROR,
153 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
154 "encountered svgtiny_NOT_SVG in svgtiny_parse()");
155 break;
156 case svgtiny_SVG_ERROR:
157 g_set_error(error,
158 GDK_PIXBUF_ERROR,
159 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
160 "SVG error in svgtiny_parse() on line %i: %s",
161 diagram->error_line,
162 diagram->error_message);
163 break;
164 }
165 return NULL;
166 }
167
168 return diagram;
169 }
170
171 /**
172 * @brief Create a cairo context from a libsvgtiny diagram.
173 *
174 * @param diagram
175 * A pointer to a valid libsvgtiny diagram.
176 *
177 * @return If successful, a pointer to a @c cairo_t context structure
178 * is returned; if not, @c NULL is returned. You are expected to @c
179 * cairo_destroy the result if it is valid.
180 */
181 static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
182 cairo_t* cr;
183 cairo_surface_t* surface;
184 cairo_status_t crs;
185 unsigned int i;
186
187 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
188 diagram->width,
189 diagram->height);
190
191 crs = cairo_surface_status(surface);
192 if (crs != CAIRO_STATUS_SUCCESS) {
193 fprintf(stderr,
194 "cairo_image_surface_create failed: %s\n",
195 cairo_status_to_string(crs));
196 cairo_surface_destroy(surface);
197 return NULL;
198 }
199
200 cr = cairo_create(surface);
201 crs = cairo_status(cr);
202
203 /* Immediately destroy the surface which is now accessible as
204 cr->target */
205 cairo_surface_destroy(surface);
206
207 if (crs != CAIRO_STATUS_SUCCESS) {
208 fprintf(stderr,
209 "cairo_create failed: %s\n",
210 cairo_status_to_string(crs));
211 cairo_destroy(cr);
212 return NULL;
213 }
214
215 cairo_set_source_rgba(cr, 0, 0, 0, 0);
216 cairo_paint(cr);
217
218 /* Loop through the shapes in the diagram... */
219 for (i = 0; i != diagram->shape_count; i++) {
220
221 /* If this shape is a path, just render it. */
222 if (diagram->shape[i].path) {
223 render_path(cr, &diagram->shape[i]);
224 }
225
226 /* If this shape is text... */
227 if (diagram->shape[i].text) {
228 /* Figure out what color to use from the R/G/B components of the
229 shape's stroke. */
230 cairo_set_source_rgba(cr,
231 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
232 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
233 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0,
234 1);
235 /* Then move to the actual position of the text within the
236 shape... */
237 cairo_move_to(cr,
238 diagram->shape[i].text_x,
239 diagram->shape[i].text_y);
240
241 /* and draw it. */
242 cairo_show_text(cr, diagram->shape[i].text);
243 }
244 }
245
246
247 /* Check the status again just for good measure? */
248 crs = cairo_status(cr);
249 if (crs != CAIRO_STATUS_SUCCESS) {
250 fprintf(stderr,
251 "cairo error: %s\n",
252 cairo_status_to_string(crs));
253 cairo_destroy(cr);
254 return NULL;
255 }
256
257 return cr;
258 }
259
260 /**
261 * @brief Create a GdkPixbuf from a buffer of SVG data.
262 *
263 * @param buffer
264 * The buffer containing the SVG document.
265 *
266 * @param bytecount
267 * The number of bytes in @c buffer.
268 *
269 * @param error
270 * The address of a @c GError pointer that we use to return errors.
271 *
272 * @return If successful, a valid pointer to a @c GdkPixbuf is
273 * returned; if not, @c NULL is returned and @c error is populated.
274 */
275 static GdkPixbuf* gdk_pixbuf_from_svg_buffer(char* buffer,
276 size_t bytecount,
277 GError** error) {
278 diagram_t* diagram;
279 cairo_t* cr = 0;
280 GdkPixbuf* pb;
281 GError* sub_error = NULL;
282
283 diagram = svgtiny_diagram_from_buffer(buffer,
284 bytecount,
285 VIEWPORT_WIDTH,
286 VIEWPORT_HEIGHT,
287 &sub_error);
288 if (!diagram) {
289 g_propagate_error(error, sub_error);
290 return NULL;
291 }
292
293 cr = cairo_context_from_diagram(diagram);
294 if (!cr) {
295 svgtiny_free(diagram);
296 g_set_error_literal(error,
297 GDK_PIXBUF_ERROR,
298 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
299 "could not create Cairo surface from SVG diagram");
300 return NULL;
301 }
302
303
304 /* I've gone back and forth on this about five times: we use the
305 * diagram width and height, and not the viewport width and height.
306 * This can ultimately render an image that's larger than the
307 * viewport size, but I think GDK will resize the final pixbuf
308 * anyway. More importantly, rendering small icons at a larger
309 * (viewport) size seems to make the whole thing go ape-shit.
310 * So for now I'm back in the diagram camp.
311 */
312 pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
313 0,
314 0,
315 diagram->width,
316 diagram->height);
317
318
319 if (!pb) {
320 g_set_error_literal(error,
321 GDK_PIXBUF_ERROR,
322 GDK_PIXBUF_ERROR_FAILED,
323 "failed to obtain a GdkPixbuf from Cairo surface");
324 }
325
326 return pb;
327 }
328
329
330 static gpointer gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func,
331 GdkPixbufModulePreparedFunc prep_func,
332 GdkPixbufModuleUpdatedFunc updated_func,
333 gpointer user_data,
334 GError **error) {
335
336 SvgTinyContext* context = g_new(SvgTinyContext, 1);
337
338 context->size_func = size_func;
339 context->prepared_func = prep_func;
340 context->updated_func = updated_func;
341 context->user_data = user_data;
342
343 /* YOLO, no error checking */
344 context->svg_data = g_malloc(0);
345 context->svg_data_size = 0;
346
347 return context;
348 }
349
350 static gboolean gdk_pixbuf_load_increment(gpointer data,
351 const guchar* buf,
352 guint buf_size,
353 GError** error) {
354 size_t increment = 0;
355 SvgTinyContext* context = (SvgTinyContext*)data;
356
357 /* YOLO, no error checking */
358 context->svg_data = g_realloc(context->svg_data,
359 context->svg_data_size + buf_size);
360 memcpy(context->svg_data + context->svg_data_size, buf, buf_size);
361 context->svg_data_size += buf_size;
362
363 return TRUE;
364 }
365
366 static void emit_updated(SvgTinyContext* context, GdkPixbuf* pixbuf) {
367 if (context->updated_func != NULL) {
368 (*context->updated_func)(pixbuf,
369 0,
370 0,
371 gdk_pixbuf_get_width(pixbuf),
372 gdk_pixbuf_get_height(pixbuf),
373 context->user_data);
374 }
375 }
376
377 static void emit_prepared(SvgTinyContext* context, GdkPixbuf* pixbuf) {
378 if (context->prepared_func != NULL) {
379 (*context->prepared_func)(pixbuf, NULL, context->user_data);
380 }
381 }
382
383
384 /*
385 static void emit_size(SvgTinyContext* context, GdkPixbuf* pixbuf) {
386 int w = gdk_pixbuf_get_width(pixbuf);
387 int h = gdk_pixbuf_get_height(pixbuf);
388 if (context->size_func != NULL) {
389 (*context->size_func)(&w, &h, context->user_data);
390 }
391 }
392 */
393
394
395 static gboolean gdk_pixbuf_stop_load(gpointer data, GError **error) {
396 SvgTinyContext* context = (SvgTinyContext*)data;
397 GdkPixbuf* pixbuf = NULL;
398 gboolean result = TRUE;
399 GError* sub_error = NULL;
400
401 pixbuf = gdk_pixbuf_from_svg_buffer(context->svg_data,
402 context->svg_data_size,
403 &sub_error);
404
405 if (pixbuf != NULL) {
406 /*emit_size(context, pixbuf);*/
407 emit_prepared(context, pixbuf);
408 emit_updated(context, pixbuf);
409 g_object_unref(pixbuf);
410 }
411 else {
412 g_propagate_error(error, sub_error);
413 result = FALSE;
414 }
415 g_free(context);
416
417 return result;
418 }
419
420
421 G_MODULE_EXPORT void fill_vtable(GdkPixbufModule* module);
422 void fill_vtable(GdkPixbufModule* module) {
423 module->begin_load = gdk_pixbuf_begin_load;
424 module->load_increment = gdk_pixbuf_load_increment;
425 module->stop_load = gdk_pixbuf_stop_load;
426 }
427
428 G_MODULE_EXPORT void fill_info(GdkPixbufFormat *info);
429 void fill_info(GdkPixbufFormat* info) {
430 /* Borrowed from librsvg-2.40.21 */
431 static const GdkPixbufModulePattern signature[] = {
432 { " <svg", "* ", 100 },
433 { " <!DOCTYPE svg", "* ", 100 },
434 { NULL, NULL, 0 }
435 };
436
437 /* I'm not sure if we should support gzipped svg here? */
438 static const gchar *mime_types[] = {
439 "image/svg+xml",
440 "image/svg",
441 "image/svg-xml",
442 "image/vnd.adobe.svg+xml",
443 "text/xml-svg",
444 "image/svg+xml-compressed",
445 NULL
446 };
447
448 static const gchar *extensions[] = {
449 "svg",
450 "svgz",
451 "svg.gz",
452 NULL
453 };
454
455 info->name = "svg";
456 info->signature = (GdkPixbufModulePattern*)signature;
457 info->description = "Scalable Vector Graphics";
458 info->mime_types = (gchar**)mime_types;
459 info->extensions = (gchar**)extensions;
460 info->flags = GDK_PIXBUF_FORMAT_SCALABLE;
461 info->license = "AGPL3";
462 }
463
464
465 int main(int argc, char** argv) {
466 char* svgpath;
467 char* pngpath;
468 FILE* fp;
469 GError* err = NULL;
470 GdkPixbuf* pb;
471
472 /* Parse arguments, and maybe print usage */
473 if (argc < 3) {
474 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
475 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
476 return 2;
477 }
478
479 svgpath = argv[1];
480 pngpath = argv[2];
481
482 pb = gdk_pixbuf_new_from_file(svgpath, &err);
483 if (!pb) {
484 fprintf(stderr,
485 "Error %d in gdk_pixbuf_new_from_file: %s\n",
486 err->code,
487 err->message);
488 g_error_free(err);
489 return 1;
490 }
491
492 gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
493 g_object_unref(pb);
494 return 0;
495 }