Code

afb3a104528f62dff82213ee53114ba488f13771
[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-flowtext.h"
28 #include "sp-flowregion.h"
29 #include "sp-flowdiv.h"
30 #include "sp-linear-gradient.h"
31 #include "sp-radial-gradient.h"
32 #include "sp-pattern.h"
33 #include "xml/repr.h"
34 #include "libnrtype/font-style-to-pos.h"
37 #include "desktop-style.h"
39 /**
40  * Set color on selection on desktop.
41  */
42 void
43 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
44 {
45     /// \todo relative color setting
46     if (is_relative) {
47         g_warning("FIXME: relative color setting not yet implemented");
48         return;
49     }
51     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
52     gchar b[64];
53     sp_svg_write_color(b, 64, rgba);
54     SPCSSAttr *css = sp_repr_css_attr_new();
55     if (fill) {
56         sp_repr_css_set_property(css, "fill", b);
57         Inkscape::CSSOStringStream osalpha;
58         osalpha << color[3];
59         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
60     } else {
61         sp_repr_css_set_property(css, "stroke", b);
62         Inkscape::CSSOStringStream osalpha;
63         osalpha << color[3];
64         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
65     }
67     sp_desktop_set_style(desktop, css);
69     sp_repr_css_attr_unref(css);
70 }
72 /**
73  * Apply style on object and children, recursively.
74  */
75 void
76 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
77 {
78     // non-items should not have style
79     if (!SP_IS_ITEM(o))
80         return;
82     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
83     // but must always inherit from the parent text. Same for textPath.
84     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
86     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
87     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
88     // flowtext).
90     if (!(skip_lines
91           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
92               || SP_IS_FLOWDIV(o)
93               || SP_IS_FLOWPARA(o)
94               || SP_IS_TEXTPATH(o))
95           && !SP_OBJECT_REPR(o)->attribute("style"))
96         &&
97         !(SP_IS_FLOWREGION(o) ||
98           SP_IS_FLOWREGIONEXCLUDE(o) ||
99           (SP_IS_USE(o) &&
100            SP_OBJECT_PARENT(o) &&
101            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
102             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
103            )
104           )
105          )
106         ) {
108         SPCSSAttr *css_set = sp_repr_css_attr_new();
109         sp_repr_css_merge(css_set, css);
111         // Scale the style by the inverse of the accumulated parent transform in the paste context.
112         {
113             NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
114             double const ex(NR::expansion(local));
115             if ( ( ex != 0. )
116                  && ( ex != 1. ) ) {
117                 sp_css_attr_scale(css_set, 1/ex);
118             }
119         }
121         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
123         sp_repr_css_attr_unref(css_set);
124     }
126     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
127     if (SP_IS_USE(o))
128         return;
130     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
131         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
132             // Unset properties which are accumulating and thus should not be set recursively.
133             // 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.
134             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
135             sp_repr_css_merge(css_recurse, css);
136             sp_repr_css_set_property(css_recurse, "opacity", NULL);
137             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
138             sp_repr_css_attr_unref(css_recurse);
139         } else {
140             sp_desktop_apply_css_recursive(child, css, skip_lines);
141         }
142     }
145 /**
146  * Apply style on selection on desktop.
147  */
148 void
149 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
151     if (write_current) {
152 // 1. Set internal value
153         sp_repr_css_merge(desktop->current, css);
155 // 1a. Write to prefs; make a copy and unset any URIs first
156         SPCSSAttr *css_write = sp_repr_css_attr_new();
157         sp_repr_css_merge(css_write, css);
158         sp_css_attr_unset_uris(css_write);
159         sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
160         sp_repr_css_attr_unref(css_write);
161     }
163     if (!change)
164         return;
166 // 2. Emit signal
167     bool intercepted = desktop->_set_style_signal.emit(css);
169 /** \todo
170  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
171  * rect corners, font size for the object's own transform so that pasting
172  * fills does not depend on preserve/optimize.
173  */
175 // 3. If nobody has intercepted the signal, apply the style to the selection
176     if (!intercepted) {
177         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
178             /// \todo if the style is text-only, apply only to texts?
179             sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
180         }
181     }
184 /**
185  * Return the desktop's current style.
186  */
187 SPCSSAttr *
188 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
190     SPCSSAttr *css = sp_repr_css_attr_new();
191     sp_repr_css_merge(css, desktop->current);
192     if (!css->attributeList()) {
193         sp_repr_css_attr_unref(css);
194         return NULL;
195     } else {
196         if (!with_text) {
197             css = sp_css_attr_unset_text(css);
198         }
199         return css;
200     }
203 /**
204  * Return the desktop's current color.
205  */
206 guint32
207 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
209     guint32 r = 0; // if there's no color, return black
210     gchar const *property = sp_repr_css_property(desktop->current,
211                                                  is_fill ? "fill" : "stroke",
212                                                  "#000");
214     if (desktop->current && property) { // if there is style and the property in it,
215         if (strncmp(property, "url", 3)) { // and if it's not url,
216             // read it
217             r = sp_svg_read_color(property, r);
218         }
219     }
221     return r;
224 double
225 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool op)
227     SPCSSAttr *css = NULL;
228     gfloat value = 1.0; // default if nothing else found
229     if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
230         css = sp_desktop_get_style(desktop, op);
231     } else { 
232         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
233         css = sp_repr_css_attr_inherited(tool_repr, "style");
234     }
235    
236     gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
237            
238     if (desktop->current && property) { // if there is style and the property in it,
239         if ( !sp_svg_number_read_f(property, &value) ) {
240             value = 1.0; // things failed. set back to the default
241         }
242     }
243     return value;
245 guint32
246 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
248     SPCSSAttr *css = NULL;
249     guint32 r = 0; // if there's no color, return black
250     bool op = true;
251     double opacity = sp_desktop_get_opacity_tool(SP_ACTIVE_DESKTOP, "tools.calligraphic", op);
252     if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
253         css = sp_desktop_get_style(desktop, is_fill);
254     } else {
255         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
256         css = sp_repr_css_attr_inherited(tool_repr, "style");
257     }
258    
259     gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
260            
261     if (desktop->current && property) { // if there is style and the property in it,
262         if (strncmp(property, "url", 3)) { // and if it's not url,
263             // read it
264             r = sp_svg_read_color(property, r);
265         }
266     }
267     return r | SP_COLOR_F_TO_U( opacity ); // return RGBA color
269 /**
270  * Apply the desktop's current style or the tool style to repr.
271  */
272 void
273 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
275     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
276     if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
277         sp_repr_css_set(repr, css_current, "style");
278     } else {
279         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
280         if (tool_repr) {
281             SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
282             sp_repr_css_set(repr, css, "style");
283             sp_repr_css_attr_unref(css);
284         }
285     }
286     if (css_current) {sp_repr_css_attr_unref(css_current);
287     }
290 /**
291  * Returns the font size (in SVG pixels) of the text tool style (if text
292  * tool uses its own style) or desktop style (otherwise).
293 */
294 double
295 sp_desktop_get_font_size_tool(SPDesktop *desktop)
297     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
298     gchar const *style_str = NULL;
299     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
300         style_str = desktop_style;
301     } else {
302         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
303         if (tool_repr) {
304             style_str = tool_repr->attribute("style");
305         }
306     }
308     double ret = 12;
309     if (style_str) {
310         SPStyle *style = sp_style_new();
311         sp_style_merge_from_style_string(style, style_str);
312         ret = style->font_size.computed;
313         sp_style_unref(style);
314     }
315     return ret;
318 /** Determine average stroke width, simple method */
319 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
320 gdouble
321 stroke_average_width (GSList const *objects)
323     if (g_slist_length ((GSList *) objects) == 0)
324         return NR_HUGE;
326     gdouble avgwidth = 0.0;
327     bool notstroked = true;
328     int n_notstroked = 0;
330     for (GSList const *l = objects; l != NULL; l = l->next) {
331         if (!SP_IS_ITEM (l->data))
332             continue;
334         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
336         SPObject *object = SP_OBJECT(l->data);
338         if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
339             ++n_notstroked;   // do not count nonstroked objects
340             continue;
341         } else {
342             notstroked = false;
343         }
345         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
346     }
348     if (notstroked)
349         return NR_HUGE;
351     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
354 /**
355  * Write to style_res the average fill or stroke of list of objects, if applicable.
356  */
357 int
358 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
360     if (g_slist_length(objects) == 0) {
361         /* No objects, set empty */
362         return QUERY_STYLE_NOTHING;
363     }
365     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
366     paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
367     paint_res->set = TRUE;
369     gfloat c[4];
370     c[0] = c[1] = c[2] = c[3] = 0.0;
371     gint num = 0;
373     gfloat prev[4];
374     prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
375     bool same_color = true;
377     for (GSList const *i = objects; i != NULL; i = i->next) {
378         SPObject *obj = SP_OBJECT (i->data);
379         SPStyle *style = SP_OBJECT_STYLE (obj);
380         if (!style) continue;
382         SPIPaint *paint = isfill? &style->fill : &style->stroke;
384         // We consider paint "effectively set" for anything within text hierarchy
385         SPObject *parent = SP_OBJECT_PARENT (obj);
386         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));
388         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
390         if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
391             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
392         }
394         if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
395             // both previous paint and this paint were a server, see if the servers are compatible
397             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
398             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
400             if (SP_IS_LINEARGRADIENT (server_res)) {
402                 if (!SP_IS_LINEARGRADIENT(server))
403                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
405                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
406                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
407                 if (vector_res != vector)
408                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
410             } else if (SP_IS_RADIALGRADIENT (server_res)) {
412                 if (!SP_IS_RADIALGRADIENT(server))
413                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
415                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
416                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
417                 if (vector_res != vector)
418                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
420             } else if (SP_IS_PATTERN (server_res)) {
422                 if (!SP_IS_PATTERN(server))
423                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
425                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
426                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
427                 if (pat_res != pat)
428                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
429             }
430         }
432         // 2. Sum color, copy server from paint to paint_res
434         if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
436             gfloat d[3];
437             sp_color_get_rgb_floatv (&paint->value.color, d);
439             // Check if this color is the same as previous
440             if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
441                 prev[0] = d[0];
442                 prev[1] = d[1];
443                 prev[2] = d[2];
444                 prev[3] = d[3];
445             } else {
446                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
447                     same_color = false;
448             }
450             // average color
451             c[0] += d[0];
452             c[1] += d[1];
453             c[2] += d[2];
454             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
455             num ++;
456         }
458        if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
459            if (isfill) {
460                SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
461            } else {
462                SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
463            }
464        }
465        paint_res->type = paint->type;
466        paint_res->set = paint_effectively_set;
467        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
468     }
470     // After all objects processed, divide the color if necessary and return
471     if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
472         g_assert (num >= 1);
474         c[0] /= num;
475         c[1] /= num;
476         c[2] /= num;
477         c[3] /= num;
478         sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
479         if (isfill) {
480             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
481         } else {
482             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
483         }
484         if (num > 1) {
485             if (same_color)
486                 return QUERY_STYLE_MULTIPLE_SAME;
487             else
488                 return QUERY_STYLE_MULTIPLE_AVERAGED;
489         } else {
490             return QUERY_STYLE_SINGLE;
491         }
492     }
494     // Not color
495     if (g_slist_length(objects) > 1) {
496         return QUERY_STYLE_MULTIPLE_SAME;
497     } else {
498         return QUERY_STYLE_SINGLE;
499     }
502 /**
503  * Write to style_res the average opacity of a list of objects.
504  */
505 int
506 objects_query_opacity (GSList *objects, SPStyle *style_res)
508     if (g_slist_length(objects) == 0) {
509         /* No objects, set empty */
510         return QUERY_STYLE_NOTHING;
511     }
513     gdouble opacity_sum = 0;
514     gdouble opacity_prev = -1;
515     bool same_opacity = true;
516     guint opacity_items = 0;
519     for (GSList const *i = objects; i != NULL; i = i->next) {
520         SPObject *obj = SP_OBJECT (i->data);
521         SPStyle *style = SP_OBJECT_STYLE (obj);
522         if (!style) continue;
524         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
525         opacity_sum += opacity;
526         if (opacity_prev != -1 && opacity != opacity_prev)
527             same_opacity = false;
528         opacity_prev = opacity;
529         opacity_items ++;
530     }
531     if (opacity_items > 1)
532         opacity_sum /= opacity_items;
534     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
536     if (opacity_items == 0) {
537         return QUERY_STYLE_NOTHING;
538     } else if (opacity_items == 1) {
539         return QUERY_STYLE_SINGLE;
540     } else {
541         if (same_opacity)
542             return QUERY_STYLE_MULTIPLE_SAME;
543         else
544             return QUERY_STYLE_MULTIPLE_AVERAGED;
545     }
548 /**
549  * Write to style_res the average stroke width of a list of objects.
550  */
551 int
552 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
554     if (g_slist_length(objects) == 0) {
555         /* No objects, set empty */
556         return QUERY_STYLE_NOTHING;
557     }
559     gdouble avgwidth = 0.0;
561     gdouble prev_sw = -1;
562     bool same_sw = true;
564     int n_stroked = 0;
566     for (GSList const *i = objects; i != NULL; i = i->next) {
567         SPObject *obj = SP_OBJECT (i->data);
568         if (!SP_IS_ITEM(obj)) continue;
569         SPStyle *style = SP_OBJECT_STYLE (obj);
570         if (!style) continue;
572         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
573             continue;
574         }
576         n_stroked ++;
578         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
579         double sw = style->stroke_width.computed * i2d.expansion();
581         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
582             same_sw = false;
583         prev_sw = sw;
585         avgwidth += sw;
586     }
588     if (n_stroked > 1)
589         avgwidth /= (n_stroked);
591     style_res->stroke_width.computed = avgwidth;
592     style_res->stroke_width.set = true;
594     if (n_stroked == 0) {
595         return QUERY_STYLE_NOTHING;
596     } else if (n_stroked == 1) {
597         return QUERY_STYLE_SINGLE;
598     } else {
599         if (same_sw)
600             return QUERY_STYLE_MULTIPLE_SAME;
601         else
602             return QUERY_STYLE_MULTIPLE_AVERAGED;
603     }
606 /**
607  * Write to style_res the average miter limit of a list of objects.
608  */
609 int
610 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
612     if (g_slist_length(objects) == 0) {
613         /* No objects, set empty */
614         return QUERY_STYLE_NOTHING;
615     }
617     gdouble avgml = 0.0;
618     int n_stroked = 0;
620     gdouble prev_ml = -1;
621     bool same_ml = true;
623     for (GSList const *i = objects; i != NULL; i = i->next) {
624         SPObject *obj = SP_OBJECT (i->data);
625         if (!SP_IS_ITEM(obj)) continue;
626         SPStyle *style = SP_OBJECT_STYLE (obj);
627         if (!style) continue;
629         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
630             continue;
631         }
633         n_stroked ++;
635         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
636             same_ml = false;
637         prev_ml = style->stroke_miterlimit.value;
639         avgml += style->stroke_miterlimit.value;
640     }
642     if (n_stroked > 1)
643         avgml /= (n_stroked);
645     style_res->stroke_miterlimit.value = avgml;
646     style_res->stroke_miterlimit.set = true;
648     if (n_stroked == 0) {
649         return QUERY_STYLE_NOTHING;
650     } else if (n_stroked == 1) {
651         return QUERY_STYLE_SINGLE;
652     } else {
653         if (same_ml)
654             return QUERY_STYLE_MULTIPLE_SAME;
655         else
656             return QUERY_STYLE_MULTIPLE_AVERAGED;
657     }
660 /**
661  * Write to style_res the stroke cap of a list of objects.
662  */
663 int
664 objects_query_strokecap (GSList *objects, SPStyle *style_res)
666     if (g_slist_length(objects) == 0) {
667         /* No objects, set empty */
668         return QUERY_STYLE_NOTHING;
669     }
671     int cap = -1;
672     gdouble prev_cap = -1;
673     bool same_cap = true;
674     int n_stroked = 0;
676     for (GSList const *i = objects; i != NULL; i = i->next) {
677         SPObject *obj = SP_OBJECT (i->data);
678         if (!SP_IS_ITEM(obj)) continue;
679         SPStyle *style = SP_OBJECT_STYLE (obj);
680         if (!style) continue;
682         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
683             continue;
684         }
686         n_stroked ++;
688         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
689             same_cap = false;
690         prev_cap = style->stroke_linecap.value;
692         cap = style->stroke_linecap.value;
693     }
695     style_res->stroke_linecap.value = cap;
696     style_res->stroke_linecap.set = true;
698     if (n_stroked == 0) {
699         return QUERY_STYLE_NOTHING;
700     } else if (n_stroked == 1) {
701         return QUERY_STYLE_SINGLE;
702     } else {
703         if (same_cap)
704             return QUERY_STYLE_MULTIPLE_SAME;
705         else
706             return QUERY_STYLE_MULTIPLE_DIFFERENT;
707     }
710 /**
711  * Write to style_res the stroke join of a list of objects.
712  */
713 int
714 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
716     if (g_slist_length(objects) == 0) {
717         /* No objects, set empty */
718         return QUERY_STYLE_NOTHING;
719     }
721     int join = -1;
722     gdouble prev_join = -1;
723     bool same_join = true;
724     int n_stroked = 0;
726     for (GSList const *i = objects; i != NULL; i = i->next) {
727         SPObject *obj = SP_OBJECT (i->data);
728         if (!SP_IS_ITEM(obj)) continue;
729         SPStyle *style = SP_OBJECT_STYLE (obj);
730         if (!style) continue;
732         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
733             continue;
734         }
736         n_stroked ++;
738         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
739             same_join = false;
740         prev_join = style->stroke_linejoin.value;
742         join = style->stroke_linejoin.value;
743     }
745     style_res->stroke_linejoin.value = join;
746     style_res->stroke_linejoin.set = true;
748     if (n_stroked == 0) {
749         return QUERY_STYLE_NOTHING;
750     } else if (n_stroked == 1) {
751         return QUERY_STYLE_SINGLE;
752     } else {
753         if (same_join)
754             return QUERY_STYLE_MULTIPLE_SAME;
755         else
756             return QUERY_STYLE_MULTIPLE_DIFFERENT;
757     }
760 /**
761  * Write to style_res the average font size and spacing of objects.
762  */
763 int
764 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
766     bool different = false;
768     double size = 0;
769     double letterspacing = 0;
770     double linespacing = 0;
771     bool linespacing_normal = false;
772     bool letterspacing_normal = false;
774     double size_prev = 0;
775     double letterspacing_prev = 0;
776     double linespacing_prev = 0;
778     /// \todo FIXME: add word spacing, kerns? rotates?
780     int texts = 0;
782     for (GSList const *i = objects; i != NULL; i = i->next) {
783         SPObject *obj = SP_OBJECT (i->data);
785         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
786             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
787             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
788             continue;
790         SPStyle *style = SP_OBJECT_STYLE (obj);
791         if (!style) continue;
793         texts ++;
794         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
796         if (style->letter_spacing.normal) {
797             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
798                 letterspacing_normal = true;
799         } else {
800             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
801             letterspacing_normal = false;
802         }
804         double linespacing_current;
805         if (style->line_height.normal) {
806             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
807             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
808                 linespacing_normal = true;
809         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
810             linespacing_current = style->line_height.value;
811             linespacing_normal = false;
812         } else { // we need % here
813             linespacing_current = style->line_height.computed / style->font_size.computed;
814             linespacing_normal = false;
815         }
816         linespacing += linespacing_current;
818         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
819             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
820             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
821             different = true;
822         }
824         size_prev = style->font_size.computed;
825         letterspacing_prev = style->letter_spacing.computed;
826         linespacing_prev = linespacing_current;
828         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
829         style_res->text_anchor.computed = style->text_anchor.computed;
830         style_res->writing_mode.computed = style->writing_mode.computed;
831     }
833     if (texts == 0)
834         return QUERY_STYLE_NOTHING;
836     if (texts > 1) {
837         size /= texts;
838         letterspacing /= texts;
839         linespacing /= texts;
840     }
842     style_res->font_size.computed = size;
843     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
845     style_res->letter_spacing.normal = letterspacing_normal;
846     style_res->letter_spacing.computed = letterspacing;
848     style_res->line_height.normal = linespacing_normal;
849     style_res->line_height.computed = linespacing;
850     style_res->line_height.value = linespacing;
851     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
853     if (texts > 1) {
854         if (different) {
855             return QUERY_STYLE_MULTIPLE_AVERAGED;
856         } else {
857             return QUERY_STYLE_MULTIPLE_SAME;
858         }
859     } else {
860         return QUERY_STYLE_SINGLE;
861     }
864 /**
865  * Write to style_res the average font style of objects.
866  */
867 int
868 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
870     bool different = false;
871     bool set = false;
873     int texts = 0;
875     for (GSList const *i = objects; i != NULL; i = i->next) {
876         SPObject *obj = SP_OBJECT (i->data);
878         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
879             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
880             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
881             continue;
883         SPStyle *style = SP_OBJECT_STYLE (obj);
884         if (!style) continue;
886         texts ++;
888         if (set &&
889             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
890             different = true;  // different styles
891         }
893         set = TRUE;
894         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
895         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
896         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
897         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
898     }
900     if (texts == 0 || !set)
901         return QUERY_STYLE_NOTHING;
903     if (texts > 1) {
904         if (different) {
905             return QUERY_STYLE_MULTIPLE_DIFFERENT;
906         } else {
907             return QUERY_STYLE_MULTIPLE_SAME;
908         }
909     } else {
910         return QUERY_STYLE_SINGLE;
911     }
914 /**
915  * Write to style_res the average font family of objects.
916  */
917 int
918 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
920     bool different = false;
921     int texts = 0;
923     if (style_res->text->font_family.value) {
924         g_free(style_res->text->font_family.value);
925         style_res->text->font_family.value = NULL;
926     }
927     style_res->text->font_family.set = FALSE;
929     for (GSList const *i = objects; i != NULL; i = i->next) {
930         SPObject *obj = SP_OBJECT (i->data);
932         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
933             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
934             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
935             continue;
937         SPStyle *style = SP_OBJECT_STYLE (obj);
938         if (!style) continue;
940         texts ++;
942         if (style_res->text->font_family.value && style->text->font_family.value &&
943             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
944             different = true;  // different fonts
945         }
947         if (style_res->text->font_family.value) {
948             g_free(style_res->text->font_family.value);
949             style_res->text->font_family.value = NULL;
950         }
952         style_res->text->font_family.set = TRUE;
953         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
954     }
956     if (texts == 0 || !style_res->text->font_family.set)
957         return QUERY_STYLE_NOTHING;
959     if (texts > 1) {
960         if (different) {
961             return QUERY_STYLE_MULTIPLE_DIFFERENT;
962         } else {
963             return QUERY_STYLE_MULTIPLE_SAME;
964         }
965     } else {
966         return QUERY_STYLE_SINGLE;
967     }
970 /**
971  * Query the given list of objects for the given property, write
972  * the result to style, return appropriate flag.
973  */
974 int
975 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
977     if (property == QUERY_STYLE_PROPERTY_FILL) {
978         return objects_query_fillstroke (list, style, true);
979     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
980         return objects_query_fillstroke (list, style, false);
982     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
983         return objects_query_strokewidth (list, style);
984     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
985         return objects_query_miterlimit (list, style);
986     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
987         return objects_query_strokecap (list, style);
988     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
989         return objects_query_strokejoin (list, style);
991     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
992         return objects_query_opacity (list, style);
994     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
995         return objects_query_fontfamily (list, style);
996     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
997         return objects_query_fontstyle (list, style);
998     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
999         return objects_query_fontnumbers (list, style);
1000     }
1002     return QUERY_STYLE_NOTHING;
1006 /**
1007  * Query the subselection (if any) or selection on the given desktop for the given property, write
1008  * the result to style, return appropriate flag.
1009  */
1010 int
1011 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1013     int ret = desktop->_query_style_signal.emit(style, property);
1015     if (ret != QUERY_STYLE_NOTHING)
1016         return ret; // subselection returned a style, pass it on
1018     // otherwise, do querying and averaging over selection
1019     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1022 /**
1023  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1024  * the properties returned QUERY_STYLE_NOTHING.
1025  */
1026 bool
1027 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1029         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1030         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1031         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1032         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1033         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1034         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1035         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1036         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1037         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1038         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1040         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);
1044 /*
1045   Local Variables:
1046   mode:c++
1047   c-file-style:"stroustrup"
1048   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1049   indent-tabs-mode:nil
1050   fill-column:99
1051   End:
1052 */
1053 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :