[gnumeric] Style: cleanup.



commit 8ac5399016eda7b55489ea613f7f31574b61f656
Author: Morten Welinder <terra gnome org>
Date:   Fri Mar 21 16:20:52 2014 -0400

    Style: cleanup.
    
    This exposes a function that can compare a single style element from
    two styles.  xlsx writing does that quite a bit -- spot the bug -- and
    its ugly with a function like this.

 plugins/excel/ChangeLog    |    4 ++
 plugins/excel/xlsx-write.c |   94 +++++++++----------------------------------
 src/mstyle.c               |   21 ++++++++++
 src/mstyle.h               |    1 +
 4 files changed, 46 insertions(+), 74 deletions(-)
---
diff --git a/plugins/excel/ChangeLog b/plugins/excel/ChangeLog
index 98d9994..c121651 100644
--- a/plugins/excel/ChangeLog
+++ b/plugins/excel/ChangeLog
@@ -1,5 +1,9 @@
 2014-03-21  Morten Welinder  <terra gnome org>
 
+       * xlsx-write.c (xlsx_find_font): Use new gnm_style_equal_elem.
+       (xlsx_find_border): Ditto.
+       (xlsx_find_fill): Ditto.
+
        * xlsx-utils.c (xlsx_output_string, xlsx_string_parser): New
        function.  Fixes #726824.
 
diff --git a/plugins/excel/xlsx-write.c b/plugins/excel/xlsx-write.c
index a264f52..a00b945 100644
--- a/plugins/excel/xlsx-write.c
+++ b/plugins/excel/xlsx-write.c
@@ -436,36 +436,15 @@ xlsx_find_font (GnmStyle const *style, GPtrArray  *styles)
        unsigned i;
        for (i = 0 ; i < styles->len ; i++) {
                GnmStyle const *old_style = g_ptr_array_index (styles, i);
-               if (style == old_style)
-                       return (gint) i;
-               if (gnm_style_is_element_set (style, MSTYLE_FONT_BOLD) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_BOLD) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_ITALIC) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_ITALIC) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_UNDERLINE) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_UNDERLINE) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_COLOR) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_COLOR) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_NAME) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_NAME) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_SIZE) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_SIZE) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_SCRIPT) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_SCRIPT) ||
-                   gnm_style_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_FONT_STRIKETHROUGH))
-                       continue;
-               if (gnm_style_get_font_bold (style) != gnm_style_get_font_bold (old_style) ||
-                   gnm_style_get_font_italic (style) != gnm_style_get_font_italic (old_style) ||
-                   gnm_style_get_font_uline (style) != gnm_style_get_font_uline (old_style) ||
-                   gnm_style_get_font_color (style)->go_color !=
-                   gnm_style_get_font_color (old_style)->go_color ||
-                   gnm_style_get_font_size (style) != gnm_style_get_font_size (old_style) ||
-                   gnm_style_get_font_script (style) != gnm_style_get_font_script (old_style) ||
-                   gnm_style_get_font_strike (style) != gnm_style_get_font_strike (old_style))
-                       continue;
-               if (0 == strcmp (gnm_style_get_font_name (style),
-                                gnm_style_get_font_name (old_style)))
+               if (style == old_style ||
+                   (gnm_style_equal_elem (style, old_style, MSTYLE_FONT_BOLD) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_ITALIC) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_UNDERLINE) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_COLOR) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_SIZE) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_SCRIPT) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_STRIKETHROUGH) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_FONT_NAME)))
                        return (gint) i;
        }
        return -1;
@@ -598,22 +577,10 @@ xlsx_find_fill (GnmStyle const *style, GPtrArray *styles)
                return j;
        for (i = 0 ; i < styles->len ; i++) {
                GnmStyle const *old_style = g_ptr_array_index (styles, i);
-               if (style == old_style)
-                       return (gint) i;
-               if (!(gnm_style_is_element_set (style, MSTYLE_COLOR_BACK) !=
-                     gnm_style_is_element_set (old_style, MSTYLE_COLOR_BACK)
-                     || gnm_style_is_element_set (style, MSTYLE_COLOR_PATTERN) !=
-                     gnm_style_is_element_set (old_style, MSTYLE_COLOR_PATTERN)
-                     || gnm_style_is_element_set (style, MSTYLE_PATTERN) !=
-                     gnm_style_is_element_set (style, MSTYLE_PATTERN)
-                     || (gnm_style_is_element_set (style, MSTYLE_PATTERN) &&
-                         gnm_style_get_pattern (style) != gnm_style_get_pattern (old_style))
-                     || (gnm_style_is_element_set (style, MSTYLE_COLOR_BACK) &&
-                         gnm_style_get_back_color (style)->go_color !=
-                         gnm_style_get_back_color (old_style)->go_color)
-                     || (gnm_style_is_element_set (style, MSTYLE_COLOR_PATTERN) &&
-                         gnm_style_get_pattern_color (style)->go_color !=
-                         gnm_style_get_pattern_color (old_style)->go_color)))
+               if (style == old_style ||
+                   (gnm_style_equal_elem (style, old_style, MSTYLE_COLOR_BACK) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_COLOR_PATTERN) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_PATTERN)))
                        return (gint) i + N_PREDEFINED_FILLS;
        }
        return -1;
@@ -722,35 +689,14 @@ xlsx_find_border (GnmStyle const *style, GPtrArray  *styles)
        unsigned i;
        for (i = 0 ; i < styles->len ; i++) {
                GnmStyle const *old_style = g_ptr_array_index (styles, i);
-               if (style == old_style)
+               if (style == old_style ||
+                   (gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_TOP) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_BOTTOM) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_LEFT) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_RIGHT) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_DIAGONAL) &&
+                    gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_REV_DIAGONAL)))
                        return (gint) i;
-               if (gnm_style_is_element_set (style, MSTYLE_BORDER_TOP) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_BORDER_TOP) ||
-                   gnm_style_is_element_set (style, MSTYLE_BORDER_BOTTOM) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_BORDER_BOTTOM) ||
-                   gnm_style_is_element_set (style, MSTYLE_BORDER_LEFT) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_BORDER_LEFT) ||
-                   gnm_style_is_element_set (style, MSTYLE_BORDER_RIGHT) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_BORDER_RIGHT) ||
-                   gnm_style_is_element_set (style, MSTYLE_BORDER_REV_DIAGONAL) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_BORDER_REV_DIAGONAL) ||
-                   gnm_style_is_element_set (style, MSTYLE_BORDER_DIAGONAL) !=
-                   gnm_style_is_element_set (old_style, MSTYLE_BORDER_DIAGONAL))
-                       continue;
-               if (gnm_style_get_border (style, MSTYLE_BORDER_TOP)
-                   != gnm_style_get_border (old_style, MSTYLE_BORDER_TOP) ||
-                   gnm_style_get_border (style, MSTYLE_BORDER_BOTTOM)
-                   != gnm_style_get_border (old_style, MSTYLE_BORDER_BOTTOM) ||
-                   gnm_style_get_border (style, MSTYLE_BORDER_LEFT)
-                   != gnm_style_get_border (old_style, MSTYLE_BORDER_LEFT) ||
-                   gnm_style_get_border (style, MSTYLE_BORDER_RIGHT)
-                   != gnm_style_get_border (old_style, MSTYLE_BORDER_RIGHT) ||
-                   gnm_style_get_border (style, MSTYLE_BORDER_REV_DIAGONAL)
-                   != gnm_style_get_border (old_style, MSTYLE_BORDER_REV_DIAGONAL) ||
-                   gnm_style_get_border (style, MSTYLE_BORDER_DIAGONAL)
-                   != gnm_style_get_border (old_style, MSTYLE_BORDER_DIAGONAL))
-                       continue;
-               return (gint) i;
        }
        return -1;
 }
diff --git a/src/mstyle.c b/src/mstyle.c
index 4809609..9c3c5e9 100644
--- a/src/mstyle.c
+++ b/src/mstyle.c
@@ -986,6 +986,27 @@ gnm_style_equal_XL (GnmStyle const *a, GnmStyle const *b)
        return TRUE;
 }
 
+/**
+ * gnm_style_equal_elem:
+ * @a: first style
+ * @b: second style
+ * @e: style element
+ *
+ * Returns: %TRUE, if the the two styles have the same contents for the
+ * given element, either because neither have it set, or because both
+ * have it set and to the same value.
+ */
+gboolean
+gnm_style_equal_elem (GnmStyle const *a, GnmStyle const *b, GnmStyleElement e)
+{
+       if (elem_is_set (a, e))
+               return elem_is_set (b, e) && elem_is_eq (a, b, e);
+       else
+               return !elem_is_set (b, e);
+}
+
+
+
 #define CMP_TRY_NUMBER_RAW(a_,b_)              \
   do {                                         \
     if ((a_) < (b_)) return -1;                        \
diff --git a/src/mstyle.h b/src/mstyle.h
index 572719e..9fe11ce 100644
--- a/src/mstyle.h
+++ b/src/mstyle.h
@@ -81,6 +81,7 @@ gboolean    gnm_style_equal         (GnmStyle const *a, GnmStyle const *b);
 gboolean    gnm_style_equal_XL     (GnmStyle const *a, GnmStyle const *b);
 gboolean    gnm_style_equal_header  (GnmStyle const *a, GnmStyle const *b,
                                     gboolean top);
+gboolean    gnm_style_equal_elem    (GnmStyle const *a, GnmStyle const *b, GnmStyleElement e);
 
 guint       gnm_style_hash          (gconstpointer style);
 guint       gnm_style_hash_XL      (gconstpointer style);


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