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 *
11 * Copyright (C) 1999-2002 Lauris Kaplinski
12 * Copyright (C) 2000-2001 Ximian, Inc.
13 * Copyright (C) 2004 David Turner
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 *
17 */
19 #define noSP_GRADIENT_VERBOSE
22 #include <libnr/nr-matrix-div.h>
23 #include <libnr/nr-matrix-fns.h>
24 #include <libnr/nr-matrix-ops.h>
25 #include <libnr/nr-matrix-scale-ops.h>
26 #include <libnr/nr-matrix-translate-ops.h>
27 #include "libnr/nr-scale-translate-ops.h"
29 #include <sigc++/functors/ptr_fun.h>
30 #include <sigc++/adaptors/bind.h>
32 #include "display/nr-gradient-gpl.h"
33 #include "svg/svg.h"
34 #include "svg/svg-color.h"
35 #include "svg/css-ostringstream.h"
36 #include "attributes.h"
37 #include "document-private.h"
38 #include "gradient-chemistry.h"
39 #include "sp-gradient-reference.h"
40 #include "sp-linear-gradient.h"
41 #include "sp-radial-gradient.h"
42 #include "sp-stop.h"
43 #include "streq.h"
44 #include "uri.h"
45 #include "xml/repr.h"
47 #define SP_MACROS_SILENT
48 #include "macros.h"
50 /// Has to be power of 2
51 #define NCOLORS NR_GRADIENT_VECTOR_LENGTH
53 static void sp_stop_class_init(SPStopClass *klass);
54 static void sp_stop_init(SPStop *stop);
56 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
57 static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
58 static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
60 static SPObjectClass *stop_parent_class;
62 /**
63 * Registers SPStop class and returns its type.
64 */
65 GType
66 sp_stop_get_type()
67 {
68 static GType type = 0;
69 if (!type) {
70 GTypeInfo info = {
71 sizeof(SPStopClass),
72 NULL, NULL,
73 (GClassInitFunc) sp_stop_class_init,
74 NULL, NULL,
75 sizeof(SPStop),
76 16,
77 (GInstanceInitFunc) sp_stop_init,
78 NULL, /* value_table */
79 };
80 type = g_type_register_static(SP_TYPE_OBJECT, "SPStop", &info, (GTypeFlags)0);
81 }
82 return type;
83 }
85 /**
86 * Callback to initialize SPStop vtable.
87 */
88 static void sp_stop_class_init(SPStopClass *klass)
89 {
90 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
92 stop_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
94 sp_object_class->build = sp_stop_build;
95 sp_object_class->set = sp_stop_set;
96 sp_object_class->write = sp_stop_write;
97 }
99 /**
100 * Callback to initialize SPStop object.
101 */
102 static void
103 sp_stop_init(SPStop *stop)
104 {
105 stop->offset = 0.0;
106 stop->currentColor = false;
107 sp_color_set_rgb_rgba32(&stop->specified_color, 0x000000ff);
108 stop->opacity = 1.0;
109 }
111 /**
112 * Virtual build: set stop attributes from its associated XML node.
113 */
114 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
115 {
116 if (((SPObjectClass *) stop_parent_class)->build)
117 (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
119 sp_object_read_attr(object, "offset");
120 sp_object_read_attr(object, "stop-color");
121 sp_object_read_attr(object, "stop-opacity");
122 sp_object_read_attr(object, "style");
123 }
125 /**
126 * Virtual set: set attribute to value.
127 */
128 static void
129 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
130 {
131 SPStop *stop = SP_STOP(object);
133 switch (key) {
134 case SP_ATTR_STYLE: {
135 /** \todo
136 * fixme: We are reading simple values 3 times during build (Lauris).
137 * \par
138 * We need presentation attributes etc.
139 * \par
140 * remove the hackish "style reading" from here: see comments in
141 * sp_object_get_style_property about the bugs in our current
142 * approach. However, note that SPStyle doesn't currently have
143 * stop-color and stop-opacity properties.
144 */
145 {
146 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
147 if (streq(p, "currentColor")) {
148 stop->currentColor = true;
149 } else {
150 guint32 const color = sp_svg_read_color(p, 0);
151 sp_color_set_rgb_rgba32(&stop->specified_color, color);
152 }
153 }
154 {
155 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
156 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
157 stop->opacity = opacity;
158 }
159 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
160 break;
161 }
162 case SP_PROP_STOP_COLOR: {
163 {
164 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
165 if (streq(p, "currentColor")) {
166 stop->currentColor = true;
167 } else {
168 stop->currentColor = false;
169 guint32 const color = sp_svg_read_color(p, 0);
170 sp_color_set_rgb_rgba32(&stop->specified_color, color);
171 }
172 }
173 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
174 break;
175 }
176 case SP_PROP_STOP_OPACITY: {
177 {
178 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
179 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
180 stop->opacity = opacity;
181 }
182 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
183 break;
184 }
185 case SP_ATTR_OFFSET: {
186 stop->offset = sp_svg_read_percentage(value, 0.0);
187 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
188 break;
189 }
190 default: {
191 if (((SPObjectClass *) stop_parent_class)->set)
192 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
193 break;
194 }
195 }
196 }
198 /**
199 * Virtual write: write object attributes to repr.
200 */
201 static Inkscape::XML::Node *
202 sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
203 {
204 SPStop *stop = SP_STOP(object);
206 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
207 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
208 repr = xml_doc->createElement("svg:stop");
209 }
211 if (((SPObjectClass *) stop_parent_class)->write)
212 (* ((SPObjectClass *) stop_parent_class)->write)(object, repr, flags);
214 // Since we do a hackish style setting here (because SPStyle does not support stop-color and
215 // stop-opacity), we must do it AFTER calling the parent write method; otherwise
216 // sp_object_write would clear our style= attribute (bug 1695287)
218 Inkscape::CSSOStringStream os;
219 os << "stop-color:";
220 if (stop->currentColor) {
221 os << "currentColor";
222 } else {
223 gchar c[64];
224 sp_svg_write_color(c, 64, sp_color_get_rgba32_ualpha(&stop->specified_color, 255));
225 os << c;
226 }
227 os << ";stop-opacity:" << stop->opacity;
228 repr->setAttribute("style", os.str().c_str());
229 repr->setAttribute("stop-color", NULL);
230 repr->setAttribute("stop-opacity", NULL);
231 sp_repr_set_css_double(repr, "offset", stop->offset);
232 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no sense
233 * for offset proportions. */
235 return repr;
236 }
238 /**
239 * Return stop's color as 32bit value.
240 */
241 guint32
242 sp_stop_get_rgba32(SPStop const *const stop)
243 {
244 guint32 rgb0 = 0;
245 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
246 * value depends on user agent, and don't give any further restrictions that I can
247 * see.) */
248 if (stop->currentColor) {
249 char const *str = sp_object_get_style_property(stop, "color", NULL);
250 if (str) {
251 rgb0 = sp_svg_read_color(str, rgb0);
252 }
253 unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
254 g_return_val_if_fail((alpha & ~0xff) == 0,
255 rgb0 | 0xff);
256 return rgb0 | alpha;
257 } else {
258 return sp_color_get_rgba32_falpha(&stop->specified_color, stop->opacity);
259 }
260 }
262 /**
263 * Return stop's color as SPColor.
264 */
265 static SPColor
266 sp_stop_get_color(SPStop const *const stop)
267 {
268 if (stop->currentColor) {
269 char const *str = sp_object_get_style_property(stop, "color", NULL);
270 guint32 const dfl = 0;
271 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
272 * value depends on user agent, and don't give any further restrictions that I can
273 * see.) */
274 guint32 color = dfl;
275 if (str) {
276 color = sp_svg_read_color(str, dfl);
277 }
278 SPColor ret;
279 sp_color_set_rgb_rgba32(&ret, color);
280 return ret;
281 } else {
282 return stop->specified_color;
283 }
284 }
286 /*
287 * Gradient
288 */
290 static void sp_gradient_class_init(SPGradientClass *klass);
291 static void sp_gradient_init(SPGradient *gr);
293 static void sp_gradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
294 static void sp_gradient_release(SPObject *object);
295 static void sp_gradient_set(SPObject *object, unsigned key, gchar const *value);
296 static void sp_gradient_child_added(SPObject *object,
297 Inkscape::XML::Node *child,
298 Inkscape::XML::Node *ref);
299 static void sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child);
300 static void sp_gradient_modified(SPObject *object, guint flags);
301 static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr,
302 guint flags);
304 static void gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient);
306 static bool sp_gradient_invalidate_vector(SPGradient *gr);
307 static void sp_gradient_rebuild_vector(SPGradient *gr);
309 static void gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gradient);
311 SPGradientSpread sp_gradient_get_spread(SPGradient *gradient);
312 SPGradientUnits sp_gradient_get_units(SPGradient *gradient);
314 static SPPaintServerClass *gradient_parent_class;
316 /**
317 * Registers SPGradient class and returns its type.
318 */
319 GType
320 sp_gradient_get_type()
321 {
322 static GType gradient_type = 0;
323 if (!gradient_type) {
324 GTypeInfo gradient_info = {
325 sizeof(SPGradientClass),
326 NULL, NULL,
327 (GClassInitFunc) sp_gradient_class_init,
328 NULL, NULL,
329 sizeof(SPGradient),
330 16,
331 (GInstanceInitFunc) sp_gradient_init,
332 NULL, /* value_table */
333 };
334 gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
335 &gradient_info, (GTypeFlags)0);
336 }
337 return gradient_type;
338 }
340 /**
341 * SPGradient vtable initialization.
342 */
343 static void
344 sp_gradient_class_init(SPGradientClass *klass)
345 {
346 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
348 gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
350 sp_object_class->build = sp_gradient_build;
351 sp_object_class->release = sp_gradient_release;
352 sp_object_class->set = sp_gradient_set;
353 sp_object_class->child_added = sp_gradient_child_added;
354 sp_object_class->remove_child = sp_gradient_remove_child;
355 sp_object_class->modified = sp_gradient_modified;
356 sp_object_class->write = sp_gradient_write;
357 }
359 /**
360 * Callback for SPGradient object initialization.
361 */
362 static void
363 sp_gradient_init(SPGradient *gr)
364 {
365 gr->ref = new SPGradientReference(SP_OBJECT(gr));
366 gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(gradient_ref_changed), gr));
368 /** \todo
369 * Fixme: reprs being rearranged (e.g. via the XML editor)
370 * may require us to clear the state.
371 */
372 gr->state = SP_GRADIENT_STATE_UNKNOWN;
374 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
375 gr->units_set = FALSE;
377 gr->gradientTransform = NR::identity();
378 gr->gradientTransform_set = FALSE;
380 gr->spread = SP_GRADIENT_SPREAD_PAD;
381 gr->spread_set = FALSE;
383 gr->has_stops = FALSE;
385 gr->vector.built = false;
386 gr->vector.stops.clear();
388 gr->color = NULL;
390 new (&gr->modified_connection) sigc::connection();
391 }
393 /**
394 * Virtual build: set gradient attributes from its associated repr.
395 */
396 static void
397 sp_gradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
398 {
399 SPGradient *gradient = SP_GRADIENT(object);
401 if (((SPObjectClass *) gradient_parent_class)->build)
402 (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
404 SPObject *ochild;
405 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
406 if (SP_IS_STOP(ochild)) {
407 gradient->has_stops = TRUE;
408 break;
409 }
410 }
412 sp_object_read_attr(object, "gradientUnits");
413 sp_object_read_attr(object, "gradientTransform");
414 sp_object_read_attr(object, "spreadMethod");
415 sp_object_read_attr(object, "xlink:href");
417 /* Register ourselves */
418 sp_document_add_resource(document, "gradient", object);
419 }
421 /**
422 * Virtual release of SPGradient members before destruction.
423 */
424 static void
425 sp_gradient_release(SPObject *object)
426 {
427 SPGradient *gradient = (SPGradient *) object;
429 #ifdef SP_GRADIENT_VERBOSE
430 g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
431 #endif
433 if (SP_OBJECT_DOCUMENT(object)) {
434 /* Unregister ourselves */
435 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
436 }
438 if (gradient->ref) {
439 gradient->modified_connection.disconnect();
440 gradient->ref->detach();
441 delete gradient->ref;
442 gradient->ref = NULL;
443 }
445 if (gradient->color) {
446 g_free(gradient->color);
447 gradient->color = NULL;
448 }
450 gradient->modified_connection.~connection();
452 if (((SPObjectClass *) gradient_parent_class)->release)
453 ((SPObjectClass *) gradient_parent_class)->release(object);
454 }
456 /**
457 * Set gradient attribute to value.
458 */
459 static void
460 sp_gradient_set(SPObject *object, unsigned key, gchar const *value)
461 {
462 SPGradient *gr = SP_GRADIENT(object);
464 switch (key) {
465 case SP_ATTR_GRADIENTUNITS:
466 if (value) {
467 if (!strcmp(value, "userSpaceOnUse")) {
468 gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
469 } else {
470 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
471 }
472 gr->units_set = TRUE;
473 } else {
474 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
475 gr->units_set = FALSE;
476 }
477 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
478 break;
479 case SP_ATTR_GRADIENTTRANSFORM: {
480 NR::Matrix t;
481 if (value && sp_svg_transform_read(value, &t)) {
482 gr->gradientTransform = t;
483 gr->gradientTransform_set = TRUE;
484 } else {
485 gr->gradientTransform = NR::identity();
486 gr->gradientTransform_set = FALSE;
487 }
488 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
489 break;
490 }
491 case SP_ATTR_SPREADMETHOD:
492 if (value) {
493 if (!strcmp(value, "reflect")) {
494 gr->spread = SP_GRADIENT_SPREAD_REFLECT;
495 } else if (!strcmp(value, "repeat")) {
496 gr->spread = SP_GRADIENT_SPREAD_REPEAT;
497 } else {
498 gr->spread = SP_GRADIENT_SPREAD_PAD;
499 }
500 gr->spread_set = TRUE;
501 } else {
502 gr->spread_set = FALSE;
503 }
504 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
505 break;
506 case SP_ATTR_XLINK_HREF:
507 if (value) {
508 try {
509 gr->ref->attach(Inkscape::URI(value));
510 } catch (Inkscape::BadURIException &e) {
511 g_warning("%s", e.what());
512 gr->ref->detach();
513 }
514 } else {
515 gr->ref->detach();
516 }
517 break;
518 default:
519 if (((SPObjectClass *) gradient_parent_class)->set)
520 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
521 break;
522 }
523 }
525 /**
526 * Gets called when the gradient is (re)attached to another gradient.
527 */
528 static void
529 gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gr)
530 {
531 if (old_ref) {
532 gr->modified_connection.disconnect();
533 }
534 if ( SP_IS_GRADIENT(ref)
535 && ref != gr )
536 {
537 gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&gradient_ref_modified), gr));
538 }
540 // Per SVG, all unset attributes must be inherited from linked gradient.
541 // So, as we're now (re)linked, we assign linkee's values to this gradient if they are not yet set -
542 // but without setting the _set flags.
543 // FIXME: do the same for gradientTransform too
544 if (!gr->units_set)
545 gr->units = sp_gradient_get_units (gr);
546 if (!gr->spread_set)
547 gr->spread = sp_gradient_get_spread (gr);
549 /// \todo Fixme: what should the flags (second) argument be? */
550 gradient_ref_modified(ref, 0, gr);
551 }
553 /**
554 * Callback for child_added event.
555 */
556 static void
557 sp_gradient_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
558 {
559 SPGradient *gr = SP_GRADIENT(object);
561 sp_gradient_invalidate_vector(gr);
563 if (((SPObjectClass *) gradient_parent_class)->child_added)
564 (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
566 SPObject *ochild = sp_object_get_child_by_repr(object, child);
567 if ( ochild && SP_IS_STOP(ochild) ) {
568 gr->has_stops = TRUE;
569 }
571 /// \todo Fixme: should we schedule "modified" here?
572 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
573 }
575 /**
576 * Callback for remove_child event.
577 */
578 static void
579 sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child)
580 {
581 SPGradient *gr = SP_GRADIENT(object);
583 sp_gradient_invalidate_vector(gr);
585 if (((SPObjectClass *) gradient_parent_class)->remove_child)
586 (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
588 gr->has_stops = FALSE;
589 SPObject *ochild;
590 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
591 if (SP_IS_STOP(ochild)) {
592 gr->has_stops = TRUE;
593 break;
594 }
595 }
597 /* Fixme: should we schedule "modified" here? */
598 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
599 }
601 /**
602 * Callback for modified event.
603 */
604 static void
605 sp_gradient_modified(SPObject *object, guint flags)
606 {
607 SPGradient *gr = SP_GRADIENT(object);
609 if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
610 sp_gradient_invalidate_vector(gr);
611 }
613 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
614 sp_gradient_ensure_colors(gr);
615 }
617 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
618 flags &= SP_OBJECT_MODIFIED_CASCADE;
620 // FIXME: climb up the ladder of hrefs
621 GSList *l = NULL;
622 for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
623 g_object_ref(G_OBJECT(child));
624 l = g_slist_prepend(l, child);
625 }
626 l = g_slist_reverse(l);
627 while (l) {
628 SPObject *child = SP_OBJECT(l->data);
629 l = g_slist_remove(l, child);
630 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
631 child->emitModified(flags);
632 }
633 g_object_unref(G_OBJECT(child));
634 }
635 }
637 /**
638 * Write gradient attributes to repr.
639 */
640 static Inkscape::XML::Node *
641 sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
642 {
643 SPGradient *gr = SP_GRADIENT(object);
645 if (((SPObjectClass *) gradient_parent_class)->write)
646 (* ((SPObjectClass *) gradient_parent_class)->write)(object, repr, flags);
648 if (flags & SP_OBJECT_WRITE_BUILD) {
649 GSList *l = NULL;
650 for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
651 Inkscape::XML::Node *crepr;
652 crepr = child->updateRepr(NULL, flags);
653 if (crepr) l = g_slist_prepend(l, crepr);
654 }
655 while (l) {
656 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
657 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
658 l = g_slist_remove(l, l->data);
659 }
660 }
662 if (gr->ref->getURI()) {
663 gchar *uri_string = gr->ref->getURI()->toString();
664 repr->setAttribute("xlink:href", uri_string);
665 g_free(uri_string);
666 }
668 if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
669 switch (gr->units) {
670 case SP_GRADIENT_UNITS_USERSPACEONUSE:
671 repr->setAttribute("gradientUnits", "userSpaceOnUse");
672 break;
673 default:
674 repr->setAttribute("gradientUnits", "objectBoundingBox");
675 break;
676 }
677 }
679 if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
680 gchar *c=sp_svg_transform_write(gr->gradientTransform);
681 repr->setAttribute("gradientTransform", c);
682 g_free(c);
683 }
685 if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
686 /* FIXME: Ensure that gr->spread is the inherited value
687 * if !gr->spread_set. Not currently happening: see sp_gradient_modified.
688 */
689 switch (gr->spread) {
690 case SP_GRADIENT_SPREAD_REFLECT:
691 repr->setAttribute("spreadMethod", "reflect");
692 break;
693 case SP_GRADIENT_SPREAD_REPEAT:
694 repr->setAttribute("spreadMethod", "repeat");
695 break;
696 default:
697 repr->setAttribute("spreadMethod", "pad");
698 break;
699 }
700 }
702 return repr;
703 }
705 /**
706 * Forces the vector to be built, if not present (i.e., changed).
707 *
708 * \pre SP_IS_GRADIENT(gradient).
709 */
710 void
711 sp_gradient_ensure_vector(SPGradient *gradient)
712 {
713 g_return_if_fail(gradient != NULL);
714 g_return_if_fail(SP_IS_GRADIENT(gradient));
716 if (!gradient->vector.built) {
717 sp_gradient_rebuild_vector(gradient);
718 }
719 }
721 /**
722 * Set units property of gradient and emit modified.
723 */
724 void
725 sp_gradient_set_units(SPGradient *gr, SPGradientUnits units)
726 {
727 if (units != gr->units) {
728 gr->units = units;
729 gr->units_set = TRUE;
730 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
731 }
732 }
734 /**
735 * Set spread property of gradient and emit modified.
736 */
737 void
738 sp_gradient_set_spread(SPGradient *gr, SPGradientSpread spread)
739 {
740 if (spread != gr->spread) {
741 gr->spread = spread;
742 gr->spread_set = TRUE;
743 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
744 }
745 }
747 /**
748 * Returns the first of {src, src-\>ref-\>getObject(),
749 * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
750 * for which \a match is true, or NULL if none found.
751 *
752 * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
753 * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
754 *
755 * \pre SP_IS_GRADIENT(src).
756 */
757 static SPGradient *
758 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
759 {
760 g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
762 /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2. If there is a
763 loop, then once p1 has entered the loop, we'll detect it the next time the distance between
764 p1 and p2 is a multiple of the loop size. */
765 SPGradient *p1 = src, *p2 = src;
766 bool do1 = false;
767 for (;;) {
768 if (match(p2)) {
769 return p2;
770 }
772 p2 = p2->ref->getObject();
773 if (!p2) {
774 return p2;
775 }
776 if (do1) {
777 p1 = p1->ref->getObject();
778 }
779 do1 = !do1;
781 if ( p2 == p1 ) {
782 /* We've been here before, so return NULL to indicate that no matching gradient found
783 * in the chain. */
784 return NULL;
785 }
786 }
787 }
789 /**
790 * True if gradient has stops.
791 */
792 static bool
793 has_stops(SPGradient const *gr)
794 {
795 return SP_GRADIENT_HAS_STOPS(gr);
796 }
798 /**
799 * True if gradient has spread set.
800 */
801 static bool
802 has_spread_set(SPGradient const *gr)
803 {
804 return gr->spread_set;
805 }
807 /**
808 * True if gradient has units set.
809 */
810 static bool
811 has_units_set(SPGradient const *gr)
812 {
813 return gr->units_set;
814 }
817 /**
818 * Returns private vector of given gradient (the gradient at the end of the href chain which has
819 * stops), optionally normalizing it.
820 *
821 * \pre SP_IS_GRADIENT(gradient).
822 * \pre There exists a gradient in the chain that has stops.
823 */
824 SPGradient *
825 sp_gradient_get_vector(SPGradient *gradient, bool force_vector)
826 {
827 g_return_val_if_fail(gradient != NULL, NULL);
828 g_return_val_if_fail(SP_IS_GRADIENT(gradient), NULL);
830 SPGradient *const src = chase_hrefs(gradient, has_stops);
831 return ( force_vector
832 ? sp_gradient_ensure_vector_normalized(src)
833 : src );
834 }
836 /**
837 * Returns the effective spread of given gradient (climbing up the refs chain if needed).
838 *
839 * \pre SP_IS_GRADIENT(gradient).
840 */
841 SPGradientSpread
842 sp_gradient_get_spread(SPGradient *gradient)
843 {
844 g_return_val_if_fail(SP_IS_GRADIENT(gradient), SP_GRADIENT_SPREAD_PAD);
846 SPGradient const *src = chase_hrefs(gradient, has_spread_set);
847 return ( src
848 ? src->spread
849 : SP_GRADIENT_SPREAD_PAD ); // pad is the default
850 }
852 /**
853 * Returns the effective units of given gradient (climbing up the refs chain if needed).
854 *
855 * \pre SP_IS_GRADIENT(gradient).
856 */
857 SPGradientUnits
858 sp_gradient_get_units(SPGradient *gradient)
859 {
860 g_return_val_if_fail(SP_IS_GRADIENT(gradient), SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX);
862 SPGradient const *src = chase_hrefs(gradient, has_units_set);
863 return ( src
864 ? src->units
865 : SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ); // bbox is the default
866 }
869 /**
870 * Clears the gradient's svg:stop children from its repr.
871 */
872 void
873 sp_gradient_repr_clear_vector(SPGradient *gr)
874 {
875 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
877 /* Collect stops from original repr */
878 GSList *sl = NULL;
879 for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
880 if (!strcmp(child->name(), "svg:stop")) {
881 sl = g_slist_prepend(sl, child);
882 }
883 }
884 /* Remove all stops */
885 while (sl) {
886 /** \todo
887 * fixme: This should work, unless we make gradient
888 * into generic group.
889 */
890 sp_repr_unparent((Inkscape::XML::Node *)sl->data);
891 sl = g_slist_remove(sl, sl->data);
892 }
893 }
895 /**
896 * Writes the gradient's internal vector (whether from its own stops, or
897 * inherited from refs) into the gradient repr as svg:stop elements.
898 */
899 void
900 sp_gradient_repr_write_vector(SPGradient *gr)
901 {
902 g_return_if_fail(gr != NULL);
903 g_return_if_fail(SP_IS_GRADIENT(gr));
905 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
906 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
908 /* We have to be careful, as vector may be our own, so construct repr list at first */
909 GSList *cl = NULL;
911 for (guint i = 0; i < gr->vector.stops.size(); i++) {
912 Inkscape::CSSOStringStream os;
913 Inkscape::XML::Node *child = xml_doc->createElement("svg:stop");
914 sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
915 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
916 * sense for offset proportions. */
917 gchar c[64];
918 sp_svg_write_color(c, 64, sp_color_get_rgba32_ualpha(&gr->vector.stops[i].color, 0x00));
919 os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
920 child->setAttribute("style", os.str().c_str());
921 /* Order will be reversed here */
922 cl = g_slist_prepend(cl, child);
923 }
925 sp_gradient_repr_clear_vector(gr);
927 /* And insert new children from list */
928 while (cl) {
929 Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
930 repr->addChild(child, NULL);
931 Inkscape::GC::release(child);
932 cl = g_slist_remove(cl, child);
933 }
934 }
937 static void
938 gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient)
939 {
940 if (sp_gradient_invalidate_vector(gradient)) {
941 SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
942 /* Conditional to avoid causing infinite loop if there's a cycle in the href chain. */
943 }
944 }
946 /** Return true iff change made. */
947 static bool
948 sp_gradient_invalidate_vector(SPGradient *gr)
949 {
950 bool ret = false;
952 if (gr->color != NULL) {
953 g_free(gr->color);
954 gr->color = NULL;
955 ret = true;
956 }
958 if (gr->vector.built) {
959 gr->vector.built = false;
960 gr->vector.stops.clear();
961 ret = true;
962 }
964 return ret;
965 }
967 /** Creates normalized color vector */
968 static void
969 sp_gradient_rebuild_vector(SPGradient *gr)
970 {
971 gint len = 0;
972 for ( SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
973 child != NULL ;
974 child = SP_OBJECT_NEXT(child) ) {
975 if (SP_IS_STOP(child)) {
976 len ++;
977 }
978 }
980 gr->has_stops = (len != 0);
982 gr->vector.stops.clear();
984 SPGradient *ref = gr->ref->getObject();
985 if ( !gr->has_stops && ref ) {
986 /* Copy vector from referenced gradient */
987 gr->vector.built = true; // Prevent infinite recursion.
988 sp_gradient_ensure_vector(ref);
989 if (!ref->vector.stops.empty()) {
990 gr->vector.built = ref->vector.built;
991 gr->vector.stops.assign(ref->vector.stops.begin(), ref->vector.stops.end());
992 return;
993 }
994 }
996 for (SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
997 child != NULL;
998 child = SP_OBJECT_NEXT(child) ) {
999 if (SP_IS_STOP(child)) {
1000 SPStop *stop = SP_STOP(child);
1002 SPGradientStop gstop;
1003 if (gr->vector.stops.size() > 0) {
1004 // "Each gradient offset value is required to be equal to or greater than the
1005 // previous gradient stop's offset value. If a given gradient stop's offset
1006 // value is not equal to or greater than all previous offset values, then the
1007 // offset value is adjusted to be equal to the largest of all previous offset
1008 // values."
1009 gstop.offset = MAX(stop->offset, gr->vector.stops.back().offset);
1010 } else {
1011 gstop.offset = stop->offset;
1012 }
1014 // "Gradient offset values less than 0 (or less than 0%) are rounded up to
1015 // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
1016 // down to 100%."
1017 gstop.offset = CLAMP(gstop.offset, 0, 1);
1019 gstop.color = sp_stop_get_color(stop);
1020 gstop.opacity = stop->opacity;
1022 gr->vector.stops.push_back(gstop);
1023 }
1024 }
1026 // Normalize per section 13.2.4 of SVG 1.1.
1027 if (gr->vector.stops.size() == 0) {
1028 /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
1029 * paint style."
1030 */
1031 {
1032 SPGradientStop gstop;
1033 gstop.offset = 0.0;
1034 sp_color_set_rgb_rgba32(&gstop.color, 0x00000000);
1035 gstop.opacity = 0.0;
1036 gr->vector.stops.push_back(gstop);
1037 }
1038 {
1039 SPGradientStop gstop;
1040 gstop.offset = 1.0;
1041 sp_color_set_rgb_rgba32(&gstop.color, 0x00000000);
1042 gstop.opacity = 0.0;
1043 gr->vector.stops.push_back(gstop);
1044 }
1045 } else {
1046 /* "If one stop is defined, then paint with the solid color fill using the color defined
1047 * for that gradient stop."
1048 */
1049 if (gr->vector.stops.front().offset > 0.0) {
1050 // If the first one is not at 0, then insert a copy of the first at 0.
1051 SPGradientStop gstop;
1052 gstop.offset = 0.0;
1053 sp_color_copy(&gstop.color, &gr->vector.stops.front().color);
1054 gstop.opacity = gr->vector.stops.front().opacity;
1055 gr->vector.stops.insert(gr->vector.stops.begin(), gstop);
1056 }
1057 if (gr->vector.stops.back().offset < 1.0) {
1058 // If the last one is not at 1, then insert a copy of the last at 1.
1059 SPGradientStop gstop;
1060 gstop.offset = 1.0;
1061 sp_color_copy(&gstop.color, &gr->vector.stops.back().color);
1062 gstop.opacity = gr->vector.stops.back().opacity;
1063 gr->vector.stops.push_back(gstop);
1064 }
1065 }
1067 gr->vector.built = true;
1068 }
1070 /**
1071 * The gradient's color array is newly created and set up from vector.
1072 */
1073 void
1074 sp_gradient_ensure_colors(SPGradient *gr)
1075 {
1076 if (!gr->vector.built) {
1077 sp_gradient_rebuild_vector(gr);
1078 }
1079 g_return_if_fail(!gr->vector.stops.empty());
1081 /// \todo Where is the memory freed?
1082 if (!gr->color) {
1083 gr->color = g_new(guchar, 4 * NCOLORS);
1084 }
1086 for (guint i = 0; i < gr->vector.stops.size() - 1; i++) {
1087 guint32 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i].color,
1088 gr->vector.stops[i].opacity);
1089 gint r0 = (color >> 24) & 0xff;
1090 gint g0 = (color >> 16) & 0xff;
1091 gint b0 = (color >> 8) & 0xff;
1092 gint a0 = color & 0xff;
1093 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i + 1].color,
1094 gr->vector.stops[i + 1].opacity);
1095 gint r1 = (color >> 24) & 0xff;
1096 gint g1 = (color >> 16) & 0xff;
1097 gint b1 = (color >> 8) & 0xff;
1098 gint a1 = color & 0xff;
1099 gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001));
1100 gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001));
1101 if (o1 > o0) {
1102 gint dr = ((r1 - r0) << 16) / (o1 - o0);
1103 gint dg = ((g1 - g0) << 16) / (o1 - o0);
1104 gint db = ((b1 - b0) << 16) / (o1 - o0);
1105 gint da = ((a1 - a0) << 16) / (o1 - o0);
1106 gint r = r0 << 16;
1107 gint g = g0 << 16;
1108 gint b = b0 << 16;
1109 gint a = a0 << 16;
1110 for (int j = o0; j < o1 + 1; j++) {
1111 gr->color[4 * j] = r >> 16;
1112 gr->color[4 * j + 1] = g >> 16;
1113 gr->color[4 * j + 2] = b >> 16;
1114 gr->color[4 * j + 3] = a >> 16;
1115 r += dr;
1116 g += dg;
1117 b += db;
1118 a += da;
1119 }
1120 }
1121 }
1122 }
1124 /**
1125 * Renders gradient vector to buffer as line.
1126 *
1127 * RGB buffer background should be set up beforehand.
1128 *
1129 * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1130 * @param span Full integer width of requested gradient.
1131 * @param pos Buffer starting position in span.
1132 */
1133 static void
1134 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1135 gint const len, gint const pos, gint const span)
1136 {
1137 g_return_if_fail(gradient != NULL);
1138 g_return_if_fail(SP_IS_GRADIENT(gradient));
1139 g_return_if_fail(buf != NULL);
1140 g_return_if_fail(len > 0);
1141 g_return_if_fail(pos >= 0);
1142 g_return_if_fail(pos + len <= span);
1143 g_return_if_fail(span > 0);
1145 if (!gradient->color) {
1146 sp_gradient_ensure_colors(gradient);
1147 }
1149 gint idx = (pos * 1024 << 8) / span;
1150 gint didx = (1024 << 8) / span;
1152 for (gint x = 0; x < len; x++) {
1153 /// \todo Can this be done with 4 byte copies?
1154 *buf++ = gradient->color[4 * (idx >> 8)];
1155 *buf++ = gradient->color[4 * (idx >> 8) + 1];
1156 *buf++ = gradient->color[4 * (idx >> 8) + 2];
1157 *buf++ = gradient->color[4 * (idx >> 8) + 3];
1158 idx += didx;
1159 }
1160 }
1162 /**
1163 * Render rectangular RGBA area from gradient vector.
1164 */
1165 void
1166 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1167 gint const width, gint const height, gint const rowstride,
1168 gint const pos, gint const span, bool const horizontal)
1169 {
1170 g_return_if_fail(gradient != NULL);
1171 g_return_if_fail(SP_IS_GRADIENT(gradient));
1172 g_return_if_fail(buf != NULL);
1173 g_return_if_fail(width > 0);
1174 g_return_if_fail(height > 0);
1175 g_return_if_fail(pos >= 0);
1176 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1177 g_return_if_fail(span > 0);
1179 if (horizontal) {
1180 sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1181 for (gint y = 1; y < height; y++) {
1182 memcpy(buf + y * rowstride, buf, 4 * width);
1183 }
1184 } else {
1185 guchar *tmp = (guchar *)alloca(4 * height);
1186 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1187 for (gint y = 0; y < height; y++) {
1188 guchar *b = buf + y * rowstride;
1189 for (gint x = 0; x < width; x++) {
1190 *b++ = tmp[0];
1191 *b++ = tmp[1];
1192 *b++ = tmp[2];
1193 *b++ = tmp[3];
1194 }
1195 tmp += 4;
1196 }
1197 }
1198 }
1200 /**
1201 * Render rectangular RGB area from gradient vector.
1202 */
1203 void
1204 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1205 gint const width, gint const height, gint const rowstride,
1206 gint const pos, gint const span, bool const horizontal)
1207 {
1208 g_return_if_fail(gradient != NULL);
1209 g_return_if_fail(SP_IS_GRADIENT(gradient));
1210 g_return_if_fail(buf != NULL);
1211 g_return_if_fail(width > 0);
1212 g_return_if_fail(height > 0);
1213 g_return_if_fail(pos >= 0);
1214 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1215 g_return_if_fail(span > 0);
1217 if (horizontal) {
1218 guchar *tmp = (guchar*)alloca(4 * width);
1219 sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1220 for (gint y = 0; y < height; y++) {
1221 guchar *t = tmp;
1222 for (gint x = 0; x < width; x++) {
1223 gint a = t[3];
1224 gint fc = (t[0] - buf[0]) * a;
1225 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1226 fc = (t[1] - buf[1]) * a;
1227 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1228 fc = (t[2] - buf[2]) * a;
1229 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1230 buf += 3;
1231 t += 4;
1232 }
1233 }
1234 } else {
1235 guchar *tmp = (guchar*)alloca(4 * height);
1236 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1237 for (gint y = 0; y < height; y++) {
1238 guchar *t = tmp + 4 * y;
1239 for (gint x = 0; x < width; x++) {
1240 gint a = t[3];
1241 gint fc = (t[0] - buf[0]) * a;
1242 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1243 fc = (t[1] - buf[1]) * a;
1244 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1245 fc = (t[2] - buf[2]) * a;
1246 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1247 }
1248 }
1249 }
1250 }
1252 NR::Matrix
1253 sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1254 {
1255 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1256 return ( NR::scale(bbox.dimensions())
1257 * NR::translate(bbox.min())
1258 * ctm );
1259 } else {
1260 return ctm;
1261 }
1262 }
1264 NR::Matrix
1265 sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1266 {
1267 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1268 return ( gr->gradientTransform
1269 * NR::scale(bbox.dimensions())
1270 * NR::translate(bbox.min())
1271 * ctm );
1272 } else {
1273 return gr->gradientTransform * ctm;
1274 }
1275 }
1277 void
1278 sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
1279 NR::Rect const &bbox, NR::Matrix const &gs2d)
1280 {
1281 gr->gradientTransform = gs2d / ctm;
1282 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1283 gr->gradientTransform = ( gr->gradientTransform
1284 / NR::translate(bbox.min())
1285 / NR::scale(bbox.dimensions()) );
1286 }
1287 gr->gradientTransform_set = TRUE;
1289 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1290 }
1292 /*
1293 * Linear Gradient
1294 */
1296 class SPLGPainter;
1298 /// A context with linear gradient, painter, and gradient renderer.
1299 struct SPLGPainter {
1300 SPPainter painter;
1301 SPLinearGradient *lg;
1303 NRLGradientRenderer lgr;
1304 };
1306 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1307 static void sp_lineargradient_init(SPLinearGradient *lg);
1309 static void sp_lineargradient_build(SPObject *object,
1310 SPDocument *document,
1311 Inkscape::XML::Node *repr);
1312 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1313 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr,
1314 guint flags);
1316 static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
1317 NR::Matrix const &full_transform,
1318 NR::Matrix const &parent_transform,
1319 NRRect const *bbox);
1320 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1322 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1324 static SPGradientClass *lg_parent_class;
1326 /**
1327 * Register SPLinearGradient class and return its type.
1328 */
1329 GType
1330 sp_lineargradient_get_type()
1331 {
1332 static GType type = 0;
1333 if (!type) {
1334 GTypeInfo info = {
1335 sizeof(SPLinearGradientClass),
1336 NULL, NULL,
1337 (GClassInitFunc) sp_lineargradient_class_init,
1338 NULL, NULL,
1339 sizeof(SPLinearGradient),
1340 16,
1341 (GInstanceInitFunc) sp_lineargradient_init,
1342 NULL, /* value_table */
1343 };
1344 type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1345 }
1346 return type;
1347 }
1349 /**
1350 * SPLinearGradient vtable initialization.
1351 */
1352 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1353 {
1354 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1355 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1357 lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1359 sp_object_class->build = sp_lineargradient_build;
1360 sp_object_class->set = sp_lineargradient_set;
1361 sp_object_class->write = sp_lineargradient_write;
1363 ps_class->painter_new = sp_lineargradient_painter_new;
1364 ps_class->painter_free = sp_lineargradient_painter_free;
1365 }
1367 /**
1368 * Callback for SPLinearGradient object initialization.
1369 */
1370 static void sp_lineargradient_init(SPLinearGradient *lg)
1371 {
1372 lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1373 lg->y1.unset(SVGLength::PERCENT, 0.5, 0.5);
1374 lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1375 lg->y2.unset(SVGLength::PERCENT, 0.5, 0.5);
1376 }
1378 /**
1379 * Callback: set attributes from associated repr.
1380 */
1381 static void sp_lineargradient_build(SPObject *object,
1382 SPDocument *document,
1383 Inkscape::XML::Node *repr)
1384 {
1385 if (((SPObjectClass *) lg_parent_class)->build)
1386 (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1388 sp_object_read_attr(object, "x1");
1389 sp_object_read_attr(object, "y1");
1390 sp_object_read_attr(object, "x2");
1391 sp_object_read_attr(object, "y2");
1392 }
1394 /**
1395 * Callback: set attribute.
1396 */
1397 static void
1398 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1399 {
1400 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1402 switch (key) {
1403 case SP_ATTR_X1:
1404 lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1405 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1406 break;
1407 case SP_ATTR_Y1:
1408 lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1409 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1410 break;
1411 case SP_ATTR_X2:
1412 lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1413 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1414 break;
1415 case SP_ATTR_Y2:
1416 lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1417 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1418 break;
1419 default:
1420 if (((SPObjectClass *) lg_parent_class)->set)
1421 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1422 break;
1423 }
1424 }
1426 /**
1427 * Callback: write attributes to associated repr.
1428 */
1429 static Inkscape::XML::Node *
1430 sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1431 {
1432 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1434 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1435 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
1436 repr = xml_doc->createElement("svg:linearGradient");
1437 }
1439 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1440 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1441 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1442 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1443 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1444 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1445 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1446 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1448 if (((SPObjectClass *) lg_parent_class)->write)
1449 (* ((SPObjectClass *) lg_parent_class)->write)(object, repr, flags);
1451 return repr;
1452 }
1454 /**
1455 * Create linear gradient context.
1456 *
1457 * Basically we have to deal with transformations
1458 *
1459 * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1460 * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1461 * 2) gradientTransform
1462 * 3) bbox2user
1463 * 4) ctm == userspace to pixel grid
1464 *
1465 * See also (*) in sp-pattern about why we may need parent_transform.
1466 *
1467 * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1468 * and end < 1.
1469 */
1470 static SPPainter *
1471 sp_lineargradient_painter_new(SPPaintServer *ps,
1472 NR::Matrix const &full_transform,
1473 NR::Matrix const &parent_transform,
1474 NRRect const *bbox)
1475 {
1476 SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1477 SPGradient *gr = SP_GRADIENT(ps);
1479 if (!gr->color) sp_gradient_ensure_colors(gr);
1481 SPLGPainter *lgp = g_new(SPLGPainter, 1);
1483 lgp->painter.type = SP_PAINTER_IND;
1484 lgp->painter.fill = sp_lg_fill;
1486 lgp->lg = lg;
1488 /** \todo
1489 * Technically speaking, we map NCOLORS on line [start,end] onto line
1490 * [0,1]. I almost think we should fill color array start and end in
1491 * that case. The alternative would be to leave these just empty garbage
1492 * or something similar. Originally I had 1023.9999 here - not sure
1493 * whether we have really to cut out ceil int (Lauris).
1494 */
1495 NR::Matrix color2norm(NR::identity());
1496 NR::Matrix color2px;
1497 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1498 NR::Matrix norm2pos(NR::identity());
1500 /* BBox to user coordinate system */
1501 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1503 NR::Matrix color2pos = color2norm * norm2pos;
1504 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1505 NR::Matrix color2user = color2tpos * bbox2user;
1506 color2px = color2user * full_transform;
1508 } else {
1509 /* Problem: What to do, if we have mixed lengths and percentages? */
1510 /* Currently we do ignore percentages at all, but that is not good (lauris) */
1512 NR::Matrix norm2pos(NR::identity());
1513 NR::Matrix color2pos = color2norm * norm2pos;
1514 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1515 color2px = color2tpos * full_transform;
1517 }
1519 NRMatrix v2px;
1520 color2px.copyto(&v2px);
1522 nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
1523 lg->x1.computed, lg->y1.computed,
1524 lg->x2.computed, lg->y2.computed);
1526 return (SPPainter *) lgp;
1527 }
1529 static void
1530 sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter)
1531 {
1532 g_free(painter);
1533 }
1535 /**
1536 * Directly set properties of linear gradient and request modified.
1537 */
1538 void
1539 sp_lineargradient_set_position(SPLinearGradient *lg,
1540 gdouble x1, gdouble y1,
1541 gdouble x2, gdouble y2)
1542 {
1543 g_return_if_fail(lg != NULL);
1544 g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1546 /* fixme: units? (Lauris) */
1547 lg->x1.set(SVGLength::NONE, x1, x1);
1548 lg->y1.set(SVGLength::NONE, y1, y1);
1549 lg->x2.set(SVGLength::NONE, x2, x2);
1550 lg->y2.set(SVGLength::NONE, y2, y2);
1552 SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1553 }
1555 /**
1556 * Callback when linear gradient object is rendered.
1557 */
1558 static void
1559 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1560 {
1561 SPLGPainter *lgp = (SPLGPainter *) painter;
1563 if (lgp->lg->color == NULL) {
1564 sp_gradient_ensure_colors (lgp->lg);
1565 lgp->lgr.vector = lgp->lg->color;
1566 }
1568 nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1569 }
1571 /*
1572 * Radial Gradient
1573 */
1575 class SPRGPainter;
1577 /// A context with radial gradient, painter, and gradient renderer.
1578 struct SPRGPainter {
1579 SPPainter painter;
1580 SPRadialGradient *rg;
1581 NRRGradientRenderer rgr;
1582 };
1584 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1585 static void sp_radialgradient_init(SPRadialGradient *rg);
1587 static void sp_radialgradient_build(SPObject *object,
1588 SPDocument *document,
1589 Inkscape::XML::Node *repr);
1590 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1591 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr,
1592 guint flags);
1594 static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
1595 NR::Matrix const &full_transform,
1596 NR::Matrix const &parent_transform,
1597 NRRect const *bbox);
1598 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1600 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1602 static SPGradientClass *rg_parent_class;
1604 /**
1605 * Register SPRadialGradient class and return its type.
1606 */
1607 GType
1608 sp_radialgradient_get_type()
1609 {
1610 static GType type = 0;
1611 if (!type) {
1612 GTypeInfo info = {
1613 sizeof(SPRadialGradientClass),
1614 NULL, NULL,
1615 (GClassInitFunc) sp_radialgradient_class_init,
1616 NULL, NULL,
1617 sizeof(SPRadialGradient),
1618 16,
1619 (GInstanceInitFunc) sp_radialgradient_init,
1620 NULL, /* value_table */
1621 };
1622 type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1623 }
1624 return type;
1625 }
1627 /**
1628 * SPRadialGradient vtable initialization.
1629 */
1630 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1631 {
1632 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1633 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1635 rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1637 sp_object_class->build = sp_radialgradient_build;
1638 sp_object_class->set = sp_radialgradient_set;
1639 sp_object_class->write = sp_radialgradient_write;
1641 ps_class->painter_new = sp_radialgradient_painter_new;
1642 ps_class->painter_free = sp_radialgradient_painter_free;
1643 }
1645 /**
1646 * Callback for SPRadialGradient object initialization.
1647 */
1648 static void
1649 sp_radialgradient_init(SPRadialGradient *rg)
1650 {
1651 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1652 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1653 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1654 rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1655 rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1656 }
1658 /**
1659 * Set radial gradient attributes from associated repr.
1660 */
1661 static void
1662 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1663 {
1664 if (((SPObjectClass *) rg_parent_class)->build)
1665 (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1667 sp_object_read_attr(object, "cx");
1668 sp_object_read_attr(object, "cy");
1669 sp_object_read_attr(object, "r");
1670 sp_object_read_attr(object, "fx");
1671 sp_object_read_attr(object, "fy");
1672 }
1674 /**
1675 * Set radial gradient attribute.
1676 */
1677 static void
1678 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1679 {
1680 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1682 switch (key) {
1683 case SP_ATTR_CX:
1684 if (!rg->cx.read(value)) {
1685 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1686 }
1687 if (!rg->fx._set) {
1688 rg->fx.value = rg->cx.value;
1689 rg->fx.computed = rg->cx.computed;
1690 }
1691 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1692 break;
1693 case SP_ATTR_CY:
1694 if (!rg->cy.read(value)) {
1695 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1696 }
1697 if (!rg->fy._set) {
1698 rg->fy.value = rg->cy.value;
1699 rg->fy.computed = rg->cy.computed;
1700 }
1701 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1702 break;
1703 case SP_ATTR_R:
1704 if (!rg->r.read(value)) {
1705 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1706 }
1707 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1708 break;
1709 case SP_ATTR_FX:
1710 if (!rg->fx.read(value)) {
1711 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1712 }
1713 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1714 break;
1715 case SP_ATTR_FY:
1716 if (!rg->fy.read(value)) {
1717 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1718 }
1719 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1720 break;
1721 default:
1722 if (((SPObjectClass *) rg_parent_class)->set)
1723 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1724 break;
1725 }
1726 }
1728 /**
1729 * Write radial gradient attributes to associated repr.
1730 */
1731 static Inkscape::XML::Node *
1732 sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1733 {
1734 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1736 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1737 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
1738 repr = xml_doc->createElement("svg:radialGradient");
1739 }
1741 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1742 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1743 if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1744 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1745 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1747 if (((SPObjectClass *) rg_parent_class)->write)
1748 (* ((SPObjectClass *) rg_parent_class)->write)(object, repr, flags);
1750 return repr;
1751 }
1753 /**
1754 * Create radial gradient context.
1755 */
1756 static SPPainter *
1757 sp_radialgradient_painter_new(SPPaintServer *ps,
1758 NR::Matrix const &full_transform,
1759 NR::Matrix const &parent_transform,
1760 NRRect const *bbox)
1761 {
1762 SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1763 SPGradient *gr = SP_GRADIENT(ps);
1765 if (!gr->color) sp_gradient_ensure_colors(gr);
1767 SPRGPainter *rgp = g_new(SPRGPainter, 1);
1769 rgp->painter.type = SP_PAINTER_IND;
1770 rgp->painter.fill = sp_rg_fill;
1772 rgp->rg = rg;
1774 NR::Matrix gs2px;
1776 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1777 /** \todo
1778 * fixme: We may try to normalize here too, look at
1779 * linearGradient (Lauris)
1780 */
1782 /* BBox to user coordinate system */
1783 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1785 NR::Matrix gs2user = gr->gradientTransform * bbox2user;
1787 gs2px = gs2user * full_transform;
1788 } else {
1789 /** \todo
1790 * Problem: What to do, if we have mixed lengths and percentages?
1791 * Currently we do ignore percentages at all, but that is not
1792 * good (lauris)
1793 */
1795 gs2px = gr->gradientTransform * full_transform;
1796 }
1798 NRMatrix gs2px_nr;
1799 gs2px.copyto(&gs2px_nr);
1801 nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
1802 &gs2px_nr,
1803 rg->cx.computed, rg->cy.computed,
1804 rg->fx.computed, rg->fy.computed,
1805 rg->r.computed);
1807 return (SPPainter *) rgp;
1808 }
1810 static void
1811 sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter)
1812 {
1813 g_free(painter);
1814 }
1816 /**
1817 * Directly set properties of radial gradient and request modified.
1818 */
1819 void
1820 sp_radialgradient_set_position(SPRadialGradient *rg,
1821 gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1822 {
1823 g_return_if_fail(rg != NULL);
1824 g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1826 /* fixme: units? (Lauris) */
1827 rg->cx.set(SVGLength::NONE, cx, cx);
1828 rg->cy.set(SVGLength::NONE, cy, cy);
1829 rg->fx.set(SVGLength::NONE, fx, fx);
1830 rg->fy.set(SVGLength::NONE, fy, fy);
1831 rg->r.set(SVGLength::NONE, r, r);
1833 SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1834 }
1836 /**
1837 * Callback when radial gradient object is rendered.
1838 */
1839 static void
1840 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1841 {
1842 SPRGPainter *rgp = (SPRGPainter *) painter;
1844 if (rgp->rg->color == NULL) {
1845 sp_gradient_ensure_colors (rgp->rg);
1846 rgp->rgr.vector = rgp->rg->color;
1847 }
1849 nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1850 }
1852 /*
1853 Local Variables:
1854 mode:c++
1855 c-file-style:"stroustrup"
1856 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1857 indent-tabs-mode:nil
1858 fill-column:99
1859 End:
1860 */
1861 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :