Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / sp-gradient.cpp
1 /** \file
2  * SPGradient, SPStop, SPLinearGradient, SPRadialGradient.
3  */
4 /*
5  * Authors:
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
9  *   Jon A. Cruz <jon@joncruz.org>
10  *   Abhishek Sharma
11  *
12  * Copyright (C) 1999-2002 Lauris Kaplinski
13  * Copyright (C) 2000-2001 Ximian, Inc.
14  * Copyright (C) 2004 David Turner
15  * Copyright (C) 2009 Jasper van de Gronde
16  *
17  * Released under GNU GPL, read the file 'COPYING' for more information
18  *
19  */
21 #define noSP_GRADIENT_VERBOSE
23 #include <cstring>
24 #include <string>
26 #include <libnr/nr-matrix-fns.h>
27 #include <libnr/nr-matrix-ops.h>
28 #include <libnr/nr-matrix-scale-ops.h>
29 #include <2geom/transforms.h>
31 #include <sigc++/functors/ptr_fun.h>
32 #include <sigc++/adaptors/bind.h>
34 #include "libnr/nr-gradient.h"
35 #include "libnr/nr-pixops.h"
36 #include "svg/svg.h"
37 #include "svg/svg-color.h"
38 #include "svg/css-ostringstream.h"
39 #include "attributes.h"
40 #include "document-private.h"
41 #include "sp-gradient.h"
42 #include "gradient-chemistry.h"
43 #include "sp-gradient-reference.h"
44 #include "sp-linear-gradient.h"
45 #include "sp-radial-gradient.h"
46 #include "sp-stop.h"
47 #include "streq.h"
48 #include "uri.h"
49 #include "xml/repr.h"
50 #include "style.h"
52 #define SP_MACROS_SILENT
53 #include "macros.h"
55 /// Has to be power of 2
56 #define NCOLORS NR_GRADIENT_VECTOR_LENGTH
58 static void sp_stop_class_init(SPStopClass *klass);
59 static void sp_stop_init(SPStop *stop);
61 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
62 static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
63 static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
65 static SPObjectClass *stop_parent_class;
67 class SPGradientImpl
68 {
69     friend class SPGradient;
71     static void classInit(SPGradientClass *klass);
73     static void init(SPGradient *gr);
74     static void build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
75     static void release(SPObject *object);
76     static void modified(SPObject *object, guint flags);
77     static Inkscape::XML::Node *write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags);
79     static void gradientRefModified(SPObject *href, guint flags, SPGradient *gradient);
80     static void gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr);
82     static void childAdded(SPObject *object,
83                            Inkscape::XML::Node *child,
84                            Inkscape::XML::Node *ref);
85     static void removeChild(SPObject *object, Inkscape::XML::Node *child);
87     static void setGradientAttr(SPObject *object, unsigned key, gchar const *value);
88 };
90 /**
91  * Registers SPStop class and returns its type.
92  */
93 GType
94 sp_stop_get_type()
95 {
96     static GType type = 0;
97     if (!type) {
98         GTypeInfo info = {
99             sizeof(SPStopClass),
100             NULL, NULL,
101             (GClassInitFunc) sp_stop_class_init,
102             NULL, NULL,
103             sizeof(SPStop),
104             16,
105             (GInstanceInitFunc) sp_stop_init,
106             NULL,   /* value_table */
107         };
108         type = g_type_register_static(SP_TYPE_OBJECT, "SPStop", &info, (GTypeFlags)0);
109     }
110     return type;
113 /**
114  * Callback to initialize SPStop vtable.
115  */
116 static void sp_stop_class_init(SPStopClass *klass)
118     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
120     stop_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
122     sp_object_class->build = sp_stop_build;
123     sp_object_class->set = sp_stop_set;
124     sp_object_class->write = sp_stop_write;
127 /**
128  * Callback to initialize SPStop object.
129  */
130 static void
131 sp_stop_init(SPStop *stop)
133     stop->offset = 0.0;
134     stop->currentColor = false;
135     stop->specified_color.set( 0x000000ff );
136     stop->opacity = 1.0;
139 /**
140  * Virtual build: set stop attributes from its associated XML node.
141  */
142 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
144     if (((SPObjectClass *) stop_parent_class)->build)
145         (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
147     object->readAttr( "offset" );
148     object->readAttr( "stop-color" );
149     object->readAttr( "stop-opacity" );
150     object->readAttr( "style" );
153 /**
154  * Virtual set: set attribute to value.
155  */
156 static void
157 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
159     SPStop *stop = SP_STOP(object);
161     switch (key) {
162         case SP_ATTR_STYLE: {
163         /** \todo
164          * fixme: We are reading simple values 3 times during build (Lauris).
165          * \par
166          * We need presentation attributes etc.
167          * \par
168          * remove the hackish "style reading" from here: see comments in
169          * sp_object_get_style_property about the bugs in our current
170          * approach.  However, note that SPStyle doesn't currently have
171          * stop-color and stop-opacity properties.
172          */
173             {
174                 gchar const *p = object->getStyleProperty( "stop-color", "black");
175                 if (streq(p, "currentColor")) {
176                     stop->currentColor = true;
177                 } else {
178                     stop->specified_color = SPStop::readStopColor( p );
179                 }
180             }
181             {
182                 gchar const *p = object->getStyleProperty( "stop-opacity", "1");
183                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
184                 stop->opacity = opacity;
185             }
186             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
187             break;
188         }
189         case SP_PROP_STOP_COLOR: {
190             {
191                 gchar const *p = object->getStyleProperty( "stop-color", "black");
192                 if (streq(p, "currentColor")) {
193                     stop->currentColor = true;
194                 } else {
195                     stop->currentColor = false;
196                     stop->specified_color = SPStop::readStopColor( p );
197                 }
198             }
199             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
200             break;
201         }
202         case SP_PROP_STOP_OPACITY: {
203             {
204                 gchar const *p = object->getStyleProperty( "stop-opacity", "1");
205                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
206                 stop->opacity = opacity;
207             }
208             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
209             break;
210         }
211         case SP_ATTR_OFFSET: {
212             stop->offset = sp_svg_read_percentage(value, 0.0);
213             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
214             break;
215         }
216         default: {
217             if (((SPObjectClass *) stop_parent_class)->set)
218                 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
219             break;
220         }
221     }
224 /**
225  * Virtual write: write object attributes to repr.
226  */
227 static Inkscape::XML::Node *
228 sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
230     SPStop *stop = SP_STOP(object);
232     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
233         repr = xml_doc->createElement("svg:stop");
234     }
236     Glib::ustring colorStr = stop->specified_color.toString();
237     gfloat opacity = stop->opacity;
239     if (((SPObjectClass *) stop_parent_class)->write) {
240         (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
241     }
243     // Since we do a hackish style setting here (because SPStyle does not support stop-color and
244     // stop-opacity), we must do it AFTER calling the parent write method; otherwise
245     // sp_object_write would clear our style= attribute (bug 1695287)
247     Inkscape::CSSOStringStream os;
248     os << "stop-color:";
249     if (stop->currentColor) {
250         os << "currentColor";
251     } else {
252         os << colorStr;
253     }
254     os << ";stop-opacity:" << opacity;
255     repr->setAttribute("style", os.str().c_str());
256     repr->setAttribute("stop-color", NULL);
257     repr->setAttribute("stop-opacity", NULL);
258     sp_repr_set_css_double(repr, "offset", stop->offset);
259     /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no sense
260      * for offset proportions. */
262     return repr;
266 bool SPGradient::hasStops() const
268     return has_stops;
271 bool SPGradient::isUnitsSet() const
273     return units_set;
276 SPGradientUnits SPGradient::getUnits() const
278     return units;
281 bool SPGradient::isSpreadSet() const
283     return spread_set;
286 SPGradientSpread SPGradient::getSpread() const
288     return spread;
291 void SPGradient::setSwatch( bool swatch )
293     if ( swatch != isSwatch() ) {
294         this->swatch = swatch; // to make isSolid() work, this happens first
295         gchar const* paintVal = swatch ? (isSolid() ? "solid" : "gradient") : 0;
296         setAttribute( "osb:paint", paintVal, 0 );
298         requestModified( SP_OBJECT_MODIFIED_FLAG );
299     }
302 /**
303  * Return stop's color as 32bit value.
304  */
305 guint32
306 sp_stop_get_rgba32(SPStop const *const stop)
308     guint32 rgb0 = 0;
309     /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
310      * value depends on user agent, and don't give any further restrictions that I can
311      * see.) */
312     if (stop->currentColor) {
313         char const *str = stop->getStyleProperty( "color", NULL);
314         if (str) {
315             rgb0 = sp_svg_read_color(str, rgb0);
316         }
317         unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
318         g_return_val_if_fail((alpha & ~0xff) == 0,
319                              rgb0 | 0xff);
320         return rgb0 | alpha;
321     } else {
322         return stop->specified_color.toRGBA32( stop->opacity );
323     }
326 /*
327  * Gradient
328  */
330 static SPPaintServerClass *gradient_parent_class;
332 /**
333  * Registers SPGradient class and returns its type.
334  */
335 GType SPGradient::getType()
337     static GType gradient_type = 0;
338     if (!gradient_type) {
339         GTypeInfo gradient_info = {
340             sizeof(SPGradientClass),
341             NULL, NULL,
342             (GClassInitFunc) SPGradientImpl::classInit,
343             NULL, NULL,
344             sizeof(SPGradient),
345             16,
346             (GInstanceInitFunc) SPGradientImpl::init,
347             NULL,   /* value_table */
348         };
349         gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
350                                                &gradient_info, (GTypeFlags)0);
351     }
352     return gradient_type;
355 /**
356  * SPGradient vtable initialization.
357  */
358 void SPGradientImpl::classInit(SPGradientClass *klass)
360     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
362     gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
364     sp_object_class->build = SPGradientImpl::build;
365     sp_object_class->release = SPGradientImpl::release;
366     sp_object_class->set = SPGradientImpl::setGradientAttr;
367     sp_object_class->child_added = SPGradientImpl::childAdded;
368     sp_object_class->remove_child = SPGradientImpl::removeChild;
369     sp_object_class->modified = SPGradientImpl::modified;
370     sp_object_class->write = SPGradientImpl::write;
373 /**
374  * Callback for SPGradient object initialization.
375  */
376 void SPGradientImpl::init(SPGradient *gr)
378     gr->ref = new SPGradientReference(SP_OBJECT(gr));
379     gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(SPGradientImpl::gradientRefChanged), gr));
381     /** \todo
382      * Fixme: reprs being rearranged (e.g. via the XML editor)
383      * may require us to clear the state.
384      */
385     gr->state = SP_GRADIENT_STATE_UNKNOWN;
387     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
388     gr->units_set = FALSE;
390     gr->gradientTransform = Geom::identity();
391     gr->gradientTransform_set = FALSE;
393     gr->spread = SP_GRADIENT_SPREAD_PAD;
394     gr->spread_set = FALSE;
396     gr->has_stops = FALSE;
398     gr->vector.built = false;
399     gr->vector.stops.clear();
401     gr->color = NULL;
403     new (&gr->modified_connection) sigc::connection();
406 /**
407  * Virtual build: set gradient attributes from its associated repr.
408  */
409 void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
411     SPGradient *gradient = SP_GRADIENT(object);
413     // Work-around in case a swatch had been marked for immediate collection:
414     if ( repr->attribute("osb:paint") && repr->attribute("inkscape:collect") ) {
415         repr->setAttribute("inkscape:collect", 0);
416     }
418     if (((SPObjectClass *) gradient_parent_class)->build) {
419         (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
420     }
422     for ( SPObject *ochild = object->firstChild() ; ochild ; ochild = ochild->getNext() ) {
423         if (SP_IS_STOP(ochild)) {
424             gradient->has_stops = TRUE;
425             break;
426         }
427     }
429     object->readAttr( "gradientUnits" );
430     object->readAttr( "gradientTransform" );
431     object->readAttr( "spreadMethod" );
432     object->readAttr( "xlink:href" );
433     object->readAttr( "osb:paint" );
435     // Register ourselves
436     document->addResource("gradient", object);
439 /**
440  * Virtual release of SPGradient members before destruction.
441  */
442 void SPGradientImpl::release(SPObject *object)
444     SPGradient *gradient = (SPGradient *) object;
446 #ifdef SP_GRADIENT_VERBOSE
447     g_print("Releasing gradient %s\n", object->getId());
448 #endif
450     if (SP_OBJECT_DOCUMENT(object)) {
451         /* Unregister ourselves */
452         SP_OBJECT_DOCUMENT(object)->removeResource("gradient", SP_OBJECT(object));
453     }
455     if (gradient->ref) {
456         gradient->modified_connection.disconnect();
457         gradient->ref->detach();
458         delete gradient->ref;
459         gradient->ref = NULL;
460     }
462     if (gradient->color) {
463         g_free(gradient->color);
464         gradient->color = NULL;
465     }
467     gradient->modified_connection.~connection();
469     if (((SPObjectClass *) gradient_parent_class)->release)
470         ((SPObjectClass *) gradient_parent_class)->release(object);
473 /**
474  * Set gradient attribute to value.
475  */
476 void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const *value)
478     SPGradient *gr = SP_GRADIENT(object);
480     switch (key) {
481         case SP_ATTR_GRADIENTUNITS:
482             if (value) {
483                 if (!strcmp(value, "userSpaceOnUse")) {
484                     gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
485                 } else {
486                     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
487                 }
488                 gr->units_set = TRUE;
489             } else {
490                 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
491                 gr->units_set = FALSE;
492             }
493             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
494             break;
495         case SP_ATTR_GRADIENTTRANSFORM: {
496             Geom::Matrix t;
497             if (value && sp_svg_transform_read(value, &t)) {
498                 gr->gradientTransform = t;
499                 gr->gradientTransform_set = TRUE;
500             } else {
501                 gr->gradientTransform = Geom::identity();
502                 gr->gradientTransform_set = FALSE;
503             }
504             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
505             break;
506         }
507         case SP_ATTR_SPREADMETHOD:
508             if (value) {
509                 if (!strcmp(value, "reflect")) {
510                     gr->spread = SP_GRADIENT_SPREAD_REFLECT;
511                 } else if (!strcmp(value, "repeat")) {
512                     gr->spread = SP_GRADIENT_SPREAD_REPEAT;
513                 } else {
514                     gr->spread = SP_GRADIENT_SPREAD_PAD;
515                 }
516                 gr->spread_set = TRUE;
517             } else {
518                 gr->spread_set = FALSE;
519             }
520             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
521             break;
522         case SP_ATTR_XLINK_HREF:
523             if (value) {
524                 try {
525                     gr->ref->attach(Inkscape::URI(value));
526                 } catch (Inkscape::BadURIException &e) {
527                     g_warning("%s", e.what());
528                     gr->ref->detach();
529                 }
530             } else {
531                 gr->ref->detach();
532             }
533             break;
534         case SP_ATTR_OSB_SWATCH:
535         {
536             bool newVal = (value != 0);
537             bool modified = false;
538             if (newVal != gr->swatch) {
539                 gr->swatch = newVal;
540                 modified = true;
541             }
542             if (newVal) {
543                 // Might need to flip solid/gradient
544                 Glib::ustring paintVal = ( gr->hasStops() && (gr->getStopCount() == 0) ) ? "solid" : "gradient";
545                 if ( paintVal != value ) {
546                     gr->setAttribute( "osb:paint", paintVal.c_str(), 0 );
547                     modified = true;
548                 }
549             }
550             if (modified) {
551                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
552             }
553         }
554             break;
555         default:
556             if (((SPObjectClass *) gradient_parent_class)->set) {
557                 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
558             }
559             break;
560     }
563 /**
564  * Gets called when the gradient is (re)attached to another gradient.
565  */
566 void SPGradientImpl::gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr)
568     if (old_ref) {
569         gr->modified_connection.disconnect();
570     }
571     if ( SP_IS_GRADIENT(ref)
572          && ref != gr )
573     {
574         gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&SPGradientImpl::gradientRefModified), gr));
575     }
577     // Per SVG, all unset attributes must be inherited from linked gradient.
578     // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
579     // but without setting the _set flags.
580     // FIXME: do the same for gradientTransform too
581     if (!gr->units_set) {
582         gr->units = gr->fetchUnits();
583     }
584     if (!gr->spread_set) {
585         gr->spread = gr->fetchSpread();
586     }
588     /// \todo Fixme: what should the flags (second) argument be? */
589     gradientRefModified(ref, 0, gr);
592 /**
593  * Callback for child_added event.
594  */
595 void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
597     SPGradient *gr = SP_GRADIENT(object);
599     gr->invalidateVector();
601     if (((SPObjectClass *) gradient_parent_class)->child_added) {
602         (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
603     }
605     SPObject *ochild = object->get_child_by_repr(child);
606     if ( ochild && SP_IS_STOP(ochild) ) {
607         gr->has_stops = TRUE;
608         if ( gr->getStopCount() > 0 ) {
609             gchar const * attr = gr->getAttribute("osb:paint");
610             if ( attr && strcmp(attr, "gradient") ) {
611                 gr->setAttribute( "osb:paint", "gradient", 0 );
612             }
613         }
614     }
616     /// \todo Fixme: should we schedule "modified" here?
617     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
620 /**
621  * Callback for remove_child event.
622  */
623 void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
625     SPGradient *gr = SP_GRADIENT(object);
627     gr->invalidateVector();
629     if (((SPObjectClass *) gradient_parent_class)->remove_child) {
630         (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
631     }
633     gr->has_stops = FALSE;
634     for ( SPObject *ochild = object->firstChild() ; ochild ; ochild = ochild->getNext() ) {
635         if (SP_IS_STOP(ochild)) {
636             gr->has_stops = TRUE;
637             break;
638         }
639     }
641     if ( gr->getStopCount() == 0 ) {
642         gchar const * attr = gr->getAttribute("osb:paint");
643         if ( attr && strcmp(attr, "solid") ) {
644             gr->setAttribute( "osb:paint", "solid", 0 );
645         }
646     }
648     /* Fixme: should we schedule "modified" here? */
649     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
652 /**
653  * Callback for modified event.
654  */
655 void SPGradientImpl::modified(SPObject *object, guint flags)
657     SPGradient *gr = SP_GRADIENT(object);
659     if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
660         gr->invalidateVector();
661     }
663     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
664         gr->ensureColors();
665     }
667     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
668     flags &= SP_OBJECT_MODIFIED_CASCADE;
670     // FIXME: climb up the ladder of hrefs
671     GSList *l = NULL;
672     for (SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
673         g_object_ref(G_OBJECT(child));
674         l = g_slist_prepend(l, child);
675     }
676     l = g_slist_reverse(l);
677     while (l) {
678         SPObject *child = SP_OBJECT(l->data);
679         l = g_slist_remove(l, child);
680         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
681             child->emitModified(flags);
682         }
683         g_object_unref(G_OBJECT(child));
684     }
687 SPStop* SPGradient::getFirstStop()
689     SPStop* first = 0;
690     for (SPObject *ochild = firstChild(); ochild && !first; ochild = ochild->getNext()) {
691         if (SP_IS_STOP(ochild)) {
692             first = SP_STOP(ochild);
693         }
694     }
695     return first;
698 int SPGradient::getStopCount() const
700     int count = 0;
702     for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
703         count++;
704     }
706     return count;
709 /**
710  * Write gradient attributes to repr.
711  */
712 Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
714     SPGradient *gr = SP_GRADIENT(object);
716     if (((SPObjectClass *) gradient_parent_class)->write) {
717         (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
718     }
720     if (flags & SP_OBJECT_WRITE_BUILD) {
721         GSList *l = NULL;
722         for (SPObject *child = object->firstChild(); child; child = child->getNext()) {
723             Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
724             if (crepr) {
725                 l = g_slist_prepend(l, crepr);
726             }
727         }
728         while (l) {
729             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
730             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
731             l = g_slist_remove(l, l->data);
732         }
733     }
735     if (gr->ref->getURI()) {
736         gchar *uri_string = gr->ref->getURI()->toString();
737         repr->setAttribute("xlink:href", uri_string);
738         g_free(uri_string);
739     }
741     if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
742         switch (gr->units) {
743             case SP_GRADIENT_UNITS_USERSPACEONUSE:
744                 repr->setAttribute("gradientUnits", "userSpaceOnUse");
745                 break;
746             default:
747                 repr->setAttribute("gradientUnits", "objectBoundingBox");
748                 break;
749         }
750     }
752     if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
753         gchar *c=sp_svg_transform_write(gr->gradientTransform);
754         repr->setAttribute("gradientTransform", c);
755         g_free(c);
756     }
758     if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
759         /* FIXME: Ensure that gr->spread is the inherited value
760          * if !gr->spread_set.  Not currently happening: see SPGradient::modified.
761          */
762         switch (gr->spread) {
763             case SP_GRADIENT_SPREAD_REFLECT:
764                 repr->setAttribute("spreadMethod", "reflect");
765                 break;
766             case SP_GRADIENT_SPREAD_REPEAT:
767                 repr->setAttribute("spreadMethod", "repeat");
768                 break;
769             default:
770                 repr->setAttribute("spreadMethod", "pad");
771                 break;
772         }
773     }
775     if ( (flags & SP_OBJECT_WRITE_EXT) && gr->isSwatch() ) {
776         if ( gr->isSolid() ) {
777             repr->setAttribute( "osb:paint", "solid" );
778         } else {
779             repr->setAttribute( "osb:paint", "gradient" );
780         }
781     } else {
782         repr->setAttribute( "osb:paint", 0 );
783     }
785     return repr;
788 /**
789  * Forces the vector to be built, if not present (i.e., changed).
790  *
791  * \pre SP_IS_GRADIENT(gradient).
792  */
793 void SPGradient::ensureVector()
795     if ( !vector.built ) {
796         rebuildVector();
797     }
800 /**
801  * Set units property of gradient and emit modified.
802  */
803 void SPGradient::setUnits(SPGradientUnits units)
805     if (units != this->units) {
806         this->units = units;
807         units_set = TRUE;
808         requestModified(SP_OBJECT_MODIFIED_FLAG);
809     }
812 /**
813  * Set spread property of gradient and emit modified.
814  */
815 void SPGradient::setSpread(SPGradientSpread spread)
817     if (spread != this->spread) {
818         this->spread = spread;
819         spread_set = TRUE;
820         requestModified(SP_OBJECT_MODIFIED_FLAG);
821     }
824 /**
825  * Returns the first of {src, src-\>ref-\>getObject(),
826  * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
827  * for which \a match is true, or NULL if none found.
828  *
829  * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
830  * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
831  *
832  * \pre SP_IS_GRADIENT(src).
833  */
834 static SPGradient *
835 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
837     g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
839     /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2.  If there is a
840        loop, then once p1 has entered the loop, we'll detect it the next time the distance between
841        p1 and p2 is a multiple of the loop size. */
842     SPGradient *p1 = src, *p2 = src;
843     bool do1 = false;
844     for (;;) {
845         if (match(p2)) {
846             return p2;
847         }
849         p2 = p2->ref->getObject();
850         if (!p2) {
851             return p2;
852         }
853         if (do1) {
854             p1 = p1->ref->getObject();
855         }
856         do1 = !do1;
858         if ( p2 == p1 ) {
859             /* We've been here before, so return NULL to indicate that no matching gradient found
860              * in the chain. */
861             return NULL;
862         }
863     }
866 /**
867  * True if gradient has stops.
868  */
869 static bool has_stopsFN(SPGradient const *gr)
871     return gr->hasStops();
874 /**
875  * True if gradient has spread set.
876  */
877 static bool has_spread_set(SPGradient const *gr)
879     return gr->isSpreadSet();
882 /**
883  * True if gradient has units set.
884  */
885 static bool
886 has_units_set(SPGradient const *gr)
888     return gr->isUnitsSet();
892 SPGradient *SPGradient::getVector(bool force_vector)
894     SPGradient * src = chase_hrefs(this, has_stopsFN);
896     if (force_vector) {
897         src = sp_gradient_ensure_vector_normalized(src);
898     }
899     return src;
902 /**
903  * Returns the effective spread of given gradient (climbing up the refs chain if needed).
904  *
905  * \pre SP_IS_GRADIENT(gradient).
906  */
907 SPGradientSpread SPGradient::fetchSpread()
909     SPGradient const *src = chase_hrefs(this, has_spread_set);
910     return ( src
911              ? src->spread
912              : SP_GRADIENT_SPREAD_PAD ); // pad is the default
915 /**
916  * Returns the effective units of given gradient (climbing up the refs chain if needed).
917  *
918  * \pre SP_IS_GRADIENT(gradient).
919  */
920 SPGradientUnits SPGradient::fetchUnits()
922     SPGradient const *src = chase_hrefs(this, has_units_set);
923     return ( src
924              ? src->units
925              : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
929 /**
930  * Clears the gradient's svg:stop children from its repr.
931  */
932 void
933 sp_gradient_repr_clear_vector(SPGradient *gr)
935     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
937     /* Collect stops from original repr */
938     GSList *sl = NULL;
939     for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
940         if (!strcmp(child->name(), "svg:stop")) {
941             sl = g_slist_prepend(sl, child);
942         }
943     }
944     /* Remove all stops */
945     while (sl) {
946         /** \todo
947          * fixme: This should work, unless we make gradient
948          * into generic group.
949          */
950         sp_repr_unparent((Inkscape::XML::Node *)sl->data);
951         sl = g_slist_remove(sl, sl->data);
952     }
955 /**
956  * Writes the gradient's internal vector (whether from its own stops, or
957  * inherited from refs) into the gradient repr as svg:stop elements.
958  */
959 void
960 sp_gradient_repr_write_vector(SPGradient *gr)
962     g_return_if_fail(gr != NULL);
963     g_return_if_fail(SP_IS_GRADIENT(gr));
965     Inkscape::XML::Document *xml_doc = SP_OBJECT_DOCUMENT(gr)->getReprDoc();
966     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
968     /* We have to be careful, as vector may be our own, so construct repr list at first */
969     GSList *cl = NULL;
971     for (guint i = 0; i < gr->vector.stops.size(); i++) {
972         Inkscape::CSSOStringStream os;
973         Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
974         sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
975         /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
976          * sense for offset proportions. */
977         os << "stop-color:" << gr->vector.stops[i].color.toString() << ";stop-opacity:" << gr->vector.stops[i].opacity;
978         child->setAttribute("style", os.str().c_str());
979         /* Order will be reversed here */
980         cl = g_slist_prepend(cl, child);
981     }
983     sp_gradient_repr_clear_vector(gr);
985     /* And insert new children from list */
986     while (cl) {
987         Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
988         repr->addChild(child, NULL);
989         Inkscape::GC::release(child);
990         cl = g_slist_remove(cl, child);
991     }
995 void SPGradientImpl::gradientRefModified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
997     if ( gradient->invalidateVector() ) {
998         SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
999         // Conditional to avoid causing infinite loop if there's a cycle in the href chain.
1000     }
1003 /** Return true if change made. */
1004 bool SPGradient::invalidateVector()
1006     bool ret = false;
1008     if (color != NULL) {
1009         g_free(color);
1010         color = NULL;
1011         ret = true;
1012     }
1014     if (vector.built) {
1015         vector.built = false;
1016         vector.stops.clear();
1017         ret = true;
1018     }
1020     return ret;
1023 /** Creates normalized color vector */
1024 void SPGradient::rebuildVector()
1026     gint len = 0;
1027     for ( SPObject *child = firstChild() ; child ; child = child->getNext() ) {
1028         if (SP_IS_STOP(child)) {
1029             len ++;
1030         }
1031     }
1033     has_stops = (len != 0);
1035     vector.stops.clear();
1037     SPGradient *reffed = ref->getObject();
1038     if ( !hasStops() && reffed ) {
1039         /* Copy vector from referenced gradient */
1040         vector.built = true;   // Prevent infinite recursion.
1041         reffed->ensureVector();
1042         if (!reffed->vector.stops.empty()) {
1043             vector.built = reffed->vector.built;
1044             vector.stops.assign(reffed->vector.stops.begin(), reffed->vector.stops.end());
1045             return;
1046         }
1047     }
1049     for ( SPObject *child = firstChild(); child; child = child->getNext() ) {
1050         if (SP_IS_STOP(child)) {
1051             SPStop *stop = SP_STOP(child);
1053             SPGradientStop gstop;
1054             if (vector.stops.size() > 0) {
1055                 // "Each gradient offset value is required to be equal to or greater than the
1056                 // previous gradient stop's offset value. If a given gradient stop's offset
1057                 // value is not equal to or greater than all previous offset values, then the
1058                 // offset value is adjusted to be equal to the largest of all previous offset
1059                 // values."
1060                 gstop.offset = MAX(stop->offset, vector.stops.back().offset);
1061             } else {
1062                 gstop.offset = stop->offset;
1063             }
1065             // "Gradient offset values less than 0 (or less than 0%) are rounded up to
1066             // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
1067             // down to 100%."
1068             gstop.offset = CLAMP(gstop.offset, 0, 1);
1070             gstop.color = stop->getEffectiveColor();
1071             gstop.opacity = stop->opacity;
1073             vector.stops.push_back(gstop);
1074         }
1075     }
1077     // Normalize per section 13.2.4 of SVG 1.1.
1078     if (vector.stops.size() == 0) {
1079         /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
1080          * paint style."
1081          */
1082         {
1083             SPGradientStop gstop;
1084             gstop.offset = 0.0;
1085             gstop.color.set( 0x00000000 );
1086             gstop.opacity = 0.0;
1087             vector.stops.push_back(gstop);
1088         }
1089         {
1090             SPGradientStop gstop;
1091             gstop.offset = 1.0;
1092             gstop.color.set( 0x00000000 );
1093             gstop.opacity = 0.0;
1094             vector.stops.push_back(gstop);
1095         }
1096     } else {
1097         /* "If one stop is defined, then paint with the solid color fill using the color defined
1098          * for that gradient stop."
1099          */
1100         if (vector.stops.front().offset > 0.0) {
1101             // If the first one is not at 0, then insert a copy of the first at 0.
1102             SPGradientStop gstop;
1103             gstop.offset = 0.0;
1104             gstop.color = vector.stops.front().color;
1105             gstop.opacity = vector.stops.front().opacity;
1106             vector.stops.insert(vector.stops.begin(), gstop);
1107         }
1108         if (vector.stops.back().offset < 1.0) {
1109             // If the last one is not at 1, then insert a copy of the last at 1.
1110             SPGradientStop gstop;
1111             gstop.offset = 1.0;
1112             gstop.color = vector.stops.back().color;
1113             gstop.opacity = vector.stops.back().opacity;
1114             vector.stops.push_back(gstop);
1115         }
1116     }
1118     vector.built = true;
1121 /**
1122  * The gradient's color array is newly created and set up from vector.
1123  */
1124 void SPGradient::ensureColors()
1126     if (!vector.built) {
1127         rebuildVector();
1128     }
1129     g_return_if_fail(!vector.stops.empty());
1131     /// \todo Where is the memory freed?
1132     if (!color) {
1133         color = g_new(guchar, 4 * NCOLORS);
1134     }
1136     // This assumes that vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1137     // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1138     // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1140     double remainder[4] = {0,0,0,0};
1141     double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1142     switch(spread) {
1143     case SP_GRADIENT_SPREAD_PAD:
1144         remainder[0] = 0.5*vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1145         remainder[1] = 0.5*vector.stops[0].color.v.c[1];
1146         remainder[2] = 0.5*vector.stops[0].color.v.c[2];
1147         remainder[3] = 0.5*vector.stops[0].opacity;
1148         remainder_for_end[0] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[0]; // Half of the first cell uses the color of the last stop
1149         remainder_for_end[1] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[1];
1150         remainder_for_end[2] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[2];
1151         remainder_for_end[3] = 0.5*vector.stops[vector.stops.size() - 1].opacity;
1152         break;
1153     case SP_GRADIENT_SPREAD_REFLECT:
1154     case SP_GRADIENT_SPREAD_REPEAT:
1155         // These two are handled differently, see below.
1156         break;
1157     default:
1158         g_error("Spread type not supported!");
1159     };
1160     for (unsigned int i = 0; i < vector.stops.size() - 1; i++) {
1161         double r0 = vector.stops[i].color.v.c[0];
1162         double g0 = vector.stops[i].color.v.c[1];
1163         double b0 = vector.stops[i].color.v.c[2];
1164         double a0 = vector.stops[i].opacity;
1165         double r1 = vector.stops[i+1].color.v.c[0];
1166         double g1 = vector.stops[i+1].color.v.c[1];
1167         double b1 = vector.stops[i+1].color.v.c[2];
1168         double a1 = vector.stops[i+1].opacity;
1169         double o0 = vector.stops[i].offset * (NCOLORS-1);
1170         double o1 = vector.stops[i + 1].offset * (NCOLORS-1);
1171         unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1172         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
1174         if (oe == ob) {
1175             // Simple case, this interval starts and stops within one cell
1176             // The contribution of this interval is:
1177             //    (o1-o0)*(c(o0)+c(o1))/2
1178             //  = (o1-o0)*(c0+c1)/2
1179             double dt = 0.5*(o1-o0);
1180             remainder[0] += dt*(r0 + r1);
1181             remainder[1] += dt*(g0 + g1);
1182             remainder[2] += dt*(b0 + b1);
1183             remainder[3] += dt*(a0 + a1);
1184         } else {
1185             // First compute colors for the cells which are fully covered by the current interval.
1186             // The prefiltered values are equal to the midpoint of each cell here.
1187             //  f = (j-o0)/(o1-o0)
1188             //    = j*(1/(o1-o0)) - o0/(o1-o0)
1189             double f = (ob-o0) / (o1-o0);
1190             double df = 1. / (o1-o0);
1191             for (unsigned int j = ob+1; j < oe; j++) {
1192                 f += df;
1193                 color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1194                 color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1195                 color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1196                 color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1197             }
1199             // Now handle the beginning
1200             // The contribution of the last point is already in remainder.
1201             // The contribution of this point is:
1202             //    (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1203             //  = (ob+.5-o0)*c((o0+ob+.5)/2)
1204             //  = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1205             //  = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1206             double dt = ob+.5-o0;
1207             f = 0.5*dt*df;
1208             if (ob==0 && spread==SP_GRADIENT_SPREAD_REFLECT) {
1209                 // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1210                 color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1211                 color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1212                 color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1213                 color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1214             } else if (ob==0 && spread==SP_GRADIENT_SPREAD_REPEAT) {
1215                 // 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.
1216                 remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1217                 remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1218                 remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1219                 remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1220             } else {
1221                 // The first half of the cell was already in remainder.
1222                 color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1223                 color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1224                 color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1225                 color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1226             }
1228             // Now handle the end, which should end up in remainder
1229             // The contribution of this point is:
1230             //    (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1231             //  = (o1-oe+.5)*c((o1+oe-.5)/2)
1232             //  = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1233             dt = o1-oe+.5;
1234             f = (0.5*(o1+oe-.5)-o0)*df;
1235             remainder[0] = dt*(r0 + f*(r1-r0));
1236             remainder[1] = dt*(g0 + f*(g1-g0));
1237             remainder[2] = dt*(b0 + f*(b1-b0));
1238             remainder[3] = dt*(a0 + f*(a1-a0));
1239         }
1240     }
1241     switch(spread) {
1242     case SP_GRADIENT_SPREAD_PAD:
1243         color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1244         color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1245         color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1246         color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1247         break;
1248     case SP_GRADIENT_SPREAD_REFLECT:
1249         // The second half is the same as the first half, so multiply by 2.
1250         color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1251         color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1252         color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1253         color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1254         break;
1255     case SP_GRADIENT_SPREAD_REPEAT:
1256         // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1257         color[0] = color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1258         color[1] = color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1259         color[2] = color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1260         color[3] = color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1261         break;
1262     }
1265 /**
1266  * Renders gradient vector to buffer as line.
1267  *
1268  * RGB buffer background should be set up beforehand.
1269  *
1270  * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1271  * @param span Full integer width of requested gradient.
1272  * @param pos Buffer starting position in span.
1273  */
1274 static void
1275 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1276                                     gint const len, gint const pos, gint const span)
1278     g_return_if_fail(gradient != NULL);
1279     g_return_if_fail(SP_IS_GRADIENT(gradient));
1280     g_return_if_fail(buf != NULL);
1281     g_return_if_fail(len > 0);
1282     g_return_if_fail(pos >= 0);
1283     g_return_if_fail(pos + len <= span);
1284     g_return_if_fail(span > 0);
1286     if (!gradient->color) {
1287         gradient->ensureColors();
1288     }
1290     gint idx = (pos * 1024 << 8) / span;
1291     gint didx = (1024 << 8) / span;
1293     for (gint x = 0; x < len; x++) {
1294         /// \todo Can this be done with 4 byte copies?
1295         *buf++ = gradient->color[4 * (idx >> 8)];
1296         *buf++ = gradient->color[4 * (idx >> 8) + 1];
1297         *buf++ = gradient->color[4 * (idx >> 8) + 2];
1298         *buf++ = gradient->color[4 * (idx >> 8) + 3];
1299         idx += didx;
1300     }
1303 /**
1304  * Render rectangular RGBA area from gradient vector.
1305  */
1306 void
1307 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1308                                      gint const width, gint const height, gint const rowstride,
1309                                      gint const pos, gint const span, bool const horizontal)
1311     g_return_if_fail(gradient != NULL);
1312     g_return_if_fail(SP_IS_GRADIENT(gradient));
1313     g_return_if_fail(buf != NULL);
1314     g_return_if_fail(width > 0);
1315     g_return_if_fail(height > 0);
1316     g_return_if_fail(pos >= 0);
1317     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1318     g_return_if_fail(span > 0);
1320     if (horizontal) {
1321         sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1322         for (gint y = 1; y < height; y++) {
1323             memcpy(buf + y * rowstride, buf, 4 * width);
1324         }
1325     } else {
1326         guchar *tmp = (guchar *)alloca(4 * height);
1327         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1328         for (gint y = 0; y < height; y++) {
1329             guchar *b = buf + y * rowstride;
1330             for (gint x = 0; x < width; x++) {
1331                 *b++ = tmp[0];
1332                 *b++ = tmp[1];
1333                 *b++ = tmp[2];
1334                 *b++ = tmp[3];
1335             }
1336             tmp += 4;
1337         }
1338     }
1341 /**
1342  * Render rectangular RGB area from gradient vector.
1343  */
1344 void
1345 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1346                                     gint const width, gint const height, gint const /*rowstride*/,
1347                                     gint const pos, gint const span, bool const horizontal)
1349     g_return_if_fail(gradient != NULL);
1350     g_return_if_fail(SP_IS_GRADIENT(gradient));
1351     g_return_if_fail(buf != NULL);
1352     g_return_if_fail(width > 0);
1353     g_return_if_fail(height > 0);
1354     g_return_if_fail(pos >= 0);
1355     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1356     g_return_if_fail(span > 0);
1358     if (horizontal) {
1359         guchar *tmp = (guchar*)alloca(4 * width);
1360         sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1361         for (gint y = 0; y < height; y++) {
1362             guchar *t = tmp;
1363             for (gint x = 0; x < width; x++) {
1364                 gint a = t[3];
1365                 gint fc = (t[0] - buf[0]) * a;
1366                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1367                 fc = (t[1] - buf[1]) * a;
1368                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1369                 fc = (t[2] - buf[2]) * a;
1370                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1371                 buf += 3;
1372                 t += 4;
1373             }
1374         }
1375     } else {
1376         guchar *tmp = (guchar*)alloca(4 * height);
1377         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1378         for (gint y = 0; y < height; y++) {
1379             guchar *t = tmp + 4 * y;
1380             for (gint x = 0; x < width; x++) {
1381                 gint a = t[3];
1382                 gint fc = (t[0] - buf[0]) * a;
1383                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1384                 fc = (t[1] - buf[1]) * a;
1385                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1386                 fc = (t[2] - buf[2]) * a;
1387                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1388             }
1389         }
1390     }
1393 Geom::Matrix
1394 sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1396     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1397         return ( Geom::Scale(bbox.dimensions())
1398                  * Geom::Translate(bbox.min())
1399                  * Geom::Matrix(ctm) );
1400     } else {
1401         return ctm;
1402     }
1405 Geom::Matrix
1406 sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1408     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1409         return ( gr->gradientTransform
1410                  * Geom::Scale(bbox.dimensions())
1411                  * Geom::Translate(bbox.min())
1412                  * Geom::Matrix(ctm) );
1413     } else {
1414         return gr->gradientTransform * ctm;
1415     }
1418 void
1419 sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1420                             Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1422     gr->gradientTransform = gs2d * ctm.inverse();
1423     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1424         gr->gradientTransform = ( gr->gradientTransform
1425                                   * Geom::Translate(-bbox.min())
1426                                   * Geom::Scale(bbox.dimensions()).inverse() );
1427     }
1428     gr->gradientTransform_set = TRUE;
1430     SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1433 /*
1434  * Linear Gradient
1435  */
1437 class SPLGPainter;
1439 /// A context with linear gradient, painter, and gradient renderer.
1440 struct SPLGPainter {
1441     SPPainter painter;
1442     SPLinearGradient *lg;
1444     NRLGradientRenderer lgr;
1446     static SPPainter * painter_new(SPPaintServer *ps,
1447                                    Geom::Matrix const &full_transform,
1448                                    Geom::Matrix const &parent_transform,
1449                                    NRRect const *bbox);
1450 };
1452 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1453 static void sp_lineargradient_init(SPLinearGradient *lg);
1455 static void sp_lineargradient_build(SPObject *object,
1456                                     SPDocument *document,
1457                                     Inkscape::XML::Node *repr);
1458 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1459 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1460                                                     guint flags);
1462 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1464 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1466 static SPGradientClass *lg_parent_class;
1468 /**
1469  * Register SPLinearGradient class and return its type.
1470  */
1471 GType
1472 sp_lineargradient_get_type()
1474     static GType type = 0;
1475     if (!type) {
1476         GTypeInfo info = {
1477             sizeof(SPLinearGradientClass),
1478             NULL, NULL,
1479             (GClassInitFunc) sp_lineargradient_class_init,
1480             NULL, NULL,
1481             sizeof(SPLinearGradient),
1482             16,
1483             (GInstanceInitFunc) sp_lineargradient_init,
1484             NULL,   /* value_table */
1485         };
1486         type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1487     }
1488     return type;
1491 /**
1492  * SPLinearGradient vtable initialization.
1493  */
1494 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1496     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1497     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1499     lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1501     sp_object_class->build = sp_lineargradient_build;
1502     sp_object_class->set = sp_lineargradient_set;
1503     sp_object_class->write = sp_lineargradient_write;
1505     ps_class->painter_new = SPLGPainter::painter_new;
1506     ps_class->painter_free = sp_lineargradient_painter_free;
1509 /**
1510  * Callback for SPLinearGradient object initialization.
1511  */
1512 static void sp_lineargradient_init(SPLinearGradient *lg)
1514     lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1515     lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
1516     lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1517     lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
1520 /**
1521  * Callback: set attributes from associated repr.
1522  */
1523 static void sp_lineargradient_build(SPObject *object,
1524                                     SPDocument *document,
1525                                     Inkscape::XML::Node *repr)
1527     if (((SPObjectClass *) lg_parent_class)->build)
1528         (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1530     object->readAttr( "x1" );
1531     object->readAttr( "y1" );
1532     object->readAttr( "x2" );
1533     object->readAttr( "y2" );
1536 /**
1537  * Callback: set attribute.
1538  */
1539 static void
1540 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1542     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1544     switch (key) {
1545         case SP_ATTR_X1:
1546             lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1547             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1548             break;
1549         case SP_ATTR_Y1:
1550             lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1551             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1552             break;
1553         case SP_ATTR_X2:
1554             lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1555             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1556             break;
1557         case SP_ATTR_Y2:
1558             lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1559             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1560             break;
1561         default:
1562             if (((SPObjectClass *) lg_parent_class)->set)
1563                 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1564             break;
1565     }
1568 /**
1569  * Callback: write attributes to associated repr.
1570  */
1571 static Inkscape::XML::Node *
1572 sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1574     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1576     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1577         repr = xml_doc->createElement("svg:linearGradient");
1578     }
1580     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1581         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1582     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1583         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1584     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1585         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1586     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1587         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1589     if (((SPObjectClass *) lg_parent_class)->write)
1590         (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
1592     return repr;
1595 /**
1596  * Create linear gradient context.
1597  *
1598  * Basically we have to deal with transformations
1599  *
1600  * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1601  * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1602  * 2) gradientTransform
1603  * 3) bbox2user
1604  * 4) ctm == userspace to pixel grid
1605  *
1606  * See also (*) in sp-pattern about why we may need parent_transform.
1607  *
1608  * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1609  * and end < 1.
1610  */
1611 SPPainter * SPLGPainter::painter_new(SPPaintServer *ps,
1612                                      Geom::Matrix const &full_transform,
1613                                      Geom::Matrix const &/*parent_transform*/,
1614                                      NRRect const *bbox)
1616     SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1617     SPGradient *gr = SP_GRADIENT(ps);
1619     if (!gr->color) {
1620         gr->ensureColors();
1621     }
1623     SPLGPainter *lgp = g_new(SPLGPainter, 1);
1625     lgp->painter.type = SP_PAINTER_IND;
1626     lgp->painter.fill = sp_lg_fill;
1628     lgp->lg = lg;
1630     /** \todo
1631      * Technically speaking, we map NCOLORS on line [start,end] onto line
1632      * [0,1].  I almost think we should fill color array start and end in
1633      * that case. The alternative would be to leave these just empty garbage
1634      * or something similar. Originally I had 1023.9999 here - not sure
1635      * whether we have really to cut out ceil int (Lauris).
1636      */
1637     Geom::Matrix color2norm(Geom::identity());
1638     Geom::Matrix color2px;
1639     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1640         Geom::Matrix norm2pos(Geom::identity());
1642         /* BBox to user coordinate system */
1643         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1645         Geom::Matrix color2pos = color2norm * norm2pos;
1646         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1647         Geom::Matrix color2user = color2tpos * bbox2user;
1648         color2px = color2user * full_transform;
1650     } else {
1651         /* Problem: What to do, if we have mixed lengths and percentages? */
1652         /* Currently we do ignore percentages at all, but that is not good (lauris) */
1654         Geom::Matrix norm2pos(Geom::identity());
1655         Geom::Matrix color2pos = color2norm * norm2pos;
1656         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1657         color2px = color2tpos * full_transform;
1659     }
1660     // TODO: remove color2px_nr after converting to 2geom
1661     NR::Matrix color2px_nr = from_2geom(color2px);
1662     nr_lgradient_renderer_setup(&lgp->lgr, gr->color, gr->fetchSpread(), &color2px_nr,
1663                                 lg->x1.computed, lg->y1.computed,
1664                                 lg->x2.computed, lg->y2.computed);
1666     return (SPPainter *) lgp;
1669 static void
1670 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1672     g_free(painter);
1675 /**
1676  * Directly set properties of linear gradient and request modified.
1677  */
1678 void
1679 sp_lineargradient_set_position(SPLinearGradient *lg,
1680                                gdouble x1, gdouble y1,
1681                                gdouble x2, gdouble y2)
1683     g_return_if_fail(lg != NULL);
1684     g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1686     /* fixme: units? (Lauris)  */
1687     lg->x1.set(SVGLength::NONE, x1, x1);
1688     lg->y1.set(SVGLength::NONE, y1, y1);
1689     lg->x2.set(SVGLength::NONE, x2, x2);
1690     lg->y2.set(SVGLength::NONE, y2, y2);
1692     SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1695 /**
1696  * Callback when linear gradient object is rendered.
1697  */
1698 static void
1699 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1701     SPLGPainter *lgp = (SPLGPainter *) painter;
1703     if (lgp->lg->color == NULL) {
1704         lgp->lg->ensureColors();
1705         lgp->lgr.vector = lgp->lg->color;
1706     }
1708     nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1711 /*
1712  * Radial Gradient
1713  */
1715 class SPRGPainter;
1717 /// A context with radial gradient, painter, and gradient renderer.
1718 struct SPRGPainter {
1719     SPPainter painter;
1720     SPRadialGradient *rg;
1721     NRRGradientRenderer rgr;
1723     static SPPainter *painter_new(SPPaintServer *ps,
1724                                   Geom::Matrix const &full_transform,
1725                                   Geom::Matrix const &parent_transform,
1726                                   NRRect const *bbox);
1727 };
1729 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1730 static void sp_radialgradient_init(SPRadialGradient *rg);
1732 static void sp_radialgradient_build(SPObject *object,
1733                                     SPDocument *document,
1734                                     Inkscape::XML::Node *repr);
1735 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1736 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1737                                                     guint flags);
1738 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1740 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1742 static SPGradientClass *rg_parent_class;
1744 /**
1745  * Register SPRadialGradient class and return its type.
1746  */
1747 GType
1748 sp_radialgradient_get_type()
1750     static GType type = 0;
1751     if (!type) {
1752         GTypeInfo info = {
1753             sizeof(SPRadialGradientClass),
1754             NULL, NULL,
1755             (GClassInitFunc) sp_radialgradient_class_init,
1756             NULL, NULL,
1757             sizeof(SPRadialGradient),
1758             16,
1759             (GInstanceInitFunc) sp_radialgradient_init,
1760             NULL,   /* value_table */
1761         };
1762         type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1763     }
1764     return type;
1767 /**
1768  * SPRadialGradient vtable initialization.
1769  */
1770 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1772     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1773     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1775     rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1777     sp_object_class->build = sp_radialgradient_build;
1778     sp_object_class->set = sp_radialgradient_set;
1779     sp_object_class->write = sp_radialgradient_write;
1781     ps_class->painter_new = SPRGPainter::painter_new;
1782     ps_class->painter_free = sp_radialgradient_painter_free;
1785 /**
1786  * Callback for SPRadialGradient object initialization.
1787  */
1788 static void
1789 sp_radialgradient_init(SPRadialGradient *rg)
1791     rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1792     rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1793     rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1794     rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1795     rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1798 /**
1799  * Set radial gradient attributes from associated repr.
1800  */
1801 static void
1802 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1804     if (((SPObjectClass *) rg_parent_class)->build)
1805         (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1807     object->readAttr( "cx" );
1808     object->readAttr( "cy" );
1809     object->readAttr( "r" );
1810     object->readAttr( "fx" );
1811     object->readAttr( "fy" );
1814 /**
1815  * Set radial gradient attribute.
1816  */
1817 static void
1818 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1820     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1822     switch (key) {
1823         case SP_ATTR_CX:
1824             if (!rg->cx.read(value)) {
1825                 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1826             }
1827             if (!rg->fx._set) {
1828                 rg->fx.value = rg->cx.value;
1829                 rg->fx.computed = rg->cx.computed;
1830             }
1831             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1832             break;
1833         case SP_ATTR_CY:
1834             if (!rg->cy.read(value)) {
1835                 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1836             }
1837             if (!rg->fy._set) {
1838                 rg->fy.value = rg->cy.value;
1839                 rg->fy.computed = rg->cy.computed;
1840             }
1841             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1842             break;
1843         case SP_ATTR_R:
1844             if (!rg->r.read(value)) {
1845                 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1846             }
1847             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1848             break;
1849         case SP_ATTR_FX:
1850             if (!rg->fx.read(value)) {
1851                 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1852             }
1853             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1854             break;
1855         case SP_ATTR_FY:
1856             if (!rg->fy.read(value)) {
1857                 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1858             }
1859             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1860             break;
1861         default:
1862             if (((SPObjectClass *) rg_parent_class)->set)
1863                 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1864             break;
1865     }
1868 /**
1869  * Write radial gradient attributes to associated repr.
1870  */
1871 static Inkscape::XML::Node *
1872 sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1874     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1876     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1877         repr = xml_doc->createElement("svg:radialGradient");
1878     }
1880     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1881     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1882     if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1883     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1884     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1886     if (((SPObjectClass *) rg_parent_class)->write)
1887         (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1889     return repr;
1892 /**
1893  * Create radial gradient context.
1894  */
1895 SPPainter *SPRGPainter::painter_new(SPPaintServer *ps,
1896                                     Geom::Matrix const &full_transform,
1897                                     Geom::Matrix const &/*parent_transform*/,
1898                                     NRRect const *bbox)
1900     SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1901     SPGradient *gr = SP_GRADIENT(ps);
1903     if (!gr->color) {
1904         gr->ensureColors();
1905     }
1907     SPRGPainter *rgp = g_new(SPRGPainter, 1);
1909     rgp->painter.type = SP_PAINTER_IND;
1910     rgp->painter.fill = sp_rg_fill;
1912     rgp->rg = rg;
1914     Geom::Matrix gs2px;
1916     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1917         /** \todo
1918          * fixme: We may try to normalize here too, look at
1919          * linearGradient (Lauris)
1920          */
1922         /* BBox to user coordinate system */
1923         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1925         Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1927         gs2px = gs2user * full_transform;
1928     } else {
1929         /** \todo
1930          * Problem: What to do, if we have mixed lengths and percentages?
1931          * Currently we do ignore percentages at all, but that is not
1932          * good (lauris)
1933          */
1935         gs2px = gr->gradientTransform * full_transform;
1936     }
1937     // TODO: remove gs2px_nr after converting to 2geom
1938     NR::Matrix gs2px_nr = from_2geom(gs2px);
1939     nr_rgradient_renderer_setup(&rgp->rgr, gr->color, gr->fetchSpread(),
1940                                 &gs2px_nr,
1941                                 rg->cx.computed, rg->cy.computed,
1942                                 rg->fx.computed, rg->fy.computed,
1943                                 rg->r.computed);
1945     return (SPPainter *) rgp;
1948 static void
1949 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1951     g_free(painter);
1954 /**
1955  * Directly set properties of radial gradient and request modified.
1956  */
1957 void
1958 sp_radialgradient_set_position(SPRadialGradient *rg,
1959                                gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1961     g_return_if_fail(rg != NULL);
1962     g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1964     /* fixme: units? (Lauris)  */
1965     rg->cx.set(SVGLength::NONE, cx, cx);
1966     rg->cy.set(SVGLength::NONE, cy, cy);
1967     rg->fx.set(SVGLength::NONE, fx, fx);
1968     rg->fy.set(SVGLength::NONE, fy, fy);
1969     rg->r.set(SVGLength::NONE, r, r);
1971     SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1974 /**
1975  * Callback when radial gradient object is rendered.
1976  */
1977 static void
1978 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1980     SPRGPainter *rgp = (SPRGPainter *) painter;
1982     if (rgp->rg->color == NULL) {
1983         rgp->rg->ensureColors();
1984         rgp->rgr.vector = rgp->rg->color;
1985     }
1987     nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1990 /*
1991   Local Variables:
1992   mode:c++
1993   c-file-style:"stroustrup"
1994   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1995   indent-tabs-mode:nil
1996   fill-column:99
1997   End:
1998 */
1999 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :