Code

Made some improvements to the osx-build.sh script to use BZR and to be able
[inkscape.git] / src / sp-gradient.cpp
1 #define __SP_GRADIENT_C__
3 /** \file
4  * SPGradient, SPStop, SPLinearGradient, SPRadialGradient.
5  */
6 /*
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
11  *
12  * Copyright (C) 1999-2002 Lauris Kaplinski
13  * Copyright (C) 2000-2001 Ximian, Inc.
14  * Copyright (C) 2004 David Turner
15  * Copyright (C) 2009 Jasper van de Gronde
16  *
17  * Released under GNU GPL, read the file 'COPYING' for more information
18  *
19  */
21 #define noSP_GRADIENT_VERBOSE
23 #include <cstring>
24 #include <string>
26 #include <libnr/nr-matrix-fns.h>
27 #include <libnr/nr-matrix-ops.h>
28 #include <libnr/nr-matrix-scale-ops.h>
29 #include <2geom/transforms.h>
31 #include <sigc++/functors/ptr_fun.h>
32 #include <sigc++/adaptors/bind.h>
34 #include "libnr/nr-gradient.h"
35 #include "libnr/nr-pixops.h"
36 #include "svg/svg.h"
37 #include "svg/svg-color.h"
38 #include "svg/css-ostringstream.h"
39 #include "attributes.h"
40 #include "document-private.h"
41 #include "gradient-chemistry.h"
42 #include "sp-gradient-reference.h"
43 #include "sp-linear-gradient.h"
44 #include "sp-radial-gradient.h"
45 #include "sp-stop.h"
46 #include "streq.h"
47 #include "uri.h"
48 #include "xml/repr.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 /**
66  * Registers SPStop class and returns its type.
67  */
68 GType
69 sp_stop_get_type()
70 {
71     static GType type = 0;
72     if (!type) {
73         GTypeInfo info = {
74             sizeof(SPStopClass),
75             NULL, NULL,
76             (GClassInitFunc) sp_stop_class_init,
77             NULL, NULL,
78             sizeof(SPStop),
79             16,
80             (GInstanceInitFunc) sp_stop_init,
81             NULL,   /* value_table */
82         };
83         type = g_type_register_static(SP_TYPE_OBJECT, "SPStop", &info, (GTypeFlags)0);
84     }
85     return type;
86 }
88 /**
89  * Callback to initialize SPStop vtable.
90  */
91 static void sp_stop_class_init(SPStopClass *klass)
92 {
93     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
95     stop_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
97     sp_object_class->build = sp_stop_build;
98     sp_object_class->set = sp_stop_set;
99     sp_object_class->write = sp_stop_write;
102 /**
103  * Callback to initialize SPStop object.
104  */
105 static void
106 sp_stop_init(SPStop *stop)
108     stop->offset = 0.0;
109     stop->currentColor = false;
110     stop->specified_color.set( 0x000000ff );
111     stop->opacity = 1.0;
114 /**
115  * Virtual build: set stop attributes from its associated XML node.
116  */
117 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
119     if (((SPObjectClass *) stop_parent_class)->build)
120         (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
122     sp_object_read_attr(object, "offset");
123     sp_object_read_attr(object, "stop-color");
124     sp_object_read_attr(object, "stop-opacity");
125     sp_object_read_attr(object, "style");
128 /**
129  * Virtual set: set attribute to value.
130  */
131 static void
132 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
134     SPStop *stop = SP_STOP(object);
136     switch (key) {
137         case SP_ATTR_STYLE: {
138         /** \todo
139          * fixme: We are reading simple values 3 times during build (Lauris).
140          * \par
141          * We need presentation attributes etc.
142          * \par
143          * remove the hackish "style reading" from here: see comments in
144          * sp_object_get_style_property about the bugs in our current
145          * approach.  However, note that SPStyle doesn't currently have
146          * stop-color and stop-opacity properties.
147          */
148             {
149                 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
150                 if (streq(p, "currentColor")) {
151                     stop->currentColor = true;
152                 } else {
153                     guint32 const color = sp_svg_read_color(p, 0);
154                     stop->specified_color.set( color );
155                 }
156             }
157             {
158                 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
159                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
160                 stop->opacity = opacity;
161             }
162             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
163             break;
164         }
165         case SP_PROP_STOP_COLOR: {
166             {
167                 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
168                 if (streq(p, "currentColor")) {
169                     stop->currentColor = true;
170                 } else {
171                     stop->currentColor = false;
172                     guint32 const color = sp_svg_read_color(p, 0);
173                     stop->specified_color.set( color );
174                 }
175             }
176             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
177             break;
178         }
179         case SP_PROP_STOP_OPACITY: {
180             {
181                 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
182                 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
183                 stop->opacity = opacity;
184             }
185             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
186             break;
187         }
188         case SP_ATTR_OFFSET: {
189             stop->offset = sp_svg_read_percentage(value, 0.0);
190             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
191             break;
192         }
193         default: {
194             if (((SPObjectClass *) stop_parent_class)->set)
195                 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
196             break;
197         }
198     }
201 /**
202  * Virtual write: write object attributes to repr.
203  */
204 static Inkscape::XML::Node *
205 sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
207     SPStop *stop = SP_STOP(object);
209     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
210         repr = xml_doc->createElement("svg:stop");
211     }
213     guint32 specifiedcolor = stop->specified_color.toRGBA32( 255 );
214     gfloat opacity = stop->opacity;
216     if (((SPObjectClass *) stop_parent_class)->write)
217         (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
219     // Since we do a hackish style setting here (because SPStyle does not support stop-color and
220     // stop-opacity), we must do it AFTER calling the parent write method; otherwise
221     // sp_object_write would clear our style= attribute (bug 1695287)
223     Inkscape::CSSOStringStream os;
224     os << "stop-color:";
225     if (stop->currentColor) {
226         os << "currentColor";
227     } else {
228         gchar c[64];
229         sp_svg_write_color(c, sizeof(c), specifiedcolor);
230         os << c;
231     }
232     os << ";stop-opacity:" << opacity;
233     repr->setAttribute("style", os.str().c_str());
234     repr->setAttribute("stop-color", NULL);
235     repr->setAttribute("stop-opacity", NULL);
236     sp_repr_set_css_double(repr, "offset", stop->offset);
237     /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no sense
238      * for offset proportions. */
240     return repr;
243 /**
244  * Return stop's color as 32bit value.
245  */
246 guint32
247 sp_stop_get_rgba32(SPStop const *const stop)
249     guint32 rgb0 = 0;
250     /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
251      * value depends on user agent, and don't give any further restrictions that I can
252      * see.) */
253     if (stop->currentColor) {
254         char const *str = sp_object_get_style_property(stop, "color", NULL);
255         if (str) {
256             rgb0 = sp_svg_read_color(str, rgb0);
257         }
258         unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
259         g_return_val_if_fail((alpha & ~0xff) == 0,
260                              rgb0 | 0xff);
261         return rgb0 | alpha;
262     } else {
263         return stop->specified_color.toRGBA32( stop->opacity );
264     }
267 /**
268  * Return stop's color as SPColor.
269  */
270 static SPColor
271 sp_stop_get_color(SPStop const *const stop)
273     if (stop->currentColor) {
274         char const *str = sp_object_get_style_property(stop, "color", NULL);
275         guint32 const dfl = 0;
276         /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
277          * value depends on user agent, and don't give any further restrictions that I can
278          * see.) */
279         guint32 color = dfl;
280         if (str) {
281             color = sp_svg_read_color(str, dfl);
282         }
283         SPColor ret( color );
284         return ret;
285     } else {
286         return stop->specified_color;
287     }
290 /*
291  * Gradient
292  */
294 static void sp_gradient_class_init(SPGradientClass *klass);
295 static void sp_gradient_init(SPGradient *gr);
297 static void sp_gradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
298 static void sp_gradient_release(SPObject *object);
299 static void sp_gradient_set(SPObject *object, unsigned key, gchar const *value);
300 static void sp_gradient_child_added(SPObject *object,
301                                     Inkscape::XML::Node *child,
302                                     Inkscape::XML::Node *ref);
303 static void sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child);
304 static void sp_gradient_modified(SPObject *object, guint flags);
305 static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
306                                               guint flags);
308 static void gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient);
310 static bool sp_gradient_invalidate_vector(SPGradient *gr);
311 static void sp_gradient_rebuild_vector(SPGradient *gr);
313 static void gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gradient);
315 SPGradientSpread sp_gradient_get_spread(SPGradient *gradient);
316 SPGradientUnits sp_gradient_get_units(SPGradient *gradient);
318 static SPPaintServerClass *gradient_parent_class;
320 /**
321  * Registers SPGradient class and returns its type.
322  */
323 GType
324 sp_gradient_get_type()
326     static GType gradient_type = 0;
327     if (!gradient_type) {
328         GTypeInfo gradient_info = {
329             sizeof(SPGradientClass),
330             NULL, NULL,
331             (GClassInitFunc) sp_gradient_class_init,
332             NULL, NULL,
333             sizeof(SPGradient),
334             16,
335             (GInstanceInitFunc) sp_gradient_init,
336             NULL,   /* value_table */
337         };
338         gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
339                                                &gradient_info, (GTypeFlags)0);
340     }
341     return gradient_type;
344 /**
345  * SPGradient vtable initialization.
346  */
347 static void
348 sp_gradient_class_init(SPGradientClass *klass)
350     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
352     gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
354     sp_object_class->build = sp_gradient_build;
355     sp_object_class->release = sp_gradient_release;
356     sp_object_class->set = sp_gradient_set;
357     sp_object_class->child_added = sp_gradient_child_added;
358     sp_object_class->remove_child = sp_gradient_remove_child;
359     sp_object_class->modified = sp_gradient_modified;
360     sp_object_class->write = sp_gradient_write;
363 /**
364  * Callback for SPGradient object initialization.
365  */
366 static void
367 sp_gradient_init(SPGradient *gr)
369     gr->ref = new SPGradientReference(SP_OBJECT(gr));
370     gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(gradient_ref_changed), gr));
372     /** \todo
373      * Fixme: reprs being rearranged (e.g. via the XML editor)
374      * may require us to clear the state.
375      */
376     gr->state = SP_GRADIENT_STATE_UNKNOWN;
378     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
379     gr->units_set = FALSE;
381     gr->gradientTransform = Geom::identity();
382     gr->gradientTransform_set = FALSE;
384     gr->spread = SP_GRADIENT_SPREAD_PAD;
385     gr->spread_set = FALSE;
387     gr->has_stops = FALSE;
389     gr->vector.built = false;
390     gr->vector.stops.clear();
392     gr->color = NULL;
394     new (&gr->modified_connection) sigc::connection();
397 /**
398  * Virtual build: set gradient attributes from its associated repr.
399  */
400 static void
401 sp_gradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
403     SPGradient *gradient = SP_GRADIENT(object);
405     if (((SPObjectClass *) gradient_parent_class)->build)
406         (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
408     SPObject *ochild;
409     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
410         if (SP_IS_STOP(ochild)) {
411             gradient->has_stops = TRUE;
412             break;
413         }
414     }
416     sp_object_read_attr(object, "gradientUnits");
417     sp_object_read_attr(object, "gradientTransform");
418     sp_object_read_attr(object, "spreadMethod");
419     sp_object_read_attr(object, "xlink:href");
421     /* Register ourselves */
422     sp_document_add_resource(document, "gradient", object);
425 /**
426  * Virtual release of SPGradient members before destruction.
427  */
428 static void
429 sp_gradient_release(SPObject *object)
431     SPGradient *gradient = (SPGradient *) object;
433 #ifdef SP_GRADIENT_VERBOSE
434     g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
435 #endif
437     if (SP_OBJECT_DOCUMENT(object)) {
438         /* Unregister ourselves */
439         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
440     }
442     if (gradient->ref) {
443         gradient->modified_connection.disconnect();
444         gradient->ref->detach();
445         delete gradient->ref;
446         gradient->ref = NULL;
447     }
449     if (gradient->color) {
450         g_free(gradient->color);
451         gradient->color = NULL;
452     }
454     gradient->modified_connection.~connection();
456     if (((SPObjectClass *) gradient_parent_class)->release)
457         ((SPObjectClass *) gradient_parent_class)->release(object);
460 /**
461  * Set gradient attribute to value.
462  */
463 static void
464 sp_gradient_set(SPObject *object, unsigned key, gchar const *value)
466     SPGradient *gr = SP_GRADIENT(object);
468     switch (key) {
469         case SP_ATTR_GRADIENTUNITS:
470             if (value) {
471                 if (!strcmp(value, "userSpaceOnUse")) {
472                     gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
473                 } else {
474                     gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
475                 }
476                 gr->units_set = TRUE;
477             } else {
478                 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
479                 gr->units_set = FALSE;
480             }
481             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
482             break;
483         case SP_ATTR_GRADIENTTRANSFORM: {
484             Geom::Matrix t;
485             if (value && sp_svg_transform_read(value, &t)) {
486                 gr->gradientTransform = t;
487                 gr->gradientTransform_set = TRUE;
488             } else {
489                 gr->gradientTransform = Geom::identity();
490                 gr->gradientTransform_set = FALSE;
491             }
492             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
493             break;
494         }
495         case SP_ATTR_SPREADMETHOD:
496             if (value) {
497                 if (!strcmp(value, "reflect")) {
498                     gr->spread = SP_GRADIENT_SPREAD_REFLECT;
499                 } else if (!strcmp(value, "repeat")) {
500                     gr->spread = SP_GRADIENT_SPREAD_REPEAT;
501                 } else {
502                     gr->spread = SP_GRADIENT_SPREAD_PAD;
503                 }
504                 gr->spread_set = TRUE;
505             } else {
506                 gr->spread_set = FALSE;
507             }
508             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
509             break;
510         case SP_ATTR_XLINK_HREF:
511             if (value) {
512                 try {
513                     gr->ref->attach(Inkscape::URI(value));
514                 } catch (Inkscape::BadURIException &e) {
515                     g_warning("%s", e.what());
516                     gr->ref->detach();
517                 }
518             } else {
519                 gr->ref->detach();
520             }
521             break;
522         default:
523             if (((SPObjectClass *) gradient_parent_class)->set)
524                 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
525             break;
526     }
529 /**
530  * Gets called when the gradient is (re)attached to another gradient.
531  */
532 static void
533 gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gr)
535     if (old_ref) {
536         gr->modified_connection.disconnect();
537     }
538     if ( SP_IS_GRADIENT(ref)
539          && ref != gr )
540     {
541         gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&gradient_ref_modified), gr));
542     }
544     // Per SVG, all unset attributes must be inherited from linked gradient.
545     // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
546     // but without setting the _set flags.
547     // FIXME: do the same for gradientTransform too
548     if (!gr->units_set)
549         gr->units = sp_gradient_get_units (gr);
550     if (!gr->spread_set)
551         gr->spread = sp_gradient_get_spread (gr);
553     /// \todo Fixme: what should the flags (second) argument be? */
554     gradient_ref_modified(ref, 0, gr);
557 /**
558  * Callback for child_added event.
559  */
560 static void
561 sp_gradient_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
563     SPGradient *gr = SP_GRADIENT(object);
565     sp_gradient_invalidate_vector(gr);
567     if (((SPObjectClass *) gradient_parent_class)->child_added)
568         (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
570     SPObject *ochild = sp_object_get_child_by_repr(object, child);
571     if ( ochild && SP_IS_STOP(ochild) ) {
572         gr->has_stops = TRUE;
573     }
575     /// \todo Fixme: should we schedule "modified" here?
576     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
579 /**
580  * Callback for remove_child event.
581  */
582 static void
583 sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child)
585     SPGradient *gr = SP_GRADIENT(object);
587     sp_gradient_invalidate_vector(gr);
589     if (((SPObjectClass *) gradient_parent_class)->remove_child)
590         (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
592     gr->has_stops = FALSE;
593     SPObject *ochild;
594     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
595         if (SP_IS_STOP(ochild)) {
596             gr->has_stops = TRUE;
597             break;
598         }
599     }
601     /* Fixme: should we schedule "modified" here? */
602     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
605 /**
606  * Callback for modified event.
607  */
608 static void
609 sp_gradient_modified(SPObject *object, guint flags)
611     SPGradient *gr = SP_GRADIENT(object);
613     if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
614         sp_gradient_invalidate_vector(gr);
615     }
617     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
618         sp_gradient_ensure_colors(gr);
619     }
621     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
622     flags &= SP_OBJECT_MODIFIED_CASCADE;
624     // FIXME: climb up the ladder of hrefs
625     GSList *l = NULL;
626     for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
627         g_object_ref(G_OBJECT(child));
628         l = g_slist_prepend(l, child);
629     }
630     l = g_slist_reverse(l);
631     while (l) {
632         SPObject *child = SP_OBJECT(l->data);
633         l = g_slist_remove(l, child);
634         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
635             child->emitModified(flags);
636         }
637         g_object_unref(G_OBJECT(child));
638     }
641 SPStop* SPGradient::getFirstStop()
643     SPStop* first = 0;
644     for (SPObject *ochild = sp_object_first_child(this); ochild && !first; ochild = SP_OBJECT_NEXT(ochild)) {
645         if (SP_IS_STOP(ochild)) {
646             first = SP_STOP(ochild);
647         }
648     }
649     return first;
652 int SPGradient::getStopCount() const
654     int count = 0;
656     for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
657         count++;
658     }
660     return count;
663 /**
664  * Write gradient attributes to repr.
665  */
666 static Inkscape::XML::Node *
667 sp_gradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
669     SPGradient *gr = SP_GRADIENT(object);
671     if (((SPObjectClass *) gradient_parent_class)->write)
672         (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
674     if (flags & SP_OBJECT_WRITE_BUILD) {
675         GSList *l = NULL;
676         for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
677             Inkscape::XML::Node *crepr;
678             crepr = child->updateRepr(xml_doc, NULL, flags);
679             if (crepr) l = g_slist_prepend(l, crepr);
680         }
681         while (l) {
682             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
683             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
684             l = g_slist_remove(l, l->data);
685         }
686     }
688     if (gr->ref->getURI()) {
689         gchar *uri_string = gr->ref->getURI()->toString();
690         repr->setAttribute("xlink:href", uri_string);
691         g_free(uri_string);
692     }
694     if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
695         switch (gr->units) {
696             case SP_GRADIENT_UNITS_USERSPACEONUSE:
697                 repr->setAttribute("gradientUnits", "userSpaceOnUse");
698                 break;
699             default:
700                 repr->setAttribute("gradientUnits", "objectBoundingBox");
701                 break;
702         }
703     }
705     if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
706         gchar *c=sp_svg_transform_write(gr->gradientTransform);
707         repr->setAttribute("gradientTransform", c);
708         g_free(c);
709     }
711     if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
712         /* FIXME: Ensure that gr->spread is the inherited value
713          * if !gr->spread_set.  Not currently happening: see sp_gradient_modified.
714          */
715         switch (gr->spread) {
716             case SP_GRADIENT_SPREAD_REFLECT:
717                 repr->setAttribute("spreadMethod", "reflect");
718                 break;
719             case SP_GRADIENT_SPREAD_REPEAT:
720                 repr->setAttribute("spreadMethod", "repeat");
721                 break;
722             default:
723                 repr->setAttribute("spreadMethod", "pad");
724                 break;
725         }
726     }
728     return repr;
731 /**
732  * Forces the vector to be built, if not present (i.e., changed).
733  *
734  * \pre SP_IS_GRADIENT(gradient).
735  */
736 void
737 sp_gradient_ensure_vector(SPGradient *gradient)
739     g_return_if_fail(gradient != NULL);
740     g_return_if_fail(SP_IS_GRADIENT(gradient));
742     if (!gradient->vector.built) {
743         sp_gradient_rebuild_vector(gradient);
744     }
747 /**
748  * Set units property of gradient and emit modified.
749  */
750 void
751 sp_gradient_set_units(SPGradient *gr, SPGradientUnits units)
753     if (units != gr->units) {
754         gr->units = units;
755         gr->units_set = TRUE;
756         SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
757     }
760 /**
761  * Set spread property of gradient and emit modified.
762  */
763 void
764 sp_gradient_set_spread(SPGradient *gr, SPGradientSpread spread)
766     if (spread != gr->spread) {
767         gr->spread = spread;
768         gr->spread_set = TRUE;
769         SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
770     }
773 /**
774  * Returns the first of {src, src-\>ref-\>getObject(),
775  * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
776  * for which \a match is true, or NULL if none found.
777  *
778  * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
779  * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
780  *
781  * \pre SP_IS_GRADIENT(src).
782  */
783 static SPGradient *
784 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
786     g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
788     /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2.  If there is a
789        loop, then once p1 has entered the loop, we'll detect it the next time the distance between
790        p1 and p2 is a multiple of the loop size. */
791     SPGradient *p1 = src, *p2 = src;
792     bool do1 = false;
793     for (;;) {
794         if (match(p2)) {
795             return p2;
796         }
798         p2 = p2->ref->getObject();
799         if (!p2) {
800             return p2;
801         }
802         if (do1) {
803             p1 = p1->ref->getObject();
804         }
805         do1 = !do1;
807         if ( p2 == p1 ) {
808             /* We've been here before, so return NULL to indicate that no matching gradient found
809              * in the chain. */
810             return NULL;
811         }
812     }
815 /**
816  * True if gradient has stops.
817  */
818 static bool has_stopsFN(SPGradient const *gr)
820     return SP_GRADIENT_HAS_STOPS(gr);
823 /**
824  * True if gradient has spread set.
825  */
826 static bool
827 has_spread_set(SPGradient const *gr)
829     return gr->spread_set;
832 /**
833  * True if gradient has units set.
834  */
835 static bool
836 has_units_set(SPGradient const *gr)
838     return gr->units_set;
842 SPGradient *SPGradient::getVector(bool force_vector)
844     SPGradient * src = chase_hrefs(this, has_stopsFN);
846     if (force_vector) {
847         src = sp_gradient_ensure_vector_normalized(src);
848     }
849     return src;
852 /**
853  * Returns the effective spread of given gradient (climbing up the refs chain if needed).
854  *
855  * \pre SP_IS_GRADIENT(gradient).
856  */
857 SPGradientSpread
858 sp_gradient_get_spread(SPGradient *gradient)
860     g_return_val_if_fail(SP_IS_GRADIENT(gradient), SP_GRADIENT_SPREAD_PAD);
862     SPGradient const *src = chase_hrefs(gradient, has_spread_set);
863     return ( src
864              ? src->spread
865              : SP_GRADIENT_SPREAD_PAD ); // pad is the default
868 /**
869  * Returns the effective units of given gradient (climbing up the refs chain if needed).
870  *
871  * \pre SP_IS_GRADIENT(gradient).
872  */
873 SPGradientUnits
874 sp_gradient_get_units(SPGradient *gradient)
876     g_return_val_if_fail(SP_IS_GRADIENT(gradient), SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX);
878     SPGradient const *src = chase_hrefs(gradient, has_units_set);
879     return ( src
880              ? src->units
881              : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
885 /**
886  * Clears the gradient's svg:stop children from its repr.
887  */
888 void
889 sp_gradient_repr_clear_vector(SPGradient *gr)
891     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
893     /* Collect stops from original repr */
894     GSList *sl = NULL;
895     for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
896         if (!strcmp(child->name(), "svg:stop")) {
897             sl = g_slist_prepend(sl, child);
898         }
899     }
900     /* Remove all stops */
901     while (sl) {
902         /** \todo
903          * fixme: This should work, unless we make gradient
904          * into generic group.
905          */
906         sp_repr_unparent((Inkscape::XML::Node *)sl->data);
907         sl = g_slist_remove(sl, sl->data);
908     }
911 /**
912  * Writes the gradient's internal vector (whether from its own stops, or
913  * inherited from refs) into the gradient repr as svg:stop elements.
914  */
915 void
916 sp_gradient_repr_write_vector(SPGradient *gr)
918     g_return_if_fail(gr != NULL);
919     g_return_if_fail(SP_IS_GRADIENT(gr));
921     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
922     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
924     /* We have to be careful, as vector may be our own, so construct repr list at first */
925     GSList *cl = NULL;
927     for (guint i = 0; i < gr->vector.stops.size(); i++) {
928         Inkscape::CSSOStringStream os;
929         Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
930         sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
931         /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
932          * sense for offset proportions. */
933         gchar c[64];
934         sp_svg_write_color(c, sizeof(c), gr->vector.stops[i].color.toRGBA32( 0x00 ));
935         os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
936         child->setAttribute("style", os.str().c_str());
937         /* Order will be reversed here */
938         cl = g_slist_prepend(cl, child);
939     }
941     sp_gradient_repr_clear_vector(gr);
943     /* And insert new children from list */
944     while (cl) {
945         Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
946         repr->addChild(child, NULL);
947         Inkscape::GC::release(child);
948         cl = g_slist_remove(cl, child);
949     }
953 static void
954 gradient_ref_modified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
956     if (sp_gradient_invalidate_vector(gradient)) {
957         SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
958         /* Conditional to avoid causing infinite loop if there's a cycle in the href chain. */
959     }
962 /** Return true iff change made. */
963 static bool
964 sp_gradient_invalidate_vector(SPGradient *gr)
966     bool ret = false;
968     if (gr->color != NULL) {
969         g_free(gr->color);
970         gr->color = NULL;
971         ret = true;
972     }
974     if (gr->vector.built) {
975         gr->vector.built = false;
976         gr->vector.stops.clear();
977         ret = true;
978     }
980     return ret;
983 /** Creates normalized color vector */
984 static void
985 sp_gradient_rebuild_vector(SPGradient *gr)
987     gint len = 0;
988     for ( SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
989           child != NULL ;
990           child = SP_OBJECT_NEXT(child) ) {
991         if (SP_IS_STOP(child)) {
992             len ++;
993         }
994     }
996     gr->has_stops = (len != 0);
998     gr->vector.stops.clear();
1000     SPGradient *ref = gr->ref->getObject();
1001     if ( !gr->has_stops && ref ) {
1002         /* Copy vector from referenced gradient */
1003         gr->vector.built = true;   // Prevent infinite recursion.
1004         sp_gradient_ensure_vector(ref);
1005         if (!ref->vector.stops.empty()) {
1006             gr->vector.built = ref->vector.built;
1007             gr->vector.stops.assign(ref->vector.stops.begin(), ref->vector.stops.end());
1008             return;
1009         }
1010     }
1012     for (SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
1013          child != NULL;
1014          child = SP_OBJECT_NEXT(child) ) {
1015         if (SP_IS_STOP(child)) {
1016             SPStop *stop = SP_STOP(child);
1018             SPGradientStop gstop;
1019             if (gr->vector.stops.size() > 0) {
1020                 // "Each gradient offset value is required to be equal to or greater than the
1021                 // previous gradient stop's offset value. If a given gradient stop's offset
1022                 // value is not equal to or greater than all previous offset values, then the
1023                 // offset value is adjusted to be equal to the largest of all previous offset
1024                 // values."
1025                 gstop.offset = MAX(stop->offset, gr->vector.stops.back().offset);
1026             } else {
1027                 gstop.offset = stop->offset;
1028             }
1030             // "Gradient offset values less than 0 (or less than 0%) are rounded up to
1031             // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
1032             // down to 100%."
1033             gstop.offset = CLAMP(gstop.offset, 0, 1);
1035             gstop.color = sp_stop_get_color(stop);
1036             gstop.opacity = stop->opacity;
1038             gr->vector.stops.push_back(gstop);
1039         }
1040     }
1042     // Normalize per section 13.2.4 of SVG 1.1.
1043     if (gr->vector.stops.size() == 0) {
1044         /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
1045          * paint style."
1046          */
1047         {
1048             SPGradientStop gstop;
1049             gstop.offset = 0.0;
1050             gstop.color.set( 0x00000000 );
1051             gstop.opacity = 0.0;
1052             gr->vector.stops.push_back(gstop);
1053         }
1054         {
1055             SPGradientStop gstop;
1056             gstop.offset = 1.0;
1057             gstop.color.set( 0x00000000 );
1058             gstop.opacity = 0.0;
1059             gr->vector.stops.push_back(gstop);
1060         }
1061     } else {
1062         /* "If one stop is defined, then paint with the solid color fill using the color defined
1063          * for that gradient stop."
1064          */
1065         if (gr->vector.stops.front().offset > 0.0) {
1066             // If the first one is not at 0, then insert a copy of the first at 0.
1067             SPGradientStop gstop;
1068             gstop.offset = 0.0;
1069             gstop.color = gr->vector.stops.front().color;
1070             gstop.opacity = gr->vector.stops.front().opacity;
1071             gr->vector.stops.insert(gr->vector.stops.begin(), gstop);
1072         }
1073         if (gr->vector.stops.back().offset < 1.0) {
1074             // If the last one is not at 1, then insert a copy of the last at 1.
1075             SPGradientStop gstop;
1076             gstop.offset = 1.0;
1077             gstop.color = gr->vector.stops.back().color;
1078             gstop.opacity = gr->vector.stops.back().opacity;
1079             gr->vector.stops.push_back(gstop);
1080         }
1081     }
1083     gr->vector.built = true;
1086 /**
1087  * The gradient's color array is newly created and set up from vector.
1088  */
1089 void
1090 sp_gradient_ensure_colors(SPGradient *gr)
1092     if (!gr->vector.built) {
1093         sp_gradient_rebuild_vector(gr);
1094     }
1095     g_return_if_fail(!gr->vector.stops.empty());
1097     /// \todo Where is the memory freed?
1098     if (!gr->color) {
1099         gr->color = g_new(guchar, 4 * NCOLORS);
1100     }
1102     // This assumes that gr->vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1103     // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1104     // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1106     double remainder[4] = {0,0,0,0};
1107     double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1108     switch(gr->spread) {
1109     case SP_GRADIENT_SPREAD_PAD:
1110         remainder[0] = 0.5*gr->vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1111         remainder[1] = 0.5*gr->vector.stops[0].color.v.c[1];
1112         remainder[2] = 0.5*gr->vector.stops[0].color.v.c[2];
1113         remainder[3] = 0.5*gr->vector.stops[0].opacity;
1114         remainder_for_end[0] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[0]; // Half of the first cell uses the color of the last stop
1115         remainder_for_end[1] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[1];
1116         remainder_for_end[2] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].color.v.c[2];
1117         remainder_for_end[3] = 0.5*gr->vector.stops[gr->vector.stops.size() - 1].opacity;
1118         break;
1119     case SP_GRADIENT_SPREAD_REFLECT:
1120     case SP_GRADIENT_SPREAD_REPEAT:
1121         // These two are handled differently, see below.
1122         break;
1123     default:
1124         g_error("Spread type not supported!");
1125     };
1126     for (unsigned int i = 0; i < gr->vector.stops.size() - 1; i++) {
1127         double r0 = gr->vector.stops[i].color.v.c[0];
1128         double g0 = gr->vector.stops[i].color.v.c[1];
1129         double b0 = gr->vector.stops[i].color.v.c[2];
1130         double a0 = gr->vector.stops[i].opacity;
1131         double r1 = gr->vector.stops[i+1].color.v.c[0];
1132         double g1 = gr->vector.stops[i+1].color.v.c[1];
1133         double b1 = gr->vector.stops[i+1].color.v.c[2];
1134         double a1 = gr->vector.stops[i+1].opacity;
1135         double o0 = gr->vector.stops[i].offset * (NCOLORS-1);
1136         double o1 = gr->vector.stops[i + 1].offset * (NCOLORS-1);
1137         unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1138         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
1140         if (oe == ob) {
1141             // Simple case, this interval starts and stops within one cell
1142             // The contribution of this interval is:
1143             //    (o1-o0)*(c(o0)+c(o1))/2
1144             //  = (o1-o0)*(c0+c1)/2
1145             double dt = 0.5*(o1-o0);
1146             remainder[0] += dt*(r0 + r1);
1147             remainder[1] += dt*(g0 + g1);
1148             remainder[2] += dt*(b0 + b1);
1149             remainder[3] += dt*(a0 + a1);
1150         } else {
1151             // First compute colors for the cells which are fully covered by the current interval.
1152             // The prefiltered values are equal to the midpoint of each cell here.
1153             //  f = (j-o0)/(o1-o0)
1154             //    = j*(1/(o1-o0)) - o0/(o1-o0)
1155             double f = (ob-o0) / (o1-o0);
1156             double df = 1. / (o1-o0);
1157             for (unsigned int j = ob+1; j < oe; j++) {
1158                 f += df;
1159                 gr->color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1160                 gr->color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1161                 gr->color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1162                 gr->color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1163             }
1165             // Now handle the beginning
1166             // The contribution of the last point is already in remainder.
1167             // The contribution of this point is:
1168             //    (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1169             //  = (ob+.5-o0)*c((o0+ob+.5)/2)
1170             //  = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1171             //  = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1172             double dt = ob+.5-o0;
1173             f = 0.5*dt*df;
1174             if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REFLECT) {
1175                 // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1176                 gr->color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1177                 gr->color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1178                 gr->color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1179                 gr->color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1180             } else if (ob==0 && gr->spread==SP_GRADIENT_SPREAD_REPEAT) {
1181                 // 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.
1182                 remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1183                 remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1184                 remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1185                 remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1186             } else {
1187                 // The first half of the cell was already in remainder.
1188                 gr->color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1189                 gr->color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1190                 gr->color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1191                 gr->color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1192             }
1194             // Now handle the end, which should end up in remainder
1195             // The contribution of this point is:
1196             //    (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1197             //  = (o1-oe+.5)*c((o1+oe-.5)/2)
1198             //  = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1199             dt = o1-oe+.5;
1200             f = (0.5*(o1+oe-.5)-o0)*df;
1201             remainder[0] = dt*(r0 + f*(r1-r0));
1202             remainder[1] = dt*(g0 + f*(g1-g0));
1203             remainder[2] = dt*(b0 + f*(b1-b0));
1204             remainder[3] = dt*(a0 + f*(a1-a0));
1205         }
1206     }
1207     switch(gr->spread) {
1208     case SP_GRADIENT_SPREAD_PAD:
1209         gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1210         gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1211         gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1212         gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1213         break;
1214     case SP_GRADIENT_SPREAD_REFLECT:
1215         // The second half is the same as the first half, so multiply by 2.
1216         gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1217         gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1218         gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1219         gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1220         break;
1221     case SP_GRADIENT_SPREAD_REPEAT:
1222         // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1223         gr->color[0] = gr->color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1224         gr->color[1] = gr->color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1225         gr->color[2] = gr->color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1226         gr->color[3] = gr->color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1227         break;
1228     }
1231 /**
1232  * Renders gradient vector to buffer as line.
1233  *
1234  * RGB buffer background should be set up beforehand.
1235  *
1236  * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1237  * @param span Full integer width of requested gradient.
1238  * @param pos Buffer starting position in span.
1239  */
1240 static void
1241 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1242                                     gint const len, gint const pos, gint const span)
1244     g_return_if_fail(gradient != NULL);
1245     g_return_if_fail(SP_IS_GRADIENT(gradient));
1246     g_return_if_fail(buf != NULL);
1247     g_return_if_fail(len > 0);
1248     g_return_if_fail(pos >= 0);
1249     g_return_if_fail(pos + len <= span);
1250     g_return_if_fail(span > 0);
1252     if (!gradient->color) {
1253         sp_gradient_ensure_colors(gradient);
1254     }
1256     gint idx = (pos * 1024 << 8) / span;
1257     gint didx = (1024 << 8) / span;
1259     for (gint x = 0; x < len; x++) {
1260         /// \todo Can this be done with 4 byte copies?
1261         *buf++ = gradient->color[4 * (idx >> 8)];
1262         *buf++ = gradient->color[4 * (idx >> 8) + 1];
1263         *buf++ = gradient->color[4 * (idx >> 8) + 2];
1264         *buf++ = gradient->color[4 * (idx >> 8) + 3];
1265         idx += didx;
1266     }
1269 /**
1270  * Render rectangular RGBA area from gradient vector.
1271  */
1272 void
1273 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1274                                      gint const width, gint const height, gint const rowstride,
1275                                      gint const pos, gint const span, bool const horizontal)
1277     g_return_if_fail(gradient != NULL);
1278     g_return_if_fail(SP_IS_GRADIENT(gradient));
1279     g_return_if_fail(buf != NULL);
1280     g_return_if_fail(width > 0);
1281     g_return_if_fail(height > 0);
1282     g_return_if_fail(pos >= 0);
1283     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1284     g_return_if_fail(span > 0);
1286     if (horizontal) {
1287         sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1288         for (gint y = 1; y < height; y++) {
1289             memcpy(buf + y * rowstride, buf, 4 * width);
1290         }
1291     } else {
1292         guchar *tmp = (guchar *)alloca(4 * height);
1293         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1294         for (gint y = 0; y < height; y++) {
1295             guchar *b = buf + y * rowstride;
1296             for (gint x = 0; x < width; x++) {
1297                 *b++ = tmp[0];
1298                 *b++ = tmp[1];
1299                 *b++ = tmp[2];
1300                 *b++ = tmp[3];
1301             }
1302             tmp += 4;
1303         }
1304     }
1307 /**
1308  * Render rectangular RGB area from gradient vector.
1309  */
1310 void
1311 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1312                                     gint const width, gint const height, gint const /*rowstride*/,
1313                                     gint const pos, gint const span, bool const horizontal)
1315     g_return_if_fail(gradient != NULL);
1316     g_return_if_fail(SP_IS_GRADIENT(gradient));
1317     g_return_if_fail(buf != NULL);
1318     g_return_if_fail(width > 0);
1319     g_return_if_fail(height > 0);
1320     g_return_if_fail(pos >= 0);
1321     g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1322     g_return_if_fail(span > 0);
1324     if (horizontal) {
1325         guchar *tmp = (guchar*)alloca(4 * width);
1326         sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1327         for (gint y = 0; y < height; y++) {
1328             guchar *t = tmp;
1329             for (gint x = 0; x < width; x++) {
1330                 gint a = t[3];
1331                 gint fc = (t[0] - buf[0]) * a;
1332                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1333                 fc = (t[1] - buf[1]) * a;
1334                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1335                 fc = (t[2] - buf[2]) * a;
1336                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1337                 buf += 3;
1338                 t += 4;
1339             }
1340         }
1341     } else {
1342         guchar *tmp = (guchar*)alloca(4 * height);
1343         sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1344         for (gint y = 0; y < height; y++) {
1345             guchar *t = tmp + 4 * y;
1346             for (gint x = 0; x < width; x++) {
1347                 gint a = t[3];
1348                 gint fc = (t[0] - buf[0]) * a;
1349                 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1350                 fc = (t[1] - buf[1]) * a;
1351                 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1352                 fc = (t[2] - buf[2]) * a;
1353                 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1354             }
1355         }
1356     }
1359 Geom::Matrix
1360 sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1362     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1363         return ( Geom::Scale(bbox.dimensions())
1364                  * Geom::Translate(bbox.min())
1365                  * Geom::Matrix(ctm) );
1366     } else {
1367         return ctm;
1368     }
1371 Geom::Matrix
1372 sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1374     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1375         return ( gr->gradientTransform
1376                  * Geom::Scale(bbox.dimensions())
1377                  * Geom::Translate(bbox.min())
1378                  * Geom::Matrix(ctm) );
1379     } else {
1380         return gr->gradientTransform * ctm;
1381     }
1384 void
1385 sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1386                             Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1388     gr->gradientTransform = gs2d * ctm.inverse();
1389     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1390         gr->gradientTransform = ( gr->gradientTransform
1391                                   * Geom::Translate(-bbox.min())
1392                                   * Geom::Scale(bbox.dimensions()).inverse() );
1393     }
1394     gr->gradientTransform_set = TRUE;
1396     SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1399 /*
1400  * Linear Gradient
1401  */
1403 class SPLGPainter;
1405 /// A context with linear gradient, painter, and gradient renderer.
1406 struct SPLGPainter {
1407     SPPainter painter;
1408     SPLinearGradient *lg;
1410     NRLGradientRenderer lgr;
1411 };
1413 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1414 static void sp_lineargradient_init(SPLinearGradient *lg);
1416 static void sp_lineargradient_build(SPObject *object,
1417                                     SPDocument *document,
1418                                     Inkscape::XML::Node *repr);
1419 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1420 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1421                                                     guint flags);
1423 static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
1424                                                 Geom::Matrix const &full_transform,
1425                                                 Geom::Matrix const &parent_transform,
1426                                                 NRRect const *bbox);
1427 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1429 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1431 static SPGradientClass *lg_parent_class;
1433 /**
1434  * Register SPLinearGradient class and return its type.
1435  */
1436 GType
1437 sp_lineargradient_get_type()
1439     static GType type = 0;
1440     if (!type) {
1441         GTypeInfo info = {
1442             sizeof(SPLinearGradientClass),
1443             NULL, NULL,
1444             (GClassInitFunc) sp_lineargradient_class_init,
1445             NULL, NULL,
1446             sizeof(SPLinearGradient),
1447             16,
1448             (GInstanceInitFunc) sp_lineargradient_init,
1449             NULL,   /* value_table */
1450         };
1451         type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1452     }
1453     return type;
1456 /**
1457  * SPLinearGradient vtable initialization.
1458  */
1459 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1461     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1462     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1464     lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1466     sp_object_class->build = sp_lineargradient_build;
1467     sp_object_class->set = sp_lineargradient_set;
1468     sp_object_class->write = sp_lineargradient_write;
1470     ps_class->painter_new = sp_lineargradient_painter_new;
1471     ps_class->painter_free = sp_lineargradient_painter_free;
1474 /**
1475  * Callback for SPLinearGradient object initialization.
1476  */
1477 static void sp_lineargradient_init(SPLinearGradient *lg)
1479     lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1480     lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
1481     lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1482     lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
1485 /**
1486  * Callback: set attributes from associated repr.
1487  */
1488 static void sp_lineargradient_build(SPObject *object,
1489                                     SPDocument *document,
1490                                     Inkscape::XML::Node *repr)
1492     if (((SPObjectClass *) lg_parent_class)->build)
1493         (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1495     sp_object_read_attr(object, "x1");
1496     sp_object_read_attr(object, "y1");
1497     sp_object_read_attr(object, "x2");
1498     sp_object_read_attr(object, "y2");
1501 /**
1502  * Callback: set attribute.
1503  */
1504 static void
1505 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1507     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1509     switch (key) {
1510         case SP_ATTR_X1:
1511             lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1512             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1513             break;
1514         case SP_ATTR_Y1:
1515             lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1516             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1517             break;
1518         case SP_ATTR_X2:
1519             lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1520             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1521             break;
1522         case SP_ATTR_Y2:
1523             lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1524             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1525             break;
1526         default:
1527             if (((SPObjectClass *) lg_parent_class)->set)
1528                 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1529             break;
1530     }
1533 /**
1534  * Callback: write attributes to associated repr.
1535  */
1536 static Inkscape::XML::Node *
1537 sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1539     SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1541     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1542         repr = xml_doc->createElement("svg:linearGradient");
1543     }
1545     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1546         sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1547     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1548         sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1549     if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1550         sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1551     if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1552         sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1554     if (((SPObjectClass *) lg_parent_class)->write)
1555         (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
1557     return repr;
1560 /**
1561  * Create linear gradient context.
1562  *
1563  * Basically we have to deal with transformations
1564  *
1565  * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1566  * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1567  * 2) gradientTransform
1568  * 3) bbox2user
1569  * 4) ctm == userspace to pixel grid
1570  *
1571  * See also (*) in sp-pattern about why we may need parent_transform.
1572  *
1573  * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1574  * and end < 1.
1575  */
1576 static SPPainter *
1577 sp_lineargradient_painter_new(SPPaintServer *ps,
1578                               Geom::Matrix const &full_transform,
1579                               Geom::Matrix const &/*parent_transform*/,
1580                               NRRect const *bbox)
1582     SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1583     SPGradient *gr = SP_GRADIENT(ps);
1585     if (!gr->color) sp_gradient_ensure_colors(gr);
1587     SPLGPainter *lgp = g_new(SPLGPainter, 1);
1589     lgp->painter.type = SP_PAINTER_IND;
1590     lgp->painter.fill = sp_lg_fill;
1592     lgp->lg = lg;
1594     /** \todo
1595      * Technically speaking, we map NCOLORS on line [start,end] onto line
1596      * [0,1].  I almost think we should fill color array start and end in
1597      * that case. The alternative would be to leave these just empty garbage
1598      * or something similar. Originally I had 1023.9999 here - not sure
1599      * whether we have really to cut out ceil int (Lauris).
1600      */
1601     Geom::Matrix color2norm(Geom::identity());
1602     Geom::Matrix color2px;
1603     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1604         Geom::Matrix norm2pos(Geom::identity());
1606         /* BBox to user coordinate system */
1607         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1609         Geom::Matrix color2pos = color2norm * norm2pos;
1610         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1611         Geom::Matrix color2user = color2tpos * bbox2user;
1612         color2px = color2user * full_transform;
1614     } else {
1615         /* Problem: What to do, if we have mixed lengths and percentages? */
1616         /* Currently we do ignore percentages at all, but that is not good (lauris) */
1618         Geom::Matrix norm2pos(Geom::identity());
1619         Geom::Matrix color2pos = color2norm * norm2pos;
1620         Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1621         color2px = color2tpos * full_transform;
1623     }
1624     // TODO: remove color2px_nr after converting to 2geom
1625     NR::Matrix color2px_nr = from_2geom(color2px);
1626     nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &color2px_nr,
1627                                 lg->x1.computed, lg->y1.computed,
1628                                 lg->x2.computed, lg->y2.computed);
1630     return (SPPainter *) lgp;
1633 static void
1634 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1636     g_free(painter);
1639 /**
1640  * Directly set properties of linear gradient and request modified.
1641  */
1642 void
1643 sp_lineargradient_set_position(SPLinearGradient *lg,
1644                                gdouble x1, gdouble y1,
1645                                gdouble x2, gdouble y2)
1647     g_return_if_fail(lg != NULL);
1648     g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1650     /* fixme: units? (Lauris)  */
1651     lg->x1.set(SVGLength::NONE, x1, x1);
1652     lg->y1.set(SVGLength::NONE, y1, y1);
1653     lg->x2.set(SVGLength::NONE, x2, x2);
1654     lg->y2.set(SVGLength::NONE, y2, y2);
1656     SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1659 /**
1660  * Callback when linear gradient object is rendered.
1661  */
1662 static void
1663 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1665     SPLGPainter *lgp = (SPLGPainter *) painter;
1667     if (lgp->lg->color == NULL) {
1668         sp_gradient_ensure_colors (lgp->lg);
1669         lgp->lgr.vector = lgp->lg->color;
1670     }
1672     nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1675 /*
1676  * Radial Gradient
1677  */
1679 class SPRGPainter;
1681 /// A context with radial gradient, painter, and gradient renderer.
1682 struct SPRGPainter {
1683     SPPainter painter;
1684     SPRadialGradient *rg;
1685     NRRGradientRenderer rgr;
1686 };
1688 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1689 static void sp_radialgradient_init(SPRadialGradient *rg);
1691 static void sp_radialgradient_build(SPObject *object,
1692                                     SPDocument *document,
1693                                     Inkscape::XML::Node *repr);
1694 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1695 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1696                                                     guint flags);
1698 static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
1699                                                 Geom::Matrix const &full_transform,
1700                                                 Geom::Matrix const &parent_transform,
1701                                                 NRRect const *bbox);
1702 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1704 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1706 static SPGradientClass *rg_parent_class;
1708 /**
1709  * Register SPRadialGradient class and return its type.
1710  */
1711 GType
1712 sp_radialgradient_get_type()
1714     static GType type = 0;
1715     if (!type) {
1716         GTypeInfo info = {
1717             sizeof(SPRadialGradientClass),
1718             NULL, NULL,
1719             (GClassInitFunc) sp_radialgradient_class_init,
1720             NULL, NULL,
1721             sizeof(SPRadialGradient),
1722             16,
1723             (GInstanceInitFunc) sp_radialgradient_init,
1724             NULL,   /* value_table */
1725         };
1726         type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1727     }
1728     return type;
1731 /**
1732  * SPRadialGradient vtable initialization.
1733  */
1734 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1736     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1737     SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1739     rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1741     sp_object_class->build = sp_radialgradient_build;
1742     sp_object_class->set = sp_radialgradient_set;
1743     sp_object_class->write = sp_radialgradient_write;
1745     ps_class->painter_new = sp_radialgradient_painter_new;
1746     ps_class->painter_free = sp_radialgradient_painter_free;
1749 /**
1750  * Callback for SPRadialGradient object initialization.
1751  */
1752 static void
1753 sp_radialgradient_init(SPRadialGradient *rg)
1755     rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1756     rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1757     rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1758     rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1759     rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1762 /**
1763  * Set radial gradient attributes from associated repr.
1764  */
1765 static void
1766 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1768     if (((SPObjectClass *) rg_parent_class)->build)
1769         (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1771     sp_object_read_attr(object, "cx");
1772     sp_object_read_attr(object, "cy");
1773     sp_object_read_attr(object, "r");
1774     sp_object_read_attr(object, "fx");
1775     sp_object_read_attr(object, "fy");
1778 /**
1779  * Set radial gradient attribute.
1780  */
1781 static void
1782 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1784     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1786     switch (key) {
1787         case SP_ATTR_CX:
1788             if (!rg->cx.read(value)) {
1789                 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1790             }
1791             if (!rg->fx._set) {
1792                 rg->fx.value = rg->cx.value;
1793                 rg->fx.computed = rg->cx.computed;
1794             }
1795             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1796             break;
1797         case SP_ATTR_CY:
1798             if (!rg->cy.read(value)) {
1799                 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1800             }
1801             if (!rg->fy._set) {
1802                 rg->fy.value = rg->cy.value;
1803                 rg->fy.computed = rg->cy.computed;
1804             }
1805             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1806             break;
1807         case SP_ATTR_R:
1808             if (!rg->r.read(value)) {
1809                 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1810             }
1811             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1812             break;
1813         case SP_ATTR_FX:
1814             if (!rg->fx.read(value)) {
1815                 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1816             }
1817             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1818             break;
1819         case SP_ATTR_FY:
1820             if (!rg->fy.read(value)) {
1821                 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1822             }
1823             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1824             break;
1825         default:
1826             if (((SPObjectClass *) rg_parent_class)->set)
1827                 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1828             break;
1829     }
1832 /**
1833  * Write radial gradient attributes to associated repr.
1834  */
1835 static Inkscape::XML::Node *
1836 sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1838     SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1840     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1841         repr = xml_doc->createElement("svg:radialGradient");
1842     }
1844     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1845     if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1846     if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1847     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1848     if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1850     if (((SPObjectClass *) rg_parent_class)->write)
1851         (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1853     return repr;
1856 /**
1857  * Create radial gradient context.
1858  */
1859 static SPPainter *
1860 sp_radialgradient_painter_new(SPPaintServer *ps,
1861                               Geom::Matrix const &full_transform,
1862                               Geom::Matrix const &/*parent_transform*/,
1863                               NRRect const *bbox)
1865     SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1866     SPGradient *gr = SP_GRADIENT(ps);
1868     if (!gr->color) sp_gradient_ensure_colors(gr);
1870     SPRGPainter *rgp = g_new(SPRGPainter, 1);
1872     rgp->painter.type = SP_PAINTER_IND;
1873     rgp->painter.fill = sp_rg_fill;
1875     rgp->rg = rg;
1877     Geom::Matrix gs2px;
1879     if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1880         /** \todo
1881          * fixme: We may try to normalize here too, look at
1882          * linearGradient (Lauris)
1883          */
1885         /* BBox to user coordinate system */
1886         Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1888         Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1890         gs2px = gs2user * full_transform;
1891     } else {
1892         /** \todo
1893          * Problem: What to do, if we have mixed lengths and percentages?
1894          * Currently we do ignore percentages at all, but that is not
1895          * good (lauris)
1896          */
1898         gs2px = gr->gradientTransform * full_transform;
1899     }
1900     // TODO: remove gs2px_nr after converting to 2geom
1901     NR::Matrix gs2px_nr = from_2geom(gs2px);
1902     nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
1903                                 &gs2px_nr,
1904                                 rg->cx.computed, rg->cy.computed,
1905                                 rg->fx.computed, rg->fy.computed,
1906                                 rg->r.computed);
1908     return (SPPainter *) rgp;
1911 static void
1912 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1914     g_free(painter);
1917 /**
1918  * Directly set properties of radial gradient and request modified.
1919  */
1920 void
1921 sp_radialgradient_set_position(SPRadialGradient *rg,
1922                                gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1924     g_return_if_fail(rg != NULL);
1925     g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1927     /* fixme: units? (Lauris)  */
1928     rg->cx.set(SVGLength::NONE, cx, cx);
1929     rg->cy.set(SVGLength::NONE, cy, cy);
1930     rg->fx.set(SVGLength::NONE, fx, fx);
1931     rg->fy.set(SVGLength::NONE, fy, fy);
1932     rg->r.set(SVGLength::NONE, r, r);
1934     SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1937 /**
1938  * Callback when radial gradient object is rendered.
1939  */
1940 static void
1941 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1943     SPRGPainter *rgp = (SPRGPainter *) painter;
1945     if (rgp->rg->color == NULL) {
1946         sp_gradient_ensure_colors (rgp->rg);
1947         rgp->rgr.vector = rgp->rg->color;
1948     }
1950     nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1953 /*
1954   Local Variables:
1955   mode:c++
1956   c-file-style:"stroustrup"
1957   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1958   indent-tabs-mode:nil
1959   fill-column:99
1960   End:
1961 */
1962 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :