Index: dia_image.c
===================================================================
RCS file: /cvs/gnome/dia/lib/dia_image.c,v
retrieving revision 1.30
diff -u -r1.30 dia_image.c
--- dia_image.c	3 Nov 2005 22:46:49 -0000	1.30
+++ dia_image.c	22 Jan 2006 13:31:22 -0000
@@ -26,11 +26,19 @@
 
 #include "dia-lib-icons.h"
 
+#ifdef HAVE_LIBRSVG
+#include <librsvg/rsvg.h>
+#endif
+
 #define SCALING_CACHE
 
 struct _DiaImage {
   GdkPixbuf *image;
   gchar *filename;
+#ifdef HAVE_LIBRSVG
+  RsvgHandle *rsvg_handle;
+  int rsvg_counter;
+#endif
 #ifdef SCALING_CACHE
   GdkPixbuf *scaled; /* a cache of the last scaled version */
   int scaled_width, scaled_height;
@@ -85,8 +93,57 @@
   GdkPixbuf *image;
   GError *error = NULL;
 
+#ifdef HAVE_LIBRSVG
+  FILE *fp;
+  RsvgHandle *rsvg_handle = NULL;
+  char buffer[512];
+
+  fp = fopen(filename,"rb");
+  if (fp) {
+    rsvg_handle = rsvg_handle_new();
+    if (rsvg_handle)
+    while (!feof(fp)) {
+      int nr_read = fread(buffer,1,512,fp);
+      if (nr_read < 0)
+	break;
+      error = NULL;
+      if (!rsvg_handle_write (rsvg_handle, buffer, nr_read, &error)) {
+	g_warning(error->message);
+	g_error_free(error);
+	rsvg_handle_free(rsvg_handle);
+	rsvg_handle = NULL;
+	break;
+      }
+    }
+    fclose(fp);
+    if (!rsvg_handle_close(rsvg_handle, &error)) {
+      g_warning(error->message);
+      g_error_free(error);
+      rsvg_handle_free(rsvg_handle);
+      rsvg_handle = NULL;
+    }
+  }
+  /*
+   * to prevent assigning NULL to image->image
+   * other functions may break if image->image is null
+   * and to check whether this file is a svg file :P
+   */
+  if (rsvg_handle) {
+    image = rsvg_handle_get_pixbuf(rsvg_handle);
+    if (image == NULL) {
+      rsvg_handle_free(rsvg_handle);
+      rsvg_handle = NULL;
+    }
+  }
+#endif
+#ifdef HAVE_LIBRSVG
+  if (rsvg_handle == NULL)
+    image = gdk_pixbuf_new_from_file(filename, &error);
+  if (image == NULL && rsvg_handle == NULL) {
+#else
   image = gdk_pixbuf_new_from_file(filename, &error);
   if (image == NULL) {
+#endif
     /* dia_image_load() function is also (mis)used to check file
      * existance. Don't warn if the file is simply not there but
      * only if there is something else wrong while loading it.
@@ -99,6 +156,10 @@
 
   dia_img = g_new(struct _DiaImage, 1);
   dia_img->image = image;
+#ifdef HAVE_LIBRSVG
+  dia_img->rsvg_handle = rsvg_handle;
+  dia_img->rsvg_counter = 1;
+#endif
   /* We have a leak here, unless we add our own refcount */
   dia_img->filename = g_strdup(filename);
 #ifdef SCALING_CACHE
@@ -114,6 +175,9 @@
 dia_image_add_ref(DiaImage image)
 {
   gdk_pixbuf_ref(image->image);
+#ifdef HAVE_LIBRSVG
+  image->rsvg_counter ++;
+#endif
 }
 
 /** Release a reference to an image.
@@ -123,8 +187,27 @@
 dia_image_release(DiaImage image)
 {
   gdk_pixbuf_unref(image->image);
+#ifdef HAVE_LIBRSVG
+  image->rsvg_counter--;
+  if (image->rsvg_counter <= 0 && image->rsvg_handle) {
+    rsvg_handle_free(image->rsvg_handle);
+    image->rsvg_handle = NULL;
+  }
+#endif
 }
 
+#ifdef HAVE_LIBRSVG
+struct rsvg_size_data {
+	gint width,height;
+};
+
+static void rsvg_size_cb (gint *width, gint *height, struct rsvg_size_data *size)
+{
+  *width = size->width;
+  *height = size->height;
+}
+#endif
+
 /** Render an image unto a window.
  * @param image Image object to render.
  * @param window The window to render the image into.
@@ -139,6 +222,35 @@
 {
   GdkPixbuf *scaled;
 
+#ifdef HAVE_LIBRSVG
+  if (image->rsvg_handle != NULL) {
+    struct rsvg_size_data size;
+    size.width = width;
+    size.height = height;
+    rsvg_handle_set_size_callback (image->rsvg_handle,
+				   (RsvgSizeFunc)rsvg_size_cb,
+				   &size,NULL);
+#ifdef SCALING_CACHE
+    if (image->scaled == NULL ||
+	image->scaled_width != width || image->scaled_height != height) {
+      if (image->scaled)
+	gdk_pixbuf_unref(image->scaled);
+      image->scaled = rsvg_handle_get_pixbuf (image->rsvg_handle);
+      image->scaled_width = width;
+      image->scaled_height = height;
+    }
+    scaled = image->scaled;
+#else
+    scaled = rsvg_handle_get_pixbuf (image->rsvg_handle);
+#endif
+    rsvg_handle_set_size_callback (image->rsvg_handle,
+				   (RsvgSizeFunc)NULL,NULL,NULL);
+  }
+#endif
+
+#ifdef HAVE_LIBRSVG
+  if (image->image) {
+#endif
   if (gdk_pixbuf_get_width(image->image) != width ||
       gdk_pixbuf_get_height(image->image) != height) {
     /* Using TILES to make it look more like PostScript */
@@ -160,6 +272,9 @@
   } else {
     scaled = image->image;
   }
+#ifdef HAVE_LIBRSVG
+  } /* if (image->image) */
+#endif
 
   /* Once we can render Alpha, we'll do it! */
   gdk_pixbuf_render_to_drawable_alpha(scaled, window,
