Code

Added 'paraller axis' rendering hint to filter primitives, that need it.
[inkscape.git] / src / gradient-chemistry.cpp
index 281704eea573078c47652f5d2b3c8c3b57192ef9..70bc835bc6420f993b45fdf93b28626361f24495 100644 (file)
 #include "sp-text.h"
 #include "sp-tspan.h"
 #include <libnr/nr-matrix-fns.h>
+#include <libnr/nr-point-fns.h>
 #include "xml/repr.h"
 #include "svg/svg.h"
 #include "svg/svg-color.h"
+#include "svg/css-ostringstream.h"
+#include "prefs-utils.h"
 
 
 // Terminology:
@@ -135,14 +138,14 @@ count_gradient_hrefs(SPObject *o, SPGradient *gr)
 
     SPStyle *style = SP_OBJECT_STYLE(o);
     if (style
-        && style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+        && style->fill.isPaintserver()
         && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style))
         && SP_GRADIENT(SP_STYLE_FILL_SERVER(style)) == gr)
     {
         i ++;
     }
     if (style
-        && style->stroke.type == SP_PAINT_TYPE_PAINTSERVER
+        && style->stroke.isPaintserver()
         && SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style))
         && SP_GRADIENT(SP_STYLE_STROKE_SERVER(style)) == gr)
     {
@@ -233,6 +236,10 @@ sp_gradient_fork_private_if_necessary(SPGradient *gr, SPGradient *vector,
 SPGradient *
 sp_gradient_fork_vector_if_necessary (SPGradient *gr)
 {
+    // Some people actually prefer their gradient vectors to be shared...
+    if (prefs_get_int_attribute("options.forkgradientvectors", "value", 1) == 0)
+        return gr;
+
     if (SP_OBJECT_HREFCOUNT(gr) > 1) {
         SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
@@ -276,7 +283,9 @@ sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item)
     sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
     NR::Maybe<NR::Rect> bbox = item->getBounds(NR::identity()); // we need "true" bbox without item_i2d_affine
 
-    g_assert( bbox && !bbox->isEmpty() );
+    if ( !bbox || bbox->isEmpty() )
+        return gr;
+
     NR::Coord const width = bbox->dimensions()[NR::X];
     NR::Coord const height = bbox->dimensions()[NR::Y];
 
@@ -451,14 +460,14 @@ sp_item_gradient (SPItem *item, bool fill_or_stroke)
     SPGradient *gradient = NULL;
 
     if (fill_or_stroke) {
-        if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
+        if (style && (style->fill.isPaintserver())) {
             SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
             if (SP_IS_GRADIENT (server)) {
                 gradient = SP_GRADIENT (server);
             }
         }
     } else {
-        if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
+        if (style && (style->stroke.isPaintserver())) {
             SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
             if (SP_IS_GRADIENT (server)) {
                 gradient = SP_GRADIENT (server);
@@ -500,11 +509,11 @@ sp_prev_stop(SPStop *stop, SPGradient *gradient)
 SPStop*
 sp_next_stop(SPStop *stop)
 {
-  for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
-       if (SP_IS_STOP (ochild))
-               return SP_STOP(ochild);
-  }
-  return NULL;
+    for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
+        if (SP_IS_STOP (ochild))
+            return SP_STOP(ochild);
+    }
+    return NULL;
 }
 
 SPStop*
@@ -512,24 +521,83 @@ sp_last_stop(SPGradient *gradient)
 {
     for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
         if (sp_next_stop (stop) == NULL)
-               return stop;
-  }
-  return NULL;
+            return stop;
+    }
+    return NULL;
+} 
+
+guint
+sp_number_of_stops(SPGradient *gradient)
+{
+    guint n = 0;
+    for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
+        if (sp_next_stop (stop) == NULL)
+            return n;
+        n ++;
+    }
+    return n;
 } 
 
+guint
+sp_number_of_stops_before_stop(SPGradient *gradient, SPStop *target)
+{
+    guint n = 0;
+    for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
+        if (stop == target)
+            return n;
+        n ++;
+    }
+    return n;
+} 
+
+
 SPStop*
 sp_get_stop_i(SPGradient *gradient, guint stop_i)
 {            
-  SPStop *stop = sp_first_stop (gradient);
+    SPStop *stop = sp_first_stop (gradient);
   
-  for (guint i=0; i < stop_i; i++) {
-    if (!stop) return NULL;  
-    stop = sp_next_stop (stop);    
-  }  
+    for (guint i=0; i < stop_i; i++) {
+        if (!stop) return NULL;  
+        stop = sp_next_stop (stop);    
+    }  
     
-  return stop;
+    return stop;
+}
+
+guint32
+average_color (guint32 c1, guint32 c2, gdouble p)
+{
+       guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
+       guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
+       guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
+       guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
+
+       return SP_RGBA32_U_COMPOSE (r, g, b, a);
 }
 
+SPStop *
+sp_vector_add_stop (SPGradient *vector, SPStop* prev_stop, SPStop* next_stop, gfloat offset)
+{
+    Inkscape::XML::Node *new_stop_repr = NULL;
+    new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
+    SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
+
+    SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
+    newstop->offset = offset;
+    sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
+    guint32 const c1 = sp_stop_get_rgba32(prev_stop);
+    guint32 const c2 = sp_stop_get_rgba32(next_stop);
+    guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
+    Inkscape::CSSOStringStream os;
+    gchar c[64];
+    sp_svg_write_color (c, sizeof(c), cnew);
+    gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
+    os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
+    SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
+    Inkscape::GC::release(new_stop_repr);
+
+    return newstop;
+}
 
 void
 sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
@@ -736,21 +804,6 @@ sp_item_gradient_reverse_vector (SPItem *item, bool fill_or_stroke)
 }
 
 
-// FIXME: make general global function
-static double
-get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
-{
-    double length = NR::L2(end - begin);
-    NR::Point be = (end - begin) / length;
-    double r = NR::dot(p - begin, be);
-        
-    if (r < 0.0) return 0.0;
-    if (r > length) return 1.0;    
-    
-    return (r / length);
-}
-
-
 /**
 Set the position of point point_type of the gradient applied to item (either fill_or_stroke) to
 p_w (in desktop coordinates). Write_repr if you want the change to become permanent.
@@ -911,7 +964,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                 double offset = get_offset_between_points (p, start, end);
                 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (rg, false);
                 sp_gradient_ensure_vector(rg);
-                rg->vector.stops.at(point_i).offset = offset; //crash
+                rg->vector.stops.at(point_i).offset = offset; 
                 SPStop* stopi = sp_get_stop_i(vector, point_i);
                 stopi->offset = offset;
                 if (write_repr) {
@@ -1065,9 +1118,8 @@ sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_
     SPStyle *style = SP_OBJECT_STYLE(item);
     g_assert(style != NULL);
 
-    guint style_type = is_fill? style->fill.type : style->stroke.type;
     SPPaintServer *ps = NULL;
-    if (style_type == SP_PAINT_TYPE_PAINTSERVER)
+    if (is_fill? style->fill.isPaintserver() : style->stroke.isPaintserver())
         ps = is_fill? SP_STYLE_FILL_SERVER(style) : SP_STYLE_STROKE_SERVER(style);
 
     if (ps
@@ -1164,7 +1216,7 @@ sp_document_default_gradient_vector(SPDocument *document, guint32 color)
     Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
 
     gchar b[64];
-    sp_svg_write_color(b, 64, color);
+    sp_svg_write_color(b, sizeof(b), color);
 
     {
         gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:1;", b);
@@ -1221,15 +1273,15 @@ sp_gradient_vector_for_object(SPDocument *const doc, SPDesktop *const desktop,
         SPIPaint const &paint = ( is_fill
                                   ? style.fill
                                   : style.stroke );
-        if (paint.type == SP_PAINT_TYPE_COLOR) {
-            rgba = sp_color_get_rgba32_ualpha(&paint.value.color, 0xff);
-        } else if (paint.type == SP_PAINT_TYPE_PAINTSERVER) {
+        if (paint.isPaintserver()) {
             SPObject *server = is_fill? SP_OBJECT_STYLE_FILL_SERVER(o) : SP_OBJECT_STYLE_STROKE_SERVER(o);
             if (SP_IS_GRADIENT (server)) {
                 return sp_gradient_get_vector(SP_GRADIENT (server), TRUE);
             } else {
                 rgba = sp_desktop_get_color(desktop, is_fill);
             }
+        } else if (paint.isColor()) {
+            rgba = paint.value.color.toRGBA32( 0xff );
         } else {
             // if o doesn't use flat color, then take current color of the desktop.
             rgba = sp_desktop_get_color(desktop, is_fill);