34912d1dafc11b0dccb1f9f78d0c7b93161327d7
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 // TODO need to properly read full color, including icc
176 guint32 const color = sp_svg_read_color(p, 0);
177 stop->specified_color.set( color );
178 }
179 }
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_PROP_STOP_COLOR: {
189 {
190 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
191 if (streq(p, "currentColor")) {
192 stop->currentColor = true;
193 } else {
194 stop->currentColor = false;
195 guint32 const color = sp_svg_read_color(p, 0);
196 stop->specified_color.set( color );
197 }
198 }
199 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
200 break;
201 }
202 case SP_PROP_STOP_OPACITY: {
203 {
204 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
205 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
206 stop->opacity = opacity;
207 }
208 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
209 break;
210 }
211 case SP_ATTR_OFFSET: {
212 stop->offset = sp_svg_read_percentage(value, 0.0);
213 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
214 break;
215 }
216 default: {
217 if (((SPObjectClass *) stop_parent_class)->set)
218 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
219 break;
220 }
221 }
222 }
224 /**
225 * Virtual write: write object attributes to repr.
226 */
227 static Inkscape::XML::Node *
228 sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
229 {
230 SPStop *stop = SP_STOP(object);
232 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
233 repr = xml_doc->createElement("svg:stop");
234 }
236 guint32 specifiedcolor = stop->specified_color.toRGBA32( 255 );
237 gfloat opacity = stop->opacity;
239 if (((SPObjectClass *) stop_parent_class)->write)
240 (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
242 // Since we do a hackish style setting here (because SPStyle does not support stop-color and
243 // stop-opacity), we must do it AFTER calling the parent write method; otherwise
244 // sp_object_write would clear our style= attribute (bug 1695287)
246 Inkscape::CSSOStringStream os;
247 os << "stop-color:";
248 if (stop->currentColor) {
249 os << "currentColor";
250 } else {
251 gchar c[64];
252 sp_svg_write_color(c, sizeof(c), specifiedcolor);
253 os << c;
254 }
255 os << ";stop-opacity:" << opacity;
256 repr->setAttribute("style", os.str().c_str());
257 repr->setAttribute("stop-color", NULL);
258 repr->setAttribute("stop-opacity", NULL);
259 sp_repr_set_css_double(repr, "offset", stop->offset);
260 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no sense
261 * for offset proportions. */
263 return repr;
264 }
267 bool SPGradient::hasStops() const
268 {
269 return has_stops;
270 }
272 bool SPGradient::isUnitsSet() const
273 {
274 return units_set;
275 }
277 SPGradientUnits SPGradient::getUnits() const
278 {
279 return units;
280 }
282 bool SPGradient::isSpreadSet() const
283 {
284 return spread_set;
285 }
287 SPGradientSpread SPGradient::getSpread() const
288 {
289 return spread;
290 }
292 void SPGradient::setSwatch( bool swatch )
293 {
294 if ( swatch != isSwatch() ) {
295 this->swatch = swatch; // to make isSolid() work, this happens first
296 gchar const* paintVal = swatch ? (isSolid() ? "solid" : "gradient") : 0;
297 sp_object_setAttribute( this, "osb:paint", paintVal, 0 );
299 requestModified( SP_OBJECT_MODIFIED_FLAG );
300 }
301 }
303 /**
304 * Return stop's color as 32bit value.
305 */
306 guint32
307 sp_stop_get_rgba32(SPStop const *const stop)
308 {
309 guint32 rgb0 = 0;
310 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
311 * value depends on user agent, and don't give any further restrictions that I can
312 * see.) */
313 if (stop->currentColor) {
314 char const *str = sp_object_get_style_property(stop, "color", NULL);
315 if (str) {
316 rgb0 = sp_svg_read_color(str, rgb0);
317 }
318 unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
319 g_return_val_if_fail((alpha & ~0xff) == 0,
320 rgb0 | 0xff);
321 return rgb0 | alpha;
322 } else {
323 return stop->specified_color.toRGBA32( stop->opacity );
324 }
325 }
327 /**
328 * Return stop's color as SPColor.
329 */
330 static SPColor
331 sp_stop_get_color(SPStop const *const stop)
332 {
333 if (stop->currentColor) {
334 char const *str = sp_object_get_style_property(stop, "color", NULL);
335 guint32 const dfl = 0;
336 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
337 * value depends on user agent, and don't give any further restrictions that I can
338 * see.) */
339 guint32 color = dfl;
340 if (str) {
341 color = sp_svg_read_color(str, dfl);
342 }
343 SPColor ret( color );
344 return ret;
345 } else {
346 return stop->specified_color;
347 }
348 }
350 /*
351 * Gradient
352 */
354 static SPPaintServerClass *gradient_parent_class;
356 /**
357 * Registers SPGradient class and returns its type.
358 */
359 GType SPGradient::getType()
360 {
361 static GType gradient_type = 0;
362 if (!gradient_type) {
363 GTypeInfo gradient_info = {
364 sizeof(SPGradientClass),
365 NULL, NULL,
366 (GClassInitFunc) SPGradientImpl::classInit,
367 NULL, NULL,
368 sizeof(SPGradient),
369 16,
370 (GInstanceInitFunc) SPGradientImpl::init,
371 NULL, /* value_table */
372 };
373 gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
374 &gradient_info, (GTypeFlags)0);
375 }
376 return gradient_type;
377 }
379 /**
380 * SPGradient vtable initialization.
381 */
382 void SPGradientImpl::classInit(SPGradientClass *klass)
383 {
384 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
386 gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
388 sp_object_class->build = SPGradientImpl::build;
389 sp_object_class->release = SPGradientImpl::release;
390 sp_object_class->set = SPGradientImpl::setGradientAttr;
391 sp_object_class->child_added = SPGradientImpl::childAdded;
392 sp_object_class->remove_child = SPGradientImpl::removeChild;
393 sp_object_class->modified = SPGradientImpl::modified;
394 sp_object_class->write = SPGradientImpl::write;
395 }
397 /**
398 * Callback for SPGradient object initialization.
399 */
400 void SPGradientImpl::init(SPGradient *gr)
401 {
402 gr->ref = new SPGradientReference(SP_OBJECT(gr));
403 gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(SPGradientImpl::gradientRefChanged), gr));
405 /** \todo
406 * Fixme: reprs being rearranged (e.g. via the XML editor)
407 * may require us to clear the state.
408 */
409 gr->state = SP_GRADIENT_STATE_UNKNOWN;
411 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
412 gr->units_set = FALSE;
414 gr->gradientTransform = Geom::identity();
415 gr->gradientTransform_set = FALSE;
417 gr->spread = SP_GRADIENT_SPREAD_PAD;
418 gr->spread_set = FALSE;
420 gr->has_stops = FALSE;
422 gr->vector.built = false;
423 gr->vector.stops.clear();
425 gr->color = NULL;
427 new (&gr->modified_connection) sigc::connection();
428 }
430 /**
431 * Virtual build: set gradient attributes from its associated repr.
432 */
433 void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
434 {
435 SPGradient *gradient = SP_GRADIENT(object);
437 // Work-around in case a swatch had been marked for immediate collection:
438 if ( repr->attribute("osb:paint") && repr->attribute("inkscape:collect") ) {
439 repr->setAttribute("inkscape:collect", 0);
440 }
442 if (((SPObjectClass *) gradient_parent_class)->build) {
443 (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
444 }
446 for ( SPObject *ochild = sp_object_first_child(object); ochild; ochild = ochild->next ) {
447 if (SP_IS_STOP(ochild)) {
448 gradient->has_stops = TRUE;
449 break;
450 }
451 }
453 sp_object_read_attr(object, "gradientUnits");
454 sp_object_read_attr(object, "gradientTransform");
455 sp_object_read_attr(object, "spreadMethod");
456 sp_object_read_attr(object, "xlink:href");
457 sp_object_read_attr(object, "osb:paint");
459 /* Register ourselves */
460 sp_document_add_resource(document, "gradient", object);
461 }
463 /**
464 * Virtual release of SPGradient members before destruction.
465 */
466 void SPGradientImpl::release(SPObject *object)
467 {
468 SPGradient *gradient = (SPGradient *) object;
470 #ifdef SP_GRADIENT_VERBOSE
471 g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
472 #endif
474 if (SP_OBJECT_DOCUMENT(object)) {
475 /* Unregister ourselves */
476 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
477 }
479 if (gradient->ref) {
480 gradient->modified_connection.disconnect();
481 gradient->ref->detach();
482 delete gradient->ref;
483 gradient->ref = NULL;
484 }
486 if (gradient->color) {
487 g_free(gradient->color);
488 gradient->color = NULL;
489 }
491 gradient->modified_connection.~connection();
493 if (((SPObjectClass *) gradient_parent_class)->release)
494 ((SPObjectClass *) gradient_parent_class)->release(object);
495 }
497 /**
498 * Set gradient attribute to value.
499 */
500 void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const *value)
501 {
502 SPGradient *gr = SP_GRADIENT(object);
504 switch (key) {
505 case SP_ATTR_GRADIENTUNITS:
506 if (value) {
507 if (!strcmp(value, "userSpaceOnUse")) {
508 gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
509 } else {
510 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
511 }
512 gr->units_set = TRUE;
513 } else {
514 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
515 gr->units_set = FALSE;
516 }
517 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
518 break;
519 case SP_ATTR_GRADIENTTRANSFORM: {
520 Geom::Matrix t;
521 if (value && sp_svg_transform_read(value, &t)) {
522 gr->gradientTransform = t;
523 gr->gradientTransform_set = TRUE;
524 } else {
525 gr->gradientTransform = Geom::identity();
526 gr->gradientTransform_set = FALSE;
527 }
528 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
529 break;
530 }
531 case SP_ATTR_SPREADMETHOD:
532 if (value) {
533 if (!strcmp(value, "reflect")) {
534 gr->spread = SP_GRADIENT_SPREAD_REFLECT;
535 } else if (!strcmp(value, "repeat")) {
536 gr->spread = SP_GRADIENT_SPREAD_REPEAT;
537 } else {
538 gr->spread = SP_GRADIENT_SPREAD_PAD;
539 }
540 gr->spread_set = TRUE;
541 } else {
542 gr->spread_set = FALSE;
543 }
544 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
545 break;
546 case SP_ATTR_XLINK_HREF:
547 if (value) {
548 try {
549 gr->ref->attach(Inkscape::URI(value));
550 } catch (Inkscape::BadURIException &e) {
551 g_warning("%s", e.what());
552 gr->ref->detach();
553 }
554 } else {
555 gr->ref->detach();
556 }
557 break;
558 case SP_ATTR_OSB_SWATCH:
559 {
560 bool newVal = (value != 0);
561 bool modified = false;
562 if (newVal != gr->swatch) {
563 gr->swatch = newVal;
564 modified = true;
565 }
566 if (newVal) {
567 // Might need to flip solid/gradient
568 Glib::ustring paintVal = ( gr->hasStops() && (gr->getStopCount() == 0) ) ? "solid" : "gradient";
569 if ( paintVal != value ) {
570 sp_object_setAttribute( gr, "osb:paint", paintVal.c_str(), 0 );
571 modified = true;
572 }
573 }
574 if (modified) {
575 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
576 }
577 }
578 break;
579 default:
580 if (((SPObjectClass *) gradient_parent_class)->set) {
581 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
582 }
583 break;
584 }
585 }
587 /**
588 * Gets called when the gradient is (re)attached to another gradient.
589 */
590 void SPGradientImpl::gradientRefChanged(SPObject *old_ref, SPObject *ref, SPGradient *gr)
591 {
592 if (old_ref) {
593 gr->modified_connection.disconnect();
594 }
595 if ( SP_IS_GRADIENT(ref)
596 && ref != gr )
597 {
598 gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&SPGradientImpl::gradientRefModified), gr));
599 }
601 // Per SVG, all unset attributes must be inherited from linked gradient.
602 // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
603 // but without setting the _set flags.
604 // FIXME: do the same for gradientTransform too
605 if (!gr->units_set) {
606 gr->units = gr->fetchUnits();
607 }
608 if (!gr->spread_set) {
609 gr->spread = gr->fetchSpread();
610 }
612 /// \todo Fixme: what should the flags (second) argument be? */
613 gradientRefModified(ref, 0, gr);
614 }
616 /**
617 * Callback for child_added event.
618 */
619 void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
620 {
621 SPGradient *gr = SP_GRADIENT(object);
623 gr->invalidateVector();
625 if (((SPObjectClass *) gradient_parent_class)->child_added) {
626 (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
627 }
629 SPObject *ochild = sp_object_get_child_by_repr(object, child);
630 if ( ochild && SP_IS_STOP(ochild) ) {
631 gr->has_stops = TRUE;
632 if ( gr->getStopCount() > 0 ) {
633 gchar const * attr = gr->repr->attribute("osb:paint");
634 if ( attr && strcmp(attr, "gradient") ) {
635 sp_object_setAttribute( gr, "osb:paint", "gradient", 0 );
636 }
637 }
638 }
640 /// \todo Fixme: should we schedule "modified" here?
641 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
642 }
644 /**
645 * Callback for remove_child event.
646 */
647 void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
648 {
649 SPGradient *gr = SP_GRADIENT(object);
651 gr->invalidateVector();
653 if (((SPObjectClass *) gradient_parent_class)->remove_child) {
654 (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
655 }
657 gr->has_stops = FALSE;
658 SPObject *ochild;
659 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
660 if (SP_IS_STOP(ochild)) {
661 gr->has_stops = TRUE;
662 break;
663 }
664 }
666 if ( gr->getStopCount() == 0 ) {
667 g_message("Check on remove");
668 gchar const * attr = gr->repr->attribute("osb:paint");
669 if ( attr && strcmp(attr, "solid") ) {
670 sp_object_setAttribute( gr, "osb:paint", "solid", 0 );
671 }
672 }
674 /* Fixme: should we schedule "modified" here? */
675 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
676 }
678 /**
679 * Callback for modified event.
680 */
681 void SPGradientImpl::modified(SPObject *object, guint flags)
682 {
683 SPGradient *gr = SP_GRADIENT(object);
685 if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
686 gr->invalidateVector();
687 }
689 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
690 gr->ensureColors();
691 }
693 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
694 flags &= SP_OBJECT_MODIFIED_CASCADE;
696 // FIXME: climb up the ladder of hrefs
697 GSList *l = NULL;
698 for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
699 g_object_ref(G_OBJECT(child));
700 l = g_slist_prepend(l, child);
701 }
702 l = g_slist_reverse(l);
703 while (l) {
704 SPObject *child = SP_OBJECT(l->data);
705 l = g_slist_remove(l, child);
706 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
707 child->emitModified(flags);
708 }
709 g_object_unref(G_OBJECT(child));
710 }
711 }
713 SPStop* SPGradient::getFirstStop()
714 {
715 SPStop* first = 0;
716 for (SPObject *ochild = sp_object_first_child(this); ochild && !first; ochild = SP_OBJECT_NEXT(ochild)) {
717 if (SP_IS_STOP(ochild)) {
718 first = SP_STOP(ochild);
719 }
720 }
721 return first;
722 }
724 int SPGradient::getStopCount() const
725 {
726 int count = 0;
728 for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
729 count++;
730 }
732 return count;
733 }
735 /**
736 * Write gradient attributes to repr.
737 */
738 Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
739 {
740 SPGradient *gr = SP_GRADIENT(object);
742 if (((SPObjectClass *) gradient_parent_class)->write) {
743 (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
744 }
746 if (flags & SP_OBJECT_WRITE_BUILD) {
747 GSList *l = NULL;
748 for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
749 Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
750 if (crepr) {
751 l = g_slist_prepend(l, crepr);
752 }
753 }
754 while (l) {
755 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
756 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
757 l = g_slist_remove(l, l->data);
758 }
759 }
761 if (gr->ref->getURI()) {
762 gchar *uri_string = gr->ref->getURI()->toString();
763 repr->setAttribute("xlink:href", uri_string);
764 g_free(uri_string);
765 }
767 if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
768 switch (gr->units) {
769 case SP_GRADIENT_UNITS_USERSPACEONUSE:
770 repr->setAttribute("gradientUnits", "userSpaceOnUse");
771 break;
772 default:
773 repr->setAttribute("gradientUnits", "objectBoundingBox");
774 break;
775 }
776 }
778 if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
779 gchar *c=sp_svg_transform_write(gr->gradientTransform);
780 repr->setAttribute("gradientTransform", c);
781 g_free(c);
782 }
784 if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
785 /* FIXME: Ensure that gr->spread is the inherited value
786 * if !gr->spread_set. Not currently happening: see SPGradient::modified.
787 */
788 switch (gr->spread) {
789 case SP_GRADIENT_SPREAD_REFLECT:
790 repr->setAttribute("spreadMethod", "reflect");
791 break;
792 case SP_GRADIENT_SPREAD_REPEAT:
793 repr->setAttribute("spreadMethod", "repeat");
794 break;
795 default:
796 repr->setAttribute("spreadMethod", "pad");
797 break;
798 }
799 }
801 if ( (flags & SP_OBJECT_WRITE_EXT) && gr->isSwatch() ) {
802 if ( gr->isSolid() ) {
803 repr->setAttribute( "osb:paint", "solid" );
804 } else {
805 repr->setAttribute( "osb:paint", "gradient" );
806 }
807 } else {
808 repr->setAttribute( "osb:paint", 0 );
809 }
811 return repr;
812 }
814 /**
815 * Forces the vector to be built, if not present (i.e., changed).
816 *
817 * \pre SP_IS_GRADIENT(gradient).
818 */
819 void SPGradient::ensureVector()
820 {
821 if ( !vector.built ) {
822 rebuildVector();
823 }
824 }
826 /**
827 * Set units property of gradient and emit modified.
828 */
829 void SPGradient::setUnits(SPGradientUnits units)
830 {
831 if (units != this->units) {
832 this->units = units;
833 units_set = TRUE;
834 requestModified(SP_OBJECT_MODIFIED_FLAG);
835 }
836 }
838 /**
839 * Set spread property of gradient and emit modified.
840 */
841 void SPGradient::setSpread(SPGradientSpread spread)
842 {
843 if (spread != this->spread) {
844 this->spread = spread;
845 spread_set = TRUE;
846 requestModified(SP_OBJECT_MODIFIED_FLAG);
847 }
848 }
850 /**
851 * Returns the first of {src, src-\>ref-\>getObject(),
852 * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
853 * for which \a match is true, or NULL if none found.
854 *
855 * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
856 * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
857 *
858 * \pre SP_IS_GRADIENT(src).
859 */
860 static SPGradient *
861 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
862 {
863 g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
865 /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2. If there is a
866 loop, then once p1 has entered the loop, we'll detect it the next time the distance between
867 p1 and p2 is a multiple of the loop size. */
868 SPGradient *p1 = src, *p2 = src;
869 bool do1 = false;
870 for (;;) {
871 if (match(p2)) {
872 return p2;
873 }
875 p2 = p2->ref->getObject();
876 if (!p2) {
877 return p2;
878 }
879 if (do1) {
880 p1 = p1->ref->getObject();
881 }
882 do1 = !do1;
884 if ( p2 == p1 ) {
885 /* We've been here before, so return NULL to indicate that no matching gradient found
886 * in the chain. */
887 return NULL;
888 }
889 }
890 }
892 /**
893 * True if gradient has stops.
894 */
895 static bool has_stopsFN(SPGradient const *gr)
896 {
897 return gr->hasStops();
898 }
900 /**
901 * True if gradient has spread set.
902 */
903 static bool has_spread_set(SPGradient const *gr)
904 {
905 return gr->isSpreadSet();
906 }
908 /**
909 * True if gradient has units set.
910 */
911 static bool
912 has_units_set(SPGradient const *gr)
913 {
914 return gr->isUnitsSet();
915 }
918 SPGradient *SPGradient::getVector(bool force_vector)
919 {
920 SPGradient * src = chase_hrefs(this, has_stopsFN);
922 if (force_vector) {
923 src = sp_gradient_ensure_vector_normalized(src);
924 }
925 return src;
926 }
928 /**
929 * Returns the effective spread of given gradient (climbing up the refs chain if needed).
930 *
931 * \pre SP_IS_GRADIENT(gradient).
932 */
933 SPGradientSpread SPGradient::fetchSpread()
934 {
935 SPGradient const *src = chase_hrefs(this, has_spread_set);
936 return ( src
937 ? src->spread
938 : SP_GRADIENT_SPREAD_PAD ); // pad is the default
939 }
941 /**
942 * Returns the effective units of given gradient (climbing up the refs chain if needed).
943 *
944 * \pre SP_IS_GRADIENT(gradient).
945 */
946 SPGradientUnits SPGradient::fetchUnits()
947 {
948 SPGradient const *src = chase_hrefs(this, has_units_set);
949 return ( src
950 ? src->units
951 : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
952 }
955 /**
956 * Clears the gradient's svg:stop children from its repr.
957 */
958 void
959 sp_gradient_repr_clear_vector(SPGradient *gr)
960 {
961 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
963 /* Collect stops from original repr */
964 GSList *sl = NULL;
965 for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
966 if (!strcmp(child->name(), "svg:stop")) {
967 sl = g_slist_prepend(sl, child);
968 }
969 }
970 /* Remove all stops */
971 while (sl) {
972 /** \todo
973 * fixme: This should work, unless we make gradient
974 * into generic group.
975 */
976 sp_repr_unparent((Inkscape::XML::Node *)sl->data);
977 sl = g_slist_remove(sl, sl->data);
978 }
979 }
981 /**
982 * Writes the gradient's internal vector (whether from its own stops, or
983 * inherited from refs) into the gradient repr as svg:stop elements.
984 */
985 void
986 sp_gradient_repr_write_vector(SPGradient *gr)
987 {
988 g_return_if_fail(gr != NULL);
989 g_return_if_fail(SP_IS_GRADIENT(gr));
991 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
992 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
994 /* We have to be careful, as vector may be our own, so construct repr list at first */
995 GSList *cl = NULL;
997 for (guint i = 0; i < gr->vector.stops.size(); i++) {
998 Inkscape::CSSOStringStream os;
999 Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
1000 sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
1001 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
1002 * sense for offset proportions. */
1003 gchar c[64];
1004 sp_svg_write_color(c, sizeof(c), gr->vector.stops[i].color.toRGBA32( 0x00 ));
1005 os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
1006 child->setAttribute("style", os.str().c_str());
1007 /* Order will be reversed here */
1008 cl = g_slist_prepend(cl, child);
1009 }
1011 sp_gradient_repr_clear_vector(gr);
1013 /* And insert new children from list */
1014 while (cl) {
1015 Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
1016 repr->addChild(child, NULL);
1017 Inkscape::GC::release(child);
1018 cl = g_slist_remove(cl, child);
1019 }
1020 }
1023 void SPGradientImpl::gradientRefModified(SPObject */*href*/, guint /*flags*/, SPGradient *gradient)
1024 {
1025 if ( gradient->invalidateVector() ) {
1026 SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1027 // Conditional to avoid causing infinite loop if there's a cycle in the href chain.
1028 }
1029 }
1031 /** Return true if change made. */
1032 bool SPGradient::invalidateVector()
1033 {
1034 bool ret = false;
1036 if (color != NULL) {
1037 g_free(color);
1038 color = NULL;
1039 ret = true;
1040 }
1042 if (vector.built) {
1043 vector.built = false;
1044 vector.stops.clear();
1045 ret = true;
1046 }
1048 return ret;
1049 }
1051 /** Creates normalized color vector */
1052 void SPGradient::rebuildVector()
1053 {
1054 gint len = 0;
1055 for ( SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
1056 child != NULL ;
1057 child = SP_OBJECT_NEXT(child) ) {
1058 if (SP_IS_STOP(child)) {
1059 len ++;
1060 }
1061 }
1063 has_stops = (len != 0);
1065 vector.stops.clear();
1067 SPGradient *reffed = ref->getObject();
1068 if ( !hasStops() && reffed ) {
1069 /* Copy vector from referenced gradient */
1070 vector.built = true; // Prevent infinite recursion.
1071 reffed->ensureVector();
1072 if (!reffed->vector.stops.empty()) {
1073 vector.built = reffed->vector.built;
1074 vector.stops.assign(reffed->vector.stops.begin(), reffed->vector.stops.end());
1075 return;
1076 }
1077 }
1079 for (SPObject *child = sp_object_first_child(SP_OBJECT(this)) ;
1080 child != NULL;
1081 child = SP_OBJECT_NEXT(child) ) {
1082 if (SP_IS_STOP(child)) {
1083 SPStop *stop = SP_STOP(child);
1085 SPGradientStop gstop;
1086 if (vector.stops.size() > 0) {
1087 // "Each gradient offset value is required to be equal to or greater than the
1088 // previous gradient stop's offset value. If a given gradient stop's offset
1089 // value is not equal to or greater than all previous offset values, then the
1090 // offset value is adjusted to be equal to the largest of all previous offset
1091 // values."
1092 gstop.offset = MAX(stop->offset, vector.stops.back().offset);
1093 } else {
1094 gstop.offset = stop->offset;
1095 }
1097 // "Gradient offset values less than 0 (or less than 0%) are rounded up to
1098 // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
1099 // down to 100%."
1100 gstop.offset = CLAMP(gstop.offset, 0, 1);
1102 gstop.color = sp_stop_get_color(stop);
1103 gstop.opacity = stop->opacity;
1105 vector.stops.push_back(gstop);
1106 }
1107 }
1109 // Normalize per section 13.2.4 of SVG 1.1.
1110 if (vector.stops.size() == 0) {
1111 /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
1112 * paint style."
1113 */
1114 {
1115 SPGradientStop gstop;
1116 gstop.offset = 0.0;
1117 gstop.color.set( 0x00000000 );
1118 gstop.opacity = 0.0;
1119 vector.stops.push_back(gstop);
1120 }
1121 {
1122 SPGradientStop gstop;
1123 gstop.offset = 1.0;
1124 gstop.color.set( 0x00000000 );
1125 gstop.opacity = 0.0;
1126 vector.stops.push_back(gstop);
1127 }
1128 } else {
1129 /* "If one stop is defined, then paint with the solid color fill using the color defined
1130 * for that gradient stop."
1131 */
1132 if (vector.stops.front().offset > 0.0) {
1133 // If the first one is not at 0, then insert a copy of the first at 0.
1134 SPGradientStop gstop;
1135 gstop.offset = 0.0;
1136 gstop.color = vector.stops.front().color;
1137 gstop.opacity = vector.stops.front().opacity;
1138 vector.stops.insert(vector.stops.begin(), gstop);
1139 }
1140 if (vector.stops.back().offset < 1.0) {
1141 // If the last one is not at 1, then insert a copy of the last at 1.
1142 SPGradientStop gstop;
1143 gstop.offset = 1.0;
1144 gstop.color = vector.stops.back().color;
1145 gstop.opacity = vector.stops.back().opacity;
1146 vector.stops.push_back(gstop);
1147 }
1148 }
1150 vector.built = true;
1151 }
1153 /**
1154 * The gradient's color array is newly created and set up from vector.
1155 */
1156 void SPGradient::ensureColors()
1157 {
1158 if (!vector.built) {
1159 rebuildVector();
1160 }
1161 g_return_if_fail(!vector.stops.empty());
1163 /// \todo Where is the memory freed?
1164 if (!color) {
1165 color = g_new(guchar, 4 * NCOLORS);
1166 }
1168 // This assumes that vector is a zero-order B-spline (box function) approximation of the "true" gradient.
1169 // This means that the "true" gradient must be prefiltered using a zero order B-spline and then sampled.
1170 // Furthermore, the first element corresponds to offset="0" and the last element to offset="1".
1172 double remainder[4] = {0,0,0,0};
1173 double remainder_for_end[4] = {0,0,0,0}; // Used at the end
1174 switch(spread) {
1175 case SP_GRADIENT_SPREAD_PAD:
1176 remainder[0] = 0.5*vector.stops[0].color.v.c[0]; // Half of the first cell uses the color of the first stop
1177 remainder[1] = 0.5*vector.stops[0].color.v.c[1];
1178 remainder[2] = 0.5*vector.stops[0].color.v.c[2];
1179 remainder[3] = 0.5*vector.stops[0].opacity;
1180 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
1181 remainder_for_end[1] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[1];
1182 remainder_for_end[2] = 0.5*vector.stops[vector.stops.size() - 1].color.v.c[2];
1183 remainder_for_end[3] = 0.5*vector.stops[vector.stops.size() - 1].opacity;
1184 break;
1185 case SP_GRADIENT_SPREAD_REFLECT:
1186 case SP_GRADIENT_SPREAD_REPEAT:
1187 // These two are handled differently, see below.
1188 break;
1189 default:
1190 g_error("Spread type not supported!");
1191 };
1192 for (unsigned int i = 0; i < vector.stops.size() - 1; i++) {
1193 double r0 = vector.stops[i].color.v.c[0];
1194 double g0 = vector.stops[i].color.v.c[1];
1195 double b0 = vector.stops[i].color.v.c[2];
1196 double a0 = vector.stops[i].opacity;
1197 double r1 = vector.stops[i+1].color.v.c[0];
1198 double g1 = vector.stops[i+1].color.v.c[1];
1199 double b1 = vector.stops[i+1].color.v.c[2];
1200 double a1 = vector.stops[i+1].opacity;
1201 double o0 = vector.stops[i].offset * (NCOLORS-1);
1202 double o1 = vector.stops[i + 1].offset * (NCOLORS-1);
1203 unsigned int ob = (unsigned int) floor(o0+.5); // These are the first and last element that might be affected by this interval.
1204 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
1206 if (oe == ob) {
1207 // Simple case, this interval starts and stops within one cell
1208 // The contribution of this interval is:
1209 // (o1-o0)*(c(o0)+c(o1))/2
1210 // = (o1-o0)*(c0+c1)/2
1211 double dt = 0.5*(o1-o0);
1212 remainder[0] += dt*(r0 + r1);
1213 remainder[1] += dt*(g0 + g1);
1214 remainder[2] += dt*(b0 + b1);
1215 remainder[3] += dt*(a0 + a1);
1216 } else {
1217 // First compute colors for the cells which are fully covered by the current interval.
1218 // The prefiltered values are equal to the midpoint of each cell here.
1219 // f = (j-o0)/(o1-o0)
1220 // = j*(1/(o1-o0)) - o0/(o1-o0)
1221 double f = (ob-o0) / (o1-o0);
1222 double df = 1. / (o1-o0);
1223 for (unsigned int j = ob+1; j < oe; j++) {
1224 f += df;
1225 color[4 * j + 0] = (unsigned char) floor(255*(r0 + f*(r1-r0)) + .5);
1226 color[4 * j + 1] = (unsigned char) floor(255*(g0 + f*(g1-g0)) + .5);
1227 color[4 * j + 2] = (unsigned char) floor(255*(b0 + f*(b1-b0)) + .5);
1228 color[4 * j + 3] = (unsigned char) floor(255*(a0 + f*(a1-a0)) + .5);
1229 }
1231 // Now handle the beginning
1232 // The contribution of the last point is already in remainder.
1233 // The contribution of this point is:
1234 // (ob+.5-o0)*(c(o0)+c(ob+.5))/2
1235 // = (ob+.5-o0)*c((o0+ob+.5)/2)
1236 // = (ob+.5-o0)*(c0+((o0+ob+.5)/2-o0)*df*(c1-c0))
1237 // = (ob+.5-o0)*(c0+(ob+.5-o0)*df*(c1-c0)/2)
1238 double dt = ob+.5-o0;
1239 f = 0.5*dt*df;
1240 if (ob==0 && spread==SP_GRADIENT_SPREAD_REFLECT) {
1241 // The first half of the first cell is just a mirror image of the second half, so simply multiply it by 2.
1242 color[4 * ob + 0] = (unsigned char) floor(2*255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1243 color[4 * ob + 1] = (unsigned char) floor(2*255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1244 color[4 * ob + 2] = (unsigned char) floor(2*255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1245 color[4 * ob + 3] = (unsigned char) floor(2*255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1246 } else if (ob==0 && spread==SP_GRADIENT_SPREAD_REPEAT) {
1247 // 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.
1248 remainder_for_end[0] = remainder[0] + dt*(r0 + f*(r1-r0));
1249 remainder_for_end[1] = remainder[1] + dt*(g0 + f*(g1-g0));
1250 remainder_for_end[2] = remainder[2] + dt*(b0 + f*(b1-b0));
1251 remainder_for_end[3] = remainder[3] + dt*(a0 + f*(a1-a0));
1252 } else {
1253 // The first half of the cell was already in remainder.
1254 color[4 * ob + 0] = (unsigned char) floor(255*(remainder[0] + dt*(r0 + f*(r1-r0))) + .5);
1255 color[4 * ob + 1] = (unsigned char) floor(255*(remainder[1] + dt*(g0 + f*(g1-g0))) + .5);
1256 color[4 * ob + 2] = (unsigned char) floor(255*(remainder[2] + dt*(b0 + f*(b1-b0))) + .5);
1257 color[4 * ob + 3] = (unsigned char) floor(255*(remainder[3] + dt*(a0 + f*(a1-a0))) + .5);
1258 }
1260 // Now handle the end, which should end up in remainder
1261 // The contribution of this point is:
1262 // (o1-oe+.5)*(c(o1)+c(oe-.5))/2
1263 // = (o1-oe+.5)*c((o1+oe-.5)/2)
1264 // = (o1-oe+.5)*(c0+((o1+oe-.5)/2-o0)*df*(c1-c0))
1265 dt = o1-oe+.5;
1266 f = (0.5*(o1+oe-.5)-o0)*df;
1267 remainder[0] = dt*(r0 + f*(r1-r0));
1268 remainder[1] = dt*(g0 + f*(g1-g0));
1269 remainder[2] = dt*(b0 + f*(b1-b0));
1270 remainder[3] = dt*(a0 + f*(a1-a0));
1271 }
1272 }
1273 switch(spread) {
1274 case SP_GRADIENT_SPREAD_PAD:
1275 color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1276 color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1277 color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1278 color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1279 break;
1280 case SP_GRADIENT_SPREAD_REFLECT:
1281 // The second half is the same as the first half, so multiply by 2.
1282 color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(2*255*remainder[0] + .5);
1283 color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(2*255*remainder[1] + .5);
1284 color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(2*255*remainder[2] + .5);
1285 color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(2*255*remainder[3] + .5);
1286 break;
1287 case SP_GRADIENT_SPREAD_REPEAT:
1288 // The second half is the same as the second half of the first cell (which was saved in remainder_for_end).
1289 color[0] = color[4 * (NCOLORS-1) + 0] = (unsigned char) floor(255*(remainder[0]+remainder_for_end[0]) + .5);
1290 color[1] = color[4 * (NCOLORS-1) + 1] = (unsigned char) floor(255*(remainder[1]+remainder_for_end[1]) + .5);
1291 color[2] = color[4 * (NCOLORS-1) + 2] = (unsigned char) floor(255*(remainder[2]+remainder_for_end[2]) + .5);
1292 color[3] = color[4 * (NCOLORS-1) + 3] = (unsigned char) floor(255*(remainder[3]+remainder_for_end[3]) + .5);
1293 break;
1294 }
1295 }
1297 /**
1298 * Renders gradient vector to buffer as line.
1299 *
1300 * RGB buffer background should be set up beforehand.
1301 *
1302 * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1303 * @param span Full integer width of requested gradient.
1304 * @param pos Buffer starting position in span.
1305 */
1306 static void
1307 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1308 gint const len, gint const pos, gint const span)
1309 {
1310 g_return_if_fail(gradient != NULL);
1311 g_return_if_fail(SP_IS_GRADIENT(gradient));
1312 g_return_if_fail(buf != NULL);
1313 g_return_if_fail(len > 0);
1314 g_return_if_fail(pos >= 0);
1315 g_return_if_fail(pos + len <= span);
1316 g_return_if_fail(span > 0);
1318 if (!gradient->color) {
1319 gradient->ensureColors();
1320 }
1322 gint idx = (pos * 1024 << 8) / span;
1323 gint didx = (1024 << 8) / span;
1325 for (gint x = 0; x < len; x++) {
1326 /// \todo Can this be done with 4 byte copies?
1327 *buf++ = gradient->color[4 * (idx >> 8)];
1328 *buf++ = gradient->color[4 * (idx >> 8) + 1];
1329 *buf++ = gradient->color[4 * (idx >> 8) + 2];
1330 *buf++ = gradient->color[4 * (idx >> 8) + 3];
1331 idx += didx;
1332 }
1333 }
1335 /**
1336 * Render rectangular RGBA area from gradient vector.
1337 */
1338 void
1339 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1340 gint const width, gint const height, gint const rowstride,
1341 gint const pos, gint const span, bool const horizontal)
1342 {
1343 g_return_if_fail(gradient != NULL);
1344 g_return_if_fail(SP_IS_GRADIENT(gradient));
1345 g_return_if_fail(buf != NULL);
1346 g_return_if_fail(width > 0);
1347 g_return_if_fail(height > 0);
1348 g_return_if_fail(pos >= 0);
1349 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1350 g_return_if_fail(span > 0);
1352 if (horizontal) {
1353 sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1354 for (gint y = 1; y < height; y++) {
1355 memcpy(buf + y * rowstride, buf, 4 * width);
1356 }
1357 } else {
1358 guchar *tmp = (guchar *)alloca(4 * height);
1359 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1360 for (gint y = 0; y < height; y++) {
1361 guchar *b = buf + y * rowstride;
1362 for (gint x = 0; x < width; x++) {
1363 *b++ = tmp[0];
1364 *b++ = tmp[1];
1365 *b++ = tmp[2];
1366 *b++ = tmp[3];
1367 }
1368 tmp += 4;
1369 }
1370 }
1371 }
1373 /**
1374 * Render rectangular RGB area from gradient vector.
1375 */
1376 void
1377 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1378 gint const width, gint const height, gint const /*rowstride*/,
1379 gint const pos, gint const span, bool const horizontal)
1380 {
1381 g_return_if_fail(gradient != NULL);
1382 g_return_if_fail(SP_IS_GRADIENT(gradient));
1383 g_return_if_fail(buf != NULL);
1384 g_return_if_fail(width > 0);
1385 g_return_if_fail(height > 0);
1386 g_return_if_fail(pos >= 0);
1387 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1388 g_return_if_fail(span > 0);
1390 if (horizontal) {
1391 guchar *tmp = (guchar*)alloca(4 * width);
1392 sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1393 for (gint y = 0; y < height; y++) {
1394 guchar *t = tmp;
1395 for (gint x = 0; x < width; x++) {
1396 gint a = t[3];
1397 gint fc = (t[0] - buf[0]) * a;
1398 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1399 fc = (t[1] - buf[1]) * a;
1400 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1401 fc = (t[2] - buf[2]) * a;
1402 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1403 buf += 3;
1404 t += 4;
1405 }
1406 }
1407 } else {
1408 guchar *tmp = (guchar*)alloca(4 * height);
1409 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1410 for (gint y = 0; y < height; y++) {
1411 guchar *t = tmp + 4 * y;
1412 for (gint x = 0; x < width; x++) {
1413 gint a = t[3];
1414 gint fc = (t[0] - buf[0]) * a;
1415 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1416 fc = (t[1] - buf[1]) * a;
1417 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1418 fc = (t[2] - buf[2]) * a;
1419 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1420 }
1421 }
1422 }
1423 }
1425 Geom::Matrix
1426 sp_gradient_get_g2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1427 {
1428 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1429 return ( Geom::Scale(bbox.dimensions())
1430 * Geom::Translate(bbox.min())
1431 * Geom::Matrix(ctm) );
1432 } else {
1433 return ctm;
1434 }
1435 }
1437 Geom::Matrix
1438 sp_gradient_get_gs2d_matrix(SPGradient const *gr, Geom::Matrix const &ctm, Geom::Rect const &bbox)
1439 {
1440 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1441 return ( gr->gradientTransform
1442 * Geom::Scale(bbox.dimensions())
1443 * Geom::Translate(bbox.min())
1444 * Geom::Matrix(ctm) );
1445 } else {
1446 return gr->gradientTransform * ctm;
1447 }
1448 }
1450 void
1451 sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Matrix const &ctm,
1452 Geom::Rect const &bbox, Geom::Matrix const &gs2d)
1453 {
1454 gr->gradientTransform = gs2d * ctm.inverse();
1455 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1456 gr->gradientTransform = ( gr->gradientTransform
1457 * Geom::Translate(-bbox.min())
1458 * Geom::Scale(bbox.dimensions()).inverse() );
1459 }
1460 gr->gradientTransform_set = TRUE;
1462 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1463 }
1465 /*
1466 * Linear Gradient
1467 */
1469 class SPLGPainter;
1471 /// A context with linear gradient, painter, and gradient renderer.
1472 struct SPLGPainter {
1473 SPPainter painter;
1474 SPLinearGradient *lg;
1476 NRLGradientRenderer lgr;
1478 static SPPainter * painter_new(SPPaintServer *ps,
1479 Geom::Matrix const &full_transform,
1480 Geom::Matrix const &parent_transform,
1481 NRRect const *bbox);
1482 };
1484 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1485 static void sp_lineargradient_init(SPLinearGradient *lg);
1487 static void sp_lineargradient_build(SPObject *object,
1488 SPDocument *document,
1489 Inkscape::XML::Node *repr);
1490 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1491 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1492 guint flags);
1494 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1496 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1498 static SPGradientClass *lg_parent_class;
1500 /**
1501 * Register SPLinearGradient class and return its type.
1502 */
1503 GType
1504 sp_lineargradient_get_type()
1505 {
1506 static GType type = 0;
1507 if (!type) {
1508 GTypeInfo info = {
1509 sizeof(SPLinearGradientClass),
1510 NULL, NULL,
1511 (GClassInitFunc) sp_lineargradient_class_init,
1512 NULL, NULL,
1513 sizeof(SPLinearGradient),
1514 16,
1515 (GInstanceInitFunc) sp_lineargradient_init,
1516 NULL, /* value_table */
1517 };
1518 type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1519 }
1520 return type;
1521 }
1523 /**
1524 * SPLinearGradient vtable initialization.
1525 */
1526 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1527 {
1528 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1529 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1531 lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1533 sp_object_class->build = sp_lineargradient_build;
1534 sp_object_class->set = sp_lineargradient_set;
1535 sp_object_class->write = sp_lineargradient_write;
1537 ps_class->painter_new = SPLGPainter::painter_new;
1538 ps_class->painter_free = sp_lineargradient_painter_free;
1539 }
1541 /**
1542 * Callback for SPLinearGradient object initialization.
1543 */
1544 static void sp_lineargradient_init(SPLinearGradient *lg)
1545 {
1546 lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1547 lg->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
1548 lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1549 lg->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
1550 }
1552 /**
1553 * Callback: set attributes from associated repr.
1554 */
1555 static void sp_lineargradient_build(SPObject *object,
1556 SPDocument *document,
1557 Inkscape::XML::Node *repr)
1558 {
1559 if (((SPObjectClass *) lg_parent_class)->build)
1560 (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1562 sp_object_read_attr(object, "x1");
1563 sp_object_read_attr(object, "y1");
1564 sp_object_read_attr(object, "x2");
1565 sp_object_read_attr(object, "y2");
1566 }
1568 /**
1569 * Callback: set attribute.
1570 */
1571 static void
1572 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1573 {
1574 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1576 switch (key) {
1577 case SP_ATTR_X1:
1578 lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1579 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1580 break;
1581 case SP_ATTR_Y1:
1582 lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1583 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1584 break;
1585 case SP_ATTR_X2:
1586 lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1587 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1588 break;
1589 case SP_ATTR_Y2:
1590 lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1591 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1592 break;
1593 default:
1594 if (((SPObjectClass *) lg_parent_class)->set)
1595 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1596 break;
1597 }
1598 }
1600 /**
1601 * Callback: write attributes to associated repr.
1602 */
1603 static Inkscape::XML::Node *
1604 sp_lineargradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1605 {
1606 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1608 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1609 repr = xml_doc->createElement("svg:linearGradient");
1610 }
1612 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1613 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1614 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1615 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1616 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1617 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1618 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1619 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1621 if (((SPObjectClass *) lg_parent_class)->write)
1622 (* ((SPObjectClass *) lg_parent_class)->write)(object, xml_doc, repr, flags);
1624 return repr;
1625 }
1627 /**
1628 * Create linear gradient context.
1629 *
1630 * Basically we have to deal with transformations
1631 *
1632 * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1633 * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1634 * 2) gradientTransform
1635 * 3) bbox2user
1636 * 4) ctm == userspace to pixel grid
1637 *
1638 * See also (*) in sp-pattern about why we may need parent_transform.
1639 *
1640 * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1641 * and end < 1.
1642 */
1643 SPPainter * SPLGPainter::painter_new(SPPaintServer *ps,
1644 Geom::Matrix const &full_transform,
1645 Geom::Matrix const &/*parent_transform*/,
1646 NRRect const *bbox)
1647 {
1648 SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1649 SPGradient *gr = SP_GRADIENT(ps);
1651 if (!gr->color) {
1652 gr->ensureColors();
1653 }
1655 SPLGPainter *lgp = g_new(SPLGPainter, 1);
1657 lgp->painter.type = SP_PAINTER_IND;
1658 lgp->painter.fill = sp_lg_fill;
1660 lgp->lg = lg;
1662 /** \todo
1663 * Technically speaking, we map NCOLORS on line [start,end] onto line
1664 * [0,1]. I almost think we should fill color array start and end in
1665 * that case. The alternative would be to leave these just empty garbage
1666 * or something similar. Originally I had 1023.9999 here - not sure
1667 * whether we have really to cut out ceil int (Lauris).
1668 */
1669 Geom::Matrix color2norm(Geom::identity());
1670 Geom::Matrix color2px;
1671 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1672 Geom::Matrix norm2pos(Geom::identity());
1674 /* BBox to user coordinate system */
1675 Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1677 Geom::Matrix color2pos = color2norm * norm2pos;
1678 Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1679 Geom::Matrix color2user = color2tpos * bbox2user;
1680 color2px = color2user * full_transform;
1682 } else {
1683 /* Problem: What to do, if we have mixed lengths and percentages? */
1684 /* Currently we do ignore percentages at all, but that is not good (lauris) */
1686 Geom::Matrix norm2pos(Geom::identity());
1687 Geom::Matrix color2pos = color2norm * norm2pos;
1688 Geom::Matrix color2tpos = color2pos * gr->gradientTransform;
1689 color2px = color2tpos * full_transform;
1691 }
1692 // TODO: remove color2px_nr after converting to 2geom
1693 NR::Matrix color2px_nr = from_2geom(color2px);
1694 nr_lgradient_renderer_setup(&lgp->lgr, gr->color, gr->fetchSpread(), &color2px_nr,
1695 lg->x1.computed, lg->y1.computed,
1696 lg->x2.computed, lg->y2.computed);
1698 return (SPPainter *) lgp;
1699 }
1701 static void
1702 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1703 {
1704 g_free(painter);
1705 }
1707 /**
1708 * Directly set properties of linear gradient and request modified.
1709 */
1710 void
1711 sp_lineargradient_set_position(SPLinearGradient *lg,
1712 gdouble x1, gdouble y1,
1713 gdouble x2, gdouble y2)
1714 {
1715 g_return_if_fail(lg != NULL);
1716 g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1718 /* fixme: units? (Lauris) */
1719 lg->x1.set(SVGLength::NONE, x1, x1);
1720 lg->y1.set(SVGLength::NONE, y1, y1);
1721 lg->x2.set(SVGLength::NONE, x2, x2);
1722 lg->y2.set(SVGLength::NONE, y2, y2);
1724 SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1725 }
1727 /**
1728 * Callback when linear gradient object is rendered.
1729 */
1730 static void
1731 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1732 {
1733 SPLGPainter *lgp = (SPLGPainter *) painter;
1735 if (lgp->lg->color == NULL) {
1736 lgp->lg->ensureColors();
1737 lgp->lgr.vector = lgp->lg->color;
1738 }
1740 nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1741 }
1743 /*
1744 * Radial Gradient
1745 */
1747 class SPRGPainter;
1749 /// A context with radial gradient, painter, and gradient renderer.
1750 struct SPRGPainter {
1751 SPPainter painter;
1752 SPRadialGradient *rg;
1753 NRRGradientRenderer rgr;
1755 static SPPainter *painter_new(SPPaintServer *ps,
1756 Geom::Matrix const &full_transform,
1757 Geom::Matrix const &parent_transform,
1758 NRRect const *bbox);
1759 };
1761 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1762 static void sp_radialgradient_init(SPRadialGradient *rg);
1764 static void sp_radialgradient_build(SPObject *object,
1765 SPDocument *document,
1766 Inkscape::XML::Node *repr);
1767 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1768 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
1769 guint flags);
1770 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1772 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1774 static SPGradientClass *rg_parent_class;
1776 /**
1777 * Register SPRadialGradient class and return its type.
1778 */
1779 GType
1780 sp_radialgradient_get_type()
1781 {
1782 static GType type = 0;
1783 if (!type) {
1784 GTypeInfo info = {
1785 sizeof(SPRadialGradientClass),
1786 NULL, NULL,
1787 (GClassInitFunc) sp_radialgradient_class_init,
1788 NULL, NULL,
1789 sizeof(SPRadialGradient),
1790 16,
1791 (GInstanceInitFunc) sp_radialgradient_init,
1792 NULL, /* value_table */
1793 };
1794 type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1795 }
1796 return type;
1797 }
1799 /**
1800 * SPRadialGradient vtable initialization.
1801 */
1802 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1803 {
1804 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1805 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1807 rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1809 sp_object_class->build = sp_radialgradient_build;
1810 sp_object_class->set = sp_radialgradient_set;
1811 sp_object_class->write = sp_radialgradient_write;
1813 ps_class->painter_new = SPRGPainter::painter_new;
1814 ps_class->painter_free = sp_radialgradient_painter_free;
1815 }
1817 /**
1818 * Callback for SPRadialGradient object initialization.
1819 */
1820 static void
1821 sp_radialgradient_init(SPRadialGradient *rg)
1822 {
1823 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1824 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1825 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1826 rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1827 rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1828 }
1830 /**
1831 * Set radial gradient attributes from associated repr.
1832 */
1833 static void
1834 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1835 {
1836 if (((SPObjectClass *) rg_parent_class)->build)
1837 (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1839 sp_object_read_attr(object, "cx");
1840 sp_object_read_attr(object, "cy");
1841 sp_object_read_attr(object, "r");
1842 sp_object_read_attr(object, "fx");
1843 sp_object_read_attr(object, "fy");
1844 }
1846 /**
1847 * Set radial gradient attribute.
1848 */
1849 static void
1850 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1851 {
1852 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1854 switch (key) {
1855 case SP_ATTR_CX:
1856 if (!rg->cx.read(value)) {
1857 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1858 }
1859 if (!rg->fx._set) {
1860 rg->fx.value = rg->cx.value;
1861 rg->fx.computed = rg->cx.computed;
1862 }
1863 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1864 break;
1865 case SP_ATTR_CY:
1866 if (!rg->cy.read(value)) {
1867 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1868 }
1869 if (!rg->fy._set) {
1870 rg->fy.value = rg->cy.value;
1871 rg->fy.computed = rg->cy.computed;
1872 }
1873 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1874 break;
1875 case SP_ATTR_R:
1876 if (!rg->r.read(value)) {
1877 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1878 }
1879 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1880 break;
1881 case SP_ATTR_FX:
1882 if (!rg->fx.read(value)) {
1883 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1884 }
1885 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1886 break;
1887 case SP_ATTR_FY:
1888 if (!rg->fy.read(value)) {
1889 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1890 }
1891 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1892 break;
1893 default:
1894 if (((SPObjectClass *) rg_parent_class)->set)
1895 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1896 break;
1897 }
1898 }
1900 /**
1901 * Write radial gradient attributes to associated repr.
1902 */
1903 static Inkscape::XML::Node *
1904 sp_radialgradient_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
1905 {
1906 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1908 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1909 repr = xml_doc->createElement("svg:radialGradient");
1910 }
1912 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1913 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1914 if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1915 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1916 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1918 if (((SPObjectClass *) rg_parent_class)->write)
1919 (* ((SPObjectClass *) rg_parent_class)->write)(object, xml_doc, repr, flags);
1921 return repr;
1922 }
1924 /**
1925 * Create radial gradient context.
1926 */
1927 SPPainter *SPRGPainter::painter_new(SPPaintServer *ps,
1928 Geom::Matrix const &full_transform,
1929 Geom::Matrix const &/*parent_transform*/,
1930 NRRect const *bbox)
1931 {
1932 SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1933 SPGradient *gr = SP_GRADIENT(ps);
1935 if (!gr->color) {
1936 gr->ensureColors();
1937 }
1939 SPRGPainter *rgp = g_new(SPRGPainter, 1);
1941 rgp->painter.type = SP_PAINTER_IND;
1942 rgp->painter.fill = sp_rg_fill;
1944 rgp->rg = rg;
1946 Geom::Matrix gs2px;
1948 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1949 /** \todo
1950 * fixme: We may try to normalize here too, look at
1951 * linearGradient (Lauris)
1952 */
1954 /* BBox to user coordinate system */
1955 Geom::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1957 Geom::Matrix gs2user = gr->gradientTransform * bbox2user;
1959 gs2px = gs2user * full_transform;
1960 } else {
1961 /** \todo
1962 * Problem: What to do, if we have mixed lengths and percentages?
1963 * Currently we do ignore percentages at all, but that is not
1964 * good (lauris)
1965 */
1967 gs2px = gr->gradientTransform * full_transform;
1968 }
1969 // TODO: remove gs2px_nr after converting to 2geom
1970 NR::Matrix gs2px_nr = from_2geom(gs2px);
1971 nr_rgradient_renderer_setup(&rgp->rgr, gr->color, gr->fetchSpread(),
1972 &gs2px_nr,
1973 rg->cx.computed, rg->cy.computed,
1974 rg->fx.computed, rg->fy.computed,
1975 rg->r.computed);
1977 return (SPPainter *) rgp;
1978 }
1980 static void
1981 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1982 {
1983 g_free(painter);
1984 }
1986 /**
1987 * Directly set properties of radial gradient and request modified.
1988 */
1989 void
1990 sp_radialgradient_set_position(SPRadialGradient *rg,
1991 gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1992 {
1993 g_return_if_fail(rg != NULL);
1994 g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1996 /* fixme: units? (Lauris) */
1997 rg->cx.set(SVGLength::NONE, cx, cx);
1998 rg->cy.set(SVGLength::NONE, cy, cy);
1999 rg->fx.set(SVGLength::NONE, fx, fx);
2000 rg->fy.set(SVGLength::NONE, fy, fy);
2001 rg->r.set(SVGLength::NONE, r, r);
2003 SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
2004 }
2006 /**
2007 * Callback when radial gradient object is rendered.
2008 */
2009 static void
2010 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
2011 {
2012 SPRGPainter *rgp = (SPRGPainter *) painter;
2014 if (rgp->rg->color == NULL) {
2015 rgp->rg->ensureColors();
2016 rgp->rgr.vector = rgp->rg->color;
2017 }
2019 nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
2020 }
2022 /*
2023 Local Variables:
2024 mode:c++
2025 c-file-style:"stroustrup"
2026 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2027 indent-tabs-mode:nil
2028 fill-column:99
2029 End:
2030 */
2031 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :