1 #define __SP_DESKTOP_STYLE_C__
3 /** \file
4 * Desktop style management
5 *
6 * Authors:
7 * bulia byak
8 * verbalshadow
9 *
10 * Copyright (C) 2004, 2006 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "desktop.h"
16 #include "color-rgba.h"
17 #include "svg/css-ostringstream.h"
18 #include "svg/svg.h"
19 #include "svg/svg-color.h"
20 #include "selection.h"
21 #include "inkscape.h"
22 #include "style.h"
23 #include "prefs-utils.h"
24 #include "sp-use.h"
25 #include "sp-feblend.h"
26 #include "sp-filter.h"
27 #include "sp-filter-reference.h"
28 #include "sp-gaussian-blur.h"
29 #include "sp-flowtext.h"
30 #include "sp-flowregion.h"
31 #include "sp-flowdiv.h"
32 #include "sp-linear-gradient.h"
33 #include "sp-pattern.h"
34 #include "sp-radial-gradient.h"
35 #include "sp-textpath.h"
36 #include "sp-tref.h"
37 #include "sp-tspan.h"
38 #include "xml/repr.h"
39 #include "libnrtype/font-style-to-pos.h"
40 #include "sp-path.h"
42 #include "desktop-style.h"
43 #include "svg/svg-icc-color.h"
44 #include "box3d-side.h"
46 /**
47 * Set color on selection on desktop.
48 */
49 void
50 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
51 {
52 /// \todo relative color setting
53 if (is_relative) {
54 g_warning("FIXME: relative color setting not yet implemented");
55 return;
56 }
58 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
59 gchar b[64];
60 sp_svg_write_color(b, sizeof(b), rgba);
61 SPCSSAttr *css = sp_repr_css_attr_new();
62 if (fill) {
63 sp_repr_css_set_property(css, "fill", b);
64 Inkscape::CSSOStringStream osalpha;
65 osalpha << color[3];
66 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
67 } else {
68 sp_repr_css_set_property(css, "stroke", b);
69 Inkscape::CSSOStringStream osalpha;
70 osalpha << color[3];
71 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
72 }
74 sp_desktop_set_style(desktop, css);
76 sp_repr_css_attr_unref(css);
77 }
79 /**
80 * Apply style on object and children, recursively.
81 */
82 void
83 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
84 {
85 // non-items should not have style
86 if (!SP_IS_ITEM(o))
87 return;
89 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
90 // but must always inherit from the parent text. Same for textPath.
91 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
93 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
94 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
95 // flowtext).
97 if (!(skip_lines
98 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
99 || SP_IS_FLOWDIV(o)
100 || SP_IS_FLOWPARA(o)
101 || SP_IS_TEXTPATH(o))
102 && !SP_OBJECT_REPR(o)->attribute("style"))
103 &&
104 !(SP_IS_FLOWREGION(o) ||
105 SP_IS_FLOWREGIONEXCLUDE(o) ||
106 (SP_IS_USE(o) &&
107 SP_OBJECT_PARENT(o) &&
108 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
109 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
110 )
111 )
112 )
113 ) {
115 SPCSSAttr *css_set = sp_repr_css_attr_new();
116 sp_repr_css_merge(css_set, css);
118 // Scale the style by the inverse of the accumulated parent transform in the paste context.
119 {
120 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
121 double const ex(NR::expansion(local));
122 if ( ( ex != 0. )
123 && ( ex != 1. ) ) {
124 sp_css_attr_scale(css_set, 1/ex);
125 }
126 }
128 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
130 sp_repr_css_attr_unref(css_set);
131 }
133 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
134 if (SP_IS_USE(o))
135 return;
137 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
138 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
139 // Unset properties which are accumulating and thus should not be set recursively.
140 // For example, setting opacity 0.5 on a group recursively would result in the visible opacity of 0.25 for an item in the group.
141 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
142 sp_repr_css_merge(css_recurse, css);
143 sp_repr_css_set_property(css_recurse, "opacity", NULL);
144 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
145 sp_repr_css_attr_unref(css_recurse);
146 } else {
147 sp_desktop_apply_css_recursive(child, css, skip_lines);
148 }
149 }
150 }
152 /**
153 * Apply style on selection on desktop.
154 */
155 void
156 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
157 {
158 if (write_current) {
159 // 1. Set internal value
160 sp_repr_css_merge(desktop->current, css);
162 // 1a. Write to prefs; make a copy and unset any URIs first
163 SPCSSAttr *css_write = sp_repr_css_attr_new();
164 sp_repr_css_merge(css_write, css);
165 sp_css_attr_unset_uris(css_write);
166 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
167 for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
168 /* last used styles for 3D box faces are stored separately */
169 if (SP_IS_BOX3D_SIDE (i->data)) {
170 //const char * descr = SP_OBJECT_REPR (G_OBJECT (i->data))->attribute ("inkscape:box3dside");
171 const char * descr = box3d_side_axes_string(SP_BOX3D_SIDE(i->data));
172 if (descr != NULL) {
173 g_print ("################ Box3DSide description found.\n");
174 gchar *style_grp = g_strconcat ("desktop.", descr, NULL);
175 sp_repr_css_change(inkscape_get_repr(INKSCAPE, style_grp), css_write, "style");
176 g_free (style_grp);
177 }
178 }
179 }
180 sp_repr_css_attr_unref(css_write);
181 }
183 if (!change)
184 return;
186 // 2. Emit signal
187 bool intercepted = desktop->_set_style_signal.emit(css);
189 /** \todo
190 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
191 * rect corners, font size for the object's own transform so that pasting
192 * fills does not depend on preserve/optimize.
193 */
195 // 3. If nobody has intercepted the signal, apply the style to the selection
196 if (!intercepted) {
197 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
198 /// \todo if the style is text-only, apply only to texts?
199 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
200 }
201 }
202 }
204 /**
205 * Return the desktop's current style.
206 */
207 SPCSSAttr *
208 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
209 {
210 SPCSSAttr *css = sp_repr_css_attr_new();
211 sp_repr_css_merge(css, desktop->current);
212 if (!css->attributeList()) {
213 sp_repr_css_attr_unref(css);
214 return NULL;
215 } else {
216 if (!with_text) {
217 css = sp_css_attr_unset_text(css);
218 }
219 return css;
220 }
221 }
223 /**
224 * Return the desktop's current color.
225 */
226 guint32
227 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
228 {
229 guint32 r = 0; // if there's no color, return black
230 gchar const *property = sp_repr_css_property(desktop->current,
231 is_fill ? "fill" : "stroke",
232 "#000");
234 if (desktop->current && property) { // if there is style and the property in it,
235 if (strncmp(property, "url", 3)) { // and if it's not url,
236 // read it
237 r = sp_svg_read_color(property, r);
238 }
239 }
241 return r;
242 }
244 double
245 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
246 {
247 SPCSSAttr *css = NULL;
248 gfloat value = 1.0; // default if nothing else found
249 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
250 css = sp_desktop_get_style(desktop, true);
251 } else {
252 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
253 if (tool_repr) {
254 css = sp_repr_css_attr_inherited(tool_repr, "style");
255 }
256 }
258 if (css) {
259 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
261 if (desktop->current && property) { // if there is style and the property in it,
262 if ( !sp_svg_number_read_f(property, &value) ) {
263 value = 1.0; // things failed. set back to the default
264 }
265 }
267 sp_repr_css_attr_unref(css);
268 }
270 return value;
271 }
272 double
273 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
274 {
275 SPCSSAttr *css = NULL;
276 gfloat value = 1.0; // default if nothing else found
277 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
278 css = sp_desktop_get_style(desktop, true);
279 } else {
280 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
281 if (tool_repr) {
282 css = sp_repr_css_attr_inherited(tool_repr, "style");
283 }
284 }
286 if (css) {
287 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
289 if (desktop->current && property) { // if there is style and the property in it,
290 if ( !sp_svg_number_read_f(property, &value) ) {
291 value = 1.0; // things failed. set back to the default
292 }
293 }
295 sp_repr_css_attr_unref(css);
296 }
298 return value;
299 }
300 guint32
301 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
302 {
303 SPCSSAttr *css = NULL;
304 guint32 r = 0; // if there's no color, return black
305 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
306 css = sp_desktop_get_style(desktop, true);
307 } else {
308 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
309 if (tool_repr) {
310 css = sp_repr_css_attr_inherited(tool_repr, "style");
311 }
312 }
314 if (css) {
315 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
317 if (desktop->current && property) { // if there is style and the property in it,
318 if (strncmp(property, "url", 3)) { // and if it's not url,
319 // read it
320 r = sp_svg_read_color(property, r);
321 }
322 }
324 sp_repr_css_attr_unref(css);
325 }
327 return r | 0xff;
328 }
329 /**
330 * Apply the desktop's current style or the tool style to repr.
331 */
332 void
333 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
334 {
335 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
336 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
337 sp_repr_css_set(repr, css_current, "style");
338 } else {
339 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
340 if (tool_repr) {
341 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
342 sp_repr_css_set(repr, css, "style");
343 sp_repr_css_attr_unref(css);
344 }
345 }
346 if (css_current) {
347 sp_repr_css_attr_unref(css_current);
348 }
349 }
351 /**
352 * Returns the font size (in SVG pixels) of the text tool style (if text
353 * tool uses its own style) or desktop style (otherwise).
354 */
355 double
356 sp_desktop_get_font_size_tool(SPDesktop *desktop)
357 {
358 (void)desktop; // TODO cleanup
359 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
360 gchar const *style_str = NULL;
361 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
362 style_str = desktop_style;
363 } else {
364 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
365 if (tool_repr) {
366 style_str = tool_repr->attribute("style");
367 }
368 }
370 double ret = 12;
371 if (style_str) {
372 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
373 sp_style_merge_from_style_string(style, style_str);
374 ret = style->font_size.computed;
375 sp_style_unref(style);
376 }
377 return ret;
378 }
380 /** Determine average stroke width, simple method */
381 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
382 gdouble
383 stroke_average_width (GSList const *objects)
384 {
385 if (g_slist_length ((GSList *) objects) == 0)
386 return NR_HUGE;
388 gdouble avgwidth = 0.0;
389 bool notstroked = true;
390 int n_notstroked = 0;
392 for (GSList const *l = objects; l != NULL; l = l->next) {
393 if (!SP_IS_ITEM (l->data))
394 continue;
396 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
398 SPObject *object = SP_OBJECT(l->data);
400 if ( object->style->stroke.isNone() ) {
401 ++n_notstroked; // do not count nonstroked objects
402 continue;
403 } else {
404 notstroked = false;
405 }
407 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
408 }
410 if (notstroked)
411 return NR_HUGE;
413 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
414 }
416 /**
417 * Write to style_res the average fill or stroke of list of objects, if applicable.
418 */
419 int
420 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
421 {
422 if (g_slist_length(objects) == 0) {
423 /* No objects, set empty */
424 return QUERY_STYLE_NOTHING;
425 }
427 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
428 bool paintImpossible = true;
429 paint_res->set = TRUE;
431 SVGICCColor* iccColor = 0;
432 bool iccSeen = false;
433 gfloat c[4];
434 c[0] = c[1] = c[2] = c[3] = 0.0;
435 gint num = 0;
437 gfloat prev[3];
438 prev[0] = prev[1] = prev[2] = 0.0;
439 bool same_color = true;
441 for (GSList const *i = objects; i != NULL; i = i->next) {
442 SPObject *obj = SP_OBJECT (i->data);
443 SPStyle *style = SP_OBJECT_STYLE (obj);
444 if (!style) continue;
446 SPIPaint *paint = isfill? &style->fill : &style->stroke;
448 // We consider paint "effectively set" for anything within text hierarchy
449 SPObject *parent = SP_OBJECT_PARENT (obj);
450 bool paint_effectively_set =
451 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
452 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
453 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
455 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
457 if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
458 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
459 }
461 if (paint_res->set && paint->set && paint_res->isPaintserver()) {
462 // both previous paint and this paint were a server, see if the servers are compatible
464 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
465 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
467 if (SP_IS_LINEARGRADIENT (server_res)) {
469 if (!SP_IS_LINEARGRADIENT(server))
470 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
472 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
473 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
474 if (vector_res != vector)
475 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
477 } else if (SP_IS_RADIALGRADIENT (server_res)) {
479 if (!SP_IS_RADIALGRADIENT(server))
480 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
482 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
483 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
484 if (vector_res != vector)
485 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
487 } else if (SP_IS_PATTERN (server_res)) {
489 if (!SP_IS_PATTERN(server))
490 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
492 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
493 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
494 if (pat_res != pat)
495 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
496 }
497 }
499 // 2. Sum color, copy server from paint to paint_res
501 if (paint_res->set && paint_effectively_set && paint->isColor()) {
502 gfloat d[3];
503 sp_color_get_rgb_floatv (&paint->value.color, d);
505 // Check if this color is the same as previous
506 if (paintImpossible) {
507 prev[0] = d[0];
508 prev[1] = d[1];
509 prev[2] = d[2];
510 paint_res->setColor(d[0], d[1], d[2]);
511 iccColor = paint->value.color.icc;
512 iccSeen = true;
513 } else {
514 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
515 same_color = false;
516 if ( iccSeen ) {
517 if(paint->value.color.icc) {
518 // TODO fix this
519 iccColor = 0;
520 }
521 }
522 }
524 // average color
525 c[0] += d[0];
526 c[1] += d[1];
527 c[2] += d[2];
528 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
529 num ++;
530 }
532 paintImpossible = false;
533 paint_res->colorSet = paint->colorSet;
534 paint_res->currentcolor = paint->currentcolor;
535 if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
536 if (isfill) {
537 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
538 } else {
539 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
540 }
541 }
542 paint_res->set = paint_effectively_set;
543 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
544 }
546 // After all objects processed, divide the color if necessary and return
547 if (paint_res->set && paint_res->isColor()) { // set the color
548 g_assert (num >= 1);
550 c[0] /= num;
551 c[1] /= num;
552 c[2] /= num;
553 c[3] /= num;
554 paint_res->setColor(c[0], c[1], c[2]);
555 if (isfill) {
556 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
557 } else {
558 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
559 }
562 if ( iccSeen && iccColor ) {
563 // TODO check for existing
564 SVGICCColor* tmp = new SVGICCColor(*iccColor);
565 paint_res->value.color.icc = tmp;
566 }
568 if (num > 1) {
569 if (same_color)
570 return QUERY_STYLE_MULTIPLE_SAME;
571 else
572 return QUERY_STYLE_MULTIPLE_AVERAGED;
573 } else {
574 return QUERY_STYLE_SINGLE;
575 }
576 }
578 // Not color
579 if (g_slist_length(objects) > 1) {
580 return QUERY_STYLE_MULTIPLE_SAME;
581 } else {
582 return QUERY_STYLE_SINGLE;
583 }
584 }
586 /**
587 * Write to style_res the average opacity of a list of objects.
588 */
589 int
590 objects_query_opacity (GSList *objects, SPStyle *style_res)
591 {
592 if (g_slist_length(objects) == 0) {
593 /* No objects, set empty */
594 return QUERY_STYLE_NOTHING;
595 }
597 gdouble opacity_sum = 0;
598 gdouble opacity_prev = -1;
599 bool same_opacity = true;
600 guint opacity_items = 0;
603 for (GSList const *i = objects; i != NULL; i = i->next) {
604 SPObject *obj = SP_OBJECT (i->data);
605 SPStyle *style = SP_OBJECT_STYLE (obj);
606 if (!style) continue;
608 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
609 opacity_sum += opacity;
610 if (opacity_prev != -1 && opacity != opacity_prev)
611 same_opacity = false;
612 opacity_prev = opacity;
613 opacity_items ++;
614 }
615 if (opacity_items > 1)
616 opacity_sum /= opacity_items;
618 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
620 if (opacity_items == 0) {
621 return QUERY_STYLE_NOTHING;
622 } else if (opacity_items == 1) {
623 return QUERY_STYLE_SINGLE;
624 } else {
625 if (same_opacity)
626 return QUERY_STYLE_MULTIPLE_SAME;
627 else
628 return QUERY_STYLE_MULTIPLE_AVERAGED;
629 }
630 }
632 /**
633 * Write to style_res the average stroke width of a list of objects.
634 */
635 int
636 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
637 {
638 if (g_slist_length(objects) == 0) {
639 /* No objects, set empty */
640 return QUERY_STYLE_NOTHING;
641 }
643 gdouble avgwidth = 0.0;
645 gdouble prev_sw = -1;
646 bool same_sw = true;
648 int n_stroked = 0;
650 for (GSList const *i = objects; i != NULL; i = i->next) {
651 SPObject *obj = SP_OBJECT (i->data);
652 if (!SP_IS_ITEM(obj)) continue;
653 SPStyle *style = SP_OBJECT_STYLE (obj);
654 if (!style) continue;
656 if ( style->stroke.isNone() ) {
657 continue;
658 }
660 n_stroked ++;
662 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
663 double sw = style->stroke_width.computed * i2d.expansion();
665 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
666 same_sw = false;
667 prev_sw = sw;
669 avgwidth += sw;
670 }
672 if (n_stroked > 1)
673 avgwidth /= (n_stroked);
675 style_res->stroke_width.computed = avgwidth;
676 style_res->stroke_width.set = true;
678 if (n_stroked == 0) {
679 return QUERY_STYLE_NOTHING;
680 } else if (n_stroked == 1) {
681 return QUERY_STYLE_SINGLE;
682 } else {
683 if (same_sw)
684 return QUERY_STYLE_MULTIPLE_SAME;
685 else
686 return QUERY_STYLE_MULTIPLE_AVERAGED;
687 }
688 }
690 /**
691 * Write to style_res the average miter limit of a list of objects.
692 */
693 int
694 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
695 {
696 if (g_slist_length(objects) == 0) {
697 /* No objects, set empty */
698 return QUERY_STYLE_NOTHING;
699 }
701 gdouble avgml = 0.0;
702 int n_stroked = 0;
704 gdouble prev_ml = -1;
705 bool same_ml = true;
707 for (GSList const *i = objects; i != NULL; i = i->next) {
708 SPObject *obj = SP_OBJECT (i->data);
709 if (!SP_IS_ITEM(obj)) continue;
710 SPStyle *style = SP_OBJECT_STYLE (obj);
711 if (!style) continue;
713 if ( style->stroke.isNone() ) {
714 continue;
715 }
717 n_stroked ++;
719 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
720 same_ml = false;
721 prev_ml = style->stroke_miterlimit.value;
723 avgml += style->stroke_miterlimit.value;
724 }
726 if (n_stroked > 1)
727 avgml /= (n_stroked);
729 style_res->stroke_miterlimit.value = avgml;
730 style_res->stroke_miterlimit.set = true;
732 if (n_stroked == 0) {
733 return QUERY_STYLE_NOTHING;
734 } else if (n_stroked == 1) {
735 return QUERY_STYLE_SINGLE;
736 } else {
737 if (same_ml)
738 return QUERY_STYLE_MULTIPLE_SAME;
739 else
740 return QUERY_STYLE_MULTIPLE_AVERAGED;
741 }
742 }
744 /**
745 * Write to style_res the stroke cap of a list of objects.
746 */
747 int
748 objects_query_strokecap (GSList *objects, SPStyle *style_res)
749 {
750 if (g_slist_length(objects) == 0) {
751 /* No objects, set empty */
752 return QUERY_STYLE_NOTHING;
753 }
755 int cap = -1;
756 gdouble prev_cap = -1;
757 bool same_cap = true;
758 int n_stroked = 0;
760 for (GSList const *i = objects; i != NULL; i = i->next) {
761 SPObject *obj = SP_OBJECT (i->data);
762 if (!SP_IS_ITEM(obj)) continue;
763 SPStyle *style = SP_OBJECT_STYLE (obj);
764 if (!style) continue;
766 if ( style->stroke.isNone() ) {
767 continue;
768 }
770 n_stroked ++;
772 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
773 same_cap = false;
774 prev_cap = style->stroke_linecap.value;
776 cap = style->stroke_linecap.value;
777 }
779 style_res->stroke_linecap.value = cap;
780 style_res->stroke_linecap.set = true;
782 if (n_stroked == 0) {
783 return QUERY_STYLE_NOTHING;
784 } else if (n_stroked == 1) {
785 return QUERY_STYLE_SINGLE;
786 } else {
787 if (same_cap)
788 return QUERY_STYLE_MULTIPLE_SAME;
789 else
790 return QUERY_STYLE_MULTIPLE_DIFFERENT;
791 }
792 }
794 /**
795 * Write to style_res the stroke join of a list of objects.
796 */
797 int
798 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
799 {
800 if (g_slist_length(objects) == 0) {
801 /* No objects, set empty */
802 return QUERY_STYLE_NOTHING;
803 }
805 int join = -1;
806 gdouble prev_join = -1;
807 bool same_join = true;
808 int n_stroked = 0;
810 for (GSList const *i = objects; i != NULL; i = i->next) {
811 SPObject *obj = SP_OBJECT (i->data);
812 if (!SP_IS_ITEM(obj)) continue;
813 SPStyle *style = SP_OBJECT_STYLE (obj);
814 if (!style) continue;
816 if ( style->stroke.isNone() ) {
817 continue;
818 }
820 n_stroked ++;
822 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
823 same_join = false;
824 prev_join = style->stroke_linejoin.value;
826 join = style->stroke_linejoin.value;
827 }
829 style_res->stroke_linejoin.value = join;
830 style_res->stroke_linejoin.set = true;
832 if (n_stroked == 0) {
833 return QUERY_STYLE_NOTHING;
834 } else if (n_stroked == 1) {
835 return QUERY_STYLE_SINGLE;
836 } else {
837 if (same_join)
838 return QUERY_STYLE_MULTIPLE_SAME;
839 else
840 return QUERY_STYLE_MULTIPLE_DIFFERENT;
841 }
842 }
844 /**
845 * Write to style_res the average font size and spacing of objects.
846 */
847 int
848 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
849 {
850 bool different = false;
852 double size = 0;
853 double letterspacing = 0;
854 double linespacing = 0;
855 bool linespacing_normal = false;
856 bool letterspacing_normal = false;
858 double size_prev = 0;
859 double letterspacing_prev = 0;
860 double linespacing_prev = 0;
862 /// \todo FIXME: add word spacing, kerns? rotates?
864 int texts = 0;
866 for (GSList const *i = objects; i != NULL; i = i->next) {
867 SPObject *obj = SP_OBJECT (i->data);
869 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
870 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
871 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
872 continue;
874 SPStyle *style = SP_OBJECT_STYLE (obj);
875 if (!style) continue;
877 texts ++;
878 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
880 if (style->letter_spacing.normal) {
881 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
882 letterspacing_normal = true;
883 } else {
884 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
885 letterspacing_normal = false;
886 }
888 double linespacing_current;
889 if (style->line_height.normal) {
890 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
891 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
892 linespacing_normal = true;
893 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
894 linespacing_current = style->line_height.value;
895 linespacing_normal = false;
896 } else { // we need % here
897 linespacing_current = style->line_height.computed / style->font_size.computed;
898 linespacing_normal = false;
899 }
900 linespacing += linespacing_current;
902 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
903 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
904 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
905 different = true;
906 }
908 size_prev = style->font_size.computed;
909 letterspacing_prev = style->letter_spacing.computed;
910 linespacing_prev = linespacing_current;
912 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
913 style_res->text_anchor.computed = style->text_anchor.computed;
914 style_res->writing_mode.computed = style->writing_mode.computed;
915 }
917 if (texts == 0)
918 return QUERY_STYLE_NOTHING;
920 if (texts > 1) {
921 size /= texts;
922 letterspacing /= texts;
923 linespacing /= texts;
924 }
926 style_res->font_size.computed = size;
927 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
929 style_res->letter_spacing.normal = letterspacing_normal;
930 style_res->letter_spacing.computed = letterspacing;
932 style_res->line_height.normal = linespacing_normal;
933 style_res->line_height.computed = linespacing;
934 style_res->line_height.value = linespacing;
935 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
937 if (texts > 1) {
938 if (different) {
939 return QUERY_STYLE_MULTIPLE_AVERAGED;
940 } else {
941 return QUERY_STYLE_MULTIPLE_SAME;
942 }
943 } else {
944 return QUERY_STYLE_SINGLE;
945 }
946 }
948 /**
949 * Write to style_res the average font style of objects.
950 */
951 int
952 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
953 {
954 bool different = false;
955 bool set = false;
957 int texts = 0;
959 for (GSList const *i = objects; i != NULL; i = i->next) {
960 SPObject *obj = SP_OBJECT (i->data);
962 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
963 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
964 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
965 continue;
967 SPStyle *style = SP_OBJECT_STYLE (obj);
968 if (!style) continue;
970 texts ++;
972 if (set &&
973 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
974 different = true; // different styles
975 }
977 set = TRUE;
978 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
979 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
980 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
981 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
982 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
983 }
985 if (texts == 0 || !set)
986 return QUERY_STYLE_NOTHING;
988 if (texts > 1) {
989 if (different) {
990 return QUERY_STYLE_MULTIPLE_DIFFERENT;
991 } else {
992 return QUERY_STYLE_MULTIPLE_SAME;
993 }
994 } else {
995 return QUERY_STYLE_SINGLE;
996 }
997 }
999 /**
1000 * Write to style_res the average font family of objects.
1001 */
1002 int
1003 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1004 {
1005 bool different = false;
1006 int texts = 0;
1008 if (style_res->text->font_family.value) {
1009 g_free(style_res->text->font_family.value);
1010 style_res->text->font_family.value = NULL;
1011 }
1012 style_res->text->font_family.set = FALSE;
1014 for (GSList const *i = objects; i != NULL; i = i->next) {
1015 SPObject *obj = SP_OBJECT (i->data);
1017 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1018 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1019 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1020 continue;
1022 SPStyle *style = SP_OBJECT_STYLE (obj);
1023 if (!style) continue;
1025 texts ++;
1027 if (style_res->text->font_family.value && style->text->font_family.value &&
1028 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1029 different = true; // different fonts
1030 }
1032 if (style_res->text->font_family.value) {
1033 g_free(style_res->text->font_family.value);
1034 style_res->text->font_family.value = NULL;
1035 }
1037 style_res->text->font_family.set = TRUE;
1038 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1039 }
1041 if (texts == 0 || !style_res->text->font_family.set)
1042 return QUERY_STYLE_NOTHING;
1044 if (texts > 1) {
1045 if (different) {
1046 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1047 } else {
1048 return QUERY_STYLE_MULTIPLE_SAME;
1049 }
1050 } else {
1051 return QUERY_STYLE_SINGLE;
1052 }
1053 }
1055 int
1056 objects_query_blend (GSList *objects, SPStyle *style_res)
1057 {
1058 const int empty_prev = -2;
1059 const int complex_filter = 5;
1060 int blend = 0;
1061 float blend_prev = empty_prev;
1062 bool same_blend = true;
1063 guint items = 0;
1065 for (GSList const *i = objects; i != NULL; i = i->next) {
1066 SPObject *obj = SP_OBJECT (i->data);
1067 SPStyle *style = SP_OBJECT_STYLE (obj);
1068 if(!style || !SP_IS_ITEM(obj)) continue;
1070 items++;
1072 //if object has a filter
1073 if (style->filter.set && style->getFilter()) {
1074 int blurcount = 0;
1075 int blendcount = 0;
1077 // determine whether filter is simple (blend and/or blur) or complex
1078 for(SPObject *primitive_obj = style->getFilter()->children;
1079 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1080 primitive_obj = primitive_obj->next) {
1081 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1082 if(SP_IS_FEBLEND(primitive))
1083 ++blendcount;
1084 else if(SP_IS_GAUSSIANBLUR(primitive))
1085 ++blurcount;
1086 else {
1087 blurcount = complex_filter;
1088 break;
1089 }
1090 }
1092 // simple filter
1093 if(blurcount == 1 || blendcount == 1) {
1094 for(SPObject *primitive_obj = style->getFilter()->children;
1095 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1096 primitive_obj = primitive_obj->next) {
1097 if(SP_IS_FEBLEND(primitive_obj)) {
1098 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1099 blend = spblend->blend_mode;
1100 }
1101 }
1102 }
1103 else {
1104 blend = complex_filter;
1105 }
1106 }
1107 // defaults to blend mode = "normal"
1108 else {
1109 blend = 0;
1110 }
1112 if(blend_prev != empty_prev && blend_prev != blend)
1113 same_blend = false;
1114 blend_prev = blend;
1115 }
1117 if (items > 0) {
1118 style_res->filter_blend_mode.value = blend;
1119 }
1121 if (items == 0) {
1122 return QUERY_STYLE_NOTHING;
1123 } else if (items == 1) {
1124 return QUERY_STYLE_SINGLE;
1125 } else {
1126 if(same_blend)
1127 return QUERY_STYLE_MULTIPLE_SAME;
1128 else
1129 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1130 }
1131 }
1133 /**
1134 * Write to style_res the average blurring of a list of objects.
1135 */
1136 int
1137 objects_query_blur (GSList *objects, SPStyle *style_res)
1138 {
1139 if (g_slist_length(objects) == 0) {
1140 /* No objects, set empty */
1141 return QUERY_STYLE_NOTHING;
1142 }
1144 float blur_sum = 0;
1145 float blur_prev = -1;
1146 bool same_blur = true;
1147 guint blur_items = 0;
1148 guint items = 0;
1150 for (GSList const *i = objects; i != NULL; i = i->next) {
1151 SPObject *obj = SP_OBJECT (i->data);
1152 SPStyle *style = SP_OBJECT_STYLE (obj);
1153 if (!style) continue;
1154 if (!SP_IS_ITEM(obj)) continue;
1156 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1158 items ++;
1160 //if object has a filter
1161 if (style->filter.set && style->getFilter()) {
1162 //cycle through filter primitives
1163 SPObject *primitive_obj = style->getFilter()->children;
1164 while (primitive_obj) {
1165 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1166 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1168 //if primitive is gaussianblur
1169 if(SP_IS_GAUSSIANBLUR(primitive)) {
1170 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1171 float num = spblur->stdDeviation.getNumber();
1172 blur_sum += num * NR::expansion(i2d);
1173 if (blur_prev != -1 && fabs (num - blur_prev) > 1e-2) // rather low tolerance because difference in blur radii is much harder to notice than e.g. difference in sizes
1174 same_blur = false;
1175 blur_prev = num;
1176 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1177 blur_items ++;
1178 }
1179 }
1180 primitive_obj = primitive_obj->next;
1181 }
1182 }
1183 }
1185 if (items > 0) {
1186 if (blur_items > 0)
1187 blur_sum /= blur_items;
1188 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1189 }
1191 if (items == 0) {
1192 return QUERY_STYLE_NOTHING;
1193 } else if (items == 1) {
1194 return QUERY_STYLE_SINGLE;
1195 } else {
1196 if (same_blur)
1197 return QUERY_STYLE_MULTIPLE_SAME;
1198 else
1199 return QUERY_STYLE_MULTIPLE_AVERAGED;
1200 }
1201 }
1203 /**
1204 * Query the given list of objects for the given property, write
1205 * the result to style, return appropriate flag.
1206 */
1207 int
1208 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1209 {
1210 if (property == QUERY_STYLE_PROPERTY_FILL) {
1211 return objects_query_fillstroke (list, style, true);
1212 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1213 return objects_query_fillstroke (list, style, false);
1215 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1216 return objects_query_strokewidth (list, style);
1217 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1218 return objects_query_miterlimit (list, style);
1219 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1220 return objects_query_strokecap (list, style);
1221 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1222 return objects_query_strokejoin (list, style);
1224 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1225 return objects_query_opacity (list, style);
1227 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1228 return objects_query_fontfamily (list, style);
1229 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1230 return objects_query_fontstyle (list, style);
1231 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1232 return objects_query_fontnumbers (list, style);
1234 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1235 return objects_query_blend (list, style);
1236 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1237 return objects_query_blur (list, style);
1238 }
1239 return QUERY_STYLE_NOTHING;
1240 }
1243 /**
1244 * Query the subselection (if any) or selection on the given desktop for the given property, write
1245 * the result to style, return appropriate flag.
1246 */
1247 int
1248 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1249 {
1250 int ret = desktop->_query_style_signal.emit(style, property);
1252 if (ret != QUERY_STYLE_NOTHING)
1253 return ret; // subselection returned a style, pass it on
1255 // otherwise, do querying and averaging over selection
1256 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1257 }
1259 /**
1260 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1261 * the properties returned QUERY_STYLE_NOTHING.
1262 */
1263 bool
1264 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1265 {
1266 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1267 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1268 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1269 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1270 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1271 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1272 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1273 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1274 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1275 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1276 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1278 return (result_family != QUERY_STYLE_NOTHING && result_fstyle != QUERY_STYLE_NOTHING && result_fnumbers != QUERY_STYLE_NOTHING && result_fill != QUERY_STYLE_NOTHING && result_stroke != QUERY_STYLE_NOTHING && result_opacity != QUERY_STYLE_NOTHING && result_strokewidth != QUERY_STYLE_NOTHING && result_strokemiterlimit != QUERY_STYLE_NOTHING && result_strokecap != QUERY_STYLE_NOTHING && result_strokejoin != QUERY_STYLE_NOTHING && result_blur != QUERY_STYLE_NOTHING);
1279 }
1282 /*
1283 Local Variables:
1284 mode:c++
1285 c-file-style:"stroustrup"
1286 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1287 indent-tabs-mode:nil
1288 fill-column:99
1289 End:
1290 */
1291 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :