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;
108 }
110 /**
111 * Callback to initialize SPStop vtable.
112 */
113 static void sp_stop_class_init(SPStopClass *klass)
114 {
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;
122 }
124 /**
125 * Callback to initialize SPStop object.
126 */
127 static void
128 sp_stop_init(SPStop *stop)
129 {
130 stop->offset = 0.0;
131 stop->currentColor = false;
132 stop->specified_color.set( 0x000000ff );
133 stop->opacity = 1.0;
134 }
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)
140 {
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");
148 }
150 /**
151 * Virtual set: set attribute to value.
152 */
153 static void
154 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
155 {
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 }
221 }
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)
228 {
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;
263 }
266 bool SPGradient::hasStops() const
267 {
268 return has_stops;
269 }
271 bool SPGradient::isUnitsSet() const
272 {
273 return units_set;
274 }
276 SPGradientUnits SPGradient::getUnits() const
277 {
278 return units;
279 }
281 bool SPGradient::isSpreadSet() const
282 {
283 return spread_set;
284 }
286 SPGradientSpread SPGradient::getSpread() const
287 {
288 return spread;
289 }
291 /**
292 * Return stop's color as 32bit value.
293 */
294 guint32
295 sp_stop_get_rgba32(SPStop const *const stop)
296 {
297 guint32 rgb0 = 0;
298 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
299 * value depends on user agent, and don't give any further restrictions that I can
300 * see.) */
301 if (stop->currentColor) {
302 char const *str = sp_object_get_style_property(stop, "color", NULL);
303 if (str) {
304 rgb0 = sp_svg_read_color(str, rgb0);
305 }
306 unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
307 g_return_val_if_fail((alpha & ~0xff) == 0,
308 rgb0 | 0xff);
309 return rgb0 | alpha;
310 } else {
311 return stop->specified_color.toRGBA32( stop->opacity );
312 }
313 }
315 /**
316 * Return stop's color as SPColor.
317 */
318 static SPColor
319 sp_stop_get_color(SPStop const *const stop)
320 {
321 if (stop->currentColor) {
322 char const *str = sp_object_get_style_property(stop, "color", NULL);
323 guint32 const dfl = 0;
324 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
325 * value depends on user agent, and don't give any further restrictions that I can
326 * see.) */
327 guint32 color = dfl;
328 if (str) {
329 color = sp_svg_read_color(str, dfl);
330 }
331 SPColor ret( color );
332 return ret;
333 } else {
334 return stop->specified_color;
335 }
336 }
338 /*
339 * Gradient
340 */
342 static SPPaintServerClass *gradient_parent_class;
344 /**
345 * Registers SPGradient class and returns its type.
346 */
347 GType SPGradient::getType()
348 {
349 static GType gradient_type = 0;
350 if (!gradient_type) {
351 GTypeInfo gradient_info = {
352 sizeof(SPGradientClass),
353 NULL, NULL,
354 (GClassInitFunc) SPGradientImpl::classInit,
355 NULL, NULL,
356 sizeof(SPGradient),
357 16,
358 (GInstanceInitFunc) SPGradientImpl::init,
359 NULL, /* value_table */
360 };
361 gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
362 &gradient_info, (GTypeFlags)0);
363 }
364 return gradient_type;
365 }
367 /**
368 * SPGradient vtable initialization.
369 */
370 void SPGradientImpl::classInit(SPGradientClass *klass)
371 {
372 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
374 gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
376 sp_object_class->build = SPGradientImpl::build;
377 sp_object_class->release = SPGradientImpl::release;
378 sp_object_class->set = SPGradientImpl::setGradientAttr;
379 sp_object_class->child_added = SPGradientImpl::childAdded;
380 sp_object_class->remove_child = SPGradientImpl::removeChild;
381 sp_object_class->modified = SPGradientImpl::modified;
382 sp_object_class->write = SPGradientImpl::write;
383 }
385 /**
386 * Callback for SPGradient object initialization.
387 */
388 void SPGradientImpl::init(SPGradient *gr)
389 {
390 gr->ref = new SPGradientReference(SP_OBJECT(gr));
391 gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(SPGradientImpl::gradientRefChanged), gr));
393 /** \todo
394 * Fixme: reprs being rearranged (e.g. via the XML editor)
395 * may require us to clear the state.
396 */
397 gr->state = SP_GRADIENT_STATE_UNKNOWN;
399 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
400 gr->units_set = FALSE;
402 gr->gradientTransform = Geom::identity();
403 gr->gradientTransform_set = FALSE;
405 gr->spread = SP_GRADIENT_SPREAD_PAD;
406 gr->spread_set = FALSE;
408 gr->has_stops = FALSE;
410 gr->vector.built = false;
411 gr->vector.stops.clear();
413 gr->color = NULL;
415 new (&gr->modified_connection) sigc::connection();
416 }
418 /**
419 * Virtual build: set gradient attributes from its associated repr.
420 */
421 void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
422 {
423 SPGradient *gradient = SP_GRADIENT(object);
425 if (((SPObjectClass *) gradient_parent_class)->build)
426 (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
428 SPObject *ochild;
429 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
430 if (SP_IS_STOP(ochild)) {
431 gradient->has_stops = TRUE;
432 break;
433 }
434 }
436 sp_object_read_attr(object, "gradientUnits");
437 sp_object_read_attr(object, "gradientTransform");
438 sp_object_read_attr(object, "spreadMethod");
439 sp_object_read_attr(object, "xlink:href");
441 /* Register ourselves */
442 sp_document_add_resource(document, "gradient", object);
443 }
445 /**
446 * Virtual release of SPGradient members before destruction.
447 */
448 void SPGradientImpl::release(SPObject *object)
449 {
450 SPGradient *gradient = (SPGradient *) object;
452 #ifdef SP_GRADIENT_VERBOSE
453 g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
454 #endif
456 if (SP_OBJECT_DOCUMENT(object)) {
457 /* Unregister ourselves */
458 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
459 }
461 if (gradient->ref) {
462 gradient->modified_connection.disconnect();
463 gradient->ref->detach();
464 delete gradient->ref;
465 gradient->ref = NULL;
466 }
468 if (gradient->color) {
469 g_free(gradient->color);
470 gradient->color = NULL;
471 }
473 gradient->modified_connection.~connection();
475 if (((SPObjectClass *) gradient_parent_class)->release)
476 ((SPObjectClass *) gradient_parent_class)->release(object);
477 }
479 /**
480 * Set gradient attribute to value.
481 */
482 void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const *value)
483 {
484 SPGradient *gr = SP_GRADIENT(object);
486 switch (key) {
487 case SP_ATTR_GRADIENTUNITS:
488 if (value) {
489 if (!strcmp(value, "userSpaceOnUse")) {
490 gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
491 } else {
492 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
493 }
494 gr->units_set = TRUE;
495 } else {
496 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
497 gr->units_set = FALSE;
498 }
499 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
500 break;
501 case SP_ATTR_GRADIENTTRANSFORM: {
502 Geom::Matrix t;
503 if (value && sp_svg_transform_read(value, &t)) {
504 gr->gradientTransform = t;
505 gr->gradientTransform_set = TRUE;
506 } else {
507 gr->gradientTransform = Geom::identity();
508 gr->gradientTransform_set = FALSE;
509 }
510 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
511 break;
512 }
513 case SP_ATTR_SPREADMETHOD:
514 if (value) {
515 if (!strcmp(value, "reflect")) {
516 gr->spread = SP_GRADIENT_SPREAD_REFLECT;
517 } else if (!strcmp(value, "repeat")) {
518 gr->spread = SP_GRADIENT_SPREAD_REPEAT;
519 } else {
520 gr->spread = SP_GRADIENT_SPREAD_PAD;
521 }
522 gr->spread_set = TRUE;
523 } else {
524 gr->spread_set = FALSE;
525 }
526 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
527 break;
528 case SP_ATTR_XLINK_HREF:
529 if (value) {
530 try {
531 gr->ref->attach(Inkscape::URI(value));
532 } catch (Inkscape::BadURIException &e) {
533 g_warning("%s", e.what());
534 gr->ref->detach();
535 }
536 } else {
537 gr->ref->detach();
538 }
539 break;
540 default:
541 if (((SPObjectClass *) gradient_parent_class)->set)
542 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
543 break;
544 }
545 }
547 /**
548 * Gets called when the gradient is (re)attached to another gradient.
549 */
550 void SPGradientImpl::gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr)
551 {
552 if (old_ref) {
553 gr->modified_connection.disconnect();
554 }
555 if ( SP_IS_GRADIENT(ref)
556 && ref != gr )
557 {
558 gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&SPGradientImpl::gradientRefModified), gr));
559 }
561 // Per SVG, all unset attributes must be inherited from linked gradient.
562 // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
563 // but without setting the _set flags.
564 // FIXME: do the same for gradientTransform too
565 if (!gr->units_set) {
566 gr->units = gr->fetchUnits();
567 }
568 if (!gr->spread_set) {
569 gr->spread = gr->fetchSpread();
570 }
572 /// \todo Fixme: what should the flags (second) argument be? */
573 gradientRefModified(ref, 0, gr);
574 }
576 /**
577 * Callback for child_added event.
578 */
579 void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
580 {
581 SPGradient *gr = SP_GRADIENT(object);
583 gr->invalidateVector();
585 if (((SPObjectClass *) gradient_parent_class)->child_added)
586 (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
588 SPObject *ochild = sp_object_get_child_by_repr(object, child);
589 if ( ochild && SP_IS_STOP(ochild) ) {
590 gr->has_stops = TRUE;
591 }
593 /// \todo Fixme: should we schedule "modified" here?
594 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
595 }
597 /**
598 * Callback for remove_child event.
599 */
600 void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
601 {
602 SPGradient *gr = SP_GRADIENT(object);
604 gr->invalidateVector();
606 if (((SPObjectClass *) gradient_parent_class)->remove_child) {
607 (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
608 }
610 gr->has_stops = FALSE;
611 SPObject *ochild;
612 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
613 if (SP_IS_STOP(ochild)) {
614 gr->has_stops = TRUE;
615 break;
616 }
617 }
619 /* Fixme: should we schedule "modified" here? */
620 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
621 }
623 /**
624 * Callback for modified event.
625 */
626 void SPGradientImpl::modified(SPObject *object, guint flags)
627 {
628 SPGradient *gr = SP_GRADIENT(object);
630 if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
631 gr->invalidateVector();
632 }
634 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
635 gr->ensureColors();
636 }
638 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
639 flags &= SP_OBJECT_MODIFIED_CASCADE;
641 // FIXME: climb up the ladder of hrefs
642 GSList *l = NULL;
643 for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
644 g_object_ref(G_OBJECT(child));
645 l = g_slist_prepend(l, child);
646 }
647 l = g_slist_reverse(l);
648 while (l) {
649 SPObject *child = SP_OBJECT(l->data);
650 l = g_slist_remove(l, child);
651 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
652 child->emitModified(flags);
653 }
654 g_object_unref(G_OBJECT(child));
655 }
656 }
658 SPStop* SPGradient::getFirstStop()
659 {
660 SPStop* first = 0;
661 for (SPObject *ochild = sp_object_first_child(this); ochild && !first; ochild = SP_OBJECT_NEXT(ochild)) {
662 if (SP_IS_STOP(ochild)) {
663 first = SP_STOP(ochild);
664 }
665 }
666 return first;
667 }
669 int SPGradient::getStopCount() const
670 {
671 int count = 0;
673 for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
674 count++;
675 }
677 return count;
678 }
680 /**
681 * Write gradient attributes to repr.
682 */
683 Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
684 {
685 SPGradient *gr = SP_GRADIENT(object);
687 if (((SPObjectClass *) gradient_parent_class)->write)
688 (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
690 if (flags & SP_OBJECT_WRITE_BUILD) {
691 GSList *l = NULL;
692 for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
693 Inkscape::XML::Node *crepr;
694 crepr = child->updateRepr(xml_doc, NULL, flags);
695 if (crepr) l = g_slist_prepend(l, crepr);
696 }
697 while (l) {
698 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
699 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
700 l = g_slist_remove(l, l->data);
701 }
702 }
704 if (gr->ref->getURI()) {
705 gchar *uri_string = gr->ref->getURI()->toString();
706 repr->setAttribute("xlink:href", uri_string);
707 g_free(uri_string);
708 }
710 if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
711 switch (gr->units) {
712 case SP_GRADIENT_UNITS_USERSPACEONUSE:
713 repr->setAttribute("gradientUnits", "userSpaceOnUse");
714 break;
715 default:
716 repr->setAttribute("gradientUnits", "objectBoundingBox");
717 break;
718 }
719 }
721 if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
722 gchar *c=sp_svg_transform_write(gr->gradientTransform);
723 repr->setAttribute("gradientTransform", c);
724 g_free(c);
725 }
727 if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
728 /* FIXME: Ensure that gr->spread is the inherited value
729 * if !gr->spread_set. Not currently happening: see SPGradient::modified.
730 */
731 switch (gr->spread) {
732 case SP_GRADIENT_SPREAD_REFLECT:
733 repr->setAttribute("spreadMethod", "reflect");
734 break;
735 case SP_GRADIENT_SPREAD_REPEAT:
736 repr->setAttribute("spreadMethod", "repeat");
737 break;
738 default:
739 repr->setAttribute("spreadMethod", "pad");
740 break;
741 }
742 }
744 return repr;
745 }
747 /**
748 * Forces the vector to be built, if not present (i.e., changed).
749 *
750 * \pre SP_IS_GRADIENT(gradient).
751 */
752 void SPGradient::ensureVector()
753 {
754 if ( !vector.built ) {
755 rebuildVector();
756 }
757 }
759 /**
760 * Set units property of gradient and emit modified.
761 */
762 void SPGradient::setUnits(SPGradientUnits units)
763 {
764 if (units != this->units) {
765 this->units = units;
766 units_set = TRUE;
767 requestModified(SP_OBJECT_MODIFIED_FLAG);
768 }
769 }
771 /**
772 * Set spread property of gradient and emit modified.
773 */
774 void SPGradient::setSpread(SPGradientSpread spread)
775 {
776 if (spread != this->spread) {
777 this->spread = spread;
778 spread_set = TRUE;
779 requestModified(SP_OBJECT_MODIFIED_FLAG);
780 }
781 }
783 /**
784 * Returns the first of {src, src-\>ref-\>getObject(),
785 * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
786 * for which \a match is true, or NULL if none found.
787 *
788 * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
789 * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
790 *
791 * \pre SP_IS_GRADIENT(src).
792 */
793 static SPGradient *
794 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
795 {
796 g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
798 /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2. If there is a
799 loop, then once p1 has entered the loop, we'll detect it the next time the distance between
800 p1 and p2 is a multiple of the loop size. */
801 SPGradient *p1 = src, *p2 = src;
802 bool do1 = false;
803 for (;;) {
804 if (match(p2)) {
805 return p2;
806 }
808 p2 = p2->ref->getObject();
809 if (!p2) {
810 return p2;
811 }
812 if (do1) {
813 p1 = p1->ref->getObject();
814 }
815 do1 = !do1;
817 if ( p2 == p1 ) {
818 /* We've been here before, so return NULL to indicate that no matching gradient found
819 * in the chain. */
820 return NULL;
821 }
822 }
823 }
825 /**
826 * True if gradient has stops.
827 */
828 static bool has_stopsFN(SPGradient const *gr)
829 {
830 return gr->hasStops();
831 }
833 /**
834 * True if gradient has spread set.
835 */
836 static bool has_spread_set(SPGradient const *gr)
837 {
838 return gr->isSpreadSet();
839 }
841 /**
842 * True if gradient has units set.
843 */
844 static bool
845 has_units_set(SPGradient const *gr)
846 {
847 return gr->isUnitsSet();
848 }
851 SPGradient *SPGradient::getVector(bool force_vector)
852 {
853 SPGradient * src = chase_hrefs(this, has_stopsFN);
855 if (force_vector) {
856 src = sp_gradient_ensure_vector_normalized(src);
857 }
858 return src;
859 }
861 /**
862 * Returns the effective spread of given gradient (climbing up the refs chain if needed).
863 *
864 * \pre SP_IS_GRADIENT(gradient).
865 */
866 SPGradientSpread SPGradient::fetchSpread()
867 {
868 SPGradient const *src = chase_hrefs(this, has_spread_set);
869 return ( src
870 ? src->spread
871 : SP_GRADIENT_SPREAD_PAD ); // pad is the default
872 }
874 /**
875 * Returns the effective units of given gradient (climbing up the refs chain if needed).
876 *
877 * \pre SP_IS_GRADIENT(gradient).
878 */
879 SPGradientUnits SPGradient::fetchUnits()
880 {
881 SPGradient const *src = chase_hrefs(this, has_units_set);
882 return ( src
883 ? src->units
884 : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
885 }
888 /**
889 * Clears the gradient's svg:stop children from its repr.
890 */
891 void
892 sp_gradient_repr_clear_vector(SPGradient *gr)
893 {
894 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
896 /* Collect stops from original repr */
897 GSList *sl = NULL;
898 for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
899 if (!strcmp(child->name(), "svg:stop")) {
900 sl = g_slist_prepend(sl, child);
901 }
902 }
903 /* Remove all stops */
904 while (sl) {
905 /** \todo
906 * fixme: This should work, unless we make gradient
907 * into generic group.
908 */
909 sp_repr_unparent((Inkscape::XML::Node *)sl->data);
910 sl = g_slist_remove(sl, sl->data);
911 }
912 }
914 /**
915 * Writes the gradient's internal vector (whether from its own stops, or
916 * inherited from refs) into the gradient repr as svg:stop elements.
917 */
918 void
919 sp_gradient_repr_write_vector(SPGradient *gr)
920 {
921 g_return_if_fail(gr != NULL);
922 g_return_if_fail(SP_IS_GRADIENT(gr));
924 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
925 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
927 /* We have to be careful, as vector may be our own, so construct repr list at first */
928 GSList *cl = NULL;
930 for (guint i = 0; i < gr->vector.stops.size(); i++) {
931 Inkscape::CSSOStringStream os;
932 Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
933 sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
934 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
935 * sense for offset proportions. */
936 gchar c[64];
937 sp_svg_write_color(c, sizeof(c), gr->vector.stops[i].color.toRGBA32( 0x00 ));
938 os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
939 child->setAttribute("style", os.str().c_str());
940 /* Order will be reversed here */
941 cl = g_slist_prepend(cl, child);
942 }
944 sp_gradient_repr_clear_vector(gr);
946 /* And insert new children from list */
947 while (cl) {
948 Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
949 repr->addChild(child, NULL);
950 Inkscape::GC::release(child);
951 cl = g_slist_remove(cl, child);
952 }
953 }
956 void SPGradientImpl::gradientRefModified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
957 {
958 if ( gradient->invalidateVector() ) {
959 SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
960 // Conditional to avoid causing infinite loop if there's a cycle in the href chain.
961 }
962 }
964 /** Return true if change made. */
965 bool SPGradient::invalidateVector()
966 {
967 bool ret = false;
969 if (color != NULL) {
970 g_free(color);
971 color = NULL;
972 ret = true;
973 }
975 if (vector.built) {
976 vector.built = false;
977 vector.stops.clear();
978 ret = true;
979 }
981 return ret;
982 }
984 /** Creates normalized color vector */
985 void SPGradient::rebuildVector()
986 {
987 gint len = 0;
988 for ( SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
989 child != NULL ;
990 child = SP_OBJECT_NEXT(child) ) {
991 if (SP_IS_STOP(child)) {
992 len ++;
993 }
994 }
996 has_stops = (len != 0);
998 vector.stops.clear();
1000 SPGradient *reffed = ref->getObject();
1001 if ( !hasStops() && reffed ) {
1002 /* Copy vector from referenced gradient */
1003 vector.built = true; // Prevent infinite recursion.
1004 reffed->ensureVector();
1005 if (!reffed->vector.stops.empty()) {
1006 vector.built = reffed->vector.built;
1007 vector.stops.assign(reffed->vector.stops.begin(), reffed->vector.stops.end());
1008 return;
1009 }
1010 }
1012 for (SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
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 (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, 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 vector.stops.push_back(gstop);
1039 }
1040 }
1042 // Normalize per section 13.2.4 of SVG 1.1.
1043 if (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 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 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 (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 = vector.stops.front().color;
1070 gstop.opacity = vector.stops.front().opacity;
1071 vector.stops.insert(vector.stops.begin(), gstop);
1072 }
1073 if (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 = vector.stops.back().color;
1078 gstop.opacity = vector.stops.back().opacity;
1079 vector.stops.push_back(gstop);
1080 }
1081 }
1083 vector.built = true;
1084 }
1086 /**
1087 * The gradient's color array is newly created and set up from vector.
1088 */
1089 void SPGradient::ensureColors()
1090 {
1091 if (!vector.built) {
1092 rebuildVector();
1093 }
1094 g_return_if_fail(!vector.stops.empty());
1096 /// \todo Where is the memory freed?
1097 if (!color) {
1098 color = g_new(guchar, 4 * NCOLORS);
1099 }
1101 // This assumes that vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1102 // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1103 // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1105 double remainder[4] = {0,0,0,0};
1106 double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1107 switch(spread) {
1108 case SP_GRADIENT_SPREAD_PAD:
1109 remainder[0] = 0.5*vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1110 remainder[1] = 0.5*vector.stops[0].color.v.c[1];
1111 remainder[2] = 0.5*vector.stops[0].color.v.c[2];
1112 remainder[3] = 0.5*vector.stops[0].opacity;
1113 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
1114 remainder_for_end[1] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[1];
1115 remainder_for_end[2] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[2];
1116 remainder_for_end[3] = 0.5*vector.stops[vector.stops.size() - 1].opacity;
1117 break;
1118 case SP_GRADIENT_SPREAD_REFLECT:
1119 case SP_GRADIENT_SPREAD_REPEAT:
1120 // These two are handled differently, see below.
1121 break;
1122 default:
1123 g_error("Spread type not supported!");
1124 };
1125 for (unsigned int i = 0; i < vector.stops.size() - 1; i++) {
1126 double r0 = vector.stops[i].color.v.c[0];
1127 double g0 = vector.stops[i].color.v.c[1];
1128 double b0 = vector.stops[i].color.v.c[2];
1129 double a0 = vector.stops[i].opacity;
1130 double r1 = vector.stops[i+1].color.v.c[0];
1131 double g1 = vector.stops[i+1].color.v.c[1];
1132 double b1 = vector.stops[i+1].color.v.c[2];
1133 double a1 = vector.stops[i+1].opacity;
1134 double o0 = vector.stops[i].offset * (NCOLORS-1);
1135 double o1 = vector.stops[i + 1].offset * (NCOLORS-1);
1136 unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1137 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
1139 if (oe == ob) {
1140 // Simple case, this interval starts and stops within one cell
1141 // The contribution of this interval is:
1142 // (o1-o0)*(c(o0)+c(o1))/2
1143 // = (o1-o0)*(c0+c1)/2
1144 double dt = 0.5*(o1-o0);
1145 remainder[0] += dt*(r0 + r1);
1146 remainder[1] += dt*(g0 + g1);
1147 remainder[2] += dt*(b0 + b1);
1148 remainder[3] += dt*(a0 + a1);
1149 } else {
1150 // First compute colors for the cells which are fully covered by the current interval.
1151 // The prefiltered values are equal to the midpoint of each cell here.
1152 // f = (j-o0)/(o1-o0)
1153 // = j*(1/(o1-o0)) - o0/(o1-o0)
1154 double f = (ob-o0) / (o1-o0);
1155 double df = 1. / (o1-o0);
1156 for (unsigned int j = ob+1; j < oe; j++) {
1157 f += df;
1158 color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1159 color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1160 color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1161 color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1162 }
1164 // Now handle the beginning
1165 // The contribution of the last point is already in remainder.
1166 // The contribution of this point is:
1167 // (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1168 // = (ob+.5-o0)*c((o0+ob+.5)/2)
1169 // = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1170 // = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1171 double dt = ob+.5-o0;
1172 f = 0.5*dt*df;
1173 if (ob==0 && spread==SP_GRADIENT_SPREAD_REFLECT) {
1174 // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1175 color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1176 color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1177 color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1178 color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1179 } else if (ob==0 && spread==SP_GRADIENT_SPREAD_REPEAT) {
1180 // 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.
1181 remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1182 remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1183 remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1184 remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1185 } else {
1186 // The first half of the cell was already in remainder.
1187 color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1188 color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1189 color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1190 color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1191 }
1193 // Now handle the end, which should end up in remainder
1194 // The contribution of this point is:
1195 // (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1196 // = (o1-oe+.5)*c((o1+oe-.5)/2)
1197 // = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1198 dt = o1-oe+.5;
1199 f = (0.5*(o1+oe-.5)-o0)*df;
1200 remainder[0] = dt*(r0 + f*(r1-r0));
1201 remainder[1] = dt*(g0 + f*(g1-g0));
1202 remainder[2] = dt*(b0 + f*(b1-b0));
1203 remainder[3] = dt*(a0 + f*(a1-a0));
1204 }
1205 }
1206 switch(spread) {
1207 case SP_GRADIENT_SPREAD_PAD:
1208 color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1209 color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1210 color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1211 color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1212 break;
1213 case SP_GRADIENT_SPREAD_REFLECT:
1214 // The second half is the same as the first half, so multiply by 2.
1215 color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1216 color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1217 color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1218 color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1219 break;
1220 case SP_GRADIENT_SPREAD_REPEAT:
1221 // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1222 color[0] = color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1223 color[1] = color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1224 color[2] = color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1225 color[3] = color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1226 break;
1227 }
1228 }
1230 /**
1231 * Renders gradient vector to buffer as line.
1232 *
1233 * RGB buffer background should be set up beforehand.
1234 *
1235 * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1236 * @param span Full integer width of requested gradient.
1237 * @param pos Buffer starting position in span.
1238 */
1239 static void
1240 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1241 gint const len, gint const pos, gint const span)
1242 {
1243 g_return_if_fail(gradient != NULL);
1244 g_return_if_fail(SP_IS_GRADIENT(gradient));
1245 g_return_if_fail(buf != NULL);
1246 g_return_if_fail(len > 0);
1247 g_return_if_fail(pos >= 0);
1248 g_return_if_fail(pos + len <= span);
1249 g_return_if_fail(span > 0);
1251 if (!gradient->color) {
1252 gradient->ensureColors();
1253 }
1255 gint idx = (pos * 1024 << 8) / span;
1256 gint didx = (1024 << 8) / span;
1258 for (gint x = 0; x < len; x++) {
1259 /// \todo Can this be done with 4 byte copies?
1260 *buf++ = gradient->color[4 * (idx >> 8)];
1261 *buf++ = gradient->color[4 * (idx >> 8) + 1];
1262 *buf++ = gradient->color[4 * (idx >> 8) + 2];
1263 *buf++ = gradient->color[4 * (idx >> 8) + 3];
1264 idx += didx;
1265 }
1266 }
1268 /**
1269 * Render rectangular RGBA area from gradient vector.
1270 */
1271 void
1272 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1273 gint const width, gint const height, gint const rowstride,
1274 gint const pos, gint const span, bool const horizontal)
1275 {
1276 g_return_if_fail(gradient != NULL);
1277 g_return_if_fail(SP_IS_GRADIENT(gradient));
1278 g_return_if_fail(buf != NULL);
1279 g_return_if_fail(width > 0);
1280 g_return_if_fail(height > 0);
1281 g_return_if_fail(pos >= 0);
1282 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1283 g_return_if_fail(span > 0);
1285 if (horizontal) {
1286 sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1287 for (gint y = 1; y < height; y++) {
1288 memcpy(buf + y * rowstride, buf, 4 * width);
1289 }
1290 } else {
1291 guchar *tmp = (guchar *)alloca(4 * height);
1292 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1293 for (gint y = 0; y < height; y++) {
1294 guchar *b = buf + y * rowstride;
1295 for (gint x = 0; x < width; x++) {
1296 *b++ = tmp[0];
1297 *b++ = tmp[1];
1298 *b++ = tmp[2];
1299 *b++ = tmp[3];
1300 }
1301 tmp += 4;
1302 }
1303 }
1304 }
1306 /**
1307 * Render rectangular RGB area from gradient vector.
1308 */
1309 void
1310 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1311 gint const width, gint const height, gint const /*rowstride*/,
1312 gint const pos, gint const span, bool const horizontal)
1313 {
1314 g_return_if_fail(gradient != NULL);
1315 g_return_if_fail(SP_IS_GRADIENT(gradient));
1316 g_return_if_fail(buf != NULL);
1317 g_return_if_fail(width > 0);
1318 g_return_if_fail(height > 0);
1319 g_return_if_fail(pos >= 0);
1320 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1321 g_return_if_fail(span > 0);
1323 if (horizontal) {
1324 guchar *tmp = (guchar*)alloca(4 * width);
1325 sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1326 for (gint y = 0; y < height; y++) {
1327 guchar *t = tmp;
1328 for (gint x = 0; x < width; x++) {
1329 gint a = t[3];
1330 gint fc = (t[0] - buf[0]) * a;
1331 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1332 fc = (t[1] - buf[1]) * a;
1333 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1334 fc = (t[2] - buf[2]) * a;
1335 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1336 buf += 3;
1337 t += 4;
1338 }
1339 }
1340 } else {
1341 guchar *tmp = (guchar*)alloca(4 * height);
1342 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1343 for (gint y = 0; y < height; y++) {
1344 guchar *t = tmp + 4 * y;
1345 for (gint x = 0; x < width; x++) {
1346 gint a = t[3];
1347 gint fc = (t[0] - buf[0]) * a;
1348 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1349 fc = (t[1] - buf[1]) * a;
1350 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1351 fc = (t[2] - buf[2]) * a;
1352 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1353 }
1354 }
1355 }
1356 }
1358 Geom::Matrix
1359 sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1360 {
1361 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1362 return ( Geom::Scale(bbox.dimensions())
1363 * Geom::Translate(bbox.min())
1364 * Geom::Matrix(ctm) );
1365 } else {
1366 return ctm;
1367 }
1368 }
1370 Geom::Matrix
1371 sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1372 {
1373 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1374 return ( gr->gradientTransform
1375 * Geom::Scale(bbox.dimensions())
1376 * Geom::Translate(bbox.min())
1377 * Geom::Matrix(ctm) );
1378 } else {
1379 return gr->gradientTransform * ctm;
1380 }
1381 }
1383 void
1384 sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1385 Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1386 {
1387 gr->gradientTransform = gs2d * ctm.inverse();
1388 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1389 gr->gradientTransform = ( gr->gradientTransform
1390 * Geom::Translate(-bbox.min())
1391 * Geom::Scale(bbox.dimensions()).inverse() );
1392 }
1393 gr->gradientTransform_set = TRUE;
1395 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1396 }
1398 /*
1399 * Linear Gradient
1400 */
1402 class SPLGPainter;
1404 /// A context with linear gradient, painter, and gradient renderer.
1405 struct SPLGPainter {
1406 SPPainter painter;
1407 SPLinearGradient *lg;
1409 NRLGradientRenderer lgr;
1411 static SPPainter * painter_new(SPPaintServer *ps,
1412 Geom::Matrix const &full_transform,
1413 Geom::Matrix const &parent_transform,
1414 NRRect const *bbox);
1415 };
1417 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1418 static void sp_lineargradient_init(SPLinearGradient *lg);
1420 static void sp_lineargradient_build(SPObject *object,
1421 SPDocument *document,
1422 Inkscape::XML::Node *repr);
1423 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1424 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1425 guint flags);
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()
1438 {
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;
1454 }
1456 /**
1457 * SPLinearGradient vtable initialization.
1458 */
1459 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1460 {
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 = SPLGPainter::painter_new;
1471 ps_class->painter_free = sp_lineargradient_painter_free;
1472 }
1474 /**
1475 * Callback for SPLinearGradient object initialization.
1476 */
1477 static void sp_lineargradient_init(SPLinearGradient *lg)
1478 {
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);
1483 }
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)
1491 {
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");
1499 }
1501 /**
1502 * Callback: set attribute.
1503 */
1504 static void
1505 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1506 {
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 }
1531 }
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)
1538 {
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;
1558 }
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 SPPainter * SPLGPainter::painter_new(SPPaintServer *ps,
1577 Geom::Matrix const &full_transform,
1578 Geom::Matrix const &/*parent_transform*/,
1579 NRRect const *bbox)
1580 {
1581 SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1582 SPGradient *gr = SP_GRADIENT(ps);
1584 if (!gr->color) {
1585 gr->ensureColors();
1586 }
1588 SPLGPainter *lgp = g_new(SPLGPainter, 1);
1590 lgp->painter.type = SP_PAINTER_IND;
1591 lgp->painter.fill = sp_lg_fill;
1593 lgp->lg = lg;
1595 /** \todo
1596 * Technically speaking, we map NCOLORS on line [start,end] onto line
1597 * [0,1]. I almost think we should fill color array start and end in
1598 * that case. The alternative would be to leave these just empty garbage
1599 * or something similar. Originally I had 1023.9999 here - not sure
1600 * whether we have really to cut out ceil int (Lauris).
1601 */
1602 Geom::Matrix color2norm(Geom::identity());
1603 Geom::Matrix color2px;
1604 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1605 Geom::Matrix norm2pos(Geom::identity());
1607 /* BBox to user coordinate system */
1608 Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1610 Geom::Matrix color2pos = color2norm * norm2pos;
1611 Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1612 Geom::Matrix color2user = color2tpos * bbox2user;
1613 color2px = color2user * full_transform;
1615 } else {
1616 /* Problem: What to do, if we have mixed lengths and percentages? */
1617 /* Currently we do ignore percentages at all, but that is not good (lauris) */
1619 Geom::Matrix norm2pos(Geom::identity());
1620 Geom::Matrix color2pos = color2norm * norm2pos;
1621 Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1622 color2px = color2tpos * full_transform;
1624 }
1625 // TODO: remove color2px_nr after converting to 2geom
1626 NR::Matrix color2px_nr = from_2geom(color2px);
1627 nr_lgradient_renderer_setup(&lgp->lgr, gr->color, gr->fetchSpread(), &color2px_nr,
1628 lg->x1.computed, lg->y1.computed,
1629 lg->x2.computed, lg->y2.computed);
1631 return (SPPainter *) lgp;
1632 }
1634 static void
1635 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1636 {
1637 g_free(painter);
1638 }
1640 /**
1641 * Directly set properties of linear gradient and request modified.
1642 */
1643 void
1644 sp_lineargradient_set_position(SPLinearGradient *lg,
1645 gdouble x1, gdouble y1,
1646 gdouble x2, gdouble y2)
1647 {
1648 g_return_if_fail(lg != NULL);
1649 g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1651 /* fixme: units? (Lauris) */
1652 lg->x1.set(SVGLength::NONE, x1, x1);
1653 lg->y1.set(SVGLength::NONE, y1, y1);
1654 lg->x2.set(SVGLength::NONE, x2, x2);
1655 lg->y2.set(SVGLength::NONE, y2, y2);
1657 SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1658 }
1660 /**
1661 * Callback when linear gradient object is rendered.
1662 */
1663 static void
1664 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1665 {
1666 SPLGPainter *lgp = (SPLGPainter *) painter;
1668 if (lgp->lg->color == NULL) {
1669 lgp->lg->ensureColors();
1670 lgp->lgr.vector = lgp->lg->color;
1671 }
1673 nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1674 }
1676 /*
1677 * Radial Gradient
1678 */
1680 class SPRGPainter;
1682 /// A context with radial gradient, painter, and gradient renderer.
1683 struct SPRGPainter {
1684 SPPainter painter;
1685 SPRadialGradient *rg;
1686 NRRGradientRenderer rgr;
1688 static SPPainter *painter_new(SPPaintServer *ps,
1689 Geom::Matrix const &full_transform,
1690 Geom::Matrix const &parent_transform,
1691 NRRect const *bbox);
1692 };
1694 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1695 static void sp_radialgradient_init(SPRadialGradient *rg);
1697 static void sp_radialgradient_build(SPObject *object,
1698 SPDocument *document,
1699 Inkscape::XML::Node *repr);
1700 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1701 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1702 guint flags);
1703 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1705 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1707 static SPGradientClass *rg_parent_class;
1709 /**
1710 * Register SPRadialGradient class and return its type.
1711 */
1712 GType
1713 sp_radialgradient_get_type()
1714 {
1715 static GType type = 0;
1716 if (!type) {
1717 GTypeInfo info = {
1718 sizeof(SPRadialGradientClass),
1719 NULL, NULL,
1720 (GClassInitFunc) sp_radialgradient_class_init,
1721 NULL, NULL,
1722 sizeof(SPRadialGradient),
1723 16,
1724 (GInstanceInitFunc) sp_radialgradient_init,
1725 NULL, /* value_table */
1726 };
1727 type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1728 }
1729 return type;
1730 }
1732 /**
1733 * SPRadialGradient vtable initialization.
1734 */
1735 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1736 {
1737 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1738 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1740 rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1742 sp_object_class->build = sp_radialgradient_build;
1743 sp_object_class->set = sp_radialgradient_set;
1744 sp_object_class->write = sp_radialgradient_write;
1746 ps_class->painter_new = SPRGPainter::painter_new;
1747 ps_class->painter_free = sp_radialgradient_painter_free;
1748 }
1750 /**
1751 * Callback for SPRadialGradient object initialization.
1752 */
1753 static void
1754 sp_radialgradient_init(SPRadialGradient *rg)
1755 {
1756 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1757 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1758 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1759 rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1760 rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1761 }
1763 /**
1764 * Set radial gradient attributes from associated repr.
1765 */
1766 static void
1767 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1768 {
1769 if (((SPObjectClass *) rg_parent_class)->build)
1770 (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1772 sp_object_read_attr(object, "cx");
1773 sp_object_read_attr(object, "cy");
1774 sp_object_read_attr(object, "r");
1775 sp_object_read_attr(object, "fx");
1776 sp_object_read_attr(object, "fy");
1777 }
1779 /**
1780 * Set radial gradient attribute.
1781 */
1782 static void
1783 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1784 {
1785 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1787 switch (key) {
1788 case SP_ATTR_CX:
1789 if (!rg->cx.read(value)) {
1790 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1791 }
1792 if (!rg->fx._set) {
1793 rg->fx.value = rg->cx.value;
1794 rg->fx.computed = rg->cx.computed;
1795 }
1796 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1797 break;
1798 case SP_ATTR_CY:
1799 if (!rg->cy.read(value)) {
1800 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1801 }
1802 if (!rg->fy._set) {
1803 rg->fy.value = rg->cy.value;
1804 rg->fy.computed = rg->cy.computed;
1805 }
1806 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1807 break;
1808 case SP_ATTR_R:
1809 if (!rg->r.read(value)) {
1810 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1811 }
1812 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1813 break;
1814 case SP_ATTR_FX:
1815 if (!rg->fx.read(value)) {
1816 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1817 }
1818 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1819 break;
1820 case SP_ATTR_FY:
1821 if (!rg->fy.read(value)) {
1822 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1823 }
1824 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1825 break;
1826 default:
1827 if (((SPObjectClass *) rg_parent_class)->set)
1828 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1829 break;
1830 }
1831 }
1833 /**
1834 * Write radial gradient attributes to associated repr.
1835 */
1836 static Inkscape::XML::Node *
1837 sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1838 {
1839 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1841 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1842 repr = xml_doc->createElement("svg:radialGradient");
1843 }
1845 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1846 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1847 if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1848 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1849 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1851 if (((SPObjectClass *) rg_parent_class)->write)
1852 (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1854 return repr;
1855 }
1857 /**
1858 * Create radial gradient context.
1859 */
1860 SPPainter *SPRGPainter::painter_new(SPPaintServer *ps,
1861 Geom::Matrix const &full_transform,
1862 Geom::Matrix const &/*parent_transform*/,
1863 NRRect const *bbox)
1864 {
1865 SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1866 SPGradient *gr = SP_GRADIENT(ps);
1868 if (!gr->color) {
1869 gr->ensureColors();
1870 }
1872 SPRGPainter *rgp = g_new(SPRGPainter, 1);
1874 rgp->painter.type = SP_PAINTER_IND;
1875 rgp->painter.fill = sp_rg_fill;
1877 rgp->rg = rg;
1879 Geom::Matrix gs2px;
1881 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1882 /** \todo
1883 * fixme: We may try to normalize here too, look at
1884 * linearGradient (Lauris)
1885 */
1887 /* BBox to user coordinate system */
1888 Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1890 Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1892 gs2px = gs2user * full_transform;
1893 } else {
1894 /** \todo
1895 * Problem: What to do, if we have mixed lengths and percentages?
1896 * Currently we do ignore percentages at all, but that is not
1897 * good (lauris)
1898 */
1900 gs2px = gr->gradientTransform * full_transform;
1901 }
1902 // TODO: remove gs2px_nr after converting to 2geom
1903 NR::Matrix gs2px_nr = from_2geom(gs2px);
1904 nr_rgradient_renderer_setup(&rgp->rgr, gr->color, gr->fetchSpread(),
1905 &gs2px_nr,
1906 rg->cx.computed, rg->cy.computed,
1907 rg->fx.computed, rg->fy.computed,
1908 rg->r.computed);
1910 return (SPPainter *) rgp;
1911 }
1913 static void
1914 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1915 {
1916 g_free(painter);
1917 }
1919 /**
1920 * Directly set properties of radial gradient and request modified.
1921 */
1922 void
1923 sp_radialgradient_set_position(SPRadialGradient *rg,
1924 gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1925 {
1926 g_return_if_fail(rg != NULL);
1927 g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1929 /* fixme: units? (Lauris) */
1930 rg->cx.set(SVGLength::NONE, cx, cx);
1931 rg->cy.set(SVGLength::NONE, cy, cy);
1932 rg->fx.set(SVGLength::NONE, fx, fx);
1933 rg->fy.set(SVGLength::NONE, fy, fy);
1934 rg->r.set(SVGLength::NONE, r, r);
1936 SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1937 }
1939 /**
1940 * Callback when radial gradient object is rendered.
1941 */
1942 static void
1943 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1944 {
1945 SPRGPainter *rgp = (SPRGPainter *) painter;
1947 if (rgp->rg->color == NULL) {
1948 rgp->rg->ensureColors();
1949 rgp->rgr.vector = rgp->rg->color;
1950 }
1952 nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1953 }
1955 /*
1956 Local Variables:
1957 mode:c++
1958 c-file-style:"stroustrup"
1959 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1960 indent-tabs-mode:nil
1961 fill-column:99
1962 End:
1963 */
1964 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :