~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to src/sp-gradient.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook, Ted Gould, Kees Cook
  • Date: 2009-06-24 14:00:43 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090624140043-07stp20mry48hqup
Tags: 0.47~pre0-0ubuntu1
* New upstream release

[ Ted Gould ]
* debian/control: Adding libgsl0 and removing version specifics on boost

[ Kees Cook ]
* debian/watch: updated to run uupdate and mangle pre-release versions.
* Dropped patches that have been taken upstream:
  - 01_mips
  - 02-poppler-0.8.3
  - 03-chinese-inkscape
  - 05_fix_latex_patch
  - 06_gcc-4.4
  - 07_cdr2svg
  - 08_skip-bad-utf-on-pdf-import
  - 09_gtk-clist
  - 10_belarussian
  - 11_libpng
  - 12_desktop
  - 13_slider
  - 100_svg_import_improvements
  - 102_sp_pattern_painter_free
  - 103_bitmap_type_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 * Authors:
8
8
 *   Lauris Kaplinski <lauris@kaplinski.com>
9
9
 *   bulia byak <buliabyak@users.sf.net>
 
10
 *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
10
11
 *
11
12
 * Copyright (C) 1999-2002 Lauris Kaplinski
12
13
 * Copyright (C) 2000-2001 Ximian, Inc.
13
14
 * Copyright (C) 2004 David Turner
 
15
 * Copyright (C) 2009 Jasper van de Gronde
14
16
 *
15
17
 * Released under GNU GPL, read the file 'COPYING' for more information
16
18
 *
21
23
#include <cstring>
22
24
#include <string>
23
25
 
24
 
#include <libnr/nr-matrix-div.h>
25
26
#include <libnr/nr-matrix-fns.h>
26
27
#include <libnr/nr-matrix-ops.h>
27
28
#include <libnr/nr-matrix-scale-ops.h>
28
 
#include <libnr/nr-matrix-translate-ops.h>
29
 
#include "libnr/nr-scale-translate-ops.h"
 
29
#include <2geom/transforms.h>
30
30
 
31
31
#include <sigc++/functors/ptr_fun.h>
32
32
#include <sigc++/adaptors/bind.h>
33
33
 
34
34
#include "libnr/nr-gradient.h"
 
35
#include "libnr/nr-pixops.h"
35
36
#include "svg/svg.h"
36
37
#include "svg/svg-color.h"
37
38
#include "svg/css-ostringstream.h"
57
58
 
58
59
static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
59
60
static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
60
 
static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
 
61
static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
61
62
 
62
63
static SPObjectClass *stop_parent_class;
63
64
 
201
202
 * Virtual write: write object attributes to repr.
202
203
 */
203
204
static Inkscape::XML::Node *
204
 
sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
 
205
sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
205
206
{
206
207
    SPStop *stop = SP_STOP(object);
207
208
 
208
209
    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
209
 
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
210
210
        repr = xml_doc->createElement("svg:stop");
211
211
    }
212
212
 
214
214
    gfloat opacity = stop->opacity;
215
215
 
216
216
    if (((SPObjectClass *) stop_parent_class)->write)
217
 
        (* ((SPObjectClass *) stop_parent_class)->write)(object, repr, flags);
 
217
        (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
218
218
 
219
219
    // Since we do a hackish style setting here (because SPStyle does not support stop-color and
220
220
    // stop-opacity), we must do it AFTER calling the parent write method; otherwise
302
302
                                    Inkscape::XML::Node *ref);
303
303
static void sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child);
304
304
static void sp_gradient_modified(SPObject *object, guint flags);
305
 
static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr,
 
305
static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
306
306
                                              guint flags);
307
307
 
308
308
static void gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient);
378
378
    gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
379
379
    gr->units_set = FALSE;
380
380
 
381
 
    gr->gradientTransform = NR::identity();
 
381
    gr->gradientTransform = Geom::identity();
382
382
    gr->gradientTransform_set = FALSE;
383
383
 
384
384
    gr->spread = SP_GRADIENT_SPREAD_PAD;
481
481
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
482
482
            break;
483
483
        case SP_ATTR_GRADIENTTRANSFORM: {
484
 
            NR::Matrix t;
 
484
            Geom::Matrix t;
485
485
            if (value && sp_svg_transform_read(value, &t)) {
486
486
                gr->gradientTransform = t;
487
487
                gr->gradientTransform_set = TRUE;
488
488
            } else {
489
 
                gr->gradientTransform = NR::identity();
 
489
                gr->gradientTransform = Geom::identity();
490
490
                gr->gradientTransform_set = FALSE;
491
491
            }
492
492
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
642
642
 * Write gradient attributes to repr.
643
643
 */
644
644
static Inkscape::XML::Node *
645
 
sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
 
645
sp_gradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
646
646
{
647
647
    SPGradient *gr = SP_GRADIENT(object);
648
648
 
649
649
    if (((SPObjectClass *) gradient_parent_class)->write)
650
 
        (* ((SPObjectClass *) gradient_parent_class)->write)(object, repr, flags);
 
650
        (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
651
651
 
652
652
    if (flags & SP_OBJECT_WRITE_BUILD) {
653
653
        GSList *l = NULL;
654
654
        for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
655
655
            Inkscape::XML::Node *crepr;
656
 
            crepr = child->updateRepr(NULL, flags);
 
656
            crepr = child->updateRepr(xml_doc, NULL, flags);
657
657
            if (crepr) l = g_slist_prepend(l, crepr);
658
658
        }
659
659
        while (l) {
1087
1087
        gr->color = g_new(guchar, 4 * NCOLORS);
1088
1088
    }
1089
1089
 
1090
 
    for (guint i = 0; i < gr->vector.stops.size() - 1; i++) {
1091
 
        guint32 color = gr->vector.stops[i].color.toRGBA32( gr->vector.stops[i].opacity );
1092
 
        gint r0 = (color >> 24) & 0xff;
1093
 
        gint g0 = (color >> 16) & 0xff;
1094
 
        gint b0 = (color >> 8) & 0xff;
1095
 
        gint a0 = color & 0xff;
1096
 
        color = gr->vector.stops[i + 1].color.toRGBA32( gr->vector.stops[i + 1].opacity );
1097
 
        gint r1 = (color >> 24) & 0xff;
1098
 
        gint g1 = (color >> 16) & 0xff;
1099
 
        gint b1 = (color >> 8) & 0xff;
1100
 
        gint a1 = color & 0xff;
1101
 
        gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001));
1102
 
        gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001));
1103
 
        if (o1 > o0) {
1104
 
            gint dr = ((r1 - r0) << 16) / (o1 - o0);
1105
 
            gint dg = ((g1 - g0) << 16) / (o1 - o0);
1106
 
            gint db = ((b1 - b0) << 16) / (o1 - o0);
1107
 
            gint da = ((a1 - a0) << 16) / (o1 - o0);
1108
 
            gint r = r0 << 16;
1109
 
            gint g = g0 << 16;
1110
 
            gint b = b0 << 16;
1111
 
            gint a = a0 << 16;
1112
 
            for (int j = o0; j < o1 + 1; j++) {
1113
 
                gr->color[4 * j] = r >> 16;
1114
 
                gr->color[4 * j + 1] = g >> 16;
1115
 
                gr->color[4 * j + 2] = b >> 16;
1116
 
                gr->color[4 * j + 3] = a >> 16;
1117
 
                r += dr;
1118
 
                g += dg;
1119
 
                b += db;
1120
 
                a += da;
1121
 
            }
 
1090
    // This assumes that gr->vector is a zero-order B-spline (box function) approximation of the "true" gradient.
 
1091
    // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
 
1092
    // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
 
1093
 
 
1094
    double remainder[4] = {0,0,0,0};
 
1095
    double remainder_for_end[4] = {0,0,0,0}; // Used at the end
 
1096
    switch(gr->spread) {
 
1097
    case SP_GRADIENT_SPREAD_PAD:
 
1098
        remainder[0] = 0.5*gr->vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
 
1099
        remainder[1] = 0.5*gr->vector.stops[0].color.v.c[1];
 
1100
        remainder[2] = 0.5*gr->vector.stops[0].color.v.c[2];
 
1101
        remainder[3] = 0.5*gr->vector.stops[0].opacity;
 
1102
        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
 
1103
        remainder_for_end[1] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[1];
 
1104
        remainder_for_end[2] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[2];
 
1105
        remainder_for_end[3] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].opacity;
 
1106
        break;
 
1107
    case SP_GRADIENT_SPREAD_REFLECT:
 
1108
    case SP_GRADIENT_SPREAD_REPEAT:
 
1109
        // These two are handled differently, see below.
 
1110
        break;
 
1111
    default:
 
1112
        g_error("Spread type not supported!");
 
1113
    };
 
1114
    for (unsigned int i = 0; i < gr->vector.stops.size() - 1; i++) {
 
1115
        double r0 = gr->vector.stops[i].color.v.c[0];
 
1116
        double g0 = gr->vector.stops[i].color.v.c[1];
 
1117
        double b0 = gr->vector.stops[i].color.v.c[2];
 
1118
        double a0 = gr->vector.stops[i].opacity;
 
1119
        double r1 = gr->vector.stops[i+1].color.v.c[0];
 
1120
        double g1 = gr->vector.stops[i+1].color.v.c[1];
 
1121
        double b1 = gr->vector.stops[i+1].color.v.c[2];
 
1122
        double a1 = gr->vector.stops[i+1].opacity;
 
1123
        double o0 = gr->vector.stops[i].offset * (NCOLORS-1);
 
1124
        double o1 = gr->vector.stops[i + 1].offset * (NCOLORS-1);
 
1125
        unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
 
1126
        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
 
1127
 
 
1128
        if (oe == ob) {
 
1129
            // Simple case, this interval starts and stops within one cell
 
1130
            // The contribution of this interval is:
 
1131
            //    (o1-o0)*(c(o0)+c(o1))/2
 
1132
            //  = (o1-o0)*(c0+c1)/2
 
1133
            double dt = 0.5*(o1-o0);
 
1134
            remainder[0] += dt*(r0 + r1);
 
1135
            remainder[1] += dt*(g0 + g1);
 
1136
            remainder[2] += dt*(b0 + b1);
 
1137
            remainder[3] += dt*(a0 + a1);
 
1138
        } else {
 
1139
            // First compute colors for the cells which are fully covered by the current interval.
 
1140
            // The prefiltered values are equal to the midpoint of each cell here.
 
1141
            //  f = (j-o0)/(o1-o0)
 
1142
            //    = j*(1/(o1-o0)) - o0/(o1-o0)
 
1143
            double f = (ob-o0) / (o1-o0);
 
1144
            double df = 1. / (o1-o0);
 
1145
            for (unsigned int j = ob+1; j < oe; j++) {
 
1146
                f += df;
 
1147
                gr->color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
 
1148
                gr->color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
 
1149
                gr->color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
 
1150
                gr->color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
 
1151
            }
 
1152
 
 
1153
            // Now handle the beginning
 
1154
            // The contribution of the last point is already in remainder.
 
1155
            // The contribution of this point is:
 
1156
            //    (ob+.5-o0)*(c(o0)+c(ob+.5))/2
 
1157
            //  = (ob+.5-o0)*c((o0+ob+.5)/2)
 
1158
            //  = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
 
1159
            //  = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
 
1160
            double dt = ob+.5-o0;
 
1161
            f = 0.5*dt*df;
 
1162
            if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REFLECT) {
 
1163
                // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
 
1164
                gr->color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
 
1165
                gr->color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
 
1166
                gr->color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
 
1167
                gr->color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
 
1168
            } else if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REPEAT) {
 
1169
                // 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.
 
1170
                remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
 
1171
                remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
 
1172
                remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
 
1173
                remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
 
1174
            } else {
 
1175
                // The first half of the cell was already in remainder.
 
1176
                gr->color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
 
1177
                gr->color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
 
1178
                gr->color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
 
1179
                gr->color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
 
1180
            }
 
1181
 
 
1182
            // Now handle the end, which should end up in remainder
 
1183
            // The contribution of this point is:
 
1184
            //    (o1-oe+.5)*(c(o1)+c(oe-.5))/2
 
1185
            //  = (o1-oe+.5)*c((o1+oe-.5)/2)
 
1186
            //  = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
 
1187
            dt = o1-oe+.5;
 
1188
            f = (0.5*(o1+oe-.5)-o0)*df;
 
1189
            remainder[0] = dt*(r0 + f*(r1-r0));
 
1190
            remainder[1] = dt*(g0 + f*(g1-g0));
 
1191
            remainder[2] = dt*(b0 + f*(b1-b0));
 
1192
            remainder[3] = dt*(a0 + f*(a1-a0));
1122
1193
        }
1123
1194
    }
 
1195
    switch(gr->spread) {
 
1196
    case SP_GRADIENT_SPREAD_PAD:
 
1197
        gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
 
1198
        gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
 
1199
        gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
 
1200
        gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
 
1201
        break;
 
1202
    case SP_GRADIENT_SPREAD_REFLECT:
 
1203
        // The second half is the same as the first half, so multiply by 2.
 
1204
        gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
 
1205
        gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
 
1206
        gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
 
1207
        gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
 
1208
        break;
 
1209
    case SP_GRADIENT_SPREAD_REPEAT:
 
1210
        // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
 
1211
        gr->color[0] = gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
 
1212
        gr->color[1] = gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
 
1213
        gr->color[2] = gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
 
1214
        gr->color[3] = gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
 
1215
        break;
 
1216
    }
1124
1217
}
1125
1218
 
1126
1219
/**
1251
1344
    }
1252
1345
}
1253
1346
 
1254
 
NR::Matrix
1255
 
sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
 
1347
Geom::Matrix
 
1348
sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1256
1349
{
1257
1350
    if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1258
 
        return ( NR::scale(bbox.dimensions())
1259
 
                 * NR::translate(bbox.min())
1260
 
                 * ctm );
 
1351
        return ( Geom::Scale(bbox.dimensions())
 
1352
                 * Geom::Translate(bbox.min())
 
1353
                 * Geom::Matrix(ctm) );
1261
1354
    } else {
1262
1355
        return ctm;
1263
1356
    }
1264
1357
}
1265
1358
 
1266
 
NR::Matrix
1267
 
sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
 
1359
Geom::Matrix
 
1360
sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1268
1361
{
1269
1362
    if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1270
1363
        return ( gr->gradientTransform
1271
 
                 * NR::scale(bbox.dimensions())
1272
 
                 * NR::translate(bbox.min())
1273
 
                 * ctm );
 
1364
                 * Geom::Scale(bbox.dimensions())
 
1365
                 * Geom::Translate(bbox.min())
 
1366
                 * Geom::Matrix(ctm) );
1274
1367
    } else {
1275
1368
        return gr->gradientTransform * ctm;
1276
1369
    }
1277
1370
}
1278
1371
 
1279
1372
void
1280
 
sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
1281
 
                            NR::Rect const &bbox, NR::Matrix const &gs2d)
 
1373
sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
 
1374
                            Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1282
1375
{
1283
 
    gr->gradientTransform = gs2d / ctm;
 
1376
    gr->gradientTransform = gs2d * ctm.inverse();
1284
1377
    if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1285
1378
        gr->gradientTransform = ( gr->gradientTransform
1286
 
                                  / NR::translate(bbox.min())
1287
 
                                  / NR::scale(bbox.dimensions()) );
 
1379
                                  * Geom::Translate(-bbox.min())
 
1380
                                  * Geom::Scale(bbox.dimensions()).inverse() );
1288
1381
    }
1289
1382
    gr->gradientTransform_set = TRUE;
1290
1383
 
1312
1405
                                    SPDocument *document,
1313
1406
                                    Inkscape::XML::Node *repr);
1314
1407
static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1315
 
static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr,
 
1408
static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1316
1409
                                                    guint flags);
1317
1410
 
1318
1411
static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
1319
 
                                                NR::Matrix const &full_transform,
1320
 
                                                NR::Matrix const &parent_transform,
 
1412
                                                Geom::Matrix const &full_transform,
 
1413
                                                Geom::Matrix const &parent_transform,
1321
1414
                                                NRRect const *bbox);
1322
1415
static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1323
1416
 
1372
1465
static void sp_lineargradient_init(SPLinearGradient *lg)
1373
1466
{
1374
1467
    lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1375
 
    lg->y1.unset(SVGLength::PERCENT, 0.5, 0.5);
 
1468
    lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
1376
1469
    lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1377
 
    lg->y2.unset(SVGLength::PERCENT, 0.5, 0.5);
 
1470
    lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
1378
1471
}
1379
1472
 
1380
1473
/**
1407
1500
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1408
1501
            break;
1409
1502
        case SP_ATTR_Y1:
1410
 
            lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
 
1503
            lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1411
1504
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1412
1505
            break;
1413
1506
        case SP_ATTR_X2:
1415
1508
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1416
1509
            break;
1417
1510
        case SP_ATTR_Y2:
1418
 
            lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
 
1511
            lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1419
1512
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1420
1513
            break;
1421
1514
        default:
1429
1522
 * Callback: write attributes to associated repr.
1430
1523
 */
1431
1524
static Inkscape::XML::Node *
1432
 
sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
 
1525
sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1433
1526
{
1434
1527
    SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1435
1528
 
1436
1529
    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1437
 
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
1438
1530
        repr = xml_doc->createElement("svg:linearGradient");
1439
1531
    }
1440
1532
 
1448
1540
        sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1449
1541
 
1450
1542
    if (((SPObjectClass *) lg_parent_class)->write)
1451
 
        (* ((SPObjectClass *) lg_parent_class)->write)(object, repr, flags);
 
1543
        (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
1452
1544
 
1453
1545
    return repr;
1454
1546
}
1471
1563
 */
1472
1564
static SPPainter *
1473
1565
sp_lineargradient_painter_new(SPPaintServer *ps,
1474
 
                              NR::Matrix const &full_transform,
1475
 
                              NR::Matrix const &/*parent_transform*/,
 
1566
                              Geom::Matrix const &full_transform,
 
1567
                              Geom::Matrix const &/*parent_transform*/,
1476
1568
                              NRRect const *bbox)
1477
1569
{
1478
1570
    SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1494
1586
     * or something similar. Originally I had 1023.9999 here - not sure
1495
1587
     * whether we have really to cut out ceil int (Lauris).
1496
1588
     */
1497
 
    NR::Matrix color2norm(NR::identity());
1498
 
    NR::Matrix color2px;
 
1589
    Geom::Matrix color2norm(Geom::identity());
 
1590
    Geom::Matrix color2px;
1499
1591
    if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1500
 
        NR::Matrix norm2pos(NR::identity());
 
1592
        Geom::Matrix norm2pos(Geom::identity());
1501
1593
 
1502
1594
        /* BBox to user coordinate system */
1503
 
        NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
 
1595
        Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1504
1596
 
1505
 
        NR::Matrix color2pos = color2norm * norm2pos;
1506
 
        NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1507
 
        NR::Matrix color2user = color2tpos * bbox2user;
 
1597
        Geom::Matrix color2pos = color2norm * norm2pos;
 
1598
        Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
 
1599
        Geom::Matrix color2user = color2tpos * bbox2user;
1508
1600
        color2px = color2user * full_transform;
1509
1601
 
1510
1602
    } else {
1511
1603
        /* Problem: What to do, if we have mixed lengths and percentages? */
1512
1604
        /* Currently we do ignore percentages at all, but that is not good (lauris) */
1513
1605
 
1514
 
        NR::Matrix norm2pos(NR::identity());
1515
 
        NR::Matrix color2pos = color2norm * norm2pos;
1516
 
        NR::Matrix color2tpos = color2pos * gr->gradientTransform;
 
1606
        Geom::Matrix norm2pos(Geom::identity());
 
1607
        Geom::Matrix color2pos = color2norm * norm2pos;
 
1608
        Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1517
1609
        color2px = color2tpos * full_transform;
1518
1610
 
1519
1611
    }
1520
 
 
1521
 
    NRMatrix v2px;
1522
 
    color2px.copyto(&v2px);
1523
 
 
1524
 
    nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
 
1612
    // TODO: remove color2px_nr after converting to 2geom
 
1613
    NR::Matrix color2px_nr = from_2geom(color2px);
 
1614
    nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &color2px_nr,
1525
1615
                                lg->x1.computed, lg->y1.computed,
1526
1616
                                lg->x2.computed, lg->y2.computed);
1527
1617
 
1590
1680
                                    SPDocument *document,
1591
1681
                                    Inkscape::XML::Node *repr);
1592
1682
static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1593
 
static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr,
 
1683
static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1594
1684
                                                    guint flags);
1595
1685
 
1596
1686
static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
1597
 
                                                NR::Matrix const &full_transform,
1598
 
                                                NR::Matrix const &parent_transform,
 
1687
                                                Geom::Matrix const &full_transform,
 
1688
                                                Geom::Matrix const &parent_transform,
1599
1689
                                                NRRect const *bbox);
1600
1690
static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1601
1691
 
1731
1821
 * Write radial gradient attributes to associated repr.
1732
1822
 */
1733
1823
static Inkscape::XML::Node *
1734
 
sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
 
1824
sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1735
1825
{
1736
1826
    SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1737
1827
 
1738
1828
    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1739
 
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
1740
1829
        repr = xml_doc->createElement("svg:radialGradient");
1741
1830
    }
1742
1831
 
1747
1836
    if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1748
1837
 
1749
1838
    if (((SPObjectClass *) rg_parent_class)->write)
1750
 
        (* ((SPObjectClass *) rg_parent_class)->write)(object, repr, flags);
 
1839
        (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1751
1840
 
1752
1841
    return repr;
1753
1842
}
1757
1846
 */
1758
1847
static SPPainter *
1759
1848
sp_radialgradient_painter_new(SPPaintServer *ps,
1760
 
                              NR::Matrix const &full_transform,
1761
 
                              NR::Matrix const &/*parent_transform*/,
 
1849
                              Geom::Matrix const &full_transform,
 
1850
                              Geom::Matrix const &/*parent_transform*/,
1762
1851
                              NRRect const *bbox)
1763
1852
{
1764
1853
    SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1773
1862
 
1774
1863
    rgp->rg = rg;
1775
1864
 
1776
 
    NR::Matrix gs2px;
 
1865
    Geom::Matrix gs2px;
1777
1866
 
1778
1867
    if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1779
1868
        /** \todo
1782
1871
         */
1783
1872
 
1784
1873
        /* BBox to user coordinate system */
1785
 
        NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
 
1874
        Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1786
1875
 
1787
 
        NR::Matrix gs2user = gr->gradientTransform * bbox2user;
 
1876
        Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1788
1877
 
1789
1878
        gs2px = gs2user * full_transform;
1790
1879
    } else {
1796
1885
 
1797
1886
        gs2px = gr->gradientTransform * full_transform;
1798
1887
    }
1799
 
 
1800
 
    NRMatrix gs2px_nr;
1801
 
    gs2px.copyto(&gs2px_nr);
1802
 
 
 
1888
    // TODO: remove gs2px_nr after converting to 2geom
 
1889
    NR::Matrix gs2px_nr = from_2geom(gs2px);
1803
1890
    nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
1804
1891
                                &gs2px_nr,
1805
1892
                                rg->cx.computed, rg->cy.computed,