Code

Fix uniconvertor extensions to work with UC version 1.1.5
[inkscape.git] / src / desktop-style.cpp
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 "box3d-side.h"
49 /**
50  * Set color on selection on desktop.
51  */
52 void
53 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
54 {
55     /// \todo relative color setting
56     if (is_relative) {
57         g_warning("FIXME: relative color setting not yet implemented");
58         return;
59     }
61     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
62     gchar b[64];
63     sp_svg_write_color(b, sizeof(b), rgba);
64     SPCSSAttr *css = sp_repr_css_attr_new();
65     if (fill) {
66         sp_repr_css_set_property(css, "fill", b);
67         Inkscape::CSSOStringStream osalpha;
68         osalpha << color[3];
69         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
70     } else {
71         sp_repr_css_set_property(css, "stroke", b);
72         Inkscape::CSSOStringStream osalpha;
73         osalpha << color[3];
74         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
75     }
77     sp_desktop_set_style(desktop, css);
79     sp_repr_css_attr_unref(css);
80 }
82 /**
83  * Apply style on object and children, recursively.
84  */
85 void
86 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
87 {
88     // non-items should not have style
89     if (!SP_IS_ITEM(o))
90         return;
92     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
93     // but must always inherit from the parent text. Same for textPath.
94     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
96     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
97     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
98     // flowtext).
100     if (!(skip_lines
101           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
102               || SP_IS_FLOWDIV(o)
103               || SP_IS_FLOWPARA(o)
104               || SP_IS_TEXTPATH(o))
105           && !SP_OBJECT_REPR(o)->attribute("style"))
106         &&
107         !(SP_IS_FLOWREGION(o) ||
108           SP_IS_FLOWREGIONEXCLUDE(o) ||
109           (SP_IS_USE(o) &&
110            SP_OBJECT_PARENT(o) &&
111            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
112             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
113            )
114           )
115          )
116         ) {
118         SPCSSAttr *css_set = sp_repr_css_attr_new();
119         sp_repr_css_merge(css_set, css);
121         // Scale the style by the inverse of the accumulated parent transform in the paste context.
122         {
123             Geom::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
124             double const ex(local.descrim());
125             if ( ( ex != 0. )
126                  && ( ex != 1. ) ) {
127                 sp_css_attr_scale(css_set, 1/ex);
128             }
129         }
131         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
133         sp_repr_css_attr_unref(css_set);
134     }
136     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
137     if (SP_IS_USE(o))
138         return;
140     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
141         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
142             // Unset properties which are accumulating and thus should not be set recursively.
143             // 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.
144             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
145             sp_repr_css_merge(css_recurse, css);
146             sp_repr_css_set_property(css_recurse, "opacity", NULL);
147             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
148             sp_repr_css_attr_unref(css_recurse);
149         } else {
150             sp_desktop_apply_css_recursive(child, css, skip_lines);
151         }
152     }
155 /**
156  * Apply style on selection on desktop.
157  */
158 void
159 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
161     if (write_current) {
162         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
163         // 1. Set internal value
164         sp_repr_css_merge(desktop->current, css);
166         // 1a. Write to prefs; make a copy and unset any URIs first
167         SPCSSAttr *css_write = sp_repr_css_attr_new();
168         sp_repr_css_merge(css_write, css);
169         sp_css_attr_unset_uris(css_write);
170         prefs->mergeStyle("/desktop/style", css_write);
172         for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
173             /* last used styles for 3D box faces are stored separately */
174             if (SP_IS_BOX3D_SIDE (i->data)) {
175                 const char * descr  = box3d_side_axes_string(SP_BOX3D_SIDE(i->data));
176                 if (descr != NULL) {
177                     prefs->mergeStyle(Glib::ustring("/desktop/") + descr + "/style", css_write);
178                 }
179             }
180         }
181         sp_repr_css_attr_unref(css_write);
182     }
184     if (!change)
185         return;
187 // 2. Emit signal
188     bool intercepted = desktop->_set_style_signal.emit(css);
190 /** \todo
191  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
192  * rect corners, font size for the object's own transform so that pasting
193  * fills does not depend on preserve/optimize.
194  */
196 // 3. If nobody has intercepted the signal, apply the style to the selection
197     if (!intercepted) {
199         // Remove text attributes if not text...
200         // Do this once in case a zillion objects are selected.
201         SPCSSAttr *css_no_text = sp_repr_css_attr_new();
202         sp_repr_css_merge(css_no_text, css);
203         css_no_text = sp_css_attr_unset_text(css_no_text);
205         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
207             // If not text, don't apply text attributes (can a group have text attributes?)
208             if ( SP_IS_TEXT(i->data) || SP_IS_FLOWTEXT(i->data)
209                 || SP_IS_TSPAN(i->data) || SP_IS_TREF(i->data) || SP_IS_TEXTPATH(i->data)
210                 || SP_IS_FLOWDIV(i->data) || SP_IS_FLOWPARA(i->data) || SP_IS_FLOWTSPAN(i->data)) {
212                 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
214             } else {
216                 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css_no_text, true);
218             }
219         }
220         sp_repr_css_attr_unref(css_no_text);
221     }
224 /**
225  * Return the desktop's current style.
226  */
227 SPCSSAttr *
228 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
230     SPCSSAttr *css = sp_repr_css_attr_new();
231     sp_repr_css_merge(css, desktop->current);
232     if (!css->attributeList()) {
233         sp_repr_css_attr_unref(css);
234         return NULL;
235     } else {
236         if (!with_text) {
237             css = sp_css_attr_unset_text(css);
238         }
239         return css;
240     }
243 /**
244  * Return the desktop's current color.
245  */
246 guint32
247 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
249     guint32 r = 0; // if there's no color, return black
250     gchar const *property = sp_repr_css_property(desktop->current,
251                                                  is_fill ? "fill" : "stroke",
252                                                  "#000");
254     if (desktop->current && property) { // if there is style and the property in it,
255         if (strncmp(property, "url", 3)) { // and if it's not url,
256             // read it
257             r = sp_svg_read_color(property, r);
258         }
259     }
261     return r;
264 double
265 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool *has_opacity)
267     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
268     SPCSSAttr *css = NULL;
269     gfloat value = 1.0; // default if nothing else found
270     if (has_opacity)
271         *has_opacity = false;
272     if (prefs->getBool(tool + "/usecurrent")) {
273         css = sp_desktop_get_style(desktop, true);
274     } else {
275         css = prefs->getStyle(tool + "/style");
276     }
278     if (css) {
279         gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
281         if (desktop->current && property) { // if there is style and the property in it,
282             if ( !sp_svg_number_read_f(property, &value) ) {
283                 value = 1.0; // things failed. set back to the default
284             } else {
285                 if (has_opacity)
286                    *has_opacity = false;
287             }
288         }
290         sp_repr_css_attr_unref(css);
291     }
293     return value;
295 double
296 sp_desktop_get_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill)
298     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
299     SPCSSAttr *css = NULL;
300     gfloat value = 1.0; // default if nothing else found
301     if (prefs->getBool(tool + "/usecurrent")) {
302         css = sp_desktop_get_style(desktop, true);
303     } else {
304         css = prefs->getStyle(tool + "/style");
305     }
307     if (css) {
308         gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
310         if (desktop->current && property) { // if there is style and the property in it,
311             if ( !sp_svg_number_read_f(property, &value) ) {
312                 value = 1.0; // things failed. set back to the default
313             }
314         }
316         sp_repr_css_attr_unref(css);
317     }
319     return value;
322 guint32
323 sp_desktop_get_color_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill, bool *has_color)
325     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
326     SPCSSAttr *css = NULL;
327     guint32 r = 0; // if there's no color, return black
328     if (has_color)
329         *has_color = false;
330     if (prefs->getBool(tool + "/usecurrent")) {
331         css = sp_desktop_get_style(desktop, true);
332     } else {
333         css = prefs->getStyle(tool + "/style");
334     }
336     if (css) {
337         gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
339         if (desktop->current && property) { // if there is style and the property in it,
340             if (strncmp(property, "url", 3) && strncmp(property, "none", 4)) { // and if it's not url or none,
341                 // read it
342                 r = sp_svg_read_color(property, r);
343                 if (has_color)
344                     *has_color = true;
345             }
346         }
348         sp_repr_css_attr_unref(css);
349     }
351     return r | 0xff;
354 /**
355  * Apply the desktop's current style or the tool style to repr.
356  */
357 void
358 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, Glib::ustring const &tool_path, bool with_text)
360     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
361     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
363     if (prefs->getBool(tool_path + "/usecurrent") && css_current) {
364         sp_repr_css_set(repr, css_current, "style");
365     } else {
366         SPCSSAttr *css = prefs->getInheritedStyle(tool_path + "/style");
367         sp_repr_css_set(repr, css, "style");
368         sp_repr_css_attr_unref(css);
369     }
370     if (css_current) {
371         sp_repr_css_attr_unref(css_current);
372     }
375 /**
376  * Returns the font size (in SVG pixels) of the text tool style (if text
377  * tool uses its own style) or desktop style (otherwise).
378 */
379 double
380 sp_desktop_get_font_size_tool(SPDesktop *desktop)
382     (void)desktop; // TODO cleanup
383     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
384     Glib::ustring desktop_style = prefs->getString("/desktop/style");
385     Glib::ustring style_str;
386     if ((prefs->getBool("/tools/text/usecurrent")) && !desktop_style.empty()) {
387         style_str = desktop_style;
388     } else {
389         style_str = prefs->getString("/tools/text/style");
390     }
392     double ret = 12;
393     if (!style_str.empty()) {
394         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
395         sp_style_merge_from_style_string(style, style_str.data());
396         ret = style->font_size.computed;
397         sp_style_unref(style);
398     }
399     return ret;
402 /** Determine average stroke width, simple method */
403 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
404 gdouble
405 stroke_average_width (GSList const *objects)
407     if (g_slist_length ((GSList *) objects) == 0)
408         return NR_HUGE;
410     gdouble avgwidth = 0.0;
411     bool notstroked = true;
412     int n_notstroked = 0;
414     for (GSList const *l = objects; l != NULL; l = l->next) {
415         if (!SP_IS_ITEM (l->data))
416             continue;
418         Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
420         SPObject *object = SP_OBJECT(l->data);
422         if ( object->style->stroke.isNone() ) {
423             ++n_notstroked;   // do not count nonstroked objects
424             continue;
425         } else {
426             notstroked = false;
427         }
429         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.descrim();
430     }
432     if (notstroked)
433         return NR_HUGE;
435     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
438 static bool vectorsClose( std::vector<double> const &lhs, std::vector<double> const &rhs )
440     static double epsilon = 1e-6;
441     bool isClose = false;
442     if ( lhs.size() == rhs.size() ) {
443         isClose = true;
444         for ( size_t i = 0; (i < lhs.size()) && isClose; ++i ) {
445             isClose = fabs(lhs[i] - rhs[i]) < epsilon;
446         }
447     }
448     return isClose;
452 /**
453  * Write to style_res the average fill or stroke of list of objects, if applicable.
454  */
455 int
456 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
458     if (g_slist_length(objects) == 0) {
459         /* No objects, set empty */
460         return QUERY_STYLE_NOTHING;
461     }
463     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
464     bool paintImpossible = true;
465     paint_res->set = TRUE;
467     SVGICCColor* iccColor = 0;
469     bool iccSeen = false;
470     gfloat c[4];
471     c[0] = c[1] = c[2] = c[3] = 0.0;
472     gint num = 0;
474     gfloat prev[3];
475     prev[0] = prev[1] = prev[2] = 0.0;
476     bool same_color = true;
478     for (GSList const *i = objects; i != NULL; i = i->next) {
479         SPObject *obj = SP_OBJECT (i->data);
480         SPStyle *style = SP_OBJECT_STYLE (obj);
481         if (!style) continue;
483         SPIPaint *paint = isfill? &style->fill : &style->stroke;
485         // We consider paint "effectively set" for anything within text hierarchy
486         SPObject *parent = SP_OBJECT_PARENT (obj);
487         bool paint_effectively_set =
488             paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
489             || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
490             || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
492         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
494         if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
495             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
496         }
498         if (paint_res->set && paint->set && paint_res->isPaintserver()) {
499             // both previous paint and this paint were a server, see if the servers are compatible
501             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
502             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
504             if (SP_IS_LINEARGRADIENT (server_res)) {
506                 if (!SP_IS_LINEARGRADIENT(server))
507                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
509                 SPGradient *vector = SP_GRADIENT(server)->getVector();
510                 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
511                 if (vector_res != vector)
512                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
514             } else if (SP_IS_RADIALGRADIENT (server_res)) {
516                 if (!SP_IS_RADIALGRADIENT(server))
517                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
519                 SPGradient *vector = SP_GRADIENT(server)->getVector();
520                 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
521                 if (vector_res != vector)
522                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
524             } else if (SP_IS_PATTERN (server_res)) {
526                 if (!SP_IS_PATTERN(server))
527                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
529                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
530                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
531                 if (pat_res != pat)
532                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
533             }
534         }
536         // 2. Sum color, copy server from paint to paint_res
538         if (paint_res->set && paint_effectively_set && paint->isColor()) {
539             gfloat d[3];
540             sp_color_get_rgb_floatv (&paint->value.color, d);
542             // Check if this color is the same as previous
543             if (paintImpossible) {
544                 prev[0] = d[0];
545                 prev[1] = d[1];
546                 prev[2] = d[2];
547                 paint_res->setColor(d[0], d[1], d[2]);
548                 iccColor = paint->value.color.icc;
549                 iccSeen = true;
550             } else {
551                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2])) {
552                     same_color = false;
553                     iccColor = 0;
554                 }
555                 if ( iccSeen && iccColor ) {
556                     if ( !paint->value.color.icc
557                          || (iccColor->colorProfile != paint->value.color.icc->colorProfile)
558                          || !vectorsClose(iccColor->colors, paint->value.color.icc->colors) ) {
559                         same_color = false;
560                         iccColor = 0;
561                     }
562                 }
563             }
565             // average color
566             c[0] += d[0];
567             c[1] += d[1];
568             c[2] += d[2];
569             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
571             num ++;
572         }
574        paintImpossible = false;
575        paint_res->colorSet = paint->colorSet;
576        paint_res->currentcolor = paint->currentcolor;
577        if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
578            if (isfill) {
579                sp_style_set_to_uri_string (style_res, true, style->getFillURI());
580            } else {
581                sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
582            }
583        }
584        paint_res->set = paint_effectively_set;
585        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
586     }
588     // After all objects processed, divide the color if necessary and return
589     if (paint_res->set && paint_res->isColor()) { // set the color
590         g_assert (num >= 1);
592         c[0] /= num;
593         c[1] /= num;
594         c[2] /= num;
595         c[3] /= num;
596         paint_res->setColor(c[0], c[1], c[2]);
597         if (isfill) {
598             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
599         } else {
600             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
601         }
604         if ( iccSeen && iccColor ) {
605             // TODO check for existing
606             SVGICCColor* tmp = new SVGICCColor(*iccColor);
607             paint_res->value.color.icc = tmp;
608         }
610         if (num > 1) {
611             if (same_color)
612                 return QUERY_STYLE_MULTIPLE_SAME;
613             else
614                 return QUERY_STYLE_MULTIPLE_AVERAGED;
615         } else {
616             return QUERY_STYLE_SINGLE;
617         }
618     }
620     // Not color
621     if (g_slist_length(objects) > 1) {
622         return QUERY_STYLE_MULTIPLE_SAME;
623     } else {
624         return QUERY_STYLE_SINGLE;
625     }
628 /**
629  * Write to style_res the average opacity of a list of objects.
630  */
631 int
632 objects_query_opacity (GSList *objects, SPStyle *style_res)
634     if (g_slist_length(objects) == 0) {
635         /* No objects, set empty */
636         return QUERY_STYLE_NOTHING;
637     }
639     gdouble opacity_sum = 0;
640     gdouble opacity_prev = -1;
641     bool same_opacity = true;
642     guint opacity_items = 0;
645     for (GSList const *i = objects; i != NULL; i = i->next) {
646         SPObject *obj = SP_OBJECT (i->data);
647         SPStyle *style = SP_OBJECT_STYLE (obj);
648         if (!style) continue;
650         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
651         opacity_sum += opacity;
652         if (opacity_prev != -1 && opacity != opacity_prev)
653             same_opacity = false;
654         opacity_prev = opacity;
655         opacity_items ++;
656     }
657     if (opacity_items > 1)
658         opacity_sum /= opacity_items;
660     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
662     if (opacity_items == 0) {
663         return QUERY_STYLE_NOTHING;
664     } else if (opacity_items == 1) {
665         return QUERY_STYLE_SINGLE;
666     } else {
667         if (same_opacity)
668             return QUERY_STYLE_MULTIPLE_SAME;
669         else
670             return QUERY_STYLE_MULTIPLE_AVERAGED;
671     }
674 /**
675  * Write to style_res the average stroke width of a list of objects.
676  */
677 int
678 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
680     if (g_slist_length(objects) == 0) {
681         /* No objects, set empty */
682         return QUERY_STYLE_NOTHING;
683     }
685     gdouble avgwidth = 0.0;
687     gdouble prev_sw = -1;
688     bool same_sw = true;
689     bool noneSet = true; // is stroke set to none?
691     int n_stroked = 0;
693     for (GSList const *i = objects; i != NULL; i = i->next) {
694         SPObject *obj = SP_OBJECT (i->data);
695         if (!SP_IS_ITEM(obj)) continue;
696         SPStyle *style = SP_OBJECT_STYLE (obj);
697         if (!style) continue;
699         if ( style->stroke.isNone() && !(
700                                 style->marker[SP_MARKER_LOC].set || // stroke width affects markers, so if there's no stroke but only markers then we should
701                                 style->marker[SP_MARKER_LOC_START].set || // still calculate the stroke width
702                                 style->marker[SP_MARKER_LOC_MID].set ||
703                                 style->marker[SP_MARKER_LOC_END].set))
704                 {
705             continue;
706         }
708         n_stroked ++;
710         noneSet &= style->stroke.isNone();
712         Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
713         double sw = style->stroke_width.computed * i2d.descrim();
715         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
716             same_sw = false;
717         prev_sw = sw;
719         avgwidth += sw;
720     }
722     if (n_stroked > 1)
723         avgwidth /= (n_stroked);
725     style_res->stroke_width.computed = avgwidth;
726     style_res->stroke_width.set = true;
727     style_res->stroke.noneSet = noneSet; // Will only be true if none of the selected objects has it's stroke set.
729     if (n_stroked == 0) {
730         return QUERY_STYLE_NOTHING;
731     } else if (n_stroked == 1) {
732         return QUERY_STYLE_SINGLE;
733     } else {
734         if (same_sw)
735             return QUERY_STYLE_MULTIPLE_SAME;
736         else
737             return QUERY_STYLE_MULTIPLE_AVERAGED;
738     }
741 /**
742  * Write to style_res the average miter limit of a list of objects.
743  */
744 int
745 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
747     if (g_slist_length(objects) == 0) {
748         /* No objects, set empty */
749         return QUERY_STYLE_NOTHING;
750     }
752     gdouble avgml = 0.0;
753     int n_stroked = 0;
755     gdouble prev_ml = -1;
756     bool same_ml = true;
758     for (GSList const *i = objects; i != NULL; i = i->next) {
759         SPObject *obj = SP_OBJECT (i->data);
760         if (!SP_IS_ITEM(obj)) continue;
761         SPStyle *style = SP_OBJECT_STYLE (obj);
762         if (!style) continue;
764         if ( style->stroke.isNone() ) {
765             continue;
766         }
768         n_stroked ++;
770         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
771             same_ml = false;
772         prev_ml = style->stroke_miterlimit.value;
774         avgml += style->stroke_miterlimit.value;
775     }
777     if (n_stroked > 1)
778         avgml /= (n_stroked);
780     style_res->stroke_miterlimit.value = avgml;
781     style_res->stroke_miterlimit.set = true;
783     if (n_stroked == 0) {
784         return QUERY_STYLE_NOTHING;
785     } else if (n_stroked == 1) {
786         return QUERY_STYLE_SINGLE;
787     } else {
788         if (same_ml)
789             return QUERY_STYLE_MULTIPLE_SAME;
790         else
791             return QUERY_STYLE_MULTIPLE_AVERAGED;
792     }
795 /**
796  * Write to style_res the stroke cap of a list of objects.
797  */
798 int
799 objects_query_strokecap (GSList *objects, SPStyle *style_res)
801     if (g_slist_length(objects) == 0) {
802         /* No objects, set empty */
803         return QUERY_STYLE_NOTHING;
804     }
806     int cap = -1;
807     gdouble prev_cap = -1;
808     bool same_cap = true;
809     int n_stroked = 0;
811     for (GSList const *i = objects; i != NULL; i = i->next) {
812         SPObject *obj = SP_OBJECT (i->data);
813         if (!SP_IS_ITEM(obj)) continue;
814         SPStyle *style = SP_OBJECT_STYLE (obj);
815         if (!style) continue;
817         if ( style->stroke.isNone() ) {
818             continue;
819         }
821         n_stroked ++;
823         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
824             same_cap = false;
825         prev_cap = style->stroke_linecap.value;
827         cap = style->stroke_linecap.value;
828     }
830     style_res->stroke_linecap.value = cap;
831     style_res->stroke_linecap.set = true;
833     if (n_stroked == 0) {
834         return QUERY_STYLE_NOTHING;
835     } else if (n_stroked == 1) {
836         return QUERY_STYLE_SINGLE;
837     } else {
838         if (same_cap)
839             return QUERY_STYLE_MULTIPLE_SAME;
840         else
841             return QUERY_STYLE_MULTIPLE_DIFFERENT;
842     }
845 /**
846  * Write to style_res the stroke join of a list of objects.
847  */
848 int
849 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
851     if (g_slist_length(objects) == 0) {
852         /* No objects, set empty */
853         return QUERY_STYLE_NOTHING;
854     }
856     int join = -1;
857     gdouble prev_join = -1;
858     bool same_join = true;
859     int n_stroked = 0;
861     for (GSList const *i = objects; i != NULL; i = i->next) {
862         SPObject *obj = SP_OBJECT (i->data);
863         if (!SP_IS_ITEM(obj)) continue;
864         SPStyle *style = SP_OBJECT_STYLE (obj);
865         if (!style) continue;
867         if ( style->stroke.isNone() ) {
868             continue;
869         }
871         n_stroked ++;
873         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
874             same_join = false;
875         prev_join = style->stroke_linejoin.value;
877         join = style->stroke_linejoin.value;
878     }
880     style_res->stroke_linejoin.value = join;
881     style_res->stroke_linejoin.set = true;
883     if (n_stroked == 0) {
884         return QUERY_STYLE_NOTHING;
885     } else if (n_stroked == 1) {
886         return QUERY_STYLE_SINGLE;
887     } else {
888         if (same_join)
889             return QUERY_STYLE_MULTIPLE_SAME;
890         else
891             return QUERY_STYLE_MULTIPLE_DIFFERENT;
892     }
895 /**
896  * Write to style_res the average font size and spacing of objects.
897  */
898 int
899 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
901     bool different = false;
903     double size = 0;
904     double letterspacing = 0;
905     double wordspacing = 0;
906     double linespacing = 0;
907     bool letterspacing_normal = false;
908     bool wordspacing_normal = false;
909     bool linespacing_normal = false;
911     double size_prev = 0;
912     double letterspacing_prev = 0;
913     double wordspacing_prev = 0;
914     double linespacing_prev = 0;
916     int texts = 0;
918     for (GSList const *i = objects; i != NULL; i = i->next) {
919         SPObject *obj = SP_OBJECT (i->data);
921         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
922             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
923             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
924             continue;
926         SPStyle *style = SP_OBJECT_STYLE (obj);
927         if (!style) continue;
929         texts ++;
930         size += style->font_size.computed * Geom::Matrix(sp_item_i2d_affine(SP_ITEM(obj))).descrim(); /// \todo FIXME: we assume non-% units here
932         if (style->letter_spacing.normal) {
933             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
934                 letterspacing_normal = true;
935         } else {
936             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
937             letterspacing_normal = false;
938         }
940         if (style->word_spacing.normal) {
941             if (!different && (wordspacing_prev == 0 || wordspacing_prev == wordspacing))
942                 wordspacing_normal = true;
943         } else {
944             wordspacing += style->word_spacing.computed; /// \todo FIXME: we assume non-% units here
945             wordspacing_normal = false;
946         }
948         double linespacing_current;
949         if (style->line_height.normal) {
950             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
951             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
952                 linespacing_normal = true;
953         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
954             linespacing_current = style->line_height.value;
955             linespacing_normal = false;
956         } else { // we need % here
957             linespacing_current = style->line_height.computed / style->font_size.computed;
958             linespacing_normal = false;
959         }
960         linespacing += linespacing_current;
962         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
963             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
964             (wordspacing_prev != 0 && style->word_spacing.computed != wordspacing_prev) ||
965             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
966             different = true;
967         }
969         size_prev = style->font_size.computed;
970         letterspacing_prev = style->letter_spacing.computed;
971         wordspacing_prev = style->word_spacing.computed;
972         linespacing_prev = linespacing_current;
974         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
975         style_res->text_anchor.computed = style->text_anchor.computed;
976         style_res->writing_mode.computed = style->writing_mode.computed;
977     }
979     if (texts == 0)
980         return QUERY_STYLE_NOTHING;
982     if (texts > 1) {
983         size /= texts;
984         letterspacing /= texts;
985         wordspacing /= texts;
986         linespacing /= texts;
987     }
989     style_res->font_size.computed = size;
990     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
992     style_res->letter_spacing.normal = letterspacing_normal;
993     style_res->letter_spacing.computed = letterspacing;
995     style_res->word_spacing.normal = wordspacing_normal;
996     style_res->word_spacing.computed = wordspacing;
998     style_res->line_height.normal = linespacing_normal;
999     style_res->line_height.computed = linespacing;
1000     style_res->line_height.value = linespacing;
1001     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
1003     if (texts > 1) {
1004         if (different) {
1005             return QUERY_STYLE_MULTIPLE_AVERAGED;
1006         } else {
1007             return QUERY_STYLE_MULTIPLE_SAME;
1008         }
1009     } else {
1010         return QUERY_STYLE_SINGLE;
1011     }
1014 /**
1015  * Write to style_res the average font style of objects.
1016  */
1017 int
1018 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
1020     bool different = false;
1021     bool set = false;
1023     int texts = 0;
1025     for (GSList const *i = objects; i != NULL; i = i->next) {
1026         SPObject *obj = SP_OBJECT (i->data);
1028         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1029             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1030             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1031             continue;
1033         SPStyle *style = SP_OBJECT_STYLE (obj);
1034         if (!style) continue;
1036         texts ++;
1038         if (set &&
1039             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
1040             different = true;  // different styles
1041         }
1043         set = TRUE;
1044         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
1045         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
1046         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
1047         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
1048         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
1049     }
1051     if (texts == 0 || !set)
1052         return QUERY_STYLE_NOTHING;
1054     if (texts > 1) {
1055         if (different) {
1056             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1057         } else {
1058             return QUERY_STYLE_MULTIPLE_SAME;
1059         }
1060     } else {
1061         return QUERY_STYLE_SINGLE;
1062     }
1065 /**
1066  * Write to style_res the baseline numbers.
1067  */
1068 int
1069 objects_query_baselines (GSList *objects, SPStyle *style_res)
1071     bool different = false;
1073     // Only baseline-shift at the moment
1074     // We will return:
1075     //   If baseline-shift is same for all objects:
1076     //     The full baseline-shift data (used for subscripts and superscripts)
1077     //   If baseline-shift is different:
1078     //     The average baseline-shift (not implemented at the moment as this is complicated June 2010)
1079     SPIBaselineShift old;
1080     old.value = 0.0;
1081     old.computed = 0.0;
1083     // double baselineshift = 0.0;
1084     bool set = false;
1086     int texts = 0;
1088     for (GSList const *i = objects; i != NULL; i = i->next) {
1089         SPObject *obj = SP_OBJECT (i->data);
1091         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1092             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1093             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1094             continue;
1096         SPStyle *style = SP_OBJECT_STYLE (obj);
1097         if (!style) continue;
1099         texts ++;
1101         SPIBaselineShift current;
1102         if(style->baseline_shift.set) {
1104             current.set      = style->baseline_shift.set;
1105             current.inherit  = style->baseline_shift.inherit;
1106             current.type     = style->baseline_shift.type;
1107             current.literal  = style->baseline_shift.literal;
1108             current.value    = style->baseline_shift.value;
1109             current.computed = style->baseline_shift.computed;
1111             if( set ) {
1112                 if( current.set      != old.set ||
1113                     current.inherit  != old.inherit ||
1114                     current.type     != old.type ||
1115                     current.literal  != old.literal ||
1116                     current.value    != old.value ||
1117                     current.computed != old.computed ) {
1118                     // Maybe this needs to be better thought out.
1119                     different = true;
1120                 }
1121             }
1123             set = true;
1125             old.set      = current.set;
1126             old.inherit  = current.inherit;
1127             old.type     = current.type;
1128             old.literal  = current.literal;
1129             old.value    = current.value;
1130             old.computed = current.computed;
1131         }
1132     }
1134     if (different || !set ) {
1135         style_res->baseline_shift.set = false;
1136         style_res->baseline_shift.computed = 0.0;
1137     } else {
1138         style_res->baseline_shift.set      = old.set;
1139         style_res->baseline_shift.inherit  = old.inherit;
1140         style_res->baseline_shift.type     = old.type;
1141         style_res->baseline_shift.literal  = old.literal;
1142         style_res->baseline_shift.value    = old.value;
1143         style_res->baseline_shift.computed = old.computed;
1144     }
1146     if (texts == 0 || !set)
1147         return QUERY_STYLE_NOTHING;
1149     if (texts > 1) {
1150         if (different) {
1151             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1152         } else {
1153             return QUERY_STYLE_MULTIPLE_SAME;
1154         }
1155     } else {
1156         return QUERY_STYLE_SINGLE;
1157     }
1160 /**
1161  * Write to style_res the average font family of objects.
1162  */
1163 int
1164 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1166     bool different = false;
1167     int texts = 0;
1169     if (style_res->text->font_family.value) {
1170         g_free(style_res->text->font_family.value);
1171         style_res->text->font_family.value = NULL;
1172     }
1173     style_res->text->font_family.set = FALSE;
1175     for (GSList const *i = objects; i != NULL; i = i->next) {
1176         SPObject *obj = SP_OBJECT (i->data);
1178         // std::cout << "  " << SP_OBJECT_ID (i->data) << std::endl;
1179         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1180             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1181             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1182             continue;
1184         SPStyle *style = SP_OBJECT_STYLE (obj);
1185         if (!style) continue;
1187         texts ++;
1189         if (style_res->text->font_family.value && style->text->font_family.value &&
1190             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1191             different = true;  // different fonts
1192         }
1194         if (style_res->text->font_family.value) {
1195             g_free(style_res->text->font_family.value);
1196             style_res->text->font_family.value = NULL;
1197         }
1199         style_res->text->font_family.set = TRUE;
1200         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1201     }
1203     if (texts == 0 || !style_res->text->font_family.set)
1204         return QUERY_STYLE_NOTHING;
1206     if (texts > 1) {
1207         if (different) {
1208             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1209         } else {
1210             return QUERY_STYLE_MULTIPLE_SAME;
1211         }
1212     } else {
1213         return QUERY_STYLE_SINGLE;
1214     }
1217 int
1218 objects_query_fontspecification (GSList *objects, SPStyle *style_res)
1220     bool different = false;
1221     int texts = 0;
1223     if (style_res->text->font_specification.value) {
1224         g_free(style_res->text->font_specification.value);
1225         style_res->text->font_specification.value = NULL;
1226     }
1227     style_res->text->font_specification.set = FALSE;
1229     for (GSList const *i = objects; i != NULL; i = i->next) {
1230         SPObject *obj = SP_OBJECT (i->data);
1232         // std::cout << "  " << SP_OBJECT_ID (i->data) << std::endl;
1233         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1234             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1235             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1236             continue;
1238         SPStyle *style = SP_OBJECT_STYLE (obj);
1239         if (!style) continue;
1241         texts ++;
1243         if (style_res->text->font_specification.value && style_res->text->font_specification.set &&
1244             style->text->font_specification.value && style->text->font_specification.set &&
1245             strcmp (style_res->text->font_specification.value, style->text->font_specification.value)) {
1246             different = true;  // different fonts
1247         }
1249         if (style->text->font_specification.set) {
1251             if (style_res->text->font_specification.value) {
1252                 g_free(style_res->text->font_specification.value);
1253                 style_res->text->font_specification.value = NULL;
1254             }
1256             style_res->text->font_specification.set = TRUE;
1257             style_res->text->font_specification.value = g_strdup(style->text->font_specification.value);
1258         }
1259     }
1261     if (texts == 0)
1262         return QUERY_STYLE_NOTHING;
1264     if (texts > 1) {
1265         if (different) {
1266             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1267         } else {
1268             return QUERY_STYLE_MULTIPLE_SAME;
1269         }
1270     } else {
1271         return QUERY_STYLE_SINGLE;
1272     }
1275 int
1276 objects_query_blend (GSList *objects, SPStyle *style_res)
1278     const int empty_prev = -2;
1279     const int complex_filter = 5;
1280     int blend = 0;
1281     float blend_prev = empty_prev;
1282     bool same_blend = true;
1283     guint items = 0;
1285     for (GSList const *i = objects; i != NULL; i = i->next) {
1286         SPObject *obj = SP_OBJECT (i->data);
1287         SPStyle *style = SP_OBJECT_STYLE (obj);
1288         if(!style || !SP_IS_ITEM(obj)) continue;
1290         items++;
1292         //if object has a filter
1293         if (style->filter.set && style->getFilter()) {
1294             int blurcount = 0;
1295             int blendcount = 0;
1297             // determine whether filter is simple (blend and/or blur) or complex
1298             for(SPObject *primitive_obj = style->getFilter()->children;
1299                 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1300                 primitive_obj = primitive_obj->next) {
1301                 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1302                 if(SP_IS_FEBLEND(primitive))
1303                     ++blendcount;
1304                 else if(SP_IS_GAUSSIANBLUR(primitive))
1305                     ++blurcount;
1306                 else {
1307                     blurcount = complex_filter;
1308                     break;
1309                 }
1310             }
1312             // simple filter
1313             if(blurcount == 1 || blendcount == 1) {
1314                 for(SPObject *primitive_obj = style->getFilter()->children;
1315                     primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1316                     primitive_obj = primitive_obj->next) {
1317                     if(SP_IS_FEBLEND(primitive_obj)) {
1318                         SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1319                         blend = spblend->blend_mode;
1320                     }
1321                 }
1322             }
1323             else {
1324                 blend = complex_filter;
1325             }
1326         }
1327         // defaults to blend mode = "normal"
1328         else {
1329             blend = 0;
1330         }
1332         if(blend_prev != empty_prev && blend_prev != blend)
1333             same_blend = false;
1334         blend_prev = blend;
1335     }
1337     if (items > 0) {
1338         style_res->filter_blend_mode.value = blend;
1339     }
1341     if (items == 0) {
1342         return QUERY_STYLE_NOTHING;
1343     } else if (items == 1) {
1344         return QUERY_STYLE_SINGLE;
1345     } else {
1346         if(same_blend)
1347             return QUERY_STYLE_MULTIPLE_SAME;
1348         else
1349             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1350     }
1353 /**
1354  * Write to style_res the average blurring of a list of objects.
1355  */
1356 int
1357 objects_query_blur (GSList *objects, SPStyle *style_res)
1359    if (g_slist_length(objects) == 0) {
1360         /* No objects, set empty */
1361         return QUERY_STYLE_NOTHING;
1362     }
1364     float blur_sum = 0;
1365     float blur_prev = -1;
1366     bool same_blur = true;
1367     guint blur_items = 0;
1368     guint items = 0;
1370     for (GSList const *i = objects; i != NULL; i = i->next) {
1371         SPObject *obj = SP_OBJECT (i->data);
1372         SPStyle *style = SP_OBJECT_STYLE (obj);
1373         if (!style) continue;
1374         if (!SP_IS_ITEM(obj)) continue;
1376         Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1378         items ++;
1380         //if object has a filter
1381         if (style->filter.set && style->getFilter()) {
1382             //cycle through filter primitives
1383             SPObject *primitive_obj = style->getFilter()->children;
1384             while (primitive_obj) {
1385                 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1386                     SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1388                     //if primitive is gaussianblur
1389                     if(SP_IS_GAUSSIANBLUR(primitive)) {
1390                         SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1391                         float num = spblur->stdDeviation.getNumber();
1392                         blur_sum += num * i2d.descrim();
1393                         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
1394                             same_blur = false;
1395                         blur_prev = num;
1396                         //TODO: deal with opt number, for the moment it's not necessary to the ui.
1397                         blur_items ++;
1398                     }
1399                 }
1400                 primitive_obj = primitive_obj->next;
1401             }
1402         }
1403     }
1405     if (items > 0) {
1406         if (blur_items > 0)
1407             blur_sum /= blur_items;
1408         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1409     }
1411     if (items == 0) {
1412         return QUERY_STYLE_NOTHING;
1413     } else if (items == 1) {
1414         return QUERY_STYLE_SINGLE;
1415     } else {
1416         if (same_blur)
1417             return QUERY_STYLE_MULTIPLE_SAME;
1418         else
1419             return QUERY_STYLE_MULTIPLE_AVERAGED;
1420     }
1423 /**
1424  * Query the given list of objects for the given property, write
1425  * the result to style, return appropriate flag.
1426  */
1427 int
1428 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1430     if (property == QUERY_STYLE_PROPERTY_FILL) {
1431         return objects_query_fillstroke (list, style, true);
1432     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1433         return objects_query_fillstroke (list, style, false);
1435     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1436         return objects_query_strokewidth (list, style);
1437     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1438         return objects_query_miterlimit (list, style);
1439     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1440         return objects_query_strokecap (list, style);
1441     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1442         return objects_query_strokejoin (list, style);
1444     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1445         return objects_query_opacity (list, style);
1447     } else if (property == QUERY_STYLE_PROPERTY_FONT_SPECIFICATION) {
1448         return objects_query_fontspecification (list, style);
1449     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1450         return objects_query_fontfamily (list, style);
1451     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1452         return objects_query_fontstyle (list, style);
1453     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1454         return objects_query_fontnumbers (list, style);
1455     } else if (property == QUERY_STYLE_PROPERTY_BASELINES) {
1456         return objects_query_baselines (list, style);
1458     } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1459         return objects_query_blend (list, style);
1460     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1461         return objects_query_blur (list, style);
1462     }
1463     return QUERY_STYLE_NOTHING;
1467 /**
1468  * Query the subselection (if any) or selection on the given desktop for the given property, write
1469  * the result to style, return appropriate flag.
1470  */
1471 int
1472 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1474     int ret = desktop->_query_style_signal.emit(style, property);
1476     if (ret != QUERY_STYLE_NOTHING)
1477         return ret; // subselection returned a style, pass it on
1479     // otherwise, do querying and averaging over selection
1480     if (desktop->selection != NULL) {
1481         return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1482     }
1484     return QUERY_STYLE_NOTHING;
1487 /**
1488  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if at
1489  * least one of the properties did not return QUERY_STYLE_NOTHING.
1490  */
1491 bool
1492 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1494         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1495         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1496         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1497         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1498         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1499         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1500         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1501         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1502         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1503         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1504         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1506         return (result_family != QUERY_STYLE_NOTHING ||
1507                 result_fstyle != QUERY_STYLE_NOTHING ||
1508                 result_fnumbers != QUERY_STYLE_NOTHING ||
1509                 result_fill != QUERY_STYLE_NOTHING ||
1510                 result_stroke != QUERY_STYLE_NOTHING ||
1511                 result_opacity != QUERY_STYLE_NOTHING ||
1512                 result_strokewidth != QUERY_STYLE_NOTHING ||
1513                 result_strokemiterlimit != QUERY_STYLE_NOTHING ||
1514                 result_strokecap != QUERY_STYLE_NOTHING ||
1515                 result_strokejoin != QUERY_STYLE_NOTHING ||
1516                 result_blur != QUERY_STYLE_NOTHING);
1520 /*
1521   Local Variables:
1522   mode:c++
1523   c-file-style:"stroustrup"
1524   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1525   indent-tabs-mode:nil
1526   fill-column:99
1527   End:
1528 */
1529 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :