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