Code

Calligraphy tool now draws in the last used color or tool style depending on the...
[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  *
9  * Copyright (C) 2004 authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "desktop.h"
15 #include "color-rgba.h"
16 #include "svg/css-ostringstream.h"
17 #include "svg/svg-color.h"
18 #include "selection.h"
19 #include "sp-tspan.h"
20 #include "sp-textpath.h"
21 #include "inkscape.h"
22 #include "style.h"
23 #include "prefs-utils.h"
24 #include "sp-use.h"
25 #include "sp-flowtext.h"
26 #include "sp-flowregion.h"
27 #include "sp-flowdiv.h"
28 #include "sp-linear-gradient.h"
29 #include "sp-radial-gradient.h"
30 #include "sp-pattern.h"
31 #include "xml/repr.h"
32 #include "libnrtype/font-style-to-pos.h"
34 #include "desktop-style.h"
36 /**
37  * Set color on selection on desktop.
38  */
39 void
40 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
41 {
42     /// \todo relative color setting
43     if (is_relative) {
44         g_warning("FIXME: relative color setting not yet implemented");
45         return;
46     }
48     guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
49     gchar b[64];
50     sp_svg_write_color(b, 64, rgba);
51     SPCSSAttr *css = sp_repr_css_attr_new();
52     if (fill) {
53         sp_repr_css_set_property(css, "fill", b);
54         Inkscape::CSSOStringStream osalpha;
55         osalpha << color[3];
56         sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
57     } else {
58         sp_repr_css_set_property(css, "stroke", b);
59         Inkscape::CSSOStringStream osalpha;
60         osalpha << color[3];
61         sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
62     }
64     sp_desktop_set_style(desktop, css);
66     sp_repr_css_attr_unref(css);
67 }
69 /**
70  * Apply style on object and children, recursively.
71  */
72 void
73 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
74 {
75     // non-items should not have style
76     if (!SP_IS_ITEM(o))
77         return;
79     // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
80     // but must always inherit from the parent text. Same for textPath.
81     // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
83     // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
84     // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
85     // flowtext).
87     if (!(skip_lines
88           && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
89               || SP_IS_FLOWDIV(o)
90               || SP_IS_FLOWPARA(o)
91               || SP_IS_TEXTPATH(o))
92           && !SP_OBJECT_REPR(o)->attribute("style"))
93         &&
94         !(SP_IS_FLOWREGION(o) ||
95           SP_IS_FLOWREGIONEXCLUDE(o) ||
96           (SP_IS_USE(o) &&
97            SP_OBJECT_PARENT(o) &&
98            (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
99             SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
100            )
101           )
102          )
103         ) {
105         SPCSSAttr *css_set = sp_repr_css_attr_new();
106         sp_repr_css_merge(css_set, css);
108         // Scale the style by the inverse of the accumulated parent transform in the paste context.
109         {
110             NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
111             double const ex(NR::expansion(local));
112             if ( ( ex != 0. )
113                  && ( ex != 1. ) ) {
114                 sp_css_attr_scale(css_set, 1/ex);
115             }
116         }
118         sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
120         sp_repr_css_attr_unref(css_set);
121     }
123     // setting style on child of clone spills into the clone original (via shared repr), don't do it!
124     if (SP_IS_USE(o))
125         return;
127     for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
128         if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
129             // Unset properties which are accumulating and thus should not be set recursively.
130             // 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.
131             SPCSSAttr *css_recurse = sp_repr_css_attr_new();
132             sp_repr_css_merge(css_recurse, css);
133             sp_repr_css_set_property(css_recurse, "opacity", NULL);
134             sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
135             sp_repr_css_attr_unref(css_recurse);
136         } else {
137             sp_desktop_apply_css_recursive(child, css, skip_lines);
138         }
139     }
142 /**
143  * Apply style on selection on desktop.
144  */
145 void
146 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
148     if (write_current) {
149 // 1. Set internal value
150         sp_repr_css_merge(desktop->current, css);
152 // 1a. Write to prefs; make a copy and unset any URIs first
153         SPCSSAttr *css_write = sp_repr_css_attr_new();
154         sp_repr_css_merge(css_write, css);
155         sp_css_attr_unset_uris(css_write);
156         sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
157         sp_repr_css_attr_unref(css_write);
158     }
160     if (!change)
161         return;
163 // 2. Emit signal
164     bool intercepted = desktop->_set_style_signal.emit(css);
166 /** \todo
167  * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
168  * rect corners, font size for the object's own transform so that pasting
169  * fills does not depend on preserve/optimize.
170  */
172 // 3. If nobody has intercepted the signal, apply the style to the selection
173     if (!intercepted) {
174         for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
175             /// \todo if the style is text-only, apply only to texts?
176             sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
177         }
178     }
181 /**
182  * Return the desktop's current style.
183  */
184 SPCSSAttr *
185 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
187     SPCSSAttr *css = sp_repr_css_attr_new();
188     sp_repr_css_merge(css, desktop->current);
189     if (!css->attributeList()) {
190         sp_repr_css_attr_unref(css);
191         return NULL;
192     } else {
193         if (!with_text) {
194             css = sp_css_attr_unset_text(css);
195         }
196         return css;
197     }
200 /**
201  * Return the desktop's current color.
202  */
203 guint32
204 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
206     guint32 r = 0; // if there's no color, return black
207     gchar const *property = sp_repr_css_property(desktop->current,
208                                                  is_fill ? "fill" : "stroke",
209                                                  "#000");
211     if (desktop->current && property) { // if there is style and the property in it,
212         if (strncmp(property, "url", 3)) { // and if it's not url,
213             // read it
214             r = sp_svg_read_color(property, r);
215         }
216     }
218     return r;
221 guint32
222 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
224     SPCSSAttr *css = NULL;
225     guint32 r = 0; // if there's no color, return black
226     if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
227         css = sp_desktop_get_style(desktop, is_fill);
228     }
229     else {
230         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
231         css = sp_repr_css_attr_inherited(tool_repr, "style");
232         }
233    
234     gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
235            
236     if (desktop->current && property) { // if there is style and the property in it,
237         if (strncmp(property, "url", 3)) { // and if it's not url,
238             // read it
239             r = sp_svg_read_color(property, r);
240             }
241         }
242     return r | 0xff;
244 /**
245  * Apply the desktop's current style or the tool style to repr.
246  */
247 void
248 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
250     SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
251     if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
252         sp_repr_css_set(repr, css_current, "style");
253     } else {
254         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
255         if (tool_repr) {
256             SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
257             sp_repr_css_set(repr, css, "style");
258             sp_repr_css_attr_unref(css);
259         }
260     }
261     if (css_current) {sp_repr_css_attr_unref(css_current);
262     }
265 /**
266  * Returns the font size (in SVG pixels) of the text tool style (if text
267  * tool uses its own style) or desktop style (otherwise).
268 */
269 double
270 sp_desktop_get_font_size_tool(SPDesktop *desktop)
272     gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
273     gchar const *style_str = NULL;
274     if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
275         style_str = desktop_style;
276     } else {
277         Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
278         if (tool_repr) {
279             style_str = tool_repr->attribute("style");
280         }
281     }
283     double ret = 12;
284     if (style_str) {
285         SPStyle *style = sp_style_new();
286         sp_style_merge_from_style_string(style, style_str);
287         ret = style->font_size.computed;
288         sp_style_unref(style);
289     }
290     return ret;
293 /** Determine average stroke width, simple method */
294 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
295 gdouble
296 stroke_average_width (GSList const *objects)
298     if (g_slist_length ((GSList *) objects) == 0)
299         return NR_HUGE;
301     gdouble avgwidth = 0.0;
302     bool notstroked = true;
303     int n_notstroked = 0;
305     for (GSList const *l = objects; l != NULL; l = l->next) {
306         if (!SP_IS_ITEM (l->data))
307             continue;
309         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
311         SPObject *object = SP_OBJECT(l->data);
313         if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
314             ++n_notstroked;   // do not count nonstroked objects
315             continue;
316         } else {
317             notstroked = false;
318         }
320         avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
321     }
323     if (notstroked)
324         return NR_HUGE;
326     return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
329 /**
330  * Write to style_res the average fill or stroke of list of objects, if applicable.
331  */
332 int
333 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
335     if (g_slist_length(objects) == 0) {
336         /* No objects, set empty */
337         return QUERY_STYLE_NOTHING;
338     }
340     SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
341     paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
342     paint_res->set = TRUE;
344     gfloat c[4];
345     c[0] = c[1] = c[2] = c[3] = 0.0;
346     gint num = 0;
348     gfloat prev[4];
349     prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
350     bool same_color = true;
352     for (GSList const *i = objects; i != NULL; i = i->next) {
353         SPObject *obj = SP_OBJECT (i->data);
354         SPStyle *style = SP_OBJECT_STYLE (obj);
355         if (!style) continue;
357         SPIPaint *paint = isfill? &style->fill : &style->stroke;
359         // We consider paint "effectively set" for anything within text hierarchy
360         SPObject *parent = SP_OBJECT_PARENT (obj);
361         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));
363         // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
365         if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
366             return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different types of paint
367         }
369         if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
370             // both previous paint and this paint were a server, see if the servers are compatible
372             SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
373             SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
375             if (SP_IS_LINEARGRADIENT (server_res)) {
377                 if (!SP_IS_LINEARGRADIENT(server))
378                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
380                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
381                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
382                 if (vector_res != vector)
383                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
385             } else if (SP_IS_RADIALGRADIENT (server_res)) {
387                 if (!SP_IS_RADIALGRADIENT(server))
388                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
390                 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
391                 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
392                 if (vector_res != vector)
393                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different gradient vectors
395             } else if (SP_IS_PATTERN (server_res)) {
397                 if (!SP_IS_PATTERN(server))
398                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different kind of server
400                 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
401                 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
402                 if (pat_res != pat)
403                    return QUERY_STYLE_MULTIPLE_DIFFERENT;  // different pattern roots
404             }
405         }
407         // 2. Sum color, copy server from paint to paint_res
409         if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
411             gfloat d[3];
412             sp_color_get_rgb_floatv (&paint->value.color, d);
414             // Check if this color is the same as previous
415             if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
416                 prev[0] = d[0];
417                 prev[1] = d[1];
418                 prev[2] = d[2];
419                 prev[3] = d[3];
420             } else {
421                 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
422                     same_color = false;
423             }
425             // average color
426             c[0] += d[0];
427             c[1] += d[1];
428             c[2] += d[2];
429             c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
430             num ++;
431         }
433        if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
434            if (isfill) {
435                SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
436            } else {
437                SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
438            }
439        }
440        paint_res->type = paint->type;
441        paint_res->set = paint_effectively_set;
442        style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
443     }
445     // After all objects processed, divide the color if necessary and return
446     if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
447         g_assert (num >= 1);
449         c[0] /= num;
450         c[1] /= num;
451         c[2] /= num;
452         c[3] /= num;
453         sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
454         if (isfill) {
455             style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
456         } else {
457             style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
458         }
459         if (num > 1) {
460             if (same_color)
461                 return QUERY_STYLE_MULTIPLE_SAME;
462             else
463                 return QUERY_STYLE_MULTIPLE_AVERAGED;
464         } else {
465             return QUERY_STYLE_SINGLE;
466         }
467     }
469     // Not color
470     if (g_slist_length(objects) > 1) {
471         return QUERY_STYLE_MULTIPLE_SAME;
472     } else {
473         return QUERY_STYLE_SINGLE;
474     }
477 /**
478  * Write to style_res the average opacity of a list of objects.
479  */
480 int
481 objects_query_opacity (GSList *objects, SPStyle *style_res)
483     if (g_slist_length(objects) == 0) {
484         /* No objects, set empty */
485         return QUERY_STYLE_NOTHING;
486     }
488     gdouble opacity_sum = 0;
489     gdouble opacity_prev = -1;
490     bool same_opacity = true;
491     guint opacity_items = 0;
494     for (GSList const *i = objects; i != NULL; i = i->next) {
495         SPObject *obj = SP_OBJECT (i->data);
496         SPStyle *style = SP_OBJECT_STYLE (obj);
497         if (!style) continue;
499         double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
500         opacity_sum += opacity;
501         if (opacity_prev != -1 && opacity != opacity_prev)
502             same_opacity = false;
503         opacity_prev = opacity;
504         opacity_items ++;
505     }
506     if (opacity_items > 1)
507         opacity_sum /= opacity_items;
509     style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
511     if (opacity_items == 0) {
512         return QUERY_STYLE_NOTHING;
513     } else if (opacity_items == 1) {
514         return QUERY_STYLE_SINGLE;
515     } else {
516         if (same_opacity)
517             return QUERY_STYLE_MULTIPLE_SAME;
518         else
519             return QUERY_STYLE_MULTIPLE_AVERAGED;
520     }
523 /**
524  * Write to style_res the average stroke width of a list of objects.
525  */
526 int
527 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
529     if (g_slist_length(objects) == 0) {
530         /* No objects, set empty */
531         return QUERY_STYLE_NOTHING;
532     }
534     gdouble avgwidth = 0.0;
536     gdouble prev_sw = -1;
537     bool same_sw = true;
539     int n_stroked = 0;
541     for (GSList const *i = objects; i != NULL; i = i->next) {
542         SPObject *obj = SP_OBJECT (i->data);
543         if (!SP_IS_ITEM(obj)) continue;
544         SPStyle *style = SP_OBJECT_STYLE (obj);
545         if (!style) continue;
547         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
548             continue;
549         }
551         n_stroked ++;
553         NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
554         double sw = style->stroke_width.computed * i2d.expansion();
556         if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
557             same_sw = false;
558         prev_sw = sw;
560         avgwidth += sw;
561     }
563     if (n_stroked > 1)
564         avgwidth /= (n_stroked);
566     style_res->stroke_width.computed = avgwidth;
567     style_res->stroke_width.set = true;
569     if (n_stroked == 0) {
570         return QUERY_STYLE_NOTHING;
571     } else if (n_stroked == 1) {
572         return QUERY_STYLE_SINGLE;
573     } else {
574         if (same_sw)
575             return QUERY_STYLE_MULTIPLE_SAME;
576         else
577             return QUERY_STYLE_MULTIPLE_AVERAGED;
578     }
581 /**
582  * Write to style_res the average miter limit of a list of objects.
583  */
584 int
585 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
587     if (g_slist_length(objects) == 0) {
588         /* No objects, set empty */
589         return QUERY_STYLE_NOTHING;
590     }
592     gdouble avgml = 0.0;
593     int n_stroked = 0;
595     gdouble prev_ml = -1;
596     bool same_ml = true;
598     for (GSList const *i = objects; i != NULL; i = i->next) {
599         SPObject *obj = SP_OBJECT (i->data);
600         if (!SP_IS_ITEM(obj)) continue;
601         SPStyle *style = SP_OBJECT_STYLE (obj);
602         if (!style) continue;
604         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
605             continue;
606         }
608         n_stroked ++;
610         if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
611             same_ml = false;
612         prev_ml = style->stroke_miterlimit.value;
614         avgml += style->stroke_miterlimit.value;
615     }
617     if (n_stroked > 1)
618         avgml /= (n_stroked);
620     style_res->stroke_miterlimit.value = avgml;
621     style_res->stroke_miterlimit.set = true;
623     if (n_stroked == 0) {
624         return QUERY_STYLE_NOTHING;
625     } else if (n_stroked == 1) {
626         return QUERY_STYLE_SINGLE;
627     } else {
628         if (same_ml)
629             return QUERY_STYLE_MULTIPLE_SAME;
630         else
631             return QUERY_STYLE_MULTIPLE_AVERAGED;
632     }
635 /**
636  * Write to style_res the stroke cap of a list of objects.
637  */
638 int
639 objects_query_strokecap (GSList *objects, SPStyle *style_res)
641     if (g_slist_length(objects) == 0) {
642         /* No objects, set empty */
643         return QUERY_STYLE_NOTHING;
644     }
646     int cap = -1;
647     gdouble prev_cap = -1;
648     bool same_cap = true;
649     int n_stroked = 0;
651     for (GSList const *i = objects; i != NULL; i = i->next) {
652         SPObject *obj = SP_OBJECT (i->data);
653         if (!SP_IS_ITEM(obj)) continue;
654         SPStyle *style = SP_OBJECT_STYLE (obj);
655         if (!style) continue;
657         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
658             continue;
659         }
661         n_stroked ++;
663         if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
664             same_cap = false;
665         prev_cap = style->stroke_linecap.value;
667         cap = style->stroke_linecap.value;
668     }
670     style_res->stroke_linecap.value = cap;
671     style_res->stroke_linecap.set = true;
673     if (n_stroked == 0) {
674         return QUERY_STYLE_NOTHING;
675     } else if (n_stroked == 1) {
676         return QUERY_STYLE_SINGLE;
677     } else {
678         if (same_cap)
679             return QUERY_STYLE_MULTIPLE_SAME;
680         else
681             return QUERY_STYLE_MULTIPLE_DIFFERENT;
682     }
685 /**
686  * Write to style_res the stroke join of a list of objects.
687  */
688 int
689 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
691     if (g_slist_length(objects) == 0) {
692         /* No objects, set empty */
693         return QUERY_STYLE_NOTHING;
694     }
696     int join = -1;
697     gdouble prev_join = -1;
698     bool same_join = true;
699     int n_stroked = 0;
701     for (GSList const *i = objects; i != NULL; i = i->next) {
702         SPObject *obj = SP_OBJECT (i->data);
703         if (!SP_IS_ITEM(obj)) continue;
704         SPStyle *style = SP_OBJECT_STYLE (obj);
705         if (!style) continue;
707         if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
708             continue;
709         }
711         n_stroked ++;
713         if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
714             same_join = false;
715         prev_join = style->stroke_linejoin.value;
717         join = style->stroke_linejoin.value;
718     }
720     style_res->stroke_linejoin.value = join;
721     style_res->stroke_linejoin.set = true;
723     if (n_stroked == 0) {
724         return QUERY_STYLE_NOTHING;
725     } else if (n_stroked == 1) {
726         return QUERY_STYLE_SINGLE;
727     } else {
728         if (same_join)
729             return QUERY_STYLE_MULTIPLE_SAME;
730         else
731             return QUERY_STYLE_MULTIPLE_DIFFERENT;
732     }
735 /**
736  * Write to style_res the average font size and spacing of objects.
737  */
738 int
739 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
741     bool different = false;
743     double size = 0;
744     double letterspacing = 0;
745     double linespacing = 0;
746     bool linespacing_normal = false;
747     bool letterspacing_normal = false;
749     double size_prev = 0;
750     double letterspacing_prev = 0;
751     double linespacing_prev = 0;
753     /// \todo FIXME: add word spacing, kerns? rotates?
755     int texts = 0;
757     for (GSList const *i = objects; i != NULL; i = i->next) {
758         SPObject *obj = SP_OBJECT (i->data);
760         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
761             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
762             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
763             continue;
765         SPStyle *style = SP_OBJECT_STYLE (obj);
766         if (!style) continue;
768         texts ++;
769         size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
771         if (style->letter_spacing.normal) {
772             if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
773                 letterspacing_normal = true;
774         } else {
775             letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
776             letterspacing_normal = false;
777         }
779         double linespacing_current;
780         if (style->line_height.normal) {
781             linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
782             if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
783                 linespacing_normal = true;
784         } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
785             linespacing_current = style->line_height.value;
786             linespacing_normal = false;
787         } else { // we need % here
788             linespacing_current = style->line_height.computed / style->font_size.computed;
789             linespacing_normal = false;
790         }
791         linespacing += linespacing_current;
793         if ((size_prev != 0 && style->font_size.computed != size_prev) ||
794             (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
795             (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
796             different = true;
797         }
799         size_prev = style->font_size.computed;
800         letterspacing_prev = style->letter_spacing.computed;
801         linespacing_prev = linespacing_current;
803         // FIXME: we must detect MULTIPLE_DIFFERENT for these too
804         style_res->text_anchor.computed = style->text_anchor.computed;
805         style_res->writing_mode.computed = style->writing_mode.computed;
806     }
808     if (texts == 0)
809         return QUERY_STYLE_NOTHING;
811     if (texts > 1) {
812         size /= texts;
813         letterspacing /= texts;
814         linespacing /= texts;
815     }
817     style_res->font_size.computed = size;
818     style_res->font_size.type = SP_FONT_SIZE_LENGTH;
820     style_res->letter_spacing.normal = letterspacing_normal;
821     style_res->letter_spacing.computed = letterspacing;
823     style_res->line_height.normal = linespacing_normal;
824     style_res->line_height.computed = linespacing;
825     style_res->line_height.value = linespacing;
826     style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
828     if (texts > 1) {
829         if (different) {
830             return QUERY_STYLE_MULTIPLE_AVERAGED;
831         } else {
832             return QUERY_STYLE_MULTIPLE_SAME;
833         }
834     } else {
835         return QUERY_STYLE_SINGLE;
836     }
839 /**
840  * Write to style_res the average font style of objects.
841  */
842 int
843 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
845     bool different = false;
846     bool set = false;
848     int texts = 0;
850     for (GSList const *i = objects; i != NULL; i = i->next) {
851         SPObject *obj = SP_OBJECT (i->data);
853         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
854             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
855             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
856             continue;
858         SPStyle *style = SP_OBJECT_STYLE (obj);
859         if (!style) continue;
861         texts ++;
863         if (set &&
864             font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
865             different = true;  // different styles
866         }
868         set = TRUE;
869         style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
870         style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
871         style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
872         style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
873     }
875     if (texts == 0 || !set)
876         return QUERY_STYLE_NOTHING;
878     if (texts > 1) {
879         if (different) {
880             return QUERY_STYLE_MULTIPLE_DIFFERENT;
881         } else {
882             return QUERY_STYLE_MULTIPLE_SAME;
883         }
884     } else {
885         return QUERY_STYLE_SINGLE;
886     }
889 /**
890  * Write to style_res the average font family of objects.
891  */
892 int
893 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
895     bool different = false;
896     int texts = 0;
898     if (style_res->text->font_family.value) {
899         g_free(style_res->text->font_family.value);
900         style_res->text->font_family.value = NULL;
901     }
902     style_res->text->font_family.set = FALSE;
904     for (GSList const *i = objects; i != NULL; i = i->next) {
905         SPObject *obj = SP_OBJECT (i->data);
907         if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
908             && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
909             && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
910             continue;
912         SPStyle *style = SP_OBJECT_STYLE (obj);
913         if (!style) continue;
915         texts ++;
917         if (style_res->text->font_family.value && style->text->font_family.value &&
918             strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
919             different = true;  // different fonts
920         }
922         if (style_res->text->font_family.value) {
923             g_free(style_res->text->font_family.value);
924             style_res->text->font_family.value = NULL;
925         }
927         style_res->text->font_family.set = TRUE;
928         style_res->text->font_family.value = g_strdup(style->text->font_family.value);
929     }
931     if (texts == 0 || !style_res->text->font_family.set)
932         return QUERY_STYLE_NOTHING;
934     if (texts > 1) {
935         if (different) {
936             return QUERY_STYLE_MULTIPLE_DIFFERENT;
937         } else {
938             return QUERY_STYLE_MULTIPLE_SAME;
939         }
940     } else {
941         return QUERY_STYLE_SINGLE;
942     }
945 /**
946  * Query the given list of objects for the given property, write
947  * the result to style, return appropriate flag.
948  */
949 int
950 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
952     if (property == QUERY_STYLE_PROPERTY_FILL) {
953         return objects_query_fillstroke (list, style, true);
954     } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
955         return objects_query_fillstroke (list, style, false);
957     } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
958         return objects_query_strokewidth (list, style);
959     } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
960         return objects_query_miterlimit (list, style);
961     } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
962         return objects_query_strokecap (list, style);
963     } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
964         return objects_query_strokejoin (list, style);
966     } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
967         return objects_query_opacity (list, style);
969     } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
970         return objects_query_fontfamily (list, style);
971     } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
972         return objects_query_fontstyle (list, style);
973     } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
974         return objects_query_fontnumbers (list, style);
975     }
977     return QUERY_STYLE_NOTHING;
981 /**
982  * Query the subselection (if any) or selection on the given desktop for the given property, write
983  * the result to style, return appropriate flag.
984  */
985 int
986 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
988     int ret = desktop->_query_style_signal.emit(style, property);
990     if (ret != QUERY_STYLE_NOTHING)
991         return ret; // subselection returned a style, pass it on
993     // otherwise, do querying and averaging over selection
994     return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
997 /**
998  * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
999  * the properties returned QUERY_STYLE_NOTHING.
1000  */
1001 bool
1002 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1004         int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1005         int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1006         int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1007         int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1008         int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1009         int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1010         int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1011         int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1012         int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1013         int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1015         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);
1019 /*
1020   Local Variables:
1021   mode:c++
1022   c-file-style:"stroustrup"
1023   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1024   indent-tabs-mode:nil
1025   fill-column:99
1026   End:
1027 */
1028 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :