[gimp] app: add widgets/gimpcairo.[ch] for cairo utility functions



commit 3e1b14fd1a83593454357a28e08909458bac81d7
Author: Michael Natterer <mitch gimp org>
Date:   Fri Aug 27 14:41:30 2010 +0200

    app: add widgets/gimpcairo.[ch] for cairo utility functions
    
    and move the stipple pattern creation function there.

 app/display/gimpdisplayshell-style.c |  144 ++++++++++++----------------------
 app/widgets/Makefile.am              |    2 +
 app/widgets/gimpcairo.c              |   86 ++++++++++++++++++++
 app/widgets/gimpcairo.h              |   33 ++++++++
 4 files changed, 173 insertions(+), 92 deletions(-)
---
diff --git a/app/display/gimpdisplayshell-style.c b/app/display/gimpdisplayshell-style.c
index bf1b67b..bed3678 100644
--- a/app/display/gimpdisplayshell-style.c
+++ b/app/display/gimpdisplayshell-style.c
@@ -34,6 +34,8 @@
 #include "core/gimpgrid.h"
 #include "core/gimplayermask.h"
 
+#include "widgets/gimpcairo.h"
+
 #include "gimpdisplayshell.h"
 #include "gimpdisplayshell-style.h"
 
@@ -62,14 +64,6 @@ static const GimpRGB selection_in_fg     = { 0.0, 0.0, 0.0, 1.0 };
 static const GimpRGB selection_in_bg     = { 1.0, 1.0, 1.0, 1.0 };
 
 
-/*  local function prototypes  */
-
-static void   gimp_display_shell_set_stipple_style (cairo_t       *cr,
-                                                    const GimpRGB *fg,
-                                                    const GimpRGB *bg,
-                                                    gint           index);
-
-
 /*  public functions  */
 
 void
@@ -77,21 +71,24 @@ gimp_display_shell_set_guide_style (GimpDisplayShell *shell,
                                     cairo_t          *cr,
                                     gboolean          active)
 {
+  cairo_pattern_t *pattern;
+
   g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
   g_return_if_fail (cr != NULL);
 
   cairo_set_line_width (cr, 1.0);
 
   if (active)
-    gimp_display_shell_set_stipple_style (cr,
-                                          &guide_active_fg,
-                                          &guide_active_bg,
-                                          0);
+    pattern = gimp_cairo_stipple_pattern_create (&guide_active_fg,
+                                                 &guide_active_bg,
+                                                 0);
   else
-    gimp_display_shell_set_stipple_style (cr,
-                                          &guide_normal_fg,
-                                          &guide_normal_bg,
-                                          0);
+    pattern = gimp_cairo_stipple_pattern_create (&guide_normal_fg,
+                                                 &guide_normal_bg,
+                                                 0);
+
+  cairo_set_source (cr, pattern);
+  cairo_pattern_destroy (pattern);
 }
 
 void
@@ -129,24 +126,27 @@ gimp_display_shell_set_grid_style (GimpDisplayShell *shell,
 
   switch (grid->style)
     {
+      cairo_pattern_t *pattern;
+
     case GIMP_GRID_ON_OFF_DASH:
     case GIMP_GRID_DOUBLE_DASH:
       if (grid->style == GIMP_GRID_DOUBLE_DASH)
         {
-          gimp_display_shell_set_stipple_style (cr,
-                                                &grid->fgcolor,
-                                                &grid->bgcolor,
-                                                0);
+          pattern = gimp_cairo_stipple_pattern_create (&grid->fgcolor,
+                                                       &grid->bgcolor,
+                                                       0);
         }
       else
         {
           GimpRGB bg = { 0.0, 0.0, 0.0, 0.0 };
 
-          gimp_display_shell_set_stipple_style (cr,
-                                                &grid->fgcolor,
-                                                &bg,
-                                                0);
+          pattern = gimp_cairo_stipple_pattern_create (&grid->fgcolor,
+                                                       &bg,
+                                                       0);
         }
+
+      cairo_set_source (cr, pattern);
+      cairo_pattern_destroy (pattern);
       break;
 
     case GIMP_GRID_DOTS:
@@ -208,6 +208,8 @@ gimp_display_shell_set_layer_style (GimpDisplayShell *shell,
                                     cairo_t          *cr,
                                     GimpDrawable     *drawable)
 {
+  cairo_pattern_t *pattern;
+
   g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
   g_return_if_fail (cr != NULL);
   g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
@@ -216,40 +218,43 @@ gimp_display_shell_set_layer_style (GimpDisplayShell *shell,
 
   if (GIMP_IS_LAYER_MASK (drawable))
     {
-      gimp_display_shell_set_stipple_style (cr,
-                                            &layer_mask_fg,
-                                            &layer_mask_bg,
-                                            0);
+      pattern = gimp_cairo_stipple_pattern_create (&layer_mask_fg,
+                                                   &layer_mask_bg,
+                                                   0);
     }
   else if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)))
     {
-      gimp_display_shell_set_stipple_style (cr,
-                                            &layer_group_fg,
-                                            &layer_group_bg,
-                                            0);
+      pattern = gimp_cairo_stipple_pattern_create (&layer_group_fg,
+                                                   &layer_group_bg,
+                                                   0);
     }
   else
     {
-      gimp_display_shell_set_stipple_style (cr,
-                                            &layer_fg,
-                                            &layer_bg,
-                                            0);
+      pattern = gimp_cairo_stipple_pattern_create (&layer_fg,
+                                                   &layer_bg,
+                                                   0);
     }
+
+  cairo_set_source (cr, pattern);
+  cairo_pattern_destroy (pattern);
 }
 
 void
 gimp_display_shell_set_selection_out_style (GimpDisplayShell *shell,
                                             cairo_t          *cr)
 {
+  cairo_pattern_t *pattern;
+
   g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
   g_return_if_fail (cr != NULL);
 
   cairo_set_line_width (cr, 1.0);
 
-  gimp_display_shell_set_stipple_style (cr,
-                                        &selection_out_fg,
-                                        &selection_out_bg,
-                                        0);
+  pattern = gimp_cairo_stipple_pattern_create (&selection_out_fg,
+                                               &selection_out_bg,
+                                               0);
+  cairo_set_source (cr, pattern);
+  cairo_pattern_destroy (pattern);
 }
 
 void
@@ -257,61 +262,16 @@ gimp_display_shell_set_selection_in_style (GimpDisplayShell *shell,
                                            cairo_t          *cr,
                                            gint              index)
 {
+  cairo_pattern_t *pattern;
+
   g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
   g_return_if_fail (cr != NULL);
 
   cairo_set_line_width (cr, 1.0);
 
-  gimp_display_shell_set_stipple_style (cr,
-                                        &selection_in_fg,
-                                        &selection_in_bg,
-                                        index);
-}
-
-
-/*  private functions  */
-
-static cairo_user_data_key_t surface_data_key = { 0, };
-
-static void
-gimp_display_shell_set_stipple_style (cairo_t       *cr,
-                                      const GimpRGB *fg,
-                                      const GimpRGB *bg,
-                                      gint           index)
-{
-  cairo_surface_t *surface;
-  guchar          *data = g_malloc0 (8 * 8 * 4);
-  guchar           fg_r, fg_g, fg_b, fg_a;
-  guchar           bg_r, bg_g, bg_b, bg_a;
-  gint             x, y;
-  guchar          *d;
-
-  gimp_rgba_get_uchar (fg, &fg_r, &fg_g, &fg_b, &fg_a);
-  gimp_rgba_get_uchar (bg, &bg_r, &bg_g, &bg_b, &bg_a);
-
-  d = data;
-
-  for (y = 0; y < 8; y++)
-    {
-      for (x = 0; x < 8; x++)
-        {
-          if ((x + y + index) % 8 >= 4)
-            GIMP_CAIRO_ARGB32_SET_PIXEL (d, fg_r, fg_g, fg_b, fg_a);
-          else
-            GIMP_CAIRO_ARGB32_SET_PIXEL (d, bg_r, bg_g, bg_b, bg_a);
-
-          d += 4;
-        }
-    }
-
-  surface = cairo_image_surface_create_for_data (data,
-                                                 CAIRO_FORMAT_ARGB32,
-                                                 8, 8, 8 * 4);
-  cairo_surface_set_user_data (surface, &surface_data_key,
-                               data, (cairo_destroy_func_t) g_free);
-
-  cairo_set_source_surface (cr, surface, 0, 0);
-  cairo_surface_destroy (surface);
-
-  cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
+  pattern = gimp_cairo_stipple_pattern_create (&selection_in_fg,
+                                               &selection_in_bg,
+                                               index);
+  cairo_set_source (cr, pattern);
+  cairo_pattern_destroy (pattern);
 }
diff --git a/app/widgets/Makefile.am b/app/widgets/Makefile.am
index 8975ce8..f90a501 100644
--- a/app/widgets/Makefile.am
+++ b/app/widgets/Makefile.am
@@ -39,6 +39,8 @@ libappwidgets_a_sources = \
 	gimpbrushselect.h		\
 	gimpbufferview.c		\
 	gimpbufferview.h		\
+	gimpcairo.c			\
+	gimpcairo.h			\
 	gimpcairo-wilber.c		\
 	gimpcairo-wilber.h		\
 	gimpcellrendererdashes.c	\
diff --git a/app/widgets/gimpcairo.c b/app/widgets/gimpcairo.c
new file mode 100644
index 0000000..e84bc6e
--- /dev/null
+++ b/app/widgets/gimpcairo.c
@@ -0,0 +1,86 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpcairo.c
+ * Copyright (C) 2010  Michael Natterer <mitch gimp org>
+ *
+ * Some code here is based on code from librsvg that was originally
+ * written by Raph Levien <raph artofcode com> for Gill.
+ *
+ * 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 <gtk/gtk.h>
+
+#include "libgimpcolor/gimpcolor.h"
+#include "libgimpwidgets/gimpwidgets.h"
+
+#include "widgets-types.h"
+
+#include "gimpcairo.h"
+
+
+static cairo_user_data_key_t surface_data_key = { 0, };
+
+cairo_pattern_t *
+gimp_cairo_stipple_pattern_create (const GimpRGB *fg,
+                                   const GimpRGB *bg,
+                                   gint           index)
+{
+  cairo_surface_t *surface;
+  cairo_pattern_t *pattern;
+  guchar          *data = g_malloc0 (8 * 8 * 4);
+  guchar           fg_r, fg_g, fg_b, fg_a;
+  guchar           bg_r, bg_g, bg_b, bg_a;
+  gint             x, y;
+  guchar          *d;
+
+  g_return_val_if_fail (fg != NULL, NULL);
+  g_return_val_if_fail (bg != NULL, NULL);
+
+  data = g_malloc (8 * 8 * 4);
+
+  gimp_rgba_get_uchar (fg, &fg_r, &fg_g, &fg_b, &fg_a);
+  gimp_rgba_get_uchar (bg, &bg_r, &bg_g, &bg_b, &bg_a);
+
+  d = data;
+
+  for (y = 0; y < 8; y++)
+    {
+      for (x = 0; x < 8; x++)
+        {
+          if ((x + y + index) % 8 >= 4)
+            GIMP_CAIRO_ARGB32_SET_PIXEL (d, fg_r, fg_g, fg_b, fg_a);
+          else
+            GIMP_CAIRO_ARGB32_SET_PIXEL (d, bg_r, bg_g, bg_b, bg_a);
+
+          d += 4;
+        }
+    }
+
+  surface = cairo_image_surface_create_for_data (data,
+                                                 CAIRO_FORMAT_ARGB32,
+                                                 8, 8, 8 * 4);
+  cairo_surface_set_user_data (surface, &surface_data_key,
+                               data, (cairo_destroy_func_t) g_free);
+
+  pattern = cairo_pattern_create_for_surface (surface);
+  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
+
+  cairo_surface_destroy (surface);
+
+  return pattern;
+}
diff --git a/app/widgets/gimpcairo.h b/app/widgets/gimpcairo.h
new file mode 100644
index 0000000..9e04305
--- /dev/null
+++ b/app/widgets/gimpcairo.h
@@ -0,0 +1,33 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpcairo.h
+ * Copyright (C) 2010  Michael Natterer <mitch gimp org>
+ *
+ * Some code here is based on code from librsvg that was originally
+ * written by Raph Levien <raph artofcode com> for Gill.
+ *
+ * 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_CAIRO_H__
+#define __GIMP_CAIRO_H__
+
+
+cairo_pattern_t * gimp_cairo_stipple_pattern_create (const GimpRGB *fg,
+                                                     const GimpRGB *bg,
+                                                     gint           index);
+
+
+#endif /* __GIMP_CAIRO_H__ */



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