e11fa14939204e7efb06b2e4e846a93bc96a1c26
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 <string>
16 #include <cstring>
18 #include "desktop.h"
19 #include "color-rgba.h"
20 #include "svg/css-ostringstream.h"
21 #include "svg/svg.h"
22 #include "svg/svg-color.h"
23 #include "selection.h"
24 #include "inkscape.h"
25 #include "style.h"
26 #include "preferences.h"
27 #include "sp-use.h"
28 #include "filters/blend.h"
29 #include "sp-filter.h"
30 #include "sp-filter-reference.h"
31 #include "sp-gaussian-blur.h"
32 #include "sp-flowtext.h"
33 #include "sp-flowregion.h"
34 #include "sp-flowdiv.h"
35 #include "sp-linear-gradient.h"
36 #include "sp-pattern.h"
37 #include "sp-radial-gradient.h"
38 #include "sp-textpath.h"
39 #include "sp-tref.h"
40 #include "sp-tspan.h"
41 #include "xml/repr.h"
42 #include "libnrtype/font-style-to-pos.h"
43 #include "sp-path.h"
45 #include "desktop-style.h"
46 #include "svg/svg-icc-color.h"
47 #include "svg/svg-device-color.h"
48 #include "box3d-side.h"
50 /**
51 * Set color on selection on desktop.
52 */
53 void
54 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
55 {
56 /// \todo relative color setting
57 if (is_relative) {
58 g_warning("FIXME: relative color setting not yet implemented");
59 return;
60 }
62 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
63 gchar b[64];
64 sp_svg_write_color(b, sizeof(b), rgba);
65 SPCSSAttr *css = sp_repr_css_attr_new();
66 if (fill) {
67 sp_repr_css_set_property(css, "fill", b);
68 Inkscape::CSSOStringStream osalpha;
69 osalpha << color[3];
70 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
71 } else {
72 sp_repr_css_set_property(css, "stroke", b);
73 Inkscape::CSSOStringStream osalpha;
74 osalpha << color[3];
75 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
76 }
78 sp_desktop_set_style(desktop, css);
80 sp_repr_css_attr_unref(css);
81 }
83 /**
84 * Apply style on object and children, recursively.
85 */
86 void
87 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
88 {
89 // non-items should not have style
90 if (!SP_IS_ITEM(o))
91 return;
93 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
94 // but must always inherit from the parent text. Same for textPath.
95 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
97 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
98 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
99 // flowtext).
101 if (!(skip_lines
102 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
103 || SP_IS_FLOWDIV(o)
104 || SP_IS_FLOWPARA(o)
105 || SP_IS_TEXTPATH(o))
106 && !SP_OBJECT_REPR(o)->attribute("style"))
107 &&
108 !(SP_IS_FLOWREGION(o) ||
109 SP_IS_FLOWREGIONEXCLUDE(o) ||
110 (SP_IS_USE(o) &&
111 SP_OBJECT_PARENT(o) &&
112 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
113 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
114 )
115 )
116 )
117 ) {
119 SPCSSAttr *css_set = sp_repr_css_attr_new();
120 sp_repr_css_merge(css_set, css);
122 // Scale the style by the inverse of the accumulated parent transform in the paste context.
123 {
124 Geom::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
125 double const ex(local.descrim());
126 if ( ( ex != 0. )
127 && ( ex != 1. ) ) {
128 sp_css_attr_scale(css_set, 1/ex);
129 }
130 }
132 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
134 sp_repr_css_attr_unref(css_set);
135 }
137 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
138 if (SP_IS_USE(o))
139 return;
141 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
142 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
143 // Unset properties which are accumulating and thus should not be set recursively.
144 // 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.
145 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
146 sp_repr_css_merge(css_recurse, css);
147 sp_repr_css_set_property(css_recurse, "opacity", NULL);
148 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
149 sp_repr_css_attr_unref(css_recurse);
150 } else {
151 sp_desktop_apply_css_recursive(child, css, skip_lines);
152 }
153 }
154 }
156 /**
157 * Apply style on selection on desktop.
158 */
159 void
160 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
161 {
162 if (write_current) {
163 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
164 // 1. Set internal value
165 sp_repr_css_merge(desktop->current, css);
167 // 1a. Write to prefs; make a copy and unset any URIs first
168 SPCSSAttr *css_write = sp_repr_css_attr_new();
169 sp_repr_css_merge(css_write, css);
170 sp_css_attr_unset_uris(css_write);
171 prefs->mergeStyle("/desktop/style", css_write);
173 for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
174 /* last used styles for 3D box faces are stored separately */
175 if (SP_IS_BOX3D_SIDE (i->data)) {
176 const char * descr = box3d_side_axes_string(SP_BOX3D_SIDE(i->data));
177 if (descr != NULL) {
178 prefs->mergeStyle(Glib::ustring("/desktop/") + descr + "/style", css_write);
179 }
180 }
181 }
182 sp_repr_css_attr_unref(css_write);
183 }
185 if (!change)
186 return;
188 // 2. Emit signal
189 bool intercepted = desktop->_set_style_signal.emit(css);
191 /** \todo
192 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
193 * rect corners, font size for the object's own transform so that pasting
194 * fills does not depend on preserve/optimize.
195 */
197 // 3. If nobody has intercepted the signal, apply the style to the selection
198 if (!intercepted) {
200 // Remove text attributes if not text...
201 // Do this once in case a zillion objects are selected.
202 SPCSSAttr *css_no_text = sp_repr_css_attr_new();
203 sp_repr_css_merge(css_no_text, css);
204 css_no_text = sp_css_attr_unset_text(css_no_text);
206 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
208 // If not text, don't apply text attributes (can a group have text attributes?)
209 if ( SP_IS_TEXT(i->data) || SP_IS_FLOWTEXT(i->data)
210 || SP_IS_TSPAN(i->data) || SP_IS_TREF(i->data) || SP_IS_TEXTPATH(i->data)
211 || SP_IS_FLOWDIV(i->data) || SP_IS_FLOWPARA(i->data) || SP_IS_FLOWTSPAN(i->data)) {
213 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
215 } else {
217 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css_no_text, true);
219 }
220 }
221 sp_repr_css_attr_unref(css_no_text);
222 }
223 }
225 /**
226 * Return the desktop's current style.
227 */
228 SPCSSAttr *
229 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
230 {
231 SPCSSAttr *css = sp_repr_css_attr_new();
232 sp_repr_css_merge(css, desktop->current);
233 if (!css->attributeList()) {
234 sp_repr_css_attr_unref(css);
235 return NULL;
236 } else {
237 if (!with_text) {
238 css = sp_css_attr_unset_text(css);
239 }
240 return css;
241 }
242 }
244 /**
245 * Return the desktop's current color.
246 */
247 guint32
248 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
249 {
250 guint32 r = 0; // if there's no color, return black
251 gchar const *property = sp_repr_css_property(desktop->current,
252 is_fill ? "fill" : "stroke",
253 "#000");
255 if (desktop->current && property) { // if there is style and the property in it,
256 if (strncmp(property, "url", 3)) { // and if it's not url,
257 // read it
258 r = sp_svg_read_color(property, r);
259 }
260 }
262 return r;
263 }
265 double
266 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool *has_opacity)
267 {
268 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
269 SPCSSAttr *css = NULL;
270 gfloat value = 1.0; // default if nothing else found
271 if (has_opacity)
272 *has_opacity = false;
273 if (prefs->getBool(tool + "/usecurrent")) {
274 css = sp_desktop_get_style(desktop, true);
275 } else {
276 css = prefs->getStyle(tool + "/style");
277 }
279 if (css) {
280 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
282 if (desktop->current && property) { // if there is style and the property in it,
283 if ( !sp_svg_number_read_f(property, &value) ) {
284 value = 1.0; // things failed. set back to the default
285 } else {
286 if (has_opacity)
287 *has_opacity = false;
288 }
289 }
291 sp_repr_css_attr_unref(css);
292 }
294 return value;
295 }
296 double
297 sp_desktop_get_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill)
298 {
299 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
300 SPCSSAttr *css = NULL;
301 gfloat value = 1.0; // default if nothing else found
302 if (prefs->getBool(tool + "/usecurrent")) {
303 css = sp_desktop_get_style(desktop, true);
304 } else {
305 css = prefs->getStyle(tool + "/style");
306 }
308 if (css) {
309 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
311 if (desktop->current && property) { // if there is style and the property in it,
312 if ( !sp_svg_number_read_f(property, &value) ) {
313 value = 1.0; // things failed. set back to the default
314 }
315 }
317 sp_repr_css_attr_unref(css);
318 }
320 return value;
321 }
323 guint32
324 sp_desktop_get_color_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill, bool *has_color)
325 {
326 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
327 SPCSSAttr *css = NULL;
328 guint32 r = 0; // if there's no color, return black
329 if (has_color)
330 *has_color = false;
331 if (prefs->getBool(tool + "/usecurrent")) {
332 css = sp_desktop_get_style(desktop, true);
333 } else {
334 css = prefs->getStyle(tool + "/style");
335 }
337 if (css) {
338 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
340 if (desktop->current && property) { // if there is style and the property in it,
341 if (strncmp(property, "url", 3) && strncmp(property, "none", 4)) { // and if it's not url or none,
342 // read it
343 r = sp_svg_read_color(property, r);
344 if (has_color)
345 *has_color = true;
346 }
347 }
349 sp_repr_css_attr_unref(css);
350 }
352 return r | 0xff;
353 }
355 /**
356 * Apply the desktop's current style or the tool style to repr.
357 */
358 void
359 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, Glib::ustring const &tool_path, bool with_text)
360 {
361 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
362 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
364 if (prefs->getBool(tool_path + "/usecurrent") && css_current) {
365 sp_repr_css_set(repr, css_current, "style");
366 } else {
367 SPCSSAttr *css = prefs->getInheritedStyle(tool_path + "/style");
368 sp_repr_css_set(repr, css, "style");
369 sp_repr_css_attr_unref(css);
370 }
371 if (css_current) {
372 sp_repr_css_attr_unref(css_current);
373 }
374 }
376 /**
377 * Returns the font size (in SVG pixels) of the text tool style (if text
378 * tool uses its own style) or desktop style (otherwise).
379 */
380 double
381 sp_desktop_get_font_size_tool(SPDesktop *desktop)
382 {
383 (void)desktop; // TODO cleanup
384 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
385 Glib::ustring desktop_style = prefs->getString("/desktop/style");
386 Glib::ustring style_str;
387 if ((prefs->getBool("/tools/text/usecurrent")) && !desktop_style.empty()) {
388 style_str = desktop_style;
389 } else {
390 style_str = prefs->getString("/tools/text/style");
391 }
393 double ret = 12;
394 if (!style_str.empty()) {
395 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
396 sp_style_merge_from_style_string(style, style_str.data());
397 ret = style->font_size.computed;
398 sp_style_unref(style);
399 }
400 return ret;
401 }
403 /** Determine average stroke width, simple method */
404 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
405 gdouble
406 stroke_average_width (GSList const *objects)
407 {
408 if (g_slist_length ((GSList *) objects) == 0)
409 return NR_HUGE;
411 gdouble avgwidth = 0.0;
412 bool notstroked = true;
413 int n_notstroked = 0;
415 for (GSList const *l = objects; l != NULL; l = l->next) {
416 if (!SP_IS_ITEM (l->data))
417 continue;
419 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
421 SPObject *object = SP_OBJECT(l->data);
423 if ( object->style->stroke.isNone() ) {
424 ++n_notstroked; // do not count nonstroked objects
425 continue;
426 } else {
427 notstroked = false;
428 }
430 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.descrim();
431 }
433 if (notstroked)
434 return NR_HUGE;
436 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
437 }
439 static bool vectorsClose( std::vector<double> const &lhs, std::vector<double> const &rhs )
440 {
441 static double epsilon = 1e-6;
442 bool isClose = false;
443 if ( lhs.size() == rhs.size() ) {
444 isClose = true;
445 for ( size_t i = 0; (i < lhs.size()) && isClose; ++i ) {
446 isClose = fabs(lhs[i] - rhs[i]) < epsilon;
447 }
448 }
449 return isClose;
450 }
453 /**
454 * Write to style_res the average fill or stroke of list of objects, if applicable.
455 */
456 int
457 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
458 {
459 if (g_slist_length(objects) == 0) {
460 /* No objects, set empty */
461 return QUERY_STYLE_NOTHING;
462 }
464 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
465 bool paintImpossible = true;
466 paint_res->set = TRUE;
468 SVGICCColor* iccColor = 0;
469 SVGDeviceColor* devColor = 0;
471 bool iccSeen = false;
472 gfloat c[4];
473 c[0] = c[1] = c[2] = c[3] = 0.0;
474 gint num = 0;
476 gfloat prev[3];
477 prev[0] = prev[1] = prev[2] = 0.0;
478 bool same_color = true;
480 for (GSList const *i = objects; i != NULL; i = i->next) {
481 SPObject *obj = SP_OBJECT (i->data);
482 SPStyle *style = SP_OBJECT_STYLE (obj);
483 if (!style) continue;
485 SPIPaint *paint = isfill? &style->fill : &style->stroke;
487 // We consider paint "effectively set" for anything within text hierarchy
488 SPObject *parent = SP_OBJECT_PARENT (obj);
489 bool paint_effectively_set =
490 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
491 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
492 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
494 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
496 if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
497 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
498 }
500 if (paint_res->set && paint->set && paint_res->isPaintserver()) {
501 // both previous paint and this paint were a server, see if the servers are compatible
503 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
504 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
506 if (SP_IS_LINEARGRADIENT (server_res)) {
508 if (!SP_IS_LINEARGRADIENT(server))
509 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
511 SPGradient *vector = SP_GRADIENT(server)->getVector();
512 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
513 if (vector_res != vector)
514 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
516 } else if (SP_IS_RADIALGRADIENT (server_res)) {
518 if (!SP_IS_RADIALGRADIENT(server))
519 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
521 SPGradient *vector = SP_GRADIENT(server)->getVector();
522 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
523 if (vector_res != vector)
524 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
526 } else if (SP_IS_PATTERN (server_res)) {
528 if (!SP_IS_PATTERN(server))
529 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
531 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
532 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
533 if (pat_res != pat)
534 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
535 }
536 }
538 // 2. Sum color, copy server from paint to paint_res
540 if (paint_res->set && paint_effectively_set && paint->isColor()) {
541 gfloat d[3];
542 sp_color_get_rgb_floatv (&paint->value.color, d);
544 // Check if this color is the same as previous
545 if (paintImpossible) {
546 prev[0] = d[0];
547 prev[1] = d[1];
548 prev[2] = d[2];
549 paint_res->setColor(d[0], d[1], d[2]);
550 iccColor = paint->value.color.icc;
551 iccSeen = true;
552 } else {
553 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2])) {
554 same_color = false;
555 iccColor = 0;
556 }
557 if ( iccSeen && iccColor ) {
558 if ( !paint->value.color.icc
559 || (iccColor->colorProfile != paint->value.color.icc->colorProfile)
560 || !vectorsClose(iccColor->colors, paint->value.color.icc->colors) ) {
561 same_color = false;
562 iccColor = 0;
563 }
564 }
565 }
567 // average color
568 c[0] += d[0];
569 c[1] += d[1];
570 c[2] += d[2];
571 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
573 // average device color
574 unsigned int it;
575 if (i==objects /*if this is the first object in the GList*/
576 && paint->value.color.device){
577 devColor = new SVGDeviceColor(*paint->value.color.device);
578 for (it=0; it < paint->value.color.device->colors.size(); it++){
579 devColor->colors[it] = 0;
580 }
581 }
583 if (devColor && paint->value.color.device && paint->value.color.device->type == devColor->type){
584 for (it=0; it < paint->value.color.device->colors.size(); it++){
585 devColor->colors[it] += paint->value.color.device->colors[it];
586 }
587 }
589 num ++;
590 }
592 paintImpossible = false;
593 paint_res->colorSet = paint->colorSet;
594 paint_res->currentcolor = paint->currentcolor;
595 if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
596 if (isfill) {
597 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
598 } else {
599 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
600 }
601 }
602 paint_res->set = paint_effectively_set;
603 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
604 }
606 // After all objects processed, divide the color if necessary and return
607 if (paint_res->set && paint_res->isColor()) { // set the color
608 g_assert (num >= 1);
610 c[0] /= num;
611 c[1] /= num;
612 c[2] /= num;
613 c[3] /= num;
614 paint_res->setColor(c[0], c[1], c[2]);
615 if (isfill) {
616 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
617 } else {
618 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
619 }
622 if ( iccSeen && iccColor ) {
623 // TODO check for existing
624 SVGICCColor* tmp = new SVGICCColor(*iccColor);
625 paint_res->value.color.icc = tmp;
626 }
628 // divide and store the device-color
629 if (devColor){
630 for (unsigned int it=0; it < devColor->colors.size(); it++){
631 devColor->colors[it] /= num;
632 }
633 paint_res->value.color.device = devColor;
634 }
636 if (num > 1) {
637 if (same_color)
638 return QUERY_STYLE_MULTIPLE_SAME;
639 else
640 return QUERY_STYLE_MULTIPLE_AVERAGED;
641 } else {
642 return QUERY_STYLE_SINGLE;
643 }
644 }
646 // Not color
647 if (g_slist_length(objects) > 1) {
648 return QUERY_STYLE_MULTIPLE_SAME;
649 } else {
650 return QUERY_STYLE_SINGLE;
651 }
652 }
654 /**
655 * Write to style_res the average opacity of a list of objects.
656 */
657 int
658 objects_query_opacity (GSList *objects, SPStyle *style_res)
659 {
660 if (g_slist_length(objects) == 0) {
661 /* No objects, set empty */
662 return QUERY_STYLE_NOTHING;
663 }
665 gdouble opacity_sum = 0;
666 gdouble opacity_prev = -1;
667 bool same_opacity = true;
668 guint opacity_items = 0;
671 for (GSList const *i = objects; i != NULL; i = i->next) {
672 SPObject *obj = SP_OBJECT (i->data);
673 SPStyle *style = SP_OBJECT_STYLE (obj);
674 if (!style) continue;
676 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
677 opacity_sum += opacity;
678 if (opacity_prev != -1 && opacity != opacity_prev)
679 same_opacity = false;
680 opacity_prev = opacity;
681 opacity_items ++;
682 }
683 if (opacity_items > 1)
684 opacity_sum /= opacity_items;
686 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
688 if (opacity_items == 0) {
689 return QUERY_STYLE_NOTHING;
690 } else if (opacity_items == 1) {
691 return QUERY_STYLE_SINGLE;
692 } else {
693 if (same_opacity)
694 return QUERY_STYLE_MULTIPLE_SAME;
695 else
696 return QUERY_STYLE_MULTIPLE_AVERAGED;
697 }
698 }
700 /**
701 * Write to style_res the average stroke width of a list of objects.
702 */
703 int
704 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
705 {
706 if (g_slist_length(objects) == 0) {
707 /* No objects, set empty */
708 return QUERY_STYLE_NOTHING;
709 }
711 gdouble avgwidth = 0.0;
713 gdouble prev_sw = -1;
714 bool same_sw = true;
715 bool noneSet = true; // is stroke set to none?
717 int n_stroked = 0;
719 for (GSList const *i = objects; i != NULL; i = i->next) {
720 SPObject *obj = SP_OBJECT (i->data);
721 if (!SP_IS_ITEM(obj)) continue;
722 SPStyle *style = SP_OBJECT_STYLE (obj);
723 if (!style) continue;
725 if ( style->stroke.isNone() && !(
726 style->marker[SP_MARKER_LOC].set || // stroke width affects markers, so if there's no stroke but only markers then we should
727 style->marker[SP_MARKER_LOC_START].set || // still calculate the stroke width
728 style->marker[SP_MARKER_LOC_MID].set ||
729 style->marker[SP_MARKER_LOC_END].set))
730 {
731 continue;
732 }
734 n_stroked ++;
736 noneSet &= style->stroke.isNone();
738 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
739 double sw = style->stroke_width.computed * i2d.descrim();
741 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
742 same_sw = false;
743 prev_sw = sw;
745 avgwidth += sw;
746 }
748 if (n_stroked > 1)
749 avgwidth /= (n_stroked);
751 style_res->stroke_width.computed = avgwidth;
752 style_res->stroke_width.set = true;
753 style_res->stroke.noneSet = noneSet; // Will only be true if none of the selected objects has it's stroke set.
755 if (n_stroked == 0) {
756 return QUERY_STYLE_NOTHING;
757 } else if (n_stroked == 1) {
758 return QUERY_STYLE_SINGLE;
759 } else {
760 if (same_sw)
761 return QUERY_STYLE_MULTIPLE_SAME;
762 else
763 return QUERY_STYLE_MULTIPLE_AVERAGED;
764 }
765 }
767 /**
768 * Write to style_res the average miter limit of a list of objects.
769 */
770 int
771 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
772 {
773 if (g_slist_length(objects) == 0) {
774 /* No objects, set empty */
775 return QUERY_STYLE_NOTHING;
776 }
778 gdouble avgml = 0.0;
779 int n_stroked = 0;
781 gdouble prev_ml = -1;
782 bool same_ml = true;
784 for (GSList const *i = objects; i != NULL; i = i->next) {
785 SPObject *obj = SP_OBJECT (i->data);
786 if (!SP_IS_ITEM(obj)) continue;
787 SPStyle *style = SP_OBJECT_STYLE (obj);
788 if (!style) continue;
790 if ( style->stroke.isNone() ) {
791 continue;
792 }
794 n_stroked ++;
796 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
797 same_ml = false;
798 prev_ml = style->stroke_miterlimit.value;
800 avgml += style->stroke_miterlimit.value;
801 }
803 if (n_stroked > 1)
804 avgml /= (n_stroked);
806 style_res->stroke_miterlimit.value = avgml;
807 style_res->stroke_miterlimit.set = true;
809 if (n_stroked == 0) {
810 return QUERY_STYLE_NOTHING;
811 } else if (n_stroked == 1) {
812 return QUERY_STYLE_SINGLE;
813 } else {
814 if (same_ml)
815 return QUERY_STYLE_MULTIPLE_SAME;
816 else
817 return QUERY_STYLE_MULTIPLE_AVERAGED;
818 }
819 }
821 /**
822 * Write to style_res the stroke cap of a list of objects.
823 */
824 int
825 objects_query_strokecap (GSList *objects, SPStyle *style_res)
826 {
827 if (g_slist_length(objects) == 0) {
828 /* No objects, set empty */
829 return QUERY_STYLE_NOTHING;
830 }
832 int cap = -1;
833 gdouble prev_cap = -1;
834 bool same_cap = true;
835 int n_stroked = 0;
837 for (GSList const *i = objects; i != NULL; i = i->next) {
838 SPObject *obj = SP_OBJECT (i->data);
839 if (!SP_IS_ITEM(obj)) continue;
840 SPStyle *style = SP_OBJECT_STYLE (obj);
841 if (!style) continue;
843 if ( style->stroke.isNone() ) {
844 continue;
845 }
847 n_stroked ++;
849 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
850 same_cap = false;
851 prev_cap = style->stroke_linecap.value;
853 cap = style->stroke_linecap.value;
854 }
856 style_res->stroke_linecap.value = cap;
857 style_res->stroke_linecap.set = true;
859 if (n_stroked == 0) {
860 return QUERY_STYLE_NOTHING;
861 } else if (n_stroked == 1) {
862 return QUERY_STYLE_SINGLE;
863 } else {
864 if (same_cap)
865 return QUERY_STYLE_MULTIPLE_SAME;
866 else
867 return QUERY_STYLE_MULTIPLE_DIFFERENT;
868 }
869 }
871 /**
872 * Write to style_res the stroke join of a list of objects.
873 */
874 int
875 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
876 {
877 if (g_slist_length(objects) == 0) {
878 /* No objects, set empty */
879 return QUERY_STYLE_NOTHING;
880 }
882 int join = -1;
883 gdouble prev_join = -1;
884 bool same_join = true;
885 int n_stroked = 0;
887 for (GSList const *i = objects; i != NULL; i = i->next) {
888 SPObject *obj = SP_OBJECT (i->data);
889 if (!SP_IS_ITEM(obj)) continue;
890 SPStyle *style = SP_OBJECT_STYLE (obj);
891 if (!style) continue;
893 if ( style->stroke.isNone() ) {
894 continue;
895 }
897 n_stroked ++;
899 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
900 same_join = false;
901 prev_join = style->stroke_linejoin.value;
903 join = style->stroke_linejoin.value;
904 }
906 style_res->stroke_linejoin.value = join;
907 style_res->stroke_linejoin.set = true;
909 if (n_stroked == 0) {
910 return QUERY_STYLE_NOTHING;
911 } else if (n_stroked == 1) {
912 return QUERY_STYLE_SINGLE;
913 } else {
914 if (same_join)
915 return QUERY_STYLE_MULTIPLE_SAME;
916 else
917 return QUERY_STYLE_MULTIPLE_DIFFERENT;
918 }
919 }
921 /**
922 * Write to style_res the average font size and spacing of objects.
923 */
924 int
925 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
926 {
927 bool different = false;
929 double size = 0;
930 double letterspacing = 0;
931 double wordspacing = 0;
932 double linespacing = 0;
933 bool letterspacing_normal = false;
934 bool wordspacing_normal = false;
935 bool linespacing_normal = false;
937 double size_prev = 0;
938 double letterspacing_prev = 0;
939 double wordspacing_prev = 0;
940 double linespacing_prev = 0;
942 int texts = 0;
944 for (GSList const *i = objects; i != NULL; i = i->next) {
945 SPObject *obj = SP_OBJECT (i->data);
947 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
948 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
949 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
950 continue;
952 SPStyle *style = SP_OBJECT_STYLE (obj);
953 if (!style) continue;
955 texts ++;
956 size += style->font_size.computed * Geom::Matrix(sp_item_i2d_affine(SP_ITEM(obj))).descrim(); /// \todo FIXME: we assume non-% units here
958 if (style->letter_spacing.normal) {
959 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
960 letterspacing_normal = true;
961 } else {
962 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
963 letterspacing_normal = false;
964 }
966 if (style->word_spacing.normal) {
967 if (!different && (wordspacing_prev == 0 || wordspacing_prev == wordspacing))
968 wordspacing_normal = true;
969 } else {
970 wordspacing += style->word_spacing.computed; /// \todo FIXME: we assume non-% units here
971 wordspacing_normal = false;
972 }
974 double linespacing_current;
975 if (style->line_height.normal) {
976 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
977 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
978 linespacing_normal = true;
979 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
980 linespacing_current = style->line_height.value;
981 linespacing_normal = false;
982 } else { // we need % here
983 linespacing_current = style->line_height.computed / style->font_size.computed;
984 linespacing_normal = false;
985 }
986 linespacing += linespacing_current;
988 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
989 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
990 (wordspacing_prev != 0 && style->word_spacing.computed != wordspacing_prev) ||
991 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
992 different = true;
993 }
995 size_prev = style->font_size.computed;
996 letterspacing_prev = style->letter_spacing.computed;
997 wordspacing_prev = style->word_spacing.computed;
998 linespacing_prev = linespacing_current;
1000 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
1001 style_res->text_anchor.computed = style->text_anchor.computed;
1002 style_res->writing_mode.computed = style->writing_mode.computed;
1003 }
1005 if (texts == 0)
1006 return QUERY_STYLE_NOTHING;
1008 if (texts > 1) {
1009 size /= texts;
1010 letterspacing /= texts;
1011 wordspacing /= texts;
1012 linespacing /= texts;
1013 }
1015 style_res->font_size.computed = size;
1016 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
1018 style_res->letter_spacing.normal = letterspacing_normal;
1019 style_res->letter_spacing.computed = letterspacing;
1021 style_res->word_spacing.normal = wordspacing_normal;
1022 style_res->word_spacing.computed = wordspacing;
1024 style_res->line_height.normal = linespacing_normal;
1025 style_res->line_height.computed = linespacing;
1026 style_res->line_height.value = linespacing;
1027 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
1029 if (texts > 1) {
1030 if (different) {
1031 return QUERY_STYLE_MULTIPLE_AVERAGED;
1032 } else {
1033 return QUERY_STYLE_MULTIPLE_SAME;
1034 }
1035 } else {
1036 return QUERY_STYLE_SINGLE;
1037 }
1038 }
1040 /**
1041 * Write to style_res the average font style of objects.
1042 */
1043 int
1044 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
1045 {
1046 bool different = false;
1047 bool set = false;
1049 int texts = 0;
1051 for (GSList const *i = objects; i != NULL; i = i->next) {
1052 SPObject *obj = SP_OBJECT (i->data);
1054 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1055 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1056 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1057 continue;
1059 SPStyle *style = SP_OBJECT_STYLE (obj);
1060 if (!style) continue;
1062 texts ++;
1064 if (set &&
1065 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
1066 different = true; // different styles
1067 }
1069 set = TRUE;
1070 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
1071 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
1072 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
1073 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
1074 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
1075 }
1077 if (texts == 0 || !set)
1078 return QUERY_STYLE_NOTHING;
1080 if (texts > 1) {
1081 if (different) {
1082 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1083 } else {
1084 return QUERY_STYLE_MULTIPLE_SAME;
1085 }
1086 } else {
1087 return QUERY_STYLE_SINGLE;
1088 }
1089 }
1091 /**
1092 * Write to style_res the baseline numbers.
1093 */
1094 int
1095 objects_query_baselines (GSList *objects, SPStyle *style_res)
1096 {
1097 bool different = false;
1099 // Only baseline-shift at the moment
1100 // We will return:
1101 // If baseline-shift is same for all objects:
1102 // The full baseline-shift data (used for subscripts and superscripts)
1103 // If baseline-shift is different:
1104 // The average baseline-shift (not implemented at the moment as this is complicated June 2010)
1105 SPIBaselineShift old;
1106 old.value = 0.0;
1107 old.computed = 0.0;
1109 // double baselineshift = 0.0;
1110 bool set = false;
1112 int texts = 0;
1114 for (GSList const *i = objects; i != NULL; i = i->next) {
1115 SPObject *obj = SP_OBJECT (i->data);
1117 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1118 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1119 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1120 continue;
1122 SPStyle *style = SP_OBJECT_STYLE (obj);
1123 if (!style) continue;
1125 texts ++;
1127 SPIBaselineShift current;
1128 if(style->baseline_shift.set) {
1130 current.set = style->baseline_shift.set;
1131 current.inherit = style->baseline_shift.inherit;
1132 current.type = style->baseline_shift.type;
1133 current.literal = style->baseline_shift.literal;
1134 current.value = style->baseline_shift.value;
1135 current.computed = style->baseline_shift.computed;
1137 if( set ) {
1138 if( current.set != old.set ||
1139 current.inherit != old.inherit ||
1140 current.type != old.type ||
1141 current.literal != old.literal ||
1142 current.value != old.value ||
1143 current.computed != old.computed ) {
1144 // Maybe this needs to be better thought out.
1145 different = true;
1146 }
1147 }
1149 set = true;
1151 old.set = current.set;
1152 old.inherit = current.inherit;
1153 old.type = current.type;
1154 old.literal = current.literal;
1155 old.value = current.value;
1156 old.computed = current.computed;
1157 }
1158 }
1160 if (different || !set ) {
1161 style_res->baseline_shift.set = false;
1162 style_res->baseline_shift.computed = 0.0;
1163 } else {
1164 style_res->baseline_shift.set = old.set;
1165 style_res->baseline_shift.inherit = old.inherit;
1166 style_res->baseline_shift.type = old.type;
1167 style_res->baseline_shift.literal = old.literal;
1168 style_res->baseline_shift.value = old.value;
1169 style_res->baseline_shift.computed = old.computed;
1170 }
1172 if (texts == 0 || !set)
1173 return QUERY_STYLE_NOTHING;
1175 if (texts > 1) {
1176 if (different) {
1177 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1178 } else {
1179 return QUERY_STYLE_MULTIPLE_SAME;
1180 }
1181 } else {
1182 return QUERY_STYLE_SINGLE;
1183 }
1184 }
1186 /**
1187 * Write to style_res the average font family of objects.
1188 */
1189 int
1190 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1191 {
1192 bool different = false;
1193 int texts = 0;
1195 if (style_res->text->font_family.value) {
1196 g_free(style_res->text->font_family.value);
1197 style_res->text->font_family.value = NULL;
1198 }
1199 style_res->text->font_family.set = FALSE;
1201 for (GSList const *i = objects; i != NULL; i = i->next) {
1202 SPObject *obj = SP_OBJECT (i->data);
1204 // std::cout << " " << SP_OBJECT_ID (i->data) << std::endl;
1205 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1206 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1207 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1208 continue;
1210 SPStyle *style = SP_OBJECT_STYLE (obj);
1211 if (!style) continue;
1213 texts ++;
1215 if (style_res->text->font_family.value && style->text->font_family.value &&
1216 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1217 different = true; // different fonts
1218 }
1220 if (style_res->text->font_family.value) {
1221 g_free(style_res->text->font_family.value);
1222 style_res->text->font_family.value = NULL;
1223 }
1225 style_res->text->font_family.set = TRUE;
1226 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1227 }
1229 if (texts == 0 || !style_res->text->font_family.set)
1230 return QUERY_STYLE_NOTHING;
1232 if (texts > 1) {
1233 if (different) {
1234 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1235 } else {
1236 return QUERY_STYLE_MULTIPLE_SAME;
1237 }
1238 } else {
1239 return QUERY_STYLE_SINGLE;
1240 }
1241 }
1243 int
1244 objects_query_fontspecification (GSList *objects, SPStyle *style_res)
1245 {
1246 bool different = false;
1247 int texts = 0;
1249 if (style_res->text->font_specification.value) {
1250 g_free(style_res->text->font_specification.value);
1251 style_res->text->font_specification.value = NULL;
1252 }
1253 style_res->text->font_specification.set = FALSE;
1255 for (GSList const *i = objects; i != NULL; i = i->next) {
1256 SPObject *obj = SP_OBJECT (i->data);
1258 // std::cout << " " << SP_OBJECT_ID (i->data) << std::endl;
1259 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1260 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1261 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1262 continue;
1264 SPStyle *style = SP_OBJECT_STYLE (obj);
1265 if (!style) continue;
1267 texts ++;
1269 if (style_res->text->font_specification.value && style_res->text->font_specification.set &&
1270 style->text->font_specification.value && style->text->font_specification.set &&
1271 strcmp (style_res->text->font_specification.value, style->text->font_specification.value)) {
1272 different = true; // different fonts
1273 }
1275 if (style->text->font_specification.set) {
1277 if (style_res->text->font_specification.value) {
1278 g_free(style_res->text->font_specification.value);
1279 style_res->text->font_specification.value = NULL;
1280 }
1282 style_res->text->font_specification.set = TRUE;
1283 style_res->text->font_specification.value = g_strdup(style->text->font_specification.value);
1284 }
1285 }
1287 if (texts == 0)
1288 return QUERY_STYLE_NOTHING;
1290 if (texts > 1) {
1291 if (different) {
1292 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1293 } else {
1294 return QUERY_STYLE_MULTIPLE_SAME;
1295 }
1296 } else {
1297 return QUERY_STYLE_SINGLE;
1298 }
1299 }
1301 int
1302 objects_query_blend (GSList *objects, SPStyle *style_res)
1303 {
1304 const int empty_prev = -2;
1305 const int complex_filter = 5;
1306 int blend = 0;
1307 float blend_prev = empty_prev;
1308 bool same_blend = true;
1309 guint items = 0;
1311 for (GSList const *i = objects; i != NULL; i = i->next) {
1312 SPObject *obj = SP_OBJECT (i->data);
1313 SPStyle *style = SP_OBJECT_STYLE (obj);
1314 if(!style || !SP_IS_ITEM(obj)) continue;
1316 items++;
1318 //if object has a filter
1319 if (style->filter.set && style->getFilter()) {
1320 int blurcount = 0;
1321 int blendcount = 0;
1323 // determine whether filter is simple (blend and/or blur) or complex
1324 for(SPObject *primitive_obj = style->getFilter()->children;
1325 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1326 primitive_obj = primitive_obj->next) {
1327 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1328 if(SP_IS_FEBLEND(primitive))
1329 ++blendcount;
1330 else if(SP_IS_GAUSSIANBLUR(primitive))
1331 ++blurcount;
1332 else {
1333 blurcount = complex_filter;
1334 break;
1335 }
1336 }
1338 // simple filter
1339 if(blurcount == 1 || blendcount == 1) {
1340 for(SPObject *primitive_obj = style->getFilter()->children;
1341 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1342 primitive_obj = primitive_obj->next) {
1343 if(SP_IS_FEBLEND(primitive_obj)) {
1344 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1345 blend = spblend->blend_mode;
1346 }
1347 }
1348 }
1349 else {
1350 blend = complex_filter;
1351 }
1352 }
1353 // defaults to blend mode = "normal"
1354 else {
1355 blend = 0;
1356 }
1358 if(blend_prev != empty_prev && blend_prev != blend)
1359 same_blend = false;
1360 blend_prev = blend;
1361 }
1363 if (items > 0) {
1364 style_res->filter_blend_mode.value = blend;
1365 }
1367 if (items == 0) {
1368 return QUERY_STYLE_NOTHING;
1369 } else if (items == 1) {
1370 return QUERY_STYLE_SINGLE;
1371 } else {
1372 if(same_blend)
1373 return QUERY_STYLE_MULTIPLE_SAME;
1374 else
1375 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1376 }
1377 }
1379 /**
1380 * Write to style_res the average blurring of a list of objects.
1381 */
1382 int
1383 objects_query_blur (GSList *objects, SPStyle *style_res)
1384 {
1385 if (g_slist_length(objects) == 0) {
1386 /* No objects, set empty */
1387 return QUERY_STYLE_NOTHING;
1388 }
1390 float blur_sum = 0;
1391 float blur_prev = -1;
1392 bool same_blur = true;
1393 guint blur_items = 0;
1394 guint items = 0;
1396 for (GSList const *i = objects; i != NULL; i = i->next) {
1397 SPObject *obj = SP_OBJECT (i->data);
1398 SPStyle *style = SP_OBJECT_STYLE (obj);
1399 if (!style) continue;
1400 if (!SP_IS_ITEM(obj)) continue;
1402 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1404 items ++;
1406 //if object has a filter
1407 if (style->filter.set && style->getFilter()) {
1408 //cycle through filter primitives
1409 SPObject *primitive_obj = style->getFilter()->children;
1410 while (primitive_obj) {
1411 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1412 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1414 //if primitive is gaussianblur
1415 if(SP_IS_GAUSSIANBLUR(primitive)) {
1416 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1417 float num = spblur->stdDeviation.getNumber();
1418 blur_sum += num * i2d.descrim();
1419 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
1420 same_blur = false;
1421 blur_prev = num;
1422 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1423 blur_items ++;
1424 }
1425 }
1426 primitive_obj = primitive_obj->next;
1427 }
1428 }
1429 }
1431 if (items > 0) {
1432 if (blur_items > 0)
1433 blur_sum /= blur_items;
1434 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1435 }
1437 if (items == 0) {
1438 return QUERY_STYLE_NOTHING;
1439 } else if (items == 1) {
1440 return QUERY_STYLE_SINGLE;
1441 } else {
1442 if (same_blur)
1443 return QUERY_STYLE_MULTIPLE_SAME;
1444 else
1445 return QUERY_STYLE_MULTIPLE_AVERAGED;
1446 }
1447 }
1449 /**
1450 * Query the given list of objects for the given property, write
1451 * the result to style, return appropriate flag.
1452 */
1453 int
1454 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1455 {
1456 if (property == QUERY_STYLE_PROPERTY_FILL) {
1457 return objects_query_fillstroke (list, style, true);
1458 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1459 return objects_query_fillstroke (list, style, false);
1461 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1462 return objects_query_strokewidth (list, style);
1463 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1464 return objects_query_miterlimit (list, style);
1465 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1466 return objects_query_strokecap (list, style);
1467 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1468 return objects_query_strokejoin (list, style);
1470 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1471 return objects_query_opacity (list, style);
1473 } else if (property == QUERY_STYLE_PROPERTY_FONT_SPECIFICATION) {
1474 return objects_query_fontspecification (list, style);
1475 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1476 return objects_query_fontfamily (list, style);
1477 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1478 return objects_query_fontstyle (list, style);
1479 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1480 return objects_query_fontnumbers (list, style);
1481 } else if (property == QUERY_STYLE_PROPERTY_BASELINES) {
1482 return objects_query_baselines (list, style);
1484 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1485 return objects_query_blend (list, style);
1486 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1487 return objects_query_blur (list, style);
1488 }
1489 return QUERY_STYLE_NOTHING;
1490 }
1493 /**
1494 * Query the subselection (if any) or selection on the given desktop for the given property, write
1495 * the result to style, return appropriate flag.
1496 */
1497 int
1498 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1499 {
1500 int ret = desktop->_query_style_signal.emit(style, property);
1502 if (ret != QUERY_STYLE_NOTHING)
1503 return ret; // subselection returned a style, pass it on
1505 // otherwise, do querying and averaging over selection
1506 if (desktop->selection != NULL) {
1507 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1508 }
1510 return QUERY_STYLE_NOTHING;
1511 }
1513 /**
1514 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if at
1515 * least one of the properties did not return QUERY_STYLE_NOTHING.
1516 */
1517 bool
1518 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1519 {
1520 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1521 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1522 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1523 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1524 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1525 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1526 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1527 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1528 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1529 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1530 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1532 return (result_family != QUERY_STYLE_NOTHING ||
1533 result_fstyle != QUERY_STYLE_NOTHING ||
1534 result_fnumbers != QUERY_STYLE_NOTHING ||
1535 result_fill != QUERY_STYLE_NOTHING ||
1536 result_stroke != QUERY_STYLE_NOTHING ||
1537 result_opacity != QUERY_STYLE_NOTHING ||
1538 result_strokewidth != QUERY_STYLE_NOTHING ||
1539 result_strokemiterlimit != QUERY_STYLE_NOTHING ||
1540 result_strokecap != QUERY_STYLE_NOTHING ||
1541 result_strokejoin != QUERY_STYLE_NOTHING ||
1542 result_blur != QUERY_STYLE_NOTHING);
1543 }
1546 /*
1547 Local Variables:
1548 mode:c++
1549 c-file-style:"stroustrup"
1550 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1551 indent-tabs-mode:nil
1552 fill-column:99
1553 End:
1554 */
1555 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :