1 /*
2 * Various utility methods for gradients
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak
7 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
8 * Jon A. Cruz <jon@joncruz.org>
9 *
10 * Copyright (C) 2010 Authors
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"
43 #define noSP_GR_VERBOSE
45 // Terminology:
46 //
47 // "vector" is a gradient that has stops but not position coords. It can be referenced by one or
48 // more privates. Objects should not refer to it directly. It has no radial/linear distinction.
49 //
50 // "private" is a gradient that has no stops but has position coords (e.g. center, radius etc for a
51 // radial). It references a vector for the actual colors. Each private is only used by one
52 // object. It is either linear or radial.
54 static void sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *gr);
56 SPGradient *sp_gradient_ensure_vector_normalized(SPGradient *gr)
57 {
58 #ifdef SP_GR_VERBOSE
59 g_message("sp_gradient_ensure_vector_normalized(%p)", gr);
60 #endif
61 g_return_val_if_fail(gr != NULL, NULL);
62 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
64 /* If we are already normalized vector, just return */
65 if (gr->state == SP_GRADIENT_STATE_VECTOR) return gr;
66 /* Fail, if we have wrong state set */
67 if (gr->state != SP_GRADIENT_STATE_UNKNOWN) {
68 g_warning("file %s: line %d: Cannot normalize private gradient to vector (%s)", __FILE__, __LINE__, gr->getId());
69 return NULL;
70 }
72 /* First make sure we have vector directly defined (i.e. gr has its own stops) */
73 if ( !gr->hasStops() ) {
74 /* We do not have stops ourselves, so flatten stops as well */
75 gr->ensureVector();
76 g_assert(gr->vector.built);
77 // this adds stops from gr->vector as children to gr
78 sp_gradient_repr_write_vector (gr);
79 }
81 /* If gr hrefs some other gradient, remove the href */
82 if (gr->ref->getObject()) {
83 /* We are hrefing someone, so require flattening */
84 SP_OBJECT(gr)->updateRepr(SP_OBJECT_WRITE_EXT | SP_OBJECT_WRITE_ALL);
85 sp_gradient_repr_set_link(SP_OBJECT_REPR(gr), NULL);
86 }
88 /* Everything is OK, set state flag */
89 gr->state = SP_GRADIENT_STATE_VECTOR;
90 return gr;
91 }
93 /**
94 * Creates new private gradient for the given vector
95 */
97 static SPGradient *sp_gradient_get_private_normalized(SPDocument *document, SPGradient *vector, SPGradientType type)
98 {
99 #ifdef SP_GR_VERBOSE
100 g_message("sp_gradient_get_private_normalized(%p, %p, %d)", document, vector, type);
101 #endif
103 g_return_val_if_fail(document != NULL, NULL);
104 g_return_val_if_fail(vector != NULL, NULL);
105 g_return_val_if_fail(SP_IS_GRADIENT(vector), NULL);
106 g_return_val_if_fail(vector->hasStops(), NULL);
108 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
110 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
111 // create a new private gradient of the requested type
112 Inkscape::XML::Node *repr;
113 if (type == SP_GRADIENT_TYPE_LINEAR) {
114 repr = xml_doc->createElement("svg:linearGradient");
115 } else {
116 repr = xml_doc->createElement("svg:radialGradient");
117 }
119 // privates are garbage-collectable
120 repr->setAttribute("inkscape:collect", "always");
122 // link to vector
123 sp_gradient_repr_set_link(repr, vector);
125 /* Append the new private gradient to defs */
126 SP_OBJECT_REPR(defs)->appendChild(repr);
127 Inkscape::GC::release(repr);
129 // get corresponding object
130 SPGradient *gr = (SPGradient *) document->getObjectByRepr(repr);
131 g_assert(gr != NULL);
132 g_assert(SP_IS_GRADIENT(gr));
134 return gr;
135 }
137 /**
138 Count how many times gr is used by the styles of o and its descendants
139 */
140 guint count_gradient_hrefs(SPObject *o, SPGradient *gr)
141 {
142 if (!o)
143 return 1;
145 guint i = 0;
147 SPStyle *style = SP_OBJECT_STYLE(o);
148 if (style
149 && style->fill.isPaintserver()
150 && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style))
151 && SP_GRADIENT(SP_STYLE_FILL_SERVER(style)) == gr)
152 {
153 i ++;
154 }
155 if (style
156 && style->stroke.isPaintserver()
157 && SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style))
158 && SP_GRADIENT(SP_STYLE_STROKE_SERVER(style)) == gr)
159 {
160 i ++;
161 }
163 for (SPObject *child = sp_object_first_child(o);
164 child != NULL; child = SP_OBJECT_NEXT(child)) {
165 i += count_gradient_hrefs(child, gr);
166 }
168 return i;
169 }
172 /**
173 * If gr has other users, create a new private; also check if gr links to vector, relink if not
174 */
175 SPGradient *sp_gradient_fork_private_if_necessary(SPGradient *gr, SPGradient *vector,
176 SPGradientType type, SPObject *o)
177 {
178 #ifdef SP_GR_VERBOSE
179 g_message("sp_gradient_fork_private_if_necessary(%p, %p, %d, %p)", gr, vector, type, o);
180 #endif
181 g_return_val_if_fail(gr != NULL, NULL);
182 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
184 // Orphaned gradient, no vector with stops at the end of the line; this used to be an assert
185 // but i think we should not abort on this - maybe just write a validity warning into some sort
186 // of log
187 if ( !vector || !vector->hasStops() ) {
188 return (gr);
189 }
191 // user is the object that uses this gradient; normally it's item but for tspans, we
192 // check its ancestor text so that tspans don't get different gradients from their
193 // texts.
194 SPObject *user = o;
195 while (SP_IS_TSPAN(user)) {
196 user = SP_OBJECT_PARENT(user);
197 }
199 // Check the number of uses of the gradient within this object;
200 // if we are private and there are no other users,
201 if (!vector->isSwatch() && (SP_OBJECT_HREFCOUNT(gr) <= count_gradient_hrefs(user, gr))) {
202 // check vector
203 if ( gr != vector && gr->ref->getObject() != vector ) {
204 /* our href is not the vector, and vector is different from gr; relink */
205 sp_gradient_repr_set_link(SP_OBJECT_REPR(gr), vector);
206 }
207 return gr;
208 }
210 SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
211 SPObject *defs = SP_DOCUMENT_DEFS(doc);
213 if ((gr->hasStops()) ||
214 (gr->state != SP_GRADIENT_STATE_UNKNOWN) ||
215 (SP_OBJECT_PARENT(gr) != SP_OBJECT(defs)) ||
216 (SP_OBJECT_HREFCOUNT(gr) > 1)) {
217 // we have to clone a fresh new private gradient for the given vector
219 // create an empty one
220 SPGradient *gr_new = sp_gradient_get_private_normalized(doc, vector, type);
222 // copy all the attributes to it
223 Inkscape::XML::Node *repr_new = SP_OBJECT_REPR(gr_new);
224 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
225 repr_new->setAttribute("gradientUnits", repr->attribute("gradientUnits"));
226 repr_new->setAttribute("gradientTransform", repr->attribute("gradientTransform"));
227 repr_new->setAttribute("spreadMethod", repr->attribute("spreadMethod"));
228 if (SP_IS_RADIALGRADIENT(gr)) {
229 repr_new->setAttribute("cx", repr->attribute("cx"));
230 repr_new->setAttribute("cy", repr->attribute("cy"));
231 repr_new->setAttribute("fx", repr->attribute("fx"));
232 repr_new->setAttribute("fy", repr->attribute("fy"));
233 repr_new->setAttribute("r", repr->attribute("r"));
234 } else {
235 repr_new->setAttribute("x1", repr->attribute("x1"));
236 repr_new->setAttribute("y1", repr->attribute("y1"));
237 repr_new->setAttribute("x2", repr->attribute("x2"));
238 repr_new->setAttribute("y2", repr->attribute("y2"));
239 }
241 return gr_new;
242 } else {
243 return gr;
244 }
245 }
247 SPGradient *sp_gradient_fork_vector_if_necessary(SPGradient *gr)
248 {
249 #ifdef SP_GR_VERBOSE
250 g_message("sp_gradient_fork_vector_if_necessary(%p)", gr);
251 #endif
252 // Some people actually prefer their gradient vectors to be shared...
253 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
254 if (!prefs->getBool("/options/forkgradientvectors/value", true))
255 return gr;
257 if (SP_OBJECT_HREFCOUNT(gr) > 1) {
258 SPDocument *doc = SP_OBJECT_DOCUMENT(gr);
259 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
261 Inkscape::XML::Node *repr = SP_OBJECT_REPR (gr)->duplicate(xml_doc);
262 SP_OBJECT_REPR (SP_DOCUMENT_DEFS (doc))->addChild(repr, NULL);
263 SPGradient *gr_new = (SPGradient *) doc->getObjectByRepr(repr);
264 gr_new = sp_gradient_ensure_vector_normalized (gr_new);
265 Inkscape::GC::release(repr);
266 return gr_new;
267 }
268 return gr;
269 }
271 /**
272 * Obtain the vector from the gradient. A forked vector will be created and linked to this gradient if another gradient uses it.
273 */
274 SPGradient *sp_gradient_get_forked_vector_if_necessary(SPGradient *gradient, bool force_vector)
275 {
276 #ifdef SP_GR_VERBOSE
277 g_message("sp_gradient_get_forked_vector_if_necessary(%p, %d)", gradient, force_vector);
278 #endif
279 SPGradient *vector = gradient->getVector(force_vector);
280 vector = sp_gradient_fork_vector_if_necessary (vector);
281 if ( gradient != vector && gradient->ref->getObject() != vector ) {
282 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
283 }
284 return vector;
285 }
288 /**
289 * Convert an item's gradient to userspace _without_ preserving coords, setting them to defaults
290 * instead. No forking or reapplying is done because this is only called for newly created privates.
291 * @return The new gradient.
292 */
293 SPGradient *sp_gradient_reset_to_userspace(SPGradient *gr, SPItem *item)
294 {
295 #ifdef SP_GR_VERBOSE
296 g_message("sp_gradient_reset_to_userspace(%p, %p)", gr, item);
297 #endif
298 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
300 // calculate the bbox of the item
301 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
302 Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
304 if (!bbox)
305 return gr;
307 Geom::Coord const width = bbox->dimensions()[Geom::X];
308 Geom::Coord const height = bbox->dimensions()[Geom::Y];
310 Geom::Point const center = bbox->midpoint();
312 if (SP_IS_RADIALGRADIENT(gr)) {
313 sp_repr_set_svg_double(repr, "cx", center[Geom::X]);
314 sp_repr_set_svg_double(repr, "cy", center[Geom::Y]);
315 sp_repr_set_svg_double(repr, "fx", center[Geom::X]);
316 sp_repr_set_svg_double(repr, "fy", center[Geom::Y]);
317 sp_repr_set_svg_double(repr, "r", width/2);
319 // we want it to be elliptic, not circular
320 Geom::Matrix squeeze = Geom::Translate (-center) *
321 Geom::Scale(1, height/width) *
322 Geom::Translate (center);
324 gr->gradientTransform = squeeze;
325 {
326 gchar *c=sp_svg_transform_write(gr->gradientTransform);
327 SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c);
328 g_free(c);
329 }
330 } else {
331 sp_repr_set_svg_double(repr, "x1", (center - Geom::Point(width/2, 0))[Geom::X]);
332 sp_repr_set_svg_double(repr, "y1", (center - Geom::Point(width/2, 0))[Geom::Y]);
333 sp_repr_set_svg_double(repr, "x2", (center + Geom::Point(width/2, 0))[Geom::X]);
334 sp_repr_set_svg_double(repr, "y2", (center + Geom::Point(width/2, 0))[Geom::Y]);
335 }
337 // set the gradientUnits
338 repr->setAttribute("gradientUnits", "userSpaceOnUse");
340 return gr;
341 }
343 /**
344 * Convert an item's gradient to userspace if necessary, also fork it if necessary.
345 * @return The new gradient.
346 */
347 SPGradient *sp_gradient_convert_to_userspace(SPGradient *gr, SPItem *item, gchar const *property)
348 {
349 #ifdef SP_GR_VERBOSE
350 g_message("sp_gradient_convert_to_userspace(%p, %p, \"%s\")", gr, item, property);
351 #endif
352 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
354 if ( gr && gr->isSolid() ) {
355 return gr;
356 }
358 // First, fork it if it is shared
359 gr = sp_gradient_fork_private_if_necessary(gr, gr->getVector(),
360 SP_IS_RADIALGRADIENT(gr) ? SP_GRADIENT_TYPE_RADIAL : SP_GRADIENT_TYPE_LINEAR, SP_OBJECT(item));
362 if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
364 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
366 // calculate the bbox of the item
367 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
368 Geom::Matrix bbox2user;
369 Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
370 if ( bbox ) {
371 bbox2user = Geom::Matrix(bbox->dimensions()[Geom::X], 0,
372 0, bbox->dimensions()[Geom::Y],
373 bbox->min()[Geom::X], bbox->min()[Geom::Y]);
374 } else {
375 // would be degenerate otherwise
376 bbox2user = Geom::identity();
377 }
379 /* skew is the additional transform, defined by the proportions of the item, that we need
380 * to apply to the gradient in order to work around this weird bit from SVG 1.1
381 * (http://www.w3.org/TR/SVG11/pservers.html#LinearGradients):
382 *
383 * When gradientUnits="objectBoundingBox" and gradientTransform is the identity
384 * matrix, the stripes of the linear gradient are perpendicular to the gradient
385 * vector in object bounding box space (i.e., the abstract coordinate system where
386 * (0,0) is at the top/left of the object bounding box and (1,1) is at the
387 * bottom/right of the object bounding box). When the object's bounding box is not
388 * square, the stripes that are conceptually perpendicular to the gradient vector
389 * within object bounding box space will render non-perpendicular relative to the
390 * gradient vector in user space due to application of the non-uniform scaling
391 * transformation from bounding box space to user space.
392 */
393 Geom::Matrix skew = bbox2user;
394 double exp = skew.descrim();
395 skew[0] /= exp;
396 skew[1] /= exp;
397 skew[2] /= exp;
398 skew[3] /= exp;
399 skew[4] = 0;
400 skew[5] = 0;
402 // apply skew to the gradient
403 gr->gradientTransform = skew;
404 {
405 gchar *c=sp_svg_transform_write(gr->gradientTransform);
406 SP_OBJECT_REPR(gr)->setAttribute("gradientTransform", c);
407 g_free(c);
408 }
410 // Matrix to convert points to userspace coords; postmultiply by inverse of skew so
411 // as to cancel it out when it's applied to the gradient during rendering
412 Geom::Matrix point_convert = bbox2user * skew.inverse();
414 if (SP_IS_RADIALGRADIENT(gr)) {
415 SPRadialGradient *rg = SP_RADIALGRADIENT(gr);
417 // original points in the bbox coords
418 Geom::Point c_b = Geom::Point(rg->cx.computed, rg->cy.computed);
419 Geom::Point f_b = Geom::Point(rg->fx.computed, rg->fy.computed);
420 double r_b = rg->r.computed;
422 // converted points in userspace coords
423 Geom::Point c_u = c_b * point_convert;
424 Geom::Point f_u = f_b * point_convert;
425 double r_u = r_b * point_convert.descrim();
427 sp_repr_set_svg_double(repr, "cx", c_u[Geom::X]);
428 sp_repr_set_svg_double(repr, "cy", c_u[Geom::Y]);
429 sp_repr_set_svg_double(repr, "fx", f_u[Geom::X]);
430 sp_repr_set_svg_double(repr, "fy", f_u[Geom::Y]);
431 sp_repr_set_svg_double(repr, "r", r_u);
433 } else {
434 SPLinearGradient *lg = SP_LINEARGRADIENT(gr);
436 Geom::Point p1_b = Geom::Point(lg->x1.computed, lg->y1.computed);
437 Geom::Point p2_b = Geom::Point(lg->x2.computed, lg->y2.computed);
439 Geom::Point p1_u = p1_b * point_convert;
440 Geom::Point p2_u = p2_b * point_convert;
442 sp_repr_set_svg_double(repr, "x1", p1_u[Geom::X]);
443 sp_repr_set_svg_double(repr, "y1", p1_u[Geom::Y]);
444 sp_repr_set_svg_double(repr, "x2", p2_u[Geom::X]);
445 sp_repr_set_svg_double(repr, "y2", p2_u[Geom::Y]);
446 }
448 // set the gradientUnits
449 repr->setAttribute("gradientUnits", "userSpaceOnUse");
450 }
452 // apply the gradient to the item (may be necessary if we forked it); not recursive
453 // generally because grouped items will be taken care of later (we're being called
454 // from sp_item_adjust_paint_recursive); however text and all its children should all
455 // refer to one gradient, hence the recursive call for text (because we can't/don't
456 // want to access tspans and set gradients on them separately)
457 if (SP_IS_TEXT(item))
458 sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), true);
459 else
460 sp_style_set_property_url(SP_OBJECT(item), property, SP_OBJECT(gr), false);
462 return gr;
463 }
465 void sp_gradient_transform_multiply(SPGradient *gradient, Geom::Matrix postmul, bool set)
466 {
467 #ifdef SP_GR_VERBOSE
468 g_message("sp_gradient_transform_multiply(%p, , %d)", gradient, set);
469 #endif
470 if (set) {
471 gradient->gradientTransform = postmul;
472 } else {
473 gradient->gradientTransform *= postmul; // fixme: get gradient transform by climbing to hrefs?
474 }
475 gradient->gradientTransform_set = TRUE;
477 gchar *c=sp_svg_transform_write(gradient->gradientTransform);
478 SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", c);
479 g_free(c);
480 }
482 SPGradient *sp_item_gradient(SPItem *item, bool fill_or_stroke)
483 {
484 SPStyle *style = item->style;
485 SPGradient *gradient = 0;
487 if (fill_or_stroke) {
488 if (style && (style->fill.isPaintserver())) {
489 SPPaintServer *server = item->style->getFillPaintServer();
490 if ( SP_IS_GRADIENT(server) ) {
491 gradient = SP_GRADIENT(server);
492 }
493 }
494 } else {
495 if (style && (style->stroke.isPaintserver())) {
496 SPPaintServer *server = item->style->getStrokePaintServer();
497 if ( SP_IS_GRADIENT(server) ) {
498 gradient = SP_GRADIENT(server);
499 }
500 }
501 }
503 return gradient;
504 }
506 SPStop *sp_last_stop(SPGradient *gradient)
507 {
508 for (SPStop *stop = gradient->getFirstStop(); stop != NULL; stop = stop->getNextStop()) {
509 if (stop->getNextStop() == NULL)
510 return stop;
511 }
512 return NULL;
513 }
515 SPStop *sp_get_stop_i(SPGradient *gradient, guint stop_i)
516 {
517 SPStop *stop = gradient->getFirstStop();
519 // if this is valid but weird gradient without an offset-zero stop element,
520 // inkscape has created a handle for the start of gradient anyway,
521 // so when it asks for stop N that corresponds to stop element N-1
522 if (stop->offset != 0)
523 stop_i --;
525 for (guint i = 0; i < stop_i; i++) {
526 if (!stop) {
527 return NULL;
528 }
529 stop = stop->getNextStop();
530 }
532 return stop;
533 }
535 guint32 average_color(guint32 c1, guint32 c2, gdouble p)
536 {
537 guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
538 guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
539 guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
540 guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
542 return SP_RGBA32_U_COMPOSE(r, g, b, a);
543 }
545 SPStop *sp_vector_add_stop(SPGradient *vector, SPStop* prev_stop, SPStop* next_stop, gfloat offset)
546 {
547 #ifdef SP_GR_VERBOSE
548 g_message("sp_vector_add_stop(%p, %p, %p, %f)", vector, prev_stop, next_stop, offset);
549 #endif
551 Inkscape::XML::Node *new_stop_repr = NULL;
552 new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
553 SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
555 SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
556 newstop->offset = offset;
557 sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
558 guint32 const c1 = sp_stop_get_rgba32(prev_stop);
559 guint32 const c2 = sp_stop_get_rgba32(next_stop);
560 guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
561 Inkscape::CSSOStringStream os;
562 gchar c[64];
563 sp_svg_write_color (c, sizeof(c), cnew);
564 gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
565 os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
566 SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
567 Inkscape::GC::release(new_stop_repr);
569 return newstop;
570 }
572 void sp_item_gradient_edit_stop(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
573 {
574 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
576 if (!gradient || !SP_IS_GRADIENT(gradient))
577 return;
579 SPGradient *vector = gradient->getVector();
580 switch (point_type) {
581 case POINT_LG_BEGIN:
582 case POINT_RG_CENTER:
583 case POINT_RG_FOCUS:
584 {
585 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, vector->getFirstStop());
586 gtk_widget_show (dialog);
587 }
588 break;
590 case POINT_LG_END:
591 case POINT_RG_R1:
592 case POINT_RG_R2:
593 {
594 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_last_stop (vector));
595 gtk_widget_show (dialog);
596 }
597 break;
599 case POINT_LG_MID:
600 case POINT_RG_MID1:
601 case POINT_RG_MID2:
602 {
603 GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_get_stop_i (vector, point_i));
604 gtk_widget_show (dialog);
605 }
606 break;
607 default:
608 break;
609 }
610 }
612 guint32 sp_item_gradient_stop_query_style(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 0;
619 SPGradient *vector = gradient->getVector();
621 if (!vector) // orphan!
622 return 0; // what else to do?
624 switch (point_type) {
625 case POINT_LG_BEGIN:
626 case POINT_RG_CENTER:
627 case POINT_RG_FOCUS:
628 {
629 SPStop *first = vector->getFirstStop();
630 if (first) {
631 return sp_stop_get_rgba32(first);
632 }
633 }
634 break;
636 case POINT_LG_END:
637 case POINT_RG_R1:
638 case POINT_RG_R2:
639 {
640 SPStop *last = sp_last_stop (vector);
641 if (last) {
642 return sp_stop_get_rgba32(last);
643 }
644 }
645 break;
647 case POINT_LG_MID:
648 case POINT_RG_MID1:
649 case POINT_RG_MID2:
650 {
651 SPStop *stopi = sp_get_stop_i (vector, point_i);
652 if (stopi) {
653 return sp_stop_get_rgba32(stopi);
654 }
655 }
656 break;
658 default:
659 break;
660 }
661 return 0;
662 }
664 void sp_item_gradient_stop_set_style(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, SPCSSAttr *stop)
665 {
666 #ifdef SP_GR_VERBOSE
667 g_message("sp_item_gradient_stop_set_style(%p, %d, %d, %d, %p)", item, point_type, point_i, fill_or_stroke, stop);
668 #endif
669 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
671 if (!gradient || !SP_IS_GRADIENT(gradient))
672 return;
674 SPGradient *vector = gradient->getVector();
676 if (!vector) // orphan!
677 return;
679 vector = sp_gradient_fork_vector_if_necessary (vector);
680 if ( gradient != vector && gradient->ref->getObject() != vector ) {
681 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
682 }
684 switch (point_type) {
685 case POINT_LG_BEGIN:
686 case POINT_RG_CENTER:
687 case POINT_RG_FOCUS:
688 {
689 SPStop *first = vector->getFirstStop();
690 if (first) {
691 sp_repr_css_change (SP_OBJECT_REPR (first), stop, "style");
692 }
693 }
694 break;
696 case POINT_LG_END:
697 case POINT_RG_R1:
698 case POINT_RG_R2:
699 {
700 SPStop *last = sp_last_stop (vector);
701 if (last) {
702 sp_repr_css_change (SP_OBJECT_REPR (last), stop, "style");
703 }
704 }
705 break;
707 case POINT_LG_MID:
708 case POINT_RG_MID1:
709 case POINT_RG_MID2:
710 {
711 SPStop *stopi = sp_get_stop_i (vector, point_i);
712 if (stopi) {
713 sp_repr_css_change (SP_OBJECT_REPR (stopi), stop, "style");
714 }
715 }
716 break;
718 default:
719 break;
720 }
721 }
723 void sp_item_gradient_reverse_vector(SPItem *item, bool fill_or_stroke)
724 {
725 #ifdef SP_GR_VERBOSE
726 g_message("sp_item_gradient_reverse_vector(%p, %d)", item, fill_or_stroke);
727 #endif
728 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
729 if (!gradient || !SP_IS_GRADIENT(gradient))
730 return;
732 SPGradient *vector = gradient->getVector();
733 if (!vector) // orphan!
734 return;
736 vector = sp_gradient_fork_vector_if_necessary (vector);
737 if ( gradient != vector && gradient->ref->getObject() != vector ) {
738 sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector);
739 }
741 GSList *child_reprs = NULL;
742 GSList *child_objects = NULL;
743 std::vector<double> offsets;
744 for (SPObject *child = sp_object_first_child(vector);
745 child != NULL; child = SP_OBJECT_NEXT(child)) {
746 child_reprs = g_slist_prepend (child_reprs, SP_OBJECT_REPR(child));
747 child_objects = g_slist_prepend (child_objects, child);
748 offsets.push_back(sp_repr_get_double_attribute(SP_OBJECT_REPR(child), "offset", 0));
749 }
751 GSList *child_copies = NULL;
752 for (GSList *i = child_reprs; i != NULL; i = i->next) {
753 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) i->data;
754 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(vector)->document();
755 child_copies = g_slist_append (child_copies, repr->duplicate(xml_doc));
756 }
759 for (GSList *i = child_objects; i != NULL; i = i->next) {
760 SPObject *child = SP_OBJECT (i->data);
761 child->deleteObject();
762 }
764 std::vector<double>::iterator iter = offsets.end() - 1;
765 for (GSList *i = child_copies; i != NULL; i = i->next) {
766 Inkscape::XML::Node *copy = (Inkscape::XML::Node *) i->data;
767 vector->appendChildRepr(copy);
768 sp_repr_set_svg_double (copy, "offset", 1 - *iter);
769 iter --;
770 Inkscape::GC::release(copy);
771 }
773 g_slist_free (child_reprs);
774 g_slist_free (child_copies);
775 g_slist_free (child_objects);
776 }
779 /**
780 Set the position of point point_type of the gradient applied to item (either fill_or_stroke) to
781 p_w (in desktop coordinates). Write_repr if you want the change to become permanent.
782 */
783 void 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)
784 {
785 #ifdef SP_GR_VERBOSE
786 g_message("sp_item_gradient_set_coords(%p, %d, %d, ...)", item, point_type, point_i );
787 #endif
788 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
790 if (!gradient || !SP_IS_GRADIENT(gradient))
791 return;
793 gradient = sp_gradient_convert_to_userspace (gradient, item, fill_or_stroke? "fill" : "stroke");
795 Geom::Matrix i2d (sp_item_i2d_affine (item));
796 Geom::Point p = p_w * i2d.inverse();
797 p *= (gradient->gradientTransform).inverse();
798 // now p is in gradient's original coordinates
800 Inkscape::XML::Node *repr = SP_OBJECT_REPR(gradient);
802 if (SP_IS_LINEARGRADIENT(gradient)) {
803 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
804 switch (point_type) {
805 case POINT_LG_BEGIN:
806 if (scale) {
807 lg->x2.computed += (lg->x1.computed - p[Geom::X]);
808 lg->y2.computed += (lg->y1.computed - p[Geom::Y]);
809 }
810 lg->x1.computed = p[Geom::X];
811 lg->y1.computed = p[Geom::Y];
812 if (write_repr) {
813 if (scale) {
814 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
815 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
816 }
817 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
818 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
819 } else {
820 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
821 }
822 break;
823 case POINT_LG_END:
824 if (scale) {
825 lg->x1.computed += (lg->x2.computed - p[Geom::X]);
826 lg->y1.computed += (lg->y2.computed - p[Geom::Y]);
827 }
828 lg->x2.computed = p[Geom::X];
829 lg->y2.computed = p[Geom::Y];
830 if (write_repr) {
831 if (scale) {
832 sp_repr_set_svg_double(repr, "x1", lg->x1.computed);
833 sp_repr_set_svg_double(repr, "y1", lg->y1.computed);
834 }
835 sp_repr_set_svg_double(repr, "x2", lg->x2.computed);
836 sp_repr_set_svg_double(repr, "y2", lg->y2.computed);
837 } else {
838 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
839 }
840 break;
841 case POINT_LG_MID:
842 {
843 // using X-coordinates only to determine the offset, assuming p has been snapped to the vector from begin to end.
844 double offset = get_offset_between_points (p, Geom::Point(lg->x1.computed, lg->y1.computed), Geom::Point(lg->x2.computed, lg->y2.computed));
845 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (lg, false);
846 lg->ensureVector();
847 lg->vector.stops.at(point_i).offset = offset;
848 SPStop* stopi = sp_get_stop_i(vector, point_i);
849 stopi->offset = offset;
850 if (write_repr) {
851 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
852 } else {
853 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
854 }
855 }
856 break;
857 default:
858 break;
859 }
860 } else if (SP_IS_RADIALGRADIENT(gradient)) {
861 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
862 Geom::Point c (rg->cx.computed, rg->cy.computed);
863 Geom::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords
864 if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && Geom::L2 (p_w - c_w) < 1e-3) {
865 // prevent setting a radius too close to the center
866 return;
867 }
868 Geom::Matrix new_transform;
869 bool transform_set = false;
871 switch (point_type) {
872 case POINT_RG_CENTER:
873 rg->fx.computed = p[Geom::X] + (rg->fx.computed - rg->cx.computed);
874 rg->fy.computed = p[Geom::Y] + (rg->fy.computed - rg->cy.computed);
875 rg->cx.computed = p[Geom::X];
876 rg->cy.computed = p[Geom::Y];
877 if (write_repr) {
878 sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
879 sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
880 sp_repr_set_svg_double(repr, "cx", rg->cx.computed);
881 sp_repr_set_svg_double(repr, "cy", rg->cy.computed);
882 } else {
883 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
884 }
885 break;
886 case POINT_RG_FOCUS:
887 rg->fx.computed = p[Geom::X];
888 rg->fy.computed = p[Geom::Y];
889 if (write_repr) {
890 sp_repr_set_svg_double(repr, "fx", rg->fx.computed);
891 sp_repr_set_svg_double(repr, "fy", rg->fy.computed);
892 } else {
893 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
894 }
895 break;
896 case POINT_RG_R1:
897 {
898 Geom::Point r1_w = (c + Geom::Point(rg->r.computed, 0)) * gradient->gradientTransform * i2d;
899 double r1_angle = Geom::atan2(r1_w - c_w);
900 double move_angle = Geom::atan2(p_w - c_w) - r1_angle;
901 double move_stretch = Geom::L2(p_w - c_w) / Geom::L2(r1_w - c_w);
903 Geom::Matrix move = Geom::Matrix (Geom::Translate (-c_w)) *
904 Geom::Matrix (Geom::Rotate(-r1_angle)) *
905 Geom::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
906 Geom::Matrix (Geom::Rotate(r1_angle)) *
907 Geom::Matrix (Geom::Rotate(move_angle)) *
908 Geom::Matrix (Geom::Translate (c_w));
910 new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
911 transform_set = true;
913 break;
914 }
915 case POINT_RG_R2:
916 {
917 Geom::Point r2_w = (c + Geom::Point(0, -rg->r.computed)) * gradient->gradientTransform * i2d;
918 double r2_angle = Geom::atan2(r2_w - c_w);
919 double move_angle = Geom::atan2(p_w - c_w) - r2_angle;
920 double move_stretch = Geom::L2(p_w - c_w) / Geom::L2(r2_w - c_w);
922 Geom::Matrix move = Geom::Matrix (Geom::Translate (-c_w)) *
923 Geom::Matrix (Geom::Rotate(-r2_angle)) *
924 Geom::Matrix (Geom::Scale(move_stretch, scale? move_stretch : 1)) *
925 Geom::Matrix (Geom::Rotate(r2_angle)) *
926 Geom::Matrix (Geom::Rotate(move_angle)) *
927 Geom::Matrix (Geom::Translate (c_w));
929 new_transform = gradient->gradientTransform * i2d * move * i2d.inverse();
930 transform_set = true;
932 break;
933 }
934 case POINT_RG_MID1:
935 {
936 Geom::Point start = Geom::Point (rg->cx.computed, rg->cy.computed);
937 Geom::Point end = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
938 double offset = get_offset_between_points (p, start, end);
939 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (rg, false);
940 rg->ensureVector();
941 rg->vector.stops.at(point_i).offset = offset;
942 SPStop* stopi = sp_get_stop_i(vector, point_i);
943 stopi->offset = offset;
944 if (write_repr) {
945 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
946 } else {
947 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
948 }
949 break;
950 }
951 case POINT_RG_MID2:
952 Geom::Point start = Geom::Point (rg->cx.computed, rg->cy.computed);
953 Geom::Point end = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
954 double offset = get_offset_between_points (p, start, end);
955 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(rg, false);
956 rg->ensureVector();
957 rg->vector.stops.at(point_i).offset = offset;
958 SPStop* stopi = sp_get_stop_i(vector, point_i);
959 stopi->offset = offset;
960 if (write_repr) {
961 sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset);
962 } else {
963 SP_OBJECT (stopi)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
964 }
965 break;
966 }
968 if (transform_set) {
969 gradient->gradientTransform = new_transform;
970 gradient->gradientTransform_set = TRUE;
971 if (write_repr) {
972 gchar *s=sp_svg_transform_write(gradient->gradientTransform);
973 SP_OBJECT_REPR(gradient)->setAttribute("gradientTransform", s);
974 g_free(s);
975 } else {
976 SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG);
977 }
978 }
979 }
980 }
982 SPGradient *sp_item_gradient_get_vector(SPItem *item, bool fill_or_stroke)
983 {
984 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
986 if (gradient) {
987 return gradient->getVector();
988 }
989 return NULL;
990 }
992 SPGradientSpread sp_item_gradient_get_spread(SPItem *item, bool fill_or_stroke)
993 {
994 SPGradientSpread spread = SP_GRADIENT_SPREAD_PAD;
995 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
997 if (gradient) {
998 spread = gradient->fetchSpread();
999 }
1000 return spread;
1001 }
1004 /**
1005 Returns the position of point point_type of the gradient applied to item (either fill_or_stroke),
1006 in desktop coordinates.
1007 */
1009 Geom::Point sp_item_gradient_get_coords(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke)
1010 {
1011 #ifdef SP_GR_VERBOSE
1012 g_message("sp_item_gradient_get_coords(%p, %d, %d, %d)", item, point_type, point_i, fill_or_stroke);
1013 #endif
1014 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
1016 Geom::Point p (0, 0);
1018 if (!gradient)
1019 return from_2geom(p);
1021 if (SP_IS_LINEARGRADIENT(gradient)) {
1022 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
1023 switch (point_type) {
1024 case POINT_LG_BEGIN:
1025 p = Geom::Point (lg->x1.computed, lg->y1.computed);
1026 break;
1027 case POINT_LG_END:
1028 p = Geom::Point (lg->x2.computed, lg->y2.computed);
1029 break;
1030 case POINT_LG_MID:
1031 {
1032 gdouble offset = lg->vector.stops.at(point_i).offset;
1033 p = (1-offset) * Geom::Point(lg->x1.computed, lg->y1.computed) + offset * Geom::Point(lg->x2.computed, lg->y2.computed);
1034 }
1035 break;
1036 }
1037 } else if (SP_IS_RADIALGRADIENT(gradient)) {
1038 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
1039 switch (point_type) {
1040 case POINT_RG_CENTER:
1041 p = Geom::Point (rg->cx.computed, rg->cy.computed);
1042 break;
1043 case POINT_RG_FOCUS:
1044 p = Geom::Point (rg->fx.computed, rg->fy.computed);
1045 break;
1046 case POINT_RG_R1:
1047 p = Geom::Point (rg->cx.computed + rg->r.computed, rg->cy.computed);
1048 break;
1049 case POINT_RG_R2:
1050 p = Geom::Point (rg->cx.computed, rg->cy.computed - rg->r.computed);
1051 break;
1052 case POINT_RG_MID1:
1053 {
1054 gdouble offset = rg->vector.stops.at(point_i).offset;
1055 p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed + rg->r.computed, rg->cy.computed);
1056 }
1057 break;
1058 case POINT_RG_MID2:
1059 {
1060 gdouble offset = rg->vector.stops.at(point_i).offset;
1061 p = (1-offset) * Geom::Point (rg->cx.computed, rg->cy.computed) + offset * Geom::Point(rg->cx.computed, rg->cy.computed - rg->r.computed);
1062 }
1063 break;
1064 }
1065 }
1067 if (SP_GRADIENT(gradient)->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX) {
1068 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
1069 Geom::OptRect bbox = item->getBounds(Geom::identity()); // we need "true" bbox without item_i2d_affine
1070 if (bbox) {
1071 p *= Geom::Matrix(bbox->dimensions()[Geom::X], 0,
1072 0, bbox->dimensions()[Geom::Y],
1073 bbox->min()[Geom::X], bbox->min()[Geom::Y]);
1074 }
1075 }
1076 p *= Geom::Matrix(gradient->gradientTransform) * (Geom::Matrix)sp_item_i2d_affine(item);
1077 return from_2geom(p);
1078 }
1081 /**
1082 * Sets item fill or stroke to the gradient of the specified type with given vector, creating
1083 * new private gradient, if needed.
1084 * gr has to be a normalized vector.
1085 */
1087 SPGradient *sp_item_set_gradient(SPItem *item, SPGradient *gr, SPGradientType type, bool is_fill)
1088 {
1089 #ifdef SP_GR_VERBOSE
1090 g_message("sp_item_set_gradient(%p, %p, %d, %d)", item, gr, type, is_fill);
1091 #endif
1092 g_return_val_if_fail(item != NULL, NULL);
1093 g_return_val_if_fail(SP_IS_ITEM(item), NULL);
1094 g_return_val_if_fail(gr != NULL, NULL);
1095 g_return_val_if_fail(SP_IS_GRADIENT(gr), NULL);
1096 g_return_val_if_fail(gr->state == SP_GRADIENT_STATE_VECTOR, NULL);
1098 SPStyle *style = SP_OBJECT_STYLE(item);
1099 g_assert(style != NULL);
1101 SPPaintServer *ps = NULL;
1102 if (is_fill? style->fill.isPaintserver() : style->stroke.isPaintserver())
1103 ps = is_fill? SP_STYLE_FILL_SERVER(style) : SP_STYLE_STROKE_SERVER(style);
1105 if (ps
1106 && ( (type == SP_GRADIENT_TYPE_LINEAR && SP_IS_LINEARGRADIENT(ps)) ||
1107 (type == SP_GRADIENT_TYPE_RADIAL && SP_IS_RADIALGRADIENT(ps)) ) )
1108 {
1110 /* Current fill style is the gradient of the required type */
1111 SPGradient *current = SP_GRADIENT(ps);
1113 //g_message("hrefcount %d count %d\n", SP_OBJECT_HREFCOUNT(current), count_gradient_hrefs(SP_OBJECT(item), current));
1115 if (!current->isSwatch()
1116 && (SP_OBJECT_HREFCOUNT(current) == 1 ||
1117 SP_OBJECT_HREFCOUNT(current) == count_gradient_hrefs(SP_OBJECT(item), current))) {
1119 // current is private and it's either used once, or all its uses are by children of item;
1120 // so just change its href to vector
1122 if ( current != gr && current->getVector() != gr ) {
1123 /* href is not the vector */
1124 sp_gradient_repr_set_link(SP_OBJECT_REPR(current), gr);
1125 }
1126 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1127 return current;
1129 } else {
1131 // the gradient is not private, or it is shared with someone else;
1132 // normalize it (this includes creating new private if necessary)
1133 SPGradient *normalized = sp_gradient_fork_private_if_necessary(current, gr, type, item);
1135 g_return_val_if_fail(normalized != NULL, NULL);
1137 if (normalized != current) {
1139 /* We have to change object style here; recursive because this is used from
1140 * fill&stroke and must work for groups etc. */
1141 sp_style_set_property_url(SP_OBJECT(item), is_fill? "fill" : "stroke", SP_OBJECT(normalized), true);
1142 }
1143 SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1144 return normalized;
1145 }
1147 } else {
1148 /* Current fill style is not a gradient or wrong type, so construct everything */
1149 SPGradient *constructed = sp_gradient_get_private_normalized(SP_OBJECT_DOCUMENT(item), gr, type);
1150 constructed = sp_gradient_reset_to_userspace(constructed, item);
1151 sp_style_set_property_url(SP_OBJECT(item), ( is_fill ? "fill" : "stroke" ), SP_OBJECT(constructed), true);
1152 SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
1153 SP_OBJECT_STYLE_MODIFIED_FLAG ));
1154 return constructed;
1155 }
1156 }
1158 static void sp_gradient_repr_set_link(Inkscape::XML::Node *repr, SPGradient *link)
1159 {
1160 #ifdef SP_GR_VERBOSE
1161 g_message("sp_gradient_repr_set_link(%p, %p)", repr, link);
1162 #endif
1163 g_return_if_fail(repr != NULL);
1164 if (link) {
1165 g_return_if_fail(SP_IS_GRADIENT(link));
1166 }
1168 if (link) {
1169 Glib::ustring ref("#");
1170 ref += link->getId();
1171 repr->setAttribute("xlink:href", ref.c_str());
1172 } else {
1173 repr->setAttribute("xlink:href", 0);
1174 }
1175 }
1178 static void addStop( Inkscape::XML::Node *parent, Glib::ustring const &color, gint opacity, gchar const *offset )
1179 {
1180 #ifdef SP_GR_VERBOSE
1181 g_message("addStop(%p, %s, %d, %s)", parent, color.c_str(), opacity, offset);
1182 #endif
1183 Inkscape::XML::Node *stop = parent->document()->createElement("svg:stop");
1184 {
1185 gchar *tmp = g_strdup_printf( "stop-color:%s;stop-opacity:%d;", color.c_str(), opacity );
1186 stop->setAttribute( "style", tmp );
1187 g_free(tmp);
1188 }
1190 stop->setAttribute( "offset", offset );
1192 parent->appendChild(stop);
1193 Inkscape::GC::release(stop);
1194 }
1196 /*
1197 * Get default normalized gradient vector of document, create if there is none
1198 */
1199 SPGradient *sp_document_default_gradient_vector( SPDocument *document, SPColor const &color, bool singleStop )
1200 {
1201 SPDefs *defs = static_cast<SPDefs *>(SP_DOCUMENT_DEFS(document));
1202 Inkscape::XML::Document *xml_doc = document->rdoc;
1204 Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient");
1206 if ( !singleStop ) {
1207 repr->setAttribute("inkscape:collect", "always");
1208 // set here, but removed when it's edited in the gradient editor
1209 // to further reduce clutter, we could
1210 // (1) here, search gradients by color and return what is found without duplication
1211 // (2) in fill & stroke, show only one copy of each gradient in list
1212 }
1214 Glib::ustring colorStr = color.toString();
1215 addStop( repr, colorStr, 1, "0" );
1216 if ( !singleStop ) {
1217 addStop( repr, colorStr, 0, "1" );
1218 }
1220 SP_OBJECT_REPR(defs)->addChild(repr, NULL);
1221 Inkscape::GC::release(repr);
1223 /* fixme: This does not look like nice */
1224 SPGradient *gr = static_cast<SPGradient *>(document->getObjectByRepr(repr));
1225 g_assert(gr != NULL);
1226 g_assert(SP_IS_GRADIENT(gr));
1227 /* fixme: Maybe add extra sanity check here */
1228 gr->state = SP_GRADIENT_STATE_VECTOR;
1230 return gr;
1231 }
1233 /**
1234 Return the preferred vector for \a o, made from (in order of preference) its current vector,
1235 current fill or stroke color, or from desktop style if \a o is NULL or doesn't have style.
1236 */
1237 SPGradient *sp_gradient_vector_for_object( SPDocument *const doc, SPDesktop *const desktop,
1238 SPObject *const o, bool const is_fill, bool singleStop )
1239 {
1240 SPColor color;
1241 if (o == NULL || SP_OBJECT_STYLE(o) == NULL) {
1242 color = sp_desktop_get_color(desktop, is_fill);
1243 } else {
1244 // take the color of the object
1245 SPStyle const &style = *SP_OBJECT_STYLE(o);
1246 SPIPaint const &paint = ( is_fill
1247 ? style.fill
1248 : style.stroke );
1249 if (paint.isPaintserver()) {
1250 SPObject *server = is_fill? o->style->getFillPaintServer() : o->style->getStrokePaintServer();
1251 if ( SP_IS_GRADIENT(server) ) {
1252 return SP_GRADIENT(server)->getVector(true);
1253 } else {
1254 color = sp_desktop_get_color(desktop, is_fill);
1255 }
1256 } else if (paint.isColor()) {
1257 color = paint.value.color;
1258 } else {
1259 // if o doesn't use flat color, then take current color of the desktop.
1260 color = sp_desktop_get_color(desktop, is_fill);
1261 }
1262 }
1264 return sp_document_default_gradient_vector( doc, color, singleStop );
1265 }
1268 /*
1269 Local Variables:
1270 mode:c++
1271 c-file-style:"stroustrup"
1272 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1273 indent-tabs-mode:nil
1274 fill-column:99
1275 End:
1276 */
1277 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :