Code

Pot and Dutch translation update
[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  *
10  * Copyright (C) 1999-2002 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2004 David Turner
13  * Copyright (C) 2009 Jasper van de Gronde
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  *
17  */
19 #define noSP_GRADIENT_VERBOSE
21 #include <cstring>
22 #include <string>
24 #include <libnr/nr-matrix-fns.h>
25 #include <libnr/nr-matrix-ops.h>
26 #include <libnr/nr-matrix-scale-ops.h>
27 #include <2geom/transforms.h>
29 #include <sigc++/functors/ptr_fun.h>
30 #include <sigc++/adaptors/bind.h>
32 #include "libnr/nr-gradient.h"
33 #include "libnr/nr-pixops.h"
34 #include "svg/svg.h"
35 #include "svg/svg-color.h"
36 #include "svg/css-ostringstream.h"
37 #include "attributes.h"
38 #include "document-private.h"
39 #include "sp-gradient.h"
40 #include "gradient-chemistry.h"
41 #include "sp-gradient-reference.h"
42 #include "sp-linear-gradient.h"
43 #include "sp-radial-gradient.h"
44 #include "sp-stop.h"
45 #include "streq.h"
46 #include "uri.h"
47 #include "xml/repr.h"
48 #include "style.h"
50 #define SP_MACROS_SILENT
51 #include "macros.h"
53 /// Has to be power of 2
54 #define NCOLORS NR_GRADIENT_VECTOR_LENGTH
56 static void sp_stop_class_init(SPStopClass *klass);
57 static void sp_stop_init(SPStop *stop);
59 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
60 static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
61 static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
63 static SPObjectClass *stop_parent_class;
65 class SPGradientImpl
66 {
67     friend class SPGradient;
69     static void classInit(SPGradientClass *klass);
71     static void init(SPGradient *gr);
72     static void build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
73     static void release(SPObject *object);
74     static void modified(SPObject *object, guint flags);
75     static Inkscape::XML::Node *write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags);
77     static void gradientRefModified(SPObject *href, guint flags, SPGradient *gradient);
78     static void gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr);
80     static void childAdded(SPObject *object,
81                            Inkscape::XML::Node *child,
82                            Inkscape::XML::Node *ref);
83     static void removeChild(SPObject *object, Inkscape::XML::Node *child);
85     static void setGradientAttr(SPObject *object, unsigned key, gchar const *value);
86 };
88 /**
89  * Registers SPStop class and returns its type.
90  */
91 GType
92 sp_stop_get_type()
93 {
94     static GType type = 0;
95     if (!type) {
96         GTypeInfo info = {
97             sizeof(SPStopClass),
98             NULL, NULL,
99             (GClassInitFunc) sp_stop_class_init,
100             NULL, NULL,
101             sizeof(SPStop),
102             16,
103             (GInstanceInitFunc) sp_stop_init,
104             NULL,   /* value_table */
105         };
106         type = g_type_register_static(SP_TYPE_OBJECT, "SPStop", &info, (GTypeFlags)0);
107     }
108     return type;
111 /**
112  * Callback to initialize SPStop vtable.
113  */
114 static void sp_stop_class_init(SPStopClass *klass)
116     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
118     stop_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
120     sp_object_class->build = sp_stop_build;
121     sp_object_class->set = sp_stop_set;
122     sp_object_class->write = sp_stop_write;
125 /**
126  * Callback to initialize SPStop object.
127  */
128 static void
129 sp_stop_init(SPStop *stop)
131     stop->offset = 0.0;
132     stop->currentColor = false;
133     stop->specified_color.set( 0x000000ff );
134     stop->opacity = 1.0;
137 /**
138  * Virtual build: set stop attributes from its associated XML node.
139  */
140 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
142     if (((SPObjectClass *) stop_parent_class)->build)
143         (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
145     sp_object_read_attr(object, "offset");
146     sp_object_read_attr(object, "stop-color");
147     sp_object_read_attr(object, "stop-opacity");
148     sp_object_read_attr(object, "style");
151 /**
152  * Virtual set: set attribute to value.
153  */
154 static void
155 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
157     SPStop *stop = SP_STOP(object);
159     switch (key) {
160         case SP_ATTR_STYLE: {
161         /** \todo
162          * fixme: We are reading simple values 3 times during build (Lauris).
163          * \par
164          * We need presentation attributes etc.
165          * \par
166          * remove the hackish "style reading" from here: see comments in
167          * sp_object_get_style_property about the bugs in our current
168          * approach.  However, note that SPStyle doesn't currently have
169          * stop-color and stop-opacity properties.
170          */
171             {
172                 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
173                 if (streq(p, "currentColor")) {
174                     stop->currentColor = true;
175                 } else {
176                     stop->specified_color = SPStop::readStopColor( p );
177                 }
178             }
179             {
180                 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
181                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
182                 stop->opacity = opacity;
183             }
184             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
185             break;
186         }
187         case SP_PROP_STOP_COLOR: {
188             {
189                 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
190                 if (streq(p, "currentColor")) {
191                     stop->currentColor = true;
192                 } else {
193                     stop->currentColor = false;
194                     stop->specified_color = SPStop::readStopColor( p );
195                 }
196             }
197             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
198             break;
199         }
200         case SP_PROP_STOP_OPACITY: {
201             {
202                 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
203                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
204                 stop->opacity = opacity;
205             }
206             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
207             break;
208         }
209         case SP_ATTR_OFFSET: {
210             stop->offset = sp_svg_read_percentage(value, 0.0);
211             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
212             break;
213         }
214         default: {
215             if (((SPObjectClass *) stop_parent_class)->set)
216                 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
217             break;
218         }
219     }
222 /**
223  * Virtual write: write object attributes to repr.
224  */
225 static Inkscape::XML::Node *
226 sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
228     SPStop *stop = SP_STOP(object);
230     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
231         repr = xml_doc->createElement("svg:stop");
232     }
234     Glib::ustring colorStr = stop->specified_color.toString();
235     gfloat opacity = stop->opacity;
237     if (((SPObjectClass *) stop_parent_class)->write) {
238         (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
239     }
241     // Since we do a hackish style setting here (because SPStyle does not support stop-color and
242     // stop-opacity), we must do it AFTER calling the parent write method; otherwise
243     // sp_object_write would clear our style= attribute (bug 1695287)
245     Inkscape::CSSOStringStream os;
246     os << "stop-color:";
247     if (stop->currentColor) {
248         os << "currentColor";
249     } else {
250         os << colorStr;
251     }
252     os << ";stop-opacity:" << opacity;
253     repr->setAttribute("style", os.str().c_str());
254     repr->setAttribute("stop-color", NULL);
255     repr->setAttribute("stop-opacity", NULL);
256     sp_repr_set_css_double(repr, "offset", stop->offset);
257     /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no sense
258      * for offset proportions. */
260     return repr;
264 bool SPGradient::hasStops() const
266     return has_stops;
269 bool SPGradient::isUnitsSet() const
271     return units_set;
274 SPGradientUnits SPGradient::getUnits() const
276     return units;
279 bool SPGradient::isSpreadSet() const
281     return spread_set;
284 SPGradientSpread SPGradient::getSpread() const
286     return spread;
289 void SPGradient::setSwatch( bool swatch )
291     if ( swatch != isSwatch() ) {
292         this->swatch = swatch; // to make isSolid() work, this happens first
293         gchar const* paintVal = swatch ? (isSolid() ? "solid" : "gradient") : 0;
294         sp_object_setAttribute( this, "osb:paint", paintVal, 0 );
296         requestModified( SP_OBJECT_MODIFIED_FLAG );
297     }
300 /**
301  * Return stop's color as 32bit value.
302  */
303 guint32
304 sp_stop_get_rgba32(SPStop const *const stop)
306     guint32 rgb0 = 0;
307     /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
308      * value depends on user agent, and don't give any further restrictions that I can
309      * see.) */
310     if (stop->currentColor) {
311         char const *str = sp_object_get_style_property(stop, "color", NULL);
312         if (str) {
313             rgb0 = sp_svg_read_color(str, rgb0);
314         }
315         unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
316         g_return_val_if_fail((alpha & ~0xff) == 0,
317                              rgb0 | 0xff);
318         return rgb0 | alpha;
319     } else {
320         return stop->specified_color.toRGBA32( stop->opacity );
321     }
324 /*
325  * Gradient
326  */
328 static SPPaintServerClass *gradient_parent_class;
330 /**
331  * Registers SPGradient class and returns its type.
332  */
333 GType SPGradient::getType()
335     static GType gradient_type = 0;
336     if (!gradient_type) {
337         GTypeInfo gradient_info = {
338             sizeof(SPGradientClass),
339             NULL, NULL,
340             (GClassInitFunc) SPGradientImpl::classInit,
341             NULL, NULL,
342             sizeof(SPGradient),
343             16,
344             (GInstanceInitFunc) SPGradientImpl::init,
345             NULL,   /* value_table */
346         };
347         gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
348                                                &gradient_info, (GTypeFlags)0);
349     }
350     return gradient_type;
353 /**
354  * SPGradient vtable initialization.
355  */
356 void SPGradientImpl::classInit(SPGradientClass *klass)
358     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
360     gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
362     sp_object_class->build = SPGradientImpl::build;
363     sp_object_class->release = SPGradientImpl::release;
364     sp_object_class->set = SPGradientImpl::setGradientAttr;
365     sp_object_class->child_added = SPGradientImpl::childAdded;
366     sp_object_class->remove_child = SPGradientImpl::removeChild;
367     sp_object_class->modified = SPGradientImpl::modified;
368     sp_object_class->write = SPGradientImpl::write;
371 /**
372  * Callback for SPGradient object initialization.
373  */
374 void SPGradientImpl::init(SPGradient *gr)
376     gr->ref = new SPGradientReference(SP_OBJECT(gr));
377     gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(SPGradientImpl::gradientRefChanged), gr));
379     /** \todo
380      * Fixme: reprs being rearranged (e.g. via the XML editor)
381      * may require us to clear the state.
382      */
383     gr->state = SP_GRADIENT_STATE_UNKNOWN;
385     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
386     gr->units_set = FALSE;
388     gr->gradientTransform = Geom::identity();
389     gr->gradientTransform_set = FALSE;
391     gr->spread = SP_GRADIENT_SPREAD_PAD;
392     gr->spread_set = FALSE;
394     gr->has_stops = FALSE;
396     gr->vector.built = false;
397     gr->vector.stops.clear();
399     gr->color = NULL;
401     new (&gr->modified_connection) sigc::connection();
404 /**
405  * Virtual build: set gradient attributes from its associated repr.
406  */
407 void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
409     SPGradient *gradient = SP_GRADIENT(object);
411     // Work-around in case a swatch had been marked for immediate collection:
412     if ( repr->attribute("osb:paint") && repr->attribute("inkscape:collect") ) {
413         repr->setAttribute("inkscape:collect", 0);
414     }
416     if (((SPObjectClass *) gradient_parent_class)->build) {
417         (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
418     }
420     for ( SPObject *ochild = sp_object_first_child(object); ochild; ochild = ochild->next ) {
421         if (SP_IS_STOP(ochild)) {
422             gradient->has_stops = TRUE;
423             break;
424         }
425     }
427     sp_object_read_attr(object, "gradientUnits");
428     sp_object_read_attr(object, "gradientTransform");
429     sp_object_read_attr(object, "spreadMethod");
430     sp_object_read_attr(object, "xlink:href");
431     sp_object_read_attr(object, "osb:paint");
433     /* Register ourselves */
434     sp_document_add_resource(document, "gradient", object);
437 /**
438  * Virtual release of SPGradient members before destruction.
439  */
440 void SPGradientImpl::release(SPObject *object)
442     SPGradient *gradient = (SPGradient *) object;
444 #ifdef SP_GRADIENT_VERBOSE
445     g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
446 #endif
448     if (SP_OBJECT_DOCUMENT(object)) {
449         /* Unregister ourselves */
450         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
451     }
453     if (gradient->ref) {
454         gradient->modified_connection.disconnect();
455         gradient->ref->detach();
456         delete gradient->ref;
457         gradient->ref = NULL;
458     }
460     if (gradient->color) {
461         g_free(gradient->color);
462         gradient->color = NULL;
463     }
465     gradient->modified_connection.~connection();
467     if (((SPObjectClass *) gradient_parent_class)->release)
468         ((SPObjectClass *) gradient_parent_class)->release(object);
471 /**
472  * Set gradient attribute to value.
473  */
474 void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const *value)
476     SPGradient *gr = SP_GRADIENT(object);
478     switch (key) {
479         case SP_ATTR_GRADIENTUNITS:
480             if (value) {
481                 if (!strcmp(value, "userSpaceOnUse")) {
482                     gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
483                 } else {
484                     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
485                 }
486                 gr->units_set = TRUE;
487             } else {
488                 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
489                 gr->units_set = FALSE;
490             }
491             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
492             break;
493         case SP_ATTR_GRADIENTTRANSFORM: {
494             Geom::Matrix t;
495             if (value && sp_svg_transform_read(value, &t)) {
496                 gr->gradientTransform = t;
497                 gr->gradientTransform_set = TRUE;
498             } else {
499                 gr->gradientTransform = Geom::identity();
500                 gr->gradientTransform_set = FALSE;
501             }
502             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
503             break;
504         }
505         case SP_ATTR_SPREADMETHOD:
506             if (value) {
507                 if (!strcmp(value, "reflect")) {
508                     gr->spread = SP_GRADIENT_SPREAD_REFLECT;
509                 } else if (!strcmp(value, "repeat")) {
510                     gr->spread = SP_GRADIENT_SPREAD_REPEAT;
511                 } else {
512                     gr->spread = SP_GRADIENT_SPREAD_PAD;
513                 }
514                 gr->spread_set = TRUE;
515             } else {
516                 gr->spread_set = FALSE;
517             }
518             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
519             break;
520         case SP_ATTR_XLINK_HREF:
521             if (value) {
522                 try {
523                     gr->ref->attach(Inkscape::URI(value));
524                 } catch (Inkscape::BadURIException &e) {
525                     g_warning("%s", e.what());
526                     gr->ref->detach();
527                 }
528             } else {
529                 gr->ref->detach();
530             }
531             break;
532         case SP_ATTR_OSB_SWATCH:
533         {
534             bool newVal = (value != 0);
535             bool modified = false;
536             if (newVal != gr->swatch) {
537                 gr->swatch = newVal;
538                 modified = true;
539             }
540             if (newVal) {
541                 // Might need to flip solid/gradient
542                 Glib::ustring paintVal = ( gr->hasStops() && (gr->getStopCount() == 0) ) ? "solid" : "gradient";
543                 if ( paintVal != value ) {
544                     sp_object_setAttribute( gr, "osb:paint", paintVal.c_str(), 0 );
545                     modified = true;
546                 }
547             }
548             if (modified) {
549                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
550             }
551         }
552             break;
553         default:
554             if (((SPObjectClass *) gradient_parent_class)->set) {
555                 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
556             }
557             break;
558     }
561 /**
562  * Gets called when the gradient is (re)attached to another gradient.
563  */
564 void SPGradientImpl::gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr)
566     if (old_ref) {
567         gr->modified_connection.disconnect();
568     }
569     if ( SP_IS_GRADIENT(ref)
570          && ref != gr )
571     {
572         gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&SPGradientImpl::gradientRefModified), gr));
573     }
575     // Per SVG, all unset attributes must be inherited from linked gradient.
576     // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
577     // but without setting the _set flags.
578     // FIXME: do the same for gradientTransform too
579     if (!gr->units_set) {
580         gr->units = gr->fetchUnits();
581     }
582     if (!gr->spread_set) {
583         gr->spread = gr->fetchSpread();
584     }
586     /// \todo Fixme: what should the flags (second) argument be? */
587     gradientRefModified(ref, 0, gr);
590 /**
591  * Callback for child_added event.
592  */
593 void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
595     SPGradient *gr = SP_GRADIENT(object);
597     gr->invalidateVector();
599     if (((SPObjectClass *) gradient_parent_class)->child_added) {
600         (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
601     }
603     SPObject *ochild = sp_object_get_child_by_repr(object, child);
604     if ( ochild && SP_IS_STOP(ochild) ) {
605         gr->has_stops = TRUE;
606         if ( gr->getStopCount() > 0 ) {
607             gchar const * attr = gr->repr->attribute("osb:paint");
608             if ( attr && strcmp(attr, "gradient") ) {
609                 sp_object_setAttribute( gr, "osb:paint", "gradient", 0 );
610             }
611         }
612     }
614     /// \todo Fixme: should we schedule "modified" here?
615     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
618 /**
619  * Callback for remove_child event.
620  */
621 void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
623     SPGradient *gr = SP_GRADIENT(object);
625     gr->invalidateVector();
627     if (((SPObjectClass *) gradient_parent_class)->remove_child) {
628         (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
629     }
631     gr->has_stops = FALSE;
632     SPObject *ochild;
633     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
634         if (SP_IS_STOP(ochild)) {
635             gr->has_stops = TRUE;
636             break;
637         }
638     }
640     if ( gr->getStopCount() == 0 ) {
641         gchar const * attr = gr->repr->attribute("osb:paint");
642         if ( attr && strcmp(attr, "solid") ) {
643             sp_object_setAttribute( gr, "osb:paint", "solid", 0 );
644         }
645     }
647     /* Fixme: should we schedule "modified" here? */
648     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
651 /**
652  * Callback for modified event.
653  */
654 void SPGradientImpl::modified(SPObject *object, guint flags)
656     SPGradient *gr = SP_GRADIENT(object);
658     if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
659         gr->invalidateVector();
660     }
662     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
663         gr->ensureColors();
664     }
666     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
667     flags &= SP_OBJECT_MODIFIED_CASCADE;
669     // FIXME: climb up the ladder of hrefs
670     GSList *l = NULL;
671     for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
672         g_object_ref(G_OBJECT(child));
673         l = g_slist_prepend(l, child);
674     }
675     l = g_slist_reverse(l);
676     while (l) {
677         SPObject *child = SP_OBJECT(l->data);
678         l = g_slist_remove(l, child);
679         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
680             child->emitModified(flags);
681         }
682         g_object_unref(G_OBJECT(child));
683     }
686 SPStop* SPGradient::getFirstStop()
688     SPStop* first = 0;
689     for (SPObject *ochild = sp_object_first_child(this); ochild && !first; ochild = SP_OBJECT_NEXT(ochild)) {
690         if (SP_IS_STOP(ochild)) {
691             first = SP_STOP(ochild);
692         }
693     }
694     return first;
697 int SPGradient::getStopCount() const
699     int count = 0;
701     for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
702         count++;
703     }
705     return count;
708 /**
709  * Write gradient attributes to repr.
710  */
711 Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
713     SPGradient *gr = SP_GRADIENT(object);
715     if (((SPObjectClass *) gradient_parent_class)->write) {
716         (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
717     }
719     if (flags & SP_OBJECT_WRITE_BUILD) {
720         GSList *l = NULL;
721         for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
722             Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
723             if (crepr) {
724                 l = g_slist_prepend(l, crepr);
725             }
726         }
727         while (l) {
728             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
729             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
730             l = g_slist_remove(l, l->data);
731         }
732     }
734     if (gr->ref->getURI()) {
735         gchar *uri_string = gr->ref->getURI()->toString();
736         repr->setAttribute("xlink:href", uri_string);
737         g_free(uri_string);
738     }
740     if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
741         switch (gr->units) {
742             case SP_GRADIENT_UNITS_USERSPACEONUSE:
743                 repr->setAttribute("gradientUnits", "userSpaceOnUse");
744                 break;
745             default:
746                 repr->setAttribute("gradientUnits", "objectBoundingBox");
747                 break;
748         }
749     }
751     if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
752         gchar *c=sp_svg_transform_write(gr->gradientTransform);
753         repr->setAttribute("gradientTransform", c);
754         g_free(c);
755     }
757     if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
758         /* FIXME: Ensure that gr->spread is the inherited value
759          * if !gr->spread_set.  Not currently happening: see SPGradient::modified.
760          */
761         switch (gr->spread) {
762             case SP_GRADIENT_SPREAD_REFLECT:
763                 repr->setAttribute("spreadMethod", "reflect");
764                 break;
765             case SP_GRADIENT_SPREAD_REPEAT:
766                 repr->setAttribute("spreadMethod", "repeat");
767                 break;
768             default:
769                 repr->setAttribute("spreadMethod", "pad");
770                 break;
771         }
772     }
774     if ( (flags & SP_OBJECT_WRITE_EXT) && gr->isSwatch() ) {
775         if ( gr->isSolid() ) {
776             repr->setAttribute( "osb:paint", "solid" );
777         } else {
778             repr->setAttribute( "osb:paint", "gradient" );
779         }
780     } else {
781         repr->setAttribute( "osb:paint", 0 );
782     }
784     return repr;
787 /**
788  * Forces the vector to be built, if not present (i.e., changed).
789  *
790  * \pre SP_IS_GRADIENT(gradient).
791  */
792 void SPGradient::ensureVector()
794     if ( !vector.built ) {
795         rebuildVector();
796     }
799 /**
800  * Set units property of gradient and emit modified.
801  */
802 void SPGradient::setUnits(SPGradientUnits units)
804     if (units != this->units) {
805         this->units = units;
806         units_set = TRUE;
807         requestModified(SP_OBJECT_MODIFIED_FLAG);
808     }
811 /**
812  * Set spread property of gradient and emit modified.
813  */
814 void SPGradient::setSpread(SPGradientSpread spread)
816     if (spread != this->spread) {
817         this->spread = spread;
818         spread_set = TRUE;
819         requestModified(SP_OBJECT_MODIFIED_FLAG);
820     }
823 /**
824  * Returns the first of {src, src-\>ref-\>getObject(),
825  * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
826  * for which \a match is true, or NULL if none found.
827  *
828  * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
829  * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
830  *
831  * \pre SP_IS_GRADIENT(src).
832  */
833 static SPGradient *
834 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
836     g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
838     /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2.  If there is a
839        loop, then once p1 has entered the loop, we'll detect it the next time the distance between
840        p1 and p2 is a multiple of the loop size. */
841     SPGradient *p1 = src, *p2 = src;
842     bool do1 = false;
843     for (;;) {
844         if (match(p2)) {
845             return p2;
846         }
848         p2 = p2->ref->getObject();
849         if (!p2) {
850             return p2;
851         }
852         if (do1) {
853             p1 = p1->ref->getObject();
854         }
855         do1 = !do1;
857         if ( p2 == p1 ) {
858             /* We've been here before, so return NULL to indicate that no matching gradient found
859              * in the chain. */
860             return NULL;
861         }
862     }
865 /**
866  * True if gradient has stops.
867  */
868 static bool has_stopsFN(SPGradient const *gr)
870     return gr->hasStops();
873 /**
874  * True if gradient has spread set.
875  */
876 static bool has_spread_set(SPGradient const *gr)
878     return gr->isSpreadSet();
881 /**
882  * True if gradient has units set.
883  */
884 static bool
885 has_units_set(SPGradient const *gr)
887     return gr->isUnitsSet();
891 SPGradient *SPGradient::getVector(bool force_vector)
893     SPGradient * src = chase_hrefs(this, has_stopsFN);
895     if (force_vector) {
896         src = sp_gradient_ensure_vector_normalized(src);
897     }
898     return src;
901 /**
902  * Returns the effective spread of given gradient (climbing up the refs chain if needed).
903  *
904  * \pre SP_IS_GRADIENT(gradient).
905  */
906 SPGradientSpread SPGradient::fetchSpread()
908     SPGradient const *src = chase_hrefs(this, has_spread_set);
909     return ( src
910              ? src->spread
911              : SP_GRADIENT_SPREAD_PAD ); // pad is the default
914 /**
915  * Returns the effective units of given gradient (climbing up the refs chain if needed).
916  *
917  * \pre SP_IS_GRADIENT(gradient).
918  */
919 SPGradientUnits SPGradient::fetchUnits()
921     SPGradient const *src = chase_hrefs(this, has_units_set);
922     return ( src
923              ? src->units
924              : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
928 /**
929  * Clears the gradient's svg:stop children from its repr.
930  */
931 void
932 sp_gradient_repr_clear_vector(SPGradient *gr)
934     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
936     /* Collect stops from original repr */
937     GSList *sl = NULL;
938     for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
939         if (!strcmp(child->name(), "svg:stop")) {
940             sl = g_slist_prepend(sl, child);
941         }
942     }
943     /* Remove all stops */
944     while (sl) {
945         /** \todo
946          * fixme: This should work, unless we make gradient
947          * into generic group.
948          */
949         sp_repr_unparent((Inkscape::XML::Node *)sl->data);
950         sl = g_slist_remove(sl, sl->data);
951     }
954 /**
955  * Writes the gradient's internal vector (whether from its own stops, or
956  * inherited from refs) into the gradient repr as svg:stop elements.
957  */
958 void
959 sp_gradient_repr_write_vector(SPGradient *gr)
961     g_return_if_fail(gr != NULL);
962     g_return_if_fail(SP_IS_GRADIENT(gr));
964     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
965     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
967     /* We have to be careful, as vector may be our own, so construct repr list at first */
968     GSList *cl = NULL;
970     for (guint i = 0; i < gr->vector.stops.size(); i++) {
971         Inkscape::CSSOStringStream os;
972         Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
973         sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
974         /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
975          * sense for offset proportions. */
976         os << "stop-color:" << gr->vector.stops[i].color.toString() << ";stop-opacity:" << gr->vector.stops[i].opacity;
977         child->setAttribute("style", os.str().c_str());
978         /* Order will be reversed here */
979         cl = g_slist_prepend(cl, child);
980     }
982     sp_gradient_repr_clear_vector(gr);
984     /* And insert new children from list */
985     while (cl) {
986         Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
987         repr->addChild(child, NULL);
988         Inkscape::GC::release(child);
989         cl = g_slist_remove(cl, child);
990     }
994 void SPGradientImpl::gradientRefModified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
996     if ( gradient->invalidateVector() ) {
997         SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
998         // Conditional to avoid causing infinite loop if there's a cycle in the href chain.
999     }
1002 /** Return true if change made. */
1003 bool SPGradient::invalidateVector()
1005     bool ret = false;
1007     if (color != NULL) {
1008         g_free(color);
1009         color = NULL;
1010         ret = true;
1011     }
1013     if (vector.built) {
1014         vector.built = false;
1015         vector.stops.clear();
1016         ret = true;
1017     }
1019     return ret;
1022 /** Creates normalized color vector */
1023 void SPGradient::rebuildVector()
1025     gint len = 0;
1026     for ( SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
1027           child != NULL ;
1028           child = SP_OBJECT_NEXT(child) ) {
1029         if (SP_IS_STOP(child)) {
1030             len ++;
1031         }
1032     }
1034     has_stops = (len != 0);
1036     vector.stops.clear();
1038     SPGradient *reffed = ref->getObject();
1039     if ( !hasStops() && reffed ) {
1040         /* Copy vector from referenced gradient */
1041         vector.built = true;   // Prevent infinite recursion.
1042         reffed->ensureVector();
1043         if (!reffed->vector.stops.empty()) {
1044             vector.built = reffed->vector.built;
1045             vector.stops.assign(reffed->vector.stops.begin(), reffed->vector.stops.end());
1046             return;
1047         }
1048     }
1050     for (SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
1051          child != NULL;
1052          child = SP_OBJECT_NEXT(child) ) {
1053         if (SP_IS_STOP(child)) {
1054             SPStop *stop = SP_STOP(child);
1056             SPGradientStop gstop;
1057             if (vector.stops.size() > 0) {
1058                 // "Each gradient offset value is required to be equal to or greater than the
1059                 // previous gradient stop's offset value. If a given gradient stop's offset
1060                 // value is not equal to or greater than all previous offset values, then the
1061                 // offset value is adjusted to be equal to the largest of all previous offset
1062                 // values."
1063                 gstop.offset = MAX(stop->offset, vector.stops.back().offset);
1064             } else {
1065                 gstop.offset = stop->offset;
1066             }
1068             // "Gradient offset values less than 0 (or less than 0%) are rounded up to
1069             // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
1070             // down to 100%."
1071             gstop.offset = CLAMP(gstop.offset, 0, 1);
1073             gstop.color = stop->getEffectiveColor();
1074             gstop.opacity = stop->opacity;
1076             vector.stops.push_back(gstop);
1077         }
1078     }
1080     // Normalize per section 13.2.4 of SVG 1.1.
1081     if (vector.stops.size() == 0) {
1082         /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
1083          * paint style."
1084          */
1085         {
1086             SPGradientStop gstop;
1087             gstop.offset = 0.0;
1088             gstop.color.set( 0x00000000 );
1089             gstop.opacity = 0.0;
1090             vector.stops.push_back(gstop);
1091         }
1092         {
1093             SPGradientStop gstop;
1094             gstop.offset = 1.0;
1095             gstop.color.set( 0x00000000 );
1096             gstop.opacity = 0.0;
1097             vector.stops.push_back(gstop);
1098         }
1099     } else {
1100         /* "If one stop is defined, then paint with the solid color fill using the color defined
1101          * for that gradient stop."
1102          */
1103         if (vector.stops.front().offset > 0.0) {
1104             // If the first one is not at 0, then insert a copy of the first at 0.
1105             SPGradientStop gstop;
1106             gstop.offset = 0.0;
1107             gstop.color = vector.stops.front().color;
1108             gstop.opacity = vector.stops.front().opacity;
1109             vector.stops.insert(vector.stops.begin(), gstop);
1110         }
1111         if (vector.stops.back().offset < 1.0) {
1112             // If the last one is not at 1, then insert a copy of the last at 1.
1113             SPGradientStop gstop;
1114             gstop.offset = 1.0;
1115             gstop.color = vector.stops.back().color;
1116             gstop.opacity = vector.stops.back().opacity;
1117             vector.stops.push_back(gstop);
1118         }
1119     }
1121     vector.built = true;
1124 /**
1125  * The gradient's color array is newly created and set up from vector.
1126  */
1127 void SPGradient::ensureColors()
1129     if (!vector.built) {
1130         rebuildVector();
1131     }
1132     g_return_if_fail(!vector.stops.empty());
1134     /// \todo Where is the memory freed?
1135     if (!color) {
1136         color = g_new(guchar, 4 * NCOLORS);
1137     }
1139     // This assumes that vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1140     // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1141     // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1143     double remainder[4] = {0,0,0,0};
1144     double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1145     switch(spread) {
1146     case SP_GRADIENT_SPREAD_PAD:
1147         remainder[0] = 0.5*vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1148         remainder[1] = 0.5*vector.stops[0].color.v.c[1];
1149         remainder[2] = 0.5*vector.stops[0].color.v.c[2];
1150         remainder[3] = 0.5*vector.stops[0].opacity;
1151         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
1152         remainder_for_end[1] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[1];
1153         remainder_for_end[2] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[2];
1154         remainder_for_end[3] = 0.5*vector.stops[vector.stops.size() - 1].opacity;
1155         break;
1156     case SP_GRADIENT_SPREAD_REFLECT:
1157     case SP_GRADIENT_SPREAD_REPEAT:
1158         // These two are handled differently, see below.
1159         break;
1160     default:
1161         g_error("Spread type not supported!");
1162     };
1163     for (unsigned int i = 0; i < vector.stops.size() - 1; i++) {
1164         double r0 = vector.stops[i].color.v.c[0];
1165         double g0 = vector.stops[i].color.v.c[1];
1166         double b0 = vector.stops[i].color.v.c[2];
1167         double a0 = vector.stops[i].opacity;
1168         double r1 = vector.stops[i+1].color.v.c[0];
1169         double g1 = vector.stops[i+1].color.v.c[1];
1170         double b1 = vector.stops[i+1].color.v.c[2];
1171         double a1 = vector.stops[i+1].opacity;
1172         double o0 = vector.stops[i].offset * (NCOLORS-1);
1173         double o1 = vector.stops[i + 1].offset * (NCOLORS-1);
1174         unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1175         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
1177         if (oe == ob) {
1178             // Simple case, this interval starts and stops within one cell
1179             // The contribution of this interval is:
1180             //    (o1-o0)*(c(o0)+c(o1))/2
1181             //  = (o1-o0)*(c0+c1)/2
1182             double dt = 0.5*(o1-o0);
1183             remainder[0] += dt*(r0 + r1);
1184             remainder[1] += dt*(g0 + g1);
1185             remainder[2] += dt*(b0 + b1);
1186             remainder[3] += dt*(a0 + a1);
1187         } else {
1188             // First compute colors for the cells which are fully covered by the current interval.
1189             // The prefiltered values are equal to the midpoint of each cell here.
1190             //  f = (j-o0)/(o1-o0)
1191             //    = j*(1/(o1-o0)) - o0/(o1-o0)
1192             double f = (ob-o0) / (o1-o0);
1193             double df = 1. / (o1-o0);
1194             for (unsigned int j = ob+1; j < oe; j++) {
1195                 f += df;
1196                 color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1197                 color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1198                 color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1199                 color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1200             }
1202             // Now handle the beginning
1203             // The contribution of the last point is already in remainder.
1204             // The contribution of this point is:
1205             //    (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1206             //  = (ob+.5-o0)*c((o0+ob+.5)/2)
1207             //  = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1208             //  = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1209             double dt = ob+.5-o0;
1210             f = 0.5*dt*df;
1211             if (ob==0 && spread==SP_GRADIENT_SPREAD_REFLECT) {
1212                 // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1213                 color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1214                 color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1215                 color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1216                 color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1217             } else if (ob==0 && spread==SP_GRADIENT_SPREAD_REPEAT) {
1218                 // 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.
1219                 remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1220                 remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1221                 remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1222                 remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1223             } else {
1224                 // The first half of the cell was already in remainder.
1225                 color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1226                 color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1227                 color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1228                 color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1229             }
1231             // Now handle the end, which should end up in remainder
1232             // The contribution of this point is:
1233             //    (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1234             //  = (o1-oe+.5)*c((o1+oe-.5)/2)
1235             //  = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1236             dt = o1-oe+.5;
1237             f = (0.5*(o1+oe-.5)-o0)*df;
1238             remainder[0] = dt*(r0 + f*(r1-r0));
1239             remainder[1] = dt*(g0 + f*(g1-g0));
1240             remainder[2] = dt*(b0 + f*(b1-b0));
1241             remainder[3] = dt*(a0 + f*(a1-a0));
1242         }
1243     }
1244     switch(spread) {
1245     case SP_GRADIENT_SPREAD_PAD:
1246         color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1247         color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1248         color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1249         color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1250         break;
1251     case SP_GRADIENT_SPREAD_REFLECT:
1252         // The second half is the same as the first half, so multiply by 2.
1253         color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1254         color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1255         color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1256         color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1257         break;
1258     case SP_GRADIENT_SPREAD_REPEAT:
1259         // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1260         color[0] = color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1261         color[1] = color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1262         color[2] = color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1263         color[3] = color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1264         break;
1265     }
1268 /**
1269  * Renders gradient vector to buffer as line.
1270  *
1271  * RGB buffer background should be set up beforehand.
1272  *
1273  * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1274  * @param span Full integer width of requested gradient.
1275  * @param pos Buffer starting position in span.
1276  */
1277 static void
1278 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1279                                     gint const len, gint const pos, gint const span)
1281     g_return_if_fail(gradient != NULL);
1282     g_return_if_fail(SP_IS_GRADIENT(gradient));
1283     g_return_if_fail(buf != NULL);
1284     g_return_if_fail(len > 0);
1285     g_return_if_fail(pos >= 0);
1286     g_return_if_fail(pos + len <= span);
1287     g_return_if_fail(span > 0);
1289     if (!gradient->color) {
1290         gradient->ensureColors();
1291     }
1293     gint idx = (pos * 1024 << 8) / span;
1294     gint didx = (1024 << 8) / span;
1296     for (gint x = 0; x < len; x++) {
1297         /// \todo Can this be done with 4 byte copies?
1298         *buf++ = gradient->color[4 * (idx >> 8)];
1299         *buf++ = gradient->color[4 * (idx >> 8) + 1];
1300         *buf++ = gradient->color[4 * (idx >> 8) + 2];
1301         *buf++ = gradient->color[4 * (idx >> 8) + 3];
1302         idx += didx;
1303     }
1306 /**
1307  * Render rectangular RGBA area from gradient vector.
1308  */
1309 void
1310 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1311                                      gint const width, gint const height, gint const rowstride,
1312                                      gint const pos, gint const span, bool const horizontal)
1314     g_return_if_fail(gradient != NULL);
1315     g_return_if_fail(SP_IS_GRADIENT(gradient));
1316     g_return_if_fail(buf != NULL);
1317     g_return_if_fail(width > 0);
1318     g_return_if_fail(height > 0);
1319     g_return_if_fail(pos >= 0);
1320     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1321     g_return_if_fail(span > 0);
1323     if (horizontal) {
1324         sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1325         for (gint y = 1; y < height; y++) {
1326             memcpy(buf + y * rowstride, buf, 4 * width);
1327         }
1328     } else {
1329         guchar *tmp = (guchar *)alloca(4 * height);
1330         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1331         for (gint y = 0; y < height; y++) {
1332             guchar *b = buf + y * rowstride;
1333             for (gint x = 0; x < width; x++) {
1334                 *b++ = tmp[0];
1335                 *b++ = tmp[1];
1336                 *b++ = tmp[2];
1337                 *b++ = tmp[3];
1338             }
1339             tmp += 4;
1340         }
1341     }
1344 /**
1345  * Render rectangular RGB area from gradient vector.
1346  */
1347 void
1348 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1349                                     gint const width, gint const height, gint const /*rowstride*/,
1350                                     gint const pos, gint const span, bool const horizontal)
1352     g_return_if_fail(gradient != NULL);
1353     g_return_if_fail(SP_IS_GRADIENT(gradient));
1354     g_return_if_fail(buf != NULL);
1355     g_return_if_fail(width > 0);
1356     g_return_if_fail(height > 0);
1357     g_return_if_fail(pos >= 0);
1358     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1359     g_return_if_fail(span > 0);
1361     if (horizontal) {
1362         guchar *tmp = (guchar*)alloca(4 * width);
1363         sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1364         for (gint y = 0; y < height; y++) {
1365             guchar *t = tmp;
1366             for (gint x = 0; x < width; x++) {
1367                 gint a = t[3];
1368                 gint fc = (t[0] - buf[0]) * a;
1369                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1370                 fc = (t[1] - buf[1]) * a;
1371                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1372                 fc = (t[2] - buf[2]) * a;
1373                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1374                 buf += 3;
1375                 t += 4;
1376             }
1377         }
1378     } else {
1379         guchar *tmp = (guchar*)alloca(4 * height);
1380         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1381         for (gint y = 0; y < height; y++) {
1382             guchar *t = tmp + 4 * y;
1383             for (gint x = 0; x < width; x++) {
1384                 gint a = t[3];
1385                 gint fc = (t[0] - buf[0]) * a;
1386                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1387                 fc = (t[1] - buf[1]) * a;
1388                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1389                 fc = (t[2] - buf[2]) * a;
1390                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1391             }
1392         }
1393     }
1396 Geom::Matrix
1397 sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1399     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1400         return ( Geom::Scale(bbox.dimensions())
1401                  * Geom::Translate(bbox.min())
1402                  * Geom::Matrix(ctm) );
1403     } else {
1404         return ctm;
1405     }
1408 Geom::Matrix
1409 sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1411     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1412         return ( gr->gradientTransform
1413                  * Geom::Scale(bbox.dimensions())
1414                  * Geom::Translate(bbox.min())
1415                  * Geom::Matrix(ctm) );
1416     } else {
1417         return gr->gradientTransform * ctm;
1418     }
1421 void
1422 sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1423                             Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1425     gr->gradientTransform = gs2d * ctm.inverse();
1426     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1427         gr->gradientTransform = ( gr->gradientTransform
1428                                   * Geom::Translate(-bbox.min())
1429                                   * Geom::Scale(bbox.dimensions()).inverse() );
1430     }
1431     gr->gradientTransform_set = TRUE;
1433     SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1436 /*
1437  * Linear Gradient
1438  */
1440 class SPLGPainter;
1442 /// A context with linear gradient, painter, and gradient renderer.
1443 struct SPLGPainter {
1444     SPPainter painter;
1445     SPLinearGradient *lg;
1447     NRLGradientRenderer lgr;
1449     static SPPainter * painter_new(SPPaintServer *ps,
1450                                    Geom::Matrix const &full_transform,
1451                                    Geom::Matrix const &parent_transform,
1452                                    NRRect const *bbox);
1453 };
1455 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1456 static void sp_lineargradient_init(SPLinearGradient *lg);
1458 static void sp_lineargradient_build(SPObject *object,
1459                                     SPDocument *document,
1460                                     Inkscape::XML::Node *repr);
1461 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1462 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1463                                                     guint flags);
1465 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1467 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1469 static SPGradientClass *lg_parent_class;
1471 /**
1472  * Register SPLinearGradient class and return its type.
1473  */
1474 GType
1475 sp_lineargradient_get_type()
1477     static GType type = 0;
1478     if (!type) {
1479         GTypeInfo info = {
1480             sizeof(SPLinearGradientClass),
1481             NULL, NULL,
1482             (GClassInitFunc) sp_lineargradient_class_init,
1483             NULL, NULL,
1484             sizeof(SPLinearGradient),
1485             16,
1486             (GInstanceInitFunc) sp_lineargradient_init,
1487             NULL,   /* value_table */
1488         };
1489         type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1490     }
1491     return type;
1494 /**
1495  * SPLinearGradient vtable initialization.
1496  */
1497 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1499     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1500     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1502     lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1504     sp_object_class->build = sp_lineargradient_build;
1505     sp_object_class->set = sp_lineargradient_set;
1506     sp_object_class->write = sp_lineargradient_write;
1508     ps_class->painter_new = SPLGPainter::painter_new;
1509     ps_class->painter_free = sp_lineargradient_painter_free;
1512 /**
1513  * Callback for SPLinearGradient object initialization.
1514  */
1515 static void sp_lineargradient_init(SPLinearGradient *lg)
1517     lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1518     lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
1519     lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1520     lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
1523 /**
1524  * Callback: set attributes from associated repr.
1525  */
1526 static void sp_lineargradient_build(SPObject *object,
1527                                     SPDocument *document,
1528                                     Inkscape::XML::Node *repr)
1530     if (((SPObjectClass *) lg_parent_class)->build)
1531         (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1533     sp_object_read_attr(object, "x1");
1534     sp_object_read_attr(object, "y1");
1535     sp_object_read_attr(object, "x2");
1536     sp_object_read_attr(object, "y2");
1539 /**
1540  * Callback: set attribute.
1541  */
1542 static void
1543 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1545     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1547     switch (key) {
1548         case SP_ATTR_X1:
1549             lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1550             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1551             break;
1552         case SP_ATTR_Y1:
1553             lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1554             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1555             break;
1556         case SP_ATTR_X2:
1557             lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1558             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1559             break;
1560         case SP_ATTR_Y2:
1561             lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1562             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1563             break;
1564         default:
1565             if (((SPObjectClass *) lg_parent_class)->set)
1566                 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1567             break;
1568     }
1571 /**
1572  * Callback: write attributes to associated repr.
1573  */
1574 static Inkscape::XML::Node *
1575 sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1577     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1579     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1580         repr = xml_doc->createElement("svg:linearGradient");
1581     }
1583     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1584         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1585     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1586         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1587     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1588         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1589     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1590         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1592     if (((SPObjectClass *) lg_parent_class)->write)
1593         (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
1595     return repr;
1598 /**
1599  * Create linear gradient context.
1600  *
1601  * Basically we have to deal with transformations
1602  *
1603  * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1604  * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1605  * 2) gradientTransform
1606  * 3) bbox2user
1607  * 4) ctm == userspace to pixel grid
1608  *
1609  * See also (*) in sp-pattern about why we may need parent_transform.
1610  *
1611  * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1612  * and end < 1.
1613  */
1614 SPPainter * SPLGPainter::painter_new(SPPaintServer *ps,
1615                                      Geom::Matrix const &full_transform,
1616                                      Geom::Matrix const &/*parent_transform*/,
1617                                      NRRect const *bbox)
1619     SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1620     SPGradient *gr = SP_GRADIENT(ps);
1622     if (!gr->color) {
1623         gr->ensureColors();
1624     }
1626     SPLGPainter *lgp = g_new(SPLGPainter, 1);
1628     lgp->painter.type = SP_PAINTER_IND;
1629     lgp->painter.fill = sp_lg_fill;
1631     lgp->lg = lg;
1633     /** \todo
1634      * Technically speaking, we map NCOLORS on line [start,end] onto line
1635      * [0,1].  I almost think we should fill color array start and end in
1636      * that case. The alternative would be to leave these just empty garbage
1637      * or something similar. Originally I had 1023.9999 here - not sure
1638      * whether we have really to cut out ceil int (Lauris).
1639      */
1640     Geom::Matrix color2norm(Geom::identity());
1641     Geom::Matrix color2px;
1642     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1643         Geom::Matrix norm2pos(Geom::identity());
1645         /* BBox to user coordinate system */
1646         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1648         Geom::Matrix color2pos = color2norm * norm2pos;
1649         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1650         Geom::Matrix color2user = color2tpos * bbox2user;
1651         color2px = color2user * full_transform;
1653     } else {
1654         /* Problem: What to do, if we have mixed lengths and percentages? */
1655         /* Currently we do ignore percentages at all, but that is not good (lauris) */
1657         Geom::Matrix norm2pos(Geom::identity());
1658         Geom::Matrix color2pos = color2norm * norm2pos;
1659         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1660         color2px = color2tpos * full_transform;
1662     }
1663     // TODO: remove color2px_nr after converting to 2geom
1664     NR::Matrix color2px_nr = from_2geom(color2px);
1665     nr_lgradient_renderer_setup(&lgp->lgr, gr->color, gr->fetchSpread(), &color2px_nr,
1666                                 lg->x1.computed, lg->y1.computed,
1667                                 lg->x2.computed, lg->y2.computed);
1669     return (SPPainter *) lgp;
1672 static void
1673 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1675     g_free(painter);
1678 /**
1679  * Directly set properties of linear gradient and request modified.
1680  */
1681 void
1682 sp_lineargradient_set_position(SPLinearGradient *lg,
1683                                gdouble x1, gdouble y1,
1684                                gdouble x2, gdouble y2)
1686     g_return_if_fail(lg != NULL);
1687     g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1689     /* fixme: units? (Lauris)  */
1690     lg->x1.set(SVGLength::NONE, x1, x1);
1691     lg->y1.set(SVGLength::NONE, y1, y1);
1692     lg->x2.set(SVGLength::NONE, x2, x2);
1693     lg->y2.set(SVGLength::NONE, y2, y2);
1695     SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1698 /**
1699  * Callback when linear gradient object is rendered.
1700  */
1701 static void
1702 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1704     SPLGPainter *lgp = (SPLGPainter *) painter;
1706     if (lgp->lg->color == NULL) {
1707         lgp->lg->ensureColors();
1708         lgp->lgr.vector = lgp->lg->color;
1709     }
1711     nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1714 /*
1715  * Radial Gradient
1716  */
1718 class SPRGPainter;
1720 /// A context with radial gradient, painter, and gradient renderer.
1721 struct SPRGPainter {
1722     SPPainter painter;
1723     SPRadialGradient *rg;
1724     NRRGradientRenderer rgr;
1726     static SPPainter *painter_new(SPPaintServer *ps,
1727                                   Geom::Matrix const &full_transform,
1728                                   Geom::Matrix const &parent_transform,
1729                                   NRRect const *bbox);
1730 };
1732 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1733 static void sp_radialgradient_init(SPRadialGradient *rg);
1735 static void sp_radialgradient_build(SPObject *object,
1736                                     SPDocument *document,
1737                                     Inkscape::XML::Node *repr);
1738 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1739 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1740                                                     guint flags);
1741 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1743 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1745 static SPGradientClass *rg_parent_class;
1747 /**
1748  * Register SPRadialGradient class and return its type.
1749  */
1750 GType
1751 sp_radialgradient_get_type()
1753     static GType type = 0;
1754     if (!type) {
1755         GTypeInfo info = {
1756             sizeof(SPRadialGradientClass),
1757             NULL, NULL,
1758             (GClassInitFunc) sp_radialgradient_class_init,
1759             NULL, NULL,
1760             sizeof(SPRadialGradient),
1761             16,
1762             (GInstanceInitFunc) sp_radialgradient_init,
1763             NULL,   /* value_table */
1764         };
1765         type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1766     }
1767     return type;
1770 /**
1771  * SPRadialGradient vtable initialization.
1772  */
1773 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1775     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1776     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1778     rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1780     sp_object_class->build = sp_radialgradient_build;
1781     sp_object_class->set = sp_radialgradient_set;
1782     sp_object_class->write = sp_radialgradient_write;
1784     ps_class->painter_new = SPRGPainter::painter_new;
1785     ps_class->painter_free = sp_radialgradient_painter_free;
1788 /**
1789  * Callback for SPRadialGradient object initialization.
1790  */
1791 static void
1792 sp_radialgradient_init(SPRadialGradient *rg)
1794     rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1795     rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1796     rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1797     rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1798     rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1801 /**
1802  * Set radial gradient attributes from associated repr.
1803  */
1804 static void
1805 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1807     if (((SPObjectClass *) rg_parent_class)->build)
1808         (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1810     sp_object_read_attr(object, "cx");
1811     sp_object_read_attr(object, "cy");
1812     sp_object_read_attr(object, "r");
1813     sp_object_read_attr(object, "fx");
1814     sp_object_read_attr(object, "fy");
1817 /**
1818  * Set radial gradient attribute.
1819  */
1820 static void
1821 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1823     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1825     switch (key) {
1826         case SP_ATTR_CX:
1827             if (!rg->cx.read(value)) {
1828                 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1829             }
1830             if (!rg->fx._set) {
1831                 rg->fx.value = rg->cx.value;
1832                 rg->fx.computed = rg->cx.computed;
1833             }
1834             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1835             break;
1836         case SP_ATTR_CY:
1837             if (!rg->cy.read(value)) {
1838                 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1839             }
1840             if (!rg->fy._set) {
1841                 rg->fy.value = rg->cy.value;
1842                 rg->fy.computed = rg->cy.computed;
1843             }
1844             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1845             break;
1846         case SP_ATTR_R:
1847             if (!rg->r.read(value)) {
1848                 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1849             }
1850             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1851             break;
1852         case SP_ATTR_FX:
1853             if (!rg->fx.read(value)) {
1854                 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1855             }
1856             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1857             break;
1858         case SP_ATTR_FY:
1859             if (!rg->fy.read(value)) {
1860                 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1861             }
1862             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1863             break;
1864         default:
1865             if (((SPObjectClass *) rg_parent_class)->set)
1866                 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1867             break;
1868     }
1871 /**
1872  * Write radial gradient attributes to associated repr.
1873  */
1874 static Inkscape::XML::Node *
1875 sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1877     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1879     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1880         repr = xml_doc->createElement("svg:radialGradient");
1881     }
1883     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1884     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1885     if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1886     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1887     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1889     if (((SPObjectClass *) rg_parent_class)->write)
1890         (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1892     return repr;
1895 /**
1896  * Create radial gradient context.
1897  */
1898 SPPainter *SPRGPainter::painter_new(SPPaintServer *ps,
1899                                     Geom::Matrix const &full_transform,
1900                                     Geom::Matrix const &/*parent_transform*/,
1901                                     NRRect const *bbox)
1903     SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1904     SPGradient *gr = SP_GRADIENT(ps);
1906     if (!gr->color) {
1907         gr->ensureColors();
1908     }
1910     SPRGPainter *rgp = g_new(SPRGPainter, 1);
1912     rgp->painter.type = SP_PAINTER_IND;
1913     rgp->painter.fill = sp_rg_fill;
1915     rgp->rg = rg;
1917     Geom::Matrix gs2px;
1919     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1920         /** \todo
1921          * fixme: We may try to normalize here too, look at
1922          * linearGradient (Lauris)
1923          */
1925         /* BBox to user coordinate system */
1926         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1928         Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1930         gs2px = gs2user * full_transform;
1931     } else {
1932         /** \todo
1933          * Problem: What to do, if we have mixed lengths and percentages?
1934          * Currently we do ignore percentages at all, but that is not
1935          * good (lauris)
1936          */
1938         gs2px = gr->gradientTransform * full_transform;
1939     }
1940     // TODO: remove gs2px_nr after converting to 2geom
1941     NR::Matrix gs2px_nr = from_2geom(gs2px);
1942     nr_rgradient_renderer_setup(&rgp->rgr, gr->color, gr->fetchSpread(),
1943                                 &gs2px_nr,
1944                                 rg->cx.computed, rg->cy.computed,
1945                                 rg->fx.computed, rg->fy.computed,
1946                                 rg->r.computed);
1948     return (SPPainter *) rgp;
1951 static void
1952 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1954     g_free(painter);
1957 /**
1958  * Directly set properties of radial gradient and request modified.
1959  */
1960 void
1961 sp_radialgradient_set_position(SPRadialGradient *rg,
1962                                gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1964     g_return_if_fail(rg != NULL);
1965     g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1967     /* fixme: units? (Lauris)  */
1968     rg->cx.set(SVGLength::NONE, cx, cx);
1969     rg->cy.set(SVGLength::NONE, cy, cy);
1970     rg->fx.set(SVGLength::NONE, fx, fx);
1971     rg->fy.set(SVGLength::NONE, fy, fy);
1972     rg->r.set(SVGLength::NONE, r, r);
1974     SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1977 /**
1978  * Callback when radial gradient object is rendered.
1979  */
1980 static void
1981 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1983     SPRGPainter *rgp = (SPRGPainter *) painter;
1985     if (rgp->rg->color == NULL) {
1986         rgp->rg->ensureColors();
1987         rgp->rgr.vector = rgp->rg->color;
1988     }
1990     nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1993 /*
1994   Local Variables:
1995   mode:c++
1996   c-file-style:"stroustrup"
1997   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1998   indent-tabs-mode:nil
1999   fill-column:99
2000   End:
2001 */
2002 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :