Code

use an accessor method to get filter from style
[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 "desktop.h"
16 #include "color-rgba.h"
17 #include "svg/css-ostringstream.h"
18 #include "svg/svg.h"
19 #include "svg/svg-color.h"
20 #include "selection.h"
21 #include "sp-tspan.h"
22 #include "sp-textpath.h"
23 #include "inkscape.h"
24 #include "style.h"
25 #include "prefs-utils.h"
26 #include "sp-use.h"
27 #include "sp-feblend.h"
28 #include "sp-filter.h"
29 #include "sp-filter-reference.h"
30 #include "sp-gaussian-blur.h"
31 #include "sp-flowtext.h"
32 #include "sp-flowregion.h"
33 #include "sp-flowdiv.h"
34 #include "sp-linear-gradient.h"
35 #include "sp-radial-gradient.h"
36 #include "sp-pattern.h"
37 #include "xml/repr.h"
38 #include "libnrtype/font-style-to-pos.h"
41 #include "desktop-style.h"
43 /**
44  * Set color on selection on desktop.
45  */
46 void
47 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
48 {
49     /// \todo relative color setting
50     if (is_relative) {
51         g_warning("FIXME: relative color setting not yet implemented");
52         return;
53     }
55     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
56     gchar b[64];
57     sp_svg_write_color(b, 64, rgba);
58     SPCSSAttr *css = sp_repr_css_attr_new();
59     if (fill) {
60         sp_repr_css_set_property(css, "fill", b);
61         Inkscape::CSSOStringStream osalpha;
62         osalpha << color[3];
63         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
64     } else {
65         sp_repr_css_set_property(css, "stroke", b);
66         Inkscape::CSSOStringStream osalpha;
67         osalpha << color[3];
68         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
69     }
71     sp_desktop_set_style(desktop, css);
73     sp_repr_css_attr_unref(css);
74 }
76 /**
77  * Apply style on object and children, recursively.
78  */
79 void
80 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
81 {
82     // non-items should not have style
83     if (!SP_IS_ITEM(o))
84         return;
86     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
87     // but must always inherit from the parent text. Same for textPath.
88     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
90     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
91     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
92     // flowtext).
94     if (!(skip_lines
95           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
96               || SP_IS_FLOWDIV(o)
97               || SP_IS_FLOWPARA(o)
98               || SP_IS_TEXTPATH(o))
99           && !SP_OBJECT_REPR(o)->attribute("style"))
100         &&
101         !(SP_IS_FLOWREGION(o) ||
102           SP_IS_FLOWREGIONEXCLUDE(o) ||
103           (SP_IS_USE(o) &&
104            SP_OBJECT_PARENT(o) &&
105            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
106             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
107            )
108           )
109          )
110         ) {
112         SPCSSAttr *css_set = sp_repr_css_attr_new();
113         sp_repr_css_merge(css_set, css);
115         // Scale the style by the inverse of the accumulated parent transform in the paste context.
116         {
117             NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
118             double const ex(NR::expansion(local));
119             if ( ( ex != 0. )
120                  && ( ex != 1. ) ) {
121                 sp_css_attr_scale(css_set, 1/ex);
122             }
123         }
125         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
127         sp_repr_css_attr_unref(css_set);
128     }
130     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
131     if (SP_IS_USE(o))
132         return;
134     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
135         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
136             // Unset properties which are accumulating and thus should not be set recursively.
137             // 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.
138             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
139             sp_repr_css_merge(css_recurse, css);
140             sp_repr_css_set_property(css_recurse, "opacity", NULL);
141             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
142             sp_repr_css_attr_unref(css_recurse);
143         } else {
144             sp_desktop_apply_css_recursive(child, css, skip_lines);
145         }
146     }
149 /**
150  * Apply style on selection on desktop.
151  */
152 void
153 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
155     if (write_current) {
156 // 1. Set internal value
157         sp_repr_css_merge(desktop->current, css);
159 // 1a. Write to prefs; make a copy and unset any URIs first
160         SPCSSAttr *css_write = sp_repr_css_attr_new();
161         sp_repr_css_merge(css_write, css);
162         sp_css_attr_unset_uris(css_write);
163         sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
164         sp_repr_css_attr_unref(css_write);
165     }
167     if (!change)
168         return;
170 // 2. Emit signal
171     bool intercepted = desktop->_set_style_signal.emit(css);
173 /** \todo
174  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
175  * rect corners, font size for the object's own transform so that pasting
176  * fills does not depend on preserve/optimize.
177  */
179 // 3. If nobody has intercepted the signal, apply the style to the selection
180     if (!intercepted) {
181         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
182             /// \todo if the style is text-only, apply only to texts?
183             sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
184         }
185     }
188 /**
189  * Return the desktop's current style.
190  */
191 SPCSSAttr *
192 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
194     SPCSSAttr *css = sp_repr_css_attr_new();
195     sp_repr_css_merge(css, desktop->current);
196     if (!css->attributeList()) {
197         sp_repr_css_attr_unref(css);
198         return NULL;
199     } else {
200         if (!with_text) {
201             css = sp_css_attr_unset_text(css);
202         }
203         return css;
204     }
207 /**
208  * Return the desktop's current color.
209  */
210 guint32
211 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
213     guint32 r = 0; // if there's no color, return black
214     gchar const *property = sp_repr_css_property(desktop->current,
215                                                  is_fill ? "fill" : "stroke",
216                                                  "#000");
218     if (desktop->current && property) { // if there is style and the property in it,
219         if (strncmp(property, "url", 3)) { // and if it's not url,
220             // read it
221             r = sp_svg_read_color(property, r);
222         }
223     }
225     return r;
228 double
229 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
231     SPCSSAttr *css = NULL;
232     gfloat value = 1.0; // default if nothing else found
233     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
234         css = sp_desktop_get_style(desktop, true);
235     } else { 
236         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
237         if (tool_repr) {
238             css = sp_repr_css_attr_inherited(tool_repr, "style");
239         }
240     }
241    
242     if (css) {
243         gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
244            
245         if (desktop->current && property) { // if there is style and the property in it,
246             if ( !sp_svg_number_read_f(property, &value) ) {
247                 value = 1.0; // things failed. set back to the default
248             }
249         }
251         sp_repr_css_attr_unref(css);
252     }
254     return value;
256 double
257 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
259     SPCSSAttr *css = NULL;
260     gfloat value = 1.0; // default if nothing else found
261     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
262         css = sp_desktop_get_style(desktop, true);
263     } else { 
264         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
265         if (tool_repr) {
266             css = sp_repr_css_attr_inherited(tool_repr, "style");
267         }
268     }
269    
270     if (css) {
271         gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
272            
273         if (desktop->current && property) { // if there is style and the property in it,
274             if ( !sp_svg_number_read_f(property, &value) ) {
275                 value = 1.0; // things failed. set back to the default
276             }
277         }
279         sp_repr_css_attr_unref(css);
280     }
282     return value;
284 guint32
285 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
287     SPCSSAttr *css = NULL;
288     guint32 r = 0; // if there's no color, return black
289     if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
290         css = sp_desktop_get_style(desktop, true);
291     } else {
292         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
293         if (tool_repr) {
294             css = sp_repr_css_attr_inherited(tool_repr, "style");
295         }
296     }
297    
298     if (css) {
299         gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
300            
301         if (desktop->current && property) { // if there is style and the property in it,
302             if (strncmp(property, "url", 3)) { // and if it's not url,
303                 // read it
304                 r = sp_svg_read_color(property, r);
305             }
306         }
308         sp_repr_css_attr_unref(css);
309     }
311     return r | 0xff;
313 /**
314  * Apply the desktop's current style or the tool style to repr.
315  */
316 void
317 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
319     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
320     if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
321         sp_repr_css_set(repr, css_current, "style");
322     } else {
323         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
324         if (tool_repr) {
325             SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
326             sp_repr_css_set(repr, css, "style");
327             sp_repr_css_attr_unref(css);
328         }
329     }
330     if (css_current) {
331         sp_repr_css_attr_unref(css_current);
332     }
335 /**
336  * Returns the font size (in SVG pixels) of the text tool style (if text
337  * tool uses its own style) or desktop style (otherwise).
338 */
339 double
340 sp_desktop_get_font_size_tool(SPDesktop *desktop)
342     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
343     gchar const *style_str = NULL;
344     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
345         style_str = desktop_style;
346     } else {
347         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
348         if (tool_repr) {
349             style_str = tool_repr->attribute("style");
350         }
351     }
353     double ret = 12;
354     if (style_str) {
355         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
356         sp_style_merge_from_style_string(style, style_str);
357         ret = style->font_size.computed;
358         sp_style_unref(style);
359     }
360     return ret;
363 /** Determine average stroke width, simple method */
364 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
365 gdouble
366 stroke_average_width (GSList const *objects)
368     if (g_slist_length ((GSList *) objects) == 0)
369         return NR_HUGE;
371     gdouble avgwidth = 0.0;
372     bool notstroked = true;
373     int n_notstroked = 0;
375     for (GSList const *l = objects; l != NULL; l = l->next) {
376         if (!SP_IS_ITEM (l->data))
377             continue;
379         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
381         SPObject *object = SP_OBJECT(l->data);
383         if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
384             ++n_notstroked;   // do not count nonstroked objects
385             continue;
386         } else {
387             notstroked = false;
388         }
390         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
391     }
393     if (notstroked)
394         return NR_HUGE;
396     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
399 /**
400  * Write to style_res the average fill or stroke of list of objects, if applicable.
401  */
402 int
403 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
405     if (g_slist_length(objects) == 0) {
406         /* No objects, set empty */
407         return QUERY_STYLE_NOTHING;
408     }
410     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
411     paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
412     paint_res->set = TRUE;
414     gfloat c[4];
415     c[0] = c[1] = c[2] = c[3] = 0.0;
416     gint num = 0;
418     gfloat prev[3];
419     prev[0] = prev[1] = prev[2] = 0.0;
420     bool same_color = true;
422     for (GSList const *i = objects; i != NULL; i = i->next) {
423         SPObject *obj = SP_OBJECT (i->data);
424         SPStyle *style = SP_OBJECT_STYLE (obj);
425         if (!style) continue;
427         SPIPaint *paint = isfill? &style->fill : &style->stroke;
429         // We consider paint "effectively set" for anything within text hierarchy
430         SPObject *parent = SP_OBJECT_PARENT (obj);
431         bool paint_effectively_set = paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent) || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent) || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
433         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
435         if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
436             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
437         }
439         if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
440             // both previous paint and this paint were a server, see if the servers are compatible
442             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
443             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
445             if (SP_IS_LINEARGRADIENT (server_res)) {
447                 if (!SP_IS_LINEARGRADIENT(server))
448                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
450                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
451                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
452                 if (vector_res != vector)
453                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
455             } else if (SP_IS_RADIALGRADIENT (server_res)) {
457                 if (!SP_IS_RADIALGRADIENT(server))
458                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
460                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
461                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
462                 if (vector_res != vector)
463                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
465             } else if (SP_IS_PATTERN (server_res)) {
467                 if (!SP_IS_PATTERN(server))
468                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
470                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
471                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
472                 if (pat_res != pat)
473                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
474             }
475         }
477         // 2. Sum color, copy server from paint to paint_res
479         if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
481             gfloat d[3];
482             sp_color_get_rgb_floatv (&paint->value.color, d);
484             // Check if this color is the same as previous
485             if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
486                 prev[0] = d[0];
487                 prev[1] = d[1];
488                 prev[2] = d[2];
489             } else {
490                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
491                     same_color = false;
492             }
494             // average color
495             c[0] += d[0];
496             c[1] += d[1];
497             c[2] += d[2];
498             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
499             num ++;
500         }
502        if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
503            if (isfill) {
504                SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
505            } else {
506                SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
507            }
508        }
509        paint_res->type = paint->type;
510        paint_res->set = paint_effectively_set;
511        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
512     }
514     // After all objects processed, divide the color if necessary and return
515     if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
516         g_assert (num >= 1);
518         c[0] /= num;
519         c[1] /= num;
520         c[2] /= num;
521         c[3] /= num;
522         sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
523         if (isfill) {
524             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
525         } else {
526             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
527         }
528         if (num > 1) {
529             if (same_color)
530                 return QUERY_STYLE_MULTIPLE_SAME;
531             else
532                 return QUERY_STYLE_MULTIPLE_AVERAGED;
533         } else {
534             return QUERY_STYLE_SINGLE;
535         }
536     }
538     // Not color
539     if (g_slist_length(objects) > 1) {
540         return QUERY_STYLE_MULTIPLE_SAME;
541     } else {
542         return QUERY_STYLE_SINGLE;
543     }
546 /**
547  * Write to style_res the average opacity of a list of objects.
548  */
549 int
550 objects_query_opacity (GSList *objects, SPStyle *style_res)
552     if (g_slist_length(objects) == 0) {
553         /* No objects, set empty */
554         return QUERY_STYLE_NOTHING;
555     }
557     gdouble opacity_sum = 0;
558     gdouble opacity_prev = -1;
559     bool same_opacity = true;
560     guint opacity_items = 0;
563     for (GSList const *i = objects; i != NULL; i = i->next) {
564         SPObject *obj = SP_OBJECT (i->data);
565         SPStyle *style = SP_OBJECT_STYLE (obj);
566         if (!style) continue;
568         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
569         opacity_sum += opacity;
570         if (opacity_prev != -1 && opacity != opacity_prev)
571             same_opacity = false;
572         opacity_prev = opacity;
573         opacity_items ++;
574     }
575     if (opacity_items > 1)
576         opacity_sum /= opacity_items;
578     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
580     if (opacity_items == 0) {
581         return QUERY_STYLE_NOTHING;
582     } else if (opacity_items == 1) {
583         return QUERY_STYLE_SINGLE;
584     } else {
585         if (same_opacity)
586             return QUERY_STYLE_MULTIPLE_SAME;
587         else
588             return QUERY_STYLE_MULTIPLE_AVERAGED;
589     }
592 /**
593  * Write to style_res the average stroke width of a list of objects.
594  */
595 int
596 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
598     if (g_slist_length(objects) == 0) {
599         /* No objects, set empty */
600         return QUERY_STYLE_NOTHING;
601     }
603     gdouble avgwidth = 0.0;
605     gdouble prev_sw = -1;
606     bool same_sw = true;
608     int n_stroked = 0;
610     for (GSList const *i = objects; i != NULL; i = i->next) {
611         SPObject *obj = SP_OBJECT (i->data);
612         if (!SP_IS_ITEM(obj)) continue;
613         SPStyle *style = SP_OBJECT_STYLE (obj);
614         if (!style) continue;
616         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
617             continue;
618         }
620         n_stroked ++;
622         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
623         double sw = style->stroke_width.computed * i2d.expansion();
625         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
626             same_sw = false;
627         prev_sw = sw;
629         avgwidth += sw;
630     }
632     if (n_stroked > 1)
633         avgwidth /= (n_stroked);
635     style_res->stroke_width.computed = avgwidth;
636     style_res->stroke_width.set = true;
638     if (n_stroked == 0) {
639         return QUERY_STYLE_NOTHING;
640     } else if (n_stroked == 1) {
641         return QUERY_STYLE_SINGLE;
642     } else {
643         if (same_sw)
644             return QUERY_STYLE_MULTIPLE_SAME;
645         else
646             return QUERY_STYLE_MULTIPLE_AVERAGED;
647     }
650 /**
651  * Write to style_res the average miter limit of a list of objects.
652  */
653 int
654 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
656     if (g_slist_length(objects) == 0) {
657         /* No objects, set empty */
658         return QUERY_STYLE_NOTHING;
659     }
661     gdouble avgml = 0.0;
662     int n_stroked = 0;
664     gdouble prev_ml = -1;
665     bool same_ml = true;
667     for (GSList const *i = objects; i != NULL; i = i->next) {
668         SPObject *obj = SP_OBJECT (i->data);
669         if (!SP_IS_ITEM(obj)) continue;
670         SPStyle *style = SP_OBJECT_STYLE (obj);
671         if (!style) continue;
673         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
674             continue;
675         }
677         n_stroked ++;
679         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
680             same_ml = false;
681         prev_ml = style->stroke_miterlimit.value;
683         avgml += style->stroke_miterlimit.value;
684     }
686     if (n_stroked > 1)
687         avgml /= (n_stroked);
689     style_res->stroke_miterlimit.value = avgml;
690     style_res->stroke_miterlimit.set = true;
692     if (n_stroked == 0) {
693         return QUERY_STYLE_NOTHING;
694     } else if (n_stroked == 1) {
695         return QUERY_STYLE_SINGLE;
696     } else {
697         if (same_ml)
698             return QUERY_STYLE_MULTIPLE_SAME;
699         else
700             return QUERY_STYLE_MULTIPLE_AVERAGED;
701     }
704 /**
705  * Write to style_res the stroke cap of a list of objects.
706  */
707 int
708 objects_query_strokecap (GSList *objects, SPStyle *style_res)
710     if (g_slist_length(objects) == 0) {
711         /* No objects, set empty */
712         return QUERY_STYLE_NOTHING;
713     }
715     int cap = -1;
716     gdouble prev_cap = -1;
717     bool same_cap = true;
718     int n_stroked = 0;
720     for (GSList const *i = objects; i != NULL; i = i->next) {
721         SPObject *obj = SP_OBJECT (i->data);
722         if (!SP_IS_ITEM(obj)) continue;
723         SPStyle *style = SP_OBJECT_STYLE (obj);
724         if (!style) continue;
726         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
727             continue;
728         }
730         n_stroked ++;
732         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
733             same_cap = false;
734         prev_cap = style->stroke_linecap.value;
736         cap = style->stroke_linecap.value;
737     }
739     style_res->stroke_linecap.value = cap;
740     style_res->stroke_linecap.set = true;
742     if (n_stroked == 0) {
743         return QUERY_STYLE_NOTHING;
744     } else if (n_stroked == 1) {
745         return QUERY_STYLE_SINGLE;
746     } else {
747         if (same_cap)
748             return QUERY_STYLE_MULTIPLE_SAME;
749         else
750             return QUERY_STYLE_MULTIPLE_DIFFERENT;
751     }
754 /**
755  * Write to style_res the stroke join of a list of objects.
756  */
757 int
758 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
760     if (g_slist_length(objects) == 0) {
761         /* No objects, set empty */
762         return QUERY_STYLE_NOTHING;
763     }
765     int join = -1;
766     gdouble prev_join = -1;
767     bool same_join = true;
768     int n_stroked = 0;
770     for (GSList const *i = objects; i != NULL; i = i->next) {
771         SPObject *obj = SP_OBJECT (i->data);
772         if (!SP_IS_ITEM(obj)) continue;
773         SPStyle *style = SP_OBJECT_STYLE (obj);
774         if (!style) continue;
776         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
777             continue;
778         }
780         n_stroked ++;
782         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
783             same_join = false;
784         prev_join = style->stroke_linejoin.value;
786         join = style->stroke_linejoin.value;
787     }
789     style_res->stroke_linejoin.value = join;
790     style_res->stroke_linejoin.set = true;
792     if (n_stroked == 0) {
793         return QUERY_STYLE_NOTHING;
794     } else if (n_stroked == 1) {
795         return QUERY_STYLE_SINGLE;
796     } else {
797         if (same_join)
798             return QUERY_STYLE_MULTIPLE_SAME;
799         else
800             return QUERY_STYLE_MULTIPLE_DIFFERENT;
801     }
804 /**
805  * Write to style_res the average font size and spacing of objects.
806  */
807 int
808 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
810     bool different = false;
812     double size = 0;
813     double letterspacing = 0;
814     double linespacing = 0;
815     bool linespacing_normal = false;
816     bool letterspacing_normal = false;
818     double size_prev = 0;
819     double letterspacing_prev = 0;
820     double linespacing_prev = 0;
822     /// \todo FIXME: add word spacing, kerns? rotates?
824     int texts = 0;
826     for (GSList const *i = objects; i != NULL; i = i->next) {
827         SPObject *obj = SP_OBJECT (i->data);
829         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
830             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
831             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
832             continue;
834         SPStyle *style = SP_OBJECT_STYLE (obj);
835         if (!style) continue;
837         texts ++;
838         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
840         if (style->letter_spacing.normal) {
841             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
842                 letterspacing_normal = true;
843         } else {
844             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
845             letterspacing_normal = false;
846         }
848         double linespacing_current;
849         if (style->line_height.normal) {
850             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
851             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
852                 linespacing_normal = true;
853         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
854             linespacing_current = style->line_height.value;
855             linespacing_normal = false;
856         } else { // we need % here
857             linespacing_current = style->line_height.computed / style->font_size.computed;
858             linespacing_normal = false;
859         }
860         linespacing += linespacing_current;
862         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
863             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
864             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
865             different = true;
866         }
868         size_prev = style->font_size.computed;
869         letterspacing_prev = style->letter_spacing.computed;
870         linespacing_prev = linespacing_current;
872         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
873         style_res->text_anchor.computed = style->text_anchor.computed;
874         style_res->writing_mode.computed = style->writing_mode.computed;
875     }
877     if (texts == 0)
878         return QUERY_STYLE_NOTHING;
880     if (texts > 1) {
881         size /= texts;
882         letterspacing /= texts;
883         linespacing /= texts;
884     }
886     style_res->font_size.computed = size;
887     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
889     style_res->letter_spacing.normal = letterspacing_normal;
890     style_res->letter_spacing.computed = letterspacing;
892     style_res->line_height.normal = linespacing_normal;
893     style_res->line_height.computed = linespacing;
894     style_res->line_height.value = linespacing;
895     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
897     if (texts > 1) {
898         if (different) {
899             return QUERY_STYLE_MULTIPLE_AVERAGED;
900         } else {
901             return QUERY_STYLE_MULTIPLE_SAME;
902         }
903     } else {
904         return QUERY_STYLE_SINGLE;
905     }
908 /**
909  * Write to style_res the average font style of objects.
910  */
911 int
912 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
914     bool different = false;
915     bool set = false;
917     int texts = 0;
919     for (GSList const *i = objects; i != NULL; i = i->next) {
920         SPObject *obj = SP_OBJECT (i->data);
922         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
923             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
924             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
925             continue;
927         SPStyle *style = SP_OBJECT_STYLE (obj);
928         if (!style) continue;
930         texts ++;
932         if (set &&
933             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
934             different = true;  // different styles
935         }
937         set = TRUE;
938         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
939         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
940         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
941         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
942         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
943     }
945     if (texts == 0 || !set)
946         return QUERY_STYLE_NOTHING;
948     if (texts > 1) {
949         if (different) {
950             return QUERY_STYLE_MULTIPLE_DIFFERENT;
951         } else {
952             return QUERY_STYLE_MULTIPLE_SAME;
953         }
954     } else {
955         return QUERY_STYLE_SINGLE;
956     }
959 /**
960  * Write to style_res the average font family of objects.
961  */
962 int
963 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
965     bool different = false;
966     int texts = 0;
968     if (style_res->text->font_family.value) {
969         g_free(style_res->text->font_family.value);
970         style_res->text->font_family.value = NULL;
971     }
972     style_res->text->font_family.set = FALSE;
974     for (GSList const *i = objects; i != NULL; i = i->next) {
975         SPObject *obj = SP_OBJECT (i->data);
977         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
978             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
979             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
980             continue;
982         SPStyle *style = SP_OBJECT_STYLE (obj);
983         if (!style) continue;
985         texts ++;
987         if (style_res->text->font_family.value && style->text->font_family.value &&
988             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
989             different = true;  // different fonts
990         }
992         if (style_res->text->font_family.value) {
993             g_free(style_res->text->font_family.value);
994             style_res->text->font_family.value = NULL;
995         }
997         style_res->text->font_family.set = TRUE;
998         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
999     }
1001     if (texts == 0 || !style_res->text->font_family.set)
1002         return QUERY_STYLE_NOTHING;
1004     if (texts > 1) {
1005         if (different) {
1006             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1007         } else {
1008             return QUERY_STYLE_MULTIPLE_SAME;
1009         }
1010     } else {
1011         return QUERY_STYLE_SINGLE;
1012     }
1015 int
1016 objects_query_blend (GSList *objects, SPStyle *style_res)
1018     const int empty_prev = -2;
1019     const int complex_filter = 5;
1020     int blend = 0;
1021     float blend_prev = empty_prev;
1022     bool same_blend = true;
1023     guint items = 0;
1024     
1025     for (GSList const *i = objects; i != NULL; i = i->next) {
1026         SPObject *obj = SP_OBJECT (i->data);
1027         SPStyle *style = SP_OBJECT_STYLE (obj);
1028         if(!style || !SP_IS_ITEM(obj)) continue;
1030         items++;
1032         //if object has a filter
1033         if (style->filter.set && style->getFilter()) {
1034             int blurcount = 0;
1035             int blendcount = 0;
1037             // determine whether filter is simple (blend and/or blur) or complex
1038             for(SPObject *primitive_obj = style->getFilter()->children;
1039                 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1040                 primitive_obj = primitive_obj->next) {
1041                 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1042                 if(SP_IS_FEBLEND(primitive))
1043                     ++blendcount;
1044                 else if(SP_IS_GAUSSIANBLUR(primitive))
1045                     ++blurcount;
1046                 else {
1047                     blurcount = complex_filter;
1048                     break;
1049                 }
1050             }
1052             // simple filter
1053             if(blurcount == 1 || blendcount == 1) {
1054                 for(SPObject *primitive_obj = style->getFilter()->children;
1055                     primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1056                     primitive_obj = primitive_obj->next) {
1057                     if(SP_IS_FEBLEND(primitive_obj)) {
1058                         SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1059                         blend = spblend->blend_mode;
1060                     }
1061                 }
1062             }
1063             else {
1064                 blend = complex_filter;
1065             }
1066         }
1067         // defaults to blend mode = "normal"
1068         else {
1069             blend = 0;
1070         }
1072         if(blend_prev != empty_prev && blend_prev != blend)
1073             same_blend = false;
1074         blend_prev = blend;
1075     }
1077     if (items > 0) {
1078         style_res->filter_blend_mode.value = blend;
1079     }
1081     if (items == 0) {
1082         return QUERY_STYLE_NOTHING;
1083     } else if (items == 1) {
1084         return QUERY_STYLE_SINGLE;
1085     } else {
1086         if(same_blend)
1087             return QUERY_STYLE_MULTIPLE_SAME;
1088         else
1089             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1090     }
1093 /**
1094  * Write to style_res the average blurring of a list of objects.
1095  */
1096 int
1097 objects_query_blur (GSList *objects, SPStyle *style_res)
1099    if (g_slist_length(objects) == 0) {
1100         /* No objects, set empty */
1101         return QUERY_STYLE_NOTHING;
1102     }
1104     float blur_sum = 0;
1105     float blur_prev = -1;
1106     bool same_blur = true;
1107     guint blur_items = 0;
1108     guint items = 0;
1109     
1110     for (GSList const *i = objects; i != NULL; i = i->next) {
1111         SPObject *obj = SP_OBJECT (i->data);
1112         SPStyle *style = SP_OBJECT_STYLE (obj);
1113         if (!style) continue;
1114         if (!SP_IS_ITEM(obj)) continue;
1116         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1118         items ++;
1120         //if object has a filter
1121         if (style->filter.set && style->getFilter()) {
1122             //cycle through filter primitives
1123             SPObject *primitive_obj = style->getFilter()->children;
1124             while (primitive_obj) {
1125                 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1126                     SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1127                     
1128                     //if primitive is gaussianblur
1129                     if(SP_IS_GAUSSIANBLUR(primitive)) {
1130                         SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1131                         float num = spblur->stdDeviation.getNumber();
1132                         blur_sum += num * NR::expansion(i2d);
1133                         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
1134                             same_blur = false;
1135                         blur_prev = num;
1136                         //TODO: deal with opt number, for the moment it's not necessary to the ui.
1137                         blur_items ++;
1138                     }
1139                 }
1140                 primitive_obj = primitive_obj->next;
1141             }
1142         }
1143     }
1145     if (items > 0) {
1146         if (blur_items > 0)
1147             blur_sum /= blur_items;
1148         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1149     }
1151     if (items == 0) {
1152         return QUERY_STYLE_NOTHING;
1153     } else if (items == 1) {
1154         return QUERY_STYLE_SINGLE;
1155     } else {
1156         if (same_blur)
1157             return QUERY_STYLE_MULTIPLE_SAME;
1158         else
1159             return QUERY_STYLE_MULTIPLE_AVERAGED;
1160     }
1163 /**
1164  * Query the given list of objects for the given property, write
1165  * the result to style, return appropriate flag.
1166  */
1167 int
1168 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1170     if (property == QUERY_STYLE_PROPERTY_FILL) {
1171         return objects_query_fillstroke (list, style, true);
1172     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1173         return objects_query_fillstroke (list, style, false);
1175     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1176         return objects_query_strokewidth (list, style);
1177     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1178         return objects_query_miterlimit (list, style);
1179     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1180         return objects_query_strokecap (list, style);
1181     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1182         return objects_query_strokejoin (list, style);
1184     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1185         return objects_query_opacity (list, style);
1187     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1188         return objects_query_fontfamily (list, style);
1189     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1190         return objects_query_fontstyle (list, style);
1191     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1192         return objects_query_fontnumbers (list, style);
1194     } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1195         return objects_query_blend (list, style);
1196     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1197         return objects_query_blur (list, style);
1198     }
1199     return QUERY_STYLE_NOTHING;
1203 /**
1204  * Query the subselection (if any) or selection on the given desktop for the given property, write
1205  * the result to style, return appropriate flag.
1206  */
1207 int
1208 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1210     int ret = desktop->_query_style_signal.emit(style, property);
1212     if (ret != QUERY_STYLE_NOTHING)
1213         return ret; // subselection returned a style, pass it on
1215     // otherwise, do querying and averaging over selection
1216     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1219 /**
1220  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1221  * the properties returned QUERY_STYLE_NOTHING.
1222  */
1223 bool
1224 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1226         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1227         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1228         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1229         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1230         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1231         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1232         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1233         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1234         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1235         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1236         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1237         
1238         return (result_family != QUERY_STYLE_NOTHING && result_fstyle != QUERY_STYLE_NOTHING && result_fnumbers != QUERY_STYLE_NOTHING && result_fill != QUERY_STYLE_NOTHING && result_stroke != QUERY_STYLE_NOTHING && result_opacity != QUERY_STYLE_NOTHING && result_strokewidth != QUERY_STYLE_NOTHING && result_strokemiterlimit != QUERY_STYLE_NOTHING && result_strokecap != QUERY_STYLE_NOTHING && result_strokejoin != QUERY_STYLE_NOTHING && result_blur != QUERY_STYLE_NOTHING);
1242 /*
1243   Local Variables:
1244   mode:c++
1245   c-file-style:"stroustrup"
1246   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1247   indent-tabs-mode:nil
1248   fill-column:99
1249   End:
1250 */
1251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :