[dia] [gradient] Standard - *: use pattern property and perform fill with it



commit 6bb2a91c65070c2d15caf1a8548d74a59b53f059
Author: Hans Breuer <hans breuer org>
Date:   Tue Dec 24 15:17:10 2013 +0100

    [gradient] Standard - *: use pattern property and perform fill with it
    
    Step 4 of 7 for gradient support in Dia: the standard objects used for
    most importers now have a pattern property.

 lib/standard-path.c          |   28 ++++++++++++++++++++++++----
 objects/standard/beziergon.c |   27 +++++++++++++++++++++++++--
 objects/standard/box.c       |   32 ++++++++++++++++++++++++++------
 objects/standard/ellipse.c   |   26 ++++++++++++++++++++++++--
 objects/standard/polygon.c   |   26 +++++++++++++++++++++++---
 5 files changed, 122 insertions(+), 17 deletions(-)
---
diff --git a/lib/standard-path.c b/lib/standard-path.c
index 1aa1b6f..f56cacf 100644
--- a/lib/standard-path.c
+++ b/lib/standard-path.c
@@ -42,6 +42,7 @@
 #include "standard-path.h"
 #include "create.h"
 #include "bezier-common.h"
+#include "pattern.h"
 
 #include "dia-lib-icons.h"
 
@@ -102,6 +103,9 @@ struct _StdPath {
   /* mostly useful for debugging transformations */
   gboolean show_control_lines;
 
+  /*! optional gradient */
+  DiaPattern *pattern;
+
   Handle handles[NUM_HANDLES];
 
   HandleMoveReason move_reason;
@@ -142,6 +146,7 @@ static PropDescription stdpath_props[] = {
   /* just to simplify transfering properties between objects */
   { "show_background", PROP_TYPE_BOOL, PROP_FLAG_DONT_SAVE,  N_("Draw background"), NULL, NULL },
   { "show_control_lines", PROP_TYPE_BOOL, PROP_FLAG_OPTIONAL, N_("Draw Control Lines") },
+  { "pattern", PROP_TYPE_PATTERN, PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL, N_("Pattern"), NULL },
   PROP_DESC_END
 };
 
@@ -156,6 +161,7 @@ static PropOffset stdpath_offsets[] = {
   { "fill_colour", PROP_TYPE_COLOUR, offsetof(StdPath, fill_color) },
   { "show_background", PROP_TYPE_BOOL, offsetof(StdPath, show_background) },
   { "show_control_lines", PROP_TYPE_BOOL, offsetof(StdPath, show_control_lines) },
+  { "pattern", PROP_TYPE_PATTERN, offsetof(StdPath, pattern) },
   { NULL, 0, 0 }
 };
 
@@ -412,16 +418,30 @@ stdpath_draw(StdPath *stdpath, DiaRenderer *renderer)
   DIA_RENDERER_GET_CLASS (renderer)->set_linecaps(renderer, stdpath->line_caps);
 
   if (DIA_RENDERER_GET_CLASS (renderer)->is_capable_to(renderer, RENDER_HOLES)) {
-    if (stdpath->stroke_or_fill & PDO_FILL)
+    if (stdpath->stroke_or_fill & PDO_FILL) {
+      Color fill = stdpath->fill_color;
+      if (stdpath->pattern) {
+       dia_pattern_get_fallback_color (stdpath->pattern, &fill);
+       if (DIA_RENDERER_GET_CLASS (renderer)->is_capable_to(renderer, RENDER_PATTERN))
+         DIA_RENDERER_GET_CLASS (renderer)->set_pattern (renderer, stdpath->pattern);
+      }
       DIA_RENDERER_GET_CLASS (renderer)->fill_bezier(renderer, stdpath->points, stdpath->num_points, 
-                                                    &stdpath->fill_color);
+                                                    &fill);
+      if (DIA_RENDERER_GET_CLASS (renderer)->is_capable_to(renderer, RENDER_PATTERN))
+       DIA_RENDERER_GET_CLASS (renderer)->set_pattern (renderer, NULL);
+    }
     if (stdpath->stroke_or_fill & PDO_STROKE)
       DIA_RENDERER_GET_CLASS (renderer)->draw_bezier(renderer, stdpath->points, stdpath->num_points, 
                                                     &stdpath->line_color);
   } else {
     /* step-wise approach */
-    if (stdpath->stroke_or_fill & PDO_FILL)
-      bezier_render_fill (renderer, stdpath->points, stdpath->num_points, &stdpath->fill_color);
+    /* if it wouldn't RENDER_HOLES it presumably also wouldn't RENDER_PATTERN ... */
+    if (stdpath->stroke_or_fill & PDO_FILL) {
+      Color fill = stdpath->fill_color;
+      if (stdpath->pattern)
+       dia_pattern_get_fallback_color (stdpath->pattern, &fill);
+      bezier_render_fill (renderer, stdpath->points, stdpath->num_points, &fill);
+    }
     if (stdpath->stroke_or_fill & PDO_STROKE)
       bezier_render_stroke (renderer, stdpath->points, stdpath->num_points, &stdpath->line_color);
   }
diff --git a/objects/standard/beziergon.c b/objects/standard/beziergon.c
index 2f1c6fd..ec8bbc9 100644
--- a/objects/standard/beziergon.c
+++ b/objects/standard/beziergon.c
@@ -35,6 +35,7 @@
 #include "diamenu.h"
 #include "properties.h"
 #include "create.h"
+#include "pattern.h"
 
 #include "tool-icons.h"
 
@@ -55,6 +56,7 @@ typedef struct _Beziergon {
   gboolean show_background;
   real dashlength;
   real line_width;
+  DiaPattern *pattern;
 } Beziergon;
 
 typedef struct _BeziergonProperties BeziergonProperties;
@@ -104,6 +106,8 @@ static PropDescription beziergon_props[] = {
   PROP_STD_LINE_JOIN_OPTIONAL,
   PROP_STD_FILL_COLOUR,
   PROP_STD_SHOW_BACKGROUND,
+  { "pattern", PROP_TYPE_PATTERN, PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL,
+    N_("Pattern"), NULL },
   PROP_DESC_END
 };
 
@@ -116,6 +120,7 @@ static PropOffset beziergon_offsets[] = {
   { "line_join", PROP_TYPE_ENUM, offsetof(Beziergon, line_join) },
   { "fill_colour", PROP_TYPE_COLOUR, offsetof(Beziergon, inner_color) },
   { "show_background", PROP_TYPE_BOOL, offsetof(Beziergon, show_background) },
+  { "pattern", PROP_TYPE_PATTERN, offsetof(Beziergon, pattern) },
   { NULL, 0, 0 }
 };
 
@@ -225,8 +230,17 @@ beziergon_draw(Beziergon *beziergon, DiaRenderer *renderer)
   renderer_ops->set_linejoin(renderer, beziergon->line_join);
   renderer_ops->set_linecaps(renderer, LINECAPS_BUTT);
 
-  if (beziergon->show_background)
-    renderer_ops->fill_bezier(renderer, points, n, &beziergon->inner_color);
+  if (beziergon->show_background) {
+    Color fill = beziergon->inner_color;
+    if (beziergon->pattern) {
+      dia_pattern_get_fallback_color (beziergon->pattern, &fill);
+      if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+        renderer_ops->set_pattern (renderer, beziergon->pattern);
+    }
+    renderer_ops->fill_bezier(renderer, points, n, &fill);
+    if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+      renderer_ops->set_pattern (renderer, NULL);
+  }
 
   renderer_ops->draw_bezier(renderer, points, n, &beziergon->line_color);
 
@@ -323,6 +337,8 @@ beziergon_copy(Beziergon *beziergon)
   newbeziergon->dashlength = beziergon->dashlength;
   newbeziergon->inner_color = beziergon->inner_color;
   newbeziergon->show_background = beziergon->show_background;
+  if (beziergon->pattern)
+    newbeziergon->pattern = g_object_ref (beziergon->pattern);
 
   return &newbeziergon->bezier.object;
 }
@@ -387,6 +403,9 @@ beziergon_save(Beziergon *beziergon, ObjectNode obj_node,
     data_add_enum(new_attribute(obj_node, "line_join"),
                   beziergon->line_join);
 
+  if (beziergon->pattern)
+    data_add_pattern(new_attribute(obj_node, "pattern"),
+                    beziergon->pattern);
 }
 
 static DiaObject *
@@ -442,6 +461,10 @@ beziergon_load(ObjectNode obj_node, int version, DiaContext *ctx)
   if (attr != NULL)
     beziergon->dashlength = data_real(attribute_first_data(attr), ctx);
 
+  attr = object_find_attribute(obj_node, "pattern");
+  if (attr != NULL)
+    beziergon->pattern = data_pattern(attribute_first_data(attr), ctx);
+
   beziergon_update_data(beziergon);
 
   return &beziergon->bezier.object;
diff --git a/objects/standard/box.c b/objects/standard/box.c
index 481e6c1..cb301a7 100644
--- a/objects/standard/box.c
+++ b/objects/standard/box.c
@@ -31,6 +31,7 @@
 #include "attributes.h"
 #include "properties.h"
 #include "create.h"
+#include "pattern.h"
 
 #include "tool-icons.h"
 
@@ -67,6 +68,7 @@ struct _Box {
   real dashlength;
   real corner_radius;
   AspectType aspect;
+  DiaPattern *pattern;
 };
 
 static struct _BoxProperties {
@@ -118,6 +120,7 @@ static PropOffset box_offsets[] = {
     offsetof(Box, line_style), offsetof(Box, dashlength) },
   { "line_join", PROP_TYPE_ENUM, offsetof(Box, line_join) },
   { "corner_radius", PROP_TYPE_REAL, offsetof(Box, corner_radius) },
+  { "pattern", PROP_TYPE_PATTERN, offsetof(Box, pattern) },
   { NULL, 0, 0 }
 };
 
@@ -141,6 +144,8 @@ static PropDescription box_props[] = {
     N_("Corner radius"), NULL, &corner_radius_data },
   { "aspect", PROP_TYPE_ENUM, PROP_FLAG_VISIBLE,
     N_("Aspect ratio"), NULL, prop_aspect_data },
+  { "pattern", PROP_TYPE_PATTERN, PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL,
+    N_("Pattern"), NULL },
   PROP_DESC_END
 };
 
@@ -316,21 +321,27 @@ box_draw(Box *box, DiaRenderer *renderer)
     renderer_ops->set_linejoin(renderer, box->line_join);
 
   if (box->show_background) {
+    Color fill = box->inner_color;
     renderer_ops->set_fillstyle(renderer, FILLSTYLE_SOLID);
-  
-    /* Problem:  How do we make the fill with rounded corners? */
+    if (box->pattern) {
+      dia_pattern_get_fallback_color (box->pattern, &fill);
+      if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+        renderer_ops->set_pattern (renderer, box->pattern);
+    }
     if (box->corner_radius > 0) {
       renderer_ops->fill_rounded_rect(renderer,
                                       &elem->corner,
                                       &lr_corner,
-                                      &box->inner_color,
+                                      &fill,
                                       box->corner_radius);
     } else {
       renderer_ops->fill_rect(renderer, 
                               &elem->corner,
                               &lr_corner, 
-                              &box->inner_color);
+                              &fill);
     }
+    if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+      renderer_ops->set_pattern (renderer, NULL);
   }
 
   if (box->corner_radius > 0) {
@@ -347,7 +358,6 @@ box_draw(Box *box, DiaRenderer *renderer)
   }
 }
 
-
 static void
 box_update_data(Box *box)
 {
@@ -496,7 +506,9 @@ box_copy(Box *box)
   newbox->dashlength = box->dashlength;
   newbox->corner_radius = box->corner_radius;
   newbox->aspect = box->aspect;
-  
+  if (box->pattern)
+    newbox->pattern = g_object_ref (box->pattern);
+
   for (i=0;i<NUM_CONNECTIONS;i++) {
     newobj->connections[i] = &newbox->connections[i];
     newbox->connections[i].object = newobj;
@@ -548,6 +560,10 @@ box_save(Box *box, ObjectNode obj_node, const char *filename)
   if (box->aspect != FREE_ASPECT)
     data_add_enum(new_attribute(obj_node, "aspect"),
                  box->aspect);
+
+  if (box->pattern)
+    data_add_pattern(new_attribute(obj_node, "pattern"),
+                    box->pattern);
 }
 
 static DiaObject *
@@ -613,6 +629,10 @@ box_load(ObjectNode obj_node, int version, DiaContext *ctx)
   if (attr != NULL)
     box->aspect = data_enum(attribute_first_data(attr), ctx);
 
+  attr = object_find_attribute(obj_node, "pattern");
+  if (attr != NULL)
+    box->pattern = data_pattern(attribute_first_data(attr), ctx);
+
   element_init(elem, 8, NUM_CONNECTIONS);
 
   for (i=0;i<NUM_CONNECTIONS;i++) {
diff --git a/objects/standard/ellipse.c b/objects/standard/ellipse.c
index a08664a..d578931 100644
--- a/objects/standard/ellipse.c
+++ b/objects/standard/ellipse.c
@@ -30,6 +30,7 @@
 #include "diarenderer.h"
 #include "attributes.h"
 #include "properties.h"
+#include "pattern.h"
 
 #include "tool-icons.h"
 
@@ -63,6 +64,7 @@ struct _Ellipse {
   AspectType aspect;
   LineStyle line_style;
   real dashlength;
+  DiaPattern *pattern;
 };
 
 static struct _EllipseProperties {
@@ -110,6 +112,7 @@ static PropOffset ellipse_offsets[] = {
   { "aspect", PROP_TYPE_ENUM, offsetof(Ellipse, aspect) },
   { "line_style", PROP_TYPE_LINESTYLE,
     offsetof(Ellipse, line_style), offsetof(Ellipse, dashlength) },
+  { "pattern", PROP_TYPE_PATTERN, offsetof(Ellipse, pattern) },
   { NULL, 0, 0 }
 };
 
@@ -128,6 +131,8 @@ static PropDescription ellipse_props[] = {
   PROP_STD_LINE_STYLE,
   { "aspect", PROP_TYPE_ENUM, PROP_FLAG_VISIBLE,
     N_("Aspect ratio"), NULL, prop_aspect_data },
+  { "pattern", PROP_TYPE_PATTERN, PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL,
+    N_("Pattern"), NULL },
   PROP_DESC_END
 };
 
@@ -304,12 +309,19 @@ ellipse_draw(Ellipse *ellipse, DiaRenderer *renderer)
   center.y = elem->corner.y + elem->height/2;
 
   if (ellipse->show_background) {
+    Color fill = ellipse->inner_color;
     renderer_ops->set_fillstyle(renderer, FILLSTYLE_SOLID);
-
+    if (ellipse->pattern) {
+      dia_pattern_get_fallback_color (ellipse->pattern, &fill);
+      if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+        renderer_ops->set_pattern (renderer, ellipse->pattern);
+    }
     renderer_ops->fill_ellipse(renderer, 
                                &center,
                                elem->width, elem->height,
-                               &ellipse->inner_color);
+                               &fill);
+    if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+      renderer_ops->set_pattern (renderer, NULL);
   }
 
   renderer_ops->set_linewidth(renderer, ellipse->border_width);
@@ -466,6 +478,8 @@ ellipse_copy(Ellipse *ellipse)
   newellipse->show_background = ellipse->show_background;
   newellipse->aspect = ellipse->aspect;
   newellipse->line_style = ellipse->line_style;
+  if (ellipse->pattern)
+    newellipse->pattern = g_object_ref (ellipse->pattern);
 
   newellipse->center_handle = ellipse->center_handle;
   newellipse->center_handle.connected_to = NULL;
@@ -517,6 +531,10 @@ ellipse_save(Ellipse *ellipse, ObjectNode obj_node, const char *filename)
            data_add_real(new_attribute(obj_node, "dashlength"),
                          ellipse->dashlength);
   }
+
+  if (ellipse->pattern)
+    data_add_pattern(new_attribute(obj_node, "pattern"),
+                    ellipse->pattern);
 }
 
 static DiaObject *ellipse_load(ObjectNode obj_node, int version, DiaContext *ctx)
@@ -571,6 +589,10 @@ static DiaObject *ellipse_load(ObjectNode obj_node, int version, DiaContext *ctx
   if (attr != NULL)
     ellipse->dashlength = data_real(attribute_first_data(attr), ctx);
 
+  attr = object_find_attribute(obj_node, "pattern");
+  if (attr != NULL)
+    ellipse->pattern = data_pattern(attribute_first_data(attr), ctx);
+
   element_init(elem, 9, 9);
 
   obj->handles[8] = &ellipse->center_handle;
diff --git a/objects/standard/polygon.c b/objects/standard/polygon.c
index 73d803b..d53d423 100644
--- a/objects/standard/polygon.c
+++ b/objects/standard/polygon.c
@@ -31,6 +31,7 @@
 #include "attributes.h"
 #include "diamenu.h"
 #include "properties.h"
+#include "pattern.h"
 
 #include "tool-icons.h"
 
@@ -60,6 +61,7 @@ typedef struct _Polygon {
   gboolean show_background;
   real dashlength;
   real line_width;
+  DiaPattern *pattern;
 } Polygon;
 
 static struct _PolygonProperties {
@@ -106,6 +108,8 @@ static PropDescription polygon_props[] = {
   PROP_STD_LINE_JOIN_OPTIONAL,
   PROP_STD_FILL_COLOUR_OPTIONAL,
   PROP_STD_SHOW_BACKGROUND_OPTIONAL,
+  { "pattern", PROP_TYPE_PATTERN, PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL,
+    N_("Pattern"), NULL },
   PROP_DESC_END
 };
 
@@ -118,6 +122,7 @@ static PropOffset polygon_offsets[] = {
   { "line_join", PROP_TYPE_ENUM, offsetof(Polygon, line_join) },
   { "fill_colour", PROP_TYPE_COLOUR, offsetof(Polygon, inner_color) },
   { "show_background", PROP_TYPE_BOOL, offsetof(Polygon, show_background) },
+  { "pattern", PROP_TYPE_PATTERN, offsetof(Polygon, pattern) },
   { NULL, 0, 0 }
 };
 
@@ -226,9 +231,15 @@ polygon_draw(Polygon *polygon, DiaRenderer *renderer)
   renderer_ops->set_linejoin(renderer, polygon->line_join);
   renderer_ops->set_linecaps(renderer, LINECAPS_BUTT);
 
-  if (polygon->show_background)
-    renderer_ops->fill_polygon(renderer, points, n, &polygon->inner_color);
-
+  if (polygon->show_background) {
+    Color fill = polygon->inner_color;
+    if (polygon->pattern) {
+      dia_pattern_get_fallback_color (polygon->pattern, &fill);
+      if (renderer_ops->is_capable_to(renderer, RENDER_PATTERN))
+        renderer_ops->set_pattern (renderer, polygon->pattern);
+    }
+    renderer_ops->fill_polygon(renderer, points, n, &fill);
+  }
   renderer_ops->draw_polygon(renderer, points, n, &polygon->line_color);
 }
 
@@ -306,6 +317,8 @@ polygon_copy(Polygon *polygon)
   newpolygon->dashlength = polygon->dashlength;
   newpolygon->inner_color = polygon->inner_color;
   newpolygon->show_background = polygon->show_background;
+  if (polygon->pattern)
+    newpolygon->pattern = g_object_ref (polygon->pattern);
 
   return &newpolygon->poly.object;
 }
@@ -360,6 +373,9 @@ polygon_save(Polygon *polygon, ObjectNode obj_node,
     data_add_enum(new_attribute(obj_node, "line_join"),
                   polygon->line_join);
 
+  if (polygon->pattern)
+    data_add_pattern(new_attribute(obj_node, "pattern"),
+                    polygon->pattern);
 }
 
 static DiaObject *
@@ -415,6 +431,10 @@ polygon_load(ObjectNode obj_node, int version, DiaContext *ctx)
   if (attr != NULL)
     polygon->dashlength = data_real(attribute_first_data(attr), ctx);
 
+  attr = object_find_attribute(obj_node, "pattern");
+  if (attr != NULL)
+    polygon->pattern = data_pattern(attribute_first_data(attr), ctx);
+
   polygon_update_data(polygon);
 
   return &polygon->poly.object;


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