X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fsp-gradient.cpp;h=84a0a98704315228b655e8b80268a47726069a72;hb=c596be6d145df956e779f427dde5aabc8559dd6c;hp=8fad6074abec95235be7761e9e09915f9352e6bb;hpb=e9ddb5915978ddd65d9a7a96bfada29cf5213b3a;p=inkscape.git diff --git a/src/sp-gradient.cpp b/src/sp-gradient.cpp index 8fad6074a..84a0a9870 100644 --- a/src/sp-gradient.cpp +++ b/src/sp-gradient.cpp @@ -7,10 +7,12 @@ * Authors: * Lauris Kaplinski * bulia byak + * Jasper van de Gronde * * Copyright (C) 1999-2002 Lauris Kaplinski * Copyright (C) 2000-2001 Ximian, Inc. * Copyright (C) 2004 David Turner + * Copyright (C) 2009 Jasper van de Gronde * * Released under GNU GPL, read the file 'COPYING' for more information * @@ -18,18 +20,19 @@ #define noSP_GRADIENT_VERBOSE +#include +#include -#include #include #include #include -#include -#include "libnr/nr-scale-translate-ops.h" +#include <2geom/transforms.h> #include #include #include "libnr/nr-gradient.h" +#include "libnr/nr-pixops.h" #include "svg/svg.h" #include "svg/svg-color.h" #include "svg/css-ostringstream.h" @@ -55,7 +58,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; @@ -104,7 +107,7 @@ sp_stop_init(SPStop *stop) { stop->offset = 0.0; stop->currentColor = false; - sp_color_set_rgb_rgba32(&stop->specified_color, 0x000000ff); + stop->specified_color.set( 0x000000ff ); stop->opacity = 1.0; } @@ -148,7 +151,7 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value) stop->currentColor = true; } else { guint32 const color = sp_svg_read_color(p, 0); - sp_color_set_rgb_rgba32(&stop->specified_color, color); + stop->specified_color.set( color ); } } { @@ -167,7 +170,7 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value) } else { stop->currentColor = false; guint32 const color = sp_svg_read_color(p, 0); - sp_color_set_rgb_rgba32(&stop->specified_color, color); + stop->specified_color.set( color ); } } object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); @@ -199,19 +202,19 @@ 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"); } - guint32 specifiedcolor = sp_color_get_rgba32_ualpha(&stop->specified_color, 255); + guint32 specifiedcolor = stop->specified_color.toRGBA32( 255 ); + 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 @@ -223,10 +226,10 @@ 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:" << stop->opacity; + os << ";stop-opacity:" << opacity; repr->setAttribute("style", os.str().c_str()); repr->setAttribute("stop-color", NULL); repr->setAttribute("stop-opacity", NULL); @@ -257,7 +260,7 @@ sp_stop_get_rgba32(SPStop const *const stop) rgb0 | 0xff); return rgb0 | alpha; } else { - return sp_color_get_rgba32_falpha(&stop->specified_color, stop->opacity); + return stop->specified_color.toRGBA32( stop->opacity ); } } @@ -277,8 +280,7 @@ sp_stop_get_color(SPStop const *const stop) if (str) { color = sp_svg_read_color(str, dfl); } - SPColor ret; - sp_color_set_rgb_rgba32(&ret, color); + SPColor ret( color ); return ret; } else { return stop->specified_color; @@ -300,7 +302,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 +378,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 +481,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 +541,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 +617,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 +642,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 +919,7 @@ sp_gradient_repr_write_vector(SPGradient *gr) /* strictly speaking, offset an SVG rather than a CSS one, but exponents make no * sense for offset proportions. */ gchar c[64]; - sp_svg_write_color(c, 64, sp_color_get_rgba32_ualpha(&gr->vector.stops[i].color, 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 +939,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); @@ -1033,14 +1035,14 @@ sp_gradient_rebuild_vector(SPGradient *gr) { SPGradientStop gstop; gstop.offset = 0.0; - sp_color_set_rgb_rgba32(&gstop.color, 0x00000000); + gstop.color.set( 0x00000000 ); gstop.opacity = 0.0; gr->vector.stops.push_back(gstop); } { SPGradientStop gstop; gstop.offset = 1.0; - sp_color_set_rgb_rgba32(&gstop.color, 0x00000000); + gstop.color.set( 0x00000000 ); gstop.opacity = 0.0; gr->vector.stops.push_back(gstop); } @@ -1052,7 +1054,7 @@ sp_gradient_rebuild_vector(SPGradient *gr) // If the first one is not at 0, then insert a copy of the first at 0. SPGradientStop gstop; gstop.offset = 0.0; - sp_color_copy(&gstop.color, &gr->vector.stops.front().color); + gstop.color = gr->vector.stops.front().color; gstop.opacity = gr->vector.stops.front().opacity; gr->vector.stops.insert(gr->vector.stops.begin(), gstop); } @@ -1060,7 +1062,7 @@ sp_gradient_rebuild_vector(SPGradient *gr) // If the last one is not at 1, then insert a copy of the last at 1. SPGradientStop gstop; gstop.offset = 1.0; - sp_color_copy(&gstop.color, &gr->vector.stops.back().color); + gstop.color = gr->vector.stops.back().color; gstop.opacity = gr->vector.stops.back().opacity; gr->vector.stops.push_back(gstop); } @@ -1085,42 +1087,133 @@ sp_gradient_ensure_colors(SPGradient *gr) gr->color = g_new(guchar, 4 * NCOLORS); } - for (guint i = 0; i < gr->vector.stops.size() - 1; i++) { - guint32 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i].color, - gr->vector.stops[i].opacity); - gint r0 = (color >> 24) & 0xff; - gint g0 = (color >> 16) & 0xff; - gint b0 = (color >> 8) & 0xff; - gint a0 = color & 0xff; - color = sp_color_get_rgba32_falpha(&gr->vector.stops[i + 1].color, - gr->vector.stops[i + 1].opacity); - gint r1 = (color >> 24) & 0xff; - gint g1 = (color >> 16) & 0xff; - gint b1 = (color >> 8) & 0xff; - gint a1 = color & 0xff; - gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001)); - gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001)); - if (o1 > o0) { - gint dr = ((r1 - r0) << 16) / (o1 - o0); - gint dg = ((g1 - g0) << 16) / (o1 - o0); - gint db = ((b1 - b0) << 16) / (o1 - o0); - gint da = ((a1 - a0) << 16) / (o1 - o0); - gint r = r0 << 16; - gint g = g0 << 16; - gint b = b0 << 16; - gint a = a0 << 16; - for (int j = o0; j < o1 + 1; j++) { - gr->color[4 * j] = r >> 16; - gr->color[4 * j + 1] = g >> 16; - gr->color[4 * j + 2] = b >> 16; - gr->color[4 * j + 3] = a >> 16; - r += dr; - g += dg; - b += db; - a += da; + // This assumes that gr->vector is a zero-order B-spline (box function) approximation of the "true" gradient. + // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled. + // Furthermore, the first element corresponds to offset="0" and the last element to offset="1". + + double remainder[4] = {0,0,0,0}; + double remainder_for_end[4] = {0,0,0,0}; // Used at the end + switch(gr->spread) { + case SP_GRADIENT_SPREAD_PAD: + remainder[0] = 0.5*gr->vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop + remainder[1] = 0.5*gr->vector.stops[0].color.v.c[1]; + remainder[2] = 0.5*gr->vector.stops[0].color.v.c[2]; + remainder[3] = 0.5*gr->vector.stops[0].opacity; + remainder_for_end[0] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[0]; // Half of the first cell uses the color of the last stop + remainder_for_end[1] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[1]; + remainder_for_end[2] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[2]; + remainder_for_end[3] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].opacity; + break; + case SP_GRADIENT_SPREAD_REFLECT: + case SP_GRADIENT_SPREAD_REPEAT: + // These two are handled differently, see below. + break; + default: + g_error("Spread type not supported!"); + }; + for (unsigned int i = 0; i < gr->vector.stops.size() - 1; i++) { + double r0 = gr->vector.stops[i].color.v.c[0]; + double g0 = gr->vector.stops[i].color.v.c[1]; + double b0 = gr->vector.stops[i].color.v.c[2]; + double a0 = gr->vector.stops[i].opacity; + double r1 = gr->vector.stops[i+1].color.v.c[0]; + double g1 = gr->vector.stops[i+1].color.v.c[1]; + double b1 = gr->vector.stops[i+1].color.v.c[2]; + double a1 = gr->vector.stops[i+1].opacity; + double o0 = gr->vector.stops[i].offset * (NCOLORS-1); + double o1 = gr->vector.stops[i + 1].offset * (NCOLORS-1); + unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval. + unsigned int oe = (unsigned int) floor(o1+.5); // These need to be computed the same to ensure that ob will be covered by the next interval if oe==ob + + if (oe == ob) { + // Simple case, this interval starts and stops within one cell + // The contribution of this interval is: + // (o1-o0)*(c(o0)+c(o1))/2 + // = (o1-o0)*(c0+c1)/2 + double dt = 0.5*(o1-o0); + remainder[0] += dt*(r0 + r1); + remainder[1] += dt*(g0 + g1); + remainder[2] += dt*(b0 + b1); + remainder[3] += dt*(a0 + a1); + } else { + // First compute colors for the cells which are fully covered by the current interval. + // The prefiltered values are equal to the midpoint of each cell here. + // f = (j-o0)/(o1-o0) + // = j*(1/(o1-o0)) - o0/(o1-o0) + double f = (ob-o0) / (o1-o0); + double df = 1. / (o1-o0); + for (unsigned int j = ob+1; j < oe; j++) { + f += df; + gr->color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5); + gr->color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5); + gr->color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5); + gr->color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5); } + + // Now handle the beginning + // The contribution of the last point is already in remainder. + // The contribution of this point is: + // (ob+.5-o0)*(c(o0)+c(ob+.5))/2 + // = (ob+.5-o0)*c((o0+ob+.5)/2) + // = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0)) + // = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2) + double dt = ob+.5-o0; + f = 0.5*dt*df; + if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REFLECT) { + // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2. + gr->color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5); + gr->color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5); + gr->color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5); + gr->color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5); + } else if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REPEAT) { + // The first cell is the same as the last cell, so save whatever is in the second half here and deal with the rest later. + remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0)); + remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0)); + remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0)); + remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0)); + } else { + // The first half of the cell was already in remainder. + gr->color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5); + gr->color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5); + gr->color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5); + gr->color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5); + } + + // Now handle the end, which should end up in remainder + // The contribution of this point is: + // (o1-oe+.5)*(c(o1)+c(oe-.5))/2 + // = (o1-oe+.5)*c((o1+oe-.5)/2) + // = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0)) + dt = o1-oe+.5; + f = (0.5*(o1+oe-.5)-o0)*df; + remainder[0] = dt*(r0 + f*(r1-r0)); + remainder[1] = dt*(g0 + f*(g1-g0)); + remainder[2] = dt*(b0 + f*(b1-b0)); + remainder[3] = dt*(a0 + f*(a1-a0)); } } + switch(gr->spread) { + case SP_GRADIENT_SPREAD_PAD: + gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5); + gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5); + gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5); + gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5); + break; + case SP_GRADIENT_SPREAD_REFLECT: + // The second half is the same as the first half, so multiply by 2. + gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5); + gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5); + gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5); + gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5); + break; + case SP_GRADIENT_SPREAD_REPEAT: + // The second half is the same as the second half of the first cell (which was saved in remainder_for_end). + gr->color[0] = gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5); + gr->color[1] = gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5); + gr->color[2] = gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5); + gr->color[3] = gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5); + break; + } } /** @@ -1204,7 +1297,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); @@ -1251,40 +1344,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; @@ -1312,12 +1405,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); @@ -1372,9 +1465,9 @@ static void sp_lineargradient_class_init(SPLinearGradientClass *klass) static void sp_lineargradient_init(SPLinearGradient *lg) { lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0); - lg->y1.unset(SVGLength::PERCENT, 0.5, 0.5); + lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0); lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0); - lg->y2.unset(SVGLength::PERCENT, 0.5, 0.5); + lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0); } /** @@ -1407,7 +1500,7 @@ sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value) object->requestModified(SP_OBJECT_MODIFIED_FLAG); break; case SP_ATTR_Y1: - lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5); + lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0); object->requestModified(SP_OBJECT_MODIFIED_FLAG); break; case SP_ATTR_X2: @@ -1415,7 +1508,7 @@ sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value) object->requestModified(SP_OBJECT_MODIFIED_FLAG); break; case SP_ATTR_Y2: - lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5); + lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0); object->requestModified(SP_OBJECT_MODIFIED_FLAG); break; default: @@ -1429,12 +1522,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"); } @@ -1448,7 +1540,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; } @@ -1471,8 +1563,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); @@ -1494,34 +1586,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); @@ -1529,7 +1619,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); } @@ -1590,12 +1680,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); @@ -1731,12 +1821,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"); } @@ -1747,7 +1836,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; } @@ -1757,8 +1846,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); @@ -1773,7 +1862,7 @@ sp_radialgradient_painter_new(SPPaintServer *ps, rgp->rg = rg; - NR::Matrix gs2px; + Geom::Matrix gs2px; if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) { /** \todo @@ -1782,9 +1871,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 { @@ -1796,10 +1885,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, @@ -1810,7 +1897,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); }