Code

Rough pass of Fill-n-Stroke swatch conversion.
[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"
49 #define SP_MACROS_SILENT
50 #include "macros.h"
52 /// Has to be power of 2
53 #define NCOLORS NR_GRADIENT_VECTOR_LENGTH
55 static void sp_stop_class_init(SPStopClass *klass);
56 static void sp_stop_init(SPStop *stop);
58 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
59 static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
60 static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
62 static SPObjectClass *stop_parent_class;
64 class SPGradientImpl
65 {
66     friend class SPGradient;
68     static void classInit(SPGradientClass *klass);
70     static void init(SPGradient *gr);
71     static void build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
72     static void release(SPObject *object);
73     static void modified(SPObject *object, guint flags);
74     static Inkscape::XML::Node *write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags);
76     static void gradientRefModified(SPObject *href, guint flags, SPGradient *gradient);
77     static void gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr);
79     static void childAdded(SPObject *object,
80                            Inkscape::XML::Node *child,
81                            Inkscape::XML::Node *ref);
82     static void removeChild(SPObject *object, Inkscape::XML::Node *child);
84     static void setGradientAttr(SPObject *object, unsigned key, gchar const *value);
85 };
87 /**
88  * Registers SPStop class and returns its type.
89  */
90 GType
91 sp_stop_get_type()
92 {
93     static GType type = 0;
94     if (!type) {
95         GTypeInfo info = {
96             sizeof(SPStopClass),
97             NULL, NULL,
98             (GClassInitFunc) sp_stop_class_init,
99             NULL, NULL,
100             sizeof(SPStop),
101             16,
102             (GInstanceInitFunc) sp_stop_init,
103             NULL,   /* value_table */
104         };
105         type = g_type_register_static(SP_TYPE_OBJECT, "SPStop", &info, (GTypeFlags)0);
106     }
107     return type;
110 /**
111  * Callback to initialize SPStop vtable.
112  */
113 static void sp_stop_class_init(SPStopClass *klass)
115     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
117     stop_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
119     sp_object_class->build = sp_stop_build;
120     sp_object_class->set = sp_stop_set;
121     sp_object_class->write = sp_stop_write;
124 /**
125  * Callback to initialize SPStop object.
126  */
127 static void
128 sp_stop_init(SPStop *stop)
130     stop->offset = 0.0;
131     stop->currentColor = false;
132     stop->specified_color.set( 0x000000ff );
133     stop->opacity = 1.0;
136 /**
137  * Virtual build: set stop attributes from its associated XML node.
138  */
139 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
141     if (((SPObjectClass *) stop_parent_class)->build)
142         (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
144     sp_object_read_attr(object, "offset");
145     sp_object_read_attr(object, "stop-color");
146     sp_object_read_attr(object, "stop-opacity");
147     sp_object_read_attr(object, "style");
150 /**
151  * Virtual set: set attribute to value.
152  */
153 static void
154 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
156     SPStop *stop = SP_STOP(object);
158     switch (key) {
159         case SP_ATTR_STYLE: {
160         /** \todo
161          * fixme: We are reading simple values 3 times during build (Lauris).
162          * \par
163          * We need presentation attributes etc.
164          * \par
165          * remove the hackish "style reading" from here: see comments in
166          * sp_object_get_style_property about the bugs in our current
167          * approach.  However, note that SPStyle doesn't currently have
168          * stop-color and stop-opacity properties.
169          */
170             {
171                 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
172                 if (streq(p, "currentColor")) {
173                     stop->currentColor = true;
174                 } else {
175                     guint32 const color = sp_svg_read_color(p, 0);
176                     stop->specified_color.set( color );
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                     guint32 const color = sp_svg_read_color(p, 0);
195                     stop->specified_color.set( color );
196                 }
197             }
198             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
199             break;
200         }
201         case SP_PROP_STOP_OPACITY: {
202             {
203                 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
204                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
205                 stop->opacity = opacity;
206             }
207             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
208             break;
209         }
210         case SP_ATTR_OFFSET: {
211             stop->offset = sp_svg_read_percentage(value, 0.0);
212             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
213             break;
214         }
215         default: {
216             if (((SPObjectClass *) stop_parent_class)->set)
217                 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
218             break;
219         }
220     }
223 /**
224  * Virtual write: write object attributes to repr.
225  */
226 static Inkscape::XML::Node *
227 sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
229     SPStop *stop = SP_STOP(object);
231     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
232         repr = xml_doc->createElement("svg:stop");
233     }
235     guint32 specifiedcolor = stop->specified_color.toRGBA32( 255 );
236     gfloat opacity = stop->opacity;
238     if (((SPObjectClass *) stop_parent_class)->write)
239         (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
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         gchar c[64];
251         sp_svg_write_color(c, sizeof(c), specifiedcolor);
252         os << c;
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()
293     if ( !isSwatch() ) {
294         if ( hasStops() && (getStopCount() == 0) ) {
295             repr->setAttribute("osb:paint", "solid");
296         } else {
297             repr->setAttribute("osb:paint", "gradient");
298         }
299         requestModified(SP_OBJECT_MODIFIED_FLAG);
300     }
303 /**
304  * Return stop's color as 32bit value.
305  */
306 guint32
307 sp_stop_get_rgba32(SPStop const *const stop)
309     guint32 rgb0 = 0;
310     /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
311      * value depends on user agent, and don't give any further restrictions that I can
312      * see.) */
313     if (stop->currentColor) {
314         char const *str = sp_object_get_style_property(stop, "color", NULL);
315         if (str) {
316             rgb0 = sp_svg_read_color(str, rgb0);
317         }
318         unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
319         g_return_val_if_fail((alpha & ~0xff) == 0,
320                              rgb0 | 0xff);
321         return rgb0 | alpha;
322     } else {
323         return stop->specified_color.toRGBA32( stop->opacity );
324     }
327 /**
328  * Return stop's color as SPColor.
329  */
330 static SPColor
331 sp_stop_get_color(SPStop const *const stop)
333     if (stop->currentColor) {
334         char const *str = sp_object_get_style_property(stop, "color", NULL);
335         guint32 const dfl = 0;
336         /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
337          * value depends on user agent, and don't give any further restrictions that I can
338          * see.) */
339         guint32 color = dfl;
340         if (str) {
341             color = sp_svg_read_color(str, dfl);
342         }
343         SPColor ret( color );
344         return ret;
345     } else {
346         return stop->specified_color;
347     }
350 /*
351  * Gradient
352  */
354 static SPPaintServerClass *gradient_parent_class;
356 /**
357  * Registers SPGradient class and returns its type.
358  */
359 GType SPGradient::getType()
361     static GType gradient_type = 0;
362     if (!gradient_type) {
363         GTypeInfo gradient_info = {
364             sizeof(SPGradientClass),
365             NULL, NULL,
366             (GClassInitFunc) SPGradientImpl::classInit,
367             NULL, NULL,
368             sizeof(SPGradient),
369             16,
370             (GInstanceInitFunc) SPGradientImpl::init,
371             NULL,   /* value_table */
372         };
373         gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
374                                                &gradient_info, (GTypeFlags)0);
375     }
376     return gradient_type;
379 /**
380  * SPGradient vtable initialization.
381  */
382 void SPGradientImpl::classInit(SPGradientClass *klass)
384     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
386     gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
388     sp_object_class->build = SPGradientImpl::build;
389     sp_object_class->release = SPGradientImpl::release;
390     sp_object_class->set = SPGradientImpl::setGradientAttr;
391     sp_object_class->child_added = SPGradientImpl::childAdded;
392     sp_object_class->remove_child = SPGradientImpl::removeChild;
393     sp_object_class->modified = SPGradientImpl::modified;
394     sp_object_class->write = SPGradientImpl::write;
397 /**
398  * Callback for SPGradient object initialization.
399  */
400 void SPGradientImpl::init(SPGradient *gr)
402     gr->ref = new SPGradientReference(SP_OBJECT(gr));
403     gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(SPGradientImpl::gradientRefChanged), gr));
405     /** \todo
406      * Fixme: reprs being rearranged (e.g. via the XML editor)
407      * may require us to clear the state.
408      */
409     gr->state = SP_GRADIENT_STATE_UNKNOWN;
411     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
412     gr->units_set = FALSE;
414     gr->gradientTransform = Geom::identity();
415     gr->gradientTransform_set = FALSE;
417     gr->spread = SP_GRADIENT_SPREAD_PAD;
418     gr->spread_set = FALSE;
420     gr->has_stops = FALSE;
422     gr->vector.built = false;
423     gr->vector.stops.clear();
425     gr->color = NULL;
427     new (&gr->modified_connection) sigc::connection();
430 /**
431  * Virtual build: set gradient attributes from its associated repr.
432  */
433 void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
435     SPGradient *gradient = SP_GRADIENT(object);
437     if (((SPObjectClass *) gradient_parent_class)->build)
438         (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
440     SPObject *ochild;
441     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
442         if (SP_IS_STOP(ochild)) {
443             gradient->has_stops = TRUE;
444             break;
445         }
446     }
448     sp_object_read_attr(object, "gradientUnits");
449     sp_object_read_attr(object, "gradientTransform");
450     sp_object_read_attr(object, "spreadMethod");
451     sp_object_read_attr(object, "xlink:href");
453     /* Register ourselves */
454     sp_document_add_resource(document, "gradient", object);
457 /**
458  * Virtual release of SPGradient members before destruction.
459  */
460 void SPGradientImpl::release(SPObject *object)
462     SPGradient *gradient = (SPGradient *) object;
464 #ifdef SP_GRADIENT_VERBOSE
465     g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
466 #endif
468     if (SP_OBJECT_DOCUMENT(object)) {
469         /* Unregister ourselves */
470         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
471     }
473     if (gradient->ref) {
474         gradient->modified_connection.disconnect();
475         gradient->ref->detach();
476         delete gradient->ref;
477         gradient->ref = NULL;
478     }
480     if (gradient->color) {
481         g_free(gradient->color);
482         gradient->color = NULL;
483     }
485     gradient->modified_connection.~connection();
487     if (((SPObjectClass *) gradient_parent_class)->release)
488         ((SPObjectClass *) gradient_parent_class)->release(object);
491 /**
492  * Set gradient attribute to value.
493  */
494 void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const *value)
496     SPGradient *gr = SP_GRADIENT(object);
498     switch (key) {
499         case SP_ATTR_GRADIENTUNITS:
500             if (value) {
501                 if (!strcmp(value, "userSpaceOnUse")) {
502                     gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
503                 } else {
504                     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
505                 }
506                 gr->units_set = TRUE;
507             } else {
508                 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
509                 gr->units_set = FALSE;
510             }
511             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
512             break;
513         case SP_ATTR_GRADIENTTRANSFORM: {
514             Geom::Matrix t;
515             if (value && sp_svg_transform_read(value, &t)) {
516                 gr->gradientTransform = t;
517                 gr->gradientTransform_set = TRUE;
518             } else {
519                 gr->gradientTransform = Geom::identity();
520                 gr->gradientTransform_set = FALSE;
521             }
522             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
523             break;
524         }
525         case SP_ATTR_SPREADMETHOD:
526             if (value) {
527                 if (!strcmp(value, "reflect")) {
528                     gr->spread = SP_GRADIENT_SPREAD_REFLECT;
529                 } else if (!strcmp(value, "repeat")) {
530                     gr->spread = SP_GRADIENT_SPREAD_REPEAT;
531                 } else {
532                     gr->spread = SP_GRADIENT_SPREAD_PAD;
533                 }
534                 gr->spread_set = TRUE;
535             } else {
536                 gr->spread_set = FALSE;
537             }
538             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
539             break;
540         case SP_ATTR_XLINK_HREF:
541             if (value) {
542                 try {
543                     gr->ref->attach(Inkscape::URI(value));
544                 } catch (Inkscape::BadURIException &e) {
545                     g_warning("%s", e.what());
546                     gr->ref->detach();
547                 }
548             } else {
549                 gr->ref->detach();
550             }
551             break;
552         default:
553             if (((SPObjectClass *) gradient_parent_class)->set)
554                 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
555             break;
556     }
559 /**
560  * Gets called when the gradient is (re)attached to another gradient.
561  */
562 void SPGradientImpl::gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr)
564     if (old_ref) {
565         gr->modified_connection.disconnect();
566     }
567     if ( SP_IS_GRADIENT(ref)
568          && ref != gr )
569     {
570         gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&SPGradientImpl::gradientRefModified), gr));
571     }
573     // Per SVG, all unset attributes must be inherited from linked gradient.
574     // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
575     // but without setting the _set flags.
576     // FIXME: do the same for gradientTransform too
577     if (!gr->units_set) {
578         gr->units = gr->fetchUnits();
579     }
580     if (!gr->spread_set) {
581         gr->spread = gr->fetchSpread();
582     }
584     /// \todo Fixme: what should the flags (second) argument be? */
585     gradientRefModified(ref, 0, gr);
588 /**
589  * Callback for child_added event.
590  */
591 void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
593     SPGradient *gr = SP_GRADIENT(object);
595     gr->invalidateVector();
597     if (((SPObjectClass *) gradient_parent_class)->child_added)
598         (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
600     SPObject *ochild = sp_object_get_child_by_repr(object, child);
601     if ( ochild && SP_IS_STOP(ochild) ) {
602         gr->has_stops = TRUE;
603     }
605     /// \todo Fixme: should we schedule "modified" here?
606     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
609 /**
610  * Callback for remove_child event.
611  */
612 void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
614     SPGradient *gr = SP_GRADIENT(object);
616     gr->invalidateVector();
618     if (((SPObjectClass *) gradient_parent_class)->remove_child) {
619         (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
620     }
622     gr->has_stops = FALSE;
623     SPObject *ochild;
624     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
625         if (SP_IS_STOP(ochild)) {
626             gr->has_stops = TRUE;
627             break;
628         }
629     }
631     /* Fixme: should we schedule "modified" here? */
632     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
635 /**
636  * Callback for modified event.
637  */
638 void SPGradientImpl::modified(SPObject *object, guint flags)
640     SPGradient *gr = SP_GRADIENT(object);
642     if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
643         gr->invalidateVector();
644     }
646     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
647         gr->ensureColors();
648     }
650     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
651     flags &= SP_OBJECT_MODIFIED_CASCADE;
653     // FIXME: climb up the ladder of hrefs
654     GSList *l = NULL;
655     for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
656         g_object_ref(G_OBJECT(child));
657         l = g_slist_prepend(l, child);
658     }
659     l = g_slist_reverse(l);
660     while (l) {
661         SPObject *child = SP_OBJECT(l->data);
662         l = g_slist_remove(l, child);
663         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
664             child->emitModified(flags);
665         }
666         g_object_unref(G_OBJECT(child));
667     }
670 SPStop* SPGradient::getFirstStop()
672     SPStop* first = 0;
673     for (SPObject *ochild = sp_object_first_child(this); ochild && !first; ochild = SP_OBJECT_NEXT(ochild)) {
674         if (SP_IS_STOP(ochild)) {
675             first = SP_STOP(ochild);
676         }
677     }
678     return first;
681 int SPGradient::getStopCount() const
683     int count = 0;
685     for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
686         count++;
687     }
689     return count;
692 /**
693  * Write gradient attributes to repr.
694  */
695 Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
697     SPGradient *gr = SP_GRADIENT(object);
699     if (((SPObjectClass *) gradient_parent_class)->write)
700         (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
702     if (flags & SP_OBJECT_WRITE_BUILD) {
703         GSList *l = NULL;
704         for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
705             Inkscape::XML::Node *crepr;
706             crepr = child->updateRepr(xml_doc, NULL, flags);
707             if (crepr) l = g_slist_prepend(l, crepr);
708         }
709         while (l) {
710             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
711             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
712             l = g_slist_remove(l, l->data);
713         }
714     }
716     if (gr->ref->getURI()) {
717         gchar *uri_string = gr->ref->getURI()->toString();
718         repr->setAttribute("xlink:href", uri_string);
719         g_free(uri_string);
720     }
722     if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
723         switch (gr->units) {
724             case SP_GRADIENT_UNITS_USERSPACEONUSE:
725                 repr->setAttribute("gradientUnits", "userSpaceOnUse");
726                 break;
727             default:
728                 repr->setAttribute("gradientUnits", "objectBoundingBox");
729                 break;
730         }
731     }
733     if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
734         gchar *c=sp_svg_transform_write(gr->gradientTransform);
735         repr->setAttribute("gradientTransform", c);
736         g_free(c);
737     }
739     if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
740         /* FIXME: Ensure that gr->spread is the inherited value
741          * if !gr->spread_set.  Not currently happening: see SPGradient::modified.
742          */
743         switch (gr->spread) {
744             case SP_GRADIENT_SPREAD_REFLECT:
745                 repr->setAttribute("spreadMethod", "reflect");
746                 break;
747             case SP_GRADIENT_SPREAD_REPEAT:
748                 repr->setAttribute("spreadMethod", "repeat");
749                 break;
750             default:
751                 repr->setAttribute("spreadMethod", "pad");
752                 break;
753         }
754     }
756     return repr;
759 /**
760  * Forces the vector to be built, if not present (i.e., changed).
761  *
762  * \pre SP_IS_GRADIENT(gradient).
763  */
764 void SPGradient::ensureVector()
766     if ( !vector.built ) {
767         rebuildVector();
768     }
771 /**
772  * Set units property of gradient and emit modified.
773  */
774 void SPGradient::setUnits(SPGradientUnits units)
776     if (units != this->units) {
777         this->units = units;
778         units_set = TRUE;
779         requestModified(SP_OBJECT_MODIFIED_FLAG);
780     }
783 /**
784  * Set spread property of gradient and emit modified.
785  */
786 void SPGradient::setSpread(SPGradientSpread spread)
788     if (spread != this->spread) {
789         this->spread = spread;
790         spread_set = TRUE;
791         requestModified(SP_OBJECT_MODIFIED_FLAG);
792     }
795 /**
796  * Returns the first of {src, src-\>ref-\>getObject(),
797  * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
798  * for which \a match is true, or NULL if none found.
799  *
800  * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
801  * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
802  *
803  * \pre SP_IS_GRADIENT(src).
804  */
805 static SPGradient *
806 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
808     g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
810     /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2.  If there is a
811        loop, then once p1 has entered the loop, we'll detect it the next time the distance between
812        p1 and p2 is a multiple of the loop size. */
813     SPGradient *p1 = src, *p2 = src;
814     bool do1 = false;
815     for (;;) {
816         if (match(p2)) {
817             return p2;
818         }
820         p2 = p2->ref->getObject();
821         if (!p2) {
822             return p2;
823         }
824         if (do1) {
825             p1 = p1->ref->getObject();
826         }
827         do1 = !do1;
829         if ( p2 == p1 ) {
830             /* We've been here before, so return NULL to indicate that no matching gradient found
831              * in the chain. */
832             return NULL;
833         }
834     }
837 /**
838  * True if gradient has stops.
839  */
840 static bool has_stopsFN(SPGradient const *gr)
842     return gr->hasStops();
845 /**
846  * True if gradient has spread set.
847  */
848 static bool has_spread_set(SPGradient const *gr)
850     return gr->isSpreadSet();
853 /**
854  * True if gradient has units set.
855  */
856 static bool
857 has_units_set(SPGradient const *gr)
859     return gr->isUnitsSet();
863 SPGradient *SPGradient::getVector(bool force_vector)
865     SPGradient * src = chase_hrefs(this, has_stopsFN);
867     if (force_vector) {
868         src = sp_gradient_ensure_vector_normalized(src);
869     }
870     return src;
873 /**
874  * Returns the effective spread of given gradient (climbing up the refs chain if needed).
875  *
876  * \pre SP_IS_GRADIENT(gradient).
877  */
878 SPGradientSpread SPGradient::fetchSpread()
880     SPGradient const *src = chase_hrefs(this, has_spread_set);
881     return ( src
882              ? src->spread
883              : SP_GRADIENT_SPREAD_PAD ); // pad is the default
886 /**
887  * Returns the effective units of given gradient (climbing up the refs chain if needed).
888  *
889  * \pre SP_IS_GRADIENT(gradient).
890  */
891 SPGradientUnits SPGradient::fetchUnits()
893     SPGradient const *src = chase_hrefs(this, has_units_set);
894     return ( src
895              ? src->units
896              : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
900 /**
901  * Clears the gradient's svg:stop children from its repr.
902  */
903 void
904 sp_gradient_repr_clear_vector(SPGradient *gr)
906     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
908     /* Collect stops from original repr */
909     GSList *sl = NULL;
910     for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
911         if (!strcmp(child->name(), "svg:stop")) {
912             sl = g_slist_prepend(sl, child);
913         }
914     }
915     /* Remove all stops */
916     while (sl) {
917         /** \todo
918          * fixme: This should work, unless we make gradient
919          * into generic group.
920          */
921         sp_repr_unparent((Inkscape::XML::Node *)sl->data);
922         sl = g_slist_remove(sl, sl->data);
923     }
926 /**
927  * Writes the gradient's internal vector (whether from its own stops, or
928  * inherited from refs) into the gradient repr as svg:stop elements.
929  */
930 void
931 sp_gradient_repr_write_vector(SPGradient *gr)
933     g_return_if_fail(gr != NULL);
934     g_return_if_fail(SP_IS_GRADIENT(gr));
936     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
937     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
939     /* We have to be careful, as vector may be our own, so construct repr list at first */
940     GSList *cl = NULL;
942     for (guint i = 0; i < gr->vector.stops.size(); i++) {
943         Inkscape::CSSOStringStream os;
944         Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
945         sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
946         /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
947          * sense for offset proportions. */
948         gchar c[64];
949         sp_svg_write_color(c, sizeof(c), gr->vector.stops[i].color.toRGBA32( 0x00 ));
950         os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
951         child->setAttribute("style", os.str().c_str());
952         /* Order will be reversed here */
953         cl = g_slist_prepend(cl, child);
954     }
956     sp_gradient_repr_clear_vector(gr);
958     /* And insert new children from list */
959     while (cl) {
960         Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
961         repr->addChild(child, NULL);
962         Inkscape::GC::release(child);
963         cl = g_slist_remove(cl, child);
964     }
968 void SPGradientImpl::gradientRefModified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
970     if ( gradient->invalidateVector() ) {
971         SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
972         // Conditional to avoid causing infinite loop if there's a cycle in the href chain.
973     }
976 /** Return true if change made. */
977 bool SPGradient::invalidateVector()
979     bool ret = false;
981     if (color != NULL) {
982         g_free(color);
983         color = NULL;
984         ret = true;
985     }
987     if (vector.built) {
988         vector.built = false;
989         vector.stops.clear();
990         ret = true;
991     }
993     return ret;
996 /** Creates normalized color vector */
997 void SPGradient::rebuildVector()
999     gint len = 0;
1000     for ( SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
1001           child != NULL ;
1002           child = SP_OBJECT_NEXT(child) ) {
1003         if (SP_IS_STOP(child)) {
1004             len ++;
1005         }
1006     }
1008     has_stops = (len != 0);
1010     vector.stops.clear();
1012     SPGradient *reffed = ref->getObject();
1013     if ( !hasStops() && reffed ) {
1014         /* Copy vector from referenced gradient */
1015         vector.built = true;   // Prevent infinite recursion.
1016         reffed->ensureVector();
1017         if (!reffed->vector.stops.empty()) {
1018             vector.built = reffed->vector.built;
1019             vector.stops.assign(reffed->vector.stops.begin(), reffed->vector.stops.end());
1020             return;
1021         }
1022     }
1024     for (SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
1025          child != NULL;
1026          child = SP_OBJECT_NEXT(child) ) {
1027         if (SP_IS_STOP(child)) {
1028             SPStop *stop = SP_STOP(child);
1030             SPGradientStop gstop;
1031             if (vector.stops.size() > 0) {
1032                 // "Each gradient offset value is required to be equal to or greater than the
1033                 // previous gradient stop's offset value. If a given gradient stop's offset
1034                 // value is not equal to or greater than all previous offset values, then the
1035                 // offset value is adjusted to be equal to the largest of all previous offset
1036                 // values."
1037                 gstop.offset = MAX(stop->offset, vector.stops.back().offset);
1038             } else {
1039                 gstop.offset = stop->offset;
1040             }
1042             // "Gradient offset values less than 0 (or less than 0%) are rounded up to
1043             // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
1044             // down to 100%."
1045             gstop.offset = CLAMP(gstop.offset, 0, 1);
1047             gstop.color = sp_stop_get_color(stop);
1048             gstop.opacity = stop->opacity;
1050             vector.stops.push_back(gstop);
1051         }
1052     }
1054     // Normalize per section 13.2.4 of SVG 1.1.
1055     if (vector.stops.size() == 0) {
1056         /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
1057          * paint style."
1058          */
1059         {
1060             SPGradientStop gstop;
1061             gstop.offset = 0.0;
1062             gstop.color.set( 0x00000000 );
1063             gstop.opacity = 0.0;
1064             vector.stops.push_back(gstop);
1065         }
1066         {
1067             SPGradientStop gstop;
1068             gstop.offset = 1.0;
1069             gstop.color.set( 0x00000000 );
1070             gstop.opacity = 0.0;
1071             vector.stops.push_back(gstop);
1072         }
1073     } else {
1074         /* "If one stop is defined, then paint with the solid color fill using the color defined
1075          * for that gradient stop."
1076          */
1077         if (vector.stops.front().offset > 0.0) {
1078             // If the first one is not at 0, then insert a copy of the first at 0.
1079             SPGradientStop gstop;
1080             gstop.offset = 0.0;
1081             gstop.color = vector.stops.front().color;
1082             gstop.opacity = vector.stops.front().opacity;
1083             vector.stops.insert(vector.stops.begin(), gstop);
1084         }
1085         if (vector.stops.back().offset < 1.0) {
1086             // If the last one is not at 1, then insert a copy of the last at 1.
1087             SPGradientStop gstop;
1088             gstop.offset = 1.0;
1089             gstop.color = vector.stops.back().color;
1090             gstop.opacity = vector.stops.back().opacity;
1091             vector.stops.push_back(gstop);
1092         }
1093     }
1095     vector.built = true;
1098 /**
1099  * The gradient's color array is newly created and set up from vector.
1100  */
1101 void SPGradient::ensureColors()
1103     if (!vector.built) {
1104         rebuildVector();
1105     }
1106     g_return_if_fail(!vector.stops.empty());
1108     /// \todo Where is the memory freed?
1109     if (!color) {
1110         color = g_new(guchar, 4 * NCOLORS);
1111     }
1113     // This assumes that vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1114     // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1115     // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1117     double remainder[4] = {0,0,0,0};
1118     double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1119     switch(spread) {
1120     case SP_GRADIENT_SPREAD_PAD:
1121         remainder[0] = 0.5*vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1122         remainder[1] = 0.5*vector.stops[0].color.v.c[1];
1123         remainder[2] = 0.5*vector.stops[0].color.v.c[2];
1124         remainder[3] = 0.5*vector.stops[0].opacity;
1125         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
1126         remainder_for_end[1] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[1];
1127         remainder_for_end[2] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[2];
1128         remainder_for_end[3] = 0.5*vector.stops[vector.stops.size() - 1].opacity;
1129         break;
1130     case SP_GRADIENT_SPREAD_REFLECT:
1131     case SP_GRADIENT_SPREAD_REPEAT:
1132         // These two are handled differently, see below.
1133         break;
1134     default:
1135         g_error("Spread type not supported!");
1136     };
1137     for (unsigned int i = 0; i < vector.stops.size() - 1; i++) {
1138         double r0 = vector.stops[i].color.v.c[0];
1139         double g0 = vector.stops[i].color.v.c[1];
1140         double b0 = vector.stops[i].color.v.c[2];
1141         double a0 = vector.stops[i].opacity;
1142         double r1 = vector.stops[i+1].color.v.c[0];
1143         double g1 = vector.stops[i+1].color.v.c[1];
1144         double b1 = vector.stops[i+1].color.v.c[2];
1145         double a1 = vector.stops[i+1].opacity;
1146         double o0 = vector.stops[i].offset * (NCOLORS-1);
1147         double o1 = vector.stops[i + 1].offset * (NCOLORS-1);
1148         unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1149         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
1151         if (oe == ob) {
1152             // Simple case, this interval starts and stops within one cell
1153             // The contribution of this interval is:
1154             //    (o1-o0)*(c(o0)+c(o1))/2
1155             //  = (o1-o0)*(c0+c1)/2
1156             double dt = 0.5*(o1-o0);
1157             remainder[0] += dt*(r0 + r1);
1158             remainder[1] += dt*(g0 + g1);
1159             remainder[2] += dt*(b0 + b1);
1160             remainder[3] += dt*(a0 + a1);
1161         } else {
1162             // First compute colors for the cells which are fully covered by the current interval.
1163             // The prefiltered values are equal to the midpoint of each cell here.
1164             //  f = (j-o0)/(o1-o0)
1165             //    = j*(1/(o1-o0)) - o0/(o1-o0)
1166             double f = (ob-o0) / (o1-o0);
1167             double df = 1. / (o1-o0);
1168             for (unsigned int j = ob+1; j < oe; j++) {
1169                 f += df;
1170                 color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1171                 color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1172                 color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1173                 color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1174             }
1176             // Now handle the beginning
1177             // The contribution of the last point is already in remainder.
1178             // The contribution of this point is:
1179             //    (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1180             //  = (ob+.5-o0)*c((o0+ob+.5)/2)
1181             //  = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1182             //  = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1183             double dt = ob+.5-o0;
1184             f = 0.5*dt*df;
1185             if (ob==0 && spread==SP_GRADIENT_SPREAD_REFLECT) {
1186                 // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1187                 color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1188                 color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1189                 color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1190                 color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1191             } else if (ob==0 && spread==SP_GRADIENT_SPREAD_REPEAT) {
1192                 // 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.
1193                 remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1194                 remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1195                 remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1196                 remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1197             } else {
1198                 // The first half of the cell was already in remainder.
1199                 color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1200                 color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1201                 color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1202                 color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1203             }
1205             // Now handle the end, which should end up in remainder
1206             // The contribution of this point is:
1207             //    (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1208             //  = (o1-oe+.5)*c((o1+oe-.5)/2)
1209             //  = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1210             dt = o1-oe+.5;
1211             f = (0.5*(o1+oe-.5)-o0)*df;
1212             remainder[0] = dt*(r0 + f*(r1-r0));
1213             remainder[1] = dt*(g0 + f*(g1-g0));
1214             remainder[2] = dt*(b0 + f*(b1-b0));
1215             remainder[3] = dt*(a0 + f*(a1-a0));
1216         }
1217     }
1218     switch(spread) {
1219     case SP_GRADIENT_SPREAD_PAD:
1220         color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1221         color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1222         color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1223         color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1224         break;
1225     case SP_GRADIENT_SPREAD_REFLECT:
1226         // The second half is the same as the first half, so multiply by 2.
1227         color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1228         color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1229         color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1230         color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1231         break;
1232     case SP_GRADIENT_SPREAD_REPEAT:
1233         // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1234         color[0] = color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1235         color[1] = color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1236         color[2] = color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1237         color[3] = color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1238         break;
1239     }
1242 /**
1243  * Renders gradient vector to buffer as line.
1244  *
1245  * RGB buffer background should be set up beforehand.
1246  *
1247  * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1248  * @param span Full integer width of requested gradient.
1249  * @param pos Buffer starting position in span.
1250  */
1251 static void
1252 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1253                                     gint const len, gint const pos, gint const span)
1255     g_return_if_fail(gradient != NULL);
1256     g_return_if_fail(SP_IS_GRADIENT(gradient));
1257     g_return_if_fail(buf != NULL);
1258     g_return_if_fail(len > 0);
1259     g_return_if_fail(pos >= 0);
1260     g_return_if_fail(pos + len <= span);
1261     g_return_if_fail(span > 0);
1263     if (!gradient->color) {
1264         gradient->ensureColors();
1265     }
1267     gint idx = (pos * 1024 << 8) / span;
1268     gint didx = (1024 << 8) / span;
1270     for (gint x = 0; x < len; x++) {
1271         /// \todo Can this be done with 4 byte copies?
1272         *buf++ = gradient->color[4 * (idx >> 8)];
1273         *buf++ = gradient->color[4 * (idx >> 8) + 1];
1274         *buf++ = gradient->color[4 * (idx >> 8) + 2];
1275         *buf++ = gradient->color[4 * (idx >> 8) + 3];
1276         idx += didx;
1277     }
1280 /**
1281  * Render rectangular RGBA area from gradient vector.
1282  */
1283 void
1284 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1285                                      gint const width, gint const height, gint const rowstride,
1286                                      gint const pos, gint const span, bool const horizontal)
1288     g_return_if_fail(gradient != NULL);
1289     g_return_if_fail(SP_IS_GRADIENT(gradient));
1290     g_return_if_fail(buf != NULL);
1291     g_return_if_fail(width > 0);
1292     g_return_if_fail(height > 0);
1293     g_return_if_fail(pos >= 0);
1294     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1295     g_return_if_fail(span > 0);
1297     if (horizontal) {
1298         sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1299         for (gint y = 1; y < height; y++) {
1300             memcpy(buf + y * rowstride, buf, 4 * width);
1301         }
1302     } else {
1303         guchar *tmp = (guchar *)alloca(4 * height);
1304         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1305         for (gint y = 0; y < height; y++) {
1306             guchar *b = buf + y * rowstride;
1307             for (gint x = 0; x < width; x++) {
1308                 *b++ = tmp[0];
1309                 *b++ = tmp[1];
1310                 *b++ = tmp[2];
1311                 *b++ = tmp[3];
1312             }
1313             tmp += 4;
1314         }
1315     }
1318 /**
1319  * Render rectangular RGB area from gradient vector.
1320  */
1321 void
1322 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1323                                     gint const width, gint const height, gint const /*rowstride*/,
1324                                     gint const pos, gint const span, bool const horizontal)
1326     g_return_if_fail(gradient != NULL);
1327     g_return_if_fail(SP_IS_GRADIENT(gradient));
1328     g_return_if_fail(buf != NULL);
1329     g_return_if_fail(width > 0);
1330     g_return_if_fail(height > 0);
1331     g_return_if_fail(pos >= 0);
1332     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1333     g_return_if_fail(span > 0);
1335     if (horizontal) {
1336         guchar *tmp = (guchar*)alloca(4 * width);
1337         sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1338         for (gint y = 0; y < height; y++) {
1339             guchar *t = tmp;
1340             for (gint x = 0; x < width; x++) {
1341                 gint a = t[3];
1342                 gint fc = (t[0] - buf[0]) * a;
1343                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1344                 fc = (t[1] - buf[1]) * a;
1345                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1346                 fc = (t[2] - buf[2]) * a;
1347                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1348                 buf += 3;
1349                 t += 4;
1350             }
1351         }
1352     } else {
1353         guchar *tmp = (guchar*)alloca(4 * height);
1354         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1355         for (gint y = 0; y < height; y++) {
1356             guchar *t = tmp + 4 * y;
1357             for (gint x = 0; x < width; x++) {
1358                 gint a = t[3];
1359                 gint fc = (t[0] - buf[0]) * a;
1360                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1361                 fc = (t[1] - buf[1]) * a;
1362                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1363                 fc = (t[2] - buf[2]) * a;
1364                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1365             }
1366         }
1367     }
1370 Geom::Matrix
1371 sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1373     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1374         return ( Geom::Scale(bbox.dimensions())
1375                  * Geom::Translate(bbox.min())
1376                  * Geom::Matrix(ctm) );
1377     } else {
1378         return ctm;
1379     }
1382 Geom::Matrix
1383 sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1385     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1386         return ( gr->gradientTransform
1387                  * Geom::Scale(bbox.dimensions())
1388                  * Geom::Translate(bbox.min())
1389                  * Geom::Matrix(ctm) );
1390     } else {
1391         return gr->gradientTransform * ctm;
1392     }
1395 void
1396 sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1397                             Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1399     gr->gradientTransform = gs2d * ctm.inverse();
1400     if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1401         gr->gradientTransform = ( gr->gradientTransform
1402                                   * Geom::Translate(-bbox.min())
1403                                   * Geom::Scale(bbox.dimensions()).inverse() );
1404     }
1405     gr->gradientTransform_set = TRUE;
1407     SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1410 /*
1411  * Linear Gradient
1412  */
1414 class SPLGPainter;
1416 /// A context with linear gradient, painter, and gradient renderer.
1417 struct SPLGPainter {
1418     SPPainter painter;
1419     SPLinearGradient *lg;
1421     NRLGradientRenderer lgr;
1423     static SPPainter * painter_new(SPPaintServer *ps,
1424                                    Geom::Matrix const &full_transform,
1425                                    Geom::Matrix const &parent_transform,
1426                                    NRRect const *bbox);
1427 };
1429 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1430 static void sp_lineargradient_init(SPLinearGradient *lg);
1432 static void sp_lineargradient_build(SPObject *object,
1433                                     SPDocument *document,
1434                                     Inkscape::XML::Node *repr);
1435 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1436 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1437                                                     guint flags);
1439 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1441 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1443 static SPGradientClass *lg_parent_class;
1445 /**
1446  * Register SPLinearGradient class and return its type.
1447  */
1448 GType
1449 sp_lineargradient_get_type()
1451     static GType type = 0;
1452     if (!type) {
1453         GTypeInfo info = {
1454             sizeof(SPLinearGradientClass),
1455             NULL, NULL,
1456             (GClassInitFunc) sp_lineargradient_class_init,
1457             NULL, NULL,
1458             sizeof(SPLinearGradient),
1459             16,
1460             (GInstanceInitFunc) sp_lineargradient_init,
1461             NULL,   /* value_table */
1462         };
1463         type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1464     }
1465     return type;
1468 /**
1469  * SPLinearGradient vtable initialization.
1470  */
1471 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1473     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1474     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1476     lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1478     sp_object_class->build = sp_lineargradient_build;
1479     sp_object_class->set = sp_lineargradient_set;
1480     sp_object_class->write = sp_lineargradient_write;
1482     ps_class->painter_new = SPLGPainter::painter_new;
1483     ps_class->painter_free = sp_lineargradient_painter_free;
1486 /**
1487  * Callback for SPLinearGradient object initialization.
1488  */
1489 static void sp_lineargradient_init(SPLinearGradient *lg)
1491     lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1492     lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
1493     lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1494     lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
1497 /**
1498  * Callback: set attributes from associated repr.
1499  */
1500 static void sp_lineargradient_build(SPObject *object,
1501                                     SPDocument *document,
1502                                     Inkscape::XML::Node *repr)
1504     if (((SPObjectClass *) lg_parent_class)->build)
1505         (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1507     sp_object_read_attr(object, "x1");
1508     sp_object_read_attr(object, "y1");
1509     sp_object_read_attr(object, "x2");
1510     sp_object_read_attr(object, "y2");
1513 /**
1514  * Callback: set attribute.
1515  */
1516 static void
1517 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1519     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1521     switch (key) {
1522         case SP_ATTR_X1:
1523             lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1524             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1525             break;
1526         case SP_ATTR_Y1:
1527             lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1528             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1529             break;
1530         case SP_ATTR_X2:
1531             lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1532             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1533             break;
1534         case SP_ATTR_Y2:
1535             lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1536             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1537             break;
1538         default:
1539             if (((SPObjectClass *) lg_parent_class)->set)
1540                 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1541             break;
1542     }
1545 /**
1546  * Callback: write attributes to associated repr.
1547  */
1548 static Inkscape::XML::Node *
1549 sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1551     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1553     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1554         repr = xml_doc->createElement("svg:linearGradient");
1555     }
1557     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1558         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1559     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1560         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1561     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1562         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1563     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1564         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1566     if (((SPObjectClass *) lg_parent_class)->write)
1567         (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
1569     return repr;
1572 /**
1573  * Create linear gradient context.
1574  *
1575  * Basically we have to deal with transformations
1576  *
1577  * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1578  * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1579  * 2) gradientTransform
1580  * 3) bbox2user
1581  * 4) ctm == userspace to pixel grid
1582  *
1583  * See also (*) in sp-pattern about why we may need parent_transform.
1584  *
1585  * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1586  * and end < 1.
1587  */
1588 SPPainter * SPLGPainter::painter_new(SPPaintServer *ps,
1589                                      Geom::Matrix const &full_transform,
1590                                      Geom::Matrix const &/*parent_transform*/,
1591                                      NRRect const *bbox)
1593     SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1594     SPGradient *gr = SP_GRADIENT(ps);
1596     if (!gr->color) {
1597         gr->ensureColors();
1598     }
1600     SPLGPainter *lgp = g_new(SPLGPainter, 1);
1602     lgp->painter.type = SP_PAINTER_IND;
1603     lgp->painter.fill = sp_lg_fill;
1605     lgp->lg = lg;
1607     /** \todo
1608      * Technically speaking, we map NCOLORS on line [start,end] onto line
1609      * [0,1].  I almost think we should fill color array start and end in
1610      * that case. The alternative would be to leave these just empty garbage
1611      * or something similar. Originally I had 1023.9999 here - not sure
1612      * whether we have really to cut out ceil int (Lauris).
1613      */
1614     Geom::Matrix color2norm(Geom::identity());
1615     Geom::Matrix color2px;
1616     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1617         Geom::Matrix norm2pos(Geom::identity());
1619         /* BBox to user coordinate system */
1620         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1622         Geom::Matrix color2pos = color2norm * norm2pos;
1623         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1624         Geom::Matrix color2user = color2tpos * bbox2user;
1625         color2px = color2user * full_transform;
1627     } else {
1628         /* Problem: What to do, if we have mixed lengths and percentages? */
1629         /* Currently we do ignore percentages at all, but that is not good (lauris) */
1631         Geom::Matrix norm2pos(Geom::identity());
1632         Geom::Matrix color2pos = color2norm * norm2pos;
1633         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1634         color2px = color2tpos * full_transform;
1636     }
1637     // TODO: remove color2px_nr after converting to 2geom
1638     NR::Matrix color2px_nr = from_2geom(color2px);
1639     nr_lgradient_renderer_setup(&lgp->lgr, gr->color, gr->fetchSpread(), &color2px_nr,
1640                                 lg->x1.computed, lg->y1.computed,
1641                                 lg->x2.computed, lg->y2.computed);
1643     return (SPPainter *) lgp;
1646 static void
1647 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1649     g_free(painter);
1652 /**
1653  * Directly set properties of linear gradient and request modified.
1654  */
1655 void
1656 sp_lineargradient_set_position(SPLinearGradient *lg,
1657                                gdouble x1, gdouble y1,
1658                                gdouble x2, gdouble y2)
1660     g_return_if_fail(lg != NULL);
1661     g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1663     /* fixme: units? (Lauris)  */
1664     lg->x1.set(SVGLength::NONE, x1, x1);
1665     lg->y1.set(SVGLength::NONE, y1, y1);
1666     lg->x2.set(SVGLength::NONE, x2, x2);
1667     lg->y2.set(SVGLength::NONE, y2, y2);
1669     SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1672 /**
1673  * Callback when linear gradient object is rendered.
1674  */
1675 static void
1676 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1678     SPLGPainter *lgp = (SPLGPainter *) painter;
1680     if (lgp->lg->color == NULL) {
1681         lgp->lg->ensureColors();
1682         lgp->lgr.vector = lgp->lg->color;
1683     }
1685     nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1688 /*
1689  * Radial Gradient
1690  */
1692 class SPRGPainter;
1694 /// A context with radial gradient, painter, and gradient renderer.
1695 struct SPRGPainter {
1696     SPPainter painter;
1697     SPRadialGradient *rg;
1698     NRRGradientRenderer rgr;
1700     static SPPainter *painter_new(SPPaintServer *ps,
1701                                   Geom::Matrix const &full_transform,
1702                                   Geom::Matrix const &parent_transform,
1703                                   NRRect const *bbox);
1704 };
1706 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1707 static void sp_radialgradient_init(SPRadialGradient *rg);
1709 static void sp_radialgradient_build(SPObject *object,
1710                                     SPDocument *document,
1711                                     Inkscape::XML::Node *repr);
1712 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1713 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1714                                                     guint flags);
1715 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1717 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1719 static SPGradientClass *rg_parent_class;
1721 /**
1722  * Register SPRadialGradient class and return its type.
1723  */
1724 GType
1725 sp_radialgradient_get_type()
1727     static GType type = 0;
1728     if (!type) {
1729         GTypeInfo info = {
1730             sizeof(SPRadialGradientClass),
1731             NULL, NULL,
1732             (GClassInitFunc) sp_radialgradient_class_init,
1733             NULL, NULL,
1734             sizeof(SPRadialGradient),
1735             16,
1736             (GInstanceInitFunc) sp_radialgradient_init,
1737             NULL,   /* value_table */
1738         };
1739         type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1740     }
1741     return type;
1744 /**
1745  * SPRadialGradient vtable initialization.
1746  */
1747 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1749     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1750     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1752     rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1754     sp_object_class->build = sp_radialgradient_build;
1755     sp_object_class->set = sp_radialgradient_set;
1756     sp_object_class->write = sp_radialgradient_write;
1758     ps_class->painter_new = SPRGPainter::painter_new;
1759     ps_class->painter_free = sp_radialgradient_painter_free;
1762 /**
1763  * Callback for SPRadialGradient object initialization.
1764  */
1765 static void
1766 sp_radialgradient_init(SPRadialGradient *rg)
1768     rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1769     rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1770     rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1771     rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1772     rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1775 /**
1776  * Set radial gradient attributes from associated repr.
1777  */
1778 static void
1779 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1781     if (((SPObjectClass *) rg_parent_class)->build)
1782         (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1784     sp_object_read_attr(object, "cx");
1785     sp_object_read_attr(object, "cy");
1786     sp_object_read_attr(object, "r");
1787     sp_object_read_attr(object, "fx");
1788     sp_object_read_attr(object, "fy");
1791 /**
1792  * Set radial gradient attribute.
1793  */
1794 static void
1795 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1797     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1799     switch (key) {
1800         case SP_ATTR_CX:
1801             if (!rg->cx.read(value)) {
1802                 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1803             }
1804             if (!rg->fx._set) {
1805                 rg->fx.value = rg->cx.value;
1806                 rg->fx.computed = rg->cx.computed;
1807             }
1808             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1809             break;
1810         case SP_ATTR_CY:
1811             if (!rg->cy.read(value)) {
1812                 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1813             }
1814             if (!rg->fy._set) {
1815                 rg->fy.value = rg->cy.value;
1816                 rg->fy.computed = rg->cy.computed;
1817             }
1818             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1819             break;
1820         case SP_ATTR_R:
1821             if (!rg->r.read(value)) {
1822                 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1823             }
1824             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1825             break;
1826         case SP_ATTR_FX:
1827             if (!rg->fx.read(value)) {
1828                 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1829             }
1830             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1831             break;
1832         case SP_ATTR_FY:
1833             if (!rg->fy.read(value)) {
1834                 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1835             }
1836             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1837             break;
1838         default:
1839             if (((SPObjectClass *) rg_parent_class)->set)
1840                 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1841             break;
1842     }
1845 /**
1846  * Write radial gradient attributes to associated repr.
1847  */
1848 static Inkscape::XML::Node *
1849 sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1851     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1853     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1854         repr = xml_doc->createElement("svg:radialGradient");
1855     }
1857     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1858     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1859     if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1860     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1861     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1863     if (((SPObjectClass *) rg_parent_class)->write)
1864         (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1866     return repr;
1869 /**
1870  * Create radial gradient context.
1871  */
1872 SPPainter *SPRGPainter::painter_new(SPPaintServer *ps,
1873                                     Geom::Matrix const &full_transform,
1874                                     Geom::Matrix const &/*parent_transform*/,
1875                                     NRRect const *bbox)
1877     SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1878     SPGradient *gr = SP_GRADIENT(ps);
1880     if (!gr->color) {
1881         gr->ensureColors();
1882     }
1884     SPRGPainter *rgp = g_new(SPRGPainter, 1);
1886     rgp->painter.type = SP_PAINTER_IND;
1887     rgp->painter.fill = sp_rg_fill;
1889     rgp->rg = rg;
1891     Geom::Matrix gs2px;
1893     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1894         /** \todo
1895          * fixme: We may try to normalize here too, look at
1896          * linearGradient (Lauris)
1897          */
1899         /* BBox to user coordinate system */
1900         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1902         Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1904         gs2px = gs2user * full_transform;
1905     } else {
1906         /** \todo
1907          * Problem: What to do, if we have mixed lengths and percentages?
1908          * Currently we do ignore percentages at all, but that is not
1909          * good (lauris)
1910          */
1912         gs2px = gr->gradientTransform * full_transform;
1913     }
1914     // TODO: remove gs2px_nr after converting to 2geom
1915     NR::Matrix gs2px_nr = from_2geom(gs2px);
1916     nr_rgradient_renderer_setup(&rgp->rgr, gr->color, gr->fetchSpread(),
1917                                 &gs2px_nr,
1918                                 rg->cx.computed, rg->cy.computed,
1919                                 rg->fx.computed, rg->fy.computed,
1920                                 rg->r.computed);
1922     return (SPPainter *) rgp;
1925 static void
1926 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1928     g_free(painter);
1931 /**
1932  * Directly set properties of radial gradient and request modified.
1933  */
1934 void
1935 sp_radialgradient_set_position(SPRadialGradient *rg,
1936                                gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1938     g_return_if_fail(rg != NULL);
1939     g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1941     /* fixme: units? (Lauris)  */
1942     rg->cx.set(SVGLength::NONE, cx, cx);
1943     rg->cy.set(SVGLength::NONE, cy, cy);
1944     rg->fx.set(SVGLength::NONE, fx, fx);
1945     rg->fy.set(SVGLength::NONE, fy, fy);
1946     rg->r.set(SVGLength::NONE, r, r);
1948     SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1951 /**
1952  * Callback when radial gradient object is rendered.
1953  */
1954 static void
1955 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1957     SPRGPainter *rgp = (SPRGPainter *) painter;
1959     if (rgp->rg->color == NULL) {
1960         rgp->rg->ensureColors();
1961         rgp->rgr.vector = rgp->rg->color;
1962     }
1964     nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1967 /*
1968   Local Variables:
1969   mode:c++
1970   c-file-style:"stroustrup"
1971   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1972   indent-tabs-mode:nil
1973   fill-column:99
1974   End:
1975 */
1976 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :