]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - io-svg.c
io-svg.c: remove code for non-incremental loading.
[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 #define SVG_BUFFER_INCREMENT (size_t)4194304
20
21 /* Convenient typedefs for libsvgtiny */
22 typedef struct svgtiny_diagram diagram_t;
23 typedef struct svgtiny_shape shape_t;
24
25
26 typedef struct {
27 GdkPixbufModuleUpdatedFunc updated_func;
28 GdkPixbufModulePreparedFunc prepared_func;
29 GdkPixbufModuleSizeFunc size_func;
30 gpointer user_data;
31
32 /* The "file" */
33 char* svg_data;
34 size_t svg_data_size;
35 size_t svg_data_max;
36
37 } SvgTinyContext;
38
39
40 /**
41 * @brief Render an svgtiny path using cairo.
42 *
43 * @param cr
44 * A pointer to a valid cairo context.
45 *
46 * @param path
47 * A pointer to an svgtiny shape that will be rendered on the
48 * cairo context's target surface.
49 */
50 static void render_path(cairo_t* cr, shape_t* path) {
51 unsigned int j;
52
53 cairo_new_path(cr);
54 for (j = 0; j != path->path_length; ) {
55 switch ((int) path->path[j]) {
56 case svgtiny_PATH_MOVE:
57 cairo_move_to(cr,
58 path->path[j + 1],
59 path->path[j + 2]);
60 j += 3;
61 break;
62 case svgtiny_PATH_CLOSE:
63 cairo_close_path(cr);
64 j += 1;
65 break;
66 case svgtiny_PATH_LINE:
67 cairo_line_to(cr,
68 path->path[j + 1],
69 path->path[j + 2]);
70 j += 3;
71 break;
72 case svgtiny_PATH_BEZIER:
73 cairo_curve_to(cr,
74 path->path[j + 1],
75 path->path[j + 2],
76 path->path[j + 3],
77 path->path[j + 4],
78 path->path[j + 5],
79 path->path[j + 6]);
80 j += 7;
81 }
82 }
83 if (path->fill != svgtiny_TRANSPARENT) {
84 cairo_set_source_rgba(cr,
85 svgtiny_RED(path->fill) / 255.0,
86 svgtiny_GREEN(path->fill) / 255.0,
87 svgtiny_BLUE(path->fill) / 255.0,
88 1);
89 cairo_fill_preserve(cr);
90 }
91 if (path->stroke != svgtiny_TRANSPARENT) {
92 cairo_set_source_rgba(cr,
93 svgtiny_RED(path->stroke) / 255.0,
94 svgtiny_GREEN(path->stroke) / 255.0,
95 svgtiny_BLUE(path->stroke) / 255.0,
96 1);
97 cairo_set_line_width(cr, path->stroke_width);
98 cairo_stroke_preserve(cr);
99 }
100 }
101
102 /**
103 * @brief Parse a buffer of SVG data into a diagram_t structure.
104 *
105 * @param buffer
106 * The buffer containing the SVG document.
107 *
108 * @param bytecount
109 * The number of bytes in @c buffer.
110 *
111 * @return If successful, a pointer to a @c diagram_t structure is
112 * returned; if not, @c NULL is returned. You are expected to @c
113 * svgtiny_free the result if it is valid.
114 */
115 static diagram_t* svgtiny_diagram_from_buffer(char* buffer,
116 size_t bytecount,
117 int width,
118 int height,
119 GError** error) {
120 diagram_t* diagram;
121 svgtiny_code code;
122
123 diagram = svgtiny_create();
124 if (!diagram) {
125 g_set_error_literal(error,
126 GDK_PIXBUF_ERROR,
127 GDK_PIXBUF_ERROR_FAILED,
128 "svgtiny_create() failed");
129 return NULL;
130 }
131
132 code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
133 free(buffer);
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 "libdom error 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 "encountered svgtiny_NOT_SVG 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 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 /* We're using the viewport width and height and not the diagram
304 * width/height for the image. The diagram can be of a different
305 * size and aspect ratio than the viewport, and our main use case is
306 * for icons that are generally square and reasonably sized. If the
307 * diagram is "small," then we want to scale it up until it fits
308 * nicely in the viewport before rendering it. That's as opposed to
309 * rendering the image small, and letting GDK scale it up. Of course
310 * this reasoning makes the assumption that the viewport is usually
311 * larger than the diagram.
312 */
313 pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
314 0,
315 0,
316 VIEWPORT_WIDTH,
317 VIEWPORT_HEIGHT);
318
319
320 if (!pb) {
321 g_set_error_literal(error,
322 GDK_PIXBUF_ERROR,
323 GDK_PIXBUF_ERROR_FAILED,
324 "failed to obtain a GdkPixbuf from Cairo surface");
325 }
326
327 return pb;
328 }
329
330
331 static gpointer gdk_pixbuf_begin_load(GdkPixbufModuleSizeFunc size_func,
332 GdkPixbufModulePreparedFunc prep_func,
333 GdkPixbufModuleUpdatedFunc updated_func,
334 gpointer user_data,
335 GError **error) {
336
337 SvgTinyContext* context = g_new(SvgTinyContext, 1);
338
339 context->size_func = size_func;
340 context->prepared_func = prep_func;
341 context->updated_func = updated_func;
342 context->user_data = user_data;
343
344 /* YOLO, no error checking */
345 context->svg_data = g_malloc(SVG_BUFFER_INCREMENT);
346 context->svg_data_size = 0;
347
348 return context;
349 }
350
351 static gboolean gdk_pixbuf_load_increment(gpointer data,
352 const guchar* buf,
353 guint size,
354 GError** error) {
355 size_t increment = 0;
356 SvgTinyContext* context = (SvgTinyContext*)data;
357
358 if (context->svg_data_size + size > context->svg_data_max) {
359 if (size > SVG_BUFFER_INCREMENT) {
360 increment = size;
361 }
362 else {
363 increment = SVG_BUFFER_INCREMENT;
364 }
365
366 /* YOLO, no error checking */
367 context->svg_data = g_realloc(context->svg_data,
368 context->svg_data_max + increment);
369
370 context->svg_data_max += increment;
371 }
372
373 memcpy(context->svg_data + context->svg_data_size, buf, size);
374 context->svg_data_size += size;
375
376 return TRUE;
377 }
378
379 static void emit_updated(SvgTinyContext* context, GdkPixbuf* pixbuf) {
380 if (context->updated_func != NULL) {
381 (*context->updated_func)(pixbuf,
382 0,
383 0,
384 gdk_pixbuf_get_width(pixbuf),
385 gdk_pixbuf_get_height(pixbuf),
386 context->user_data);
387 }
388 }
389
390 static void emit_prepared(SvgTinyContext* context, GdkPixbuf* pixbuf) {
391 if (context->prepared_func != NULL) {
392 (*context->prepared_func)(pixbuf, NULL, context->user_data);
393 }
394 }
395
396
397 /*
398 static void emit_size(SvgTinyContext* context, GdkPixbuf* pixbuf) {
399 int w = gdk_pixbuf_get_width(pixbuf);
400 int h = gdk_pixbuf_get_height(pixbuf);
401 if (context->size_func != NULL) {
402 (*context->size_func)(&w, &h, context->user_data);
403 }
404 }
405 */
406
407
408 static gboolean gdk_pixbuf_stop_load(gpointer data, GError **error) {
409 SvgTinyContext* context = (SvgTinyContext*)data;
410 GdkPixbuf* pixbuf = NULL;
411 gboolean result = TRUE;
412 GError* sub_error = NULL;
413
414 pixbuf = gdk_pixbuf_from_svg_buffer(context->svg_data,
415 context->svg_data_size,
416 &sub_error);
417
418 if (pixbuf != NULL) {
419 /*emit_size(context, pixbuf);*/
420 emit_prepared(context, pixbuf);
421 emit_updated(context, pixbuf);
422 g_object_unref(pixbuf);
423 }
424 else {
425 g_propagate_error(error, sub_error);
426 result = FALSE;
427 }
428 g_free(context);
429
430 return result;
431 }
432
433
434 G_MODULE_EXPORT void fill_vtable(GdkPixbufModule* module);
435 void fill_vtable(GdkPixbufModule* module) {
436 module->begin_load = gdk_pixbuf_begin_load;
437 module->load_increment = gdk_pixbuf_load_increment;
438 module->stop_load = gdk_pixbuf_stop_load;
439 }
440
441 G_MODULE_EXPORT void fill_info(GdkPixbufFormat *info);
442 void fill_info(GdkPixbufFormat* info) {
443 /* Borrowed from librsvg-2.40.21 */
444 static const GdkPixbufModulePattern signature[] = {
445 { " <svg", "* ", 100 },
446 { " <!DOCTYPE svg", "* ", 100 },
447 { NULL, NULL, 0 }
448 };
449
450 /* I'm not sure if we should support gzipped svg here? */
451 static const gchar *mime_types[] = {
452 "image/svg+xml",
453 "image/svg",
454 "image/svg-xml",
455 "image/vnd.adobe.svg+xml",
456 "text/xml-svg",
457 "image/svg+xml-compressed",
458 NULL
459 };
460
461 static const gchar *extensions[] = {
462 "svg",
463 "svgz",
464 "svg.gz",
465 NULL
466 };
467
468 info->name = "svg";
469 info->signature = (GdkPixbufModulePattern*)signature;
470 info->description = "Scalable Vector Graphics";
471 info->mime_types = (gchar**)mime_types;
472 info->extensions = (gchar**)extensions;
473 info->flags = GDK_PIXBUF_FORMAT_SCALABLE;
474 info->license = "AGPL3";
475 }
476
477
478 int main(int argc, char** argv) {
479 char* svgpath;
480 char* pngpath;
481 FILE* fp;
482 GError* err = NULL;
483 GdkPixbuf* pb;
484
485 /* Parse arguments, and maybe print usage */
486 if (argc < 3) {
487 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
488 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
489 return 2;
490 }
491
492 svgpath = argv[1];
493 pngpath = argv[2];
494
495 pb = gdk_pixbuf_new_from_file(svgpath, &err);
496 if (!pb) {
497 fprintf(stderr,
498 "Error %d in gdk_pixbuf_new_from_file: %s\n",
499 err->code,
500 err->message);
501 g_error_free(err);
502 return 1;
503 }
504
505 gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
506 g_object_unref(pb);
507 return 0;
508 }