Code

remove desktop-affine.cpp
[inkscape.git] / src / sp-gradient.cpp
index d86e3b0e3cd8c73c01e492b9ff2c055bc04476b7..5c973cf0df4ad04c76bf0cfc7111862ce2f5625f 100644 (file)
 
 #define noSP_GRADIENT_VERBOSE
 
+#include <cstring>
+#include <string>
 
-#include <libnr/nr-matrix-div.h>
 #include <libnr/nr-matrix-fns.h>
 #include <libnr/nr-matrix-ops.h>
 #include <libnr/nr-matrix-scale-ops.h>
-#include <libnr/nr-matrix-translate-ops.h>
-#include "libnr/nr-scale-translate-ops.h"
+#include <2geom/transforms.h>
 
 #include <sigc++/functors/ptr_fun.h>
 #include <sigc++/adaptors/bind.h>
@@ -55,7 +55,7 @@ static void sp_stop_init(SPStop *stop);
 
 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
 static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
-static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static SPObjectClass *stop_parent_class;
 
@@ -199,12 +199,11 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value)
  * Virtual write: write object attributes to repr.
  */
 static Inkscape::XML::Node *
-sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPStop *stop = SP_STOP(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:stop");
     }
 
@@ -212,7 +211,7 @@ sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     gfloat opacity = stop->opacity;
 
     if (((SPObjectClass *) stop_parent_class)->write)
-        (* ((SPObjectClass *) stop_parent_class)->write)(object, repr, flags);
+        (* ((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
@@ -224,7 +223,7 @@ sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
         os << "currentColor";
     } else {
         gchar c[64];
-        sp_svg_write_color(c, 64, specifiedcolor);
+        sp_svg_write_color(c, sizeof(c), specifiedcolor);
         os << c;
     }
     os << ";stop-opacity:" << opacity;
@@ -300,7 +299,7 @@ static void sp_gradient_child_added(SPObject *object,
                                     Inkscape::XML::Node *ref);
 static void sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child);
 static void sp_gradient_modified(SPObject *object, guint flags);
-static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr,
+static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
                                               guint flags);
 
 static void gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient);
@@ -376,7 +375,7 @@ sp_gradient_init(SPGradient *gr)
     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
     gr->units_set = FALSE;
 
-    gr->gradientTransform = NR::identity();
+    gr->gradientTransform = Geom::identity();
     gr->gradientTransform_set = FALSE;
 
     gr->spread = SP_GRADIENT_SPREAD_PAD;
@@ -479,12 +478,12 @@ sp_gradient_set(SPObject *object, unsigned key, gchar const *value)
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_GRADIENTTRANSFORM: {
-            NR::Matrix t;
+            Geom::Matrix t;
             if (value && sp_svg_transform_read(value, &t)) {
                 gr->gradientTransform = t;
                 gr->gradientTransform_set = TRUE;
             } else {
-                gr->gradientTransform = NR::identity();
+                gr->gradientTransform = Geom::identity();
                 gr->gradientTransform_set = FALSE;
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
@@ -539,9 +538,9 @@ gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gr)
         gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&gradient_ref_modified), gr));
     }
 
-    // Per SVG, all unset attributes must be inherited from linked gradient. 
+    // Per SVG, all unset attributes must be inherited from linked gradient.
     // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
-    // but without setting the _set flags. 
+    // but without setting the _set flags.
     // FIXME: do the same for gradientTransform too
     if (!gr->units_set)
         gr->units = sp_gradient_get_units (gr);
@@ -615,7 +614,7 @@ sp_gradient_modified(SPObject *object, guint flags)
     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
         sp_gradient_ensure_colors(gr);
     }
-    
+
     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
     flags &= SP_OBJECT_MODIFIED_CASCADE;
 
@@ -640,18 +639,18 @@ sp_gradient_modified(SPObject *object, guint flags)
  * Write gradient attributes to repr.
  */
 static Inkscape::XML::Node *
-sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_gradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPGradient *gr = SP_GRADIENT(object);
 
     if (((SPObjectClass *) gradient_parent_class)->write)
-        (* ((SPObjectClass *) gradient_parent_class)->write)(object, repr, flags);
+        (* ((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(NULL, flags);
+            crepr = child->updateRepr(xml_doc, NULL, flags);
             if (crepr) l = g_slist_prepend(l, crepr);
         }
         while (l) {
@@ -917,7 +916,7 @@ sp_gradient_repr_write_vector(SPGradient *gr)
         /* 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, 64, gr->vector.stops[i].color.toRGBA32( 0x00 ));
+        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;
         child->setAttribute("style", os.str().c_str());
         /* Order will be reversed here */
@@ -937,7 +936,7 @@ sp_gradient_repr_write_vector(SPGradient *gr)
 
 
 static void
-gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient)
+gradient_ref_modified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
 {
     if (sp_gradient_invalidate_vector(gradient)) {
         SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
@@ -1202,7 +1201,7 @@ sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
  */
 void
 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
-                                    gint const width, gint const height, gint const rowstride,
+                                    gint const width, gint const height, gint const /*rowstride*/,
                                     gint const pos, gint const span, bool const horizontal)
 {
     g_return_if_fail(gradient != NULL);
@@ -1249,40 +1248,40 @@ sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
     }
 }
 
-NR::Matrix
-sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
+Geom::Matrix
+sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
 {
     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
-        return ( NR::scale(bbox.dimensions())
-                 * NR::translate(bbox.min())
-                 * ctm );
+        return ( Geom::Scale(bbox.dimensions())
+                 * Geom::Translate(bbox.min())
+                 * Geom::Matrix(ctm) );
     } else {
         return ctm;
     }
 }
 
-NR::Matrix
-sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
+Geom::Matrix
+sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
 {
     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
         return ( gr->gradientTransform
-                 * NR::scale(bbox.dimensions())
-                 * NR::translate(bbox.min())
-                 * ctm );
+                 * Geom::Scale(bbox.dimensions())
+                 * Geom::Translate(bbox.min())
+                 * Geom::Matrix(ctm) );
     } else {
         return gr->gradientTransform * ctm;
     }
 }
 
 void
-sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
-                            NR::Rect const &bbox, NR::Matrix const &gs2d)
+sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
+                            Geom::Rect const &bbox, Geom::Matrix const &gs2d)
 {
-    gr->gradientTransform = gs2d / ctm;
+    gr->gradientTransform = gs2d * ctm.inverse();
     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
         gr->gradientTransform = ( gr->gradientTransform
-                                  / NR::translate(bbox.min())
-                                  / NR::scale(bbox.dimensions()) );
+                                  * Geom::Translate(-bbox.min())
+                                  * Geom::Scale(bbox.dimensions()).inverse() );
     }
     gr->gradientTransform_set = TRUE;
 
@@ -1310,12 +1309,12 @@ static void sp_lineargradient_build(SPObject *object,
                                     SPDocument *document,
                                     Inkscape::XML::Node *repr);
 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
-static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr,
+static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
                                                     guint flags);
 
 static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
-                                                NR::Matrix const &full_transform,
-                                                NR::Matrix const &parent_transform,
+                                                Geom::Matrix const &full_transform,
+                                                Geom::Matrix const &parent_transform,
                                                 NRRect const *bbox);
 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
 
@@ -1427,12 +1426,11 @@ sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
  * Callback: write attributes to associated repr.
  */
 static Inkscape::XML::Node *
-sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:linearGradient");
     }
 
@@ -1446,7 +1444,7 @@ sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags
         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
 
     if (((SPObjectClass *) lg_parent_class)->write)
-        (* ((SPObjectClass *) lg_parent_class)->write)(object, repr, flags);
+        (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -1469,8 +1467,8 @@ sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags
  */
 static SPPainter *
 sp_lineargradient_painter_new(SPPaintServer *ps,
-                              NR::Matrix const &full_transform,
-                              NR::Matrix const &parent_transform,
+                              Geom::Matrix const &full_transform,
+                              Geom::Matrix const &/*parent_transform*/,
                               NRRect const *bbox)
 {
     SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
@@ -1492,34 +1490,32 @@ sp_lineargradient_painter_new(SPPaintServer *ps,
      * or something similar. Originally I had 1023.9999 here - not sure
      * whether we have really to cut out ceil int (Lauris).
      */
-    NR::Matrix color2norm(NR::identity());
-    NR::Matrix color2px;
+    Geom::Matrix color2norm(Geom::identity());
+    Geom::Matrix color2px;
     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
-        NR::Matrix norm2pos(NR::identity());
+        Geom::Matrix norm2pos(Geom::identity());
 
         /* BBox to user coordinate system */
-        NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
+        Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
 
-        NR::Matrix color2pos = color2norm * norm2pos;
-        NR::Matrix color2tpos = color2pos * gr->gradientTransform;
-        NR::Matrix color2user = color2tpos * bbox2user;
+        Geom::Matrix color2pos = color2norm * norm2pos;
+        Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
+        Geom::Matrix color2user = color2tpos * bbox2user;
         color2px = color2user * full_transform;
 
     } else {
         /* Problem: What to do, if we have mixed lengths and percentages? */
         /* Currently we do ignore percentages at all, but that is not good (lauris) */
 
-        NR::Matrix norm2pos(NR::identity());
-        NR::Matrix color2pos = color2norm * norm2pos;
-        NR::Matrix color2tpos = color2pos * gr->gradientTransform;
+        Geom::Matrix norm2pos(Geom::identity());
+        Geom::Matrix color2pos = color2norm * norm2pos;
+        Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
         color2px = color2tpos * full_transform;
 
     }
-
-    NRMatrix v2px;
-    color2px.copyto(&v2px);
-
-    nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
+    // TODO: remove color2px_nr after converting to 2geom
+    NR::Matrix color2px_nr = from_2geom(color2px);
+    nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &color2px_nr,
                                 lg->x1.computed, lg->y1.computed,
                                 lg->x2.computed, lg->y2.computed);
 
@@ -1527,7 +1523,7 @@ sp_lineargradient_painter_new(SPPaintServer *ps,
 }
 
 static void
-sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter)
+sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
 {
     g_free(painter);
 }
@@ -1588,12 +1584,12 @@ static void sp_radialgradient_build(SPObject *object,
                                     SPDocument *document,
                                     Inkscape::XML::Node *repr);
 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
-static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr,
+static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
                                                     guint flags);
 
 static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
-                                                NR::Matrix const &full_transform,
-                                                NR::Matrix const &parent_transform,
+                                                Geom::Matrix const &full_transform,
+                                                Geom::Matrix const &parent_transform,
                                                 NRRect const *bbox);
 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
 
@@ -1729,12 +1725,11 @@ sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
  * Write radial gradient attributes to associated repr.
  */
 static Inkscape::XML::Node *
-sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:radialGradient");
     }
 
@@ -1745,7 +1740,7 @@ sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags
     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
 
     if (((SPObjectClass *) rg_parent_class)->write)
-        (* ((SPObjectClass *) rg_parent_class)->write)(object, repr, flags);
+        (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -1755,8 +1750,8 @@ sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags
  */
 static SPPainter *
 sp_radialgradient_painter_new(SPPaintServer *ps,
-                              NR::Matrix const &full_transform,
-                              NR::Matrix const &parent_transform,
+                              Geom::Matrix const &full_transform,
+                              Geom::Matrix const &/*parent_transform*/,
                               NRRect const *bbox)
 {
     SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
@@ -1771,7 +1766,7 @@ sp_radialgradient_painter_new(SPPaintServer *ps,
 
     rgp->rg = rg;
 
-    NR::Matrix gs2px;
+    Geom::Matrix gs2px;
 
     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
         /** \todo
@@ -1780,9 +1775,9 @@ sp_radialgradient_painter_new(SPPaintServer *ps,
          */
 
         /* BBox to user coordinate system */
-        NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
+        Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
 
-        NR::Matrix gs2user = gr->gradientTransform * bbox2user;
+        Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
 
         gs2px = gs2user * full_transform;
     } else {
@@ -1794,10 +1789,8 @@ sp_radialgradient_painter_new(SPPaintServer *ps,
 
         gs2px = gr->gradientTransform * full_transform;
     }
-
-    NRMatrix gs2px_nr;
-    gs2px.copyto(&gs2px_nr);
-
+    // TODO: remove gs2px_nr after converting to 2geom
+    NR::Matrix gs2px_nr = from_2geom(gs2px);
     nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
                                 &gs2px_nr,
                                 rg->cx.computed, rg->cy.computed,
@@ -1808,7 +1801,7 @@ sp_radialgradient_painter_new(SPPaintServer *ps,
 }
 
 static void
-sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter)
+sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
 {
     g_free(painter);
 }