d28f631ded7aab5513232452c8e0259811236d9a
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 repr = sp_repr_new("svg:stop");
208 }
211 Inkscape::CSSOStringStream os;
212 os << "stop-color:";
213 if (stop->currentColor) {
214 os << "currentColor";
215 } else {
216 gchar c[64];
217 sp_svg_write_color(c, 64, sp_color_get_rgba32_ualpha(&stop->specified_color, 255));
218 os << c;
219 }
220 os << ";stop-opacity:" << stop->opacity;
221 repr->setAttribute("style", os.str().c_str());
222 repr->setAttribute("stop-color", NULL);
223 repr->setAttribute("stop-opacity", NULL);
224 sp_repr_set_css_double(repr, "offset", stop->offset);
225 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no sense
226 * for offset proportions. */
228 if (((SPObjectClass *) stop_parent_class)->write)
229 (* ((SPObjectClass *) stop_parent_class)->write)(object, repr, flags);
231 return repr;
232 }
234 /**
235 * Return stop's color as 32bit value.
236 */
237 guint32
238 sp_stop_get_rgba32(SPStop const *const stop)
239 {
240 guint32 rgb0 = 0;
241 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
242 * value depends on user agent, and don't give any further restrictions that I can
243 * see.) */
244 if (stop->currentColor) {
245 char const *str = sp_object_get_style_property(stop, "color", NULL);
246 if (str) {
247 rgb0 = sp_svg_read_color(str, rgb0);
248 }
249 unsigned const alpha = static_cast<unsigned>(stop->opacity * 0xff + 0.5);
250 g_return_val_if_fail((alpha & ~0xff) == 0,
251 rgb0 | 0xff);
252 return rgb0 | alpha;
253 } else {
254 return sp_color_get_rgba32_falpha(&stop->specified_color, stop->opacity);
255 }
256 }
258 /**
259 * Return stop's color as SPColor.
260 */
261 static SPColor
262 sp_stop_get_color(SPStop const *const stop)
263 {
264 if (stop->currentColor) {
265 char const *str = sp_object_get_style_property(stop, "color", NULL);
266 guint32 const dfl = 0;
267 /* Default value: arbitrarily black. (SVG1.1 and CSS2 both say that the initial
268 * value depends on user agent, and don't give any further restrictions that I can
269 * see.) */
270 guint32 color = dfl;
271 if (str) {
272 color = sp_svg_read_color(str, dfl);
273 }
274 SPColor ret;
275 sp_color_set_rgb_rgba32(&ret, color);
276 return ret;
277 } else {
278 return stop->specified_color;
279 }
280 }
282 /*
283 * Gradient
284 */
286 static void sp_gradient_class_init(SPGradientClass *klass);
287 static void sp_gradient_init(SPGradient *gr);
289 static void sp_gradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
290 static void sp_gradient_release(SPObject *object);
291 static void sp_gradient_set(SPObject *object, unsigned key, gchar const *value);
292 static void sp_gradient_child_added(SPObject *object,
293 Inkscape::XML::Node *child,
294 Inkscape::XML::Node *ref);
295 static void sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child);
296 static void sp_gradient_modified(SPObject *object, guint flags);
297 static Inkscape::XML::Node *sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr,
298 guint flags);
300 static void gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient);
302 static bool sp_gradient_invalidate_vector(SPGradient *gr);
303 static void sp_gradient_rebuild_vector(SPGradient *gr);
305 static void gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gradient);
307 static SPPaintServerClass *gradient_parent_class;
309 /**
310 * Registers SPGradient class and returns its type.
311 */
312 GType
313 sp_gradient_get_type()
314 {
315 static GType gradient_type = 0;
316 if (!gradient_type) {
317 GTypeInfo gradient_info = {
318 sizeof(SPGradientClass),
319 NULL, NULL,
320 (GClassInitFunc) sp_gradient_class_init,
321 NULL, NULL,
322 sizeof(SPGradient),
323 16,
324 (GInstanceInitFunc) sp_gradient_init,
325 NULL, /* value_table */
326 };
327 gradient_type = g_type_register_static(SP_TYPE_PAINT_SERVER, "SPGradient",
328 &gradient_info, (GTypeFlags)0);
329 }
330 return gradient_type;
331 }
333 /**
334 * SPGradient vtable initialization.
335 */
336 static void
337 sp_gradient_class_init(SPGradientClass *klass)
338 {
339 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
341 gradient_parent_class = (SPPaintServerClass *)g_type_class_ref(SP_TYPE_PAINT_SERVER);
343 sp_object_class->build = sp_gradient_build;
344 sp_object_class->release = sp_gradient_release;
345 sp_object_class->set = sp_gradient_set;
346 sp_object_class->child_added = sp_gradient_child_added;
347 sp_object_class->remove_child = sp_gradient_remove_child;
348 sp_object_class->modified = sp_gradient_modified;
349 sp_object_class->write = sp_gradient_write;
350 }
352 /**
353 * Callback for SPGradient object initialization.
354 */
355 static void
356 sp_gradient_init(SPGradient *gr)
357 {
358 gr->ref = new SPGradientReference(SP_OBJECT(gr));
359 gr->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(gradient_ref_changed), gr));
361 /** \todo
362 * Fixme: reprs being rearranged (e.g. via the XML editor)
363 * may require us to clear the state.
364 */
365 gr->state = SP_GRADIENT_STATE_UNKNOWN;
367 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
368 gr->units_set = FALSE;
370 gr->gradientTransform = NR::identity();
371 gr->gradientTransform_set = FALSE;
373 gr->spread = SP_GRADIENT_SPREAD_PAD;
374 gr->spread_set = FALSE;
376 gr->has_stops = FALSE;
378 gr->vector.built = false;
379 gr->vector.stops.clear();
381 gr->color = NULL;
383 new (&gr->modified_connection) sigc::connection();
384 }
386 /**
387 * Virtual build: set gradient attributes from its associated repr.
388 */
389 static void
390 sp_gradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
391 {
392 SPGradient *gradient = SP_GRADIENT(object);
394 if (((SPObjectClass *) gradient_parent_class)->build)
395 (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
397 SPObject *ochild;
398 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
399 if (SP_IS_STOP(ochild)) {
400 gradient->has_stops = TRUE;
401 break;
402 }
403 }
405 sp_object_read_attr(object, "gradientUnits");
406 sp_object_read_attr(object, "gradientTransform");
407 sp_object_read_attr(object, "spreadMethod");
408 sp_object_read_attr(object, "xlink:href");
410 /* Register ourselves */
411 sp_document_add_resource(document, "gradient", object);
412 }
414 /**
415 * Virtual release of SPGradient members before destruction.
416 */
417 static void
418 sp_gradient_release(SPObject *object)
419 {
420 SPGradient *gradient = (SPGradient *) object;
422 #ifdef SP_GRADIENT_VERBOSE
423 g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
424 #endif
426 if (SP_OBJECT_DOCUMENT(object)) {
427 /* Unregister ourselves */
428 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "gradient", SP_OBJECT(object));
429 }
431 if (gradient->ref) {
432 gradient->modified_connection.disconnect();
433 gradient->ref->detach();
434 delete gradient->ref;
435 gradient->ref = NULL;
436 }
438 if (gradient->color) {
439 g_free(gradient->color);
440 gradient->color = NULL;
441 }
443 gradient->modified_connection.~connection();
445 if (((SPObjectClass *) gradient_parent_class)->release)
446 ((SPObjectClass *) gradient_parent_class)->release(object);
447 }
449 /**
450 * Set gradient attribute to value.
451 */
452 static void
453 sp_gradient_set(SPObject *object, unsigned key, gchar const *value)
454 {
455 SPGradient *gr = SP_GRADIENT(object);
457 switch (key) {
458 case SP_ATTR_GRADIENTUNITS:
459 if (value) {
460 if (!strcmp(value, "userSpaceOnUse")) {
461 gr->units = SP_GRADIENT_UNITS_USERSPACEONUSE;
462 } else {
463 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
464 }
465 gr->units_set = TRUE;
466 } else {
467 gr->units = SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX;
468 gr->units_set = FALSE;
469 }
470 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
471 break;
472 case SP_ATTR_GRADIENTTRANSFORM: {
473 NR::Matrix t;
474 if (value && sp_svg_transform_read(value, &t)) {
475 gr->gradientTransform = t;
476 gr->gradientTransform_set = TRUE;
477 } else {
478 gr->gradientTransform = NR::identity();
479 gr->gradientTransform_set = FALSE;
480 }
481 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
482 break;
483 }
484 case SP_ATTR_SPREADMETHOD:
485 if (value) {
486 if (!strcmp(value, "reflect")) {
487 gr->spread = SP_GRADIENT_SPREAD_REFLECT;
488 } else if (!strcmp(value, "repeat")) {
489 gr->spread = SP_GRADIENT_SPREAD_REPEAT;
490 } else {
491 gr->spread = SP_GRADIENT_SPREAD_PAD;
492 }
493 gr->spread_set = TRUE;
494 } else {
495 gr->spread_set = FALSE;
496 }
497 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
498 break;
499 case SP_ATTR_XLINK_HREF:
500 if (value) {
501 try {
502 gr->ref->attach(Inkscape::URI(value));
503 } catch (Inkscape::BadURIException &e) {
504 g_warning("%s", e.what());
505 gr->ref->detach();
506 }
507 } else {
508 gr->ref->detach();
509 }
510 break;
511 default:
512 if (((SPObjectClass *) gradient_parent_class)->set)
513 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
514 break;
515 }
516 }
518 /**
519 * Gets called when the gradient is (re)attached to another gradient.
520 */
521 static void
522 gradient_ref_changed(SPObject *old_ref, SPObject *ref, SPGradient *gr)
523 {
524 if (old_ref) {
525 gr->modified_connection.disconnect();
526 }
527 if ( SP_IS_GRADIENT(ref)
528 && ref != gr )
529 {
530 gr->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&gradient_ref_modified), gr));
531 }
532 /// \todo Fixme: what should the flags (second) argument be? */
533 gradient_ref_modified(ref, 0, gr);
534 }
536 /**
537 * Callback for child_added event.
538 */
539 static void
540 sp_gradient_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
541 {
542 SPGradient *gr = SP_GRADIENT(object);
544 sp_gradient_invalidate_vector(gr);
546 if (((SPObjectClass *) gradient_parent_class)->child_added)
547 (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
549 SPObject *ochild = sp_object_get_child_by_repr(object, child);
550 if ( ochild && SP_IS_STOP(ochild) ) {
551 gr->has_stops = TRUE;
552 }
554 /// \todo Fixme: should we schedule "modified" here?
555 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
556 }
558 /**
559 * Callback for remove_child event.
560 */
561 static void
562 sp_gradient_remove_child(SPObject *object, Inkscape::XML::Node *child)
563 {
564 SPGradient *gr = SP_GRADIENT(object);
566 sp_gradient_invalidate_vector(gr);
568 if (((SPObjectClass *) gradient_parent_class)->remove_child)
569 (* ((SPObjectClass *) gradient_parent_class)->remove_child)(object, child);
571 gr->has_stops = FALSE;
572 SPObject *ochild;
573 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
574 if (SP_IS_STOP(ochild)) {
575 gr->has_stops = TRUE;
576 break;
577 }
578 }
580 /* Fixme: should we schedule "modified" here? */
581 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
582 }
584 /**
585 * Callback for modified event.
586 */
587 static void
588 sp_gradient_modified(SPObject *object, guint flags)
589 {
590 SPGradient *gr = SP_GRADIENT(object);
592 if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) {
593 sp_gradient_invalidate_vector(gr);
594 }
596 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
597 sp_gradient_ensure_colors(gr);
598 }
600 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
601 flags &= SP_OBJECT_MODIFIED_CASCADE;
603 // FIXME: climb up the ladder of hrefs
604 GSList *l = NULL;
605 for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
606 g_object_ref(G_OBJECT(child));
607 l = g_slist_prepend(l, child);
608 }
609 l = g_slist_reverse(l);
610 while (l) {
611 SPObject *child = SP_OBJECT(l->data);
612 l = g_slist_remove(l, child);
613 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
614 child->emitModified(flags);
615 }
616 g_object_unref(G_OBJECT(child));
617 }
618 }
620 /**
621 * Write gradient attributes to repr.
622 */
623 static Inkscape::XML::Node *
624 sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
625 {
626 SPGradient *gr = SP_GRADIENT(object);
628 if (((SPObjectClass *) gradient_parent_class)->write)
629 (* ((SPObjectClass *) gradient_parent_class)->write)(object, repr, flags);
631 if (flags & SP_OBJECT_WRITE_BUILD) {
632 GSList *l = NULL;
633 for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
634 Inkscape::XML::Node *crepr;
635 crepr = child->updateRepr(NULL, flags);
636 if (crepr) l = g_slist_prepend(l, crepr);
637 }
638 while (l) {
639 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
640 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
641 l = g_slist_remove(l, l->data);
642 }
643 }
645 if (gr->ref->getURI()) {
646 gchar *uri_string = gr->ref->getURI()->toString();
647 repr->setAttribute("xlink:href", uri_string);
648 g_free(uri_string);
649 }
651 if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
652 switch (gr->units) {
653 case SP_GRADIENT_UNITS_USERSPACEONUSE:
654 repr->setAttribute("gradientUnits", "userSpaceOnUse");
655 break;
656 default:
657 repr->setAttribute("gradientUnits", "objectBoundingBox");
658 break;
659 }
660 }
662 if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
663 gchar c[256];
664 if (sp_svg_transform_write(c, 256, gr->gradientTransform)) {
665 repr->setAttribute("gradientTransform", c);
666 } else {
667 repr->setAttribute("gradientTransform", NULL);
668 }
669 }
671 if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
672 /* FIXME: Ensure that gr->spread is the inherited value
673 * if !gr->spread_set. Not currently happening: see sp_gradient_modified.
674 */
675 switch (gr->spread) {
676 case SP_GRADIENT_SPREAD_REFLECT:
677 repr->setAttribute("spreadMethod", "reflect");
678 break;
679 case SP_GRADIENT_SPREAD_REPEAT:
680 repr->setAttribute("spreadMethod", "repeat");
681 break;
682 default:
683 repr->setAttribute("spreadMethod", "pad");
684 break;
685 }
686 }
688 return repr;
689 }
691 /**
692 * Forces the vector to be built, if not present (i.e., changed).
693 *
694 * \pre SP_IS_GRADIENT(gradient).
695 */
696 void
697 sp_gradient_ensure_vector(SPGradient *gradient)
698 {
699 g_return_if_fail(gradient != NULL);
700 g_return_if_fail(SP_IS_GRADIENT(gradient));
702 if (!gradient->vector.built) {
703 sp_gradient_rebuild_vector(gradient);
704 }
705 }
707 /**
708 * Set units property of gradient and emit modified.
709 */
710 void
711 sp_gradient_set_units(SPGradient *gr, SPGradientUnits units)
712 {
713 if (units != gr->units) {
714 gr->units = units;
715 gr->units_set = TRUE;
716 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
717 }
718 }
720 /**
721 * Set spread property of gradient and emit modified.
722 */
723 void
724 sp_gradient_set_spread(SPGradient *gr, SPGradientSpread spread)
725 {
726 if (spread != gr->spread) {
727 gr->spread = spread;
728 gr->spread_set = TRUE;
729 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
730 }
731 }
733 /**
734 * Returns the first of {src, src-\>ref-\>getObject(),
735 * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
736 * for which \a match is true, or NULL if none found.
737 *
738 * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
739 * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
740 *
741 * \pre SP_IS_GRADIENT(src).
742 */
743 static SPGradient *
744 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
745 {
746 g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
748 /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2. If there is a
749 loop, then once p1 has entered the loop, we'll detect it the next time the distance between
750 p1 and p2 is a multiple of the loop size. */
751 SPGradient *p1 = src, *p2 = src;
752 bool do1 = false;
753 for (;;) {
754 if (match(p2)) {
755 return p2;
756 }
758 p2 = p2->ref->getObject();
759 if (!p2) {
760 return p2;
761 }
762 if (do1) {
763 p1 = p1->ref->getObject();
764 }
765 do1 = !do1;
767 if ( p2 == p1 ) {
768 /* We've been here before, so return NULL to indicate that no matching gradient found
769 * in the chain. */
770 return NULL;
771 }
772 }
773 }
775 /**
776 * True if gradient has stops.
777 */
778 static bool
779 has_stops(SPGradient const *gr)
780 {
781 return SP_GRADIENT_HAS_STOPS(gr);
782 }
784 /**
785 * True if gradient has spread set.
786 */
787 static bool
788 has_spread_set(SPGradient const *gr)
789 {
790 return gr->spread_set;
791 }
794 /**
795 * Returns private vector of given gradient (the gradient at the end of the href chain which has
796 * stops), optionally normalizing it.
797 *
798 * \pre SP_IS_GRADIENT(gradient).
799 * \pre There exists a gradient in the chain that has stops.
800 */
801 SPGradient *
802 sp_gradient_get_vector(SPGradient *gradient, bool force_vector)
803 {
804 g_return_val_if_fail(gradient != NULL, NULL);
805 g_return_val_if_fail(SP_IS_GRADIENT(gradient), NULL);
807 SPGradient *const src = chase_hrefs(gradient, has_stops);
808 return ( force_vector
809 ? sp_gradient_ensure_vector_normalized(src)
810 : src );
811 }
813 /**
814 * Returns the effective spread of given gradient (climbing up the refs chain if needed).
815 *
816 * \pre SP_IS_GRADIENT(gradient).
817 */
818 SPGradientSpread
819 sp_gradient_get_spread(SPGradient *gradient)
820 {
821 g_return_val_if_fail(SP_IS_GRADIENT(gradient), SP_GRADIENT_SPREAD_PAD);
823 SPGradient const *src = chase_hrefs(gradient, has_spread_set);
824 return ( src
825 ? src->spread
826 : SP_GRADIENT_SPREAD_PAD ); // pad is the default
827 }
829 /**
830 * Clears the gradient's svg:stop children from its repr.
831 */
832 void
833 sp_gradient_repr_clear_vector(SPGradient *gr)
834 {
835 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
837 /* Collect stops from original repr */
838 GSList *sl = NULL;
839 for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
840 if (!strcmp(child->name(), "svg:stop")) {
841 sl = g_slist_prepend(sl, child);
842 }
843 }
844 /* Remove all stops */
845 while (sl) {
846 /** \todo
847 * fixme: This should work, unless we make gradient
848 * into generic group.
849 */
850 sp_repr_unparent((Inkscape::XML::Node *)sl->data);
851 sl = g_slist_remove(sl, sl->data);
852 }
853 }
855 /**
856 * Writes the gradient's internal vector (whether from its own stops, or
857 * inherited from refs) into the gradient repr as svg:stop elements.
858 */
859 void
860 sp_gradient_repr_write_vector(SPGradient *gr)
861 {
862 g_return_if_fail(gr != NULL);
863 g_return_if_fail(SP_IS_GRADIENT(gr));
865 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
867 /* We have to be careful, as vector may be our own, so construct repr list at first */
868 GSList *cl = NULL;
870 for (guint i = 0; i < gr->vector.stops.size(); i++) {
871 Inkscape::CSSOStringStream os;
872 Inkscape::XML::Node *child = sp_repr_new("svg:stop");
873 sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
874 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
875 * sense for offset proportions. */
876 gchar c[64];
877 sp_svg_write_color(c, 64, sp_color_get_rgba32_ualpha(&gr->vector.stops[i].color, 0x00));
878 os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
879 child->setAttribute("style", os.str().c_str());
880 /* Order will be reversed here */
881 cl = g_slist_prepend(cl, child);
882 }
884 sp_gradient_repr_clear_vector(gr);
886 /* And insert new children from list */
887 while (cl) {
888 Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
889 repr->addChild(child, NULL);
890 Inkscape::GC::release(child);
891 cl = g_slist_remove(cl, child);
892 }
893 }
896 static void
897 gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient)
898 {
899 if (sp_gradient_invalidate_vector(gradient)) {
900 SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
901 /* Conditional to avoid causing infinite loop if there's a cycle in the href chain. */
902 }
903 }
905 /** Return true iff change made. */
906 static bool
907 sp_gradient_invalidate_vector(SPGradient *gr)
908 {
909 bool ret = false;
911 if (gr->color != NULL) {
912 g_free(gr->color);
913 gr->color = NULL;
914 ret = true;
915 }
917 if (gr->vector.built) {
918 gr->vector.built = false;
919 gr->vector.stops.clear();
920 ret = true;
921 }
923 return ret;
924 }
926 /** Creates normalized color vector */
927 static void
928 sp_gradient_rebuild_vector(SPGradient *gr)
929 {
930 gint len = 0;
931 for ( SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
932 child != NULL ;
933 child = SP_OBJECT_NEXT(child) ) {
934 if (SP_IS_STOP(child)) {
935 len ++;
936 }
937 }
939 gr->has_stops = (len != 0);
941 gr->vector.stops.clear();
943 SPGradient *ref = gr->ref->getObject();
944 if ( !gr->has_stops && ref ) {
945 /* Copy vector from referenced gradient */
946 gr->vector.built = true; // Prevent infinite recursion.
947 sp_gradient_ensure_vector(ref);
948 if (!ref->vector.stops.empty()) {
949 gr->vector.built = ref->vector.built;
950 gr->vector.stops.assign(ref->vector.stops.begin(), ref->vector.stops.end());
951 return;
952 }
953 }
955 for (SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
956 child != NULL;
957 child = SP_OBJECT_NEXT(child) ) {
958 if (SP_IS_STOP(child)) {
959 SPStop *stop = SP_STOP(child);
961 SPGradientStop gstop;
962 if (gr->vector.stops.size() > 0) {
963 // "Each gradient offset value is required to be equal to or greater than the
964 // previous gradient stop's offset value. If a given gradient stop's offset
965 // value is not equal to or greater than all previous offset values, then the
966 // offset value is adjusted to be equal to the largest of all previous offset
967 // values."
968 gstop.offset = MAX(stop->offset, gr->vector.stops.back().offset);
969 } else {
970 gstop.offset = stop->offset;
971 }
973 // "Gradient offset values less than 0 (or less than 0%) are rounded up to
974 // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
975 // down to 100%."
976 gstop.offset = CLAMP(gstop.offset, 0, 1);
978 gstop.color = sp_stop_get_color(stop);
979 gstop.opacity = stop->opacity;
981 gr->vector.stops.push_back(gstop);
982 }
983 }
985 // Normalize per section 13.2.4 of SVG 1.1.
986 if (gr->vector.stops.size() == 0) {
987 /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
988 * paint style."
989 */
990 {
991 SPGradientStop gstop;
992 gstop.offset = 0.0;
993 sp_color_set_rgb_rgba32(&gstop.color, 0x00000000);
994 gstop.opacity = 0.0;
995 gr->vector.stops.push_back(gstop);
996 }
997 {
998 SPGradientStop gstop;
999 gstop.offset = 1.0;
1000 sp_color_set_rgb_rgba32(&gstop.color, 0x00000000);
1001 gstop.opacity = 0.0;
1002 gr->vector.stops.push_back(gstop);
1003 }
1004 } else {
1005 /* "If one stop is defined, then paint with the solid color fill using the color defined
1006 * for that gradient stop."
1007 */
1008 if (gr->vector.stops.front().offset > 0.0) {
1009 // If the first one is not at 0, then insert a copy of the first at 0.
1010 SPGradientStop gstop;
1011 gstop.offset = 0.0;
1012 sp_color_copy(&gstop.color, &gr->vector.stops.front().color);
1013 gstop.opacity = gr->vector.stops.front().opacity;
1014 gr->vector.stops.insert(gr->vector.stops.begin(), gstop);
1015 }
1016 if (gr->vector.stops.back().offset < 1.0) {
1017 // If the last one is not at 1, then insert a copy of the last at 1.
1018 SPGradientStop gstop;
1019 gstop.offset = 1.0;
1020 sp_color_copy(&gstop.color, &gr->vector.stops.back().color);
1021 gstop.opacity = gr->vector.stops.back().opacity;
1022 gr->vector.stops.push_back(gstop);
1023 }
1024 }
1026 gr->vector.built = true;
1027 }
1029 /**
1030 * The gradient's color array is newly created and set up from vector.
1031 */
1032 void
1033 sp_gradient_ensure_colors(SPGradient *gr)
1034 {
1035 if (!gr->vector.built) {
1036 sp_gradient_rebuild_vector(gr);
1037 }
1038 g_return_if_fail(!gr->vector.stops.empty());
1040 /// \todo Where is the memory freed?
1041 if (!gr->color) {
1042 gr->color = g_new(guchar, 4 * NCOLORS);
1043 }
1045 for (guint i = 0; i < gr->vector.stops.size() - 1; i++) {
1046 guint32 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i].color,
1047 gr->vector.stops[i].opacity);
1048 gint r0 = (color >> 24) & 0xff;
1049 gint g0 = (color >> 16) & 0xff;
1050 gint b0 = (color >> 8) & 0xff;
1051 gint a0 = color & 0xff;
1052 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i + 1].color,
1053 gr->vector.stops[i + 1].opacity);
1054 gint r1 = (color >> 24) & 0xff;
1055 gint g1 = (color >> 16) & 0xff;
1056 gint b1 = (color >> 8) & 0xff;
1057 gint a1 = color & 0xff;
1058 gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001));
1059 gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001));
1060 if (o1 > o0) {
1061 gint dr = ((r1 - r0) << 16) / (o1 - o0);
1062 gint dg = ((g1 - g0) << 16) / (o1 - o0);
1063 gint db = ((b1 - b0) << 16) / (o1 - o0);
1064 gint da = ((a1 - a0) << 16) / (o1 - o0);
1065 gint r = r0 << 16;
1066 gint g = g0 << 16;
1067 gint b = b0 << 16;
1068 gint a = a0 << 16;
1069 for (int j = o0; j < o1 + 1; j++) {
1070 gr->color[4 * j] = r >> 16;
1071 gr->color[4 * j + 1] = g >> 16;
1072 gr->color[4 * j + 2] = b >> 16;
1073 gr->color[4 * j + 3] = a >> 16;
1074 r += dr;
1075 g += dg;
1076 b += db;
1077 a += da;
1078 }
1079 }
1080 }
1081 }
1083 /**
1084 * Renders gradient vector to buffer as line.
1085 *
1086 * RGB buffer background should be set up beforehand.
1087 *
1088 * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1089 * @param span Full integer width of requested gradient.
1090 * @param pos Buffer starting position in span.
1091 */
1092 static void
1093 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1094 gint const len, gint const pos, gint const span)
1095 {
1096 g_return_if_fail(gradient != NULL);
1097 g_return_if_fail(SP_IS_GRADIENT(gradient));
1098 g_return_if_fail(buf != NULL);
1099 g_return_if_fail(len > 0);
1100 g_return_if_fail(pos >= 0);
1101 g_return_if_fail(pos + len <= span);
1102 g_return_if_fail(span > 0);
1104 if (!gradient->color) {
1105 sp_gradient_ensure_colors(gradient);
1106 }
1108 gint idx = (pos * 1024 << 8) / span;
1109 gint didx = (1024 << 8) / span;
1111 for (gint x = 0; x < len; x++) {
1112 /// \todo Can this be done with 4 byte copies?
1113 *buf++ = gradient->color[4 * (idx >> 8)];
1114 *buf++ = gradient->color[4 * (idx >> 8) + 1];
1115 *buf++ = gradient->color[4 * (idx >> 8) + 2];
1116 *buf++ = gradient->color[4 * (idx >> 8) + 3];
1117 idx += didx;
1118 }
1119 }
1121 /**
1122 * Render rectangular RGBA area from gradient vector.
1123 */
1124 void
1125 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1126 gint const width, gint const height, gint const rowstride,
1127 gint const pos, gint const span, bool const horizontal)
1128 {
1129 g_return_if_fail(gradient != NULL);
1130 g_return_if_fail(SP_IS_GRADIENT(gradient));
1131 g_return_if_fail(buf != NULL);
1132 g_return_if_fail(width > 0);
1133 g_return_if_fail(height > 0);
1134 g_return_if_fail(pos >= 0);
1135 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1136 g_return_if_fail(span > 0);
1138 if (horizontal) {
1139 sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1140 for (gint y = 1; y < height; y++) {
1141 memcpy(buf + y * rowstride, buf, 4 * width);
1142 }
1143 } else {
1144 guchar *tmp = (guchar *)alloca(4 * height);
1145 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1146 for (gint y = 0; y < height; y++) {
1147 guchar *b = buf + y * rowstride;
1148 for (gint x = 0; x < width; x++) {
1149 *b++ = tmp[0];
1150 *b++ = tmp[1];
1151 *b++ = tmp[2];
1152 *b++ = tmp[3];
1153 }
1154 tmp += 4;
1155 }
1156 }
1157 }
1159 /**
1160 * Render rectangular RGB area from gradient vector.
1161 */
1162 void
1163 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1164 gint const width, gint const height, gint const rowstride,
1165 gint const pos, gint const span, bool const horizontal)
1166 {
1167 g_return_if_fail(gradient != NULL);
1168 g_return_if_fail(SP_IS_GRADIENT(gradient));
1169 g_return_if_fail(buf != NULL);
1170 g_return_if_fail(width > 0);
1171 g_return_if_fail(height > 0);
1172 g_return_if_fail(pos >= 0);
1173 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1174 g_return_if_fail(span > 0);
1176 if (horizontal) {
1177 guchar *tmp = (guchar*)alloca(4 * width);
1178 sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1179 for (gint y = 0; y < height; y++) {
1180 guchar *t = tmp;
1181 for (gint x = 0; x < width; x++) {
1182 gint a = t[3];
1183 gint fc = (t[0] - buf[0]) * a;
1184 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1185 fc = (t[1] - buf[1]) * a;
1186 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1187 fc = (t[2] - buf[2]) * a;
1188 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1189 buf += 3;
1190 t += 4;
1191 }
1192 }
1193 } else {
1194 guchar *tmp = (guchar*)alloca(4 * height);
1195 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1196 for (gint y = 0; y < height; y++) {
1197 guchar *t = tmp + 4 * y;
1198 for (gint x = 0; x < width; x++) {
1199 gint a = t[3];
1200 gint fc = (t[0] - buf[0]) * a;
1201 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1202 fc = (t[1] - buf[1]) * a;
1203 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1204 fc = (t[2] - buf[2]) * a;
1205 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1206 }
1207 }
1208 }
1209 }
1211 NR::Matrix
1212 sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1213 {
1214 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1215 return ( NR::scale(bbox.dimensions())
1216 * NR::translate(bbox.min())
1217 * ctm );
1218 } else {
1219 return ctm;
1220 }
1221 }
1223 NR::Matrix
1224 sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1225 {
1226 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1227 return ( gr->gradientTransform
1228 * NR::scale(bbox.dimensions())
1229 * NR::translate(bbox.min())
1230 * ctm );
1231 } else {
1232 return gr->gradientTransform * ctm;
1233 }
1234 }
1236 void
1237 sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
1238 NR::Rect const &bbox, NR::Matrix const &gs2d)
1239 {
1240 gr->gradientTransform = gs2d / ctm;
1241 if ( gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1242 gr->gradientTransform = ( gr->gradientTransform
1243 / NR::translate(bbox.min())
1244 / NR::scale(bbox.dimensions()) );
1245 }
1246 gr->gradientTransform_set = TRUE;
1248 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1249 }
1251 /*
1252 * Linear Gradient
1253 */
1255 class SPLGPainter;
1257 /// A context with linear gradient, painter, and gradient renderer.
1258 struct SPLGPainter {
1259 SPPainter painter;
1260 SPLinearGradient *lg;
1262 NRLGradientRenderer lgr;
1263 };
1265 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1266 static void sp_lineargradient_init(SPLinearGradient *lg);
1268 static void sp_lineargradient_build(SPObject *object,
1269 SPDocument *document,
1270 Inkscape::XML::Node *repr);
1271 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1272 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr,
1273 guint flags);
1275 static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
1276 NR::Matrix const &full_transform,
1277 NR::Matrix const &parent_transform,
1278 NRRect const *bbox);
1279 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1281 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1283 static SPGradientClass *lg_parent_class;
1285 /**
1286 * Register SPLinearGradient class and return its type.
1287 */
1288 GType
1289 sp_lineargradient_get_type()
1290 {
1291 static GType type = 0;
1292 if (!type) {
1293 GTypeInfo info = {
1294 sizeof(SPLinearGradientClass),
1295 NULL, NULL,
1296 (GClassInitFunc) sp_lineargradient_class_init,
1297 NULL, NULL,
1298 sizeof(SPLinearGradient),
1299 16,
1300 (GInstanceInitFunc) sp_lineargradient_init,
1301 NULL, /* value_table */
1302 };
1303 type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1304 }
1305 return type;
1306 }
1308 /**
1309 * SPLinearGradient vtable initialization.
1310 */
1311 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1312 {
1313 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1314 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1316 lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1318 sp_object_class->build = sp_lineargradient_build;
1319 sp_object_class->set = sp_lineargradient_set;
1320 sp_object_class->write = sp_lineargradient_write;
1322 ps_class->painter_new = sp_lineargradient_painter_new;
1323 ps_class->painter_free = sp_lineargradient_painter_free;
1324 }
1326 /**
1327 * Callback for SPLinearGradient object initialization.
1328 */
1329 static void sp_lineargradient_init(SPLinearGradient *lg)
1330 {
1331 lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1332 lg->y1.unset(SVGLength::PERCENT, 0.5, 0.5);
1333 lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1334 lg->y2.unset(SVGLength::PERCENT, 0.5, 0.5);
1335 }
1337 /**
1338 * Callback: set attributes from associated repr.
1339 */
1340 static void sp_lineargradient_build(SPObject *object,
1341 SPDocument *document,
1342 Inkscape::XML::Node *repr)
1343 {
1344 if (((SPObjectClass *) lg_parent_class)->build)
1345 (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1347 sp_object_read_attr(object, "x1");
1348 sp_object_read_attr(object, "y1");
1349 sp_object_read_attr(object, "x2");
1350 sp_object_read_attr(object, "y2");
1351 }
1353 /**
1354 * Callback: set attribute.
1355 */
1356 static void
1357 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1358 {
1359 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1361 switch (key) {
1362 case SP_ATTR_X1:
1363 lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1364 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1365 break;
1366 case SP_ATTR_Y1:
1367 lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1368 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1369 break;
1370 case SP_ATTR_X2:
1371 lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1372 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1373 break;
1374 case SP_ATTR_Y2:
1375 lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1376 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1377 break;
1378 default:
1379 if (((SPObjectClass *) lg_parent_class)->set)
1380 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1381 break;
1382 }
1383 }
1385 /**
1386 * Callback: write attributes to associated repr.
1387 */
1388 static Inkscape::XML::Node *
1389 sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1390 {
1391 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1393 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1394 repr = sp_repr_new("svg:linearGradient");
1395 }
1397 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1398 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1399 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1400 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1401 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1402 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1403 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1404 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1406 if (((SPObjectClass *) lg_parent_class)->write)
1407 (* ((SPObjectClass *) lg_parent_class)->write)(object, repr, flags);
1409 return repr;
1410 }
1412 /**
1413 * Create linear gradient context.
1414 *
1415 * Basically we have to deal with transformations
1416 *
1417 * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1418 * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1419 * 2) gradientTransform
1420 * 3) bbox2user
1421 * 4) ctm == userspace to pixel grid
1422 *
1423 * See also (*) in sp-pattern about why we may need parent_transform.
1424 *
1425 * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1426 * and end < 1.
1427 */
1428 static SPPainter *
1429 sp_lineargradient_painter_new(SPPaintServer *ps,
1430 NR::Matrix const &full_transform,
1431 NR::Matrix const &parent_transform,
1432 NRRect const *bbox)
1433 {
1434 SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1435 SPGradient *gr = SP_GRADIENT(ps);
1437 if (!gr->color) sp_gradient_ensure_colors(gr);
1439 SPLGPainter *lgp = g_new(SPLGPainter, 1);
1441 lgp->painter.type = SP_PAINTER_IND;
1442 lgp->painter.fill = sp_lg_fill;
1444 lgp->lg = lg;
1446 /** \todo
1447 * Technically speaking, we map NCOLORS on line [start,end] onto line
1448 * [0,1]. I almost think we should fill color array start and end in
1449 * that case. The alternative would be to leave these just empty garbage
1450 * or something similar. Originally I had 1023.9999 here - not sure
1451 * whether we have really to cut out ceil int (Lauris).
1452 */
1453 NR::Matrix color2norm(NR::identity());
1454 NR::Matrix color2px;
1455 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1456 NR::Matrix norm2pos(NR::identity());
1458 /* BBox to user coordinate system */
1459 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1461 NR::Matrix color2pos = color2norm * norm2pos;
1462 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1463 NR::Matrix color2user = color2tpos * bbox2user;
1464 color2px = color2user * full_transform;
1466 } else {
1467 /* Problem: What to do, if we have mixed lengths and percentages? */
1468 /* Currently we do ignore percentages at all, but that is not good (lauris) */
1470 NR::Matrix norm2pos(NR::identity());
1471 NR::Matrix color2pos = color2norm * norm2pos;
1472 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1473 color2px = color2tpos * full_transform;
1475 }
1477 NRMatrix v2px;
1478 color2px.copyto(&v2px);
1480 nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
1481 lg->x1.computed, lg->y1.computed,
1482 lg->x2.computed, lg->y2.computed);
1484 return (SPPainter *) lgp;
1485 }
1487 static void
1488 sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter)
1489 {
1490 g_free(painter);
1491 }
1493 /**
1494 * Directly set properties of linear gradient and request modified.
1495 */
1496 void
1497 sp_lineargradient_set_position(SPLinearGradient *lg,
1498 gdouble x1, gdouble y1,
1499 gdouble x2, gdouble y2)
1500 {
1501 g_return_if_fail(lg != NULL);
1502 g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1504 /* fixme: units? (Lauris) */
1505 lg->x1.set(SVGLength::NONE, x1, x1);
1506 lg->y1.set(SVGLength::NONE, y1, y1);
1507 lg->x2.set(SVGLength::NONE, x2, x2);
1508 lg->y2.set(SVGLength::NONE, y2, y2);
1510 SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1511 }
1513 /**
1514 * Callback when linear gradient object is rendered.
1515 */
1516 static void
1517 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1518 {
1519 SPLGPainter *lgp = (SPLGPainter *) painter;
1521 if (lgp->lg->color == NULL) {
1522 sp_gradient_ensure_colors (lgp->lg);
1523 lgp->lgr.vector = lgp->lg->color;
1524 }
1526 nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1527 }
1529 /*
1530 * Radial Gradient
1531 */
1533 class SPRGPainter;
1535 /// A context with radial gradient, painter, and gradient renderer.
1536 struct SPRGPainter {
1537 SPPainter painter;
1538 SPRadialGradient *rg;
1539 NRRGradientRenderer rgr;
1540 };
1542 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1543 static void sp_radialgradient_init(SPRadialGradient *rg);
1545 static void sp_radialgradient_build(SPObject *object,
1546 SPDocument *document,
1547 Inkscape::XML::Node *repr);
1548 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1549 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr,
1550 guint flags);
1552 static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
1553 NR::Matrix const &full_transform,
1554 NR::Matrix const &parent_transform,
1555 NRRect const *bbox);
1556 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1558 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1560 static SPGradientClass *rg_parent_class;
1562 /**
1563 * Register SPRadialGradient class and return its type.
1564 */
1565 GType
1566 sp_radialgradient_get_type()
1567 {
1568 static GType type = 0;
1569 if (!type) {
1570 GTypeInfo info = {
1571 sizeof(SPRadialGradientClass),
1572 NULL, NULL,
1573 (GClassInitFunc) sp_radialgradient_class_init,
1574 NULL, NULL,
1575 sizeof(SPRadialGradient),
1576 16,
1577 (GInstanceInitFunc) sp_radialgradient_init,
1578 NULL, /* value_table */
1579 };
1580 type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1581 }
1582 return type;
1583 }
1585 /**
1586 * SPRadialGradient vtable initialization.
1587 */
1588 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1589 {
1590 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1591 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1593 rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1595 sp_object_class->build = sp_radialgradient_build;
1596 sp_object_class->set = sp_radialgradient_set;
1597 sp_object_class->write = sp_radialgradient_write;
1599 ps_class->painter_new = sp_radialgradient_painter_new;
1600 ps_class->painter_free = sp_radialgradient_painter_free;
1601 }
1603 /**
1604 * Callback for SPRadialGradient object initialization.
1605 */
1606 static void
1607 sp_radialgradient_init(SPRadialGradient *rg)
1608 {
1609 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1610 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1611 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1612 rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1613 rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1614 }
1616 /**
1617 * Set radial gradient attributes from associated repr.
1618 */
1619 static void
1620 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1621 {
1622 if (((SPObjectClass *) rg_parent_class)->build)
1623 (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1625 sp_object_read_attr(object, "cx");
1626 sp_object_read_attr(object, "cy");
1627 sp_object_read_attr(object, "r");
1628 sp_object_read_attr(object, "fx");
1629 sp_object_read_attr(object, "fy");
1630 }
1632 /**
1633 * Set radial gradient attribute.
1634 */
1635 static void
1636 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1637 {
1638 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1640 switch (key) {
1641 case SP_ATTR_CX:
1642 if (!rg->cx.read(value)) {
1643 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1644 }
1645 if (!rg->fx._set) {
1646 rg->fx.value = rg->cx.value;
1647 rg->fx.computed = rg->cx.computed;
1648 }
1649 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1650 break;
1651 case SP_ATTR_CY:
1652 if (!rg->cy.read(value)) {
1653 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1654 }
1655 if (!rg->fy._set) {
1656 rg->fy.value = rg->cy.value;
1657 rg->fy.computed = rg->cy.computed;
1658 }
1659 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1660 break;
1661 case SP_ATTR_R:
1662 if (!rg->r.read(value)) {
1663 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1664 }
1665 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1666 break;
1667 case SP_ATTR_FX:
1668 if (!rg->fx.read(value)) {
1669 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1670 }
1671 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1672 break;
1673 case SP_ATTR_FY:
1674 if (!rg->fy.read(value)) {
1675 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1676 }
1677 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1678 break;
1679 default:
1680 if (((SPObjectClass *) rg_parent_class)->set)
1681 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1682 break;
1683 }
1684 }
1686 /**
1687 * Write radial gradient attributes to associated repr.
1688 */
1689 static Inkscape::XML::Node *
1690 sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1691 {
1692 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1694 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1695 repr = sp_repr_new("svg:radialGradient");
1696 }
1698 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1699 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1700 if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1701 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1702 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1704 if (((SPObjectClass *) rg_parent_class)->write)
1705 (* ((SPObjectClass *) rg_parent_class)->write)(object, repr, flags);
1707 return repr;
1708 }
1710 /**
1711 * Create radial gradient context.
1712 */
1713 static SPPainter *
1714 sp_radialgradient_painter_new(SPPaintServer *ps,
1715 NR::Matrix const &full_transform,
1716 NR::Matrix const &parent_transform,
1717 NRRect const *bbox)
1718 {
1719 SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1720 SPGradient *gr = SP_GRADIENT(ps);
1722 if (!gr->color) sp_gradient_ensure_colors(gr);
1724 SPRGPainter *rgp = g_new(SPRGPainter, 1);
1726 rgp->painter.type = SP_PAINTER_IND;
1727 rgp->painter.fill = sp_rg_fill;
1729 rgp->rg = rg;
1731 NR::Matrix gs2px;
1733 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1734 /** \todo
1735 * fixme: We may try to normalize here too, look at
1736 * linearGradient (Lauris)
1737 */
1739 /* BBox to user coordinate system */
1740 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1742 NR::Matrix gs2user = gr->gradientTransform * bbox2user;
1744 gs2px = gs2user * full_transform;
1745 } else {
1746 /** \todo
1747 * Problem: What to do, if we have mixed lengths and percentages?
1748 * Currently we do ignore percentages at all, but that is not
1749 * good (lauris)
1750 */
1752 gs2px = gr->gradientTransform * full_transform;
1753 }
1755 NRMatrix gs2px_nr;
1756 gs2px.copyto(&gs2px_nr);
1758 nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
1759 &gs2px_nr,
1760 rg->cx.computed, rg->cy.computed,
1761 rg->fx.computed, rg->fy.computed,
1762 rg->r.computed);
1764 return (SPPainter *) rgp;
1765 }
1767 static void
1768 sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter)
1769 {
1770 g_free(painter);
1771 }
1773 /**
1774 * Directly set properties of radial gradient and request modified.
1775 */
1776 void
1777 sp_radialgradient_set_position(SPRadialGradient *rg,
1778 gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1779 {
1780 g_return_if_fail(rg != NULL);
1781 g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1783 /* fixme: units? (Lauris) */
1784 rg->cx.set(SVGLength::NONE, cx, cx);
1785 rg->cy.set(SVGLength::NONE, cy, cy);
1786 rg->fx.set(SVGLength::NONE, fx, fx);
1787 rg->fy.set(SVGLength::NONE, fy, fy);
1788 rg->r.set(SVGLength::NONE, r, r);
1790 SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1791 }
1793 /**
1794 * Callback when radial gradient object is rendered.
1795 */
1796 static void
1797 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1798 {
1799 SPRGPainter *rgp = (SPRGPainter *) painter;
1801 if (rgp->rg->color == NULL) {
1802 sp_gradient_ensure_colors (rgp->rg);
1803 rgp->rgr.vector = rgp->rg->color;
1804 }
1806 nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1807 }
1809 /*
1810 Local Variables:
1811 mode:c++
1812 c-file-style:"stroustrup"
1813 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1814 indent-tabs-mode:nil
1815 fill-column:99
1816 End:
1817 */
1818 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :