]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blobdiff - example.c
example.c: use GdkPixbuf instead of Cairo for SVG -> PNG conversion.
[libsvgtiny-pixbuf.git] / example.c
index 1f54a00f283acbf5f984ff86bb558537c565c9f5..3dab2bace2093f70bc9d1a0625418ae45c810a8d 100644 (file)
--- a/example.c
+++ b/example.c
@@ -1,12 +1,22 @@
+#include <fcntl.h> /* open */
+#include <stdio.h> /* printf, fprintf */
+#include <stdlib.h> /* malloc */
+#include <sys/stat.h> /* fstat */
+#include <unistd.h> /* read */
+
 #include <cairo.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <gdk/gdk.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
 #include <svgtiny.h>
 
+/* Convenient typedefs for libsvgtiny */
+typedef struct svgtiny_diagram diagram_t;
+
+
 /**
  * Render an svgtiny path using cairo.
  */
-void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) {
+void render_path(cairo_t *cr, struct svgtiny_shape *path) {
   unsigned int j;
 
   cairo_new_path(cr);
@@ -14,8 +24,8 @@ void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) {
     switch ((int) path->path[j]) {
     case svgtiny_PATH_MOVE:
       cairo_move_to(cr,
-                   scale * path->path[j + 1],
-                   scale * path->path[j + 2]);
+                   path->path[j + 1],
+                   path->path[j + 2]);
       j += 3;
       break;
     case svgtiny_PATH_CLOSE:
@@ -24,18 +34,18 @@ void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) {
       break;
     case svgtiny_PATH_LINE:
       cairo_line_to(cr,
-                   scale * path->path[j + 1],
-                   scale * path->path[j + 2]);
+                   path->path[j + 1],
+                   path->path[j + 2]);
       j += 3;
       break;
     case svgtiny_PATH_BEZIER:
       cairo_curve_to(cr,
-                    scale * path->path[j + 1],
-                    scale * path->path[j + 2],
-                    scale * path->path[j + 3],
-                    scale * path->path[j + 4],
-                    scale * path->path[j + 5],
-                    scale * path->path[j + 6]);
+                    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:
@@ -55,63 +65,66 @@ void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) {
                         svgtiny_RED(path->stroke) / 255.0,
                         svgtiny_GREEN(path->stroke) / 255.0,
                         svgtiny_BLUE(path->stroke) / 255.0);
-    cairo_set_line_width(cr, scale * path->stroke_width);
+    cairo_set_line_width(cr, path->stroke_width);
     cairo_stroke_preserve(cr);
   }
 }
 
+/*
+ * @brief Parse an SVG file into a diagram_t structure.
+ *
+ * @param path
+ *   The path to the SVG file.
+ *
+ * @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.
+ */
+diagram_t* svgtiny_diagram_from_path(char* path, int width, int height) {
+  diagram_t *diagram;
 
-int main(int argc, char** argv) {
-  int exit_code = 0;
-
-  /* Initial viewport width and height */
-  int width = 1024, height = 1024;
-  float scale = 1.0;
-  char* svgpath = "Ghostscript_Tiger.svg";
-  char* pngpath = "Ghostscript_Tiger.png";
-  size_t svgsize = 68630;
-
-  FILE *fd;
+  int fd;
+  size_t bytecount;
   char *buffer;
   size_t bytesread;
-
+  struct stat sb;
   svgtiny_code code;
-  struct svgtiny_diagram *diagram;
-  cairo_surface_t *surface;
-  cairo_t *cr = 0;
-  cairo_status_t cr_status;
-
-  unsigned int i;
 
   /* load file into memory buffer */
-  fd = fopen(svgpath, "rb");
-  if (!fd) {
-    perror(svgpath);
-    return 1;
+  fd = open(path, O_RDONLY);
+  if (fd == -1) {
+    perror(path);
+    return NULL;
   }
 
-  buffer = malloc(svgsize);
+  if (fstat(fd, &sb)) {
+    perror(path);
+    return NULL;
+  }
+  bytecount = sb.st_size;
+
+  buffer = malloc(bytecount);
   if (!buffer) {
-    fprintf(stderr, "Unable to allocate %zd bytes\n", svgsize);
-    return 1;
+    fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount);
+    return NULL;
   }
 
-  bytesread = fread(buffer, 1, svgsize, fd);
-  if (bytesread != svgsize) {
-    perror(svgpath);
-    return 1;
+  bytesread = read(fd, buffer, bytecount);
+  if (bytesread != bytecount) {
+    perror(path);
+    return NULL;
   }
-  fclose(fd);
+  close(fd);
 
 
   /* create svgtiny object */
   diagram = svgtiny_create();
   if (!diagram) {
     fprintf(stderr, "svgtiny_create() failed\n");
-    return 1;
+    return NULL;
   }
 
-  code = svgtiny_parse(diagram, buffer, svgsize, svgpath, width, height);
+  code = svgtiny_parse(diagram, buffer, bytecount, path, width, height);
   free(buffer);
 
   if (code != svgtiny_OK) {
@@ -136,25 +149,54 @@ int main(int argc, char** argv) {
       break;
     }
     fprintf(stderr, "\n");
-    return 1;
+    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.
+ */
+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_RGB24,
+                                      diagram->width,
+                                      diagram->height);
 
-  surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
-  if (!surface) {
-    fprintf(stderr, "cairo_image_surface_create failed\n");
-    exit_code = 1;
-    goto cleanup;
+  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);
-  cr_status = cairo_status(cr);
-  if (cr_status != CAIRO_STATUS_SUCCESS) {
+  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(cr_status));
-    exit_code = 1;
-    goto cleanup;
+           cairo_status_to_string(crs));
+    cairo_destroy(cr);
+    return NULL;
   }
 
   cairo_set_source_rgb(cr, 1, 1, 1);
@@ -165,7 +207,7 @@ int main(int argc, char** argv) {
 
     /* If this shape is a path, just render it. */
     if (diagram->shape[i].path) {
-      render_path(cr, scale, &diagram->shape[i]);
+      render_path(cr, &diagram->shape[i]);
     }
 
     /* If this shape is text... */
@@ -179,37 +221,70 @@ int main(int argc, char** argv) {
       /* Then move to the actual position of the text within the
         shape... */
       cairo_move_to(cr,
-                   scale * diagram->shape[i].text_x,
-                   scale * diagram->shape[i].text_y);
+                   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. */
-  cr_status = cairo_status(cr);
-  if (cr_status != CAIRO_STATUS_SUCCESS) {
-    fprintf(stderr, "cairo error: %s\n",
-           cairo_status_to_string(cr_status));
-    exit_code = 1;
-    goto cleanup;
+
+  /* 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;
   }
 
-  cr_status = cairo_surface_write_to_png(surface, pngpath);
-  if (cr_status != CAIRO_STATUS_SUCCESS) {
-    fprintf(stderr, "cairo error: %s\n",
-           cairo_status_to_string(cr_status));
-    exit_code = 1;
-    goto cleanup;
+  return cr;
+}
+
+int main(int argc, char** argv) {
+  char* svgpath;
+  char* pngpath;
+  int pngwidth = 1024, pngheight = 1024;
+
+  diagram_t* diagram;
+  cairo_t *cr = 0;
+
+  GdkPixbuf* pb;
+
+  /* Parse arguments, and maybe print usage */
+  if (argc < 3) {
+    printf("Usage: %s INPUT OUTPUT\n", argv[0]);
+    printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
+    return 2;
   }
 
-cleanup:
-  if (cr) {
-    cairo_destroy(cr);
+  svgpath = argv[1];
+  pngpath = argv[2];
+
+  diagram = svgtiny_diagram_from_path(svgpath, pngwidth, pngheight);
+  if (!diagram) {
+    return 1;
+  }
+
+  cr = cairo_context_from_diagram(diagram);
+  if (!cr) {
+    svgtiny_free(diagram);
+    return 1;
+  }
+
+  pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
+                                  0,
+                                  0,
+                                  pngwidth,
+                                  pngheight);
+
+
+  if (pb) {
+    gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
+    g_object_unref(pb);
   }
-  cairo_surface_destroy(surface);
-  svgtiny_free(diagram);
 
-  return exit_code;
+  return 0;
 }