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
21 #include <cstring>
22 #include <string>
24 #include <libnr/nr-matrix-div.h>
25 #include <libnr/nr-matrix-fns.h>
26 #include <libnr/nr-matrix-ops.h>
27 #include <libnr/nr-matrix-scale-ops.h>
28 #include <libnr/nr-matrix-translate-ops.h>
29 #include "libnr/nr-scale-translate-ops.h"
31 #include <sigc++/functors/ptr_fun.h>
32 #include <sigc++/adaptors/bind.h>
34 #include "libnr/nr-gradient.h"
35 #include "svg/svg.h"
36 #include "svg/svg-color.h"
37 #include "svg/css-ostringstream.h"
38 #include "attributes.h"
39 #include "document-private.h"
40 #include "gradient-chemistry.h"
41 #include "sp-gradient-reference.h"
42 #include "sp-linear-gradient.h"
43 #include "sp-radial-gradient.h"
44 #include "sp-stop.h"
45 #include "streq.h"
46 #include "uri.h"
47 #include "xml/repr.h"
49 #define SP_MACROS_SILENT
50 #include "macros.h"
52 /// Has to be power of 2
53 #define NCOLORS NR_GRADIENT_VECTOR_LENGTH
55 static void sp_stop_class_init(SPStopClass *klass);
56 static void sp_stop_init(SPStop *stop);
58 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
59 static void sp_stop_set(SPObject *object, unsigned key, gchar const *value);
60 static Inkscape::XML::Node *sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
62 static SPObjectClass *stop_parent_class;
64 /**
65 * Registers SPStop class and returns its type.
66 */
67 GType
68 sp_stop_get_type()
69 {
70 static GType type = 0;
71 if (!type) {
72 GTypeInfo info = {
73 sizeof(SPStopClass),
74 NULL, NULL,
75 (GClassInitFunc) sp_stop_class_init,
76 NULL, NULL,
77 sizeof(SPStop),
78 16,
79 (GInstanceInitFunc) sp_stop_init,
80 NULL, /* value_table */
81 };
82 type = g_type_register_static(SP_TYPE_OBJECT, "SPStop", &info, (GTypeFlags)0);
83 }
84 return type;
85 }
87 /**
88 * Callback to initialize SPStop vtable.
89 */
90 static void sp_stop_class_init(SPStopClass *klass)
91 {
92 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
94 stop_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
96 sp_object_class->build = sp_stop_build;
97 sp_object_class->set = sp_stop_set;
98 sp_object_class->write = sp_stop_write;
99 }
101 /**
102 * Callback to initialize SPStop object.
103 */
104 static void
105 sp_stop_init(SPStop *stop)
106 {
107 stop->offset = 0.0;
108 stop->currentColor = false;
109 stop->specified_color.set( 0x000000ff );
110 stop->opacity = 1.0;
111 }
113 /**
114 * Virtual build: set stop attributes from its associated XML node.
115 */
116 static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
117 {
118 if (((SPObjectClass *) stop_parent_class)->build)
119 (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
121 sp_object_read_attr(object, "offset");
122 sp_object_read_attr(object, "stop-color");
123 sp_object_read_attr(object, "stop-opacity");
124 sp_object_read_attr(object, "style");
125 }
127 /**
128 * Virtual set: set attribute to value.
129 */
130 static void
131 sp_stop_set(SPObject *object, unsigned key, gchar const *value)
132 {
133 SPStop *stop = SP_STOP(object);
135 switch (key) {
136 case SP_ATTR_STYLE: {
137 /** \todo
138 * fixme: We are reading simple values 3 times during build (Lauris).
139 * \par
140 * We need presentation attributes etc.
141 * \par
142 * remove the hackish "style reading" from here: see comments in
143 * sp_object_get_style_property about the bugs in our current
144 * approach. However, note that SPStyle doesn't currently have
145 * stop-color and stop-opacity properties.
146 */
147 {
148 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
149 if (streq(p, "currentColor")) {
150 stop->currentColor = true;
151 } else {
152 guint32 const color = sp_svg_read_color(p, 0);
153 stop->specified_color.set( color );
154 }
155 }
156 {
157 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
158 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
159 stop->opacity = opacity;
160 }
161 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
162 break;
163 }
164 case SP_PROP_STOP_COLOR: {
165 {
166 gchar const *p = sp_object_get_style_property(object, "stop-color", "black");
167 if (streq(p, "currentColor")) {
168 stop->currentColor = true;
169 } else {
170 stop->currentColor = false;
171 guint32 const color = sp_svg_read_color(p, 0);
172 stop->specified_color.set( color );
173 }
174 }
175 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
176 break;
177 }
178 case SP_PROP_STOP_OPACITY: {
179 {
180 gchar const *p = sp_object_get_style_property(object, "stop-opacity", "1");
181 gdouble opacity = sp_svg_read_percentage(p, stop->opacity);
182 stop->opacity = opacity;
183 }
184 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
185 break;
186 }
187 case SP_ATTR_OFFSET: {
188 stop->offset = sp_svg_read_percentage(value, 0.0);
189 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
190 break;
191 }
192 default: {
193 if (((SPObjectClass *) stop_parent_class)->set)
194 (* ((SPObjectClass *) stop_parent_class)->set)(object, key, value);
195 break;
196 }
197 }
198 }
200 /**
201 * Virtual write: write object attributes to repr.
202 */
203 static Inkscape::XML::Node *
204 sp_stop_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
205 {
206 SPStop *stop = SP_STOP(object);
208 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
209 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
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, 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::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 = NR::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 NR::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 = NR::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::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, 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(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 for (guint i = 0; i < gr->vector.stops.size() - 1; i++) {
1091 guint32 color = gr->vector.stops[i].color.toRGBA32( gr->vector.stops[i].opacity );
1092 gint r0 = (color >> 24) & 0xff;
1093 gint g0 = (color >> 16) & 0xff;
1094 gint b0 = (color >> 8) & 0xff;
1095 gint a0 = color & 0xff;
1096 color = gr->vector.stops[i + 1].color.toRGBA32( gr->vector.stops[i + 1].opacity );
1097 gint r1 = (color >> 24) & 0xff;
1098 gint g1 = (color >> 16) & 0xff;
1099 gint b1 = (color >> 8) & 0xff;
1100 gint a1 = color & 0xff;
1101 gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001));
1102 gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001));
1103 if (o1 > o0) {
1104 gint dr = ((r1 - r0) << 16) / (o1 - o0);
1105 gint dg = ((g1 - g0) << 16) / (o1 - o0);
1106 gint db = ((b1 - b0) << 16) / (o1 - o0);
1107 gint da = ((a1 - a0) << 16) / (o1 - o0);
1108 gint r = r0 << 16;
1109 gint g = g0 << 16;
1110 gint b = b0 << 16;
1111 gint a = a0 << 16;
1112 for (int j = o0; j < o1 + 1; j++) {
1113 gr->color[4 * j] = r >> 16;
1114 gr->color[4 * j + 1] = g >> 16;
1115 gr->color[4 * j + 2] = b >> 16;
1116 gr->color[4 * j + 3] = a >> 16;
1117 r += dr;
1118 g += dg;
1119 b += db;
1120 a += da;
1121 }
1122 }
1123 }
1124 }
1126 /**
1127 * Renders gradient vector to buffer as line.
1128 *
1129 * RGB buffer background should be set up beforehand.
1130 *
1131 * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1132 * @param span Full integer width of requested gradient.
1133 * @param pos Buffer starting position in span.
1134 */
1135 static void
1136 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1137 gint const len, gint const pos, gint const span)
1138 {
1139 g_return_if_fail(gradient != NULL);
1140 g_return_if_fail(SP_IS_GRADIENT(gradient));
1141 g_return_if_fail(buf != NULL);
1142 g_return_if_fail(len > 0);
1143 g_return_if_fail(pos >= 0);
1144 g_return_if_fail(pos + len <= span);
1145 g_return_if_fail(span > 0);
1147 if (!gradient->color) {
1148 sp_gradient_ensure_colors(gradient);
1149 }
1151 gint idx = (pos * 1024 << 8) / span;
1152 gint didx = (1024 << 8) / span;
1154 for (gint x = 0; x < len; x++) {
1155 /// \todo Can this be done with 4 byte copies?
1156 *buf++ = gradient->color[4 * (idx >> 8)];
1157 *buf++ = gradient->color[4 * (idx >> 8) + 1];
1158 *buf++ = gradient->color[4 * (idx >> 8) + 2];
1159 *buf++ = gradient->color[4 * (idx >> 8) + 3];
1160 idx += didx;
1161 }
1162 }
1164 /**
1165 * Render rectangular RGBA area from gradient vector.
1166 */
1167 void
1168 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1169 gint const width, gint const height, gint const rowstride,
1170 gint const pos, gint const span, bool const horizontal)
1171 {
1172 g_return_if_fail(gradient != NULL);
1173 g_return_if_fail(SP_IS_GRADIENT(gradient));
1174 g_return_if_fail(buf != NULL);
1175 g_return_if_fail(width > 0);
1176 g_return_if_fail(height > 0);
1177 g_return_if_fail(pos >= 0);
1178 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1179 g_return_if_fail(span > 0);
1181 if (horizontal) {
1182 sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1183 for (gint y = 1; y < height; y++) {
1184 memcpy(buf + y * rowstride, buf, 4 * width);
1185 }
1186 } else {
1187 guchar *tmp = (guchar *)alloca(4 * height);
1188 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1189 for (gint y = 0; y < height; y++) {
1190 guchar *b = buf + y * rowstride;
1191 for (gint x = 0; x < width; x++) {
1192 *b++ = tmp[0];
1193 *b++ = tmp[1];
1194 *b++ = tmp[2];
1195 *b++ = tmp[3];
1196 }
1197 tmp += 4;
1198 }
1199 }
1200 }
1202 /**
1203 * Render rectangular RGB area from gradient vector.
1204 */
1205 void
1206 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1207 gint const width, gint const height, gint const /*rowstride*/,
1208 gint const pos, gint const span, bool const horizontal)
1209 {
1210 g_return_if_fail(gradient != NULL);
1211 g_return_if_fail(SP_IS_GRADIENT(gradient));
1212 g_return_if_fail(buf != NULL);
1213 g_return_if_fail(width > 0);
1214 g_return_if_fail(height > 0);
1215 g_return_if_fail(pos >= 0);
1216 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1217 g_return_if_fail(span > 0);
1219 if (horizontal) {
1220 guchar *tmp = (guchar*)alloca(4 * width);
1221 sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1222 for (gint y = 0; y < height; y++) {
1223 guchar *t = tmp;
1224 for (gint x = 0; x < width; x++) {
1225 gint a = t[3];
1226 gint fc = (t[0] - buf[0]) * a;
1227 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1228 fc = (t[1] - buf[1]) * a;
1229 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1230 fc = (t[2] - buf[2]) * a;
1231 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1232 buf += 3;
1233 t += 4;
1234 }
1235 }
1236 } else {
1237 guchar *tmp = (guchar*)alloca(4 * height);
1238 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1239 for (gint y = 0; y < height; y++) {
1240 guchar *t = tmp + 4 * y;
1241 for (gint x = 0; x < width; x++) {
1242 gint a = t[3];
1243 gint fc = (t[0] - buf[0]) * a;
1244 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1245 fc = (t[1] - buf[1]) * a;
1246 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1247 fc = (t[2] - buf[2]) * a;
1248 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1249 }
1250 }
1251 }
1252 }
1254 NR::Matrix
1255 sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1256 {
1257 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1258 return ( NR::scale(bbox.dimensions())
1259 * NR::translate(bbox.min())
1260 * ctm );
1261 } else {
1262 return ctm;
1263 }
1264 }
1266 NR::Matrix
1267 sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1268 {
1269 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1270 return ( gr->gradientTransform
1271 * NR::scale(bbox.dimensions())
1272 * NR::translate(bbox.min())
1273 * ctm );
1274 } else {
1275 return gr->gradientTransform * ctm;
1276 }
1277 }
1279 void
1280 sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
1281 NR::Rect const &bbox, NR::Matrix const &gs2d)
1282 {
1283 gr->gradientTransform = gs2d / ctm;
1284 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1285 gr->gradientTransform = ( gr->gradientTransform
1286 / NR::translate(bbox.min())
1287 / NR::scale(bbox.dimensions()) );
1288 }
1289 gr->gradientTransform_set = TRUE;
1291 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1292 }
1294 /*
1295 * Linear Gradient
1296 */
1298 class SPLGPainter;
1300 /// A context with linear gradient, painter, and gradient renderer.
1301 struct SPLGPainter {
1302 SPPainter painter;
1303 SPLinearGradient *lg;
1305 NRLGradientRenderer lgr;
1306 };
1308 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1309 static void sp_lineargradient_init(SPLinearGradient *lg);
1311 static void sp_lineargradient_build(SPObject *object,
1312 SPDocument *document,
1313 Inkscape::XML::Node *repr);
1314 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1315 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr,
1316 guint flags);
1318 static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
1319 NR::Matrix const &full_transform,
1320 NR::Matrix const &parent_transform,
1321 NRRect const *bbox);
1322 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1324 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1326 static SPGradientClass *lg_parent_class;
1328 /**
1329 * Register SPLinearGradient class and return its type.
1330 */
1331 GType
1332 sp_lineargradient_get_type()
1333 {
1334 static GType type = 0;
1335 if (!type) {
1336 GTypeInfo info = {
1337 sizeof(SPLinearGradientClass),
1338 NULL, NULL,
1339 (GClassInitFunc) sp_lineargradient_class_init,
1340 NULL, NULL,
1341 sizeof(SPLinearGradient),
1342 16,
1343 (GInstanceInitFunc) sp_lineargradient_init,
1344 NULL, /* value_table */
1345 };
1346 type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1347 }
1348 return type;
1349 }
1351 /**
1352 * SPLinearGradient vtable initialization.
1353 */
1354 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1355 {
1356 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1357 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1359 lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1361 sp_object_class->build = sp_lineargradient_build;
1362 sp_object_class->set = sp_lineargradient_set;
1363 sp_object_class->write = sp_lineargradient_write;
1365 ps_class->painter_new = sp_lineargradient_painter_new;
1366 ps_class->painter_free = sp_lineargradient_painter_free;
1367 }
1369 /**
1370 * Callback for SPLinearGradient object initialization.
1371 */
1372 static void sp_lineargradient_init(SPLinearGradient *lg)
1373 {
1374 lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1375 lg->y1.unset(SVGLength::PERCENT, 0.5, 0.5);
1376 lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1377 lg->y2.unset(SVGLength::PERCENT, 0.5, 0.5);
1378 }
1380 /**
1381 * Callback: set attributes from associated repr.
1382 */
1383 static void sp_lineargradient_build(SPObject *object,
1384 SPDocument *document,
1385 Inkscape::XML::Node *repr)
1386 {
1387 if (((SPObjectClass *) lg_parent_class)->build)
1388 (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1390 sp_object_read_attr(object, "x1");
1391 sp_object_read_attr(object, "y1");
1392 sp_object_read_attr(object, "x2");
1393 sp_object_read_attr(object, "y2");
1394 }
1396 /**
1397 * Callback: set attribute.
1398 */
1399 static void
1400 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1401 {
1402 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1404 switch (key) {
1405 case SP_ATTR_X1:
1406 lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1407 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1408 break;
1409 case SP_ATTR_Y1:
1410 lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1411 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1412 break;
1413 case SP_ATTR_X2:
1414 lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1415 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1416 break;
1417 case SP_ATTR_Y2:
1418 lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1419 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1420 break;
1421 default:
1422 if (((SPObjectClass *) lg_parent_class)->set)
1423 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1424 break;
1425 }
1426 }
1428 /**
1429 * Callback: write attributes to associated repr.
1430 */
1431 static Inkscape::XML::Node *
1432 sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1433 {
1434 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1436 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1437 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
1438 repr = xml_doc->createElement("svg:linearGradient");
1439 }
1441 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1442 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1443 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1444 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1445 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1446 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1447 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1448 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1450 if (((SPObjectClass *) lg_parent_class)->write)
1451 (* ((SPObjectClass *) lg_parent_class)->write)(object, repr, flags);
1453 return repr;
1454 }
1456 /**
1457 * Create linear gradient context.
1458 *
1459 * Basically we have to deal with transformations
1460 *
1461 * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1462 * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1463 * 2) gradientTransform
1464 * 3) bbox2user
1465 * 4) ctm == userspace to pixel grid
1466 *
1467 * See also (*) in sp-pattern about why we may need parent_transform.
1468 *
1469 * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1470 * and end < 1.
1471 */
1472 static SPPainter *
1473 sp_lineargradient_painter_new(SPPaintServer *ps,
1474 NR::Matrix const &full_transform,
1475 NR::Matrix const &/*parent_transform*/,
1476 NRRect const *bbox)
1477 {
1478 SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1479 SPGradient *gr = SP_GRADIENT(ps);
1481 if (!gr->color) sp_gradient_ensure_colors(gr);
1483 SPLGPainter *lgp = g_new(SPLGPainter, 1);
1485 lgp->painter.type = SP_PAINTER_IND;
1486 lgp->painter.fill = sp_lg_fill;
1488 lgp->lg = lg;
1490 /** \todo
1491 * Technically speaking, we map NCOLORS on line [start,end] onto line
1492 * [0,1]. I almost think we should fill color array start and end in
1493 * that case. The alternative would be to leave these just empty garbage
1494 * or something similar. Originally I had 1023.9999 here - not sure
1495 * whether we have really to cut out ceil int (Lauris).
1496 */
1497 NR::Matrix color2norm(NR::identity());
1498 NR::Matrix color2px;
1499 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1500 NR::Matrix norm2pos(NR::identity());
1502 /* BBox to user coordinate system */
1503 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1505 NR::Matrix color2pos = color2norm * norm2pos;
1506 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1507 NR::Matrix color2user = color2tpos * bbox2user;
1508 color2px = color2user * full_transform;
1510 } else {
1511 /* Problem: What to do, if we have mixed lengths and percentages? */
1512 /* Currently we do ignore percentages at all, but that is not good (lauris) */
1514 NR::Matrix norm2pos(NR::identity());
1515 NR::Matrix color2pos = color2norm * norm2pos;
1516 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1517 color2px = color2tpos * full_transform;
1519 }
1521 NRMatrix v2px;
1522 color2px.copyto(&v2px);
1524 nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
1525 lg->x1.computed, lg->y1.computed,
1526 lg->x2.computed, lg->y2.computed);
1528 return (SPPainter *) lgp;
1529 }
1531 static void
1532 sp_lineargradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1533 {
1534 g_free(painter);
1535 }
1537 /**
1538 * Directly set properties of linear gradient and request modified.
1539 */
1540 void
1541 sp_lineargradient_set_position(SPLinearGradient *lg,
1542 gdouble x1, gdouble y1,
1543 gdouble x2, gdouble y2)
1544 {
1545 g_return_if_fail(lg != NULL);
1546 g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1548 /* fixme: units? (Lauris) */
1549 lg->x1.set(SVGLength::NONE, x1, x1);
1550 lg->y1.set(SVGLength::NONE, y1, y1);
1551 lg->x2.set(SVGLength::NONE, x2, x2);
1552 lg->y2.set(SVGLength::NONE, y2, y2);
1554 SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1555 }
1557 /**
1558 * Callback when linear gradient object is rendered.
1559 */
1560 static void
1561 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1562 {
1563 SPLGPainter *lgp = (SPLGPainter *) painter;
1565 if (lgp->lg->color == NULL) {
1566 sp_gradient_ensure_colors (lgp->lg);
1567 lgp->lgr.vector = lgp->lg->color;
1568 }
1570 nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1571 }
1573 /*
1574 * Radial Gradient
1575 */
1577 class SPRGPainter;
1579 /// A context with radial gradient, painter, and gradient renderer.
1580 struct SPRGPainter {
1581 SPPainter painter;
1582 SPRadialGradient *rg;
1583 NRRGradientRenderer rgr;
1584 };
1586 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1587 static void sp_radialgradient_init(SPRadialGradient *rg);
1589 static void sp_radialgradient_build(SPObject *object,
1590 SPDocument *document,
1591 Inkscape::XML::Node *repr);
1592 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1593 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr,
1594 guint flags);
1596 static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
1597 NR::Matrix const &full_transform,
1598 NR::Matrix const &parent_transform,
1599 NRRect const *bbox);
1600 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1602 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1604 static SPGradientClass *rg_parent_class;
1606 /**
1607 * Register SPRadialGradient class and return its type.
1608 */
1609 GType
1610 sp_radialgradient_get_type()
1611 {
1612 static GType type = 0;
1613 if (!type) {
1614 GTypeInfo info = {
1615 sizeof(SPRadialGradientClass),
1616 NULL, NULL,
1617 (GClassInitFunc) sp_radialgradient_class_init,
1618 NULL, NULL,
1619 sizeof(SPRadialGradient),
1620 16,
1621 (GInstanceInitFunc) sp_radialgradient_init,
1622 NULL, /* value_table */
1623 };
1624 type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1625 }
1626 return type;
1627 }
1629 /**
1630 * SPRadialGradient vtable initialization.
1631 */
1632 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1633 {
1634 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1635 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1637 rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1639 sp_object_class->build = sp_radialgradient_build;
1640 sp_object_class->set = sp_radialgradient_set;
1641 sp_object_class->write = sp_radialgradient_write;
1643 ps_class->painter_new = sp_radialgradient_painter_new;
1644 ps_class->painter_free = sp_radialgradient_painter_free;
1645 }
1647 /**
1648 * Callback for SPRadialGradient object initialization.
1649 */
1650 static void
1651 sp_radialgradient_init(SPRadialGradient *rg)
1652 {
1653 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1654 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1655 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1656 rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1657 rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1658 }
1660 /**
1661 * Set radial gradient attributes from associated repr.
1662 */
1663 static void
1664 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1665 {
1666 if (((SPObjectClass *) rg_parent_class)->build)
1667 (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1669 sp_object_read_attr(object, "cx");
1670 sp_object_read_attr(object, "cy");
1671 sp_object_read_attr(object, "r");
1672 sp_object_read_attr(object, "fx");
1673 sp_object_read_attr(object, "fy");
1674 }
1676 /**
1677 * Set radial gradient attribute.
1678 */
1679 static void
1680 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1681 {
1682 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1684 switch (key) {
1685 case SP_ATTR_CX:
1686 if (!rg->cx.read(value)) {
1687 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1688 }
1689 if (!rg->fx._set) {
1690 rg->fx.value = rg->cx.value;
1691 rg->fx.computed = rg->cx.computed;
1692 }
1693 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1694 break;
1695 case SP_ATTR_CY:
1696 if (!rg->cy.read(value)) {
1697 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1698 }
1699 if (!rg->fy._set) {
1700 rg->fy.value = rg->cy.value;
1701 rg->fy.computed = rg->cy.computed;
1702 }
1703 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1704 break;
1705 case SP_ATTR_R:
1706 if (!rg->r.read(value)) {
1707 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1708 }
1709 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1710 break;
1711 case SP_ATTR_FX:
1712 if (!rg->fx.read(value)) {
1713 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1714 }
1715 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1716 break;
1717 case SP_ATTR_FY:
1718 if (!rg->fy.read(value)) {
1719 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1720 }
1721 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1722 break;
1723 default:
1724 if (((SPObjectClass *) rg_parent_class)->set)
1725 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1726 break;
1727 }
1728 }
1730 /**
1731 * Write radial gradient attributes to associated repr.
1732 */
1733 static Inkscape::XML::Node *
1734 sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1735 {
1736 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1738 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1739 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
1740 repr = xml_doc->createElement("svg:radialGradient");
1741 }
1743 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1744 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1745 if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1746 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1747 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1749 if (((SPObjectClass *) rg_parent_class)->write)
1750 (* ((SPObjectClass *) rg_parent_class)->write)(object, repr, flags);
1752 return repr;
1753 }
1755 /**
1756 * Create radial gradient context.
1757 */
1758 static SPPainter *
1759 sp_radialgradient_painter_new(SPPaintServer *ps,
1760 NR::Matrix const &full_transform,
1761 NR::Matrix const &/*parent_transform*/,
1762 NRRect const *bbox)
1763 {
1764 SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1765 SPGradient *gr = SP_GRADIENT(ps);
1767 if (!gr->color) sp_gradient_ensure_colors(gr);
1769 SPRGPainter *rgp = g_new(SPRGPainter, 1);
1771 rgp->painter.type = SP_PAINTER_IND;
1772 rgp->painter.fill = sp_rg_fill;
1774 rgp->rg = rg;
1776 NR::Matrix gs2px;
1778 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1779 /** \todo
1780 * fixme: We may try to normalize here too, look at
1781 * linearGradient (Lauris)
1782 */
1784 /* BBox to user coordinate system */
1785 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1787 NR::Matrix gs2user = gr->gradientTransform * bbox2user;
1789 gs2px = gs2user * full_transform;
1790 } else {
1791 /** \todo
1792 * Problem: What to do, if we have mixed lengths and percentages?
1793 * Currently we do ignore percentages at all, but that is not
1794 * good (lauris)
1795 */
1797 gs2px = gr->gradientTransform * full_transform;
1798 }
1800 NRMatrix gs2px_nr;
1801 gs2px.copyto(&gs2px_nr);
1803 nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
1804 &gs2px_nr,
1805 rg->cx.computed, rg->cy.computed,
1806 rg->fx.computed, rg->fy.computed,
1807 rg->r.computed);
1809 return (SPPainter *) rgp;
1810 }
1812 static void
1813 sp_radialgradient_painter_free(SPPaintServer */*ps*/, SPPainter *painter)
1814 {
1815 g_free(painter);
1816 }
1818 /**
1819 * Directly set properties of radial gradient and request modified.
1820 */
1821 void
1822 sp_radialgradient_set_position(SPRadialGradient *rg,
1823 gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1824 {
1825 g_return_if_fail(rg != NULL);
1826 g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1828 /* fixme: units? (Lauris) */
1829 rg->cx.set(SVGLength::NONE, cx, cx);
1830 rg->cy.set(SVGLength::NONE, cy, cy);
1831 rg->fx.set(SVGLength::NONE, fx, fx);
1832 rg->fy.set(SVGLength::NONE, fy, fy);
1833 rg->r.set(SVGLength::NONE, r, r);
1835 SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1836 }
1838 /**
1839 * Callback when radial gradient object is rendered.
1840 */
1841 static void
1842 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1843 {
1844 SPRGPainter *rgp = (SPRGPainter *) painter;
1846 if (rgp->rg->color == NULL) {
1847 sp_gradient_ensure_colors (rgp->rg);
1848 rgp->rgr.vector = rgp->rg->color;
1849 }
1851 nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1852 }
1854 /*
1855 Local Variables:
1856 mode:c++
1857 c-file-style:"stroustrup"
1858 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1859 indent-tabs-mode:nil
1860 fill-column:99
1861 End:
1862 */
1863 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :