Code

fix crash when editing text with multiple tspans
[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-filter.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-radial-gradient.h"
34 #include "sp-pattern.h"
35 #include "xml/repr.h"
36 #include "libnrtype/font-style-to-pos.h"
39 #include "desktop-style.h"
41 /**
42  * Set color on selection on desktop.
43  */
44 void
45 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
46 {
47     /// \todo relative color setting
48     if (is_relative) {
49         g_warning("FIXME: relative color setting not yet implemented");
50         return;
51     }
53     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
54     gchar b[64];
55     sp_svg_write_color(b, 64, rgba);
56     SPCSSAttr *css = sp_repr_css_attr_new();
57     if (fill) {
58         sp_repr_css_set_property(css, "fill", b);
59         Inkscape::CSSOStringStream osalpha;
60         osalpha << color[3];
61         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
62     } else {
63         sp_repr_css_set_property(css, "stroke", b);
64         Inkscape::CSSOStringStream osalpha;
65         osalpha << color[3];
66         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
67     }
69     sp_desktop_set_style(desktop, css);
71     sp_repr_css_attr_unref(css);
72 }
74 /**
75  * Apply style on object and children, recursively.
76  */
77 void
78 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
79 {
80     // non-items should not have style
81     if (!SP_IS_ITEM(o))
82         return;
84     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
85     // but must always inherit from the parent text. Same for textPath.
86     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
88     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
89     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
90     // flowtext).
92     if (!(skip_lines
93           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
94               || SP_IS_FLOWDIV(o)
95               || SP_IS_FLOWPARA(o)
96               || SP_IS_TEXTPATH(o))
97           && !SP_OBJECT_REPR(o)->attribute("style"))
98         &&
99         !(SP_IS_FLOWREGION(o) ||
100           SP_IS_FLOWREGIONEXCLUDE(o) ||
101           (SP_IS_USE(o) &&
102            SP_OBJECT_PARENT(o) &&
103            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
104             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
105            )
106           )
107          )
108         ) {
110         SPCSSAttr *css_set = sp_repr_css_attr_new();
111         sp_repr_css_merge(css_set, css);
113         // Scale the style by the inverse of the accumulated parent transform in the paste context.
114         {
115             NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
116             double const ex(NR::expansion(local));
117             if ( ( ex != 0. )
118                  && ( ex != 1. ) ) {
119                 sp_css_attr_scale(css_set, 1/ex);
120             }
121         }
123         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
125         sp_repr_css_attr_unref(css_set);
126     }
128     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
129     if (SP_IS_USE(o))
130         return;
132     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
133         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
134             // Unset properties which are accumulating and thus should not be set recursively.
135             // 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.
136             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
137             sp_repr_css_merge(css_recurse, css);
138             sp_repr_css_set_property(css_recurse, "opacity", NULL);
139             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
140             sp_repr_css_attr_unref(css_recurse);
141         } else {
142             sp_desktop_apply_css_recursive(child, css, skip_lines);
143         }
144     }
147 /**
148  * Apply style on selection on desktop.
149  */
150 void
151 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
153     if (write_current) {
154 // 1. Set internal value
155         sp_repr_css_merge(desktop->current, css);
157 // 1a. Write to prefs; make a copy and unset any URIs first
158         SPCSSAttr *css_write = sp_repr_css_attr_new();
159         sp_repr_css_merge(css_write, css);
160         sp_css_attr_unset_uris(css_write);
161         sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
162         sp_repr_css_attr_unref(css_write);
163     }
165     if (!change)
166         return;
168 // 2. Emit signal
169     bool intercepted = desktop->_set_style_signal.emit(css);
171 /** \todo
172  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
173  * rect corners, font size for the object's own transform so that pasting
174  * fills does not depend on preserve/optimize.
175  */
177 // 3. If nobody has intercepted the signal, apply the style to the selection
178     if (!intercepted) {
179         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
180             /// \todo if the style is text-only, apply only to texts?
181             sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
182         }
183     }
186 /**
187  * Return the desktop's current style.
188  */
189 SPCSSAttr *
190 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
192     SPCSSAttr *css = sp_repr_css_attr_new();
193     sp_repr_css_merge(css, desktop->current);
194     if (!css->attributeList()) {
195         sp_repr_css_attr_unref(css);
196         return NULL;
197     } else {
198         if (!with_text) {
199             css = sp_css_attr_unset_text(css);
200         }
201         return css;
202     }
205 /**
206  * Return the desktop's current color.
207  */
208 guint32
209 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
211     guint32 r = 0; // if there's no color, return black
212     gchar const *property = sp_repr_css_property(desktop->current,
213                                                  is_fill ? "fill" : "stroke",
214                                                  "#000");
216     if (desktop->current && property) { // if there is style and the property in it,
217         if (strncmp(property, "url", 3)) { // and if it's not url,
218             // read it
219             r = sp_svg_read_color(property, r);
220         }
221     }
223     return r;
226 double
227 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
229     SPCSSAttr *css = NULL;
230     gfloat value = 1.0; // default if nothing else found
231     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
232         css = sp_desktop_get_style(desktop, true);
233     } else { 
234         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
235         if (tool_repr) {
236             css = sp_repr_css_attr_inherited(tool_repr, "style");
237         }
238     }
239    
240     if (css) {
241         gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
242            
243         if (desktop->current && property) { // if there is style and the property in it,
244             if ( !sp_svg_number_read_f(property, &value) ) {
245                 value = 1.0; // things failed. set back to the default
246             }
247         }
249         sp_repr_css_attr_unref(css);
250     }
252     return value;
254 double
255 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
257     SPCSSAttr *css = NULL;
258     gfloat value = 1.0; // default if nothing else found
259     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
260         css = sp_desktop_get_style(desktop, true);
261     } else { 
262         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
263         if (tool_repr) {
264             css = sp_repr_css_attr_inherited(tool_repr, "style");
265         }
266     }
267    
268     if (css) {
269         gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
270            
271         if (desktop->current && property) { // if there is style and the property in it,
272             if ( !sp_svg_number_read_f(property, &value) ) {
273                 value = 1.0; // things failed. set back to the default
274             }
275         }
277         sp_repr_css_attr_unref(css);
278     }
280     return value;
282 guint32
283 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
285     SPCSSAttr *css = NULL;
286     guint32 r = 0; // if there's no color, return black
287     if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
288         css = sp_desktop_get_style(desktop, true);
289     } else {
290         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
291         if (tool_repr) {
292             css = sp_repr_css_attr_inherited(tool_repr, "style");
293         }
294     }
295    
296     if (css) {
297         gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
298            
299         if (desktop->current && property) { // if there is style and the property in it,
300             if (strncmp(property, "url", 3)) { // and if it's not url,
301                 // read it
302                 r = sp_svg_read_color(property, r);
303             }
304         }
306         sp_repr_css_attr_unref(css);
307     }
309     return r | 0xff;
311 /**
312  * Apply the desktop's current style or the tool style to repr.
313  */
314 void
315 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
317     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
318     if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
319         sp_repr_css_set(repr, css_current, "style");
320     } else {
321         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
322         if (tool_repr) {
323             SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
324             sp_repr_css_set(repr, css, "style");
325             sp_repr_css_attr_unref(css);
326         }
327     }
328     if (css_current) {
329         sp_repr_css_attr_unref(css_current);
330     }
333 /**
334  * Returns the font size (in SVG pixels) of the text tool style (if text
335  * tool uses its own style) or desktop style (otherwise).
336 */
337 double
338 sp_desktop_get_font_size_tool(SPDesktop *desktop)
340     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
341     gchar const *style_str = NULL;
342     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
343         style_str = desktop_style;
344     } else {
345         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
346         if (tool_repr) {
347             style_str = tool_repr->attribute("style");
348         }
349     }
351     double ret = 12;
352     if (style_str) {
353         SPStyle *style = sp_style_new();
354         sp_style_merge_from_style_string(style, style_str);
355         ret = style->font_size.computed;
356         sp_style_unref(style);
357     }
358     return ret;
361 /** Determine average stroke width, simple method */
362 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
363 gdouble
364 stroke_average_width (GSList const *objects)
366     if (g_slist_length ((GSList *) objects) == 0)
367         return NR_HUGE;
369     gdouble avgwidth = 0.0;
370     bool notstroked = true;
371     int n_notstroked = 0;
373     for (GSList const *l = objects; l != NULL; l = l->next) {
374         if (!SP_IS_ITEM (l->data))
375             continue;
377         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
379         SPObject *object = SP_OBJECT(l->data);
381         if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
382             ++n_notstroked;   // do not count nonstroked objects
383             continue;
384         } else {
385             notstroked = false;
386         }
388         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
389     }
391     if (notstroked)
392         return NR_HUGE;
394     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
397 /**
398  * Write to style_res the average fill or stroke of list of objects, if applicable.
399  */
400 int
401 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
403     if (g_slist_length(objects) == 0) {
404         /* No objects, set empty */
405         return QUERY_STYLE_NOTHING;
406     }
408     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
409     paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
410     paint_res->set = TRUE;
412     gfloat c[4];
413     c[0] = c[1] = c[2] = c[3] = 0.0;
414     gint num = 0;
416     gfloat prev[3];
417     prev[0] = prev[1] = prev[2] = 0.0;
418     bool same_color = true;
420     for (GSList const *i = objects; i != NULL; i = i->next) {
421         SPObject *obj = SP_OBJECT (i->data);
422         SPStyle *style = SP_OBJECT_STYLE (obj);
423         if (!style) continue;
425         SPIPaint *paint = isfill? &style->fill : &style->stroke;
427         // We consider paint "effectively set" for anything within text hierarchy
428         SPObject *parent = SP_OBJECT_PARENT (obj);
429         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));
431         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
433         if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
434             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
435         }
437         if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
438             // both previous paint and this paint were a server, see if the servers are compatible
440             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
441             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
443             if (SP_IS_LINEARGRADIENT (server_res)) {
445                 if (!SP_IS_LINEARGRADIENT(server))
446                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
448                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
449                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
450                 if (vector_res != vector)
451                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
453             } else if (SP_IS_RADIALGRADIENT (server_res)) {
455                 if (!SP_IS_RADIALGRADIENT(server))
456                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
458                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
459                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
460                 if (vector_res != vector)
461                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
463             } else if (SP_IS_PATTERN (server_res)) {
465                 if (!SP_IS_PATTERN(server))
466                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
468                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
469                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
470                 if (pat_res != pat)
471                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
472             }
473         }
475         // 2. Sum color, copy server from paint to paint_res
477         if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
479             gfloat d[3];
480             sp_color_get_rgb_floatv (&paint->value.color, d);
482             // Check if this color is the same as previous
483             if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
484                 prev[0] = d[0];
485                 prev[1] = d[1];
486                 prev[2] = d[2];
487             } else {
488                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
489                     same_color = false;
490             }
492             // average color
493             c[0] += d[0];
494             c[1] += d[1];
495             c[2] += d[2];
496             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
497             num ++;
498         }
500        if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
501            if (isfill) {
502                SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
503            } else {
504                SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
505            }
506        }
507        paint_res->type = paint->type;
508        paint_res->set = paint_effectively_set;
509        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
510     }
512     // After all objects processed, divide the color if necessary and return
513     if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
514         g_assert (num >= 1);
516         c[0] /= num;
517         c[1] /= num;
518         c[2] /= num;
519         c[3] /= num;
520         sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
521         if (isfill) {
522             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
523         } else {
524             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
525         }
526         if (num > 1) {
527             if (same_color)
528                 return QUERY_STYLE_MULTIPLE_SAME;
529             else
530                 return QUERY_STYLE_MULTIPLE_AVERAGED;
531         } else {
532             return QUERY_STYLE_SINGLE;
533         }
534     }
536     // Not color
537     if (g_slist_length(objects) > 1) {
538         return QUERY_STYLE_MULTIPLE_SAME;
539     } else {
540         return QUERY_STYLE_SINGLE;
541     }
544 /**
545  * Write to style_res the average opacity of a list of objects.
546  */
547 int
548 objects_query_opacity (GSList *objects, SPStyle *style_res)
550     if (g_slist_length(objects) == 0) {
551         /* No objects, set empty */
552         return QUERY_STYLE_NOTHING;
553     }
555     gdouble opacity_sum = 0;
556     gdouble opacity_prev = -1;
557     bool same_opacity = true;
558     guint opacity_items = 0;
561     for (GSList const *i = objects; i != NULL; i = i->next) {
562         SPObject *obj = SP_OBJECT (i->data);
563         SPStyle *style = SP_OBJECT_STYLE (obj);
564         if (!style) continue;
566         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
567         opacity_sum += opacity;
568         if (opacity_prev != -1 && opacity != opacity_prev)
569             same_opacity = false;
570         opacity_prev = opacity;
571         opacity_items ++;
572     }
573     if (opacity_items > 1)
574         opacity_sum /= opacity_items;
576     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
578     if (opacity_items == 0) {
579         return QUERY_STYLE_NOTHING;
580     } else if (opacity_items == 1) {
581         return QUERY_STYLE_SINGLE;
582     } else {
583         if (same_opacity)
584             return QUERY_STYLE_MULTIPLE_SAME;
585         else
586             return QUERY_STYLE_MULTIPLE_AVERAGED;
587     }
590 /**
591  * Write to style_res the average stroke width of a list of objects.
592  */
593 int
594 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
596     if (g_slist_length(objects) == 0) {
597         /* No objects, set empty */
598         return QUERY_STYLE_NOTHING;
599     }
601     gdouble avgwidth = 0.0;
603     gdouble prev_sw = -1;
604     bool same_sw = true;
606     int n_stroked = 0;
608     for (GSList const *i = objects; i != NULL; i = i->next) {
609         SPObject *obj = SP_OBJECT (i->data);
610         if (!SP_IS_ITEM(obj)) continue;
611         SPStyle *style = SP_OBJECT_STYLE (obj);
612         if (!style) continue;
614         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
615             continue;
616         }
618         n_stroked ++;
620         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
621         double sw = style->stroke_width.computed * i2d.expansion();
623         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
624             same_sw = false;
625         prev_sw = sw;
627         avgwidth += sw;
628     }
630     if (n_stroked > 1)
631         avgwidth /= (n_stroked);
633     style_res->stroke_width.computed = avgwidth;
634     style_res->stroke_width.set = true;
636     if (n_stroked == 0) {
637         return QUERY_STYLE_NOTHING;
638     } else if (n_stroked == 1) {
639         return QUERY_STYLE_SINGLE;
640     } else {
641         if (same_sw)
642             return QUERY_STYLE_MULTIPLE_SAME;
643         else
644             return QUERY_STYLE_MULTIPLE_AVERAGED;
645     }
648 /**
649  * Write to style_res the average miter limit of a list of objects.
650  */
651 int
652 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
654     if (g_slist_length(objects) == 0) {
655         /* No objects, set empty */
656         return QUERY_STYLE_NOTHING;
657     }
659     gdouble avgml = 0.0;
660     int n_stroked = 0;
662     gdouble prev_ml = -1;
663     bool same_ml = true;
665     for (GSList const *i = objects; i != NULL; i = i->next) {
666         SPObject *obj = SP_OBJECT (i->data);
667         if (!SP_IS_ITEM(obj)) continue;
668         SPStyle *style = SP_OBJECT_STYLE (obj);
669         if (!style) continue;
671         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
672             continue;
673         }
675         n_stroked ++;
677         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
678             same_ml = false;
679         prev_ml = style->stroke_miterlimit.value;
681         avgml += style->stroke_miterlimit.value;
682     }
684     if (n_stroked > 1)
685         avgml /= (n_stroked);
687     style_res->stroke_miterlimit.value = avgml;
688     style_res->stroke_miterlimit.set = true;
690     if (n_stroked == 0) {
691         return QUERY_STYLE_NOTHING;
692     } else if (n_stroked == 1) {
693         return QUERY_STYLE_SINGLE;
694     } else {
695         if (same_ml)
696             return QUERY_STYLE_MULTIPLE_SAME;
697         else
698             return QUERY_STYLE_MULTIPLE_AVERAGED;
699     }
702 /**
703  * Write to style_res the stroke cap of a list of objects.
704  */
705 int
706 objects_query_strokecap (GSList *objects, SPStyle *style_res)
708     if (g_slist_length(objects) == 0) {
709         /* No objects, set empty */
710         return QUERY_STYLE_NOTHING;
711     }
713     int cap = -1;
714     gdouble prev_cap = -1;
715     bool same_cap = true;
716     int n_stroked = 0;
718     for (GSList const *i = objects; i != NULL; i = i->next) {
719         SPObject *obj = SP_OBJECT (i->data);
720         if (!SP_IS_ITEM(obj)) continue;
721         SPStyle *style = SP_OBJECT_STYLE (obj);
722         if (!style) continue;
724         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
725             continue;
726         }
728         n_stroked ++;
730         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
731             same_cap = false;
732         prev_cap = style->stroke_linecap.value;
734         cap = style->stroke_linecap.value;
735     }
737     style_res->stroke_linecap.value = cap;
738     style_res->stroke_linecap.set = true;
740     if (n_stroked == 0) {
741         return QUERY_STYLE_NOTHING;
742     } else if (n_stroked == 1) {
743         return QUERY_STYLE_SINGLE;
744     } else {
745         if (same_cap)
746             return QUERY_STYLE_MULTIPLE_SAME;
747         else
748             return QUERY_STYLE_MULTIPLE_DIFFERENT;
749     }
752 /**
753  * Write to style_res the stroke join of a list of objects.
754  */
755 int
756 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
758     if (g_slist_length(objects) == 0) {
759         /* No objects, set empty */
760         return QUERY_STYLE_NOTHING;
761     }
763     int join = -1;
764     gdouble prev_join = -1;
765     bool same_join = true;
766     int n_stroked = 0;
768     for (GSList const *i = objects; i != NULL; i = i->next) {
769         SPObject *obj = SP_OBJECT (i->data);
770         if (!SP_IS_ITEM(obj)) continue;
771         SPStyle *style = SP_OBJECT_STYLE (obj);
772         if (!style) continue;
774         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
775             continue;
776         }
778         n_stroked ++;
780         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
781             same_join = false;
782         prev_join = style->stroke_linejoin.value;
784         join = style->stroke_linejoin.value;
785     }
787     style_res->stroke_linejoin.value = join;
788     style_res->stroke_linejoin.set = true;
790     if (n_stroked == 0) {
791         return QUERY_STYLE_NOTHING;
792     } else if (n_stroked == 1) {
793         return QUERY_STYLE_SINGLE;
794     } else {
795         if (same_join)
796             return QUERY_STYLE_MULTIPLE_SAME;
797         else
798             return QUERY_STYLE_MULTIPLE_DIFFERENT;
799     }
802 /**
803  * Write to style_res the average font size and spacing of objects.
804  */
805 int
806 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
808     bool different = false;
810     double size = 0;
811     double letterspacing = 0;
812     double linespacing = 0;
813     bool linespacing_normal = false;
814     bool letterspacing_normal = false;
816     double size_prev = 0;
817     double letterspacing_prev = 0;
818     double linespacing_prev = 0;
820     /// \todo FIXME: add word spacing, kerns? rotates?
822     int texts = 0;
824     for (GSList const *i = objects; i != NULL; i = i->next) {
825         SPObject *obj = SP_OBJECT (i->data);
827         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
828             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
829             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
830             continue;
832         SPStyle *style = SP_OBJECT_STYLE (obj);
833         if (!style) continue;
835         texts ++;
836         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
838         if (style->letter_spacing.normal) {
839             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
840                 letterspacing_normal = true;
841         } else {
842             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
843             letterspacing_normal = false;
844         }
846         double linespacing_current;
847         if (style->line_height.normal) {
848             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
849             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
850                 linespacing_normal = true;
851         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
852             linespacing_current = style->line_height.value;
853             linespacing_normal = false;
854         } else { // we need % here
855             linespacing_current = style->line_height.computed / style->font_size.computed;
856             linespacing_normal = false;
857         }
858         linespacing += linespacing_current;
860         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
861             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
862             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
863             different = true;
864         }
866         size_prev = style->font_size.computed;
867         letterspacing_prev = style->letter_spacing.computed;
868         linespacing_prev = linespacing_current;
870         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
871         style_res->text_anchor.computed = style->text_anchor.computed;
872         style_res->writing_mode.computed = style->writing_mode.computed;
873     }
875     if (texts == 0)
876         return QUERY_STYLE_NOTHING;
878     if (texts > 1) {
879         size /= texts;
880         letterspacing /= texts;
881         linespacing /= texts;
882     }
884     style_res->font_size.computed = size;
885     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
887     style_res->letter_spacing.normal = letterspacing_normal;
888     style_res->letter_spacing.computed = letterspacing;
890     style_res->line_height.normal = linespacing_normal;
891     style_res->line_height.computed = linespacing;
892     style_res->line_height.value = linespacing;
893     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
895     if (texts > 1) {
896         if (different) {
897             return QUERY_STYLE_MULTIPLE_AVERAGED;
898         } else {
899             return QUERY_STYLE_MULTIPLE_SAME;
900         }
901     } else {
902         return QUERY_STYLE_SINGLE;
903     }
906 /**
907  * Write to style_res the average font style of objects.
908  */
909 int
910 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
912     bool different = false;
913     bool set = false;
915     int texts = 0;
917     for (GSList const *i = objects; i != NULL; i = i->next) {
918         SPObject *obj = SP_OBJECT (i->data);
920         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
921             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
922             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
923             continue;
925         SPStyle *style = SP_OBJECT_STYLE (obj);
926         if (!style) continue;
928         texts ++;
930         if (set &&
931             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
932             different = true;  // different styles
933         }
935         set = TRUE;
936         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
937         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
938         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
939         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
940         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
941     }
943     if (texts == 0 || !set)
944         return QUERY_STYLE_NOTHING;
946     if (texts > 1) {
947         if (different) {
948             return QUERY_STYLE_MULTIPLE_DIFFERENT;
949         } else {
950             return QUERY_STYLE_MULTIPLE_SAME;
951         }
952     } else {
953         return QUERY_STYLE_SINGLE;
954     }
957 /**
958  * Write to style_res the average font family of objects.
959  */
960 int
961 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
963     bool different = false;
964     int texts = 0;
966     if (style_res->text->font_family.value) {
967         g_free(style_res->text->font_family.value);
968         style_res->text->font_family.value = NULL;
969     }
970     style_res->text->font_family.set = FALSE;
972     for (GSList const *i = objects; i != NULL; i = i->next) {
973         SPObject *obj = SP_OBJECT (i->data);
975         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
976             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
977             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
978             continue;
980         SPStyle *style = SP_OBJECT_STYLE (obj);
981         if (!style) continue;
983         texts ++;
985         if (style_res->text->font_family.value && style->text->font_family.value &&
986             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
987             different = true;  // different fonts
988         }
990         if (style_res->text->font_family.value) {
991             g_free(style_res->text->font_family.value);
992             style_res->text->font_family.value = NULL;
993         }
995         style_res->text->font_family.set = TRUE;
996         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
997     }
999     if (texts == 0 || !style_res->text->font_family.set)
1000         return QUERY_STYLE_NOTHING;
1002     if (texts > 1) {
1003         if (different) {
1004             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1005         } else {
1006             return QUERY_STYLE_MULTIPLE_SAME;
1007         }
1008     } else {
1009         return QUERY_STYLE_SINGLE;
1010     }
1013 /**
1014  * Write to style_res the average blurring of a list of objects.
1015  */
1016 int
1017 objects_query_blur (GSList *objects, SPStyle *style_res)
1019    if (g_slist_length(objects) == 0) {
1020         /* No objects, set empty */
1021         return QUERY_STYLE_NOTHING;
1022     }
1024     float blur_sum = 0;
1025     float blur_prev = -1;
1026     bool same_blur = true;
1027     guint blur_items = 0;
1028     guint items = 0;
1029     
1030     for (GSList const *i = objects; i != NULL; i = i->next) {
1031         SPObject *obj = SP_OBJECT (i->data);
1032         SPStyle *style = SP_OBJECT_STYLE (obj);
1033         if (!style) continue;
1034         if (!SP_IS_ITEM(obj)) continue;
1036         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1038         items ++;
1040         //if object has a filter
1041         if (style->filter.set && style->filter.filter) {
1042             //cycle through filter primitives
1043             for(int i=0; i<style->filter.filter->_primitive_count; i++)
1044             {
1045                 SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1046                 //if primitive is gaussianblur
1047                 if(SP_IS_GAUSSIANBLUR(primitive)) {
1048                     SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1049                     float num = spblur->stdDeviation.getNumber();
1050                     blur_sum += num * NR::expansion(i2d);
1051                     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
1052                         same_blur = false;
1053                     blur_prev = num;
1054                     //TODO: deal with opt number, for the moment it's not necessary to the ui.
1055                     blur_items ++;
1056                 }
1057             }
1058         }
1060     }
1062     if (items > 0) {
1063         if (blur_items > 0)
1064             blur_sum /= blur_items;
1065         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1066     }
1068     if (items == 0) {
1069         return QUERY_STYLE_NOTHING;
1070     } else if (items == 1) {
1071         return QUERY_STYLE_SINGLE;
1072     } else {
1073         if (same_blur)
1074             return QUERY_STYLE_MULTIPLE_SAME;
1075         else
1076             return QUERY_STYLE_MULTIPLE_AVERAGED;
1077     }
1080 /**
1081  * Query the given list of objects for the given property, write
1082  * the result to style, return appropriate flag.
1083  */
1084 int
1085 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1087     if (property == QUERY_STYLE_PROPERTY_FILL) {
1088         return objects_query_fillstroke (list, style, true);
1089     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1090         return objects_query_fillstroke (list, style, false);
1092     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1093         return objects_query_strokewidth (list, style);
1094     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1095         return objects_query_miterlimit (list, style);
1096     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1097         return objects_query_strokecap (list, style);
1098     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1099         return objects_query_strokejoin (list, style);
1101     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1102         return objects_query_opacity (list, style);
1104     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1105         return objects_query_fontfamily (list, style);
1106     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1107         return objects_query_fontstyle (list, style);
1108     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1109         return objects_query_fontnumbers (list, style);
1111     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1112         return objects_query_blur (list, style);
1113     }
1114     return QUERY_STYLE_NOTHING;
1118 /**
1119  * Query the subselection (if any) or selection on the given desktop for the given property, write
1120  * the result to style, return appropriate flag.
1121  */
1122 int
1123 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1125     int ret = desktop->_query_style_signal.emit(style, property);
1127     if (ret != QUERY_STYLE_NOTHING)
1128         return ret; // subselection returned a style, pass it on
1130     // otherwise, do querying and averaging over selection
1131     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1134 /**
1135  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1136  * the properties returned QUERY_STYLE_NOTHING.
1137  */
1138 bool
1139 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1141         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1142         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1143         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1144         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1145         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1146         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1147         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1148         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1149         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1150         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1151         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1152         
1153         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);
1157 /*
1158   Local Variables:
1159   mode:c++
1160   c-file-style:"stroustrup"
1161   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1162   indent-tabs-mode:nil
1163   fill-column:99
1164   End:
1165 */
1166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :