Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / desktop-style.cpp
1 /** \file
2  * Desktop style management
3  *
4  * Authors:
5  *   bulia byak
6  *   verbalshadow
7  *   Jon A. Cruz <jon@joncruz.org> 
8  *   Abhishek Sharma
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"
44 #include "event-context.h"
46 #include "desktop-style.h"
47 #include "svg/svg-icc-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           &&  !o->getAttribute("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(o)->i2doc_affine());
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         o->changeCSS(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 = o->firstChild() ; child ; child = child->getNext() ) {
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     }
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)
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) {
199         // If we have an event context, update its cursor (TODO: it could be neater to do this with the signal sent above, but what if the signal gets intercepted?)
200         if (desktop->event_context) {
201             sp_event_context_update_cursor(desktop->event_context);
202         }
204         // Remove text attributes if not text...
205         // Do this once in case a zillion objects are selected.
206         SPCSSAttr *css_no_text = sp_repr_css_attr_new();
207         sp_repr_css_merge(css_no_text, css);
208         css_no_text = sp_css_attr_unset_text(css_no_text);
210         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
212             // If not text, don't apply text attributes (can a group have text attributes?)
213             if ( SP_IS_TEXT(i->data) || SP_IS_FLOWTEXT(i->data)
214                 || SP_IS_TSPAN(i->data) || SP_IS_TREF(i->data) || SP_IS_TEXTPATH(i->data)
215                 || SP_IS_FLOWDIV(i->data) || SP_IS_FLOWPARA(i->data) || SP_IS_FLOWTSPAN(i->data)) {
217                 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
219             } else {
221                 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css_no_text, true);
223             }
224         }
225         sp_repr_css_attr_unref(css_no_text);
226     }
229 /**
230  * Return the desktop's current style.
231  */
232 SPCSSAttr *
233 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
235     SPCSSAttr *css = sp_repr_css_attr_new();
236     sp_repr_css_merge(css, desktop->current);
237     if (!css->attributeList()) {
238         sp_repr_css_attr_unref(css);
239         return NULL;
240     } else {
241         if (!with_text) {
242             css = sp_css_attr_unset_text(css);
243         }
244         return css;
245     }
248 /**
249  * Return the desktop's current color.
250  */
251 guint32
252 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
254     guint32 r = 0; // if there's no color, return black
255     gchar const *property = sp_repr_css_property(desktop->current,
256                                                  is_fill ? "fill" : "stroke",
257                                                  "#000");
259     if (desktop->current && property) { // if there is style and the property in it,
260         if (strncmp(property, "url", 3)) { // and if it's not url,
261             // read it
262             r = sp_svg_read_color(property, r);
263         }
264     }
266     return r;
269 double
270 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool *has_opacity)
272     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
273     SPCSSAttr *css = NULL;
274     gfloat value = 1.0; // default if nothing else found
275     if (has_opacity)
276         *has_opacity = false;
277     if (prefs->getBool(tool + "/usecurrent")) {
278         css = sp_desktop_get_style(desktop, true);
279     } else {
280         css = prefs->getStyle(tool + "/style");
281     }
283     if (css) {
284         gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
286         if (desktop->current && property) { // if there is style and the property in it,
287             if ( !sp_svg_number_read_f(property, &value) ) {
288                 value = 1.0; // things failed. set back to the default
289             } else {
290                 if (has_opacity)
291                    *has_opacity = false;
292             }
293         }
295         sp_repr_css_attr_unref(css);
296     }
298     return value;
300 double
301 sp_desktop_get_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill)
303     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
304     SPCSSAttr *css = NULL;
305     gfloat value = 1.0; // default if nothing else found
306     if (prefs->getBool(tool + "/usecurrent")) {
307         css = sp_desktop_get_style(desktop, true);
308     } else {
309         css = prefs->getStyle(tool + "/style");
310     }
312     if (css) {
313         gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
315         if (desktop->current && property) { // if there is style and the property in it,
316             if ( !sp_svg_number_read_f(property, &value) ) {
317                 value = 1.0; // things failed. set back to the default
318             }
319         }
321         sp_repr_css_attr_unref(css);
322     }
324     return value;
327 guint32
328 sp_desktop_get_color_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill, bool *has_color)
330     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
331     SPCSSAttr *css = NULL;
332     guint32 r = 0; // if there's no color, return black
333     if (has_color)
334         *has_color = false;
335     if (prefs->getBool(tool + "/usecurrent")) {
336         css = sp_desktop_get_style(desktop, true);
337     } else {
338         css = prefs->getStyle(tool + "/style");
339     }
341     if (css) {
342         gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
344         if (desktop->current && property) { // if there is style and the property in it,
345             if (strncmp(property, "url", 3) && strncmp(property, "none", 4)) { // and if it's not url or none,
346                 // read it
347                 r = sp_svg_read_color(property, r);
348                 if (has_color)
349                     *has_color = true;
350             }
351         }
353         sp_repr_css_attr_unref(css);
354     }
356     return r | 0xff;
359 /**
360  * Apply the desktop's current style or the tool style to repr.
361  */
362 void
363 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, Glib::ustring const &tool_path, bool with_text)
365     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
366     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
368     if (prefs->getBool(tool_path + "/usecurrent") && css_current) {
369         sp_repr_css_set(repr, css_current, "style");
370     } else {
371         SPCSSAttr *css = prefs->getInheritedStyle(tool_path + "/style");
372         sp_repr_css_set(repr, css, "style");
373         sp_repr_css_attr_unref(css);
374     }
375     if (css_current) {
376         sp_repr_css_attr_unref(css_current);
377     }
380 /**
381  * Returns the font size (in SVG pixels) of the text tool style (if text
382  * tool uses its own style) or desktop style (otherwise).
383 */
384 double
385 sp_desktop_get_font_size_tool(SPDesktop *desktop)
387     (void)desktop; // TODO cleanup
388     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
389     Glib::ustring desktop_style = prefs->getString("/desktop/style");
390     Glib::ustring style_str;
391     if ((prefs->getBool("/tools/text/usecurrent")) && !desktop_style.empty()) {
392         style_str = desktop_style;
393     } else {
394         style_str = prefs->getString("/tools/text/style");
395     }
397     double ret = 12;
398     if (!style_str.empty()) {
399         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
400         sp_style_merge_from_style_string(style, style_str.data());
401         ret = style->font_size.computed;
402         sp_style_unref(style);
403     }
404     return ret;
407 /** Determine average stroke width, simple method */
408 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
409 gdouble
410 stroke_average_width (GSList const *objects)
412     if (g_slist_length ((GSList *) objects) == 0)
413         return NR_HUGE;
415     gdouble avgwidth = 0.0;
416     bool notstroked = true;
417     int n_notstroked = 0;
419     for (GSList const *l = objects; l != NULL; l = l->next) {
420         if (!SP_IS_ITEM (l->data))
421             continue;
423         Geom::Matrix i2d = SP_ITEM(l->data)->i2d_affine();
425         SPObject *object = SP_OBJECT(l->data);
427         if ( object->style->stroke.isNone() ) {
428             ++n_notstroked;   // do not count nonstroked objects
429             continue;
430         } else {
431             notstroked = false;
432         }
434         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.descrim();
435     }
437     if (notstroked)
438         return NR_HUGE;
440     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
443 static bool vectorsClose( std::vector<double> const &lhs, std::vector<double> const &rhs )
445     static double epsilon = 1e-6;
446     bool isClose = false;
447     if ( lhs.size() == rhs.size() ) {
448         isClose = true;
449         for ( size_t i = 0; (i < lhs.size()) && isClose; ++i ) {
450             isClose = fabs(lhs[i] - rhs[i]) < epsilon;
451         }
452     }
453     return isClose;
457 /**
458  * Write to style_res the average fill or stroke of list of objects, if applicable.
459  */
460 int
461 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
463     if (g_slist_length(objects) == 0) {
464         /* No objects, set empty */
465         return QUERY_STYLE_NOTHING;
466     }
468     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
469     bool paintImpossible = true;
470     paint_res->set = TRUE;
472     SVGICCColor* iccColor = 0;
474     bool iccSeen = false;
475     gfloat c[4];
476     c[0] = c[1] = c[2] = c[3] = 0.0;
477     gint num = 0;
479     gfloat prev[3];
480     prev[0] = prev[1] = prev[2] = 0.0;
481     bool same_color = true;
483     for (GSList const *i = objects; i != NULL; i = i->next) {
484         SPObject *obj = SP_OBJECT (i->data);
485         SPStyle *style = SP_OBJECT_STYLE (obj);
486         if (!style) continue;
488         SPIPaint *paint = isfill? &style->fill : &style->stroke;
490         // We consider paint "effectively set" for anything within text hierarchy
491         SPObject *parent = SP_OBJECT_PARENT (obj);
492         bool paint_effectively_set =
493             paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
494             || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
495             || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
497         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
499         if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
500             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
501         }
503         if (paint_res->set && paint->set && paint_res->isPaintserver()) {
504             // both previous paint and this paint were a server, see if the servers are compatible
506             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
507             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
509             if (SP_IS_LINEARGRADIENT (server_res)) {
511                 if (!SP_IS_LINEARGRADIENT(server))
512                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
514                 SPGradient *vector = SP_GRADIENT(server)->getVector();
515                 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
516                 if (vector_res != vector)
517                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
519             } else if (SP_IS_RADIALGRADIENT (server_res)) {
521                 if (!SP_IS_RADIALGRADIENT(server))
522                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
524                 SPGradient *vector = SP_GRADIENT(server)->getVector();
525                 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
526                 if (vector_res != vector)
527                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
529             } else if (SP_IS_PATTERN (server_res)) {
531                 if (!SP_IS_PATTERN(server))
532                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
534                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
535                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
536                 if (pat_res != pat)
537                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
538             }
539         }
541         // 2. Sum color, copy server from paint to paint_res
543         if (paint_res->set && paint_effectively_set && paint->isColor()) {
544             gfloat d[3];
545             sp_color_get_rgb_floatv (&paint->value.color, d);
547             // Check if this color is the same as previous
548             if (paintImpossible) {
549                 prev[0] = d[0];
550                 prev[1] = d[1];
551                 prev[2] = d[2];
552                 paint_res->setColor(d[0], d[1], d[2]);
553                 iccColor = paint->value.color.icc;
554                 iccSeen = true;
555             } else {
556                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2])) {
557                     same_color = false;
558                     iccColor = 0;
559                 }
560                 if ( iccSeen && iccColor ) {
561                     if ( !paint->value.color.icc
562                          || (iccColor->colorProfile != paint->value.color.icc->colorProfile)
563                          || !vectorsClose(iccColor->colors, paint->value.color.icc->colors) ) {
564                         same_color = false;
565                         iccColor = 0;
566                     }
567                 }
568             }
570             // average color
571             c[0] += d[0];
572             c[1] += d[1];
573             c[2] += d[2];
574             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
576             num ++;
577         }
579        paintImpossible = false;
580        paint_res->colorSet = paint->colorSet;
581        paint_res->currentcolor = paint->currentcolor;
582        if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
583            if (isfill) {
584                sp_style_set_to_uri_string (style_res, true, style->getFillURI());
585            } else {
586                sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
587            }
588        }
589        paint_res->set = paint_effectively_set;
590        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
591     }
593     // After all objects processed, divide the color if necessary and return
594     if (paint_res->set && paint_res->isColor()) { // set the color
595         g_assert (num >= 1);
597         c[0] /= num;
598         c[1] /= num;
599         c[2] /= num;
600         c[3] /= num;
601         paint_res->setColor(c[0], c[1], c[2]);
602         if (isfill) {
603             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
604         } else {
605             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
606         }
609         if ( iccSeen && iccColor ) {
610             // TODO check for existing
611             SVGICCColor* tmp = new SVGICCColor(*iccColor);
612             paint_res->value.color.icc = tmp;
613         }
615         if (num > 1) {
616             if (same_color)
617                 return QUERY_STYLE_MULTIPLE_SAME;
618             else
619                 return QUERY_STYLE_MULTIPLE_AVERAGED;
620         } else {
621             return QUERY_STYLE_SINGLE;
622         }
623     }
625     // Not color
626     if (g_slist_length(objects) > 1) {
627         return QUERY_STYLE_MULTIPLE_SAME;
628     } else {
629         return QUERY_STYLE_SINGLE;
630     }
633 /**
634  * Write to style_res the average opacity of a list of objects.
635  */
636 int
637 objects_query_opacity (GSList *objects, SPStyle *style_res)
639     if (g_slist_length(objects) == 0) {
640         /* No objects, set empty */
641         return QUERY_STYLE_NOTHING;
642     }
644     gdouble opacity_sum = 0;
645     gdouble opacity_prev = -1;
646     bool same_opacity = true;
647     guint opacity_items = 0;
650     for (GSList const *i = objects; i != NULL; i = i->next) {
651         SPObject *obj = SP_OBJECT (i->data);
652         SPStyle *style = SP_OBJECT_STYLE (obj);
653         if (!style) continue;
655         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
656         opacity_sum += opacity;
657         if (opacity_prev != -1 && opacity != opacity_prev)
658             same_opacity = false;
659         opacity_prev = opacity;
660         opacity_items ++;
661     }
662     if (opacity_items > 1)
663         opacity_sum /= opacity_items;
665     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
667     if (opacity_items == 0) {
668         return QUERY_STYLE_NOTHING;
669     } else if (opacity_items == 1) {
670         return QUERY_STYLE_SINGLE;
671     } else {
672         if (same_opacity)
673             return QUERY_STYLE_MULTIPLE_SAME;
674         else
675             return QUERY_STYLE_MULTIPLE_AVERAGED;
676     }
679 /**
680  * Write to style_res the average stroke width of a list of objects.
681  */
682 int
683 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
685     if (g_slist_length(objects) == 0) {
686         /* No objects, set empty */
687         return QUERY_STYLE_NOTHING;
688     }
690     gdouble avgwidth = 0.0;
692     gdouble prev_sw = -1;
693     bool same_sw = true;
694     bool noneSet = true; // is stroke set to none?
696     int n_stroked = 0;
698     for (GSList const *i = objects; i != NULL; i = i->next) {
699         SPObject *obj = SP_OBJECT (i->data);
700         if (!SP_IS_ITEM(obj)) continue;
701         SPStyle *style = SP_OBJECT_STYLE (obj);
702         if (!style) continue;
704         if ( style->stroke.isNone() && !(
705                  style->marker[SP_MARKER_LOC].set || // stroke width affects markers, so if there's no stroke but only markers then we should
706                  style->marker[SP_MARKER_LOC_START].set || // still calculate the stroke width
707                  style->marker[SP_MARKER_LOC_MID].set ||
708                  style->marker[SP_MARKER_LOC_END].set))
709         {
710             continue;
711         }
713         n_stroked ++;
715         noneSet &= style->stroke.isNone();
717         Geom::Matrix i2d = SP_ITEM(obj)->i2d_affine();
718         double sw = style->stroke_width.computed * i2d.descrim();
720         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
721             same_sw = false;
722         prev_sw = sw;
724         avgwidth += sw;
725     }
727     if (n_stroked > 1)
728         avgwidth /= (n_stroked);
730     style_res->stroke_width.computed = avgwidth;
731     style_res->stroke_width.set = true;
732     style_res->stroke.noneSet = noneSet; // Will only be true if none of the selected objects has it's stroke set.
734     if (n_stroked == 0) {
735         return QUERY_STYLE_NOTHING;
736     } else if (n_stroked == 1) {
737         return QUERY_STYLE_SINGLE;
738     } else {
739         if (same_sw)
740             return QUERY_STYLE_MULTIPLE_SAME;
741         else
742             return QUERY_STYLE_MULTIPLE_AVERAGED;
743     }
746 /**
747  * Write to style_res the average miter limit of a list of objects.
748  */
749 int
750 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
752     if (g_slist_length(objects) == 0) {
753         /* No objects, set empty */
754         return QUERY_STYLE_NOTHING;
755     }
757     gdouble avgml = 0.0;
758     int n_stroked = 0;
760     gdouble prev_ml = -1;
761     bool same_ml = true;
763     for (GSList const *i = objects; i != NULL; i = i->next) {
764         SPObject *obj = SP_OBJECT (i->data);
765         if (!SP_IS_ITEM(obj)) continue;
766         SPStyle *style = SP_OBJECT_STYLE (obj);
767         if (!style) continue;
769         if ( style->stroke.isNone() ) {
770             continue;
771         }
773         n_stroked ++;
775         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
776             same_ml = false;
777         prev_ml = style->stroke_miterlimit.value;
779         avgml += style->stroke_miterlimit.value;
780     }
782     if (n_stroked > 1)
783         avgml /= (n_stroked);
785     style_res->stroke_miterlimit.value = avgml;
786     style_res->stroke_miterlimit.set = true;
788     if (n_stroked == 0) {
789         return QUERY_STYLE_NOTHING;
790     } else if (n_stroked == 1) {
791         return QUERY_STYLE_SINGLE;
792     } else {
793         if (same_ml)
794             return QUERY_STYLE_MULTIPLE_SAME;
795         else
796             return QUERY_STYLE_MULTIPLE_AVERAGED;
797     }
800 /**
801  * Write to style_res the stroke cap of a list of objects.
802  */
803 int
804 objects_query_strokecap (GSList *objects, SPStyle *style_res)
806     if (g_slist_length(objects) == 0) {
807         /* No objects, set empty */
808         return QUERY_STYLE_NOTHING;
809     }
811     int cap = -1;
812     gdouble prev_cap = -1;
813     bool same_cap = true;
814     int n_stroked = 0;
816     for (GSList const *i = objects; i != NULL; i = i->next) {
817         SPObject *obj = SP_OBJECT (i->data);
818         if (!SP_IS_ITEM(obj)) continue;
819         SPStyle *style = SP_OBJECT_STYLE (obj);
820         if (!style) continue;
822         if ( style->stroke.isNone() ) {
823             continue;
824         }
826         n_stroked ++;
828         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
829             same_cap = false;
830         prev_cap = style->stroke_linecap.value;
832         cap = style->stroke_linecap.value;
833     }
835     style_res->stroke_linecap.value = cap;
836     style_res->stroke_linecap.set = true;
838     if (n_stroked == 0) {
839         return QUERY_STYLE_NOTHING;
840     } else if (n_stroked == 1) {
841         return QUERY_STYLE_SINGLE;
842     } else {
843         if (same_cap)
844             return QUERY_STYLE_MULTIPLE_SAME;
845         else
846             return QUERY_STYLE_MULTIPLE_DIFFERENT;
847     }
850 /**
851  * Write to style_res the stroke join of a list of objects.
852  */
853 int
854 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
856     if (g_slist_length(objects) == 0) {
857         /* No objects, set empty */
858         return QUERY_STYLE_NOTHING;
859     }
861     int join = -1;
862     gdouble prev_join = -1;
863     bool same_join = true;
864     int n_stroked = 0;
866     for (GSList const *i = objects; i != NULL; i = i->next) {
867         SPObject *obj = SP_OBJECT (i->data);
868         if (!SP_IS_ITEM(obj)) continue;
869         SPStyle *style = SP_OBJECT_STYLE (obj);
870         if (!style) continue;
872         if ( style->stroke.isNone() ) {
873             continue;
874         }
876         n_stroked ++;
878         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
879             same_join = false;
880         prev_join = style->stroke_linejoin.value;
882         join = style->stroke_linejoin.value;
883     }
885     style_res->stroke_linejoin.value = join;
886     style_res->stroke_linejoin.set = true;
888     if (n_stroked == 0) {
889         return QUERY_STYLE_NOTHING;
890     } else if (n_stroked == 1) {
891         return QUERY_STYLE_SINGLE;
892     } else {
893         if (same_join)
894             return QUERY_STYLE_MULTIPLE_SAME;
895         else
896             return QUERY_STYLE_MULTIPLE_DIFFERENT;
897     }
900 /**
901  * Write to style_res the average font size and spacing of objects.
902  */
903 int
904 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
906     bool different = false;
908     double size = 0;
909     double letterspacing = 0;
910     double wordspacing = 0;
911     double linespacing = 0;
912     bool letterspacing_normal = false;
913     bool wordspacing_normal = false;
914     bool linespacing_normal = false;
916     double size_prev = 0;
917     double letterspacing_prev = 0;
918     double wordspacing_prev = 0;
919     double linespacing_prev = 0;
921     int texts = 0;
923     for (GSList const *i = objects; i != NULL; i = i->next) {
924         SPObject *obj = SP_OBJECT (i->data);
926         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
927             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
928             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
929             continue;
931         SPStyle *style = SP_OBJECT_STYLE (obj);
932         if (!style) continue;
934         texts ++;
935         size += style->font_size.computed * Geom::Matrix(SP_ITEM(obj)->i2d_affine()).descrim(); /// \todo FIXME: we assume non-% units here
937         if (style->letter_spacing.normal) {
938             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
939                 letterspacing_normal = true;
940         } else {
941             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
942             letterspacing_normal = false;
943         }
945         if (style->word_spacing.normal) {
946             if (!different && (wordspacing_prev == 0 || wordspacing_prev == wordspacing))
947                 wordspacing_normal = true;
948         } else {
949             wordspacing += style->word_spacing.computed; /// \todo FIXME: we assume non-% units here
950             wordspacing_normal = false;
951         }
953         double linespacing_current;
954         if (style->line_height.normal) {
955             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
956             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
957                 linespacing_normal = true;
958         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
959             linespacing_current = style->line_height.value;
960             linespacing_normal = false;
961         } else { // we need % here
962             linespacing_current = style->line_height.computed / style->font_size.computed;
963             linespacing_normal = false;
964         }
965         linespacing += linespacing_current;
967         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
968             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
969             (wordspacing_prev != 0 && style->word_spacing.computed != wordspacing_prev) ||
970             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
971             different = true;
972         }
974         size_prev = style->font_size.computed;
975         letterspacing_prev = style->letter_spacing.computed;
976         wordspacing_prev = style->word_spacing.computed;
977         linespacing_prev = linespacing_current;
979         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
980         style_res->text_anchor.computed = style->text_anchor.computed;
981         style_res->writing_mode.computed = style->writing_mode.computed;
982     }
984     if (texts == 0)
985         return QUERY_STYLE_NOTHING;
987     if (texts > 1) {
988         size /= texts;
989         letterspacing /= texts;
990         wordspacing /= texts;
991         linespacing /= texts;
992     }
994     style_res->font_size.computed = size;
995     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
997     style_res->letter_spacing.normal = letterspacing_normal;
998     style_res->letter_spacing.computed = letterspacing;
1000     style_res->word_spacing.normal = wordspacing_normal;
1001     style_res->word_spacing.computed = wordspacing;
1003     style_res->line_height.normal = linespacing_normal;
1004     style_res->line_height.computed = linespacing;
1005     style_res->line_height.value = linespacing;
1006     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
1008     if (texts > 1) {
1009         if (different) {
1010             return QUERY_STYLE_MULTIPLE_AVERAGED;
1011         } else {
1012             return QUERY_STYLE_MULTIPLE_SAME;
1013         }
1014     } else {
1015         return QUERY_STYLE_SINGLE;
1016     }
1019 /**
1020  * Write to style_res the average font style of objects.
1021  */
1022 int
1023 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
1025     bool different = false;
1026     bool set = false;
1028     int texts = 0;
1030     for (GSList const *i = objects; i != NULL; i = i->next) {
1031         SPObject *obj = SP_OBJECT (i->data);
1033         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1034             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1035             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1036             continue;
1038         SPStyle *style = SP_OBJECT_STYLE (obj);
1039         if (!style) continue;
1041         texts ++;
1043         if (set &&
1044             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
1045             different = true;  // different styles
1046         }
1048         set = TRUE;
1049         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
1050         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
1051         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
1052         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
1053         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
1054     }
1056     if (texts == 0 || !set)
1057         return QUERY_STYLE_NOTHING;
1059     if (texts > 1) {
1060         if (different) {
1061             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1062         } else {
1063             return QUERY_STYLE_MULTIPLE_SAME;
1064         }
1065     } else {
1066         return QUERY_STYLE_SINGLE;
1067     }
1070 /**
1071  * Write to style_res the baseline numbers.
1072  */
1073 int
1074 objects_query_baselines (GSList *objects, SPStyle *style_res)
1076     bool different = false;
1078     // Only baseline-shift at the moment
1079     // We will return:
1080     //   If baseline-shift is same for all objects:
1081     //     The full baseline-shift data (used for subscripts and superscripts)
1082     //   If baseline-shift is different:
1083     //     The average baseline-shift (not implemented at the moment as this is complicated June 2010)
1084     SPIBaselineShift old;
1085     old.value = 0.0;
1086     old.computed = 0.0;
1088     // double baselineshift = 0.0;
1089     bool set = false;
1091     int texts = 0;
1093     for (GSList const *i = objects; i != NULL; i = i->next) {
1094         SPObject *obj = SP_OBJECT (i->data);
1096         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1097             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1098             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1099             continue;
1101         SPStyle *style = SP_OBJECT_STYLE (obj);
1102         if (!style) continue;
1104         texts ++;
1106         SPIBaselineShift current;
1107         if(style->baseline_shift.set) {
1109             current.set      = style->baseline_shift.set;
1110             current.inherit  = style->baseline_shift.inherit;
1111             current.type     = style->baseline_shift.type;
1112             current.literal  = style->baseline_shift.literal;
1113             current.value    = style->baseline_shift.value;
1114             current.computed = style->baseline_shift.computed;
1116             if( set ) {
1117                 if( current.set      != old.set ||
1118                     current.inherit  != old.inherit ||
1119                     current.type     != old.type ||
1120                     current.literal  != old.literal ||
1121                     current.value    != old.value ||
1122                     current.computed != old.computed ) {
1123                     // Maybe this needs to be better thought out.
1124                     different = true;
1125                 }
1126             }
1128             set = true;
1130             old.set      = current.set;
1131             old.inherit  = current.inherit;
1132             old.type     = current.type;
1133             old.literal  = current.literal;
1134             old.value    = current.value;
1135             old.computed = current.computed;
1136         }
1137     }
1139     if (different || !set ) {
1140         style_res->baseline_shift.set = false;
1141         style_res->baseline_shift.computed = 0.0;
1142     } else {
1143         style_res->baseline_shift.set      = old.set;
1144         style_res->baseline_shift.inherit  = old.inherit;
1145         style_res->baseline_shift.type     = old.type;
1146         style_res->baseline_shift.literal  = old.literal;
1147         style_res->baseline_shift.value    = old.value;
1148         style_res->baseline_shift.computed = old.computed;
1149     }
1151     if (texts == 0 || !set)
1152         return QUERY_STYLE_NOTHING;
1154     if (texts > 1) {
1155         if (different) {
1156             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1157         } else {
1158             return QUERY_STYLE_MULTIPLE_SAME;
1159         }
1160     } else {
1161         return QUERY_STYLE_SINGLE;
1162     }
1165 /**
1166  * Write to style_res the average font family of objects.
1167  */
1168 int
1169 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1171     bool different = false;
1172     int texts = 0;
1174     if (style_res->text->font_family.value) {
1175         g_free(style_res->text->font_family.value);
1176         style_res->text->font_family.value = NULL;
1177     }
1178     style_res->text->font_family.set = FALSE;
1180     for (GSList const *i = objects; i != NULL; i = i->next) {
1181         SPObject *obj = SP_OBJECT (i->data);
1183         // std::cout << "  " << reinterpret_cast<SPObject*>(i->data)->getId() << std::endl;
1184         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1185             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1186             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj)) {
1187             continue;
1188         }
1190         SPStyle *style = SP_OBJECT_STYLE (obj);
1191         if (!style) continue;
1193         texts ++;
1195         if (style_res->text->font_family.value && style->text->font_family.value &&
1196             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1197             different = true;  // different fonts
1198         }
1200         if (style_res->text->font_family.value) {
1201             g_free(style_res->text->font_family.value);
1202             style_res->text->font_family.value = NULL;
1203         }
1205         style_res->text->font_family.set = TRUE;
1206         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1207     }
1209     if (texts == 0 || !style_res->text->font_family.set)
1210         return QUERY_STYLE_NOTHING;
1212     if (texts > 1) {
1213         if (different) {
1214             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1215         } else {
1216             return QUERY_STYLE_MULTIPLE_SAME;
1217         }
1218     } else {
1219         return QUERY_STYLE_SINGLE;
1220     }
1223 int
1224 objects_query_fontspecification (GSList *objects, SPStyle *style_res)
1226     bool different = false;
1227     int texts = 0;
1229     if (style_res->text->font_specification.value) {
1230         g_free(style_res->text->font_specification.value);
1231         style_res->text->font_specification.value = NULL;
1232     }
1233     style_res->text->font_specification.set = FALSE;
1235     for (GSList const *i = objects; i != NULL; i = i->next) {
1236         SPObject *obj = SP_OBJECT (i->data);
1238         // std::cout << "  " << reinterpret_cast<SPObject*>(i->data)->getId() << std::endl;
1239         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1240             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1241             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj)) {
1242             continue;
1243         }
1245         SPStyle *style = SP_OBJECT_STYLE (obj);
1246         if (!style) continue;
1248         texts ++;
1250         if (style_res->text->font_specification.value && style_res->text->font_specification.set &&
1251             style->text->font_specification.value && style->text->font_specification.set &&
1252             strcmp (style_res->text->font_specification.value, style->text->font_specification.value)) {
1253             different = true;  // different fonts
1254         }
1256         if (style->text->font_specification.set) {
1258             if (style_res->text->font_specification.value) {
1259                 g_free(style_res->text->font_specification.value);
1260                 style_res->text->font_specification.value = NULL;
1261             }
1263             style_res->text->font_specification.set = TRUE;
1264             style_res->text->font_specification.value = g_strdup(style->text->font_specification.value);
1265         }
1266     }
1268     if (texts == 0)
1269         return QUERY_STYLE_NOTHING;
1271     if (texts > 1) {
1272         if (different) {
1273             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1274         } else {
1275             return QUERY_STYLE_MULTIPLE_SAME;
1276         }
1277     } else {
1278         return QUERY_STYLE_SINGLE;
1279     }
1282 int
1283 objects_query_blend (GSList *objects, SPStyle *style_res)
1285     const int empty_prev = -2;
1286     const int complex_filter = 5;
1287     int blend = 0;
1288     float blend_prev = empty_prev;
1289     bool same_blend = true;
1290     guint items = 0;
1292     for (GSList const *i = objects; i != NULL; i = i->next) {
1293         SPObject *obj = SP_OBJECT (i->data);
1294         SPStyle *style = SP_OBJECT_STYLE (obj);
1295         if(!style || !SP_IS_ITEM(obj)) continue;
1297         items++;
1299         //if object has a filter
1300         if (style->filter.set && style->getFilter()) {
1301             int blurcount = 0;
1302             int blendcount = 0;
1304             // determine whether filter is simple (blend and/or blur) or complex
1305             for(SPObject *primitive_obj = style->getFilter()->children;
1306                 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1307                 primitive_obj = primitive_obj->next) {
1308                 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1309                 if(SP_IS_FEBLEND(primitive))
1310                     ++blendcount;
1311                 else if(SP_IS_GAUSSIANBLUR(primitive))
1312                     ++blurcount;
1313                 else {
1314                     blurcount = complex_filter;
1315                     break;
1316                 }
1317             }
1319             // simple filter
1320             if(blurcount == 1 || blendcount == 1) {
1321                 for(SPObject *primitive_obj = style->getFilter()->children;
1322                     primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1323                     primitive_obj = primitive_obj->next) {
1324                     if(SP_IS_FEBLEND(primitive_obj)) {
1325                         SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1326                         blend = spblend->blend_mode;
1327                     }
1328                 }
1329             }
1330             else {
1331                 blend = complex_filter;
1332             }
1333         }
1334         // defaults to blend mode = "normal"
1335         else {
1336             blend = 0;
1337         }
1339         if(blend_prev != empty_prev && blend_prev != blend)
1340             same_blend = false;
1341         blend_prev = blend;
1342     }
1344     if (items > 0) {
1345         style_res->filter_blend_mode.value = blend;
1346     }
1348     if (items == 0) {
1349         return QUERY_STYLE_NOTHING;
1350     } else if (items == 1) {
1351         return QUERY_STYLE_SINGLE;
1352     } else {
1353         if(same_blend)
1354             return QUERY_STYLE_MULTIPLE_SAME;
1355         else
1356             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1357     }
1360 /**
1361  * Write to style_res the average blurring of a list of objects.
1362  */
1363 int
1364 objects_query_blur (GSList *objects, SPStyle *style_res)
1366    if (g_slist_length(objects) == 0) {
1367         /* No objects, set empty */
1368         return QUERY_STYLE_NOTHING;
1369     }
1371     float blur_sum = 0;
1372     float blur_prev = -1;
1373     bool same_blur = true;
1374     guint blur_items = 0;
1375     guint items = 0;
1377     for (GSList const *i = objects; i != NULL; i = i->next) {
1378         SPObject *obj = SP_OBJECT (i->data);
1379         SPStyle *style = SP_OBJECT_STYLE (obj);
1380         if (!style) continue;
1381         if (!SP_IS_ITEM(obj)) continue;
1383         Geom::Matrix i2d = SP_ITEM(obj)->i2d_affine();
1385         items ++;
1387         //if object has a filter
1388         if (style->filter.set && style->getFilter()) {
1389             //cycle through filter primitives
1390             SPObject *primitive_obj = style->getFilter()->children;
1391             while (primitive_obj) {
1392                 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1393                     SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1395                     //if primitive is gaussianblur
1396                     if(SP_IS_GAUSSIANBLUR(primitive)) {
1397                         SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1398                         float num = spblur->stdDeviation.getNumber();
1399                         blur_sum += num * i2d.descrim();
1400                         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
1401                             same_blur = false;
1402                         blur_prev = num;
1403                         //TODO: deal with opt number, for the moment it's not necessary to the ui.
1404                         blur_items ++;
1405                     }
1406                 }
1407                 primitive_obj = primitive_obj->next;
1408             }
1409         }
1410     }
1412     if (items > 0) {
1413         if (blur_items > 0)
1414             blur_sum /= blur_items;
1415         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1416     }
1418     if (items == 0) {
1419         return QUERY_STYLE_NOTHING;
1420     } else if (items == 1) {
1421         return QUERY_STYLE_SINGLE;
1422     } else {
1423         if (same_blur)
1424             return QUERY_STYLE_MULTIPLE_SAME;
1425         else
1426             return QUERY_STYLE_MULTIPLE_AVERAGED;
1427     }
1430 /**
1431  * Query the given list of objects for the given property, write
1432  * the result to style, return appropriate flag.
1433  */
1434 int
1435 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1437     if (property == QUERY_STYLE_PROPERTY_FILL) {
1438         return objects_query_fillstroke (list, style, true);
1439     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1440         return objects_query_fillstroke (list, style, false);
1442     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1443         return objects_query_strokewidth (list, style);
1444     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1445         return objects_query_miterlimit (list, style);
1446     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1447         return objects_query_strokecap (list, style);
1448     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1449         return objects_query_strokejoin (list, style);
1451     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1452         return objects_query_opacity (list, style);
1454     } else if (property == QUERY_STYLE_PROPERTY_FONT_SPECIFICATION) {
1455         return objects_query_fontspecification (list, style);
1456     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1457         return objects_query_fontfamily (list, style);
1458     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1459         return objects_query_fontstyle (list, style);
1460     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1461         return objects_query_fontnumbers (list, style);
1462     } else if (property == QUERY_STYLE_PROPERTY_BASELINES) {
1463         return objects_query_baselines (list, style);
1465     } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1466         return objects_query_blend (list, style);
1467     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1468         return objects_query_blur (list, style);
1469     }
1470     return QUERY_STYLE_NOTHING;
1474 /**
1475  * Query the subselection (if any) or selection on the given desktop for the given property, write
1476  * the result to style, return appropriate flag.
1477  */
1478 int
1479 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1481     int ret = desktop->_query_style_signal.emit(style, property);
1483     if (ret != QUERY_STYLE_NOTHING)
1484         return ret; // subselection returned a style, pass it on
1486     // otherwise, do querying and averaging over selection
1487     if (desktop->selection != NULL) {
1488         return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1489     }
1491     return QUERY_STYLE_NOTHING;
1494 /**
1495  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if at
1496  * least one of the properties did not return QUERY_STYLE_NOTHING.
1497  */
1498 bool
1499 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1501         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1502         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1503         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1504         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1505         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1506         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1507         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1508         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1509         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1510         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1511         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1513         return (result_family != QUERY_STYLE_NOTHING ||
1514                 result_fstyle != QUERY_STYLE_NOTHING ||
1515                 result_fnumbers != QUERY_STYLE_NOTHING ||
1516                 result_fill != QUERY_STYLE_NOTHING ||
1517                 result_stroke != QUERY_STYLE_NOTHING ||
1518                 result_opacity != QUERY_STYLE_NOTHING ||
1519                 result_strokewidth != QUERY_STYLE_NOTHING ||
1520                 result_strokemiterlimit != QUERY_STYLE_NOTHING ||
1521                 result_strokecap != QUERY_STYLE_NOTHING ||
1522                 result_strokejoin != QUERY_STYLE_NOTHING ||
1523                 result_blur != QUERY_STYLE_NOTHING);
1527 /*
1528   Local Variables:
1529   mode:c++
1530   c-file-style:"stroustrup"
1531   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1532   indent-tabs-mode:nil
1533   fill-column:99
1534   End:
1535 */
1536 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :