Code

Correcting black gradient stops when swatches are set or drug.
[inkscape.git] / src / sp-gradient.cpp
index 0c0c947840aa148f86a2c3049dbf83e13a48c56f..68efd08322e2990a5bbdbde7638ca9bdce17d7bd 100644 (file)
@@ -45,6 +45,7 @@
 #include "streq.h"
 #include "uri.h"
 #include "xml/repr.h"
+#include "style.h"
 
 #define SP_MACROS_SILENT
 #include "macros.h"
@@ -172,8 +173,7 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value)
                 if (streq(p, "currentColor")) {
                     stop->currentColor = true;
                 } else {
-                    guint32 const color = sp_svg_read_color(p, 0);
-                    stop->specified_color.set( color );
+                    stop->specified_color = SPStop::readStopColor( p );
                 }
             }
             {
@@ -191,8 +191,7 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value)
                     stop->currentColor = true;
                 } else {
                     stop->currentColor = false;
-                    guint32 const color = sp_svg_read_color(p, 0);
-                    stop->specified_color.set( color );
+                    stop->specified_color = SPStop::readStopColor( p );
                 }
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
@@ -232,11 +231,12 @@ sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML:
         repr = xml_doc->createElement("svg:stop");
     }
 
-    guint32 specifiedcolor = stop->specified_color.toRGBA32( 255 );
+    Glib::ustring colorStr = stop->specified_color.toString();
     gfloat opacity = stop->opacity;
 
-    if (((SPObjectClass *) stop_parent_class)->write)
+    if (((SPObjectClass *) stop_parent_class)->write) {
         (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
+    }
 
     // Since we do a hackish style setting here (because SPStyle does not support stop-color and
     // stop-opacity), we must do it AFTER calling the parent write method; otherwise
@@ -247,9 +247,7 @@ sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML:
     if (stop->currentColor) {
         os << "currentColor";
     } else {
-        gchar c[64];
-        sp_svg_write_color(c, sizeof(c), specifiedcolor);
-        os << c;
+        os << colorStr;
     }
     os << ";stop-opacity:" << opacity;
     repr->setAttribute("style", os.str().c_str());
@@ -288,6 +286,17 @@ SPGradientSpread SPGradient::getSpread() const
     return spread;
 }
 
+void SPGradient::setSwatch( bool swatch )
+{
+    if ( swatch != isSwatch() ) {
+        this->swatch = swatch; // to make isSolid() work, this happens first
+        gchar const* paintVal = swatch ? (isSolid() ? "solid" : "gradient") : 0;
+        sp_object_setAttribute( this, "osb:paint", paintVal, 0 );
+
+        requestModified( SP_OBJECT_MODIFIED_FLAG );
+    }
+}
+
 /**
  * Return stop's color as 32bit value.
  */
@@ -312,29 +321,6 @@ sp_stop_get_rgba32(SPStop const *const stop)
     }
 }
 
-/**
- * Return stop's color as SPColor.
- */
-static SPColor
-sp_stop_get_color(SPStop const *const stop)
-{
-    if (stop->currentColor) {
-        char const *str = sp_object_get_style_property(stop, "color", NULL);
-        guint32 const dfl = 0;
-        /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
-         * value depends on user agent, and don't give any further restrictions that I can
-         * see.) */
-        guint32 color = dfl;
-        if (str) {
-            color = sp_svg_read_color(str, dfl);
-        }
-        SPColor ret( color );
-        return ret;
-    } else {
-        return stop->specified_color;
-    }
-}
-
 /*
  * Gradient
  */
@@ -422,11 +408,16 @@ void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML
 {
     SPGradient *gradient = SP_GRADIENT(object);
 
-    if (((SPObjectClass *) gradient_parent_class)->build)
+    // Work-around in case a swatch had been marked for immediate collection:
+    if ( repr->attribute("osb:paint") && repr->attribute("inkscape:collect") ) {
+        repr->setAttribute("inkscape:collect", 0);
+    }
+
+    if (((SPObjectClass *) gradient_parent_class)->build) {
         (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
+    }
 
-    SPObject *ochild;
-    for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
+    for ( SPObject *ochild = sp_object_first_child(object); ochild; ochild = ochild->next ) {
         if (SP_IS_STOP(ochild)) {
             gradient->has_stops = TRUE;
             break;
@@ -437,6 +428,7 @@ void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML
     sp_object_read_attr(object, "gradientTransform");
     sp_object_read_attr(object, "spreadMethod");
     sp_object_read_attr(object, "xlink:href");
+    sp_object_read_attr(object, "osb:paint");
 
     /* Register ourselves */
     sp_document_add_resource(document, "gradient", object);
@@ -537,9 +529,31 @@ void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const
                 gr->ref->detach();
             }
             break;
+        case SP_ATTR_OSB_SWATCH:
+        {
+            bool newVal = (value != 0);
+            bool modified = false;
+            if (newVal != gr->swatch) {
+                gr->swatch = newVal;
+                modified = true;
+            }
+            if (newVal) {
+                // Might need to flip solid/gradient
+                Glib::ustring paintVal = ( gr->hasStops() && (gr->getStopCount() == 0) ) ? "solid" : "gradient";
+                if ( paintVal != value ) {
+                    sp_object_setAttribute( gr, "osb:paint", paintVal.c_str(), 0 );
+                    modified = true;
+                }
+            }
+            if (modified) {
+                object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            }
+        }
+            break;
         default:
-            if (((SPObjectClass *) gradient_parent_class)->set)
+            if (((SPObjectClass *) gradient_parent_class)->set) {
                 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
+            }
             break;
     }
 }
@@ -582,12 +596,19 @@ void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, In
 
     gr->invalidateVector();
 
-    if (((SPObjectClass *) gradient_parent_class)->child_added)
+    if (((SPObjectClass *) gradient_parent_class)->child_added) {
         (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
+    }
 
     SPObject *ochild = sp_object_get_child_by_repr(object, child);
     if ( ochild && SP_IS_STOP(ochild) ) {
         gr->has_stops = TRUE;
+        if ( gr->getStopCount() > 0 ) {
+            gchar const * attr = gr->repr->attribute("osb:paint");
+            if ( attr && strcmp(attr, "gradient") ) {
+                sp_object_setAttribute( gr, "osb:paint", "gradient", 0 );
+            }
+        }
     }
 
     /// \todo Fixme: should we schedule "modified" here?
@@ -616,6 +637,13 @@ void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
         }
     }
 
+    if ( gr->getStopCount() == 0 ) {
+        gchar const * attr = gr->repr->attribute("osb:paint");
+        if ( attr && strcmp(attr, "solid") ) {
+            sp_object_setAttribute( gr, "osb:paint", "solid", 0 );
+        }
+    }
+
     /* Fixme: should we schedule "modified" here? */
     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
 }
@@ -684,15 +712,17 @@ Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Docu
 {
     SPGradient *gr = SP_GRADIENT(object);
 
-    if (((SPObjectClass *) gradient_parent_class)->write)
+    if (((SPObjectClass *) gradient_parent_class)->write) {
         (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
+    }
 
     if (flags & SP_OBJECT_WRITE_BUILD) {
         GSList *l = NULL;
         for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
-            Inkscape::XML::Node *crepr;
-            crepr = child->updateRepr(xml_doc, NULL, flags);
-            if (crepr) l = g_slist_prepend(l, crepr);
+            Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
+            if (crepr) {
+                l = g_slist_prepend(l, crepr);
+            }
         }
         while (l) {
             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
@@ -741,6 +771,16 @@ Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Docu
         }
     }
 
+    if ( (flags & SP_OBJECT_WRITE_EXT) && gr->isSwatch() ) {
+        if ( gr->isSolid() ) {
+            repr->setAttribute( "osb:paint", "solid" );
+        } else {
+            repr->setAttribute( "osb:paint", "gradient" );
+        }
+    } else {
+        repr->setAttribute( "osb:paint", 0 );
+    }
+
     return repr;
 }
 
@@ -933,9 +973,7 @@ sp_gradient_repr_write_vector(SPGradient *gr)
         sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
         /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
          * sense for offset proportions. */
-        gchar c[64];
-        sp_svg_write_color(c, sizeof(c), gr->vector.stops[i].color.toRGBA32( 0x00 ));
-        os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
+        os << "stop-color:" << gr->vector.stops[i].color.toString() << ";stop-opacity:" << gr->vector.stops[i].opacity;
         child->setAttribute("style", os.str().c_str());
         /* Order will be reversed here */
         cl = g_slist_prepend(cl, child);
@@ -1032,7 +1070,7 @@ void SPGradient::rebuildVector()
             // down to 100%."
             gstop.offset = CLAMP(gstop.offset, 0, 1);
 
-            gstop.color = sp_stop_get_color(stop);
+            gstop.color = stop->getEffectiveColor();
             gstop.opacity = stop->opacity;
 
             vector.stops.push_back(gstop);