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