Code

If necessary, split up perspectives when applying transformations to boxes; moreover...
[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"
43 #include "svg/svg-icc-color.h"
44 #include "box3d-side.h"
46 /**
47  * Set color on selection on desktop.
48  */
49 void
50 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
51 {
52     /// \todo relative color setting
53     if (is_relative) {
54         g_warning("FIXME: relative color setting not yet implemented");
55         return;
56     }
58     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
59     gchar b[64];
60     sp_svg_write_color(b, sizeof(b), rgba);
61     SPCSSAttr *css = sp_repr_css_attr_new();
62     if (fill) {
63         sp_repr_css_set_property(css, "fill", b);
64         Inkscape::CSSOStringStream osalpha;
65         osalpha << color[3];
66         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
67     } else {
68         sp_repr_css_set_property(css, "stroke", b);
69         Inkscape::CSSOStringStream osalpha;
70         osalpha << color[3];
71         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
72     }
74     sp_desktop_set_style(desktop, css);
76     sp_repr_css_attr_unref(css);
77 }
79 /**
80  * Apply style on object and children, recursively.
81  */
82 void
83 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
84 {
85     // non-items should not have style
86     if (!SP_IS_ITEM(o))
87         return;
89     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
90     // but must always inherit from the parent text. Same for textPath.
91     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
93     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
94     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
95     // flowtext).
97     if (!(skip_lines
98           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
99               || SP_IS_FLOWDIV(o)
100               || SP_IS_FLOWPARA(o)
101               || SP_IS_TEXTPATH(o))
102           && !SP_OBJECT_REPR(o)->attribute("style"))
103         &&
104         !(SP_IS_FLOWREGION(o) ||
105           SP_IS_FLOWREGIONEXCLUDE(o) ||
106           (SP_IS_USE(o) &&
107            SP_OBJECT_PARENT(o) &&
108            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
109             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
110            )
111           )
112          )
113         ) {
115         SPCSSAttr *css_set = sp_repr_css_attr_new();
116         sp_repr_css_merge(css_set, css);
118         // Scale the style by the inverse of the accumulated parent transform in the paste context.
119         {
120             NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
121             double const ex(NR::expansion(local));
122             if ( ( ex != 0. )
123                  && ( ex != 1. ) ) {
124                 sp_css_attr_scale(css_set, 1/ex);
125             }
126         }
128         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
130         sp_repr_css_attr_unref(css_set);
131     }
133     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
134     if (SP_IS_USE(o))
135         return;
137     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
138         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
139             // Unset properties which are accumulating and thus should not be set recursively.
140             // 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.
141             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
142             sp_repr_css_merge(css_recurse, css);
143             sp_repr_css_set_property(css_recurse, "opacity", NULL);
144             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
145             sp_repr_css_attr_unref(css_recurse);
146         } else {
147             sp_desktop_apply_css_recursive(child, css, skip_lines);
148         }
149     }
152 /**
153  * Apply style on selection on desktop.
154  */
155 void
156 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
158     if (write_current) {
159 // 1. Set internal value
160         sp_repr_css_merge(desktop->current, css);
162 // 1a. Write to prefs; make a copy and unset any URIs first
163         SPCSSAttr *css_write = sp_repr_css_attr_new();
164         sp_repr_css_merge(css_write, css);
165         sp_css_attr_unset_uris(css_write);
166         sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
167         for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
168             /* last used styles for 3D box faces are stored separately */
169             if (SP_IS_BOX3D_SIDE (i->data)) {
170                 const char * descr  = box3d_side_axes_string(SP_BOX3D_SIDE(i->data));
171                 if (descr != NULL) {
172                     gchar *style_grp = g_strconcat ("desktop.", descr, NULL);
173                     sp_repr_css_change(inkscape_get_repr(INKSCAPE, style_grp), css_write, "style");
174                     g_free (style_grp);
175                 }
176             }
177         }
178         sp_repr_css_attr_unref(css_write);
179     }
181     if (!change)
182         return;
184 // 2. Emit signal
185     bool intercepted = desktop->_set_style_signal.emit(css);
187 /** \todo
188  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
189  * rect corners, font size for the object's own transform so that pasting
190  * fills does not depend on preserve/optimize.
191  */
193 // 3. If nobody has intercepted the signal, apply the style to the selection
194     if (!intercepted) {
195         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
196             /// \todo if the style is text-only, apply only to texts?
197             sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
198         }
199     }
202 /**
203  * Return the desktop's current style.
204  */
205 SPCSSAttr *
206 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
208     SPCSSAttr *css = sp_repr_css_attr_new();
209     sp_repr_css_merge(css, desktop->current);
210     if (!css->attributeList()) {
211         sp_repr_css_attr_unref(css);
212         return NULL;
213     } else {
214         if (!with_text) {
215             css = sp_css_attr_unset_text(css);
216         }
217         return css;
218     }
221 /**
222  * Return the desktop's current color.
223  */
224 guint32
225 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
227     guint32 r = 0; // if there's no color, return black
228     gchar const *property = sp_repr_css_property(desktop->current,
229                                                  is_fill ? "fill" : "stroke",
230                                                  "#000");
232     if (desktop->current && property) { // if there is style and the property in it,
233         if (strncmp(property, "url", 3)) { // and if it's not url,
234             // read it
235             r = sp_svg_read_color(property, r);
236         }
237     }
239     return r;
242 double
243 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
245     SPCSSAttr *css = NULL;
246     gfloat value = 1.0; // default if nothing else found
247     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
248         css = sp_desktop_get_style(desktop, true);
249     } else { 
250         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
251         if (tool_repr) {
252             css = sp_repr_css_attr_inherited(tool_repr, "style");
253         }
254     }
255    
256     if (css) {
257         gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
258            
259         if (desktop->current && property) { // if there is style and the property in it,
260             if ( !sp_svg_number_read_f(property, &value) ) {
261                 value = 1.0; // things failed. set back to the default
262             }
263         }
265         sp_repr_css_attr_unref(css);
266     }
268     return value;
270 double
271 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
273     SPCSSAttr *css = NULL;
274     gfloat value = 1.0; // default if nothing else found
275     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
276         css = sp_desktop_get_style(desktop, true);
277     } else { 
278         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
279         if (tool_repr) {
280             css = sp_repr_css_attr_inherited(tool_repr, "style");
281         }
282     }
283    
284     if (css) {
285         gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
286            
287         if (desktop->current && property) { // if there is style and the property in it,
288             if ( !sp_svg_number_read_f(property, &value) ) {
289                 value = 1.0; // things failed. set back to the default
290             }
291         }
293         sp_repr_css_attr_unref(css);
294     }
296     return value;
298 guint32
299 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
301     SPCSSAttr *css = NULL;
302     guint32 r = 0; // if there's no color, return black
303     if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
304         css = sp_desktop_get_style(desktop, true);
305     } else {
306         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
307         if (tool_repr) {
308             css = sp_repr_css_attr_inherited(tool_repr, "style");
309         }
310     }
311    
312     if (css) {
313         gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
314            
315         if (desktop->current && property) { // if there is style and the property in it,
316             if (strncmp(property, "url", 3)) { // and if it's not url,
317                 // read it
318                 r = sp_svg_read_color(property, r);
319             }
320         }
322         sp_repr_css_attr_unref(css);
323     }
325     return r | 0xff;
327 /**
328  * Apply the desktop's current style or the tool style to repr.
329  */
330 void
331 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
333     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
334     if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
335         sp_repr_css_set(repr, css_current, "style");
336     } else {
337         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
338         if (tool_repr) {
339             SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
340             sp_repr_css_set(repr, css, "style");
341             sp_repr_css_attr_unref(css);
342         }
343     }
344     if (css_current) {
345         sp_repr_css_attr_unref(css_current);
346     }
349 /**
350  * Returns the font size (in SVG pixels) of the text tool style (if text
351  * tool uses its own style) or desktop style (otherwise).
352 */
353 double
354 sp_desktop_get_font_size_tool(SPDesktop *desktop)
356     (void)desktop; // TODO cleanup
357     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
358     gchar const *style_str = NULL;
359     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
360         style_str = desktop_style;
361     } else {
362         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
363         if (tool_repr) {
364             style_str = tool_repr->attribute("style");
365         }
366     }
368     double ret = 12;
369     if (style_str) {
370         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
371         sp_style_merge_from_style_string(style, style_str);
372         ret = style->font_size.computed;
373         sp_style_unref(style);
374     }
375     return ret;
378 /** Determine average stroke width, simple method */
379 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
380 gdouble
381 stroke_average_width (GSList const *objects)
383     if (g_slist_length ((GSList *) objects) == 0)
384         return NR_HUGE;
386     gdouble avgwidth = 0.0;
387     bool notstroked = true;
388     int n_notstroked = 0;
390     for (GSList const *l = objects; l != NULL; l = l->next) {
391         if (!SP_IS_ITEM (l->data))
392             continue;
394         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
396         SPObject *object = SP_OBJECT(l->data);
398         if ( object->style->stroke.isNone() ) {
399             ++n_notstroked;   // do not count nonstroked objects
400             continue;
401         } else {
402             notstroked = false;
403         }
405         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
406     }
408     if (notstroked)
409         return NR_HUGE;
411     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
414 /**
415  * Write to style_res the average fill or stroke of list of objects, if applicable.
416  */
417 int
418 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
420     if (g_slist_length(objects) == 0) {
421         /* No objects, set empty */
422         return QUERY_STYLE_NOTHING;
423     }
425     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
426     bool paintImpossible = true;
427     paint_res->set = TRUE;
429     SVGICCColor* iccColor = 0;
430     bool iccSeen = false;
431     gfloat c[4];
432     c[0] = c[1] = c[2] = c[3] = 0.0;
433     gint num = 0;
435     gfloat prev[3];
436     prev[0] = prev[1] = prev[2] = 0.0;
437     bool same_color = true;
439     for (GSList const *i = objects; i != NULL; i = i->next) {
440         SPObject *obj = SP_OBJECT (i->data);
441         SPStyle *style = SP_OBJECT_STYLE (obj);
442         if (!style) continue;
444         SPIPaint *paint = isfill? &style->fill : &style->stroke;
446         // We consider paint "effectively set" for anything within text hierarchy
447         SPObject *parent = SP_OBJECT_PARENT (obj);
448         bool paint_effectively_set =
449             paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
450             || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
451             || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
453         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
455         if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
456             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
457         }
459         if (paint_res->set && paint->set && paint_res->isPaintserver()) {
460             // both previous paint and this paint were a server, see if the servers are compatible
462             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
463             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
465             if (SP_IS_LINEARGRADIENT (server_res)) {
467                 if (!SP_IS_LINEARGRADIENT(server))
468                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
470                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
471                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
472                 if (vector_res != vector)
473                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
475             } else if (SP_IS_RADIALGRADIENT (server_res)) {
477                 if (!SP_IS_RADIALGRADIENT(server))
478                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
480                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
481                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
482                 if (vector_res != vector)
483                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
485             } else if (SP_IS_PATTERN (server_res)) {
487                 if (!SP_IS_PATTERN(server))
488                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
490                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
491                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
492                 if (pat_res != pat)
493                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
494             }
495         }
497         // 2. Sum color, copy server from paint to paint_res
499         if (paint_res->set && paint_effectively_set && paint->isColor()) {
500             gfloat d[3];
501             sp_color_get_rgb_floatv (&paint->value.color, d);
503             // Check if this color is the same as previous
504             if (paintImpossible) {
505                 prev[0] = d[0];
506                 prev[1] = d[1];
507                 prev[2] = d[2];
508                 paint_res->setColor(d[0], d[1], d[2]);
509                 iccColor = paint->value.color.icc;
510                 iccSeen = true;
511             } else {
512                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
513                     same_color = false;
514                 if ( iccSeen ) {
515                     if(paint->value.color.icc) {
516                         // TODO fix this
517                         iccColor = 0;
518                     }
519                 }
520             }
522             // average color
523             c[0] += d[0];
524             c[1] += d[1];
525             c[2] += d[2];
526             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
527             num ++;
528         }
530        paintImpossible = false;
531        paint_res->colorSet = paint->colorSet;
532        paint_res->currentcolor = paint->currentcolor;
533        if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
534            if (isfill) {
535                sp_style_set_to_uri_string (style_res, true, style->getFillURI());
536            } else {
537                sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
538            }
539        }
540        paint_res->set = paint_effectively_set;
541        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
542     }
544     // After all objects processed, divide the color if necessary and return
545     if (paint_res->set && paint_res->isColor()) { // set the color
546         g_assert (num >= 1);
548         c[0] /= num;
549         c[1] /= num;
550         c[2] /= num;
551         c[3] /= num;
552         paint_res->setColor(c[0], c[1], c[2]);
553         if (isfill) {
554             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
555         } else {
556             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
557         }
560         if ( iccSeen && iccColor ) {
561             // TODO check for existing
562             SVGICCColor* tmp = new SVGICCColor(*iccColor);
563             paint_res->value.color.icc = tmp;
564         }
566         if (num > 1) {
567             if (same_color)
568                 return QUERY_STYLE_MULTIPLE_SAME;
569             else
570                 return QUERY_STYLE_MULTIPLE_AVERAGED;
571         } else {
572             return QUERY_STYLE_SINGLE;
573         }
574     }
576     // Not color
577     if (g_slist_length(objects) > 1) {
578         return QUERY_STYLE_MULTIPLE_SAME;
579     } else {
580         return QUERY_STYLE_SINGLE;
581     }
584 /**
585  * Write to style_res the average opacity of a list of objects.
586  */
587 int
588 objects_query_opacity (GSList *objects, SPStyle *style_res)
590     if (g_slist_length(objects) == 0) {
591         /* No objects, set empty */
592         return QUERY_STYLE_NOTHING;
593     }
595     gdouble opacity_sum = 0;
596     gdouble opacity_prev = -1;
597     bool same_opacity = true;
598     guint opacity_items = 0;
601     for (GSList const *i = objects; i != NULL; i = i->next) {
602         SPObject *obj = SP_OBJECT (i->data);
603         SPStyle *style = SP_OBJECT_STYLE (obj);
604         if (!style) continue;
606         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
607         opacity_sum += opacity;
608         if (opacity_prev != -1 && opacity != opacity_prev)
609             same_opacity = false;
610         opacity_prev = opacity;
611         opacity_items ++;
612     }
613     if (opacity_items > 1)
614         opacity_sum /= opacity_items;
616     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
618     if (opacity_items == 0) {
619         return QUERY_STYLE_NOTHING;
620     } else if (opacity_items == 1) {
621         return QUERY_STYLE_SINGLE;
622     } else {
623         if (same_opacity)
624             return QUERY_STYLE_MULTIPLE_SAME;
625         else
626             return QUERY_STYLE_MULTIPLE_AVERAGED;
627     }
630 /**
631  * Write to style_res the average stroke width of a list of objects.
632  */
633 int
634 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
636     if (g_slist_length(objects) == 0) {
637         /* No objects, set empty */
638         return QUERY_STYLE_NOTHING;
639     }
641     gdouble avgwidth = 0.0;
643     gdouble prev_sw = -1;
644     bool same_sw = true;
646     int n_stroked = 0;
648     for (GSList const *i = objects; i != NULL; i = i->next) {
649         SPObject *obj = SP_OBJECT (i->data);
650         if (!SP_IS_ITEM(obj)) continue;
651         SPStyle *style = SP_OBJECT_STYLE (obj);
652         if (!style) continue;
654         if ( style->stroke.isNone() ) {
655             continue;
656         }
658         n_stroked ++;
660         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
661         double sw = style->stroke_width.computed * i2d.expansion();
663         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
664             same_sw = false;
665         prev_sw = sw;
667         avgwidth += sw;
668     }
670     if (n_stroked > 1)
671         avgwidth /= (n_stroked);
673     style_res->stroke_width.computed = avgwidth;
674     style_res->stroke_width.set = true;
676     if (n_stroked == 0) {
677         return QUERY_STYLE_NOTHING;
678     } else if (n_stroked == 1) {
679         return QUERY_STYLE_SINGLE;
680     } else {
681         if (same_sw)
682             return QUERY_STYLE_MULTIPLE_SAME;
683         else
684             return QUERY_STYLE_MULTIPLE_AVERAGED;
685     }
688 /**
689  * Write to style_res the average miter limit of a list of objects.
690  */
691 int
692 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
694     if (g_slist_length(objects) == 0) {
695         /* No objects, set empty */
696         return QUERY_STYLE_NOTHING;
697     }
699     gdouble avgml = 0.0;
700     int n_stroked = 0;
702     gdouble prev_ml = -1;
703     bool same_ml = true;
705     for (GSList const *i = objects; i != NULL; i = i->next) {
706         SPObject *obj = SP_OBJECT (i->data);
707         if (!SP_IS_ITEM(obj)) continue;
708         SPStyle *style = SP_OBJECT_STYLE (obj);
709         if (!style) continue;
711         if ( style->stroke.isNone() ) {
712             continue;
713         }
715         n_stroked ++;
717         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
718             same_ml = false;
719         prev_ml = style->stroke_miterlimit.value;
721         avgml += style->stroke_miterlimit.value;
722     }
724     if (n_stroked > 1)
725         avgml /= (n_stroked);
727     style_res->stroke_miterlimit.value = avgml;
728     style_res->stroke_miterlimit.set = true;
730     if (n_stroked == 0) {
731         return QUERY_STYLE_NOTHING;
732     } else if (n_stroked == 1) {
733         return QUERY_STYLE_SINGLE;
734     } else {
735         if (same_ml)
736             return QUERY_STYLE_MULTIPLE_SAME;
737         else
738             return QUERY_STYLE_MULTIPLE_AVERAGED;
739     }
742 /**
743  * Write to style_res the stroke cap of a list of objects.
744  */
745 int
746 objects_query_strokecap (GSList *objects, SPStyle *style_res)
748     if (g_slist_length(objects) == 0) {
749         /* No objects, set empty */
750         return QUERY_STYLE_NOTHING;
751     }
753     int cap = -1;
754     gdouble prev_cap = -1;
755     bool same_cap = true;
756     int n_stroked = 0;
758     for (GSList const *i = objects; i != NULL; i = i->next) {
759         SPObject *obj = SP_OBJECT (i->data);
760         if (!SP_IS_ITEM(obj)) continue;
761         SPStyle *style = SP_OBJECT_STYLE (obj);
762         if (!style) continue;
764         if ( style->stroke.isNone() ) {
765             continue;
766         }
768         n_stroked ++;
770         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
771             same_cap = false;
772         prev_cap = style->stroke_linecap.value;
774         cap = style->stroke_linecap.value;
775     }
777     style_res->stroke_linecap.value = cap;
778     style_res->stroke_linecap.set = true;
780     if (n_stroked == 0) {
781         return QUERY_STYLE_NOTHING;
782     } else if (n_stroked == 1) {
783         return QUERY_STYLE_SINGLE;
784     } else {
785         if (same_cap)
786             return QUERY_STYLE_MULTIPLE_SAME;
787         else
788             return QUERY_STYLE_MULTIPLE_DIFFERENT;
789     }
792 /**
793  * Write to style_res the stroke join of a list of objects.
794  */
795 int
796 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
798     if (g_slist_length(objects) == 0) {
799         /* No objects, set empty */
800         return QUERY_STYLE_NOTHING;
801     }
803     int join = -1;
804     gdouble prev_join = -1;
805     bool same_join = true;
806     int n_stroked = 0;
808     for (GSList const *i = objects; i != NULL; i = i->next) {
809         SPObject *obj = SP_OBJECT (i->data);
810         if (!SP_IS_ITEM(obj)) continue;
811         SPStyle *style = SP_OBJECT_STYLE (obj);
812         if (!style) continue;
814         if ( style->stroke.isNone() ) {
815             continue;
816         }
818         n_stroked ++;
820         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
821             same_join = false;
822         prev_join = style->stroke_linejoin.value;
824         join = style->stroke_linejoin.value;
825     }
827     style_res->stroke_linejoin.value = join;
828     style_res->stroke_linejoin.set = true;
830     if (n_stroked == 0) {
831         return QUERY_STYLE_NOTHING;
832     } else if (n_stroked == 1) {
833         return QUERY_STYLE_SINGLE;
834     } else {
835         if (same_join)
836             return QUERY_STYLE_MULTIPLE_SAME;
837         else
838             return QUERY_STYLE_MULTIPLE_DIFFERENT;
839     }
842 /**
843  * Write to style_res the average font size and spacing of objects.
844  */
845 int
846 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
848     bool different = false;
850     double size = 0;
851     double letterspacing = 0;
852     double linespacing = 0;
853     bool linespacing_normal = false;
854     bool letterspacing_normal = false;
856     double size_prev = 0;
857     double letterspacing_prev = 0;
858     double linespacing_prev = 0;
860     /// \todo FIXME: add word spacing, kerns? rotates?
862     int texts = 0;
864     for (GSList const *i = objects; i != NULL; i = i->next) {
865         SPObject *obj = SP_OBJECT (i->data);
867         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
868             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
869             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
870             continue;
872         SPStyle *style = SP_OBJECT_STYLE (obj);
873         if (!style) continue;
875         texts ++;
876         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
878         if (style->letter_spacing.normal) {
879             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
880                 letterspacing_normal = true;
881         } else {
882             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
883             letterspacing_normal = false;
884         }
886         double linespacing_current;
887         if (style->line_height.normal) {
888             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
889             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
890                 linespacing_normal = true;
891         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
892             linespacing_current = style->line_height.value;
893             linespacing_normal = false;
894         } else { // we need % here
895             linespacing_current = style->line_height.computed / style->font_size.computed;
896             linespacing_normal = false;
897         }
898         linespacing += linespacing_current;
900         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
901             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
902             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
903             different = true;
904         }
906         size_prev = style->font_size.computed;
907         letterspacing_prev = style->letter_spacing.computed;
908         linespacing_prev = linespacing_current;
910         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
911         style_res->text_anchor.computed = style->text_anchor.computed;
912         style_res->writing_mode.computed = style->writing_mode.computed;
913     }
915     if (texts == 0)
916         return QUERY_STYLE_NOTHING;
918     if (texts > 1) {
919         size /= texts;
920         letterspacing /= texts;
921         linespacing /= texts;
922     }
924     style_res->font_size.computed = size;
925     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
927     style_res->letter_spacing.normal = letterspacing_normal;
928     style_res->letter_spacing.computed = letterspacing;
930     style_res->line_height.normal = linespacing_normal;
931     style_res->line_height.computed = linespacing;
932     style_res->line_height.value = linespacing;
933     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
935     if (texts > 1) {
936         if (different) {
937             return QUERY_STYLE_MULTIPLE_AVERAGED;
938         } else {
939             return QUERY_STYLE_MULTIPLE_SAME;
940         }
941     } else {
942         return QUERY_STYLE_SINGLE;
943     }
946 /**
947  * Write to style_res the average font style of objects.
948  */
949 int
950 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
952     bool different = false;
953     bool set = false;
955     int texts = 0;
957     for (GSList const *i = objects; i != NULL; i = i->next) {
958         SPObject *obj = SP_OBJECT (i->data);
960         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
961             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
962             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
963             continue;
965         SPStyle *style = SP_OBJECT_STYLE (obj);
966         if (!style) continue;
968         texts ++;
970         if (set &&
971             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
972             different = true;  // different styles
973         }
975         set = TRUE;
976         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
977         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
978         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
979         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
980         style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
981     }
983     if (texts == 0 || !set)
984         return QUERY_STYLE_NOTHING;
986     if (texts > 1) {
987         if (different) {
988             return QUERY_STYLE_MULTIPLE_DIFFERENT;
989         } else {
990             return QUERY_STYLE_MULTIPLE_SAME;
991         }
992     } else {
993         return QUERY_STYLE_SINGLE;
994     }
997 /**
998  * Write to style_res the average font family of objects.
999  */
1000 int
1001 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1003     bool different = false;
1004     int texts = 0;
1006     if (style_res->text->font_family.value) {
1007         g_free(style_res->text->font_family.value);
1008         style_res->text->font_family.value = NULL;
1009     }
1010     style_res->text->font_family.set = FALSE;
1012     for (GSList const *i = objects; i != NULL; i = i->next) {
1013         SPObject *obj = SP_OBJECT (i->data);
1015         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1016             && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1017             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1018             continue;
1020         SPStyle *style = SP_OBJECT_STYLE (obj);
1021         if (!style) continue;
1023         texts ++;
1025         if (style_res->text->font_family.value && style->text->font_family.value &&
1026             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1027             different = true;  // different fonts
1028         }
1030         if (style_res->text->font_family.value) {
1031             g_free(style_res->text->font_family.value);
1032             style_res->text->font_family.value = NULL;
1033         }
1035         style_res->text->font_family.set = TRUE;
1036         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1037     }
1039     if (texts == 0 || !style_res->text->font_family.set)
1040         return QUERY_STYLE_NOTHING;
1042     if (texts > 1) {
1043         if (different) {
1044             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1045         } else {
1046             return QUERY_STYLE_MULTIPLE_SAME;
1047         }
1048     } else {
1049         return QUERY_STYLE_SINGLE;
1050     }
1053 int
1054 objects_query_blend (GSList *objects, SPStyle *style_res)
1056     const int empty_prev = -2;
1057     const int complex_filter = 5;
1058     int blend = 0;
1059     float blend_prev = empty_prev;
1060     bool same_blend = true;
1061     guint items = 0;
1062     
1063     for (GSList const *i = objects; i != NULL; i = i->next) {
1064         SPObject *obj = SP_OBJECT (i->data);
1065         SPStyle *style = SP_OBJECT_STYLE (obj);
1066         if(!style || !SP_IS_ITEM(obj)) continue;
1068         items++;
1070         //if object has a filter
1071         if (style->filter.set && style->getFilter()) {
1072             int blurcount = 0;
1073             int blendcount = 0;
1075             // determine whether filter is simple (blend and/or blur) or complex
1076             for(SPObject *primitive_obj = style->getFilter()->children;
1077                 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1078                 primitive_obj = primitive_obj->next) {
1079                 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1080                 if(SP_IS_FEBLEND(primitive))
1081                     ++blendcount;
1082                 else if(SP_IS_GAUSSIANBLUR(primitive))
1083                     ++blurcount;
1084                 else {
1085                     blurcount = complex_filter;
1086                     break;
1087                 }
1088             }
1090             // simple filter
1091             if(blurcount == 1 || blendcount == 1) {
1092                 for(SPObject *primitive_obj = style->getFilter()->children;
1093                     primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1094                     primitive_obj = primitive_obj->next) {
1095                     if(SP_IS_FEBLEND(primitive_obj)) {
1096                         SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1097                         blend = spblend->blend_mode;
1098                     }
1099                 }
1100             }
1101             else {
1102                 blend = complex_filter;
1103             }
1104         }
1105         // defaults to blend mode = "normal"
1106         else {
1107             blend = 0;
1108         }
1110         if(blend_prev != empty_prev && blend_prev != blend)
1111             same_blend = false;
1112         blend_prev = blend;
1113     }
1115     if (items > 0) {
1116         style_res->filter_blend_mode.value = blend;
1117     }
1119     if (items == 0) {
1120         return QUERY_STYLE_NOTHING;
1121     } else if (items == 1) {
1122         return QUERY_STYLE_SINGLE;
1123     } else {
1124         if(same_blend)
1125             return QUERY_STYLE_MULTIPLE_SAME;
1126         else
1127             return QUERY_STYLE_MULTIPLE_DIFFERENT;
1128     }
1131 /**
1132  * Write to style_res the average blurring of a list of objects.
1133  */
1134 int
1135 objects_query_blur (GSList *objects, SPStyle *style_res)
1137    if (g_slist_length(objects) == 0) {
1138         /* No objects, set empty */
1139         return QUERY_STYLE_NOTHING;
1140     }
1142     float blur_sum = 0;
1143     float blur_prev = -1;
1144     bool same_blur = true;
1145     guint blur_items = 0;
1146     guint items = 0;
1147     
1148     for (GSList const *i = objects; i != NULL; i = i->next) {
1149         SPObject *obj = SP_OBJECT (i->data);
1150         SPStyle *style = SP_OBJECT_STYLE (obj);
1151         if (!style) continue;
1152         if (!SP_IS_ITEM(obj)) continue;
1154         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1156         items ++;
1158         //if object has a filter
1159         if (style->filter.set && style->getFilter()) {
1160             //cycle through filter primitives
1161             SPObject *primitive_obj = style->getFilter()->children;
1162             while (primitive_obj) {
1163                 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1164                     SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1165                     
1166                     //if primitive is gaussianblur
1167                     if(SP_IS_GAUSSIANBLUR(primitive)) {
1168                         SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1169                         float num = spblur->stdDeviation.getNumber();
1170                         blur_sum += num * NR::expansion(i2d);
1171                         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
1172                             same_blur = false;
1173                         blur_prev = num;
1174                         //TODO: deal with opt number, for the moment it's not necessary to the ui.
1175                         blur_items ++;
1176                     }
1177                 }
1178                 primitive_obj = primitive_obj->next;
1179             }
1180         }
1181     }
1183     if (items > 0) {
1184         if (blur_items > 0)
1185             blur_sum /= blur_items;
1186         style_res->filter_gaussianBlur_deviation.value = blur_sum;
1187     }
1189     if (items == 0) {
1190         return QUERY_STYLE_NOTHING;
1191     } else if (items == 1) {
1192         return QUERY_STYLE_SINGLE;
1193     } else {
1194         if (same_blur)
1195             return QUERY_STYLE_MULTIPLE_SAME;
1196         else
1197             return QUERY_STYLE_MULTIPLE_AVERAGED;
1198     }
1201 /**
1202  * Query the given list of objects for the given property, write
1203  * the result to style, return appropriate flag.
1204  */
1205 int
1206 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1208     if (property == QUERY_STYLE_PROPERTY_FILL) {
1209         return objects_query_fillstroke (list, style, true);
1210     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1211         return objects_query_fillstroke (list, style, false);
1213     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1214         return objects_query_strokewidth (list, style);
1215     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1216         return objects_query_miterlimit (list, style);
1217     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1218         return objects_query_strokecap (list, style);
1219     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1220         return objects_query_strokejoin (list, style);
1222     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1223         return objects_query_opacity (list, style);
1225     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1226         return objects_query_fontfamily (list, style);
1227     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1228         return objects_query_fontstyle (list, style);
1229     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1230         return objects_query_fontnumbers (list, style);
1232     } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1233         return objects_query_blend (list, style);
1234     } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1235         return objects_query_blur (list, style);
1236     }
1237     return QUERY_STYLE_NOTHING;
1241 /**
1242  * Query the subselection (if any) or selection on the given desktop for the given property, write
1243  * the result to style, return appropriate flag.
1244  */
1245 int
1246 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1248     int ret = desktop->_query_style_signal.emit(style, property);
1250     if (ret != QUERY_STYLE_NOTHING)
1251         return ret; // subselection returned a style, pass it on
1253     // otherwise, do querying and averaging over selection
1254     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1257 /**
1258  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1259  * the properties returned QUERY_STYLE_NOTHING.
1260  */
1261 bool
1262 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1264         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1265         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1266         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1267         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1268         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1269         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1270         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1271         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1272         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1273         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1274         int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1275         
1276         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);
1280 /*
1281   Local Variables:
1282   mode:c++
1283   c-file-style:"stroustrup"
1284   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1285   indent-tabs-mode:nil
1286   fill-column:99
1287   End:
1288 */
1289 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :