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