+#include <stdio.h> /* fopen, fprintf, fread, printf */
+#include <stdlib.h> /* malloc */
+
+#include <cairo.h>
+#include <gdk/gdk.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <svgtiny.h>
+
+/*
+ * The width and height of the viewport that we'll render the SVG
+ * into. The final "picture" may not actually be this size; based on
+ * the height, width, viewBox, and preserveAspectRatio attributes in
+ * the SVG itself, libsvgtiny may scale, stretch, offset, etc. the
+ * paths to make them fit nicely into the viewport.
+ */
+#define VIEWPORT_WIDTH 512
+#define VIEWPORT_HEIGHT 512
+
+/* Convenient typedefs for libsvgtiny */
+typedef struct svgtiny_diagram diagram_t;
+typedef struct svgtiny_shape shape_t;
+
+
+/**
+ * @brief Render an svgtiny path using cairo.
+ *
+ * @param cr
+ * A pointer to a valid cairo context.
+ *
+ * @param path
+ * A pointer to an svgtiny shape that will be rendered on the
+ * cairo context's target surface.
+ */
+static void render_path(cairo_t* cr, shape_t* path) {
+ unsigned int j;
+
+ cairo_new_path(cr);
+ for (j = 0; j != path->path_length; ) {
+ switch ((int) path->path[j]) {
+ case svgtiny_PATH_MOVE:
+ cairo_move_to(cr,
+ path->path[j + 1],
+ path->path[j + 2]);
+ j += 3;
+ break;
+ case svgtiny_PATH_CLOSE:
+ cairo_close_path(cr);
+ j += 1;
+ break;
+ case svgtiny_PATH_LINE:
+ cairo_line_to(cr,
+ path->path[j + 1],
+ path->path[j + 2]);
+ j += 3;
+ break;
+ case svgtiny_PATH_BEZIER:
+ cairo_curve_to(cr,
+ path->path[j + 1],
+ path->path[j + 2],
+ path->path[j + 3],
+ path->path[j + 4],
+ path->path[j + 5],
+ path->path[j + 6]);
+ j += 7;
+ break;
+ default:
+ printf("error ");
+ j += 1;
+ }
+ }
+ if (path->fill != svgtiny_TRANSPARENT) {
+ cairo_set_source_rgba(cr,
+ svgtiny_RED(path->fill) / 255.0,
+ svgtiny_GREEN(path->fill) / 255.0,
+ svgtiny_BLUE(path->fill) / 255.0,
+ 1);
+ cairo_fill_preserve(cr);
+ }
+ if (path->stroke != svgtiny_TRANSPARENT) {
+ cairo_set_source_rgba(cr,
+ svgtiny_RED(path->stroke) / 255.0,
+ svgtiny_GREEN(path->stroke) / 255.0,
+ svgtiny_BLUE(path->stroke) / 255.0,
+ 1);
+ cairo_set_line_width(cr, path->stroke_width);
+ cairo_stroke_preserve(cr);
+ }
+}
+
+/**
+ * @brief Parse an SVG file into a diagram_t structure.
+ *
+ * @param fp
+ * A pointer to an open file stream.
+ *
+ * @return If successful, a pointer to a @c diagram_t structure is
+ * returned; if not, @c NULL is returned. You are expected to @c
+ * svgtiny_free the result if it is valid.
+ */
+static diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
+ diagram_t* diagram;
+
+ size_t bytecount;
+ char* buffer;
+ size_t bytesread;
+ svgtiny_code code;
+
+ /* Find the size of the file stream */
+ fseek(fp, 0L, SEEK_END);
+ bytecount = ftell(fp);
+ rewind(fp);
+
+ buffer = malloc(bytecount);
+ if (!buffer) {
+ fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount);
+ return NULL;
+ }
+
+ bytesread = fread(buffer, 1, bytecount, fp);
+ if (bytesread != bytecount) {
+ fprintf(stderr, "Read only %zd of %zd bytes from stream\n",
+ bytesread,
+ bytecount);
+ }
+ fclose(fp);
+
+ diagram = svgtiny_create();
+ if (!diagram) {
+ fprintf(stderr, "svgtiny_create() failed\n");
+ return NULL;
+ }
+
+ code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
+ free(buffer);
+
+ if (code != svgtiny_OK) {
+ fprintf(stderr, "svgtiny_parse failed: ");
+ switch (code) {
+ case svgtiny_OUT_OF_MEMORY:
+ fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
+ break;
+ case svgtiny_LIBDOM_ERROR:
+ fprintf(stderr, "svgtiny_LIBDOM_ERROR");
+ break;
+ case svgtiny_NOT_SVG:
+ fprintf(stderr, "svgtiny_NOT_SVG");
+ break;
+ case svgtiny_SVG_ERROR:
+ fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
+ diagram->error_line,
+ diagram->error_message);
+ break;
+ default:
+ fprintf(stderr, "unknown svgtiny_code %i", code);
+ break;
+ }
+ fprintf(stderr, "\n");
+ return NULL;
+ }
+
+ return diagram;
+}
+
+/**
+ * @brief Create a cairo context from a libsvgtiny diagram.
+ *
+ * @param diagram
+ * A pointer to a valid libsvgtiny diagram.
+ *
+ * @return If successful, a pointer to a @c cairo_t context structure
+ * is returned; if not, @c NULL is returned. You are expected to @c
+ * cairo_destroy the result if it is valid.
+ */
+static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
+ cairo_t* cr;
+ cairo_surface_t* surface;
+ cairo_status_t crs;
+ unsigned int i;
+
+ surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
+ diagram->width,
+ diagram->height);
+
+ crs = cairo_surface_status(surface);
+ if (crs != CAIRO_STATUS_SUCCESS) {
+ fprintf(stderr,
+ "cairo_image_surface_create failed: %s\n",
+ cairo_status_to_string(crs));
+ cairo_surface_destroy(surface);
+ return NULL;
+ }
+
+ cr = cairo_create(surface);
+ crs = cairo_status(cr);
+
+ /* Immediately destroy the surface which is now accessible as
+ cr->target */
+ cairo_surface_destroy(surface);
+
+ if (crs != CAIRO_STATUS_SUCCESS) {
+ fprintf(stderr,
+ "cairo_create failed: %s\n",
+ cairo_status_to_string(crs));
+ cairo_destroy(cr);
+ return NULL;
+ }
+
+ cairo_set_source_rgba(cr, 0, 0, 0, 0);
+ cairo_paint(cr);
+
+ /* Loop through the shapes in the diagram... */
+ for (i = 0; i != diagram->shape_count; i++) {
+
+ /* If this shape is a path, just render it. */
+ if (diagram->shape[i].path) {
+ render_path(cr, &diagram->shape[i]);
+ }
+
+ /* If this shape is text... */
+ if (diagram->shape[i].text) {
+ /* Figure out what color to use from the R/G/B components of the
+ shape's stroke. */
+ cairo_set_source_rgba(cr,
+ svgtiny_RED(diagram->shape[i].stroke) / 255.0,
+ svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
+ svgtiny_BLUE(diagram->shape[i].stroke) / 255.0,
+ 1);
+ /* Then move to the actual position of the text within the
+ shape... */
+ cairo_move_to(cr,
+ diagram->shape[i].text_x,
+ diagram->shape[i].text_y);
+
+ /* and draw it. */
+ cairo_show_text(cr, diagram->shape[i].text);
+ }
+ }
+
+
+ /* Check the status again just for good measure? */
+ crs = cairo_status(cr);
+ if (crs != CAIRO_STATUS_SUCCESS) {
+ fprintf(stderr,
+ "cairo error: %s\n",
+ cairo_status_to_string(crs));
+ cairo_destroy(cr);
+ return NULL;
+ }
+
+ return cr;
+}