Code

56fc9d4be7fc14552b7103587d249e8a3ab53b02
[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     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
355     gchar const *style_str = NULL;
356     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
357         style_str = desktop_style;
358     } else {
359         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
360         if (tool_repr) {
361             style_str = tool_repr->attribute("style");
362         }
363     }
365     double ret = 12;
366     if (style_str) {
367         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
368         sp_style_merge_from_style_string(style, style_str);
369         ret = style->font_size.computed;
370         sp_style_unref(style);
371     }
372     return ret;
375 /** Determine average stroke width, simple method */
376 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
377 gdouble
378 stroke_average_width (GSList const *objects)
380     if (g_slist_length ((GSList *) objects) == 0)
381         return NR_HUGE;
383     gdouble avgwidth = 0.0;
384     bool notstroked = true;
385     int n_notstroked = 0;
387     for (GSList const *l = objects; l != NULL; l = l->next) {
388         if (!SP_IS_ITEM (l->data))
389             continue;
391         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
393         SPObject *object = SP_OBJECT(l->data);
395         if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
396             ++n_notstroked;   // do not count nonstroked objects
397             continue;
398         } else {
399             notstroked = false;
400         }
402         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
403     }
405     if (notstroked)
406         return NR_HUGE;
408     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
411 /**
412  * Write to style_res the average fill or stroke of list of objects, if applicable.
413  */
414 int
415 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
417     if (g_slist_length(objects) == 0) {
418         /* No objects, set empty */
419         return QUERY_STYLE_NOTHING;
420     }
422     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
423     paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
424     paint_res->set = TRUE;
426     gfloat c[4];
427     c[0] = c[1] = c[2] = c[3] = 0.0;
428     gint num = 0;
430     gfloat prev[3];
431     prev[0] = prev[1] = prev[2] = 0.0;
432     bool same_color = true;
434     for (GSList const *i = objects; i != NULL; i = i->next) {
435         SPObject *obj = SP_OBJECT (i->data);
436         SPStyle *style = SP_OBJECT_STYLE (obj);
437         if (!style) continue;
439         SPIPaint *paint = isfill? &style->fill : &style->stroke;
441         // We consider paint "effectively set" for anything within text hierarchy
442         SPObject *parent = SP_OBJECT_PARENT (obj);
443         bool paint_effectively_set = 
444             paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
445             || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent) 
446             || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
448         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
450         if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
451             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
452         }
454         if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
455             // both previous paint and this paint were a server, see if the servers are compatible
457             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
458             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
460             if (SP_IS_LINEARGRADIENT (server_res)) {
462                 if (!SP_IS_LINEARGRADIENT(server))
463                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
465                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
466                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
467                 if (vector_res != vector)
468                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
470             } else if (SP_IS_RADIALGRADIENT (server_res)) {
472                 if (!SP_IS_RADIALGRADIENT(server))
473                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
475                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
476                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
477                 if (vector_res != vector)
478                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
480             } else if (SP_IS_PATTERN (server_res)) {
482                 if (!SP_IS_PATTERN(server))
483                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
485                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
486                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
487                 if (pat_res != pat)
488                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
489             }
490         }
492         // 2. Sum color, copy server from paint to paint_res
494         if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
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 (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
501                 prev[0] = d[0];
502                 prev[1] = d[1];
503                 prev[2] = d[2];
504             } else {
505                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
506                     same_color = false;
507             }
509             // average color
510             c[0] += d[0];
511             c[1] += d[1];
512             c[2] += d[2];
513             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
514             num ++;
515         }
517        paint_res->type = paint->type;
518        if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
519            if (isfill) {
520                sp_style_set_to_uri_string (style_res, true, style->getFillURI());
521            } else {
522                sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
523            }
524        }
525        paint_res->set = paint_effectively_set;
526        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
527     }
529     // After all objects processed, divide the color if necessary and return
530     if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
531         g_assert (num >= 1);
533         c[0] /= num;
534         c[1] /= num;
535         c[2] /= num;
536         c[3] /= num;
537         sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
538         if (isfill) {
539             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
540         } else {
541             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
542         }
543         if (num > 1) {
544             if (same_color)
545                 return QUERY_STYLE_MULTIPLE_SAME;
546             else
547                 return QUERY_STYLE_MULTIPLE_AVERAGED;
548         } else {
549             return QUERY_STYLE_SINGLE;
550         }
551     }
553     // Not color
554     if (g_slist_length(objects) > 1) {
555         return QUERY_STYLE_MULTIPLE_SAME;
556     } else {
557         return QUERY_STYLE_SINGLE;
558     }
561 /**
562  * Write to style_res the average opacity of a list of objects.
563  */
564 int
565 objects_query_opacity (GSList *objects, SPStyle *style_res)
567     if (g_slist_length(objects) == 0) {
568         /* No objects, set empty */
569         return QUERY_STYLE_NOTHING;
570     }
572     gdouble opacity_sum = 0;
573     gdouble opacity_prev = -1;
574     bool same_opacity = true;
575     guint opacity_items = 0;
578     for (GSList const *i = objects; i != NULL; i = i->next) {
579         SPObject *obj = SP_OBJECT (i->data);
580         SPStyle *style = SP_OBJECT_STYLE (obj);
581         if (!style) continue;
583         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
584         opacity_sum += opacity;
585         if (opacity_prev != -1 && opacity != opacity_prev)
586             same_opacity = false;
587         opacity_prev = opacity;
588         opacity_items ++;
589     }
590     if (opacity_items > 1)
591         opacity_sum /= opacity_items;
593     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
595     if (opacity_items == 0) {
596         return QUERY_STYLE_NOTHING;
597     } else if (opacity_items == 1) {
598         return QUERY_STYLE_SINGLE;
599     } else {
600         if (same_opacity)
601             return QUERY_STYLE_MULTIPLE_SAME;
602         else
603             return QUERY_STYLE_MULTIPLE_AVERAGED;
604     }
607 /**
608  * Write to style_res the average stroke width of a list of objects.
609  */
610 int
611 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
613     if (g_slist_length(objects) == 0) {
614         /* No objects, set empty */
615         return QUERY_STYLE_NOTHING;
616     }
618     gdouble avgwidth = 0.0;
620     gdouble prev_sw = -1;
621     bool same_sw = true;
623     int n_stroked = 0;
625     for (GSList const *i = objects; i != NULL; i = i->next) {
626         SPObject *obj = SP_OBJECT (i->data);
627         if (!SP_IS_ITEM(obj)) continue;
628         SPStyle *style = SP_OBJECT_STYLE (obj);
629         if (!style) continue;
631         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
632             continue;
633         }
635         n_stroked ++;
637         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
638         double sw = style->stroke_width.computed * i2d.expansion();
640         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
641             same_sw = false;
642         prev_sw = sw;
644         avgwidth += sw;
645     }
647     if (n_stroked > 1)
648         avgwidth /= (n_stroked);
650     style_res->stroke_width.computed = avgwidth;
651     style_res->stroke_width.set = true;
653     if (n_stroked == 0) {
654         return QUERY_STYLE_NOTHING;
655     } else if (n_stroked == 1) {
656         return QUERY_STYLE_SINGLE;
657     } else {
658         if (same_sw)
659             return QUERY_STYLE_MULTIPLE_SAME;
660         else
661             return QUERY_STYLE_MULTIPLE_AVERAGED;
662     }
665 /**
666  * Write to style_res the average miter limit of a list of objects.
667  */
668 int
669 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
671     if (g_slist_length(objects) == 0) {
672         /* No objects, set empty */
673         return QUERY_STYLE_NOTHING;
674     }
676     gdouble avgml = 0.0;
677     int n_stroked = 0;
679     gdouble prev_ml = -1;
680     bool same_ml = true;
682     for (GSList const *i = objects; i != NULL; i = i->next) {
683         SPObject *obj = SP_OBJECT (i->data);
684         if (!SP_IS_ITEM(obj)) continue;
685         SPStyle *style = SP_OBJECT_STYLE (obj);
686         if (!style) continue;
688         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
689             continue;
690         }
692         n_stroked ++;
694         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
695             same_ml = false;
696         prev_ml = style->stroke_miterlimit.value;
698         avgml += style->stroke_miterlimit.value;
699     }
701     if (n_stroked > 1)
702         avgml /= (n_stroked);
704     style_res->stroke_miterlimit.value = avgml;
705     style_res->stroke_miterlimit.set = true;
707     if (n_stroked == 0) {
708         return QUERY_STYLE_NOTHING;
709     } else if (n_stroked == 1) {
710         return QUERY_STYLE_SINGLE;
711     } else {
712         if (same_ml)
713             return QUERY_STYLE_MULTIPLE_SAME;
714         else
715             return QUERY_STYLE_MULTIPLE_AVERAGED;
716     }
719 /**
720  * Write to style_res the stroke cap of a list of objects.
721  */
722 int
723 objects_query_strokecap (GSList *objects, SPStyle *style_res)
725     if (g_slist_length(objects) == 0) {
726         /* No objects, set empty */
727         return QUERY_STYLE_NOTHING;
728     }
730     int cap = -1;
731     gdouble prev_cap = -1;
732     bool same_cap = true;
733     int n_stroked = 0;
735     for (GSList const *i = objects; i != NULL; i = i->next) {
736         SPObject *obj = SP_OBJECT (i->data);
737         if (!SP_IS_ITEM(obj)) continue;
738         SPStyle *style = SP_OBJECT_STYLE (obj);
739         if (!style) continue;
741         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
742             continue;
743         }
745         n_stroked ++;
747         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
748             same_cap = false;
749         prev_cap = style->stroke_linecap.value;
751         cap = style->stroke_linecap.value;
752     }
754     style_res->stroke_linecap.value = cap;
755     style_res->stroke_linecap.set = true;
757     if (n_stroked == 0) {
758         return QUERY_STYLE_NOTHING;
759     } else if (n_stroked == 1) {
760         return QUERY_STYLE_SINGLE;
761     } else {
762         if (same_cap)
763             return QUERY_STYLE_MULTIPLE_SAME;
764         else
765             return QUERY_STYLE_MULTIPLE_DIFFERENT;
766     }
769 /**
770  * Write to style_res the stroke join of a list of objects.
771  */
772 int
773 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
775     if (g_slist_length(objects) == 0) {
776         /* No objects, set empty */
777         return QUERY_STYLE_NOTHING;
778     }
780     int join = -1;
781     gdouble prev_join = -1;
782     bool same_join = true;
783     int n_stroked = 0;
785     for (GSList const *i = objects; i != NULL; i = i->next) {
786         SPObject *obj = SP_OBJECT (i->data);
787         if (!SP_IS_ITEM(obj)) continue;
788         SPStyle *style = SP_OBJECT_STYLE (obj);
789         if (!style) continue;
791         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
792             continue;
793         }
795         n_stroked ++;
797         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
798             same_join = false;
799         prev_join = style->stroke_linejoin.value;
801         join = style->stroke_linejoin.value;
802     }
804     style_res->stroke_linejoin.value = join;
805     style_res->stroke_linejoin.set = true;
807     if (n_stroked == 0) {
808         return QUERY_STYLE_NOTHING;
809     } else if (n_stroked == 1) {
810         return QUERY_STYLE_SINGLE;
811     } else {
812         if (same_join)
813             return QUERY_STYLE_MULTIPLE_SAME;
814         else
815             return QUERY_STYLE_MULTIPLE_DIFFERENT;
816     }
819 /**
820  * Write to style_res the average font size and spacing of objects.
821  */
822 int
823 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
825     bool different = false;
827     double size = 0;
828     double letterspacing = 0;
829     double linespacing = 0;
830     bool linespacing_normal = false;
831     bool letterspacing_normal = false;
833     double size_prev = 0;
834     double letterspacing_prev = 0;
835     double linespacing_prev = 0;
837     /// \todo FIXME: add word spacing, kerns? rotates?
839     int texts = 0;
841     for (GSList const *i = objects; i != NULL; i = i->next) {
842         SPObject *obj = SP_OBJECT (i->data);
844         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
845             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
846             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
847             continue;
849         SPStyle *style = SP_OBJECT_STYLE (obj);
850         if (!style) continue;
852         texts ++;
853         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
855         if (style->letter_spacing.normal) {
856             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
857                 letterspacing_normal = true;
858         } else {
859             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
860             letterspacing_normal = false;
861         }
863         double linespacing_current;
864         if (style->line_height.normal) {
865             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
866             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
867                 linespacing_normal = true;
868         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
869             linespacing_current = style->line_height.value;
870             linespacing_normal = false;
871         } else { // we need % here
872             linespacing_current = style->line_height.computed / style->font_size.computed;
873             linespacing_normal = false;
874         }
875         linespacing += linespacing_current;
877         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
878             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
879             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
880             different = true;
881         }
883         size_prev = style->font_size.computed;
884         letterspacing_prev = style->letter_spacing.computed;
885         linespacing_prev = linespacing_current;
887         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
888         style_res->text_anchor.computed = style->text_anchor.computed;
889         style_res->writing_mode.computed = style->writing_mode.computed;
890     }
892     if (texts == 0)
893         return QUERY_STYLE_NOTHING;
895     if (texts > 1) {
896         size /= texts;
897         letterspacing /= texts;
898         linespacing /= texts;
899     }
901     style_res->font_size.computed = size;
902     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
904     style_res->letter_spacing.normal = letterspacing_normal;
905     style_res->letter_spacing.computed = letterspacing;
907     style_res->line_height.normal = linespacing_normal;
908     style_res->line_height.computed = linespacing;
909     style_res->line_height.value = linespacing;
910     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
912     if (texts > 1) {
913         if (different) {
914             return QUERY_STYLE_MULTIPLE_AVERAGED;
915         } else {
916             return QUERY_STYLE_MULTIPLE_SAME;
917         }
918     } else {
919         return QUERY_STYLE_SINGLE;
920     }
923 /**
924  * Write to style_res the average font style of objects.
925  */
926 int
927 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
929     bool different = false;
930     bool set = false;
932     int texts = 0;
934     for (GSList const *i = objects; i != NULL; i = i->next) {
935         SPObject *obj = SP_OBJECT (i->data);
937         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
938             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
939             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
940             continue;
942         SPStyle *style = SP_OBJECT_STYLE (obj);
943         if (!style) continue;
945         texts ++;
947         if (set &&
948             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
949             different = true;  // different styles
950         }
952         set = TRUE;
953         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
954         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
955         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
956         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
957         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
958     }
960     if (texts == 0 || !set)
961         return QUERY_STYLE_NOTHING;
963     if (texts > 1) {
964         if (different) {
965             return QUERY_STYLE_MULTIPLE_DIFFERENT;
966         } else {
967             return QUERY_STYLE_MULTIPLE_SAME;
968         }
969     } else {
970         return QUERY_STYLE_SINGLE;
971     }
974 /**
975  * Write to style_res the average font family of objects.
976  */
977 int
978 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
980     bool different = false;
981     int texts = 0;
983     if (style_res->text->font_family.value) {
984         g_free(style_res->text->font_family.value);
985         style_res->text->font_family.value = NULL;
986     }
987     style_res->text->font_family.set = FALSE;
989     for (GSList const *i = objects; i != NULL; i = i->next) {
990         SPObject *obj = SP_OBJECT (i->data);
992         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
993             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
994             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
995             continue;
997         SPStyle *style = SP_OBJECT_STYLE (obj);
998         if (!style) continue;
1000         texts ++;
1002         if (style_res->text->font_family.value && style->text->font_family.value &&
1003             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1004             different = true;  // different fonts
1005         }
1007         if (style_res->text->font_family.value) {
1008             g_free(style_res->text->font_family.value);
1009             style_res->text->font_family.value = NULL;
1010         }
1012         style_res->text->font_family.set = TRUE;
1013         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1014     }
1016     if (texts == 0 || !style_res->text->font_family.set)
1017         return QUERY_STYLE_NOTHING;
1019     if (texts > 1) {
1020         if (different) {
1021             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1022         } else {
1023             return QUERY_STYLE_MULTIPLE_SAME;
1024         }
1025     } else {
1026         return QUERY_STYLE_SINGLE;
1027     }
1030 int
1031 objects_query_blend (GSList *objects, SPStyle *style_res)
1033     const int empty_prev = -2;
1034     const int complex_filter = 5;
1035     int blend = 0;
1036     float blend_prev = empty_prev;
1037     bool same_blend = true;
1038     guint items = 0;
1039     
1040     for (GSList const *i = objects; i != NULL; i = i->next) {
1041         SPObject *obj = SP_OBJECT (i->data);
1042         SPStyle *style = SP_OBJECT_STYLE (obj);
1043         if(!style || !SP_IS_ITEM(obj)) continue;
1045         items++;
1047         //if object has a filter
1048         if (style->filter.set && style->getFilter()) {
1049             int blurcount = 0;
1050             int blendcount = 0;
1052             // determine whether filter is simple (blend and/or blur) or complex
1053             for(SPObject *primitive_obj = style->getFilter()->children;
1054                 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1055                 primitive_obj = primitive_obj->next) {
1056                 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1057                 if(SP_IS_FEBLEND(primitive))
1058                     ++blendcount;
1059                 else if(SP_IS_GAUSSIANBLUR(primitive))
1060                     ++blurcount;
1061                 else {
1062                     blurcount = complex_filter;
1063                     break;
1064                 }
1065             }
1067             // simple filter
1068             if(blurcount == 1 || blendcount == 1) {
1069                 for(SPObject *primitive_obj = style->getFilter()->children;
1070                     primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1071                     primitive_obj = primitive_obj->next) {
1072                     if(SP_IS_FEBLEND(primitive_obj)) {
1073                         SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1074                         blend = spblend->blend_mode;
1075                     }
1076                 }
1077             }
1078             else {
1079                 blend = complex_filter;
1080             }
1081         }
1082         // defaults to blend mode = "normal"
1083         else {
1084             blend = 0;
1085         }
1087         if(blend_prev != empty_prev && blend_prev != blend)
1088             same_blend = false;
1089         blend_prev = blend;
1090     }
1092     if (items > 0) {
1093         style_res->filter_blend_mode.value = blend;
1094     }
1096     if (items == 0) {
1097         return QUERY_STYLE_NOTHING;
1098     } else if (items == 1) {
1099         return QUERY_STYLE_SINGLE;
1100     } else {
1101         if(same_blend)
1102             return QUERY_STYLE_MULTIPLE_SAME;
1103         else
1104             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1105     }
1108 /**
1109  * Write to style_res the average blurring of a list of objects.
1110  */
1111 int
1112 objects_query_blur (GSList *objects, SPStyle *style_res)
1114    if (g_slist_length(objects) == 0) {
1115         /* No objects, set empty */
1116         return QUERY_STYLE_NOTHING;
1117     }
1119     float blur_sum = 0;
1120     float blur_prev = -1;
1121     bool same_blur = true;
1122     guint blur_items = 0;
1123     guint items = 0;
1124     
1125     for (GSList const *i = objects; i != NULL; i = i->next) {
1126         SPObject *obj = SP_OBJECT (i->data);
1127         SPStyle *style = SP_OBJECT_STYLE (obj);
1128         if (!style) continue;
1129         if (!SP_IS_ITEM(obj)) continue;
1131         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1133         items ++;
1135         //if object has a filter
1136         if (style->filter.set && style->getFilter()) {
1137             //cycle through filter primitives
1138             SPObject *primitive_obj = style->getFilter()->children;
1139             while (primitive_obj) {
1140                 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1141                     SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1142                     
1143                     //if primitive is gaussianblur
1144                     if(SP_IS_GAUSSIANBLUR(primitive)) {
1145                         SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1146                         float num = spblur->stdDeviation.getNumber();
1147                         blur_sum += num * NR::expansion(i2d);
1148                         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
1149                             same_blur = false;
1150                         blur_prev = num;
1151                         //TODO: deal with opt number, for the moment it's not necessary to the ui.
1152                         blur_items ++;
1153                     }
1154                 }
1155                 primitive_obj = primitive_obj->next;
1156             }
1157         }
1158     }
1160     if (items > 0) {
1161         if (blur_items > 0)
1162             blur_sum /= blur_items;
1163         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1164     }
1166     if (items == 0) {
1167         return QUERY_STYLE_NOTHING;
1168     } else if (items == 1) {
1169         return QUERY_STYLE_SINGLE;
1170     } else {
1171         if (same_blur)
1172             return QUERY_STYLE_MULTIPLE_SAME;
1173         else
1174             return QUERY_STYLE_MULTIPLE_AVERAGED;
1175     }
1178 /**
1179  * Query the given list of objects for the given property, write
1180  * the result to style, return appropriate flag.
1181  */
1182 int
1183 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1185     if (property == QUERY_STYLE_PROPERTY_FILL) {
1186         return objects_query_fillstroke (list, style, true);
1187     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1188         return objects_query_fillstroke (list, style, false);
1190     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1191         return objects_query_strokewidth (list, style);
1192     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1193         return objects_query_miterlimit (list, style);
1194     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1195         return objects_query_strokecap (list, style);
1196     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1197         return objects_query_strokejoin (list, style);
1199     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1200         return objects_query_opacity (list, style);
1202     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1203         return objects_query_fontfamily (list, style);
1204     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1205         return objects_query_fontstyle (list, style);
1206     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1207         return objects_query_fontnumbers (list, style);
1209     } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1210         return objects_query_blend (list, style);
1211     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1212         return objects_query_blur (list, style);
1213     }
1214     return QUERY_STYLE_NOTHING;
1218 /**
1219  * Query the subselection (if any) or selection on the given desktop for the given property, write
1220  * the result to style, return appropriate flag.
1221  */
1222 int
1223 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1225     int ret = desktop->_query_style_signal.emit(style, property);
1227     if (ret != QUERY_STYLE_NOTHING)
1228         return ret; // subselection returned a style, pass it on
1230     // otherwise, do querying and averaging over selection
1231     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1234 /**
1235  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1236  * the properties returned QUERY_STYLE_NOTHING.
1237  */
1238 bool
1239 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1241         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1242         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1243         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1244         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1245         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1246         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1247         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1248         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1249         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1250         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1251         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1252         
1253         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);
1257 /*
1258   Local Variables:
1259   mode:c++
1260   c-file-style:"stroustrup"
1261   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1262   indent-tabs-mode:nil
1263   fill-column:99
1264   End:
1265 */
1266 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :