[goocanvas] GooCanvasImage: Correct the size when using units other than pixels.



commit 4c303484c2d7a87c0f0edd16539004a0ae0c1ea7
Author: Damon Chaplin <damon gnome org>
Date:   Thu Sep 15 10:23:34 2011 +0100

    GooCanvasImage: Correct the size when using units other than pixels.
    
    2011-09-07  Murray Cumming  <murrayc murrayc com>
    
    	    GooCanvasImage: Correct the size when using units other than pixels.
    	    #657592.
    
    	    * src/goocanvas.[h|c]: Added goo_canvas_convert_units_to_pixels() and
    	    goo_canvas_convert_units_from_pixels(), which ignore the scale and
    	    ignore the bounds.
    	    Added a static units_to_pixels_ratios() function shared by these new
    	    functions and the existing recalculate_scales() function.
    	    * src/goocanvasimage.c: Added static
    	    goo_canvas_image_convert_pixbuf_sizes()	function, used by
    	    goo_canvas_image_new() and goo_canvas_image_set_common_property() to
    	    get the true size of the image in the units that are used.
    
    	    (committed by Damon with a few minor changes)

 ChangeLog                    |   17 ++++++++
 docs/goocanvas2-sections.txt |    3 +
 src/goocanvas.c              |   86 ++++++++++++++++++++++++++++++++++++-----
 src/goocanvas.h              |    7 +++
 src/goocanvasimage.c         |   61 +++++++++++++++++++++++++++++
 src/goocanvaspolyline.h      |    1 +
 src/goocanvasutils.h         |    1 +
 7 files changed, 165 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2d88c06..f1046fb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2011-09-07  Murray Cumming  <murrayc murrayc com>
+
+	GooCanvasImage: Correct the size when using units other than pixels.
+	#657592.
+
+	* src/goocanvas.[h|c]: Added goo_canvas_convert_units_to_pixels() and
+	goo_canvas_convert_units_from_pixels(), which ignore the scale and
+	ignore the bounds.
+	Added a static units_to_pixels_ratios() function shared by these new
+	functions and the existing recalculate_scales() function.
+	* src/goocanvasimage.c: Added static
+	goo_canvas_image_convert_pixbuf_sizes()	function, used by
+	goo_canvas_image_new() and goo_canvas_image_set_common_property() to
+	get the true size of the image in the units that are used.
+
+	(committed by Damon with a few minor changes)
+
 2011-09-14  Damon Chaplin  <damon gnome org>
 
 	* src/goocanvasitem.h: added get_requested_area_for_width() method.
diff --git a/docs/goocanvas2-sections.txt b/docs/goocanvas2-sections.txt
index 96f1bed..d643b01 100644
--- a/docs/goocanvas2-sections.txt
+++ b/docs/goocanvas2-sections.txt
@@ -140,6 +140,7 @@ goo_canvas_item_request_update
 goo_canvas_item_ensure_updated
 goo_canvas_item_update
 goo_canvas_item_get_requested_area
+goo_canvas_item_get_requested_area_for_width
 goo_canvas_item_get_requested_height
 goo_canvas_item_allocate_area
 goo_canvas_item_get_items_at
@@ -388,6 +389,8 @@ goo_canvas_convert_from_pixels
 goo_canvas_convert_to_item_space
 goo_canvas_convert_from_item_space
 goo_canvas_convert_bounds_to_item_space
+goo_canvas_convert_units_to_pixels
+goo_canvas_convert_units_from_pixels
 
 <SUBSECTION>
 goo_canvas_pointer_grab
diff --git a/src/goocanvas.c b/src/goocanvas.c
index aba3b6f..61eb635 100644
--- a/src/goocanvas.c
+++ b/src/goocanvas.c
@@ -153,6 +153,9 @@ enum {
 
 static guint canvas_signals[LAST_SIGNAL] = { 0 };
 
+static const double GOO_CANVAS_MM_PER_INCH =  25.4;
+static const double GOO_CANVAS_POINTS_PER_INCH = 72.0;
+
 static void     goo_canvas_dispose	   (GObject          *object);
 static void     goo_canvas_finalize	   (GObject          *object);
 static void     goo_canvas_realize         (GtkWidget        *widget);
@@ -645,7 +648,7 @@ goo_canvas_get_default_line_width (GooCanvas *canvas)
       line_width = 2.0;
       break;
     case GTK_UNIT_INCH:
-      line_width = 2.0 / 72.0;
+      line_width = 2.0 / GOO_CANVAS_POINTS_PER_INCH;
       break;
     case GTK_UNIT_MM:
       line_width = 0.7;
@@ -1724,32 +1727,45 @@ goo_canvas_configure_vadjustment (GooCanvas *canvas,
 
 
 static void
-recalculate_scales (GooCanvas *canvas)
+units_to_pixels_ratios (GooCanvas *canvas,
+			gdouble   *x_ratio,
+			gdouble   *y_ratio)
 {
   switch (canvas->units)
     {
     case GTK_UNIT_PIXEL:
-      canvas->device_to_pixels_x = canvas->scale_x;
-      canvas->device_to_pixels_y = canvas->scale_y;
+      *x_ratio = 1.0;
+      *y_ratio = *x_ratio;
       break;
     case GTK_UNIT_POINTS:
-      canvas->device_to_pixels_x = canvas->scale_x * (canvas->resolution_x / 72.0);
-      canvas->device_to_pixels_y = canvas->scale_y * (canvas->resolution_y / 72.0);
+      *x_ratio = canvas->resolution_x / GOO_CANVAS_POINTS_PER_INCH;
+      *y_ratio = canvas->resolution_y / GOO_CANVAS_POINTS_PER_INCH;
       break;
     case GTK_UNIT_INCH:
-      canvas->device_to_pixels_x = canvas->scale_x * canvas->resolution_x;
-      canvas->device_to_pixels_y = canvas->scale_y * canvas->resolution_y;
+      *x_ratio = canvas->resolution_x;
+      *y_ratio = canvas->resolution_y;
       break;
     case GTK_UNIT_MM:
-      /* There are 25.4 mm to an inch. */
-      canvas->device_to_pixels_x = canvas->scale_x * (canvas->resolution_x / 25.4);
-      canvas->device_to_pixels_y = canvas->scale_y * (canvas->resolution_y / 25.4);
+      *x_ratio = canvas->resolution_x / GOO_CANVAS_MM_PER_INCH;
+      *y_ratio = canvas->resolution_y / GOO_CANVAS_MM_PER_INCH;
       break;
     }
 }
 
 
 static void
+recalculate_scales (GooCanvas *canvas)
+{
+  gdouble x_ratio = 0;
+  gdouble y_ratio = 0;
+  units_to_pixels_ratios (canvas, &x_ratio, &y_ratio);
+ 
+  canvas->device_to_pixels_x = canvas->scale_x * x_ratio;
+  canvas->device_to_pixels_y = canvas->scale_y * y_ratio;
+}
+
+
+static void
 request_static_redraw (GooCanvas             *canvas,
 		       const GooCanvasBounds *bounds)
 {
@@ -3641,6 +3657,54 @@ goo_canvas_convert_from_pixels (GooCanvas     *canvas,
   *y = ((*y - canvas->canvas_y_offset) / canvas->device_to_pixels_y) + canvas->bounds.y1;
 }
 
+/**
+ * goo_canvas_convert_units_to_pixels:
+ * @canvas: a #GooCanvas.
+ * @x: a pointer to the x coordinate to convert.
+ * @y: a pointer to the y coordinate to convert.
+ *
+ * Converts a coordinate from the canvas's units to pixels,
+ * ignoring scaling and ignoring the coordinate space specified
+ * in the call to goo_canvas_set_bounds().
+ *
+ **/
+void
+goo_canvas_convert_units_to_pixels (GooCanvas     *canvas,
+			      gdouble       *x,
+			      gdouble       *y)
+{
+  gdouble x_ratio = 0;
+  gdouble y_ratio = 0;
+  units_to_pixels_ratios (canvas, &x_ratio, &y_ratio);
+ 
+  *x = *x * x_ratio;
+  *y = *y * y_ratio;
+}
+
+
+/**
+ * goo_canvas_convert_units_from_pixels:
+ * @canvas: a #GooCanvas.
+ * @x: a pointer to the x coordinate to convert.
+ * @y: a pointer to the y coordinate to convert.
+ *
+ * Converts a coordinate from pixels to the canvas's units,
+ * ignoring scaling and ignoring the coordinate space specified
+ * in the call to goo_canvas_set_bounds().
+ *
+ **/
+void
+goo_canvas_convert_units_from_pixels (GooCanvas     *canvas,
+				gdouble       *x,
+				gdouble       *y)
+{
+  gdouble x_ratio = 0;
+  gdouble y_ratio = 0;
+  units_to_pixels_ratios (canvas, &x_ratio, &y_ratio);
+ 
+  *x = *x / x_ratio;
+  *y = *y / y_ratio;
+}
 
 static void
 goo_canvas_convert_from_window_pixels (GooCanvas     *canvas,
diff --git a/src/goocanvas.h b/src/goocanvas.h
index 8cea33f..a7f41dd 100644
--- a/src/goocanvas.h
+++ b/src/goocanvas.h
@@ -271,6 +271,13 @@ void		goo_canvas_convert_to_pixels	   (GooCanvas       *canvas,
 void		goo_canvas_convert_from_pixels	   (GooCanvas       *canvas,
 						    gdouble         *x,
 						    gdouble         *y);
+						    
+void goo_canvas_convert_units_to_pixels (GooCanvas *canvas,
+						    gdouble         *x,
+						    gdouble         *y);
+void goo_canvas_convert_units_from_pixels (GooCanvas *canvas,
+						    gdouble         *x,
+						    gdouble         *y);
 
 void		goo_canvas_convert_to_item_space   (GooCanvas	    *canvas,
 						    GooCanvasItem   *item,
diff --git a/src/goocanvasimage.c b/src/goocanvasimage.c
index 5ecc7db..a2908f7 100644
--- a/src/goocanvasimage.c
+++ b/src/goocanvasimage.c
@@ -12,6 +12,11 @@
  *
  * GooCanvasImage represents an image item.
  *
+ * <note><para>
+ * It is usually necessary to set the "scale-to-fit" property to %TRUE to
+ * scale the image to fit the given rectangle.
+ * </para></note>
+ *
  * It is a subclass of #GooCanvasItemSimple and so inherits all of the style
  * properties such as "operator" and "pointer-events".
  *
@@ -173,6 +178,24 @@ goo_canvas_image_init (GooCanvasImage *image)
 }
 
 
+/*
+ * Convert the width and height to the canvas's units, from the pixbuf's size 
+ * in pixels.
+ */
+static void
+goo_canvas_image_convert_pixbuf_sizes (GooCanvasItem *item,
+				       GooCanvasImageData *image_data)
+{
+  GooCanvas *canvas = goo_canvas_item_get_canvas (item);
+  if (canvas)
+    {
+      goo_canvas_convert_units_from_pixels (canvas, 
+					    &(image_data->width),
+					    &(image_data->height));
+    }
+}
+
+
 /**
  * goo_canvas_image_new:
  * @parent: the parent item, or %NULL. If a parent is specified, it will assume
@@ -195,6 +218,16 @@ goo_canvas_image_init (GooCanvasImage *image)
  *                                               NULL);
  * </programlisting></informalexample>
  *
+ * This example creates an image scaled to a size of 200x200:
+ *
+ * <informalexample><programlisting>
+ *  GooCanvasItem *image = goo_canvas_image_new (mygroup, pixbuf, 100.0, 100.0,
+ *                                               "width", 200.0,
+ *                                               "height", 200.0,
+ *                                               "scale-to-fit", TRUE,
+ *                                               NULL);
+ * </programlisting></informalexample>
+ *
  * Returns: a new image item.
  **/
 GooCanvasItem*
@@ -222,6 +255,8 @@ goo_canvas_image_new (GooCanvasItem *parent,
       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);
     }
 
   va_start (var_args, y);
@@ -364,6 +399,11 @@ goo_canvas_image_set_common_property (GObject              *object,
       image_data->pattern = pixbuf ? goo_canvas_cairo_pattern_from_pixbuf (pixbuf) : NULL;
       image_data->width = pixbuf ? gdk_pixbuf_get_width (pixbuf) : 0;
       image_data->height = pixbuf ? gdk_pixbuf_get_height (pixbuf) : 0;
+
+      if (GOO_IS_CANVAS_ITEM (object))
+	goo_canvas_image_convert_pixbuf_sizes (GOO_CANVAS_ITEM (object),
+					       image_data);
+
       break;
     case PROP_ALPHA:
       priv->alpha = g_value_get_double (value);
@@ -549,6 +589,13 @@ goo_canvas_image_class_init (GooCanvasImageClass *klass)
  *
  * GooCanvasImageModel represent a model for image items.
  *
+ * <note><para>
+ * It is usually necessary to set the "scale-to-fit" property to %TRUE to
+ * scale the image to fit the given rectangle. When using units other than
+ * %GTK_UNIT_PIXEL it is also necessary to set the "width" and "height"
+ * properties to set the desired size.
+ * </para></note>
+ *
  * It is a subclass of #GooCanvasItemModelSimple and so inherits all of the
  * style properties such as "operator" and "pointer-events".
  *
@@ -630,6 +677,16 @@ goo_canvas_image_model_init (GooCanvasImageModel *emodel)
  *                                                          NULL);
  * </programlisting></informalexample>
  *
+ * This example creates an image scaled to a size of 200x200:
+ *
+ * <informalexample><programlisting>
+ *  GooCanvasItemModel *image = goo_canvas_image_model_new (mygroup, pixbuf, 100.0, 100.0,
+ *                                                          "width", 200.0,
+ *                                                          "height", 200.0,
+ *                                                          "scale-to-fit", TRUE,
+ *                                                          NULL);
+ * </programlisting></informalexample>
+ *
  * Returns: a new image model.
  **/
 GooCanvasItemModel*
@@ -657,6 +714,10 @@ goo_canvas_image_model_new (GooCanvasItemModel *parent,
       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);
+
+      /* This is not possible with a model, because we don't know the canvas
+	 units being used. */
+      /*goo_canvas_image_convert_pixbuf_sizes (item, image_data);*/
     }
 
   va_start (var_args, y);
diff --git a/src/goocanvaspolyline.h b/src/goocanvaspolyline.h
index 96212bd..107cb98 100644
--- a/src/goocanvaspolyline.h
+++ b/src/goocanvaspolyline.h
@@ -24,6 +24,7 @@ G_BEGIN_DECLS
 typedef struct _GooCanvasPoints GooCanvasPoints;
 struct _GooCanvasPoints
 {
+  /*< public >*/
   double *coords;
   int num_points;
   int ref_count;
diff --git a/src/goocanvasutils.h b/src/goocanvasutils.h
index 23bbda0..ddc58bd 100644
--- a/src/goocanvasutils.h
+++ b/src/goocanvasutils.h
@@ -245,6 +245,7 @@ typedef struct _GooCanvasLineDash GooCanvasLineDash;
  */
 struct _GooCanvasLineDash
 {
+  /*< public >*/
   int ref_count;
   int num_dashes;
   double *dashes;



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