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 "preferences.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 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
244 if (!prefs->getBool("/options/forkgradientvectors/value", true))
245 return gr;
247 if (SP_OBJECT_HREFCOUNT(gr) > 1) {
248 SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
249 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
251 Inkscape::XML::Node *repr = SP_OBJECT_REPR (gr)->duplicate(xml_doc);
252 SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
253 SPGradient *gr_new = (SPGradient *) doc->getObjectByRepr(repr);
254 gr_new = sp_gradient_ensure_vector_normalized (gr_new);
255 Inkscape::GC::release(repr);
256 return gr_new;
257 }
258 return gr;
259 }
261 /**
262 * Obtain the vector from the gradient. A forked vector will be created and linked to this gradient if another gradient uses it.
263 */
264 SPGradient *
265 sp_gradient_get_forked_vector_if_necessary(SPGradient *gradient, bool force_vector)
266 {
267 SPGradient *vector = sp_gradient_get_vector (gradient, force_vector);
268 vector = sp_gradient_fork_vector_if_necessary (vector);
269 if ( gradient != vector && gradient->ref->getObject() != vector ) {
270 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
271 }
272 return vector;
273 }
276 /**
277 * Convert an item's gradient to userspace _without_ preserving coords, setting them to defaults
278 * instead. No forking or reapplying is done because this is only called for newly created privates.
279 * @return The new gradient.
280 */
281 SPGradient *
282 sp_gradient_reset_to_userspace (SPGradient *gr, SPItem *item)
283 {
284 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
286 // calculate the bbox of the item
287 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
288 Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
290 if (!bbox)
291 return gr;
293 Geom::Coord const width = bbox->dimensions()[Geom::X];
294 Geom::Coord const height = bbox->dimensions()[Geom::Y];
296 Geom::Point const center = bbox->midpoint();
298 if (SP_IS_RADIALGRADIENT(gr)) {
299 sp_repr_set_svg_double(repr, "cx", center[Geom::X]);
300 sp_repr_set_svg_double(repr, "cy", center[Geom::Y]);
301 sp_repr_set_svg_double(repr, "fx", center[Geom::X]);
302 sp_repr_set_svg_double(repr, "fy", center[Geom::Y]);
303 sp_repr_set_svg_double(repr, "r", width/2);
305 // we want it to be elliptic, not circular
306 Geom::Matrix squeeze = Geom::Translate (-center) *
307 Geom::Scale(1, height/width) *
308 Geom::Translate (center);
310 gr->gradientTransform = squeeze;
311 {
312 gchar *c=sp_svg_transform_write(gr->gradientTransform);
313 SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c);
314 g_free(c);
315 }
316 } else {
317 sp_repr_set_svg_double(repr, "x1", (center - Geom::Point(width/2, 0))[Geom::X]);
318 sp_repr_set_svg_double(repr, "y1", (center - Geom::Point(width/2, 0))[Geom::Y]);
319 sp_repr_set_svg_double(repr, "x2", (center + Geom::Point(width/2, 0))[Geom::X]);
320 sp_repr_set_svg_double(repr, "y2", (center + Geom::Point(width/2, 0))[Geom::Y]);
321 }
323 // set the gradientUnits
324 repr->setAttribute("gradientUnits", "userSpaceOnUse");
326 return gr;
327 }
329 /**
330 * Convert an item's gradient to userspace if necessary, also fork it if necessary.
331 * @return The new gradient.
332 */
333 SPGradient *
334 sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *property)
335 {
336 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
338 // First, fork it if it is shared
339 gr = sp_gradient_fork_private_if_necessary(gr, sp_gradient_get_vector(gr, FALSE),
340 SP_IS_RADIALGRADIENT(gr) ? SP_GRADIENT_TYPE_RADIAL : SP_GRADIENT_TYPE_LINEAR, SP_OBJECT(item));
342 if (gr->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
344 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
346 // calculate the bbox of the item
347 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
348 Geom::Matrix bbox2user;
349 Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
350 if ( bbox ) {
351 bbox2user = Geom::Matrix(bbox->dimensions()[Geom::X], 0,
352 0, bbox->dimensions()[Geom::Y],
353 bbox->min()[Geom::X], bbox->min()[Geom::Y]);
354 } else {
355 // would be degenerate otherwise
356 bbox2user = Geom::identity();
357 }
359 /* skew is the additional transform, defined by the proportions of the item, that we need
360 * to apply to the gradient in order to work around this weird bit from SVG 1.1
361 * (http://www.w3.org/TR/SVG11/pservers.html#LinearGradients):
362 *
363 * When gradientUnits="objectBoundingBox" and gradientTransform is the identity
364 * matrix, the stripes of the linear gradient are perpendicular to the gradient
365 * vector in object bounding box space (i.e., the abstract coordinate system where
366 * (0,0) is at the top/left of the object bounding box and (1,1) is at the
367 * bottom/right of the object bounding box). When the object's bounding box is not
368 * square, the stripes that are conceptually perpendicular to the gradient vector
369 * within object bounding box space will render non-perpendicular relative to the
370 * gradient vector in user space due to application of the non-uniform scaling
371 * transformation from bounding box space to user space.
372 */
373 Geom::Matrix skew = bbox2user;
374 double exp = skew.descrim();
375 skew[0] /= exp;
376 skew[1] /= exp;
377 skew[2] /= exp;
378 skew[3] /= exp;
379 skew[4] = 0;
380 skew[5] = 0;
382 // apply skew to the gradient
383 gr->gradientTransform = skew;
384 {
385 gchar *c=sp_svg_transform_write(gr->gradientTransform);
386 SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c);
387 g_free(c);
388 }
390 // Matrix to convert points to userspace coords; postmultiply by inverse of skew so
391 // as to cancel it out when it's applied to the gradient during rendering
392 Geom::Matrix point_convert = bbox2user * skew.inverse();
394 if (SP_IS_RADIALGRADIENT(gr)) {
395 SPRadialGradient *rg = SP_RADIALGRADIENT(gr);
397 // original points in the bbox coords
398 Geom::Point c_b = Geom::Point(rg->cx.computed, rg->cy.computed);
399 Geom::Point f_b = Geom::Point(rg->fx.computed, rg->fy.computed);
400 double r_b = rg->r.computed;
402 // converted points in userspace coords
403 Geom::Point c_u = c_b * point_convert;
404 Geom::Point f_u = f_b * point_convert;
405 double r_u = r_b * point_convert.descrim();
407 sp_repr_set_svg_double(repr, "cx", c_u[Geom::X]);
408 sp_repr_set_svg_double(repr, "cy", c_u[Geom::Y]);
409 sp_repr_set_svg_double(repr, "fx", f_u[Geom::X]);
410 sp_repr_set_svg_double(repr, "fy", f_u[Geom::Y]);
411 sp_repr_set_svg_double(repr, "r", r_u);
413 } else {
414 SPLinearGradient *lg = SP_LINEARGRADIENT(gr);
416 Geom::Point p1_b = Geom::Point(lg->x1.computed, lg->y1.computed);
417 Geom::Point p2_b = Geom::Point(lg->x2.computed, lg->y2.computed);
419 Geom::Point p1_u = p1_b * point_convert;
420 Geom::Point p2_u = p2_b * point_convert;
422 sp_repr_set_svg_double(repr, "x1", p1_u[Geom::X]);
423 sp_repr_set_svg_double(repr, "y1", p1_u[Geom::Y]);
424 sp_repr_set_svg_double(repr, "x2", p2_u[Geom::X]);
425 sp_repr_set_svg_double(repr, "y2", p2_u[Geom::Y]);
426 }
428 // set the gradientUnits
429 repr->setAttribute("gradientUnits", "userSpaceOnUse");
430 }
432 // apply the gradient to the item (may be necessary if we forked it); not recursive
433 // generally because grouped items will be taken care of later (we're being called
434 // from sp_item_adjust_paint_recursive); however text and all its children should all
435 // refer to one gradient, hence the recursive call for text (because we can't/don't
436 // want to access tspans and set gradients on them separately)
437 if (SP_IS_TEXT(item))
438 sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), true);
439 else
440 sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), false);
442 return gr;
443 }
445 void
446 sp_gradient_transform_multiply(SPGradient *gradient, Geom::Matrix postmul, bool set)
447 {
448 if (set) {
449 gradient->gradientTransform = postmul;
450 } else {
451 gradient->gradientTransform *= postmul; // fixme: get gradient transform by climbing to hrefs?
452 }
453 gradient->gradientTransform_set = TRUE;
455 gchar *c=sp_svg_transform_write(gradient->gradientTransform);
456 SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", c);
457 g_free(c);
458 }
460 SPGradient *
461 sp_item_gradient (SPItem *item, bool fill_or_stroke)
462 {
463 SPStyle *style = SP_OBJECT_STYLE (item);
464 SPGradient *gradient = NULL;
466 if (fill_or_stroke) {
467 if (style && (style->fill.isPaintserver())) {
468 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
469 if (SP_IS_GRADIENT (server)) {
470 gradient = SP_GRADIENT (server);
471 }
472 }
473 } else {
474 if (style && (style->stroke.isPaintserver())) {
475 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
476 if (SP_IS_GRADIENT (server)) {
477 gradient = SP_GRADIENT (server);
478 }
479 }
480 }
482 return gradient;
483 }
486 SPStop*
487 sp_first_stop(SPGradient *gradient)
488 {
489 for (SPObject *ochild = sp_object_first_child(gradient); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
490 if (SP_IS_STOP (ochild))
491 return SP_STOP(ochild);
492 }
493 return NULL;
494 }
496 SPStop*
497 sp_prev_stop(SPStop *stop, SPGradient *gradient)
498 {
499 if (sp_object_first_child(SP_OBJECT(gradient)) == SP_OBJECT(stop))
500 return NULL;
501 SPObject *found = NULL;
502 for ( SPObject *ochild = sp_object_first_child(SP_OBJECT(gradient)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) {
503 if (SP_IS_STOP (ochild)) {
504 found = ochild;
505 }
506 if (SP_OBJECT_NEXT(ochild) == SP_OBJECT(stop) || SP_OBJECT(ochild) == SP_OBJECT(stop)) {
507 break;
508 }
509 }
510 return SP_STOP(found);
511 }
513 SPStop*
514 sp_next_stop(SPStop *stop)
515 {
516 for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) {
517 if (SP_IS_STOP (ochild))
518 return SP_STOP(ochild);
519 }
520 return NULL;
521 }
523 SPStop*
524 sp_last_stop(SPGradient *gradient)
525 {
526 for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
527 if (sp_next_stop (stop) == NULL)
528 return stop;
529 }
530 return NULL;
531 }
533 guint
534 sp_number_of_stops(SPGradient *gradient)
535 {
536 guint n = 0;
537 for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
538 if (sp_next_stop (stop) == NULL)
539 return n;
540 n ++;
541 }
542 return n;
543 }
545 guint
546 sp_number_of_stops_before_stop(SPGradient *gradient, SPStop *target)
547 {
548 guint n = 0;
549 for (SPStop *stop = sp_first_stop (gradient); stop != NULL; stop = sp_next_stop (stop)) {
550 if (stop == target)
551 return n;
552 n ++;
553 }
554 return n;
555 }
558 SPStop*
559 sp_get_stop_i(SPGradient *gradient, guint stop_i)
560 {
561 SPStop *stop = sp_first_stop (gradient);
563 // if this is valid but weird gradient without an offset-zero stop element,
564 // inkscape has created a handle for the start of gradient anyway,
565 // so when it asks for stop N that corresponds to stop element N-1
566 if (stop->offset != 0)
567 stop_i --;
569 for (guint i=0; i < stop_i; i++) {
570 if (!stop) return NULL;
571 stop = sp_next_stop (stop);
572 }
574 return stop;
575 }
577 guint32
578 average_color (guint32 c1, guint32 c2, gdouble p)
579 {
580 guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
581 guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
582 guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
583 guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
585 return SP_RGBA32_U_COMPOSE (r, g, b, a);
586 }
588 SPStop *
589 sp_vector_add_stop (SPGradient *vector, SPStop* prev_stop, SPStop* next_stop, gfloat offset)
590 {
591 Inkscape::XML::Node *new_stop_repr = NULL;
592 new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
593 SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
595 SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
596 newstop->offset = offset;
597 sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
598 guint32 const c1 = sp_stop_get_rgba32(prev_stop);
599 guint32 const c2 = sp_stop_get_rgba32(next_stop);
600 guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
601 Inkscape::CSSOStringStream os;
602 gchar c[64];
603 sp_svg_write_color (c, sizeof(c), cnew);
604 gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
605 os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
606 SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
607 Inkscape::GC::release(new_stop_repr);
609 return newstop;
610 }
612 void
613 sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
614 {
615 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
617 if (!gradient || !SP_IS_GRADIENT(gradient))
618 return;
620 SPGradient *vector = sp_gradient_get_vector (gradient, false);
621 switch (point_type) {
622 case POINT_LG_BEGIN:
623 case POINT_RG_CENTER:
624 case POINT_RG_FOCUS:
625 {
626 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_first_stop (vector));
627 gtk_widget_show (dialog);
628 }
629 break;
631 case POINT_LG_END:
632 case POINT_RG_R1:
633 case POINT_RG_R2:
634 {
635 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_last_stop (vector));
636 gtk_widget_show (dialog);
637 }
638 break;
640 case POINT_LG_MID:
641 case POINT_RG_MID1:
642 case POINT_RG_MID2:
643 {
644 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_get_stop_i (vector, point_i));
645 gtk_widget_show (dialog);
646 }
647 break;
648 default:
649 break;
650 }
651 }
653 guint32
654 sp_item_gradient_stop_query_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
655 {
656 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
658 if (!gradient || !SP_IS_GRADIENT(gradient))
659 return 0;
661 SPGradient *vector = sp_gradient_get_vector (gradient, false);
663 if (!vector) // orphan!
664 return 0; // what else to do?
666 switch (point_type) {
667 case POINT_LG_BEGIN:
668 case POINT_RG_CENTER:
669 case POINT_RG_FOCUS:
670 {
671 SPStop *first = sp_first_stop (vector);
672 if (first) {
673 return sp_stop_get_rgba32(first);
674 }
675 }
676 break;
678 case POINT_LG_END:
679 case POINT_RG_R1:
680 case POINT_RG_R2:
681 {
682 SPStop *last = sp_last_stop (vector);
683 if (last) {
684 return sp_stop_get_rgba32(last);
685 }
686 }
687 break;
689 case POINT_LG_MID:
690 case POINT_RG_MID1:
691 case POINT_RG_MID2:
692 {
693 SPStop *stopi = sp_get_stop_i (vector, point_i);
694 if (stopi) {
695 return sp_stop_get_rgba32(stopi);
696 }
697 }
698 break;
700 default:
701 break;
702 }
703 return 0;
704 }
706 void
707 sp_item_gradient_stop_set_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, SPCSSAttr *stop)
708 {
709 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
711 if (!gradient || !SP_IS_GRADIENT(gradient))
712 return;
714 SPGradient *vector = sp_gradient_get_vector (gradient, false);
716 if (!vector) // orphan!
717 return;
719 vector = sp_gradient_fork_vector_if_necessary (vector);
720 if ( gradient != vector && gradient->ref->getObject() != vector ) {
721 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
722 }
724 switch (point_type) {
725 case POINT_LG_BEGIN:
726 case POINT_RG_CENTER:
727 case POINT_RG_FOCUS:
728 {
729 SPStop *first = sp_first_stop (vector);
730 if (first) {
731 sp_repr_css_change (SP_OBJECT_REPR (first), stop, "style");
732 }
733 }
734 break;
736 case POINT_LG_END:
737 case POINT_RG_R1:
738 case POINT_RG_R2:
739 {
740 SPStop *last = sp_last_stop (vector);
741 if (last) {
742 sp_repr_css_change (SP_OBJECT_REPR (last), stop, "style");
743 }
744 }
745 break;
747 case POINT_LG_MID:
748 case POINT_RG_MID1:
749 case POINT_RG_MID2:
750 {
751 SPStop *stopi = sp_get_stop_i (vector, point_i);
752 if (stopi) {
753 sp_repr_css_change (SP_OBJECT_REPR (stopi), stop, "style");
754 }
755 }
756 break;
758 default:
759 break;
760 }
761 }
763 void
764 sp_item_gradient_reverse_vector (SPItem *item, bool fill_or_stroke)
765 {
766 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
767 if (!gradient || !SP_IS_GRADIENT(gradient))
768 return;
770 SPGradient *vector = sp_gradient_get_vector (gradient, false);
771 if (!vector) // orphan!
772 return;
774 vector = sp_gradient_fork_vector_if_necessary (vector);
775 if ( gradient != vector && gradient->ref->getObject() != vector ) {
776 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
777 }
779 GSList *child_reprs = NULL;
780 GSList *child_objects = NULL;
781 std::vector<double> offsets;
782 for (SPObject *child = sp_object_first_child(vector);
783 child != NULL; child = SP_OBJECT_NEXT(child)) {
784 child_reprs = g_slist_prepend (child_reprs, SP_OBJECT_REPR(child));
785 child_objects = g_slist_prepend (child_objects, child);
786 offsets.push_back(sp_repr_get_double_attribute(SP_OBJECT_REPR(child), "offset", 0));
787 }
789 GSList *child_copies = NULL;
790 for (GSList *i = child_reprs; i != NULL; i = i->next) {
791 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) i->data;
792 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(vector)->document();
793 child_copies = g_slist_append (child_copies, repr->duplicate(xml_doc));
794 }
797 for (GSList *i = child_objects; i != NULL; i = i->next) {
798 SPObject *child = SP_OBJECT (i->data);
799 child->deleteObject();
800 }
802 std::vector<double>::iterator iter = offsets.end() - 1;
803 for (GSList *i = child_copies; i != NULL; i = i->next) {
804 Inkscape::XML::Node *copy = (Inkscape::XML::Node *) i->data;
805 vector->appendChildRepr(copy);
806 sp_repr_set_svg_double (copy, "offset", 1 - *iter);
807 iter --;
808 Inkscape::GC::release(copy);
809 }
811 g_slist_free (child_reprs);
812 g_slist_free (child_copies);
813 g_slist_free (child_objects);
814 }
817 /**
818 Set the position of point point_type of the gradient applied to item (either fill_or_stroke) to
819 p_w (in desktop coordinates). Write_repr if you want the change to become permanent.
820 */
821 void
822 sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, Geom::Point p_w, bool fill_or_stroke, bool write_repr, bool scale)
823 {
824 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
826 if (!gradient || !SP_IS_GRADIENT(gradient))
827 return;
829 gradient = sp_gradient_convert_to_userspace (gradient, item, fill_or_stroke? "fill" : "stroke");
831 Geom::Matrix i2d (sp_item_i2d_affine (item));
832 Geom::Point p = p_w * i2d.inverse();
833 p *= (gradient->gradientTransform).inverse();
834 // now p is in gradient's original coordinates
836 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gradient);
838 if (SP_IS_LINEARGRADIENT(gradient)) {
839 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
840 switch (point_type) {
841 case POINT_LG_BEGIN:
842 if (scale) {
843 lg->x2.computed += (lg->x1.computed - p[Geom::X]);
844 lg->y2.computed += (lg->y1.computed - p[Geom::Y]);
845 }
846 lg->x1.computed = p[Geom::X];
847 lg->y1.computed = p[Geom::Y];
848 if (write_repr) {
849 if (scale) {
850 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
851 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
852 }
853 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
854 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
855 } else {
856 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
857 }
858 break;
859 case POINT_LG_END:
860 if (scale) {
861 lg->x1.computed += (lg->x2.computed - p[Geom::X]);
862 lg->y1.computed += (lg->y2.computed - p[Geom::Y]);
863 }
864 lg->x2.computed = p[Geom::X];
865 lg->y2.computed = p[Geom::Y];
866 if (write_repr) {
867 if (scale) {
868 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
869 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
870 }
871 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
872 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
873 } else {
874 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
875 }
876 break;
877 case POINT_LG_MID:
878 {
879 // using X-coordinates only to determine the offset, assuming p has been snapped to the vector from begin to end.
880 double offset = get_offset_between_points (p, Geom::Point(lg->x1.computed, lg->y1.computed), Geom::Point(lg->x2.computed, lg->y2.computed));
881 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (lg, false);
882 sp_gradient_ensure_vector(lg);
883 lg->vector.stops.at(point_i).offset = offset;
884 SPStop* stopi = sp_get_stop_i(vector, point_i);
885 stopi->offset = offset;
886 if (write_repr) {
887 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
888 } else {
889 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
890 }
891 }
892 break;
893 default:
894 break;
895 }
896 } else if (SP_IS_RADIALGRADIENT(gradient)) {
897 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
898 Geom::Point c (rg->cx.computed, rg->cy.computed);
899 Geom::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
900 if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && Geom::L2 (p_w - c_w) < 1e-3) {
901 // prevent setting a radius too close to the center
902 return;
903 }
904 Geom::Matrix new_transform;
905 bool transform_set = false;
907 switch (point_type) {
908 case POINT_RG_CENTER:
909 rg->fx.computed = p[Geom::X] + (rg->fx.computed - rg->cx.computed);
910 rg->fy.computed = p[Geom::Y] + (rg->fy.computed - rg->cy.computed);
911 rg->cx.computed = p[Geom::X];
912 rg->cy.computed = p[Geom::Y];
913 if (write_repr) {
914 sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
915 sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
916 sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
917 sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
918 } else {
919 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
920 }
921 break;
922 case POINT_RG_FOCUS:
923 rg->fx.computed = p[Geom::X];
924 rg->fy.computed = p[Geom::Y];
925 if (write_repr) {
926 sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
927 sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
928 } else {
929 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
930 }
931 break;
932 case POINT_RG_R1:
933 {
934 Geom::Point r1_w = (c + Geom::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
935 double r1_angle = Geom::atan2(r1_w - c_w);
936 double move_angle = Geom::atan2(p_w - c_w) - r1_angle;
937 double move_stretch = Geom::L2(p_w - c_w) / Geom::L2(r1_w - c_w);
939 Geom::Matrix move = Geom::Matrix (Geom::Translate (-c_w)) *
940 Geom::Matrix (Geom::Rotate(-r1_angle)) *
941 Geom::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
942 Geom::Matrix (Geom::Rotate(r1_angle)) *
943 Geom::Matrix (Geom::Rotate(move_angle)) *
944 Geom::Matrix (Geom::Translate (c_w));
946 new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
947 transform_set = true;
949 break;
950 }
951 case POINT_RG_R2:
952 {
953 Geom::Point r2_w = (c + Geom::Point(0, -rg->r.computed)) * gradient->gradientTransform * i2d;
954 double r2_angle = Geom::atan2(r2_w - c_w);
955 double move_angle = Geom::atan2(p_w - c_w) - r2_angle;
956 double move_stretch = Geom::L2(p_w - c_w) / Geom::L2(r2_w - c_w);
958 Geom::Matrix move = Geom::Matrix (Geom::Translate (-c_w)) *
959 Geom::Matrix (Geom::Rotate(-r2_angle)) *
960 Geom::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
961 Geom::Matrix (Geom::Rotate(r2_angle)) *
962 Geom::Matrix (Geom::Rotate(move_angle)) *
963 Geom::Matrix (Geom::Translate (c_w));
965 new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
966 transform_set = true;
968 break;
969 }
970 case POINT_RG_MID1:
971 {
972 Geom::Point start = Geom::Point (rg->cx.computed, rg->cy.computed);
973 Geom::Point end = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
974 double offset = get_offset_between_points (p, start, end);
975 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (rg, false);
976 sp_gradient_ensure_vector(rg);
977 rg->vector.stops.at(point_i).offset = offset;
978 SPStop* stopi = sp_get_stop_i(vector, point_i);
979 stopi->offset = offset;
980 if (write_repr) {
981 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
982 } else {
983 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
984 }
985 break;
986 }
987 case POINT_RG_MID2:
988 Geom::Point start = Geom::Point (rg->cx.computed, rg->cy.computed);
989 Geom::Point end = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
990 double offset = get_offset_between_points (p, start, end);
991 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(rg, false);
992 sp_gradient_ensure_vector(rg);
993 rg->vector.stops.at(point_i).offset = offset;
994 SPStop* stopi = sp_get_stop_i(vector, point_i);
995 stopi->offset = offset;
996 if (write_repr) {
997 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
998 } else {
999 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1000 }
1001 break;
1002 }
1003 if (transform_set) {
1004 gradient->gradientTransform = new_transform;
1005 gradient->gradientTransform_set = TRUE;
1006 if (write_repr) {
1007 gchar *s=sp_svg_transform_write(gradient->gradientTransform);
1008 SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", s);
1009 g_free(s);
1010 } else {
1011 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1012 }
1013 }
1014 }
1015 }
1017 SPGradient *
1018 sp_item_gradient_get_vector (SPItem *item, bool fill_or_stroke)
1019 {
1020 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1022 if (gradient)
1023 return sp_gradient_get_vector (gradient, false);
1024 return NULL;
1025 }
1027 SPGradientSpread
1028 sp_item_gradient_get_spread (SPItem *item, bool fill_or_stroke)
1029 {
1030 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1032 if (gradient)
1033 return sp_gradient_get_spread (gradient);
1034 return SP_GRADIENT_SPREAD_PAD;
1035 }
1038 /**
1039 Returns the position of point point_type of the gradient applied to item (either fill_or_stroke),
1040 in desktop coordinates.
1041 */
1043 Geom::Point
1044 sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1045 {
1046 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1048 Geom::Point p (0, 0);
1050 if (!gradient)
1051 return from_2geom(p);
1053 if (SP_IS_LINEARGRADIENT(gradient)) {
1054 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
1055 switch (point_type) {
1056 case POINT_LG_BEGIN:
1057 p = Geom::Point (lg->x1.computed, lg->y1.computed);
1058 break;
1059 case POINT_LG_END:
1060 p = Geom::Point (lg->x2.computed, lg->y2.computed);
1061 break;
1062 case POINT_LG_MID:
1063 {
1064 gdouble offset = lg->vector.stops.at(point_i).offset;
1065 p = (1-offset) * Geom::Point(lg->x1.computed, lg->y1.computed) + offset * Geom::Point(lg->x2.computed, lg->y2.computed);
1066 }
1067 break;
1068 }
1069 } else if (SP_IS_RADIALGRADIENT(gradient)) {
1070 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
1071 switch (point_type) {
1072 case POINT_RG_CENTER:
1073 p = Geom::Point (rg->cx.computed, rg->cy.computed);
1074 break;
1075 case POINT_RG_FOCUS:
1076 p = Geom::Point (rg->fx.computed, rg->fy.computed);
1077 break;
1078 case POINT_RG_R1:
1079 p = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
1080 break;
1081 case POINT_RG_R2:
1082 p = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
1083 break;
1084 case POINT_RG_MID1:
1085 {
1086 gdouble offset = rg->vector.stops.at(point_i).offset;
1087 p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed + rg->r.computed, rg->cy.computed);
1088 }
1089 break;
1090 case POINT_RG_MID2:
1091 {
1092 gdouble offset = rg->vector.stops.at(point_i).offset;
1093 p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed, rg->cy.computed - rg->r.computed);
1094 }
1095 break;
1096 }
1097 }
1099 if (SP_GRADIENT(gradient)->units == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1100 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
1101 Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
1102 if (bbox) {
1103 p *= Geom::Matrix(bbox->dimensions()[Geom::X], 0,
1104 0, bbox->dimensions()[Geom::Y],
1105 bbox->min()[Geom::X], bbox->min()[Geom::Y]);
1106 }
1107 }
1108 p *= Geom::Matrix(gradient->gradientTransform) * (Geom::Matrix)sp_item_i2d_affine(item);
1109 return from_2geom(p);
1110 }
1113 /**
1114 * Sets item fill or stroke to the gradient of the specified type with given vector, creating
1115 * new private gradient, if needed.
1116 * gr has to be a normalized vector.
1117 */
1119 SPGradient *
1120 sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_fill)
1121 {
1122 g_return_val_if_fail(item != NULL, NULL);
1123 g_return_val_if_fail(SP_IS_ITEM(item), NULL);
1124 g_return_val_if_fail(gr != NULL, NULL);
1125 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
1126 g_return_val_if_fail(gr->state == SP_GRADIENT_STATE_VECTOR, NULL);
1128 SPStyle *style = SP_OBJECT_STYLE(item);
1129 g_assert(style != NULL);
1131 SPPaintServer *ps = NULL;
1132 if (is_fill? style->fill.isPaintserver() : style->stroke.isPaintserver())
1133 ps = is_fill? SP_STYLE_FILL_SERVER(style) : SP_STYLE_STROKE_SERVER(style);
1135 if (ps
1136 && ( (type == SP_GRADIENT_TYPE_LINEAR && SP_IS_LINEARGRADIENT(ps)) ||
1137 (type == SP_GRADIENT_TYPE_RADIAL && SP_IS_RADIALGRADIENT(ps)) ) )
1138 {
1140 /* Current fill style is the gradient of the required type */
1141 SPGradient *current = SP_GRADIENT(ps);
1143 //g_print("hrefcount %d count %d\n", SP_OBJECT_HREFCOUNT(ig), count_gradient_hrefs(SP_OBJECT(item), ig));
1145 if (SP_OBJECT_HREFCOUNT(current) == 1 ||
1146 SP_OBJECT_HREFCOUNT(current) == count_gradient_hrefs(SP_OBJECT(item), current)) {
1148 // current is private and it's either used once, or all its uses are by children of item;
1149 // so just change its href to vector
1151 if ( current != gr && sp_gradient_get_vector(current, false) != gr ) {
1152 /* href is not the vector */
1153 sp_gradient_repr_set_link(SP_OBJECT_REPR(current), gr);
1154 }
1155 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1156 return current;
1158 } else {
1160 // the gradient is not private, or it is shared with someone else;
1161 // normalize it (this includes creating new private if necessary)
1162 SPGradient *normalized = sp_gradient_fork_private_if_necessary(current, gr, type, item);
1164 g_return_val_if_fail(normalized != NULL, NULL);
1166 if (normalized != current) {
1168 /* We have to change object style here; recursive because this is used from
1169 * fill&stroke and must work for groups etc. */
1170 sp_style_set_property_url(SP_OBJECT(item), is_fill? "fill" : "stroke", SP_OBJECT(normalized), true);
1171 }
1172 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1173 return normalized;
1174 }
1176 } else {
1177 /* Current fill style is not a gradient or wrong type, so construct everything */
1178 SPGradient *constructed = sp_gradient_get_private_normalized(SP_OBJECT_DOCUMENT(item), gr, type);
1179 constructed = sp_gradient_reset_to_userspace(constructed, item);
1180 sp_style_set_property_url(SP_OBJECT(item), ( is_fill ? "fill" : "stroke" ), SP_OBJECT(constructed), true);
1181 SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
1182 SP_OBJECT_STYLE_MODIFIED_FLAG ));
1183 return constructed;
1184 }
1185 }
1187 static void
1188 sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *link)
1189 {
1190 g_return_if_fail(repr != NULL);
1191 g_return_if_fail(link != NULL);
1192 g_return_if_fail(SP_IS_GRADIENT(link));
1194 gchar *ref;
1195 if (link) {
1196 gchar const *id = SP_OBJECT_ID(link);
1197 size_t const len = strlen(id);
1198 ref = (gchar*) alloca(len + 2);
1199 *ref = '#';
1200 memcpy(ref + 1, id, len + 1);
1201 } else {
1202 ref = NULL;
1203 }
1205 repr->setAttribute("xlink:href", ref);
1206 }
1208 /*
1209 * Get default normalized gradient vector of document, create if there is none
1210 */
1212 SPGradient *
1213 sp_document_default_gradient_vector(SPDocument *document, guint32 color)
1214 {
1215 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
1216 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
1218 Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient");
1220 repr->setAttribute("inkscape:collect", "always");
1221 // set here, but removed when it's edited in the gradient editor
1222 // to further reduce clutter, we could
1223 // (1) here, search gradients by color and return what is found without duplication
1224 // (2) in fill & stroke, show only one copy of each gradient in list
1226 Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
1228 gchar b[64];
1229 sp_svg_write_color(b, sizeof(b), color);
1231 {
1232 gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:1;", b);
1233 stop->setAttribute("style", t);
1234 g_free(t);
1235 }
1237 stop->setAttribute("offset", "0");
1239 repr->appendChild(stop);
1240 Inkscape::GC::release(stop);
1242 stop = xml_doc->createElement("svg:stop");
1244 {
1245 gchar *t = g_strdup_printf("stop-color:%s;stop-opacity:0;", b);
1246 stop->setAttribute("style", t);
1247 g_free(t);
1248 }
1250 stop->setAttribute("offset", "1");
1252 repr->appendChild(stop);
1253 Inkscape::GC::release(stop);
1255 SP_OBJECT_REPR(defs)->addChild(repr, NULL);
1256 Inkscape::GC::release(repr);
1258 /* fixme: This does not look like nice */
1259 SPGradient *gr;
1260 gr = (SPGradient *) document->getObjectByRepr(repr);
1261 g_assert(gr != NULL);
1262 g_assert(SP_IS_GRADIENT(gr));
1263 /* fixme: Maybe add extra sanity check here */
1264 gr->state = SP_GRADIENT_STATE_VECTOR;
1266 return gr;
1267 }
1269 /**
1270 Return the preferred vector for \a o, made from (in order of preference) its current vector,
1271 current fill or stroke color, or from desktop style if \a o is NULL or doesn't have style.
1272 */
1273 SPGradient *
1274 sp_gradient_vector_for_object(SPDocument *const doc, SPDesktop *const desktop,
1275 SPObject *const o, bool const is_fill)
1276 {
1277 guint32 rgba = 0;
1278 if (o == NULL || SP_OBJECT_STYLE(o) == NULL) {
1279 rgba = sp_desktop_get_color(desktop, is_fill);
1280 } else {
1281 // take the color of the object
1282 SPStyle const &style = *SP_OBJECT_STYLE(o);
1283 SPIPaint const &paint = ( is_fill
1284 ? style.fill
1285 : style.stroke );
1286 if (paint.isPaintserver()) {
1287 SPObject *server = is_fill? SP_OBJECT_STYLE_FILL_SERVER(o) : SP_OBJECT_STYLE_STROKE_SERVER(o);
1288 if (SP_IS_GRADIENT (server)) {
1289 return sp_gradient_get_vector(SP_GRADIENT (server), TRUE);
1290 } else {
1291 rgba = sp_desktop_get_color(desktop, is_fill);
1292 }
1293 } else if (paint.isColor()) {
1294 rgba = paint.value.color.toRGBA32( 0xff );
1295 } else {
1296 // if o doesn't use flat color, then take current color of the desktop.
1297 rgba = sp_desktop_get_color(desktop, is_fill);
1298 }
1299 }
1301 return sp_document_default_gradient_vector(doc, rgba);
1302 }
1305 /*
1306 Local Variables:
1307 mode:c++
1308 c-file-style:"stroustrup"
1309 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1310 indent-tabs-mode:nil
1311 fill-column:99
1312 End:
1313 */
1314 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :