Code

b1dca7f17e209a7faa8a4f668812c6c6610c4b52
[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 "inkscape.h"
22 #include "style.h"
23 #include "prefs-utils.h"
24 #include "sp-use.h"
25 #include "sp-feblend.h"
26 #include "sp-filter.h"
27 #include "sp-filter-reference.h"
28 #include "sp-gaussian-blur.h"
29 #include "sp-flowtext.h"
30 #include "sp-flowregion.h"
31 #include "sp-flowdiv.h"
32 #include "sp-linear-gradient.h"
33 #include "sp-pattern.h"
34 #include "sp-radial-gradient.h"
35 #include "sp-textpath.h"
36 #include "sp-tref.h"
37 #include "sp-tspan.h"
38 #include "xml/repr.h"
39 #include "libnrtype/font-style-to-pos.h"
40 #include "sp-path.h"
42 #include "desktop-style.h"
44 /**
45  * Set color on selection on desktop.
46  */
47 void
48 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
49 {
50     /// \todo relative color setting
51     if (is_relative) {
52         g_warning("FIXME: relative color setting not yet implemented");
53         return;
54     }
56     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
57     gchar b[64];
58     sp_svg_write_color(b, 64, rgba);
59     SPCSSAttr *css = sp_repr_css_attr_new();
60     if (fill) {
61         sp_repr_css_set_property(css, "fill", b);
62         Inkscape::CSSOStringStream osalpha;
63         osalpha << color[3];
64         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
65     } else {
66         sp_repr_css_set_property(css, "stroke", b);
67         Inkscape::CSSOStringStream osalpha;
68         osalpha << color[3];
69         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
70     }
72     sp_desktop_set_style(desktop, css);
74     sp_repr_css_attr_unref(css);
75 }
77 /**
78  * Apply style on object and children, recursively.
79  */
80 void
81 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
82 {
83     // non-items should not have style
84     if (!SP_IS_ITEM(o))
85         return;
87     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
88     // but must always inherit from the parent text. Same for textPath.
89     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
91     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
92     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
93     // flowtext).
95     if (!(skip_lines
96           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
97               || SP_IS_FLOWDIV(o)
98               || SP_IS_FLOWPARA(o)
99               || SP_IS_TEXTPATH(o))
100           && !SP_OBJECT_REPR(o)->attribute("style"))
101         &&
102         !(SP_IS_FLOWREGION(o) ||
103           SP_IS_FLOWREGIONEXCLUDE(o) ||
104           (SP_IS_USE(o) &&
105            SP_OBJECT_PARENT(o) &&
106            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
107             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
108            )
109           )
110          )
111         ) {
113         SPCSSAttr *css_set = sp_repr_css_attr_new();
114         sp_repr_css_merge(css_set, css);
116         // Scale the style by the inverse of the accumulated parent transform in the paste context.
117         {
118             NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
119             double const ex(NR::expansion(local));
120             if ( ( ex != 0. )
121                  && ( ex != 1. ) ) {
122                 sp_css_attr_scale(css_set, 1/ex);
123             }
124         }
126         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
128         sp_repr_css_attr_unref(css_set);
129     }
131     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
132     if (SP_IS_USE(o))
133         return;
135     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
136         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
137             // Unset properties which are accumulating and thus should not be set recursively.
138             // 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.
139             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
140             sp_repr_css_merge(css_recurse, css);
141             sp_repr_css_set_property(css_recurse, "opacity", NULL);
142             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
143             sp_repr_css_attr_unref(css_recurse);
144         } else {
145             sp_desktop_apply_css_recursive(child, css, skip_lines);
146         }
147     }
150 /**
151  * Apply style on selection on desktop.
152  */
153 void
154 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
156     if (write_current) {
157 // 1. Set internal value
158         sp_repr_css_merge(desktop->current, css);
160 // 1a. Write to prefs; make a copy and unset any URIs first
161         SPCSSAttr *css_write = sp_repr_css_attr_new();
162         sp_repr_css_merge(css_write, css);
163         sp_css_attr_unset_uris(css_write);
164         sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
165         for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
166             /* last used styles for 3D box faces are stored separately */
167             if (SP_IS_PATH (i->data)) {
168                 const char * descr  = SP_OBJECT_REPR (G_OBJECT (i->data))->attribute ("inkscape:box3dface");
169                 if (descr != NULL) {
170                     gchar *style_grp = g_strconcat ("desktop.", descr, NULL);
171                     sp_repr_css_change(inkscape_get_repr(INKSCAPE, style_grp), css_write, "style");
172                     g_free (style_grp);
173                 }
174             }
175         }
176         sp_repr_css_attr_unref(css_write);
177     }
179     if (!change)
180         return;
182 // 2. Emit signal
183     bool intercepted = desktop->_set_style_signal.emit(css);
185 /** \todo
186  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
187  * rect corners, font size for the object's own transform so that pasting
188  * fills does not depend on preserve/optimize.
189  */
191 // 3. If nobody has intercepted the signal, apply the style to the selection
192     if (!intercepted) {
193         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
194             /// \todo if the style is text-only, apply only to texts?
195             sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
196         }
197     }
200 /**
201  * Return the desktop's current style.
202  */
203 SPCSSAttr *
204 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
206     SPCSSAttr *css = sp_repr_css_attr_new();
207     sp_repr_css_merge(css, desktop->current);
208     if (!css->attributeList()) {
209         sp_repr_css_attr_unref(css);
210         return NULL;
211     } else {
212         if (!with_text) {
213             css = sp_css_attr_unset_text(css);
214         }
215         return css;
216     }
219 /**
220  * Return the desktop's current color.
221  */
222 guint32
223 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
225     guint32 r = 0; // if there's no color, return black
226     gchar const *property = sp_repr_css_property(desktop->current,
227                                                  is_fill ? "fill" : "stroke",
228                                                  "#000");
230     if (desktop->current && property) { // if there is style and the property in it,
231         if (strncmp(property, "url", 3)) { // and if it's not url,
232             // read it
233             r = sp_svg_read_color(property, r);
234         }
235     }
237     return r;
240 double
241 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
243     SPCSSAttr *css = NULL;
244     gfloat value = 1.0; // default if nothing else found
245     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
246         css = sp_desktop_get_style(desktop, true);
247     } else { 
248         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
249         if (tool_repr) {
250             css = sp_repr_css_attr_inherited(tool_repr, "style");
251         }
252     }
253    
254     if (css) {
255         gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
256            
257         if (desktop->current && property) { // if there is style and the property in it,
258             if ( !sp_svg_number_read_f(property, &value) ) {
259                 value = 1.0; // things failed. set back to the default
260             }
261         }
263         sp_repr_css_attr_unref(css);
264     }
266     return value;
268 double
269 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
271     SPCSSAttr *css = NULL;
272     gfloat value = 1.0; // default if nothing else found
273     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
274         css = sp_desktop_get_style(desktop, true);
275     } else { 
276         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
277         if (tool_repr) {
278             css = sp_repr_css_attr_inherited(tool_repr, "style");
279         }
280     }
281    
282     if (css) {
283         gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
284            
285         if (desktop->current && property) { // if there is style and the property in it,
286             if ( !sp_svg_number_read_f(property, &value) ) {
287                 value = 1.0; // things failed. set back to the default
288             }
289         }
291         sp_repr_css_attr_unref(css);
292     }
294     return value;
296 guint32
297 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
299     SPCSSAttr *css = NULL;
300     guint32 r = 0; // if there's no color, return black
301     if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
302         css = sp_desktop_get_style(desktop, true);
303     } else {
304         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
305         if (tool_repr) {
306             css = sp_repr_css_attr_inherited(tool_repr, "style");
307         }
308     }
309    
310     if (css) {
311         gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
312            
313         if (desktop->current && property) { // if there is style and the property in it,
314             if (strncmp(property, "url", 3)) { // and if it's not url,
315                 // read it
316                 r = sp_svg_read_color(property, r);
317             }
318         }
320         sp_repr_css_attr_unref(css);
321     }
323     return r | 0xff;
325 /**
326  * Apply the desktop's current style or the tool style to repr.
327  */
328 void
329 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
331     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
332     if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
333         sp_repr_css_set(repr, css_current, "style");
334     } else {
335         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
336         if (tool_repr) {
337             SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
338             sp_repr_css_set(repr, css, "style");
339             sp_repr_css_attr_unref(css);
340         }
341     }
342     if (css_current) {
343         sp_repr_css_attr_unref(css_current);
344     }
347 /**
348  * Returns the font size (in SVG pixels) of the text tool style (if text
349  * tool uses its own style) or desktop style (otherwise).
350 */
351 double
352 sp_desktop_get_font_size_tool(SPDesktop *desktop)
354     (void)desktop; // TODO cleanup
355     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
356     gchar const *style_str = NULL;
357     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
358         style_str = desktop_style;
359     } else {
360         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
361         if (tool_repr) {
362             style_str = tool_repr->attribute("style");
363         }
364     }
366     double ret = 12;
367     if (style_str) {
368         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
369         sp_style_merge_from_style_string(style, style_str);
370         ret = style->font_size.computed;
371         sp_style_unref(style);
372     }
373     return ret;
376 /** Determine average stroke width, simple method */
377 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
378 gdouble
379 stroke_average_width (GSList const *objects)
381     if (g_slist_length ((GSList *) objects) == 0)
382         return NR_HUGE;
384     gdouble avgwidth = 0.0;
385     bool notstroked = true;
386     int n_notstroked = 0;
388     for (GSList const *l = objects; l != NULL; l = l->next) {
389         if (!SP_IS_ITEM (l->data))
390             continue;
392         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
394         SPObject *object = SP_OBJECT(l->data);
396         if ( object->style->stroke.isNone() ) {
397             ++n_notstroked;   // do not count nonstroked objects
398             continue;
399         } else {
400             notstroked = false;
401         }
403         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
404     }
406     if (notstroked)
407         return NR_HUGE;
409     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
412 /**
413  * Write to style_res the average fill or stroke of list of objects, if applicable.
414  */
415 int
416 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
418     if (g_slist_length(objects) == 0) {
419         /* No objects, set empty */
420         return QUERY_STYLE_NOTHING;
421     }
423     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
424     bool paintImpossible = true;
425     paint_res->set = TRUE;
427     gfloat c[4];
428     c[0] = c[1] = c[2] = c[3] = 0.0;
429     gint num = 0;
431     gfloat prev[3];
432     prev[0] = prev[1] = prev[2] = 0.0;
433     bool same_color = true;
435     for (GSList const *i = objects; i != NULL; i = i->next) {
436         SPObject *obj = SP_OBJECT (i->data);
437         SPStyle *style = SP_OBJECT_STYLE (obj);
438         if (!style) continue;
440         SPIPaint *paint = isfill? &style->fill : &style->stroke;
442         // We consider paint "effectively set" for anything within text hierarchy
443         SPObject *parent = SP_OBJECT_PARENT (obj);
444         bool paint_effectively_set =
445             paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
446             || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
447             || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
449         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
451         if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
452             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
453         }
455         if (paint_res->set && paint->set && paint_res->isPaintserver()) {
456             // both previous paint and this paint were a server, see if the servers are compatible
458             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
459             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
461             if (SP_IS_LINEARGRADIENT (server_res)) {
463                 if (!SP_IS_LINEARGRADIENT(server))
464                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
466                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
467                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
468                 if (vector_res != vector)
469                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
471             } else if (SP_IS_RADIALGRADIENT (server_res)) {
473                 if (!SP_IS_RADIALGRADIENT(server))
474                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
476                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
477                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
478                 if (vector_res != vector)
479                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
481             } else if (SP_IS_PATTERN (server_res)) {
483                 if (!SP_IS_PATTERN(server))
484                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
486                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
487                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
488                 if (pat_res != pat)
489                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
490             }
491         }
493         // 2. Sum color, copy server from paint to paint_res
495         if (paint_res->set && paint_effectively_set && paint->isColor()) {
496             gfloat d[3];
497             sp_color_get_rgb_floatv (&paint->value.color, d);
499             // Check if this color is the same as previous
500             if (paintImpossible) {
501                 prev[0] = d[0];
502                 prev[1] = d[1];
503                 prev[2] = d[2];
504                 paint_res->setColor(d[0], d[1], d[2]);
505             } else {
506                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
507                     same_color = false;
508             }
510             // average color
511             c[0] += d[0];
512             c[1] += d[1];
513             c[2] += d[2];
514             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
515             num ++;
516         }
518        paintImpossible = false;
519        paint_res->colorSet = paint->colorSet;
520        paint_res->currentcolor = paint->currentcolor;
521        if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
522            if (isfill) {
523                sp_style_set_to_uri_string (style_res, true, style->getFillURI());
524            } else {
525                sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
526            }
527        }
528        paint_res->set = paint_effectively_set;
529        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
530     }
532     // After all objects processed, divide the color if necessary and return
533     if (paint_res->set && paint_res->isColor()) { // set the color
534         g_assert (num >= 1);
536         c[0] /= num;
537         c[1] /= num;
538         c[2] /= num;
539         c[3] /= num;
540         paint_res->setColor(c[0], c[1], c[2]);
541         if (isfill) {
542             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
543         } else {
544             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
545         }
546         if (num > 1) {
547             if (same_color)
548                 return QUERY_STYLE_MULTIPLE_SAME;
549             else
550                 return QUERY_STYLE_MULTIPLE_AVERAGED;
551         } else {
552             return QUERY_STYLE_SINGLE;
553         }
554     }
556     // Not color
557     if (g_slist_length(objects) > 1) {
558         return QUERY_STYLE_MULTIPLE_SAME;
559     } else {
560         return QUERY_STYLE_SINGLE;
561     }
564 /**
565  * Write to style_res the average opacity of a list of objects.
566  */
567 int
568 objects_query_opacity (GSList *objects, SPStyle *style_res)
570     if (g_slist_length(objects) == 0) {
571         /* No objects, set empty */
572         return QUERY_STYLE_NOTHING;
573     }
575     gdouble opacity_sum = 0;
576     gdouble opacity_prev = -1;
577     bool same_opacity = true;
578     guint opacity_items = 0;
581     for (GSList const *i = objects; i != NULL; i = i->next) {
582         SPObject *obj = SP_OBJECT (i->data);
583         SPStyle *style = SP_OBJECT_STYLE (obj);
584         if (!style) continue;
586         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
587         opacity_sum += opacity;
588         if (opacity_prev != -1 && opacity != opacity_prev)
589             same_opacity = false;
590         opacity_prev = opacity;
591         opacity_items ++;
592     }
593     if (opacity_items > 1)
594         opacity_sum /= opacity_items;
596     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
598     if (opacity_items == 0) {
599         return QUERY_STYLE_NOTHING;
600     } else if (opacity_items == 1) {
601         return QUERY_STYLE_SINGLE;
602     } else {
603         if (same_opacity)
604             return QUERY_STYLE_MULTIPLE_SAME;
605         else
606             return QUERY_STYLE_MULTIPLE_AVERAGED;
607     }
610 /**
611  * Write to style_res the average stroke width of a list of objects.
612  */
613 int
614 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
616     if (g_slist_length(objects) == 0) {
617         /* No objects, set empty */
618         return QUERY_STYLE_NOTHING;
619     }
621     gdouble avgwidth = 0.0;
623     gdouble prev_sw = -1;
624     bool same_sw = true;
626     int n_stroked = 0;
628     for (GSList const *i = objects; i != NULL; i = i->next) {
629         SPObject *obj = SP_OBJECT (i->data);
630         if (!SP_IS_ITEM(obj)) continue;
631         SPStyle *style = SP_OBJECT_STYLE (obj);
632         if (!style) continue;
634         if ( style->stroke.isNone() ) {
635             continue;
636         }
638         n_stroked ++;
640         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
641         double sw = style->stroke_width.computed * i2d.expansion();
643         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
644             same_sw = false;
645         prev_sw = sw;
647         avgwidth += sw;
648     }
650     if (n_stroked > 1)
651         avgwidth /= (n_stroked);
653     style_res->stroke_width.computed = avgwidth;
654     style_res->stroke_width.set = true;
656     if (n_stroked == 0) {
657         return QUERY_STYLE_NOTHING;
658     } else if (n_stroked == 1) {
659         return QUERY_STYLE_SINGLE;
660     } else {
661         if (same_sw)
662             return QUERY_STYLE_MULTIPLE_SAME;
663         else
664             return QUERY_STYLE_MULTIPLE_AVERAGED;
665     }
668 /**
669  * Write to style_res the average miter limit of a list of objects.
670  */
671 int
672 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
674     if (g_slist_length(objects) == 0) {
675         /* No objects, set empty */
676         return QUERY_STYLE_NOTHING;
677     }
679     gdouble avgml = 0.0;
680     int n_stroked = 0;
682     gdouble prev_ml = -1;
683     bool same_ml = true;
685     for (GSList const *i = objects; i != NULL; i = i->next) {
686         SPObject *obj = SP_OBJECT (i->data);
687         if (!SP_IS_ITEM(obj)) continue;
688         SPStyle *style = SP_OBJECT_STYLE (obj);
689         if (!style) continue;
691         if ( style->stroke.isNone() ) {
692             continue;
693         }
695         n_stroked ++;
697         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
698             same_ml = false;
699         prev_ml = style->stroke_miterlimit.value;
701         avgml += style->stroke_miterlimit.value;
702     }
704     if (n_stroked > 1)
705         avgml /= (n_stroked);
707     style_res->stroke_miterlimit.value = avgml;
708     style_res->stroke_miterlimit.set = true;
710     if (n_stroked == 0) {
711         return QUERY_STYLE_NOTHING;
712     } else if (n_stroked == 1) {
713         return QUERY_STYLE_SINGLE;
714     } else {
715         if (same_ml)
716             return QUERY_STYLE_MULTIPLE_SAME;
717         else
718             return QUERY_STYLE_MULTIPLE_AVERAGED;
719     }
722 /**
723  * Write to style_res the stroke cap of a list of objects.
724  */
725 int
726 objects_query_strokecap (GSList *objects, SPStyle *style_res)
728     if (g_slist_length(objects) == 0) {
729         /* No objects, set empty */
730         return QUERY_STYLE_NOTHING;
731     }
733     int cap = -1;
734     gdouble prev_cap = -1;
735     bool same_cap = true;
736     int n_stroked = 0;
738     for (GSList const *i = objects; i != NULL; i = i->next) {
739         SPObject *obj = SP_OBJECT (i->data);
740         if (!SP_IS_ITEM(obj)) continue;
741         SPStyle *style = SP_OBJECT_STYLE (obj);
742         if (!style) continue;
744         if ( style->stroke.isNone() ) {
745             continue;
746         }
748         n_stroked ++;
750         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
751             same_cap = false;
752         prev_cap = style->stroke_linecap.value;
754         cap = style->stroke_linecap.value;
755     }
757     style_res->stroke_linecap.value = cap;
758     style_res->stroke_linecap.set = true;
760     if (n_stroked == 0) {
761         return QUERY_STYLE_NOTHING;
762     } else if (n_stroked == 1) {
763         return QUERY_STYLE_SINGLE;
764     } else {
765         if (same_cap)
766             return QUERY_STYLE_MULTIPLE_SAME;
767         else
768             return QUERY_STYLE_MULTIPLE_DIFFERENT;
769     }
772 /**
773  * Write to style_res the stroke join of a list of objects.
774  */
775 int
776 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
778     if (g_slist_length(objects) == 0) {
779         /* No objects, set empty */
780         return QUERY_STYLE_NOTHING;
781     }
783     int join = -1;
784     gdouble prev_join = -1;
785     bool same_join = true;
786     int n_stroked = 0;
788     for (GSList const *i = objects; i != NULL; i = i->next) {
789         SPObject *obj = SP_OBJECT (i->data);
790         if (!SP_IS_ITEM(obj)) continue;
791         SPStyle *style = SP_OBJECT_STYLE (obj);
792         if (!style) continue;
794         if ( style->stroke.isNone() ) {
795             continue;
796         }
798         n_stroked ++;
800         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
801             same_join = false;
802         prev_join = style->stroke_linejoin.value;
804         join = style->stroke_linejoin.value;
805     }
807     style_res->stroke_linejoin.value = join;
808     style_res->stroke_linejoin.set = true;
810     if (n_stroked == 0) {
811         return QUERY_STYLE_NOTHING;
812     } else if (n_stroked == 1) {
813         return QUERY_STYLE_SINGLE;
814     } else {
815         if (same_join)
816             return QUERY_STYLE_MULTIPLE_SAME;
817         else
818             return QUERY_STYLE_MULTIPLE_DIFFERENT;
819     }
822 /**
823  * Write to style_res the average font size and spacing of objects.
824  */
825 int
826 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
828     bool different = false;
830     double size = 0;
831     double letterspacing = 0;
832     double linespacing = 0;
833     bool linespacing_normal = false;
834     bool letterspacing_normal = false;
836     double size_prev = 0;
837     double letterspacing_prev = 0;
838     double linespacing_prev = 0;
840     /// \todo FIXME: add word spacing, kerns? rotates?
842     int texts = 0;
844     for (GSList const *i = objects; i != NULL; i = i->next) {
845         SPObject *obj = SP_OBJECT (i->data);
847         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
848             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
849             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
850             continue;
852         SPStyle *style = SP_OBJECT_STYLE (obj);
853         if (!style) continue;
855         texts ++;
856         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
858         if (style->letter_spacing.normal) {
859             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
860                 letterspacing_normal = true;
861         } else {
862             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
863             letterspacing_normal = false;
864         }
866         double linespacing_current;
867         if (style->line_height.normal) {
868             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
869             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
870                 linespacing_normal = true;
871         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
872             linespacing_current = style->line_height.value;
873             linespacing_normal = false;
874         } else { // we need % here
875             linespacing_current = style->line_height.computed / style->font_size.computed;
876             linespacing_normal = false;
877         }
878         linespacing += linespacing_current;
880         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
881             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
882             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
883             different = true;
884         }
886         size_prev = style->font_size.computed;
887         letterspacing_prev = style->letter_spacing.computed;
888         linespacing_prev = linespacing_current;
890         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
891         style_res->text_anchor.computed = style->text_anchor.computed;
892         style_res->writing_mode.computed = style->writing_mode.computed;
893     }
895     if (texts == 0)
896         return QUERY_STYLE_NOTHING;
898     if (texts > 1) {
899         size /= texts;
900         letterspacing /= texts;
901         linespacing /= texts;
902     }
904     style_res->font_size.computed = size;
905     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
907     style_res->letter_spacing.normal = letterspacing_normal;
908     style_res->letter_spacing.computed = letterspacing;
910     style_res->line_height.normal = linespacing_normal;
911     style_res->line_height.computed = linespacing;
912     style_res->line_height.value = linespacing;
913     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
915     if (texts > 1) {
916         if (different) {
917             return QUERY_STYLE_MULTIPLE_AVERAGED;
918         } else {
919             return QUERY_STYLE_MULTIPLE_SAME;
920         }
921     } else {
922         return QUERY_STYLE_SINGLE;
923     }
926 /**
927  * Write to style_res the average font style of objects.
928  */
929 int
930 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
932     bool different = false;
933     bool set = false;
935     int texts = 0;
937     for (GSList const *i = objects; i != NULL; i = i->next) {
938         SPObject *obj = SP_OBJECT (i->data);
940         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
941             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
942             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
943             continue;
945         SPStyle *style = SP_OBJECT_STYLE (obj);
946         if (!style) continue;
948         texts ++;
950         if (set &&
951             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
952             different = true;  // different styles
953         }
955         set = TRUE;
956         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
957         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
958         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
959         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
960         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
961     }
963     if (texts == 0 || !set)
964         return QUERY_STYLE_NOTHING;
966     if (texts > 1) {
967         if (different) {
968             return QUERY_STYLE_MULTIPLE_DIFFERENT;
969         } else {
970             return QUERY_STYLE_MULTIPLE_SAME;
971         }
972     } else {
973         return QUERY_STYLE_SINGLE;
974     }
977 /**
978  * Write to style_res the average font family of objects.
979  */
980 int
981 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
983     bool different = false;
984     int texts = 0;
986     if (style_res->text->font_family.value) {
987         g_free(style_res->text->font_family.value);
988         style_res->text->font_family.value = NULL;
989     }
990     style_res->text->font_family.set = FALSE;
992     for (GSList const *i = objects; i != NULL; i = i->next) {
993         SPObject *obj = SP_OBJECT (i->data);
995         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
996             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
997             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
998             continue;
1000         SPStyle *style = SP_OBJECT_STYLE (obj);
1001         if (!style) continue;
1003         texts ++;
1005         if (style_res->text->font_family.value && style->text->font_family.value &&
1006             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1007             different = true;  // different fonts
1008         }
1010         if (style_res->text->font_family.value) {
1011             g_free(style_res->text->font_family.value);
1012             style_res->text->font_family.value = NULL;
1013         }
1015         style_res->text->font_family.set = TRUE;
1016         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1017     }
1019     if (texts == 0 || !style_res->text->font_family.set)
1020         return QUERY_STYLE_NOTHING;
1022     if (texts > 1) {
1023         if (different) {
1024             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1025         } else {
1026             return QUERY_STYLE_MULTIPLE_SAME;
1027         }
1028     } else {
1029         return QUERY_STYLE_SINGLE;
1030     }
1033 int
1034 objects_query_blend (GSList *objects, SPStyle *style_res)
1036     const int empty_prev = -2;
1037     const int complex_filter = 5;
1038     int blend = 0;
1039     float blend_prev = empty_prev;
1040     bool same_blend = true;
1041     guint items = 0;
1042     
1043     for (GSList const *i = objects; i != NULL; i = i->next) {
1044         SPObject *obj = SP_OBJECT (i->data);
1045         SPStyle *style = SP_OBJECT_STYLE (obj);
1046         if(!style || !SP_IS_ITEM(obj)) continue;
1048         items++;
1050         //if object has a filter
1051         if (style->filter.set && style->getFilter()) {
1052             int blurcount = 0;
1053             int blendcount = 0;
1055             // determine whether filter is simple (blend and/or blur) or complex
1056             for(SPObject *primitive_obj = style->getFilter()->children;
1057                 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1058                 primitive_obj = primitive_obj->next) {
1059                 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1060                 if(SP_IS_FEBLEND(primitive))
1061                     ++blendcount;
1062                 else if(SP_IS_GAUSSIANBLUR(primitive))
1063                     ++blurcount;
1064                 else {
1065                     blurcount = complex_filter;
1066                     break;
1067                 }
1068             }
1070             // simple filter
1071             if(blurcount == 1 || blendcount == 1) {
1072                 for(SPObject *primitive_obj = style->getFilter()->children;
1073                     primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1074                     primitive_obj = primitive_obj->next) {
1075                     if(SP_IS_FEBLEND(primitive_obj)) {
1076                         SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1077                         blend = spblend->blend_mode;
1078                     }
1079                 }
1080             }
1081             else {
1082                 blend = complex_filter;
1083             }
1084         }
1085         // defaults to blend mode = "normal"
1086         else {
1087             blend = 0;
1088         }
1090         if(blend_prev != empty_prev && blend_prev != blend)
1091             same_blend = false;
1092         blend_prev = blend;
1093     }
1095     if (items > 0) {
1096         style_res->filter_blend_mode.value = blend;
1097     }
1099     if (items == 0) {
1100         return QUERY_STYLE_NOTHING;
1101     } else if (items == 1) {
1102         return QUERY_STYLE_SINGLE;
1103     } else {
1104         if(same_blend)
1105             return QUERY_STYLE_MULTIPLE_SAME;
1106         else
1107             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1108     }
1111 /**
1112  * Write to style_res the average blurring of a list of objects.
1113  */
1114 int
1115 objects_query_blur (GSList *objects, SPStyle *style_res)
1117    if (g_slist_length(objects) == 0) {
1118         /* No objects, set empty */
1119         return QUERY_STYLE_NOTHING;
1120     }
1122     float blur_sum = 0;
1123     float blur_prev = -1;
1124     bool same_blur = true;
1125     guint blur_items = 0;
1126     guint items = 0;
1127     
1128     for (GSList const *i = objects; i != NULL; i = i->next) {
1129         SPObject *obj = SP_OBJECT (i->data);
1130         SPStyle *style = SP_OBJECT_STYLE (obj);
1131         if (!style) continue;
1132         if (!SP_IS_ITEM(obj)) continue;
1134         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1136         items ++;
1138         //if object has a filter
1139         if (style->filter.set && style->getFilter()) {
1140             //cycle through filter primitives
1141             SPObject *primitive_obj = style->getFilter()->children;
1142             while (primitive_obj) {
1143                 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1144                     SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1145                     
1146                     //if primitive is gaussianblur
1147                     if(SP_IS_GAUSSIANBLUR(primitive)) {
1148                         SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1149                         float num = spblur->stdDeviation.getNumber();
1150                         blur_sum += num * NR::expansion(i2d);
1151                         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
1152                             same_blur = false;
1153                         blur_prev = num;
1154                         //TODO: deal with opt number, for the moment it's not necessary to the ui.
1155                         blur_items ++;
1156                     }
1157                 }
1158                 primitive_obj = primitive_obj->next;
1159             }
1160         }
1161     }
1163     if (items > 0) {
1164         if (blur_items > 0)
1165             blur_sum /= blur_items;
1166         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1167     }
1169     if (items == 0) {
1170         return QUERY_STYLE_NOTHING;
1171     } else if (items == 1) {
1172         return QUERY_STYLE_SINGLE;
1173     } else {
1174         if (same_blur)
1175             return QUERY_STYLE_MULTIPLE_SAME;
1176         else
1177             return QUERY_STYLE_MULTIPLE_AVERAGED;
1178     }
1181 /**
1182  * Query the given list of objects for the given property, write
1183  * the result to style, return appropriate flag.
1184  */
1185 int
1186 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1188     if (property == QUERY_STYLE_PROPERTY_FILL) {
1189         return objects_query_fillstroke (list, style, true);
1190     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1191         return objects_query_fillstroke (list, style, false);
1193     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1194         return objects_query_strokewidth (list, style);
1195     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1196         return objects_query_miterlimit (list, style);
1197     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1198         return objects_query_strokecap (list, style);
1199     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1200         return objects_query_strokejoin (list, style);
1202     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1203         return objects_query_opacity (list, style);
1205     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1206         return objects_query_fontfamily (list, style);
1207     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1208         return objects_query_fontstyle (list, style);
1209     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1210         return objects_query_fontnumbers (list, style);
1212     } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1213         return objects_query_blend (list, style);
1214     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1215         return objects_query_blur (list, style);
1216     }
1217     return QUERY_STYLE_NOTHING;
1221 /**
1222  * Query the subselection (if any) or selection on the given desktop for the given property, write
1223  * the result to style, return appropriate flag.
1224  */
1225 int
1226 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1228     int ret = desktop->_query_style_signal.emit(style, property);
1230     if (ret != QUERY_STYLE_NOTHING)
1231         return ret; // subselection returned a style, pass it on
1233     // otherwise, do querying and averaging over selection
1234     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1237 /**
1238  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1239  * the properties returned QUERY_STYLE_NOTHING.
1240  */
1241 bool
1242 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1244         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1245         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1246         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1247         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1248         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1249         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1250         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1251         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1252         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1253         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1254         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1255         
1256         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);
1260 /*
1261   Local Variables:
1262   mode:c++
1263   c-file-style:"stroustrup"
1264   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1265   indent-tabs-mode:nil
1266   fill-column:99
1267   End:
1268 */
1269 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :