1 #define __SP_GRADIENT_CHEMISTRY_C__
3 /*
4 * Various utility methods for gradients
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak
9 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
10 *
11 * Copyright (C) 2007 Johan Engelen
12 * Copyright (C) 2001-2005 authors
13 * Copyright (C) 2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
19 #include "style.h"
20 #include "document-private.h"
21 #include "desktop-style.h"
23 #include "sp-gradient-reference.h"
24 #include "sp-gradient-vector.h"
25 #include "sp-linear-gradient.h"
26 #include "sp-radial-gradient.h"
27 #include "sp-stop.h"
28 #include "widgets/gradient-vector.h"
30 #include "sp-text.h"
31 #include "sp-tspan.h"
32 #include <libnr/nr-matrix-fns.h>
33 #include <libnr/nr-point-fns.h>
34 #include <libnr/nr-matrix-ops.h>
35 #include <libnr/nr-rotate-ops.h>
36 #include <2geom/transforms.h>
37 #include "xml/repr.h"
38 #include "svg/svg.h"
39 #include "svg/svg-color.h"
40 #include "svg/css-ostringstream.h"
41 #include "prefs-utils.h"
44 // Terminology:
45 //
46 // "vector" is a gradient that has stops but not position coords. It can be referenced by one or
47 // more privates. Objects should not refer to it directly. It has no radial/linear distinction.
48 //
49 // "private" is a gradient that has no stops but has position coords (e.g. center, radius etc for a
50 // radial). It references a vector for the actual colors. Each private is only used by one
51 // object. It is either linear or radial.
53 static void sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *gr);
55 SPGradient *
56 sp_gradient_ensure_vector_normalized(SPGradient *gr)
57 {
58 g_return_val_if_fail(gr != NULL, NULL);
59 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
61 /* If we are already normalized vector, just return */
62 if (gr->state == SP_GRADIENT_STATE_VECTOR) return gr;
63 /* Fail, if we have wrong state set */
64 if (gr->state != SP_GRADIENT_STATE_UNKNOWN) {
65 g_warning("file %s: line %d: Cannot normalize private gradient to vector (%s)", __FILE__, __LINE__, SP_OBJECT_ID(gr));
66 return NULL;
67 }
69 /* First make sure we have vector directly defined (i.e. gr has its own stops) */
70 if (!gr->has_stops) {
71 /* We do not have stops ourselves, so flatten stops as well */
72 sp_gradient_ensure_vector(gr);
73 g_assert(gr->vector.built);
74 // this adds stops from gr->vector as children to gr
75 sp_gradient_repr_write_vector (gr);
76 }
78 /* If gr hrefs some other gradient, remove the href */
79 if (gr->ref->getObject()) {
80 /* We are hrefing someone, so require flattening */
81 SP_OBJECT(gr)->updateRepr(SP_OBJECT_WRITE_EXT | SP_OBJECT_WRITE_ALL);
82 sp_gradient_repr_set_link(SP_OBJECT_REPR(gr), NULL);
83 }
85 /* Everything is OK, set state flag */
86 gr->state = SP_GRADIENT_STATE_VECTOR;
87 return gr;
88 }
90 /**
91 * Creates new private gradient for the given vector
92 */
94 static SPGradient *
95 sp_gradient_get_private_normalized(SPDocument *document, SPGradient *vector, SPGradientType type)
96 {
97 g_return_val_if_fail(document != NULL, NULL);
98 g_return_val_if_fail(vector != NULL, NULL);
99 g_return_val_if_fail(SP_IS_GRADIENT(vector), NULL);
100 g_return_val_if_fail(SP_GRADIENT_HAS_STOPS(vector), NULL);
102 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
104 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
105 // create a new private gradient of the requested type
106 Inkscape::XML::Node *repr;
107 if (type == SP_GRADIENT_TYPE_LINEAR) {
108 repr = xml_doc->createElement("svg:linearGradient");
109 } else {
110 repr = xml_doc->createElement("svg:radialGradient");
111 }
113 // privates are garbage-collectable
114 repr->setAttribute("inkscape:collect", "always");
116 // link to vector
117 sp_gradient_repr_set_link(repr, vector);
119 /* Append the new private gradient to defs */
120 SP_OBJECT_REPR(defs)->appendChild(repr);
121 Inkscape::GC::release(repr);
123 // get corresponding object
124 SPGradient *gr = (SPGradient *) document->getObjectByRepr(repr);
125 g_assert(gr != NULL);
126 g_assert(SP_IS_GRADIENT(gr));
128 return gr;
129 }
131 /**
132 Count how many times gr is used by the styles of o and its descendants
133 */
134 guint
135 count_gradient_hrefs(SPObject *o, SPGradient *gr)
136 {
137 if (!o)
138 return 1;
140 guint i = 0;
142 SPStyle *style = SP_OBJECT_STYLE(o);
143 if (style
144 && style->fill.isPaintserver()
145 && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style))
146 && SP_GRADIENT(SP_STYLE_FILL_SERVER(style)) == gr)
147 {
148 i ++;
149 }
150 if (style
151 && style->stroke.isPaintserver()
152 && SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style))
153 && SP_GRADIENT(SP_STYLE_STROKE_SERVER(style)) == gr)
154 {
155 i ++;
156 }
158 for (SPObject *child = sp_object_first_child(o);
159 child != NULL; child = SP_OBJECT_NEXT(child)) {
160 i += count_gradient_hrefs(child, gr);
161 }
163 return i;
164 }
167 /**
168 * If gr has other users, create a new private; also check if gr links to vector, relink if not
169 */
170 SPGradient *
171 sp_gradient_fork_private_if_necessary(SPGradient *gr, SPGradient *vector,
172 SPGradientType type, SPObject *o)
173 {
174 g_return_val_if_fail(gr != NULL, NULL);
175 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
177 // Orphaned gradient, no vector with stops at the end of the line; this used to be an assert
178 // but i think we should not abort on this - maybe just write a validity warning into some sort
179 // of log
180 if (!vector || !SP_GRADIENT_HAS_STOPS(vector))
181 return (gr);
183 // user is the object that uses this gradient; normally it's item but for tspans, we
184 // check its ancestor text so that tspans don't get different gradients from their
185 // texts.
186 SPObject *user = o;
187 while (SP_IS_TSPAN(user)) {
188 user = SP_OBJECT_PARENT(user);
189 }
191 // Check the number of uses of the gradient within this object;
192 // if we are private and there are no other users,
193 if (SP_OBJECT_HREFCOUNT(gr) <= count_gradient_hrefs(user, gr)) {
194 // check vector
195 if ( gr != vector && gr->ref->getObject() != vector ) {
196 /* our href is not the vector, and vector is different from gr; relink */
197 sp_gradient_repr_set_link(SP_OBJECT_REPR(gr), vector);
198 }
199 return gr;
200 }
202 SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
203 SPObject *defs = SP_DOCUMENT_DEFS(doc);
205 if ((gr->has_stops) ||
206 (gr->state != SP_GRADIENT_STATE_UNKNOWN) ||
207 (SP_OBJECT_PARENT(gr) != SP_OBJECT(defs)) ||
208 (SP_OBJECT_HREFCOUNT(gr) > 1)) {
209 // we have to clone a fresh new private gradient for the given vector
211 // create an empty one
212 SPGradient *gr_new = sp_gradient_get_private_normalized(doc, vector, type);
214 // copy all the attributes to it
215 Inkscape::XML::Node *repr_new = SP_OBJECT_REPR(gr_new);
216 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
217 repr_new->setAttribute("gradientUnits", repr->attribute("gradientUnits"));
218 repr_new->setAttribute("gradientTransform", repr->attribute("gradientTransform"));
219 repr_new->setAttribute("spreadMethod", repr->attribute("spreadMethod"));
220 if (SP_IS_RADIALGRADIENT(gr)) {
221 repr_new->setAttribute("cx", repr->attribute("cx"));
222 repr_new->setAttribute("cy", repr->attribute("cy"));
223 repr_new->setAttribute("fx", repr->attribute("fx"));
224 repr_new->setAttribute("fy", repr->attribute("fy"));
225 repr_new->setAttribute("r", repr->attribute("r"));
226 } else {
227 repr_new->setAttribute("x1", repr->attribute("x1"));
228 repr_new->setAttribute("y1", repr->attribute("y1"));
229 repr_new->setAttribute("x2", repr->attribute("x2"));
230 repr_new->setAttribute("y2", repr->attribute("y2"));
231 }
233 return gr_new;
234 } else {
235 return gr;
236 }
237 }
239 SPGradient *
240 sp_gradient_fork_vector_if_necessary (SPGradient *gr)
241 {
242 // Some people actually prefer their gradient vectors to be shared...
243 if (prefs_get_int_attribute("options.forkgradientvectors", "value", 1) == 0)
244 return gr;
246 if (SP_OBJECT_HREFCOUNT(gr) > 1) {
247 SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
248 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
250 Inkscape::XML::Node *repr = SP_OBJECT_REPR (gr)->duplicate(xml_doc);
251 SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
252 SPGradient *gr_new = (SPGradient *) doc->getObjectByRepr(repr);
253 gr_new = sp_gradient_ensure_vector_normalized (gr_new);
254 Inkscape::GC::release(repr);
255 return gr_new;
256 }
257 return gr;
258 }
260 /**
261 * Obtain the vector from the gradient. A forked vector will be created and linked to this gradient if another gradient uses it.
262 */
263 SPGradient *
264 sp_gradient_get_forked_vector_if_necessary(SPGradient *gradient, bool force_vector)
265 {
266 SPGradient *vector = sp_gradient_get_vector (gradient, force_vector);
267 vector = sp_gradient_fork_vector_if_necessary (vector);
268 if ( gradient != vector && gradient->ref->getObject() != vector ) {
269 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
270 }
271 return vector;
272 }
275 /**
276 * Convert an item's gradient to userspace _without_ preserving coords, setting them to defaults
277 * instead. No forking or reapplying is done because this is only called for newly created privates.
278 * @return The new gradient.
279 */
280 SPGradient *
281 sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item)
282 {
283 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
285 // calculate the bbox of the item
286 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
287 boost::optional<Geom::Rect> bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
289 if ( !bbox || bbox->isEmpty() )
290 return gr;
292 Geom::Coord const width = bbox->dimensions()[Geom::X];
293 Geom::Coord const height = bbox->dimensions()[Geom::Y];
295 Geom::Point const center = bbox->midpoint();
297 if (SP_IS_RADIALGRADIENT(gr)) {
298 sp_repr_set_svg_double(repr, "cx", center[Geom::X]);
299 sp_repr_set_svg_double(repr, "cy", center[Geom::Y]);
300 sp_repr_set_svg_double(repr, "fx", center[Geom::X]);
301 sp_repr_set_svg_double(repr, "fy", center[Geom::Y]);
302 sp_repr_set_svg_double(repr, "r", width/2);
304 // we want it to be elliptic, not circular
305 Geom::Matrix squeeze = Geom::Translate (-center) *
306 Geom::Scale(1, height/width) *
307 Geom::Translate (center);
309 gr->gradientTransform = squeeze;
310 {
311 gchar *c=sp_svg_transform_write(gr->gradientTransform);
312 SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c);
313 g_free(c);
314 }
315 } else {
316 sp_repr_set_svg_double(repr, "x1", (center - Geom::Point(width/2, 0))[Geom::X]);
317 sp_repr_set_svg_double(repr, "y1", (center - Geom::Point(width/2, 0))[Geom::Y]);
318 sp_repr_set_svg_double(repr, "x2", (center + Geom::Point(width/2, 0))[Geom::X]);
319 sp_repr_set_svg_double(repr, "y2", (center + Geom::Point(width/2, 0))[Geom::Y]);
320 }
322 // set the gradientUnits
323 repr->setAttribute("gradientUnits", "userSpaceOnUse");
325 return gr;
326 }
328 /**
329 * Convert an item's gradient to userspace if necessary, also fork it if necessary.
330 * @return The new gradient.
331 */
332 SPGradient *
333 sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *property)
334 {
335 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
337 // First, fork it if it is shared
338 gr = sp_gradient_fork_private_if_necessary(gr, sp_gradient_get_vector(gr, FALSE),
339 SP_IS_RADIALGRADIENT(gr) ? SP_GRADIENT_TYPE_RADIAL : SP_GRADIENT_TYPE_LINEAR, SP_OBJECT(item));
341 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
343 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
345 // calculate the bbox of the item
346 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
347 Geom::Matrix bbox2user;
348 boost::optional<Geom::Rect> bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
349 if ( bbox && !bbox->isEmpty() ) {
350 bbox2user = Geom::Matrix(bbox->dimensions()[Geom::X], 0,
351 0, bbox->dimensions()[Geom::Y],
352 bbox->min()[Geom::X], bbox->min()[Geom::Y]);
353 } else {
354 // would be degenerate otherwise
355 bbox2user = Geom::identity();
356 }
358 /* skew is the additional transform, defined by the proportions of the item, that we need
359 * to apply to the gradient in order to work around this weird bit from SVG 1.1
360 * (http://www.w3.org/TR/SVG11/pservers.html#LinearGradients):
361 *
362 * When gradientUnits="objectBoundingBox" and gradientTransform is the identity
363 * matrix, the stripes of the linear gradient are perpendicular to the gradient
364 * vector in object bounding box space (i.e., the abstract coordinate system where
365 * (0,0) is at the top/left of the object bounding box and (1,1) is at the
366 * bottom/right of the object bounding box). When the object's bounding box is not
367 * square, the stripes that are conceptually perpendicular to the gradient vector
368 * within object bounding box space will render non-perpendicular relative to the
369 * gradient vector in user space due to application of the non-uniform scaling
370 * transformation from bounding box space to user space.
371 */
372 Geom::Matrix skew = bbox2user;
373 double exp = skew.descrim();
374 skew[0] /= exp;
375 skew[1] /= exp;
376 skew[2] /= exp;
377 skew[3] /= exp;
378 skew[4] = 0;
379 skew[5] = 0;
381 // apply skew to the gradient
382 gr->gradientTransform = skew;
383 {
384 gchar *c=sp_svg_transform_write(gr->gradientTransform);
385 SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c);
386 g_free(c);
387 }
389 // Matrix to convert points to userspace coords; postmultiply by inverse of skew so
390 // as to cancel it out when it's applied to the gradient during rendering
391 Geom::Matrix point_convert = bbox2user * skew.inverse();
393 if (SP_IS_RADIALGRADIENT(gr)) {
394 SPRadialGradient *rg = SP_RADIALGRADIENT(gr);
396 // original points in the bbox coords
397 Geom::Point c_b = Geom::Point(rg->cx.computed, rg->cy.computed);
398 Geom::Point f_b = Geom::Point(rg->fx.computed, rg->fy.computed);
399 double r_b = rg->r.computed;
401 // converted points in userspace coords
402 Geom::Point c_u = c_b * point_convert;
403 Geom::Point f_u = f_b * point_convert;
404 double r_u = r_b * point_convert.descrim();
406 sp_repr_set_svg_double(repr, "cx", c_u[Geom::X]);
407 sp_repr_set_svg_double(repr, "cy", c_u[Geom::Y]);
408 sp_repr_set_svg_double(repr, "fx", f_u[Geom::X]);
409 sp_repr_set_svg_double(repr, "fy", f_u[Geom::Y]);
410 sp_repr_set_svg_double(repr, "r", r_u);
412 } else {
413 SPLinearGradient *lg = SP_LINEARGRADIENT(gr);
415 Geom::Point p1_b = Geom::Point(lg->x1.computed, lg->y1.computed);
416 Geom::Point p2_b = Geom::Point(lg->x2.computed, lg->y2.computed);
418 Geom::Point p1_u = p1_b * point_convert;
419 Geom::Point p2_u = p2_b * point_convert;
421 sp_repr_set_svg_double(repr, "x1", p1_u[Geom::X]);
422 sp_repr_set_svg_double(repr, "y1", p1_u[Geom::Y]);
423 sp_repr_set_svg_double(repr, "x2", p2_u[Geom::X]);
424 sp_repr_set_svg_double(repr, "y2", p2_u[Geom::Y]);
425 }
427 // set the gradientUnits
428 repr->setAttribute("gradientUnits", "userSpaceOnUse");
429 }
431 // apply the gradient to the item (may be necessary if we forked it); not recursive
432 // generally because grouped items will be taken care of later (we're being called
433 // from sp_item_adjust_paint_recursive); however text and all its children should all
434 // refer to one gradient, hence the recursive call for text (because we can't/don't
435 // want to access tspans and set gradients on them separately)
436 if (SP_IS_TEXT(item))
437 sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), true);
438 else
439 sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), false);
441 return gr;
442 }
444 void
445 sp_gradient_transform_multiply(SPGradient *gradient, NR::Matrix postmul, bool set)
446 {
447 if (set) {
448 gradient->gradientTransform = postmul;
449 } else {
450 gradient->gradientTransform *= postmul; // fixme: get gradient transform by climbing to hrefs?
451 }
452 gradient->gradientTransform_set = TRUE;
454 gchar *c=sp_svg_transform_write(gradient->gradientTransform);
455 SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", c);
456 g_free(c);
457 }
459 SPGradient *
460 sp_item_gradient (SPItem *item, bool fill_or_stroke)
461 {
462 SPStyle *style = SP_OBJECT_STYLE (item);
463 SPGradient *gradient = NULL;
465 if (fill_or_stroke) {
466 if (style && (style->fill.isPaintserver())) {
467 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
468 if (SP_IS_GRADIENT (server)) {
469 gradient = SP_GRADIENT (server);
470 }
471 }
472 } else {
473 if (style && (style->stroke.isPaintserver())) {
474 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
475 if (SP_IS_GRADIENT (server)) {
476 gradient = SP_GRADIENT (server);
477 }
478 }
479 }
481 return gradient;
482 }
485 SPStop*
486 sp_first_stop(SPGradient *gradient)
487 {
488 for (SPObject *ochild = sp_object_first_child(gradient); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
489 if (SP_IS_STOP (ochild))
490 return SP_STOP(ochild);
491 }
492 return NULL;
493 }
495 SPStop*
496 sp_prev_stop(SPStop *stop, SPGradient *gradient)
497 {
498 if (sp_object_first_child(SP_OBJECT(gradient)) == SP_OBJECT(stop))
499 return NULL;
500 SPObject *found = NULL;
501 for ( SPObject *ochild = sp_object_first_child(SP_OBJECT(gradient)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) {
502 if (SP_IS_STOP (ochild)) {
503 found = ochild;
504 }
505 if (SP_OBJECT_NEXT(ochild) == SP_OBJECT(stop) || SP_OBJECT(ochild) == SP_OBJECT(stop)) {
506 break;
507 }
508 }
509 return SP_STOP(found);
510 }
512 SPStop*
513 sp_next_stop(SPStop *stop)
514 {
515 for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
516 if (SP_IS_STOP (ochild))
517 return SP_STOP(ochild);
518 }
519 return NULL;
520 }
522 SPStop*
523 sp_last_stop(SPGradient *gradient)
524 {
525 for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
526 if (sp_next_stop (stop) == NULL)
527 return stop;
528 }
529 return NULL;
530 }
532 guint
533 sp_number_of_stops(SPGradient *gradient)
534 {
535 guint n = 0;
536 for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
537 if (sp_next_stop (stop) == NULL)
538 return n;
539 n ++;
540 }
541 return n;
542 }
544 guint
545 sp_number_of_stops_before_stop(SPGradient *gradient, SPStop *target)
546 {
547 guint n = 0;
548 for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
549 if (stop == target)
550 return n;
551 n ++;
552 }
553 return n;
554 }
557 SPStop*
558 sp_get_stop_i(SPGradient *gradient, guint stop_i)
559 {
560 SPStop *stop = sp_first_stop (gradient);
562 // if this is valid but weird gradient without an offset-zero stop element,
563 // inkscape has created a handle for the start of gradient anyway,
564 // so when it asks for stop N that corresponds to stop element N-1
565 if (stop->offset != 0)
566 stop_i --;
568 for (guint i=0; i < stop_i; i++) {
569 if (!stop) return NULL;
570 stop = sp_next_stop (stop);
571 }
573 return stop;
574 }
576 guint32
577 average_color (guint32 c1, guint32 c2, gdouble p)
578 {
579 guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
580 guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
581 guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
582 guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
584 return SP_RGBA32_U_COMPOSE (r, g, b, a);
585 }
587 SPStop *
588 sp_vector_add_stop (SPGradient *vector, SPStop* prev_stop, SPStop* next_stop, gfloat offset)
589 {
590 Inkscape::XML::Node *new_stop_repr = NULL;
591 new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
592 SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
594 SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
595 newstop->offset = offset;
596 sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
597 guint32 const c1 = sp_stop_get_rgba32(prev_stop);
598 guint32 const c2 = sp_stop_get_rgba32(next_stop);
599 guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
600 Inkscape::CSSOStringStream os;
601 gchar c[64];
602 sp_svg_write_color (c, sizeof(c), cnew);
603 gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
604 os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
605 SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
606 Inkscape::GC::release(new_stop_repr);
608 return newstop;
609 }
611 void
612 sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
613 {
614 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
616 if (!gradient || !SP_IS_GRADIENT(gradient))
617 return;
619 SPGradient *vector = sp_gradient_get_vector (gradient, false);
620 switch (point_type) {
621 case POINT_LG_BEGIN:
622 case POINT_RG_CENTER:
623 case POINT_RG_FOCUS:
624 {
625 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_first_stop (vector));
626 gtk_widget_show (dialog);
627 }
628 break;
630 case POINT_LG_END:
631 case POINT_RG_R1:
632 case POINT_RG_R2:
633 {
634 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_last_stop (vector));
635 gtk_widget_show (dialog);
636 }
637 break;
639 case POINT_LG_MID:
640 case POINT_RG_MID1:
641 case POINT_RG_MID2:
642 {
643 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_get_stop_i (vector, point_i));
644 gtk_widget_show (dialog);
645 }
646 break;
647 default:
648 break;
649 }
650 }
652 guint32
653 sp_item_gradient_stop_query_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
654 {
655 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
657 if (!gradient || !SP_IS_GRADIENT(gradient))
658 return 0;
660 SPGradient *vector = sp_gradient_get_vector (gradient, false);
662 if (!vector) // orphan!
663 return 0; // what else to do?
665 switch (point_type) {
666 case POINT_LG_BEGIN:
667 case POINT_RG_CENTER:
668 case POINT_RG_FOCUS:
669 {
670 SPStop *first = sp_first_stop (vector);
671 if (first) {
672 return sp_stop_get_rgba32(first);
673 }
674 }
675 break;
677 case POINT_LG_END:
678 case POINT_RG_R1:
679 case POINT_RG_R2:
680 {
681 SPStop *last = sp_last_stop (vector);
682 if (last) {
683 return sp_stop_get_rgba32(last);
684 }
685 }
686 break;
688 case POINT_LG_MID:
689 case POINT_RG_MID1:
690 case POINT_RG_MID2:
691 {
692 SPStop *stopi = sp_get_stop_i (vector, point_i);
693 if (stopi) {
694 return sp_stop_get_rgba32(stopi);
695 }
696 }
697 break;
699 default:
700 break;
701 }
702 return 0;
703 }
705 void
706 sp_item_gradient_stop_set_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, SPCSSAttr *stop)
707 {
708 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
710 if (!gradient || !SP_IS_GRADIENT(gradient))
711 return;
713 SPGradient *vector = sp_gradient_get_vector (gradient, false);
715 if (!vector) // orphan!
716 return;
718 vector = sp_gradient_fork_vector_if_necessary (vector);
719 if ( gradient != vector && gradient->ref->getObject() != vector ) {
720 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
721 }
723 switch (point_type) {
724 case POINT_LG_BEGIN:
725 case POINT_RG_CENTER:
726 case POINT_RG_FOCUS:
727 {
728 SPStop *first = sp_first_stop (vector);
729 if (first) {
730 sp_repr_css_change (SP_OBJECT_REPR (first), stop, "style");
731 }
732 }
733 break;
735 case POINT_LG_END:
736 case POINT_RG_R1:
737 case POINT_RG_R2:
738 {
739 SPStop *last = sp_last_stop (vector);
740 if (last) {
741 sp_repr_css_change (SP_OBJECT_REPR (last), stop, "style");
742 }
743 }
744 break;
746 case POINT_LG_MID:
747 case POINT_RG_MID1:
748 case POINT_RG_MID2:
749 {
750 SPStop *stopi = sp_get_stop_i (vector, point_i);
751 if (stopi) {
752 sp_repr_css_change (SP_OBJECT_REPR (stopi), stop, "style");
753 }
754 }
755 break;
757 default:
758 break;
759 }
760 }
762 void
763 sp_item_gradient_reverse_vector (SPItem *item, bool fill_or_stroke)
764 {
765 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
766 if (!gradient || !SP_IS_GRADIENT(gradient))
767 return;
769 SPGradient *vector = sp_gradient_get_vector (gradient, false);
770 if (!vector) // orphan!
771 return;
773 vector = sp_gradient_fork_vector_if_necessary (vector);
774 if ( gradient != vector && gradient->ref->getObject() != vector ) {
775 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
776 }
778 GSList *child_reprs = NULL;
779 GSList *child_objects = NULL;
780 std::vector<double> offsets;
781 for (SPObject *child = sp_object_first_child(vector);
782 child != NULL; child = SP_OBJECT_NEXT(child)) {
783 child_reprs = g_slist_prepend (child_reprs, SP_OBJECT_REPR(child));
784 child_objects = g_slist_prepend (child_objects, child);
785 offsets.push_back(sp_repr_get_double_attribute(SP_OBJECT_REPR(child), "offset", 0));
786 }
788 GSList *child_copies = NULL;
789 for (GSList *i = child_reprs; i != NULL; i = i->next) {
790 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) i->data;
791 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(vector)->document();
792 child_copies = g_slist_append (child_copies, repr->duplicate(xml_doc));
793 }
796 for (GSList *i = child_objects; i != NULL; i = i->next) {
797 SPObject *child = SP_OBJECT (i->data);
798 child->deleteObject();
799 }
801 std::vector<double>::iterator iter = offsets.end() - 1;
802 for (GSList *i = child_copies; i != NULL; i = i->next) {
803 Inkscape::XML::Node *copy = (Inkscape::XML::Node *) i->data;
804 vector->appendChildRepr(copy);
805 sp_repr_set_svg_double (copy, "offset", 1 - *iter);
806 iter --;
807 Inkscape::GC::release(copy);
808 }
810 g_slist_free (child_reprs);
811 g_slist_free (child_copies);
812 g_slist_free (child_objects);
813 }
816 /**
817 Set the position of point point_type of the gradient applied to item (either fill_or_stroke) to
818 p_w (in desktop coordinates). Write_repr if you want the change to become permanent.
819 */
820 void
821 sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::Point p_w, bool fill_or_stroke, bool write_repr, bool scale)
822 {
823 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
825 if (!gradient || !SP_IS_GRADIENT(gradient))
826 return;
828 gradient = sp_gradient_convert_to_userspace (gradient, item, fill_or_stroke? "fill" : "stroke");
830 NR::Matrix i2d (sp_item_i2d_affine (item));
831 NR::Point p = p_w * i2d.inverse();
832 p *= (gradient->gradientTransform).inverse();
833 // now p is in gradient's original coordinates
835 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gradient);
837 if (SP_IS_LINEARGRADIENT(gradient)) {
838 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
839 switch (point_type) {
840 case POINT_LG_BEGIN:
841 if (scale) {
842 lg->x2.computed += (lg->x1.computed - p[NR::X]);
843 lg->y2.computed += (lg->y1.computed - p[NR::Y]);
844 }
845 lg->x1.computed = p[NR::X];
846 lg->y1.computed = p[NR::Y];
847 if (write_repr) {
848 if (scale) {
849 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
850 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
851 }
852 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
853 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
854 } else {
855 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
856 }
857 break;
858 case POINT_LG_END:
859 if (scale) {
860 lg->x1.computed += (lg->x2.computed - p[NR::X]);
861 lg->y1.computed += (lg->y2.computed - p[NR::Y]);
862 }
863 lg->x2.computed = p[NR::X];
864 lg->y2.computed = p[NR::Y];
865 if (write_repr) {
866 if (scale) {
867 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
868 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
869 }
870 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
871 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
872 } else {
873 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
874 }
875 break;
876 case POINT_LG_MID:
877 {
878 // using X-coordinates only to determine the offset, assuming p has been snapped to the vector from begin to end.
879 double offset = get_offset_between_points (p, NR::Point(lg->x1.computed, lg->y1.computed), NR::Point(lg->x2.computed, lg->y2.computed));
880 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (lg, false);
881 sp_gradient_ensure_vector(lg);
882 lg->vector.stops.at(point_i).offset = offset;
883 SPStop* stopi = sp_get_stop_i(vector, point_i);
884 stopi->offset = offset;
885 if (write_repr) {
886 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
887 } else {
888 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
889 }
890 }
891 break;
892 default:
893 break;
894 }
895 } else if (SP_IS_RADIALGRADIENT(gradient)) {
896 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
897 NR::Point c (rg->cx.computed, rg->cy.computed);
898 NR::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
899 if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && NR::L2 (p_w - c_w) < 1e-3) {
900 // prevent setting a radius too close to the center
901 return;
902 }
903 NR::Matrix new_transform;
904 bool transform_set = false;
906 switch (point_type) {
907 case POINT_RG_CENTER:
908 rg->fx.computed = p[NR::X] + (rg->fx.computed - rg->cx.computed);
909 rg->fy.computed = p[NR::Y] + (rg->fy.computed - rg->cy.computed);
910 rg->cx.computed = p[NR::X];
911 rg->cy.computed = p[NR::Y];
912 if (write_repr) {
913 sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
914 sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
915 sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
916 sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
917 } else {
918 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
919 }
920 break;
921 case POINT_RG_FOCUS:
922 rg->fx.computed = p[NR::X];
923 rg->fy.computed = p[NR::Y];
924 if (write_repr) {
925 sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
926 sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
927 } else {
928 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
929 }
930 break;
931 case POINT_RG_R1:
932 {
933 NR::Point r1_w = (c + NR::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
934 double r1_angle = NR::atan2(r1_w - c_w);
935 double move_angle = NR::atan2(p_w - c_w) - r1_angle;
936 double move_stretch = NR::L2(p_w - c_w) / NR::L2(r1_w - c_w);
938 NR::Matrix move = NR::Matrix (Geom::Translate (-c_w)) *
939 NR::Matrix (Geom::Rotate(-r1_angle)) *
940 NR::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
941 NR::Matrix (Geom::Rotate(r1_angle)) *
942 NR::Matrix (Geom::Rotate(move_angle)) *
943 NR::Matrix (Geom::Translate (c_w));
945 new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
946 transform_set = true;
948 break;
949 }
950 case POINT_RG_R2:
951 {
952 NR::Point r2_w = (c + NR::Point(0, -rg->r.computed)) * gradient->gradientTransform * i2d;
953 double r2_angle = NR::atan2(r2_w - c_w);
954 double move_angle = NR::atan2(p_w - c_w) - r2_angle;
955 double move_stretch = NR::L2(p_w - c_w) / NR::L2(r2_w - c_w);
957 NR::Matrix move = NR::Matrix (Geom::Translate (-c_w)) *
958 NR::Matrix (Geom::Rotate(-r2_angle)) *
959 NR::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
960 NR::Matrix (Geom::Rotate(r2_angle)) *
961 NR::Matrix (Geom::Rotate(move_angle)) *
962 NR::Matrix (Geom::Translate (c_w));
964 new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
965 transform_set = true;
967 break;
968 }
969 case POINT_RG_MID1:
970 {
971 NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed);
972 NR::Point end = NR::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
973 double offset = get_offset_between_points (p, start, end);
974 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (rg, false);
975 sp_gradient_ensure_vector(rg);
976 rg->vector.stops.at(point_i).offset = offset;
977 SPStop* stopi = sp_get_stop_i(vector, point_i);
978 stopi->offset = offset;
979 if (write_repr) {
980 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
981 } else {
982 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
983 }
984 break;
985 }
986 case POINT_RG_MID2:
987 NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed);
988 NR::Point end = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
989 double offset = get_offset_between_points (p, start, end);
990 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(rg, false);
991 sp_gradient_ensure_vector(rg);
992 rg->vector.stops.at(point_i).offset = offset;
993 SPStop* stopi = sp_get_stop_i(vector, point_i);
994 stopi->offset = offset;
995 if (write_repr) {
996 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
997 } else {
998 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
999 }
1000 break;
1001 }
1002 if (transform_set) {
1003 gradient->gradientTransform = new_transform;
1004 gradient->gradientTransform_set = TRUE;
1005 if (write_repr) {
1006 gchar *s=sp_svg_transform_write(gradient->gradientTransform);
1007 SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", s);
1008 g_free(s);
1009 } else {
1010 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1011 }
1012 }
1013 }
1014 }
1016 SPGradient *
1017 sp_item_gradient_get_vector (SPItem *item, bool fill_or_stroke)
1018 {
1019 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1021 if (gradient)
1022 return sp_gradient_get_vector (gradient, false);
1023 return NULL;
1024 }
1026 SPGradientSpread
1027 sp_item_gradient_get_spread (SPItem *item, bool fill_or_stroke)
1028 {
1029 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1031 if (gradient)
1032 return sp_gradient_get_spread (gradient);
1033 return SP_GRADIENT_SPREAD_PAD;
1034 }
1037 /**
1038 Returns the position of point point_type of the gradient applied to item (either fill_or_stroke),
1039 in desktop coordinates.
1040 */
1042 NR::Point
1043 sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1044 {
1045 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1047 Geom::Point p (0, 0);
1049 if (!gradient)
1050 return from_2geom(p);
1052 if (SP_IS_LINEARGRADIENT(gradient)) {
1053 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
1054 switch (point_type) {
1055 case POINT_LG_BEGIN:
1056 p = NR::Point (lg->x1.computed, lg->y1.computed);
1057 break;
1058 case POINT_LG_END:
1059 p = NR::Point (lg->x2.computed, lg->y2.computed);
1060 break;
1061 case POINT_LG_MID:
1062 {
1063 gdouble offset = lg->vector.stops.at(point_i).offset;
1064 p = (1-offset) * Geom::Point(lg->x1.computed, lg->y1.computed) + offset * Geom::Point(lg->x2.computed, lg->y2.computed);
1065 }
1066 break;
1067 }
1068 } else if (SP_IS_RADIALGRADIENT(gradient)) {
1069 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
1070 switch (point_type) {
1071 case POINT_RG_CENTER:
1072 p = Geom::Point (rg->cx.computed, rg->cy.computed);
1073 break;
1074 case POINT_RG_FOCUS:
1075 p = Geom::Point (rg->fx.computed, rg->fy.computed);
1076 break;
1077 case POINT_RG_R1:
1078 p = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
1079 break;
1080 case POINT_RG_R2:
1081 p = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
1082 break;
1083 case POINT_RG_MID1:
1084 {
1085 gdouble offset = rg->vector.stops.at(point_i).offset;
1086 p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed + rg->r.computed, rg->cy.computed);
1087 }
1088 break;
1089 case POINT_RG_MID2:
1090 {
1091 gdouble offset = rg->vector.stops.at(point_i).offset;
1092 p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed, rg->cy.computed - rg->r.computed);
1093 }
1094 break;
1095 }
1096 }
1098 if (SP_GRADIENT(gradient)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1099 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
1100 boost::optional<Geom::Rect> bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
1101 if (bbox) {
1102 p *= Geom::Matrix(bbox->dimensions()[Geom::X], 0,
1103 0, bbox->dimensions()[Geom::Y],
1104 bbox->min()[Geom::X], bbox->min()[Geom::Y]);
1105 }
1106 }
1107 p *= Geom::Matrix(gradient->gradientTransform) * (Geom::Matrix)sp_item_i2d_affine(item);
1108 return from_2geom(p);
1109 }
1112 /**
1113 * Sets item fill or stroke to the gradient of the specified type with given vector, creating
1114 * new private gradient, if needed.
1115 * gr has to be a normalized vector.
1116 */
1118 SPGradient *
1119 sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_fill)
1120 {
1121 g_return_val_if_fail(item != NULL, NULL);
1122 g_return_val_if_fail(SP_IS_ITEM(item), NULL);
1123 g_return_val_if_fail(gr != NULL, NULL);
1124 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
1125 g_return_val_if_fail(gr->state == SP_GRADIENT_STATE_VECTOR, NULL);
1127 SPStyle *style = SP_OBJECT_STYLE(item);
1128 g_assert(style != NULL);
1130 SPPaintServer *ps = NULL;
1131 if (is_fill? style->fill.isPaintserver() : style->stroke.isPaintserver())
1132 ps = is_fill? SP_STYLE_FILL_SERVER(style) : SP_STYLE_STROKE_SERVER(style);
1134 if (ps
1135 && ( (type == SP_GRADIENT_TYPE_LINEAR && SP_IS_LINEARGRADIENT(ps)) ||
1136 (type == SP_GRADIENT_TYPE_RADIAL && SP_IS_RADIALGRADIENT(ps)) ) )
1137 {
1139 /* Current fill style is the gradient of the required type */
1140 SPGradient *current = SP_GRADIENT(ps);
1142 //g_print("hrefcount %d count %d\n", SP_OBJECT_HREFCOUNT(ig), count_gradient_hrefs(SP_OBJECT(item), ig));
1144 if (SP_OBJECT_HREFCOUNT(current) == 1 ||
1145 SP_OBJECT_HREFCOUNT(current) == count_gradient_hrefs(SP_OBJECT(item), current)) {
1147 // current is private and it's either used once, or all its uses are by children of item;
1148 // so just change its href to vector
1150 if ( current != gr && sp_gradient_get_vector(current, false) != gr ) {
1151 /* href is not the vector */
1152 sp_gradient_repr_set_link(SP_OBJECT_REPR(current), gr);
1153 }
1154 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1155 return current;
1157 } else {
1159 // the gradient is not private, or it is shared with someone else;
1160 // normalize it (this includes creating new private if necessary)
1161 SPGradient *normalized = sp_gradient_fork_private_if_necessary(current, gr, type, item);
1163 g_return_val_if_fail(normalized != NULL, NULL);
1165 if (normalized != current) {
1167 /* We have to change object style here; recursive because this is used from
1168 * fill&stroke and must work for groups etc. */
1169 sp_style_set_property_url(SP_OBJECT(item), is_fill? "fill" : "stroke", SP_OBJECT(normalized), true);
1170 }
1171 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1172 return normalized;
1173 }
1175 } else {
1176 /* Current fill style is not a gradient or wrong type, so construct everything */
1177 SPGradient *constructed = sp_gradient_get_private_normalized(SP_OBJECT_DOCUMENT(item), gr, type);
1178 constructed = sp_gradient_reset_to_userspace(constructed, item);
1179 sp_style_set_property_url(SP_OBJECT(item), ( is_fill ? "fill" : "stroke" ), SP_OBJECT(constructed), true);
1180 SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
1181 SP_OBJECT_STYLE_MODIFIED_FLAG ));
1182 return constructed;
1183 }
1184 }
1186 static void
1187 sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *link)
1188 {
1189 g_return_if_fail(repr != NULL);
1190 g_return_if_fail(link != NULL);
1191 g_return_if_fail(SP_IS_GRADIENT(link));
1193 gchar *ref;
1194 if (link) {
1195 gchar const *id = SP_OBJECT_ID(link);
1196 size_t const len = strlen(id);
1197 ref = (gchar*) alloca(len + 2);
1198 *ref = '#';
1199 memcpy(ref + 1, id, len + 1);
1200 } else {
1201 ref = NULL;
1202 }
1204 repr->setAttribute("xlink:href", ref);
1205 }
1207 /*
1208 * Get default normalized gradient vector of document, create if there is none
1209 */
1211 SPGradient *
1212 sp_document_default_gradient_vector(SPDocument *document, guint32 color)
1213 {
1214 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
1215 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
1217 Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient");
1219 repr->setAttribute("inkscape:collect", "always");
1220 // set here, but removed when it's edited in the gradient editor
1221 // to further reduce clutter, we could
1222 // (1) here, search gradients by color and return what is found without duplication
1223 // (2) in fill & stroke, show only one copy of each gradient in list
1225 Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
1227 gchar b[64];
1228 sp_svg_write_color(b, sizeof(b), color);
1230 {
1231 gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:1;", b);
1232 stop->setAttribute("style", t);
1233 g_free(t);
1234 }
1236 stop->setAttribute("offset", "0");
1238 repr->appendChild(stop);
1239 Inkscape::GC::release(stop);
1241 stop = xml_doc->createElement("svg:stop");
1243 {
1244 gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:0;", b);
1245 stop->setAttribute("style", t);
1246 g_free(t);
1247 }
1249 stop->setAttribute("offset", "1");
1251 repr->appendChild(stop);
1252 Inkscape::GC::release(stop);
1254 SP_OBJECT_REPR(defs)->addChild(repr, NULL);
1255 Inkscape::GC::release(repr);
1257 /* fixme: This does not look like nice */
1258 SPGradient *gr;
1259 gr = (SPGradient *) document->getObjectByRepr(repr);
1260 g_assert(gr != NULL);
1261 g_assert(SP_IS_GRADIENT(gr));
1262 /* fixme: Maybe add extra sanity check here */
1263 gr->state = SP_GRADIENT_STATE_VECTOR;
1265 return gr;
1266 }
1268 /**
1269 Return the preferred vector for \a o, made from (in order of preference) its current vector,
1270 current fill or stroke color, or from desktop style if \a o is NULL or doesn't have style.
1271 */
1272 SPGradient *
1273 sp_gradient_vector_for_object(SPDocument *const doc, SPDesktop *const desktop,
1274 SPObject *const o, bool const is_fill)
1275 {
1276 guint32 rgba = 0;
1277 if (o == NULL || SP_OBJECT_STYLE(o) == NULL) {
1278 rgba = sp_desktop_get_color(desktop, is_fill);
1279 } else {
1280 // take the color of the object
1281 SPStyle const &style = *SP_OBJECT_STYLE(o);
1282 SPIPaint const &paint = ( is_fill
1283 ? style.fill
1284 : style.stroke );
1285 if (paint.isPaintserver()) {
1286 SPObject *server = is_fill? SP_OBJECT_STYLE_FILL_SERVER(o) : SP_OBJECT_STYLE_STROKE_SERVER(o);
1287 if (SP_IS_GRADIENT (server)) {
1288 return sp_gradient_get_vector(SP_GRADIENT (server), TRUE);
1289 } else {
1290 rgba = sp_desktop_get_color(desktop, is_fill);
1291 }
1292 } else if (paint.isColor()) {
1293 rgba = paint.value.color.toRGBA32( 0xff );
1294 } else {
1295 // if o doesn't use flat color, then take current color of the desktop.
1296 rgba = sp_desktop_get_color(desktop, is_fill);
1297 }
1298 }
1300 return sp_document_default_gradient_vector(doc, rgba);
1301 }
1304 /*
1305 Local Variables:
1306 mode:c++
1307 c-file-style:"stroustrup"
1308 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1309 indent-tabs-mode:nil
1310 fill-column:99
1311 End:
1312 */
1313 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :