[goocanvas] GooCanvasImage: Correct the image appearance when using non-pixel units.



commit c17e4bd8a90ec021685f015e5f3a737719d574c1
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Sep 26 16:55:38 2011 +0200

    GooCanvasImage: Correct the image appearance when using non-pixel units.
    
    * src/goocanvasimage.c: (goo_canvas_image_convert_pixbuf_sizes):
    Store the scale factor so we can use it later when drawing the image.
    (goo_canvas_image_new): Move the call to
    goo_canvas_image_convert_pixbuf_sizes() until after adding the item,
    so that it can get the canvas, instead of passing the parent item
    instead. We need to pass the actual item so we can store the
    scale factor.
    (goo_canvas_image_paint): Use the scale factor.
    Bug #657592

 ChangeLog            |   14 ++++++++++++++
 src/goocanvasimage.c |   47 ++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 52 insertions(+), 9 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b227667..e179328 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2011-09-26  Murray Cumming  <murrayc murrayc com>
+
+  GooCanvasImage: Correct the image appearance when using non-pixel units.
+    
+	* src/goocanvasimage.c: (goo_canvas_image_convert_pixbuf_sizes):
+	Store the scale factor so we can use it later when drawing the image.
+	(goo_canvas_image_new): Move the call to
+	goo_canvas_image_convert_pixbuf_sizes() until after adding the item,
+	so that it can get the canvas, instead of passing the parent item
+	instead. We need to pass the actual item so we can store the
+	scale factor.
+	(goo_canvas_image_paint): Use the scale factor.
+	Bug #657592
+	
 2011-09-24  Krzesimir Nowak  <qdlacz gmail com>
 
 	Add GdkRGBA properties. #651125.
diff --git a/src/goocanvasimage.c b/src/goocanvasimage.c
index a2908f7..85fab38 100644
--- a/src/goocanvasimage.c
+++ b/src/goocanvasimage.c
@@ -42,6 +42,7 @@ typedef struct _GooCanvasImagePrivate GooCanvasImagePrivate;
 struct _GooCanvasImagePrivate {
   gboolean scale_to_fit;
   gdouble alpha;
+  gdouble scale_to_units; /* How much to scale the image when the units are not pixels. */
 };
 
 #define GOO_CANVAS_IMAGE_GET_PRIVATE(image)  \
@@ -186,6 +187,10 @@ static void
 goo_canvas_image_convert_pixbuf_sizes (GooCanvasItem *item,
 				       GooCanvasImageData *image_data)
 {
+  GooCanvasImagePrivate *priv = goo_canvas_image_get_private (item);
+
+  const double original_width = image_data->width;
+  
   GooCanvas *canvas = goo_canvas_item_get_canvas (item);
   if (canvas)
     {
@@ -193,6 +198,11 @@ goo_canvas_image_convert_pixbuf_sizes (GooCanvasItem *item,
 					    &(image_data->width),
 					    &(image_data->height));
     }
+    
+  if(image_data->width) /* Avoid division by zero. */
+    priv->scale_to_units = original_width / image_data->width;
+  else
+    priv->scale_to_units = 1.0f;
 }
 
 
@@ -244,19 +254,29 @@ goo_canvas_image_new (GooCanvasItem *parent,
   va_list var_args;
 
   item = g_object_new (GOO_TYPE_CANVAS_IMAGE, NULL);
+
+  /* Put it in the parent first because other property setters might need
+   * access to the parent.
+   */  
+  if (parent)
+    {
+      goo_canvas_item_add_child (parent, item, -1);
+      g_object_unref (item);
+    }
+    
   image = (GooCanvasImage*) item;
 
   image_data = image->image_data;
   image_data->x = x;
   image_data->y = y;
-
+  
   if (pixbuf)
     {
       image_data->pattern = goo_canvas_cairo_pattern_from_pixbuf (pixbuf);
       image_data->width = gdk_pixbuf_get_width (pixbuf);
       image_data->height = gdk_pixbuf_get_height (pixbuf);
-
-      goo_canvas_image_convert_pixbuf_sizes (parent, image_data);
+      
+      goo_canvas_image_convert_pixbuf_sizes (item, image_data);
     }
 
   va_start (var_args, y);
@@ -265,12 +285,6 @@ goo_canvas_image_new (GooCanvasItem *parent,
     g_object_set_valist ((GObject*) item, first_property, var_args);
   va_end (var_args);
 
-  if (parent)
-    {
-      goo_canvas_item_add_child (parent, item, -1);
-      g_object_unref (item);
-    }
-
   return item;
 }
 
@@ -491,6 +505,9 @@ goo_canvas_image_paint (GooCanvasItemSimple   *simple,
     return;
 
 #if 1
+
+  /* scale-to-fit means a simple scale, not keeping the aspect ratio.
+     This does not need to consider the units used. */ 
   if (priv->scale_to_fit)
     {
       if (cairo_pattern_get_surface (image_data->pattern, &surface)
@@ -502,7 +519,19 @@ goo_canvas_image_paint (GooCanvasItemSimple   *simple,
 	  cairo_matrix_scale (&matrix, width / image_data->width,
 			      height / image_data->height);
 	}
+    } else if (priv->scale_to_units && (priv->scale_to_units != 1.0f))
+   {
+      /* Scale the image to fit the size in units.
+         We have already scaled the width and height numbers. */
+      if (cairo_pattern_get_surface (image_data->pattern, &surface)
+	  == CAIRO_STATUS_SUCCESS
+	  && cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE)
+	{
+	  cairo_matrix_scale (&matrix, priv->scale_to_units,
+			      priv->scale_to_units);
+	}
     }
+ 
 
   cairo_matrix_translate (&matrix, -image_data->x, -image_data->y);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]