Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / gradient-chemistry.cpp
index fce26435c6b75a6037147b3dda2d888c35e98f8c..4abd7483f42207c2c968d39e51e4ea5c8da17992 100644 (file)
 #include "sp-text.h"
 #include "sp-tspan.h"
 #include <libnr/nr-matrix-fns.h>
+#include <libnr/nr-point-fns.h>
+#include <libnr/nr-matrix-ops.h>
+#include <libnr/nr-rotate-ops.h>
+#include <2geom/transforms.h>
 #include "xml/repr.h"
 #include "svg/svg.h"
 #include "svg/svg-color.h"
+#include "svg/css-ostringstream.h"
+#include "preferences.h"
 
 
 // Terminology:
@@ -72,7 +78,7 @@ sp_gradient_ensure_vector_normalized(SPGradient *gr)
     /* If gr hrefs some other gradient, remove the href */
     if (gr->ref->getObject()) {
         /* We are hrefing someone, so require flattening */
-        SP_OBJECT(gr)->updateRepr(((SPObject *) gr)->repr, SP_OBJECT_WRITE_EXT | SP_OBJECT_WRITE_ALL);
+        SP_OBJECT(gr)->updateRepr(SP_OBJECT_WRITE_EXT | SP_OBJECT_WRITE_ALL);
         sp_gradient_repr_set_link(SP_OBJECT_REPR(gr), NULL);
     }
 
@@ -135,14 +141,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 +239,11 @@ 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...
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (!prefs->getBool("/options/forkgradientvectors/value", true))
+        return gr;
+
     if (SP_OBJECT_HREFCOUNT(gr) > 1) {
         SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
@@ -274,27 +285,27 @@ sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item)
 
     // calculate the bbox of the 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
+    Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
 
-    if ( !bbox || bbox->isEmpty() )
+    if (!bbox)
         return gr;
 
-    NR::Coord const width = bbox->dimensions()[NR::X];
-    NR::Coord const height = bbox->dimensions()[NR::Y];
+    Geom::Coord const width = bbox->dimensions()[Geom::X];
+    Geom::Coord const height = bbox->dimensions()[Geom::Y];
 
-    NR::Point const center = bbox->midpoint();
+    Geom::Point const center = bbox->midpoint();
 
     if (SP_IS_RADIALGRADIENT(gr)) {
-        sp_repr_set_svg_double(repr, "cx", center[NR::X]);
-        sp_repr_set_svg_double(repr, "cy", center[NR::Y]);
-        sp_repr_set_svg_double(repr, "fx", center[NR::X]);
-        sp_repr_set_svg_double(repr, "fy", center[NR::Y]);
+        sp_repr_set_svg_double(repr, "cx", center[Geom::X]);
+        sp_repr_set_svg_double(repr, "cy", center[Geom::Y]);
+        sp_repr_set_svg_double(repr, "fx", center[Geom::X]);
+        sp_repr_set_svg_double(repr, "fy", center[Geom::Y]);
         sp_repr_set_svg_double(repr, "r", width/2);
 
         // we want it to be elliptic, not circular
-        NR::Matrix squeeze = NR::Matrix (NR::translate (-center)) *
-            NR::Matrix (NR::scale(1, height/width)) *
-            NR::Matrix (NR::translate (center));
+        Geom::Matrix squeeze = Geom::Translate (-center) *
+            Geom::Scale(1, height/width) *
+            Geom::Translate (center);
 
         gr->gradientTransform = squeeze;
         {
@@ -303,10 +314,10 @@ sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item)
             g_free(c);
         }
     } else {
-        sp_repr_set_svg_double(repr, "x1", (center - NR::Point(width/2, 0))[NR::X]);
-        sp_repr_set_svg_double(repr, "y1", (center - NR::Point(width/2, 0))[NR::Y]);
-        sp_repr_set_svg_double(repr, "x2", (center + NR::Point(width/2, 0))[NR::X]);
-        sp_repr_set_svg_double(repr, "y2", (center + NR::Point(width/2, 0))[NR::Y]);
+        sp_repr_set_svg_double(repr, "x1", (center - Geom::Point(width/2, 0))[Geom::X]);
+        sp_repr_set_svg_double(repr, "y1", (center - Geom::Point(width/2, 0))[Geom::Y]);
+        sp_repr_set_svg_double(repr, "x2", (center + Geom::Point(width/2, 0))[Geom::X]);
+        sp_repr_set_svg_double(repr, "y2", (center + Geom::Point(width/2, 0))[Geom::Y]);
     }
 
     // set the gradientUnits
@@ -334,15 +345,15 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
 
         // calculate the bbox of the item
         sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
-        NR::Matrix bbox2user;
-        NR::Maybe<NR::Rect> bbox = item->getBounds(NR::identity()); // we need "true" bbox without item_i2d_affine
-        if ( bbox && !bbox->isEmpty() ) {
-            bbox2user = NR::Matrix(bbox->dimensions()[NR::X], 0,
-                                   0, bbox->dimensions()[NR::Y],
-                                   bbox->min()[NR::X], bbox->min()[NR::Y]);
+        Geom::Matrix bbox2user;
+        Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
+        if ( bbox ) {
+            bbox2user = Geom::Matrix(bbox->dimensions()[Geom::X], 0,
+                                   0, bbox->dimensions()[Geom::Y],
+                                   bbox->min()[Geom::X], bbox->min()[Geom::Y]);
         } else {
             // would be degenerate otherwise
-            bbox2user = NR::identity();
+            bbox2user = Geom::identity();
         }
 
         /* skew is the additional transform, defined by the proportions of the item, that we need
@@ -359,8 +370,8 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
          *   gradient vector in user space due to application of the non-uniform scaling
          *   transformation from bounding box space to user space.
          */
-        NR::Matrix skew = bbox2user;
-        double exp = skew.expansion();
+        Geom::Matrix skew = bbox2user;
+        double exp = skew.descrim();
         skew[0] /= exp;
         skew[1] /= exp;
         skew[2] /= exp;
@@ -378,40 +389,40 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
 
         // Matrix to convert points to userspace coords; postmultiply by inverse of skew so
         // as to cancel it out when it's applied to the gradient during rendering
-        NR::Matrix point_convert = bbox2user * skew.inverse();
+        Geom::Matrix point_convert = bbox2user * skew.inverse();
 
         if (SP_IS_RADIALGRADIENT(gr)) {
             SPRadialGradient *rg = SP_RADIALGRADIENT(gr);
 
             // original points in the bbox coords
-            NR::Point c_b = NR::Point(rg->cx.computed, rg->cy.computed);
-            NR::Point f_b = NR::Point(rg->fx.computed, rg->fy.computed);
+            Geom::Point c_b = Geom::Point(rg->cx.computed, rg->cy.computed);
+            Geom::Point f_b = Geom::Point(rg->fx.computed, rg->fy.computed);
             double r_b = rg->r.computed;
 
             // converted points in userspace coords
-            NR::Point c_u = c_b * point_convert;
-            NR::Point f_u = f_b * point_convert;
-            double r_u = r_b * point_convert.expansion();
-
-            sp_repr_set_svg_double(repr, "cx", c_u[NR::X]);
-            sp_repr_set_svg_double(repr, "cy", c_u[NR::Y]);
-            sp_repr_set_svg_double(repr, "fx", f_u[NR::X]);
-            sp_repr_set_svg_double(repr, "fy", f_u[NR::Y]);
+            Geom::Point c_u = c_b * point_convert;
+            Geom::Point f_u = f_b * point_convert;
+            double r_u = r_b * point_convert.descrim();
+
+            sp_repr_set_svg_double(repr, "cx", c_u[Geom::X]);
+            sp_repr_set_svg_double(repr, "cy", c_u[Geom::Y]);
+            sp_repr_set_svg_double(repr, "fx", f_u[Geom::X]);
+            sp_repr_set_svg_double(repr, "fy", f_u[Geom::Y]);
             sp_repr_set_svg_double(repr, "r", r_u);
 
         } else {
             SPLinearGradient *lg = SP_LINEARGRADIENT(gr);
 
-            NR::Point p1_b = NR::Point(lg->x1.computed, lg->y1.computed);
-            NR::Point p2_b = NR::Point(lg->x2.computed, lg->y2.computed);
+            Geom::Point p1_b = Geom::Point(lg->x1.computed, lg->y1.computed);
+            Geom::Point p2_b = Geom::Point(lg->x2.computed, lg->y2.computed);
 
-            NR::Point p1_u = p1_b * point_convert;
-            NR::Point p2_u = p2_b * point_convert;
+            Geom::Point p1_u = p1_b * point_convert;
+            Geom::Point p2_u = p2_b * point_convert;
 
-            sp_repr_set_svg_double(repr, "x1", p1_u[NR::X]);
-            sp_repr_set_svg_double(repr, "y1", p1_u[NR::Y]);
-            sp_repr_set_svg_double(repr, "x2", p2_u[NR::X]);
-            sp_repr_set_svg_double(repr, "y2", p2_u[NR::Y]);
+            sp_repr_set_svg_double(repr, "x1", p1_u[Geom::X]);
+            sp_repr_set_svg_double(repr, "y1", p1_u[Geom::Y]);
+            sp_repr_set_svg_double(repr, "x2", p2_u[Geom::X]);
+            sp_repr_set_svg_double(repr, "y2", p2_u[Geom::Y]);
         }
 
         // set the gradientUnits
@@ -432,7 +443,7 @@ sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *prop
 }
 
 void
-sp_gradient_transform_multiply(SPGradient *gradient, NR::Matrix postmul, bool set)
+sp_gradient_transform_multiply(SPGradient *gradient, Geom::Matrix postmul, bool set)
 {
     if (set) {
         gradient->gradientTransform = postmul;
@@ -453,14 +464,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);
@@ -502,11 +513,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*
@@ -514,24 +525,89 @@ 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);
+
+    // if this is valid but weird gradient without an offset-zero stop element,  
+    // inkscape has created a handle for the start of gradient anyway,
+    // so when it asks for stop N that corresponds to stop element N-1
+    if (stop->offset != 0)
+        stop_i --;
   
-  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)
@@ -738,27 +814,12 @@ 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.
 */
 void
-sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::Point p_w, bool fill_or_stroke, bool write_repr, bool scale)
+sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, Geom::Point p_w, bool fill_or_stroke, bool write_repr, bool scale)
 {
     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
 
@@ -767,8 +828,8 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
 
     gradient = sp_gradient_convert_to_userspace (gradient, item, fill_or_stroke? "fill" : "stroke");
 
-    NR::Matrix i2d = sp_item_i2d_affine (item);
-    NR::Point p = p_w * i2d.inverse();
+    Geom::Matrix i2d (sp_item_i2d_affine (item));
+    Geom::Point p = p_w * i2d.inverse();
     p *= (gradient->gradientTransform).inverse();
     // now p is in gradient's original coordinates
 
@@ -779,11 +840,11 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
         switch (point_type) {
             case POINT_LG_BEGIN:
                 if (scale) {
-                    lg->x2.computed += (lg->x1.computed - p[NR::X]);
-                    lg->y2.computed += (lg->y1.computed - p[NR::Y]);
+                    lg->x2.computed += (lg->x1.computed - p[Geom::X]);
+                    lg->y2.computed += (lg->y1.computed - p[Geom::Y]);
                 }
-                lg->x1.computed = p[NR::X];
-                lg->y1.computed = p[NR::Y];
+                lg->x1.computed = p[Geom::X];
+                lg->y1.computed = p[Geom::Y];
                 if (write_repr) {
                     if (scale) {
                         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
@@ -797,11 +858,11 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                 break;
             case POINT_LG_END:
                 if (scale) {
-                    lg->x1.computed += (lg->x2.computed - p[NR::X]);
-                    lg->y1.computed += (lg->y2.computed - p[NR::Y]);
+                    lg->x1.computed += (lg->x2.computed - p[Geom::X]);
+                    lg->y1.computed += (lg->y2.computed - p[Geom::Y]);
                 }
-                lg->x2.computed = p[NR::X];
-                lg->y2.computed = p[NR::Y];
+                lg->x2.computed = p[Geom::X];
+                lg->y2.computed = p[Geom::Y];
                 if (write_repr) {
                     if (scale) {
                         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
@@ -816,7 +877,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
             case POINT_LG_MID:
             {                              
                 // using X-coordinates only to determine the offset, assuming p has been snapped to the vector from begin to end.
-                double offset = get_offset_between_points (p, NR::Point(lg->x1.computed, lg->y1.computed), NR::Point(lg->x2.computed, lg->y2.computed));
+                double offset = get_offset_between_points (p, Geom::Point(lg->x1.computed, lg->y1.computed), Geom::Point(lg->x2.computed, lg->y2.computed));
                 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (lg, false);
                 sp_gradient_ensure_vector(lg);
                 lg->vector.stops.at(point_i).offset = offset;
@@ -834,21 +895,21 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                }
        } else if (SP_IS_RADIALGRADIENT(gradient)) {
         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
-        NR::Point c (rg->cx.computed, rg->cy.computed);
-        NR::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
-        if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && NR::L2 (p_w - c_w) < 1e-3) {
+        Geom::Point c (rg->cx.computed, rg->cy.computed);
+        Geom::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
+        if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && Geom::L2 (p_w - c_w) < 1e-3) {
             // prevent setting a radius too close to the center
             return;
         }
-        NR::Matrix new_transform;
+        Geom::Matrix new_transform;
         bool transform_set = false;
 
                switch (point_type) {
                case POINT_RG_CENTER:
-                       rg->fx.computed = p[NR::X] + (rg->fx.computed - rg->cx.computed);
-                       rg->fy.computed = p[NR::Y] + (rg->fy.computed - rg->cy.computed);
-                       rg->cx.computed = p[NR::X];
-                       rg->cy.computed = p[NR::Y];
+                       rg->fx.computed = p[Geom::X] + (rg->fx.computed - rg->cx.computed);
+                       rg->fy.computed = p[Geom::Y] + (rg->fy.computed - rg->cy.computed);
+                       rg->cx.computed = p[Geom::X];
+                       rg->cy.computed = p[Geom::Y];
                        if (write_repr) {
                                sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
                                sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
@@ -859,8 +920,8 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                        }
                        break;
                case POINT_RG_FOCUS:
-                       rg->fx.computed = p[NR::X];
-                       rg->fy.computed = p[NR::Y];
+                       rg->fx.computed = p[Geom::X];
+                       rg->fy.computed = p[Geom::Y];
                        if (write_repr) {
                                sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
                                sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
@@ -870,17 +931,17 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                        break;
                case POINT_RG_R1:
                        {
-                NR::Point r1_w = (c + NR::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
-                               double r1_angle = NR::atan2(r1_w - c_w);
-                               double move_angle = NR::atan2(p_w - c_w) - r1_angle;
-                               double move_stretch = NR::L2(p_w - c_w) / NR::L2(r1_w - c_w);
-
-                               NR::Matrix move = NR::Matrix (NR::translate (-c_w)) *
-                                                                                                NR::Matrix (NR::rotate(-r1_angle)) *
-                                                                                                NR::Matrix (NR::scale(move_stretch, scale? move_stretch : 1)) *
-                                                                                                NR::Matrix (NR::rotate(r1_angle)) *
-                                                                                                NR::Matrix (NR::rotate(move_angle)) *
-                                                                                                NR::Matrix (NR::translate (c_w));
+                Geom::Point r1_w = (c + Geom::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
+                               double r1_angle = Geom::atan2(r1_w - c_w);
+                               double move_angle = Geom::atan2(p_w - c_w) - r1_angle;
+                               double move_stretch = Geom::L2(p_w - c_w) / Geom::L2(r1_w - c_w);
+
+                               Geom::Matrix move = Geom::Matrix (Geom::Translate (-c_w)) *
+                                                                                                Geom::Matrix (Geom::Rotate(-r1_angle)) *
+                                                                                                Geom::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
+                                                                                                Geom::Matrix (Geom::Rotate(r1_angle)) *
+                                                                                                Geom::Matrix (Geom::Rotate(move_angle)) *
+                                                                                                Geom::Matrix (Geom::Translate (c_w));
 
                                new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
                                transform_set = true;
@@ -889,17 +950,17 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                        }
                case POINT_RG_R2:
                        {
-                               NR::Point r2_w = (c + NR::Point(0, -rg->r.computed)) * gradient->gradientTransform * i2d;
-                               double r2_angle = NR::atan2(r2_w - c_w);
-                               double move_angle = NR::atan2(p_w - c_w) - r2_angle;
-                               double move_stretch = NR::L2(p_w - c_w) / NR::L2(r2_w - c_w);
-
-                               NR::Matrix move = NR::Matrix (NR::translate (-c_w)) *
-                                                                                                NR::Matrix (NR::rotate(-r2_angle)) *
-                                                                                                NR::Matrix (NR::scale(move_stretch, scale? move_stretch : 1)) *
-                                                                                                NR::Matrix (NR::rotate(r2_angle)) *
-                                                                                                NR::Matrix (NR::rotate(move_angle)) *
-                                                                                                NR::Matrix (NR::translate (c_w));
+                               Geom::Point r2_w = (c + Geom::Point(0, -rg->r.computed)) * gradient->gradientTransform * i2d;
+                               double r2_angle = Geom::atan2(r2_w - c_w);
+                               double move_angle = Geom::atan2(p_w - c_w) - r2_angle;
+                               double move_stretch = Geom::L2(p_w - c_w) / Geom::L2(r2_w - c_w);
+
+                               Geom::Matrix move = Geom::Matrix (Geom::Translate (-c_w)) *
+                                                                                                Geom::Matrix (Geom::Rotate(-r2_angle)) *
+                                                                                                Geom::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
+                                                                                                Geom::Matrix (Geom::Rotate(r2_angle)) *
+                                                                                                Geom::Matrix (Geom::Rotate(move_angle)) *
+                                                                                                Geom::Matrix (Geom::Translate (c_w));
 
                                new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
                                transform_set = true;
@@ -908,8 +969,8 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
             }
         case POINT_RG_MID1:
             {
-                NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed);
-                 NR::Point end   = NR::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
+                Geom::Point start = Geom::Point (rg->cx.computed, rg->cy.computed);
+                 Geom::Point end   = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
                 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);
@@ -924,8 +985,8 @@ sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::
                 break;
             }
         case POINT_RG_MID2:
-                NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed);
-                NR::Point end   = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
+                Geom::Point start = Geom::Point (rg->cx.computed, rg->cy.computed);
+                Geom::Point end   = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
                 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);
@@ -979,29 +1040,29 @@ Returns the position of point point_type of the gradient applied to item (either
 in desktop coordinates.
 */
 
-NR::Point
+Geom::Point
 sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
 {
     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
 
-    NR::Point p (0, 0);
+    Geom::Point p (0, 0);
 
     if (!gradient)
-        return p;
+        return from_2geom(p);
 
     if (SP_IS_LINEARGRADIENT(gradient)) {
         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
         switch (point_type) {
             case POINT_LG_BEGIN:
-                p = NR::Point (lg->x1.computed, lg->y1.computed);
+                p = Geom::Point (lg->x1.computed, lg->y1.computed);
                 break;
             case POINT_LG_END:
-                p = NR::Point (lg->x2.computed, lg->y2.computed);
+                p = Geom::Point (lg->x2.computed, lg->y2.computed);
                 break;
             case POINT_LG_MID:
                 {   
                     gdouble offset = lg->vector.stops.at(point_i).offset;
-                    p = (1-offset) * NR::Point(lg->x1.computed, lg->y1.computed) + offset * NR::Point(lg->x2.computed, lg->y2.computed);
+                    p = (1-offset) * Geom::Point(lg->x1.computed, lg->y1.computed) + offset * Geom::Point(lg->x2.computed, lg->y2.computed);
                 }
                 break;
         }
@@ -1009,27 +1070,27 @@ sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool
         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
         switch (point_type) {
             case POINT_RG_CENTER:
-                p = NR::Point (rg->cx.computed, rg->cy.computed);
+                p = Geom::Point (rg->cx.computed, rg->cy.computed);
                 break;
             case POINT_RG_FOCUS:
-                p = NR::Point (rg->fx.computed, rg->fy.computed);
+                p = Geom::Point (rg->fx.computed, rg->fy.computed);
                 break;
             case POINT_RG_R1:
-                p = NR::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
+                p = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
                 break;
             case POINT_RG_R2:
-                p = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
+                p = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
                 break;
             case POINT_RG_MID1:
                 {
                     gdouble offset = rg->vector.stops.at(point_i).offset;
-                    p = (1-offset) * NR::Point (rg->cx.computed, rg->cy.computed) + offset * NR::Point(rg->cx.computed + rg->r.computed, rg->cy.computed);
+                    p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed + rg->r.computed, rg->cy.computed);
                 }        
                 break;
             case POINT_RG_MID2:
                 {
                     gdouble offset = rg->vector.stops.at(point_i).offset;
-                    p = (1-offset) * NR::Point (rg->cx.computed, rg->cy.computed) + offset * NR::Point(rg->cx.computed, rg->cy.computed - rg->r.computed);
+                    p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed, rg->cy.computed - rg->r.computed);
                 }        
                 break;
         }
@@ -1037,15 +1098,15 @@ sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool
 
     if (SP_GRADIENT(gradient)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
         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
+        Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
         if (bbox) {
-            p *= NR::Matrix(bbox->dimensions()[NR::X], 0,
-                            0, bbox->dimensions()[NR::Y],
-                            bbox->min()[NR::X], bbox->min()[NR::Y]);
+            p *= Geom::Matrix(bbox->dimensions()[Geom::X], 0,
+                            0, bbox->dimensions()[Geom::Y],
+                            bbox->min()[Geom::X], bbox->min()[Geom::Y]);
         }
     }
-    p *= NR::Matrix(gradient->gradientTransform) * sp_item_i2d_affine(item);
-    return p;
+    p *= Geom::Matrix(gradient->gradientTransform) * (Geom::Matrix)sp_item_i2d_affine(item);
+    return from_2geom(p);
 }
 
 
@@ -1067,9 +1128,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
@@ -1166,7 +1226,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);
@@ -1223,15 +1283,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);