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);
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_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
597 flags &= SP_OBJECT_MODIFIED_CASCADE;
599 // FIXME: climb up the ladder of hrefs
600 GSList *l = NULL;
601 for (SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
602 g_object_ref(G_OBJECT(child));
603 l = g_slist_prepend(l, child);
604 }
605 l = g_slist_reverse(l);
606 while (l) {
607 SPObject *child = SP_OBJECT(l->data);
608 l = g_slist_remove(l, child);
609 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
610 child->emitModified(flags);
611 }
612 g_object_unref(G_OBJECT(child));
613 }
614 }
616 /**
617 * Write gradient attributes to repr.
618 */
619 static Inkscape::XML::Node *
620 sp_gradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
621 {
622 SPGradient *gr = SP_GRADIENT(object);
624 if (((SPObjectClass *) gradient_parent_class)->write)
625 (* ((SPObjectClass *) gradient_parent_class)->write)(object, repr, flags);
627 if (flags & SP_OBJECT_WRITE_BUILD) {
628 GSList *l = NULL;
629 for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) {
630 Inkscape::XML::Node *crepr;
631 crepr = child->updateRepr(NULL, flags);
632 if (crepr) l = g_slist_prepend(l, crepr);
633 }
634 while (l) {
635 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
636 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
637 l = g_slist_remove(l, l->data);
638 }
639 }
641 if (gr->ref->getURI()) {
642 gchar *uri_string = gr->ref->getURI()->toString();
643 repr->setAttribute("xlink:href", uri_string);
644 g_free(uri_string);
645 }
647 if ((flags & SP_OBJECT_WRITE_ALL) || gr->units_set) {
648 switch (gr->units) {
649 case SP_GRADIENT_UNITS_USERSPACEONUSE:
650 repr->setAttribute("gradientUnits", "userSpaceOnUse");
651 break;
652 default:
653 repr->setAttribute("gradientUnits", "objectBoundingBox");
654 break;
655 }
656 }
658 if ((flags & SP_OBJECT_WRITE_ALL) || gr->gradientTransform_set) {
659 gchar c[256];
660 if (sp_svg_transform_write(c, 256, gr->gradientTransform)) {
661 repr->setAttribute("gradientTransform", c);
662 } else {
663 repr->setAttribute("gradientTransform", NULL);
664 }
665 }
667 if ((flags & SP_OBJECT_WRITE_ALL) || gr->spread_set) {
668 /* FIXME: Ensure that gr->spread is the inherited value
669 * if !gr->spread_set. Not currently happening: see sp_gradient_modified.
670 */
671 switch (gr->spread) {
672 case SP_GRADIENT_SPREAD_REFLECT:
673 repr->setAttribute("spreadMethod", "reflect");
674 break;
675 case SP_GRADIENT_SPREAD_REPEAT:
676 repr->setAttribute("spreadMethod", "repeat");
677 break;
678 default:
679 repr->setAttribute("spreadMethod", "pad");
680 break;
681 }
682 }
684 return repr;
685 }
687 /**
688 * Forces the vector to be built, if not present (i.e., changed).
689 *
690 * \pre SP_IS_GRADIENT(gradient).
691 */
692 void
693 sp_gradient_ensure_vector(SPGradient *gradient)
694 {
695 g_return_if_fail(gradient != NULL);
696 g_return_if_fail(SP_IS_GRADIENT(gradient));
698 if (!gradient->vector.built) {
699 sp_gradient_rebuild_vector(gradient);
700 }
701 }
703 /**
704 * Set units property of gradient and emit modified.
705 */
706 void
707 sp_gradient_set_units(SPGradient *gr, SPGradientUnits units)
708 {
709 if (units != gr->units) {
710 gr->units = units;
711 gr->units_set = TRUE;
712 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
713 }
714 }
716 /**
717 * Set spread property of gradient and emit modified.
718 */
719 void
720 sp_gradient_set_spread(SPGradient *gr, SPGradientSpread spread)
721 {
722 if (spread != gr->spread) {
723 gr->spread = spread;
724 gr->spread_set = TRUE;
725 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
726 }
727 }
729 /**
730 * Returns the first of {src, src-\>ref-\>getObject(),
731 * src-\>ref-\>getObject()-\>ref-\>getObject(),...}
732 * for which \a match is true, or NULL if none found.
733 *
734 * The raison d'être of this routine is that it correctly handles cycles in the href chain (e.g., if
735 * a gradient gives itself as its href, or if each of two gradients gives the other as its href).
736 *
737 * \pre SP_IS_GRADIENT(src).
738 */
739 static SPGradient *
740 chase_hrefs(SPGradient *const src, bool (*match)(SPGradient const *))
741 {
742 g_return_val_if_fail(SP_IS_GRADIENT(src), NULL);
744 /* Use a pair of pointers for detecting loops: p1 advances half as fast as p2. If there is a
745 loop, then once p1 has entered the loop, we'll detect it the next time the distance between
746 p1 and p2 is a multiple of the loop size. */
747 SPGradient *p1 = src, *p2 = src;
748 bool do1 = false;
749 for (;;) {
750 if (match(p2)) {
751 return p2;
752 }
754 p2 = p2->ref->getObject();
755 if (!p2) {
756 return p2;
757 }
758 if (do1) {
759 p1 = p1->ref->getObject();
760 }
761 do1 = !do1;
763 if ( p2 == p1 ) {
764 /* We've been here before, so return NULL to indicate that no matching gradient found
765 * in the chain. */
766 return NULL;
767 }
768 }
769 }
771 /**
772 * True if gradient has stops.
773 */
774 static bool
775 has_stops(SPGradient const *gr)
776 {
777 return SP_GRADIENT_HAS_STOPS(gr);
778 }
780 /**
781 * True if gradient has spread set.
782 */
783 static bool
784 has_spread_set(SPGradient const *gr)
785 {
786 return gr->spread_set;
787 }
790 /**
791 * Returns private vector of given gradient (the gradient at the end of the href chain which has
792 * stops), optionally normalizing it.
793 *
794 * \pre SP_IS_GRADIENT(gradient).
795 * \pre There exists a gradient in the chain that has stops.
796 */
797 SPGradient *
798 sp_gradient_get_vector(SPGradient *gradient, bool force_vector)
799 {
800 g_return_val_if_fail(gradient != NULL, NULL);
801 g_return_val_if_fail(SP_IS_GRADIENT(gradient), NULL);
803 SPGradient *const src = chase_hrefs(gradient, has_stops);
804 return ( force_vector
805 ? sp_gradient_ensure_vector_normalized(src)
806 : src );
807 }
809 /**
810 * Returns the effective spread of given gradient (climbing up the refs chain if needed).
811 *
812 * \pre SP_IS_GRADIENT(gradient).
813 */
814 SPGradientSpread
815 sp_gradient_get_spread(SPGradient *gradient)
816 {
817 g_return_val_if_fail(SP_IS_GRADIENT(gradient), SP_GRADIENT_SPREAD_PAD);
819 SPGradient const *src = chase_hrefs(gradient, has_spread_set);
820 return ( src
821 ? src->spread
822 : SP_GRADIENT_SPREAD_PAD ); // pad is the default
823 }
825 /**
826 * Clears the gradient's svg:stop children from its repr.
827 */
828 void
829 sp_gradient_repr_clear_vector(SPGradient *gr)
830 {
831 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
833 /* Collect stops from original repr */
834 GSList *sl = NULL;
835 for (Inkscape::XML::Node *child = repr->firstChild() ; child != NULL; child = child->next() ) {
836 if (!strcmp(child->name(), "svg:stop")) {
837 sl = g_slist_prepend(sl, child);
838 }
839 }
840 /* Remove all stops */
841 while (sl) {
842 /** \todo
843 * fixme: This should work, unless we make gradient
844 * into generic group.
845 */
846 sp_repr_unparent((Inkscape::XML::Node *)sl->data);
847 sl = g_slist_remove(sl, sl->data);
848 }
849 }
851 /**
852 * Writes the gradient's internal vector (whether from its own stops, or
853 * inherited from refs) into the gradient repr as svg:stop elements.
854 */
855 void
856 sp_gradient_repr_write_vector(SPGradient *gr)
857 {
858 g_return_if_fail(gr != NULL);
859 g_return_if_fail(SP_IS_GRADIENT(gr));
861 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
863 /* We have to be careful, as vector may be our own, so construct repr list at first */
864 GSList *cl = NULL;
866 for (guint i = 0; i < gr->vector.stops.size(); i++) {
867 Inkscape::CSSOStringStream os;
868 Inkscape::XML::Node *child = sp_repr_new("svg:stop");
869 sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
870 /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
871 * sense for offset proportions. */
872 gchar c[64];
873 sp_svg_write_color(c, 64, sp_color_get_rgba32_ualpha(&gr->vector.stops[i].color, 0x00));
874 os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
875 child->setAttribute("style", os.str().c_str());
876 /* Order will be reversed here */
877 cl = g_slist_prepend(cl, child);
878 }
880 sp_gradient_repr_clear_vector(gr);
882 /* And insert new children from list */
883 while (cl) {
884 Inkscape::XML::Node *child = static_cast<Inkscape::XML::Node *>(cl->data);
885 repr->addChild(child, NULL);
886 Inkscape::GC::release(child);
887 cl = g_slist_remove(cl, child);
888 }
889 }
892 static void
893 gradient_ref_modified(SPObject *href, guint flags, SPGradient *gradient)
894 {
895 if (sp_gradient_invalidate_vector(gradient)) {
896 SP_OBJECT(gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
897 /* Conditional to avoid causing infinite loop if there's a cycle in the href chain. */
898 }
899 }
901 /** Return true iff change made. */
902 static bool
903 sp_gradient_invalidate_vector(SPGradient *gr)
904 {
905 bool ret = false;
907 if (gr->color != NULL) {
908 g_free(gr->color);
909 gr->color = NULL;
910 ret = true;
911 }
913 if (gr->vector.built) {
914 gr->vector.built = false;
915 gr->vector.stops.clear();
916 ret = true;
917 }
919 return ret;
920 }
922 /** Creates normalized color vector */
923 static void
924 sp_gradient_rebuild_vector(SPGradient *gr)
925 {
926 gint len = 0;
927 for ( SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
928 child != NULL ;
929 child = SP_OBJECT_NEXT(child) ) {
930 if (SP_IS_STOP(child)) {
931 len ++;
932 }
933 }
935 gr->has_stops = (len != 0);
937 gr->vector.stops.clear();
939 SPGradient *ref = gr->ref->getObject();
940 if ( !gr->has_stops && ref ) {
941 /* Copy vector from referenced gradient */
942 gr->vector.built = true; // Prevent infinite recursion.
943 sp_gradient_ensure_vector(ref);
944 if (!ref->vector.stops.empty()) {
945 gr->vector.built = ref->vector.built;
946 gr->vector.stops.assign(ref->vector.stops.begin(), ref->vector.stops.end());
947 return;
948 }
949 }
951 for (SPObject *child = sp_object_first_child(SP_OBJECT(gr)) ;
952 child != NULL;
953 child = SP_OBJECT_NEXT(child) ) {
954 if (SP_IS_STOP(child)) {
955 SPStop *stop = SP_STOP(child);
957 SPGradientStop gstop;
958 if (gr->vector.stops.size() > 0) {
959 // "Each gradient offset value is required to be equal to or greater than the
960 // previous gradient stop's offset value. If a given gradient stop's offset
961 // value is not equal to or greater than all previous offset values, then the
962 // offset value is adjusted to be equal to the largest of all previous offset
963 // values."
964 gstop.offset = MAX(stop->offset, gr->vector.stops.back().offset);
965 } else {
966 gstop.offset = stop->offset;
967 }
969 // "Gradient offset values less than 0 (or less than 0%) are rounded up to
970 // 0%. Gradient offset values greater than 1 (or greater than 100%) are rounded
971 // down to 100%."
972 gstop.offset = CLAMP(gstop.offset, 0, 1);
974 gstop.color = sp_stop_get_color(stop);
975 gstop.opacity = stop->opacity;
977 gr->vector.stops.push_back(gstop);
978 }
979 }
981 // Normalize per section 13.2.4 of SVG 1.1.
982 if (gr->vector.stops.size() == 0) {
983 /* "If no stops are defined, then painting shall occur as if 'none' were specified as the
984 * paint style."
985 */
986 {
987 SPGradientStop gstop;
988 gstop.offset = 0.0;
989 sp_color_set_rgb_rgba32(&gstop.color, 0x00000000);
990 gstop.opacity = 0.0;
991 gr->vector.stops.push_back(gstop);
992 }
993 {
994 SPGradientStop gstop;
995 gstop.offset = 1.0;
996 sp_color_set_rgb_rgba32(&gstop.color, 0x00000000);
997 gstop.opacity = 0.0;
998 gr->vector.stops.push_back(gstop);
999 }
1000 } else {
1001 /* "If one stop is defined, then paint with the solid color fill using the color defined
1002 * for that gradient stop."
1003 */
1004 if (gr->vector.stops.front().offset > 0.0) {
1005 // If the first one is not at 0, then insert a copy of the first at 0.
1006 SPGradientStop gstop;
1007 gstop.offset = 0.0;
1008 sp_color_copy(&gstop.color, &gr->vector.stops.front().color);
1009 gstop.opacity = gr->vector.stops.front().opacity;
1010 gr->vector.stops.insert(gr->vector.stops.begin(), gstop);
1011 }
1012 if (gr->vector.stops.back().offset < 1.0) {
1013 // If the last one is not at 1, then insert a copy of the last at 1.
1014 SPGradientStop gstop;
1015 gstop.offset = 1.0;
1016 sp_color_copy(&gstop.color, &gr->vector.stops.back().color);
1017 gstop.opacity = gr->vector.stops.back().opacity;
1018 gr->vector.stops.push_back(gstop);
1019 }
1020 }
1022 gr->vector.built = true;
1023 }
1025 /**
1026 * The gradient's color array is newly created and set up from vector.
1027 */
1028 void
1029 sp_gradient_ensure_colors(SPGradient *gr)
1030 {
1031 if (!gr->vector.built) {
1032 sp_gradient_rebuild_vector(gr);
1033 }
1034 g_return_if_fail(!gr->vector.stops.empty());
1036 /// \todo Where is the memory freed?
1037 if (!gr->color) {
1038 gr->color = g_new(guchar, 4 * NCOLORS);
1039 }
1041 for (guint i = 0; i < gr->vector.stops.size() - 1; i++) {
1042 guint32 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i].color,
1043 gr->vector.stops[i].opacity);
1044 gint r0 = (color >> 24) & 0xff;
1045 gint g0 = (color >> 16) & 0xff;
1046 gint b0 = (color >> 8) & 0xff;
1047 gint a0 = color & 0xff;
1048 color = sp_color_get_rgba32_falpha(&gr->vector.stops[i + 1].color,
1049 gr->vector.stops[i + 1].opacity);
1050 gint r1 = (color >> 24) & 0xff;
1051 gint g1 = (color >> 16) & 0xff;
1052 gint b1 = (color >> 8) & 0xff;
1053 gint a1 = color & 0xff;
1054 gint o0 = (gint) floor(gr->vector.stops[i].offset * (NCOLORS - 0.001));
1055 gint o1 = (gint) floor(gr->vector.stops[i + 1].offset * (NCOLORS - 0.001));
1056 if (o1 > o0) {
1057 gint dr = ((r1 - r0) << 16) / (o1 - o0);
1058 gint dg = ((g1 - g0) << 16) / (o1 - o0);
1059 gint db = ((b1 - b0) << 16) / (o1 - o0);
1060 gint da = ((a1 - a0) << 16) / (o1 - o0);
1061 gint r = r0 << 16;
1062 gint g = g0 << 16;
1063 gint b = b0 << 16;
1064 gint a = a0 << 16;
1065 for (int j = o0; j < o1 + 1; j++) {
1066 gr->color[4 * j] = r >> 16;
1067 gr->color[4 * j + 1] = g >> 16;
1068 gr->color[4 * j + 2] = b >> 16;
1069 gr->color[4 * j + 3] = a >> 16;
1070 r += dr;
1071 g += dg;
1072 b += db;
1073 a += da;
1074 }
1075 }
1076 }
1077 }
1079 /**
1080 * Renders gradient vector to buffer as line.
1081 *
1082 * RGB buffer background should be set up beforehand.
1083 *
1084 * @param len,width,height,rowstride Buffer parameters (1 or 2 dimensional).
1085 * @param span Full integer width of requested gradient.
1086 * @param pos Buffer starting position in span.
1087 */
1088 static void
1089 sp_gradient_render_vector_line_rgba(SPGradient *const gradient, guchar *buf,
1090 gint const len, gint const pos, gint const span)
1091 {
1092 g_return_if_fail(gradient != NULL);
1093 g_return_if_fail(SP_IS_GRADIENT(gradient));
1094 g_return_if_fail(buf != NULL);
1095 g_return_if_fail(len > 0);
1096 g_return_if_fail(pos >= 0);
1097 g_return_if_fail(pos + len <= span);
1098 g_return_if_fail(span > 0);
1100 if (!gradient->color) {
1101 sp_gradient_ensure_colors(gradient);
1102 }
1104 gint idx = (pos * 1024 << 8) / span;
1105 gint didx = (1024 << 8) / span;
1107 for (gint x = 0; x < len; x++) {
1108 /// \todo Can this be done with 4 byte copies?
1109 *buf++ = gradient->color[4 * (idx >> 8)];
1110 *buf++ = gradient->color[4 * (idx >> 8) + 1];
1111 *buf++ = gradient->color[4 * (idx >> 8) + 2];
1112 *buf++ = gradient->color[4 * (idx >> 8) + 3];
1113 idx += didx;
1114 }
1115 }
1117 /**
1118 * Render rectangular RGBA area from gradient vector.
1119 */
1120 void
1121 sp_gradient_render_vector_block_rgba(SPGradient *const gradient, guchar *buf,
1122 gint const width, gint const height, gint const rowstride,
1123 gint const pos, gint const span, bool const horizontal)
1124 {
1125 g_return_if_fail(gradient != NULL);
1126 g_return_if_fail(SP_IS_GRADIENT(gradient));
1127 g_return_if_fail(buf != NULL);
1128 g_return_if_fail(width > 0);
1129 g_return_if_fail(height > 0);
1130 g_return_if_fail(pos >= 0);
1131 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1132 g_return_if_fail(span > 0);
1134 if (horizontal) {
1135 sp_gradient_render_vector_line_rgba(gradient, buf, width, pos, span);
1136 for (gint y = 1; y < height; y++) {
1137 memcpy(buf + y * rowstride, buf, 4 * width);
1138 }
1139 } else {
1140 guchar *tmp = (guchar *)alloca(4 * height);
1141 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1142 for (gint y = 0; y < height; y++) {
1143 guchar *b = buf + y * rowstride;
1144 for (gint x = 0; x < width; x++) {
1145 *b++ = tmp[0];
1146 *b++ = tmp[1];
1147 *b++ = tmp[2];
1148 *b++ = tmp[3];
1149 }
1150 tmp += 4;
1151 }
1152 }
1153 }
1155 /**
1156 * Render rectangular RGB area from gradient vector.
1157 */
1158 void
1159 sp_gradient_render_vector_block_rgb(SPGradient *gradient, guchar *buf,
1160 gint const width, gint const height, gint const rowstride,
1161 gint const pos, gint const span, bool const horizontal)
1162 {
1163 g_return_if_fail(gradient != NULL);
1164 g_return_if_fail(SP_IS_GRADIENT(gradient));
1165 g_return_if_fail(buf != NULL);
1166 g_return_if_fail(width > 0);
1167 g_return_if_fail(height > 0);
1168 g_return_if_fail(pos >= 0);
1169 g_return_if_fail((horizontal && (pos + width <= span)) || (!horizontal && (pos + height <= span)));
1170 g_return_if_fail(span > 0);
1172 if (horizontal) {
1173 guchar *tmp = (guchar*)alloca(4 * width);
1174 sp_gradient_render_vector_line_rgba(gradient, tmp, width, pos, span);
1175 for (gint y = 0; y < height; y++) {
1176 guchar *t = tmp;
1177 for (gint x = 0; x < width; x++) {
1178 gint a = t[3];
1179 gint fc = (t[0] - buf[0]) * a;
1180 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1181 fc = (t[1] - buf[1]) * a;
1182 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1183 fc = (t[2] - buf[2]) * a;
1184 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1185 buf += 3;
1186 t += 4;
1187 }
1188 }
1189 } else {
1190 guchar *tmp = (guchar*)alloca(4 * height);
1191 sp_gradient_render_vector_line_rgba(gradient, tmp, height, pos, span);
1192 for (gint y = 0; y < height; y++) {
1193 guchar *t = tmp + 4 * y;
1194 for (gint x = 0; x < width; x++) {
1195 gint a = t[3];
1196 gint fc = (t[0] - buf[0]) * a;
1197 buf[0] = buf[0] + ((fc + (fc >> 8) + 0x80) >> 8);
1198 fc = (t[1] - buf[1]) * a;
1199 buf[1] = buf[1] + ((fc + (fc >> 8) + 0x80) >> 8);
1200 fc = (t[2] - buf[2]) * a;
1201 buf[2] = buf[2] + ((fc + (fc >> 8) + 0x80) >> 8);
1202 }
1203 }
1204 }
1205 }
1207 NR::Matrix
1208 sp_gradient_get_g2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1209 {
1210 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1211 return ( NR::scale(bbox.dimensions())
1212 * NR::translate(bbox.min())
1213 * ctm );
1214 } else {
1215 return ctm;
1216 }
1217 }
1219 NR::Matrix
1220 sp_gradient_get_gs2d_matrix(SPGradient const *gr, NR::Matrix const &ctm, NR::Rect const &bbox)
1221 {
1222 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1223 return ( gr->gradientTransform
1224 * NR::scale(bbox.dimensions())
1225 * NR::translate(bbox.min())
1226 * ctm );
1227 } else {
1228 return gr->gradientTransform * ctm;
1229 }
1230 }
1232 void
1233 sp_gradient_set_gs2d_matrix(SPGradient *gr, NR::Matrix const &ctm,
1234 NR::Rect const &bbox, NR::Matrix const &gs2d)
1235 {
1236 gr->gradientTransform = gs2d / ctm;
1237 if ( gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) {
1238 gr->gradientTransform = ( gr->gradientTransform
1239 / NR::translate(bbox.min())
1240 / NR::scale(bbox.dimensions()) );
1241 }
1242 gr->gradientTransform_set = TRUE;
1244 SP_OBJECT(gr)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1245 }
1247 /*
1248 * Linear Gradient
1249 */
1251 class SPLGPainter;
1253 /// A context with linear gradient, painter, and gradient renderer.
1254 struct SPLGPainter {
1255 SPPainter painter;
1256 SPLinearGradient *lg;
1258 NRLGradientRenderer lgr;
1259 };
1261 static void sp_lineargradient_class_init(SPLinearGradientClass *klass);
1262 static void sp_lineargradient_init(SPLinearGradient *lg);
1264 static void sp_lineargradient_build(SPObject *object,
1265 SPDocument *document,
1266 Inkscape::XML::Node *repr);
1267 static void sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value);
1268 static Inkscape::XML::Node *sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr,
1269 guint flags);
1271 static SPPainter *sp_lineargradient_painter_new(SPPaintServer *ps,
1272 NR::Matrix const &full_transform,
1273 NR::Matrix const &parent_transform,
1274 NRRect const *bbox);
1275 static void sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1277 static void sp_lg_fill(SPPainter *painter, NRPixBlock *pb);
1279 static SPGradientClass *lg_parent_class;
1281 /**
1282 * Register SPLinearGradient class and return its type.
1283 */
1284 GType
1285 sp_lineargradient_get_type()
1286 {
1287 static GType type = 0;
1288 if (!type) {
1289 GTypeInfo info = {
1290 sizeof(SPLinearGradientClass),
1291 NULL, NULL,
1292 (GClassInitFunc) sp_lineargradient_class_init,
1293 NULL, NULL,
1294 sizeof(SPLinearGradient),
1295 16,
1296 (GInstanceInitFunc) sp_lineargradient_init,
1297 NULL, /* value_table */
1298 };
1299 type = g_type_register_static(SP_TYPE_GRADIENT, "SPLinearGradient", &info, (GTypeFlags)0);
1300 }
1301 return type;
1302 }
1304 /**
1305 * SPLinearGradient vtable initialization.
1306 */
1307 static void sp_lineargradient_class_init(SPLinearGradientClass *klass)
1308 {
1309 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1310 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1312 lg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1314 sp_object_class->build = sp_lineargradient_build;
1315 sp_object_class->set = sp_lineargradient_set;
1316 sp_object_class->write = sp_lineargradient_write;
1318 ps_class->painter_new = sp_lineargradient_painter_new;
1319 ps_class->painter_free = sp_lineargradient_painter_free;
1320 }
1322 /**
1323 * Callback for SPLinearGradient object initialization.
1324 */
1325 static void sp_lineargradient_init(SPLinearGradient *lg)
1326 {
1327 lg->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
1328 lg->y1.unset(SVGLength::PERCENT, 0.5, 0.5);
1329 lg->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
1330 lg->y2.unset(SVGLength::PERCENT, 0.5, 0.5);
1331 }
1333 /**
1334 * Callback: set attributes from associated repr.
1335 */
1336 static void sp_lineargradient_build(SPObject *object,
1337 SPDocument *document,
1338 Inkscape::XML::Node *repr)
1339 {
1340 if (((SPObjectClass *) lg_parent_class)->build)
1341 (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
1343 sp_object_read_attr(object, "x1");
1344 sp_object_read_attr(object, "y1");
1345 sp_object_read_attr(object, "x2");
1346 sp_object_read_attr(object, "y2");
1347 }
1349 /**
1350 * Callback: set attribute.
1351 */
1352 static void
1353 sp_lineargradient_set(SPObject *object, unsigned key, gchar const *value)
1354 {
1355 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1357 switch (key) {
1358 case SP_ATTR_X1:
1359 lg->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
1360 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1361 break;
1362 case SP_ATTR_Y1:
1363 lg->y1.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1364 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1365 break;
1366 case SP_ATTR_X2:
1367 lg->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
1368 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1369 break;
1370 case SP_ATTR_Y2:
1371 lg->y2.readOrUnset(value, SVGLength::PERCENT, 0.5, 0.5);
1372 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1373 break;
1374 default:
1375 if (((SPObjectClass *) lg_parent_class)->set)
1376 (* ((SPObjectClass *) lg_parent_class)->set)(object, key, value);
1377 break;
1378 }
1379 }
1381 /**
1382 * Callback: write attributes to associated repr.
1383 */
1384 static Inkscape::XML::Node *
1385 sp_lineargradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1386 {
1387 SPLinearGradient *lg = SP_LINEARGRADIENT(object);
1389 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1390 repr = sp_repr_new("svg:linearGradient");
1391 }
1393 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x1._set)
1394 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
1395 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y1._set)
1396 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
1397 if ((flags & SP_OBJECT_WRITE_ALL) || lg->x2._set)
1398 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
1399 if ((flags & SP_OBJECT_WRITE_ALL) || lg->y2._set)
1400 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
1402 if (((SPObjectClass *) lg_parent_class)->write)
1403 (* ((SPObjectClass *) lg_parent_class)->write)(object, repr, flags);
1405 return repr;
1406 }
1408 /**
1409 * Create linear gradient context.
1410 *
1411 * Basically we have to deal with transformations
1412 *
1413 * 1) color2norm - maps point in (0,NCOLORS) vector to (0,1) vector
1414 * 2) norm2pos - maps (0,1) vector to x1,y1 - x2,y2
1415 * 2) gradientTransform
1416 * 3) bbox2user
1417 * 4) ctm == userspace to pixel grid
1418 *
1419 * See also (*) in sp-pattern about why we may need parent_transform.
1420 *
1421 * \todo (point 1 above) fixme: I do not know how to deal with start > 0
1422 * and end < 1.
1423 */
1424 static SPPainter *
1425 sp_lineargradient_painter_new(SPPaintServer *ps,
1426 NR::Matrix const &full_transform,
1427 NR::Matrix const &parent_transform,
1428 NRRect const *bbox)
1429 {
1430 SPLinearGradient *lg = SP_LINEARGRADIENT(ps);
1431 SPGradient *gr = SP_GRADIENT(ps);
1433 if (!gr->color) sp_gradient_ensure_colors(gr);
1435 SPLGPainter *lgp = g_new(SPLGPainter, 1);
1437 lgp->painter.type = SP_PAINTER_IND;
1438 lgp->painter.fill = sp_lg_fill;
1440 lgp->lg = lg;
1442 /** \todo
1443 * Technically speaking, we map NCOLORS on line [start,end] onto line
1444 * [0,1]. I almost think we should fill color array start and end in
1445 * that case. The alternative would be to leave these just empty garbage
1446 * or something similar. Originally I had 1023.9999 here - not sure
1447 * whether we have really to cut out ceil int (Lauris).
1448 */
1449 NR::Matrix color2norm(NR::identity());
1450 NR::Matrix color2px;
1451 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1452 NR::Matrix norm2pos(NR::identity());
1454 /* BBox to user coordinate system */
1455 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1457 NR::Matrix color2pos = color2norm * norm2pos;
1458 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1459 NR::Matrix color2user = color2tpos * bbox2user;
1460 color2px = color2user * full_transform;
1462 } else {
1463 /* Problem: What to do, if we have mixed lengths and percentages? */
1464 /* Currently we do ignore percentages at all, but that is not good (lauris) */
1466 NR::Matrix norm2pos(NR::identity());
1467 NR::Matrix color2pos = color2norm * norm2pos;
1468 NR::Matrix color2tpos = color2pos * gr->gradientTransform;
1469 color2px = color2tpos * full_transform;
1471 }
1473 NRMatrix v2px;
1474 color2px.copyto(&v2px);
1476 nr_lgradient_renderer_setup(&lgp->lgr, gr->color, sp_gradient_get_spread(gr), &v2px,
1477 lg->x1.computed, lg->y1.computed,
1478 lg->x2.computed, lg->y2.computed);
1480 return (SPPainter *) lgp;
1481 }
1483 static void
1484 sp_lineargradient_painter_free(SPPaintServer *ps, SPPainter *painter)
1485 {
1486 g_free(painter);
1487 }
1489 /**
1490 * Directly set properties of linear gradient and request modified.
1491 */
1492 void
1493 sp_lineargradient_set_position(SPLinearGradient *lg,
1494 gdouble x1, gdouble y1,
1495 gdouble x2, gdouble y2)
1496 {
1497 g_return_if_fail(lg != NULL);
1498 g_return_if_fail(SP_IS_LINEARGRADIENT(lg));
1500 /* fixme: units? (Lauris) */
1501 lg->x1.set(SVGLength::NONE, x1, x1);
1502 lg->y1.set(SVGLength::NONE, y1, y1);
1503 lg->x2.set(SVGLength::NONE, x2, x2);
1504 lg->y2.set(SVGLength::NONE, y2, y2);
1506 SP_OBJECT(lg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1507 }
1509 /**
1510 * Callback when linear gradient object is rendered.
1511 */
1512 static void
1513 sp_lg_fill(SPPainter *painter, NRPixBlock *pb)
1514 {
1515 SPLGPainter *lgp = (SPLGPainter *) painter;
1517 if (lgp->lg->color == NULL) {
1518 sp_gradient_ensure_colors (lgp->lg);
1519 lgp->lgr.vector = lgp->lg->color;
1520 }
1522 nr_render((NRRenderer *) &lgp->lgr, pb, NULL);
1523 }
1525 /*
1526 * Radial Gradient
1527 */
1529 class SPRGPainter;
1531 /// A context with radial gradient, painter, and gradient renderer.
1532 struct SPRGPainter {
1533 SPPainter painter;
1534 SPRadialGradient *rg;
1535 NRRGradientRenderer rgr;
1536 };
1538 static void sp_radialgradient_class_init(SPRadialGradientClass *klass);
1539 static void sp_radialgradient_init(SPRadialGradient *rg);
1541 static void sp_radialgradient_build(SPObject *object,
1542 SPDocument *document,
1543 Inkscape::XML::Node *repr);
1544 static void sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value);
1545 static Inkscape::XML::Node *sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr,
1546 guint flags);
1548 static SPPainter *sp_radialgradient_painter_new(SPPaintServer *ps,
1549 NR::Matrix const &full_transform,
1550 NR::Matrix const &parent_transform,
1551 NRRect const *bbox);
1552 static void sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter);
1554 static void sp_rg_fill(SPPainter *painter, NRPixBlock *pb);
1556 static SPGradientClass *rg_parent_class;
1558 /**
1559 * Register SPRadialGradient class and return its type.
1560 */
1561 GType
1562 sp_radialgradient_get_type()
1563 {
1564 static GType type = 0;
1565 if (!type) {
1566 GTypeInfo info = {
1567 sizeof(SPRadialGradientClass),
1568 NULL, NULL,
1569 (GClassInitFunc) sp_radialgradient_class_init,
1570 NULL, NULL,
1571 sizeof(SPRadialGradient),
1572 16,
1573 (GInstanceInitFunc) sp_radialgradient_init,
1574 NULL, /* value_table */
1575 };
1576 type = g_type_register_static(SP_TYPE_GRADIENT, "SPRadialGradient", &info, (GTypeFlags)0);
1577 }
1578 return type;
1579 }
1581 /**
1582 * SPRadialGradient vtable initialization.
1583 */
1584 static void sp_radialgradient_class_init(SPRadialGradientClass *klass)
1585 {
1586 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
1587 SPPaintServerClass *ps_class = (SPPaintServerClass *) klass;
1589 rg_parent_class = (SPGradientClass*)g_type_class_ref(SP_TYPE_GRADIENT);
1591 sp_object_class->build = sp_radialgradient_build;
1592 sp_object_class->set = sp_radialgradient_set;
1593 sp_object_class->write = sp_radialgradient_write;
1595 ps_class->painter_new = sp_radialgradient_painter_new;
1596 ps_class->painter_free = sp_radialgradient_painter_free;
1597 }
1599 /**
1600 * Callback for SPRadialGradient object initialization.
1601 */
1602 static void
1603 sp_radialgradient_init(SPRadialGradient *rg)
1604 {
1605 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1606 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1607 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1608 rg->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
1609 rg->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
1610 }
1612 /**
1613 * Set radial gradient attributes from associated repr.
1614 */
1615 static void
1616 sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
1617 {
1618 if (((SPObjectClass *) rg_parent_class)->build)
1619 (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
1621 sp_object_read_attr(object, "cx");
1622 sp_object_read_attr(object, "cy");
1623 sp_object_read_attr(object, "r");
1624 sp_object_read_attr(object, "fx");
1625 sp_object_read_attr(object, "fy");
1626 }
1628 /**
1629 * Set radial gradient attribute.
1630 */
1631 static void
1632 sp_radialgradient_set(SPObject *object, unsigned key, gchar const *value)
1633 {
1634 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1636 switch (key) {
1637 case SP_ATTR_CX:
1638 if (!rg->cx.read(value)) {
1639 rg->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
1640 }
1641 if (!rg->fx._set) {
1642 rg->fx.value = rg->cx.value;
1643 rg->fx.computed = rg->cx.computed;
1644 }
1645 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1646 break;
1647 case SP_ATTR_CY:
1648 if (!rg->cy.read(value)) {
1649 rg->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
1650 }
1651 if (!rg->fy._set) {
1652 rg->fy.value = rg->cy.value;
1653 rg->fy.computed = rg->cy.computed;
1654 }
1655 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1656 break;
1657 case SP_ATTR_R:
1658 if (!rg->r.read(value)) {
1659 rg->r.unset(SVGLength::PERCENT, 0.5, 0.5);
1660 }
1661 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1662 break;
1663 case SP_ATTR_FX:
1664 if (!rg->fx.read(value)) {
1665 rg->fx.unset(rg->cx.unit, rg->cx.value, rg->cx.computed);
1666 }
1667 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1668 break;
1669 case SP_ATTR_FY:
1670 if (!rg->fy.read(value)) {
1671 rg->fy.unset(rg->cy.unit, rg->cy.value, rg->cy.computed);
1672 }
1673 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
1674 break;
1675 default:
1676 if (((SPObjectClass *) rg_parent_class)->set)
1677 ((SPObjectClass *) rg_parent_class)->set(object, key, value);
1678 break;
1679 }
1680 }
1682 /**
1683 * Write radial gradient attributes to associated repr.
1684 */
1685 static Inkscape::XML::Node *
1686 sp_radialgradient_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1687 {
1688 SPRadialGradient *rg = SP_RADIALGRADIENT(object);
1690 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1691 repr = sp_repr_new("svg:radialGradient");
1692 }
1694 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cx._set) sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
1695 if ((flags & SP_OBJECT_WRITE_ALL) || rg->cy._set) sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
1696 if ((flags & SP_OBJECT_WRITE_ALL) || rg->r._set) sp_repr_set_svg_double(repr, "r", rg->r.computed);
1697 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fx._set) sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
1698 if ((flags & SP_OBJECT_WRITE_ALL) || rg->fy._set) sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
1700 if (((SPObjectClass *) rg_parent_class)->write)
1701 (* ((SPObjectClass *) rg_parent_class)->write)(object, repr, flags);
1703 return repr;
1704 }
1706 /**
1707 * Create radial gradient context.
1708 */
1709 static SPPainter *
1710 sp_radialgradient_painter_new(SPPaintServer *ps,
1711 NR::Matrix const &full_transform,
1712 NR::Matrix const &parent_transform,
1713 NRRect const *bbox)
1714 {
1715 SPRadialGradient *rg = SP_RADIALGRADIENT(ps);
1716 SPGradient *gr = SP_GRADIENT(ps);
1718 if (!gr->color) sp_gradient_ensure_colors(gr);
1720 SPRGPainter *rgp = g_new(SPRGPainter, 1);
1722 rgp->painter.type = SP_PAINTER_IND;
1723 rgp->painter.fill = sp_rg_fill;
1725 rgp->rg = rg;
1727 NR::Matrix gs2px;
1729 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1730 /** \todo
1731 * fixme: We may try to normalize here too, look at
1732 * linearGradient (Lauris)
1733 */
1735 /* BBox to user coordinate system */
1736 NR::Matrix bbox2user(bbox->x1 - bbox->x0, 0, 0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
1738 NR::Matrix gs2user = gr->gradientTransform * bbox2user;
1740 gs2px = gs2user * full_transform;
1741 } else {
1742 /** \todo
1743 * Problem: What to do, if we have mixed lengths and percentages?
1744 * Currently we do ignore percentages at all, but that is not
1745 * good (lauris)
1746 */
1748 gs2px = gr->gradientTransform * full_transform;
1749 }
1751 NRMatrix gs2px_nr;
1752 gs2px.copyto(&gs2px_nr);
1754 nr_rgradient_renderer_setup(&rgp->rgr, gr->color, sp_gradient_get_spread(gr),
1755 &gs2px_nr,
1756 rg->cx.computed, rg->cy.computed,
1757 rg->fx.computed, rg->fy.computed,
1758 rg->r.computed);
1760 return (SPPainter *) rgp;
1761 }
1763 static void
1764 sp_radialgradient_painter_free(SPPaintServer *ps, SPPainter *painter)
1765 {
1766 g_free(painter);
1767 }
1769 /**
1770 * Directly set properties of radial gradient and request modified.
1771 */
1772 void
1773 sp_radialgradient_set_position(SPRadialGradient *rg,
1774 gdouble cx, gdouble cy, gdouble fx, gdouble fy, gdouble r)
1775 {
1776 g_return_if_fail(rg != NULL);
1777 g_return_if_fail(SP_IS_RADIALGRADIENT(rg));
1779 /* fixme: units? (Lauris) */
1780 rg->cx.set(SVGLength::NONE, cx, cx);
1781 rg->cy.set(SVGLength::NONE, cy, cy);
1782 rg->fx.set(SVGLength::NONE, fx, fx);
1783 rg->fy.set(SVGLength::NONE, fy, fy);
1784 rg->r.set(SVGLength::NONE, r, r);
1786 SP_OBJECT(rg)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1787 }
1789 /**
1790 * Callback when radial gradient object is rendered.
1791 */
1792 static void
1793 sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
1794 {
1795 SPRGPainter *rgp = (SPRGPainter *) painter;
1797 if (rgp->rg->color == NULL) {
1798 sp_gradient_ensure_colors (rgp->rg);
1799 rgp->rgr.vector = rgp->rg->color;
1800 }
1802 nr_render((NRRenderer *) &rgp->rgr, pb, NULL);
1803 }
1805 /*
1806 Local Variables:
1807 mode:c++
1808 c-file-style:"stroustrup"
1809 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1810 indent-tabs-mode:nil
1811 fill-column:99
1812 End:
1813 */
1814 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :