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