Code

* src/Makefile_insert, src/Makefile.am, src/svg/Makefile_insert,
[inkscape.git] / src / style.cpp
index c826c2831c46f8fd819eababa700e4e50cd53dbe..567825164b44d1d9933ff52482a8ee9a164c8ae1 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "svg/svg.h"
 #include "svg/svg-color.h"
+#include "svg/svg-icc-color.h"
 
 #include "display/canvas-bpath.h"
 #include "attributes.h"
@@ -40,6 +41,9 @@
 #include "unit-constants.h"
 #include "isnan.h"
 
+using Inkscape::CSSOStringStream;
+using std::vector;
+
 namespace Inkscape {
 
 /**
@@ -111,6 +115,7 @@ static void sp_style_clear(SPStyle *style);
 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
 
 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
+static void sp_style_merge_ifilter(SPIFilter *child, SPIFilter const *parent);
 static void sp_style_read_dash(SPStyle *style, gchar const *str);
 
 static SPTextStyle *sp_text_style_new(void);
@@ -130,6 +135,7 @@ static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *s
 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
+static void sp_style_read_ifilter(SPIFilter *f, gchar const *str, SPDocument *document);
 
 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
@@ -145,6 +151,7 @@ static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint
 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
 static gint sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key, SPILengthOrNormal const *const val, SPILengthOrNormal const *const base, guint const flags);
 static gint sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key, SPITextDecoration const *const val, SPITextDecoration const *const base, guint const flags);
+static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
 
 static void css2_unescape_unquote(SPIString *val);
 
@@ -644,6 +651,14 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
         }
     }
 
+    /* filter effects */
+    if (!style->filter.set) {
+        val = repr->attribute("filter");
+        if (val) {
+            sp_style_read_ifilter(&style->filter, val, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+        }
+    }
+            
     /* 3. Merge from parent */
     if (object) {
         if (object->parent) {
@@ -871,12 +886,14 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
                 sp_style_read_iscale24(&style->opacity, val);
             }
             break;
-            /* Filter */
         case SP_PROP_ENABLE_BACKGROUND:
             g_warning("Unimplemented style property SP_PROP_ENABLE_BACKGROUND: value: %s", val);
             break;
+            /* Filter */
         case SP_PROP_FILTER:
-            g_warning("Unimplemented style property SP_PROP_FILTER: value: %s", val);
+            if (!style->filter.set && !style->filter.inherit) {
+                sp_style_read_ifilter(&style->filter, val, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
+            }
             break;
         case SP_PROP_FLOOD_COLOR:
             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
@@ -1411,6 +1428,11 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
             style->marker[i].value = g_strdup(parent->marker[i].value);
         }
     }
+
+    /* Filter effects */
+    if(style->filter.set && style->filter.inherit) {
+        sp_style_merge_ifilter(&style->filter, &parent->filter);
+    }
 }
 
 template <typename T>
@@ -1921,6 +1943,8 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
          * represent it as a normal SPILength; though will need to do something about existing
          * users of stroke_dash.offset and stroke_dashoffset_set. */
     }
+
+    /* TODO: deal with filters */
 }
 
 
@@ -2038,6 +2062,19 @@ sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
 }
 
 
+/**
+ * Merge filter style from parent.
+ * Filter effects do not inherit by default
+ */
+static void
+sp_style_merge_ifilter(SPIFilter *child, SPIFilter const *parent)
+{
+    child->set = parent->set;
+    child->inherit = parent->inherit;
+    child->filter = parent->filter;
+    child->uri = parent->uri;
+}
+
 /**
  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
@@ -2159,6 +2196,9 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
 
+    /* filter: */
+    p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
+
     /* fixme: */
     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
 
@@ -2273,6 +2313,9 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
 
+    /* filter: */
+    p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
+
     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
 
     /** \todo
@@ -2423,6 +2466,13 @@ sp_style_clear(SPStyle *style)
         g_free(style->marker[i].value);
         style->marker[i].set = FALSE;
     }
+
+    style->filter.set = FALSE;
+//Are these really needed?
+    style->filter.inherit = FALSE;
+    style->filter.uri = NULL;
+    style->filter.filter = FALSE;
+
 }
 
 
@@ -2899,13 +2949,25 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
             }
         }
     } else {
-        guint32 const rgb0 = sp_svg_read_color(str, 0xff);
+        guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
         if (rgb0 != 0xff) {
             paint->type = SP_PAINT_TYPE_COLOR;
             sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
             paint->set = TRUE;
             paint->inherit = FALSE;
             paint->currentcolor = FALSE;
+
+            while (g_ascii_isspace(*str)) {
+                ++str;
+            }
+            if (strneq(str, "icc-color(", 10)) {
+                SVGICCColor* tmp = new SVGICCColor();
+                if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
+                    delete tmp;
+                    tmp = 0;
+                }
+                paint->iccColor = tmp;
+            }
         }
     }
 }
@@ -2978,6 +3040,56 @@ sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
 
 
 
+/**
+ * Set SPIFilter object from string.
+ */
+static void
+sp_style_read_ifilter(SPIFilter *f, gchar const *str, SPDocument *document)
+{
+    /* Try all possible values: inherit, none, uri */
+    if (streq(str, "inherit")) {
+        f->set = TRUE;
+        f->inherit = TRUE;
+        f->filter = NULL;
+    } else if(streq(str, "none")) {
+        f->set = TRUE;
+        f->inherit = FALSE;
+        f->filter = NULL;
+    } else if (strneq(str, "url", 3)) {
+        f->uri = extract_uri(str);
+        if(f->uri == NULL || f->uri[0] == '\0') {
+            g_warning("Specified filter url is empty");
+            f->set = TRUE;
+            f->inherit = FALSE;
+            f->filter = NULL;
+            return;
+        }
+        f->set = TRUE;
+        f->inherit = FALSE;
+        f->filter = NULL;
+        if (document) {
+            SPObject *obj;
+            obj = sp_uri_reference_resolve(document, str);
+            if (SP_IS_FILTER(obj)) {
+                f->filter = SP_FILTER(obj);
+                //g_signal_connect(G_OBJECT(f->filter), "release",
+                //                 G_CALLBACK(sp_style_filter_release), style);
+                //g_signal_connect(G_OBJECT(f->filter), "modified",
+                //                 G_CALLBACK(sp_style_filter_modified), style);
+            } else {
+                g_warning("Element '%s' not found or is not a filter", f->uri);
+            }
+        }
+
+    } else {
+        /* We shouldn't reach this if SVG input is well-formed */
+        f->set = FALSE;
+        f->inherit = FALSE;
+        f->filter = NULL;
+        f->uri = NULL;
+    }
+}
+
 /**
  * Set SPIEnum object from repr attribute.
  */
@@ -3325,7 +3437,13 @@ sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
     if (a->type != b->type)
         return true;
     if (a->type == SP_PAINT_TYPE_COLOR)
-        return !sp_color_is_equal(&a->value.color, &b->value.color);
+        return !(sp_color_is_equal(&a->value.color, &b->value.color)
+                 && ((a->iccColor == b->iccColor)
+                     || (a->iccColor && b->iccColor
+                         && (a->iccColor->colorProfile == b->iccColor->colorProfile)
+                         && (a->iccColor->colors == b->iccColor->colors))));
+    /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
+     * in the string representation. */
     if (a->type == SP_PAINT_TYPE_PAINTSERVER)
         return (a->value.paint.server != b->value.paint.server);
     return false;
@@ -3354,7 +3472,19 @@ sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
                 case SP_PAINT_TYPE_COLOR: {
                     char color_buf[8];
                     sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
-                    return g_snprintf(b, len, "%s:%s;", key, color_buf);
+                    if (paint->iccColor) {
+                        CSSOStringStream css;
+                        css << color_buf << " icc-color(" << paint->iccColor->colorProfile;
+                        for (vector<double>::const_iterator i(paint->iccColor->colors.begin()),
+                                 iEnd(paint->iccColor->colors.end());
+                             i != iEnd; ++i) {
+                            css << ", " << *i;
+                        }
+                        css << ')';
+                        return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
+                    } else {
+                        return g_snprintf(b, len, "%s:%s;", key, color_buf);
+                    }
                 }
                 case SP_PAINT_TYPE_PAINTSERVER:
                     return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
@@ -3422,6 +3552,30 @@ sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
 }
 
 
+/**
+ * Write SPIFilter object into string.
+ */
+static gint
+sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
+                         SPIFilter const *const val, SPIFilter const *const base,
+                         guint const flags)
+{
+    if ((flags & SP_STYLE_FLAG_ALWAYS)
+        || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
+        || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
+    {
+        if (val->inherit) {
+            return g_snprintf(p, len, "%s:inherit;", key);
+        } else if (val->uri) {
+            return g_snprintf(p, len, "%s:url(%s);", key, val->uri);
+        }
+    }
+
+
+    return 0;
+}
+
+
 /**
  * Clear paint object, and disconnect style from paintserver (if present).
  */
@@ -3453,9 +3607,11 @@ sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
         }
 
         paint->value.paint.server = NULL;
-        paint->value.paint.uri = NULL;
-        paint->type = SP_PAINT_TYPE_NONE;
     }
+    paint->value.paint.uri = NULL;
+    paint->type = SP_PAINT_TYPE_NONE;
+    delete paint->iccColor;
+    paint->iccColor = NULL;
 }
 
 /**
@@ -3529,6 +3685,9 @@ sp_style_unset_property_attrs(SPObject *o)
     if (style->writing_mode.set) {
         repr->setAttribute("writing_mode", NULL);
     }
+    if (style->filter.set) {
+        repr->setAttribute("filter", NULL);
+    }
 }
 
 /**