[gimp] app: add a path canvas item class based on cairo_path_t (GimpBezierDesc)



commit 149ba1ec4b05fcc3284018d1784db8e4bb9dac56
Author: Michael Natterer <mitch gimp org>
Date:   Wed Oct 6 23:02:16 2010 +0200

    app: add a path canvas item class based on cairo_path_t (GimpBezierDesc)
    
    and use it in gimp_display_shell_draw_vectors().

 app/display/Makefile.am              |    2 +
 app/display/gimpcanvaspath.c         |  310 ++++++++++++++++++++++++++++++++++
 app/display/gimpcanvaspath.h         |   58 +++++++
 app/display/gimpdisplayshell-draw.c  |   58 ++-----
 app/display/gimpdisplayshell-style.c |    6 +-
 app/display/gimpdisplayshell-style.h |    2 -
 6 files changed, 387 insertions(+), 49 deletions(-)
---
diff --git a/app/display/Makefile.am b/app/display/Makefile.am
index fa22ef9..a1293c3 100644
--- a/app/display/Makefile.am
+++ b/app/display/Makefile.am
@@ -41,6 +41,8 @@ libappdisplay_a_sources = \
 	gimpcanvaslayerboundary.h		\
 	gimpcanvasline.c			\
 	gimpcanvasline.h			\
+	gimpcanvaspath.c			\
+	gimpcanvaspath.h			\
 	gimpcanvaspen.c				\
 	gimpcanvaspen.h				\
 	gimpcanvaspolygon.c			\
diff --git a/app/display/gimpcanvaspath.c b/app/display/gimpcanvaspath.c
new file mode 100644
index 0000000..ccafd26
--- /dev/null
+++ b/app/display/gimpcanvaspath.c
@@ -0,0 +1,310 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpcanvaspath.c
+ * Copyright (C) 2010 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+
+#include "libgimpbase/gimpbase.h"
+#include "libgimpmath/gimpmath.h"
+
+#include "display-types.h"
+
+#include "core/gimpparamspecs.h"
+
+#include "vectors/gimpbezierdesc.h"
+
+#include "gimpcanvaspath.h"
+#include "gimpdisplayshell.h"
+#include "gimpdisplayshell-style.h"
+#include "gimpdisplayshell-transform.h"
+
+
+enum
+{
+  PROP_0,
+  PROP_PATH,
+  PROP_FILLED,
+  PROP_PATH_STYLE
+};
+
+
+typedef struct _GimpCanvasPathPrivate GimpCanvasPathPrivate;
+
+struct _GimpCanvasPathPrivate
+{
+  cairo_path_t *path;
+  gboolean      filled;
+  gboolean      path_style;
+};
+
+#define GET_PRIVATE(path) \
+        G_TYPE_INSTANCE_GET_PRIVATE (path, \
+                                     GIMP_TYPE_CANVAS_PATH, \
+                                     GimpCanvasPathPrivate)
+
+
+/*  local function prototypes  */
+
+static void        gimp_canvas_path_finalize     (GObject          *object);
+static void        gimp_canvas_path_set_property (GObject          *object,
+                                                  guint             property_id,
+                                                  const GValue     *value,
+                                                  GParamSpec       *pspec);
+static void        gimp_canvas_path_get_property (GObject          *object,
+                                                  guint             property_id,
+                                                  GValue           *value,
+                                                  GParamSpec       *pspec);
+static void        gimp_canvas_path_draw         (GimpCanvasItem   *item,
+                                                  GimpDisplayShell *shell,
+                                                  cairo_t          *cr);
+static GdkRegion * gimp_canvas_path_get_extents  (GimpCanvasItem   *item,
+                                                  GimpDisplayShell *shell);
+static void        gimp_canvas_path_stroke       (GimpCanvasItem   *item,
+                                                  GimpDisplayShell *shell,
+                                                  cairo_t          *cr);
+
+
+G_DEFINE_TYPE (GimpCanvasPath, gimp_canvas_path,
+               GIMP_TYPE_CANVAS_ITEM)
+
+#define parent_class gimp_canvas_path_parent_class
+
+
+static void
+gimp_canvas_path_class_init (GimpCanvasPathClass *klass)
+{
+  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
+  GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);
+
+  object_class->finalize     = gimp_canvas_path_finalize;
+  object_class->set_property = gimp_canvas_path_set_property;
+  object_class->get_property = gimp_canvas_path_get_property;
+
+  item_class->draw           = gimp_canvas_path_draw;
+  item_class->get_extents    = gimp_canvas_path_get_extents;
+  item_class->stroke         = gimp_canvas_path_stroke;
+
+  g_object_class_install_property (object_class, PROP_PATH,
+                                   g_param_spec_pointer ("path", NULL, NULL,
+                                                         GIMP_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class, PROP_FILLED,
+                                   g_param_spec_boolean ("filled", NULL, NULL,
+                                                         FALSE,
+                                                         GIMP_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class, PROP_PATH_STYLE,
+                                   g_param_spec_boolean ("path-style",
+                                                         NULL, NULL,
+                                                         FALSE,
+                                                         GIMP_PARAM_READWRITE));
+
+  g_type_class_add_private (klass, sizeof (GimpCanvasPathPrivate));
+}
+
+static void
+gimp_canvas_path_init (GimpCanvasPath *path)
+{
+}
+
+static void
+gimp_canvas_path_finalize (GObject *object)
+{
+  GimpCanvasPathPrivate *private = GET_PRIVATE (object);
+
+  if (private->path)
+    {
+      gimp_bezier_desc_free (private->path, TRUE);
+      private->path = NULL;
+    }
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gimp_canvas_path_set_property (GObject      *object,
+                               guint         property_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  GimpCanvasPathPrivate *private = GET_PRIVATE (object);
+
+  switch (property_id)
+    {
+    case PROP_PATH:
+      {
+        cairo_path_t *path = g_value_get_pointer (value);
+
+        if (private->path)
+          {
+            cairo_path_destroy (private->path);
+            private->path = NULL;
+          }
+
+        if (path)
+          private->path = gimp_bezier_desc_copy (path);
+      }
+      break;
+    case PROP_FILLED:
+      private->filled = g_value_get_boolean (value);
+      break;
+    case PROP_PATH_STYLE:
+      private->path_style = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_canvas_path_get_property (GObject    *object,
+                               guint       property_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  GimpCanvasPathPrivate *private = GET_PRIVATE (object);
+
+  switch (property_id)
+    {
+    case PROP_PATH:
+      g_value_set_pointer (value, private->path);
+      break;
+    case PROP_FILLED:
+      g_value_set_boolean (value, private->filled);
+      break;
+    case PROP_PATH_STYLE:
+      g_value_set_boolean (value, private->path_style);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_canvas_path_draw (GimpCanvasItem   *item,
+                       GimpDisplayShell *shell,
+                       cairo_t          *cr)
+{
+  GimpCanvasPathPrivate *private = GET_PRIVATE (item);
+
+  if (private->path)
+    {
+      cairo_save (cr);
+      cairo_translate (cr, - shell->offset_x, - shell->offset_y);
+      cairo_scale (cr, shell->scale_x, shell->scale_y);
+
+      cairo_append_path (cr, private->path);
+      cairo_restore (cr);
+
+      if (private->filled)
+        _gimp_canvas_item_fill (item, cr);
+      else
+        _gimp_canvas_item_stroke (item, cr);
+    }
+}
+
+static GdkRegion *
+gimp_canvas_path_get_extents (GimpCanvasItem   *item,
+                              GimpDisplayShell *shell)
+{
+  GimpCanvasPathPrivate *private = GET_PRIVATE (item);
+
+  if (private->path)
+    {
+      cairo_t      *cr;
+      GdkRectangle  rectangle;
+      gdouble       x1, y1, x2, y2;
+
+      cr = gdk_cairo_create (gtk_widget_get_window (shell->canvas));
+
+      cairo_translate (cr, - shell->offset_x, - shell->offset_y);
+      cairo_scale (cr, shell->scale_x, shell->scale_y);
+
+      cairo_append_path (cr, private->path);
+
+      cairo_path_extents (cr, &x1, &y1, &x2, &y2);
+
+      cairo_destroy (cr);
+
+      if (private->filled)
+        {
+          rectangle.x      = floor (x1 - 1.0);
+          rectangle.y      = floor (x1 - 1.0);
+          rectangle.width  = ceil (x2 + 1.0) - rectangle.x;
+          rectangle.height = ceil (y2 + 1.0) - rectangle.y;
+        }
+      else
+        {
+          rectangle.x      = floor (x1 - 1.5);
+          rectangle.y      = floor (x1 - 1.5);
+          rectangle.width  = ceil (x2 + 1.5) - rectangle.x;
+          rectangle.height = ceil (y2 + 1.5) - rectangle.y;
+        }
+
+      return gdk_region_rectangle (&rectangle);
+    }
+
+  return NULL;
+}
+
+static void
+gimp_canvas_path_stroke (GimpCanvasItem   *item,
+                         GimpDisplayShell *shell,
+                         cairo_t          *cr)
+{
+  GimpCanvasPathPrivate *private = GET_PRIVATE (item);
+
+  if (private->path_style)
+    {
+      gboolean active = gimp_canvas_item_get_highlight (item);
+
+      gimp_display_shell_set_vectors_bg_style (shell, cr, active);
+      cairo_stroke_preserve (cr);
+
+      gimp_display_shell_set_vectors_fg_style (shell, cr, active);
+      cairo_stroke (cr);
+    }
+  else
+    {
+      GIMP_CANVAS_ITEM_CLASS (parent_class)->stroke (item, shell, cr);
+    }
+}
+
+GimpCanvasItem *
+gimp_canvas_path_new (GimpDisplayShell *shell,
+                      GimpBezierDesc   *path,
+                      gboolean          filled,
+                      gboolean          path_style)
+{
+  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
+
+  return g_object_new (GIMP_TYPE_CANVAS_PATH,
+                       "shell",      shell,
+                       "path",       path,
+                       "filled",     filled,
+                       "path-style", path_style,
+                       NULL);
+}
diff --git a/app/display/gimpcanvaspath.h b/app/display/gimpcanvaspath.h
new file mode 100644
index 0000000..9c2bb6c
--- /dev/null
+++ b/app/display/gimpcanvaspath.h
@@ -0,0 +1,58 @@
+/* GIMP - The GNU Image Manipulation Program Copyright (C) 1995
+ * Spencer Kimball and Peter Mattis
+ *
+ * gimpcanvaspolygon.h
+ * Copyright (C) 2010 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_CANVAS_POLYGON_H__
+#define __GIMP_CANVAS_PATH_H__
+
+
+#include "gimpcanvasitem.h"
+
+
+#define GIMP_TYPE_CANVAS_PATH            (gimp_canvas_path_get_type ())
+#define GIMP_CANVAS_PATH(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_CANVAS_PATH, GimpCanvasPath))
+#define GIMP_CANVAS_PATH_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_CANVAS_PATH, GimpCanvasPathClass))
+#define GIMP_IS_CANVAS_PATH(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_CANVAS_PATH))
+#define GIMP_IS_CANVAS_PATH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_CANVAS_PATH))
+#define GIMP_CANVAS_PATH_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_CANVAS_PATH, GimpCanvasPathClass))
+
+
+typedef struct _GimpCanvasPath      GimpCanvasPath;
+typedef struct _GimpCanvasPathClass GimpCanvasPathClass;
+
+struct _GimpCanvasPath
+{
+  GimpCanvasItem  parent_instance;
+};
+
+struct _GimpCanvasPathClass
+{
+  GimpCanvasItemClass  parent_class;
+};
+
+
+GType            gimp_canvas_path_get_type (void) G_GNUC_CONST;
+
+GimpCanvasItem * gimp_canvas_path_new      (GimpDisplayShell *shell,
+                                            GimpBezierDesc   *path,
+                                            gboolean          filled,
+                                            gboolean          path_style);
+
+
+#endif /* __GIMP_CANVAS_PATH_H__ */
diff --git a/app/display/gimpdisplayshell-draw.c b/app/display/gimpdisplayshell-draw.c
index 99279b6..2ca5308 100644
--- a/app/display/gimpdisplayshell-draw.c
+++ b/app/display/gimpdisplayshell-draw.c
@@ -39,6 +39,7 @@
 #include "widgets/gimpcairo.h"
 
 #include "gimpcanvas.h"
+#include "gimpcanvaspath.h"
 #include "gimpdisplay.h"
 #include "gimpdisplayshell.h"
 #include "gimpdisplayshell-draw.h"
@@ -148,32 +149,6 @@ gimp_display_shell_draw_selection_in (GimpDisplayShell   *shell,
   cairo_mask (cr, mask);
 }
 
-static void
-gimp_display_shell_draw_one_vectors (GimpDisplayShell *shell,
-                                     cairo_t          *cr,
-                                     GimpVectors      *vectors,
-                                     gdouble           width,
-                                     gboolean          active)
-{
-  GimpStroke *stroke;
-
-  for (stroke = gimp_vectors_stroke_get_next (vectors, NULL);
-       stroke;
-       stroke = gimp_vectors_stroke_get_next (vectors, stroke))
-    {
-      const GimpBezierDesc *desc = gimp_vectors_get_bezier (vectors);
-
-      if (desc)
-        cairo_append_path (cr, (cairo_path_t *) desc);
-    }
-
-  gimp_display_shell_set_vectors_bg_style (shell, cr, width, active);
-  cairo_stroke_preserve (cr);
-
-  gimp_display_shell_set_vectors_fg_style (shell, cr, width, active);
-  cairo_stroke (cr);
-}
-
 void
 gimp_display_shell_draw_vectors (GimpDisplayShell *shell,
                                  cairo_t          *cr)
@@ -186,39 +161,36 @@ gimp_display_shell_draw_vectors (GimpDisplayShell *shell,
 
   if (image && TRUE /* gimp_display_shell_get_show_vectors (shell) */)
     {
-      GList       *all_vectors = gimp_image_get_vectors_list (image);
-      const GList *list;
-      gdouble      xscale;
-      gdouble      yscale;
-      gdouble      width;
+      GList          *all_vectors = gimp_image_get_vectors_list (image);
+      const GList    *list;
+      GimpCanvasItem *path;
+
+      path = gimp_canvas_path_new (shell, NULL, FALSE, TRUE);
 
       if (! all_vectors)
         return;
 
-      cairo_translate (cr, - shell->offset_x, - shell->offset_y);
-      cairo_scale (cr, shell->scale_x, shell->scale_y);
-
-      /* determine a reasonable line width */
-      xscale = yscale = 1.0;
-      cairo_device_to_user_distance (cr, &xscale, &yscale);
-      width = MAX (xscale, yscale);
-      width = MIN (width, 1.0);
-
       for (list = all_vectors; list; list = list->next)
         {
           GimpVectors *vectors = list->data;
 
           if (gimp_item_get_visible (GIMP_ITEM (vectors)))
             {
-              gboolean active;
+              const GimpBezierDesc *desc;
+              gboolean              active;
 
+              desc   = gimp_vectors_get_bezier (vectors);
               active = (vectors == gimp_image_get_active_vectors (image));
 
-              gimp_display_shell_draw_one_vectors (shell, cr, vectors,
-                                                   width, active);
+              g_object_set (path, "path", desc, NULL);
+              gimp_canvas_item_set_highlight (path, active);
+
+              gimp_canvas_item_draw (path, cr);
             }
         }
 
+      g_object_unref (path);
+
       g_list_free (all_vectors);
     }
 }
diff --git a/app/display/gimpdisplayshell-style.c b/app/display/gimpdisplayshell-style.c
index 4220bc6..71502f2 100644
--- a/app/display/gimpdisplayshell-style.c
+++ b/app/display/gimpdisplayshell-style.c
@@ -268,13 +268,12 @@ gimp_display_shell_set_selection_in_style (GimpDisplayShell *shell,
 void
 gimp_display_shell_set_vectors_bg_style (GimpDisplayShell *shell,
                                          cairo_t          *cr,
-                                         gdouble           width,
                                          gboolean          active)
 {
   g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
   g_return_if_fail (cr != NULL);
 
-  cairo_set_line_width (cr, width * 3.0);
+  cairo_set_line_width (cr, 3.0);
 
   if (active)
     gimp_cairo_set_source_rgba (cr, &vectors_active_bg);
@@ -285,13 +284,12 @@ gimp_display_shell_set_vectors_bg_style (GimpDisplayShell *shell,
 void
 gimp_display_shell_set_vectors_fg_style (GimpDisplayShell *shell,
                                          cairo_t          *cr,
-                                         gdouble           width,
                                          gboolean          active)
 {
   g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
   g_return_if_fail (cr != NULL);
 
-  cairo_set_line_width (cr, width);
+  cairo_set_line_width (cr, 1.0);
 
   if (active)
     gimp_cairo_set_source_rgba (cr, &vectors_active_fg);
diff --git a/app/display/gimpdisplayshell-style.h b/app/display/gimpdisplayshell-style.h
index a8ba939..e87affb 100644
--- a/app/display/gimpdisplayshell-style.h
+++ b/app/display/gimpdisplayshell-style.h
@@ -45,11 +45,9 @@ void   gimp_display_shell_set_selection_in_style  (GimpDisplayShell *shell,
                                                    gint              index);
 void   gimp_display_shell_set_vectors_bg_style    (GimpDisplayShell *shell,
                                                    cairo_t          *cr,
-                                                   gdouble           width,
                                                    gboolean          active);
 void   gimp_display_shell_set_vectors_fg_style    (GimpDisplayShell *shell,
                                                    cairo_t          *cr,
-                                                   gdouble           width,
                                                    gboolean          active);
 
 void   gimp_display_shell_set_dim_style           (GimpDisplayShell *shell,



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