a68c99d65c887ed3e095a9876e15eceaaa88e019
1 #define __SP_DESKTOP_STYLE_C__
3 /** \file
4 * Desktop style management
5 *
6 * Authors:
7 * bulia byak
8 * verbalshadow
9 *
10 * Copyright (C) 2004, 2006 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "desktop.h"
16 #include "color-rgba.h"
17 #include "svg/css-ostringstream.h"
18 #include "svg/svg.h"
19 #include "svg/svg-color.h"
20 #include "selection.h"
21 #include "inkscape.h"
22 #include "style.h"
23 #include "prefs-utils.h"
24 #include "sp-use.h"
25 #include "sp-feblend.h"
26 #include "sp-filter.h"
27 #include "sp-filter-reference.h"
28 #include "sp-gaussian-blur.h"
29 #include "sp-flowtext.h"
30 #include "sp-flowregion.h"
31 #include "sp-flowdiv.h"
32 #include "sp-linear-gradient.h"
33 #include "sp-pattern.h"
34 #include "sp-radial-gradient.h"
35 #include "sp-textpath.h"
36 #include "sp-tref.h"
37 #include "sp-tspan.h"
38 #include "xml/repr.h"
39 #include "libnrtype/font-style-to-pos.h"
40 #include "sp-path.h"
42 #include "desktop-style.h"
44 /**
45 * Set color on selection on desktop.
46 */
47 void
48 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
49 {
50 /// \todo relative color setting
51 if (is_relative) {
52 g_warning("FIXME: relative color setting not yet implemented");
53 return;
54 }
56 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
57 gchar b[64];
58 sp_svg_write_color(b, 64, rgba);
59 SPCSSAttr *css = sp_repr_css_attr_new();
60 if (fill) {
61 sp_repr_css_set_property(css, "fill", b);
62 Inkscape::CSSOStringStream osalpha;
63 osalpha << color[3];
64 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
65 } else {
66 sp_repr_css_set_property(css, "stroke", b);
67 Inkscape::CSSOStringStream osalpha;
68 osalpha << color[3];
69 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
70 }
72 sp_desktop_set_style(desktop, css);
74 sp_repr_css_attr_unref(css);
75 }
77 /**
78 * Apply style on object and children, recursively.
79 */
80 void
81 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
82 {
83 // non-items should not have style
84 if (!SP_IS_ITEM(o))
85 return;
87 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
88 // but must always inherit from the parent text. Same for textPath.
89 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
91 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
92 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
93 // flowtext).
95 if (!(skip_lines
96 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
97 || SP_IS_FLOWDIV(o)
98 || SP_IS_FLOWPARA(o)
99 || SP_IS_TEXTPATH(o))
100 && !SP_OBJECT_REPR(o)->attribute("style"))
101 &&
102 !(SP_IS_FLOWREGION(o) ||
103 SP_IS_FLOWREGIONEXCLUDE(o) ||
104 (SP_IS_USE(o) &&
105 SP_OBJECT_PARENT(o) &&
106 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
107 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
108 )
109 )
110 )
111 ) {
113 SPCSSAttr *css_set = sp_repr_css_attr_new();
114 sp_repr_css_merge(css_set, css);
116 // Scale the style by the inverse of the accumulated parent transform in the paste context.
117 {
118 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
119 double const ex(NR::expansion(local));
120 if ( ( ex != 0. )
121 && ( ex != 1. ) ) {
122 sp_css_attr_scale(css_set, 1/ex);
123 }
124 }
126 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
128 sp_repr_css_attr_unref(css_set);
129 }
131 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
132 if (SP_IS_USE(o))
133 return;
135 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
136 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
137 // Unset properties which are accumulating and thus should not be set recursively.
138 // For example, setting opacity 0.5 on a group recursively would result in the visible opacity of 0.25 for an item in the group.
139 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
140 sp_repr_css_merge(css_recurse, css);
141 sp_repr_css_set_property(css_recurse, "opacity", NULL);
142 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
143 sp_repr_css_attr_unref(css_recurse);
144 } else {
145 sp_desktop_apply_css_recursive(child, css, skip_lines);
146 }
147 }
148 }
150 /**
151 * Apply style on selection on desktop.
152 */
153 void
154 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
155 {
156 if (write_current) {
157 // 1. Set internal value
158 sp_repr_css_merge(desktop->current, css);
160 // 1a. Write to prefs; make a copy and unset any URIs first
161 SPCSSAttr *css_write = sp_repr_css_attr_new();
162 sp_repr_css_merge(css_write, css);
163 sp_css_attr_unset_uris(css_write);
164 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
165 for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
166 /* last used styles for 3D box faces are stored separately */
167 if (SP_IS_PATH (i->data)) {
168 const char * descr = SP_OBJECT_REPR (G_OBJECT (i->data))->attribute ("inkscape:box3dface");
169 if (descr != NULL) {
170 gchar *style_grp = g_strconcat ("desktop.", descr, NULL);
171 sp_repr_css_change(inkscape_get_repr(INKSCAPE, style_grp), css_write, "style");
172 g_free (style_grp);
173 }
174 }
175 }
176 sp_repr_css_attr_unref(css_write);
177 }
179 if (!change)
180 return;
182 // 2. Emit signal
183 bool intercepted = desktop->_set_style_signal.emit(css);
185 /** \todo
186 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
187 * rect corners, font size for the object's own transform so that pasting
188 * fills does not depend on preserve/optimize.
189 */
191 // 3. If nobody has intercepted the signal, apply the style to the selection
192 if (!intercepted) {
193 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
194 /// \todo if the style is text-only, apply only to texts?
195 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
196 }
197 }
198 }
200 /**
201 * Return the desktop's current style.
202 */
203 SPCSSAttr *
204 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
205 {
206 SPCSSAttr *css = sp_repr_css_attr_new();
207 sp_repr_css_merge(css, desktop->current);
208 if (!css->attributeList()) {
209 sp_repr_css_attr_unref(css);
210 return NULL;
211 } else {
212 if (!with_text) {
213 css = sp_css_attr_unset_text(css);
214 }
215 return css;
216 }
217 }
219 /**
220 * Return the desktop's current color.
221 */
222 guint32
223 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
224 {
225 guint32 r = 0; // if there's no color, return black
226 gchar const *property = sp_repr_css_property(desktop->current,
227 is_fill ? "fill" : "stroke",
228 "#000");
230 if (desktop->current && property) { // if there is style and the property in it,
231 if (strncmp(property, "url", 3)) { // and if it's not url,
232 // read it
233 r = sp_svg_read_color(property, r);
234 }
235 }
237 return r;
238 }
240 double
241 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
242 {
243 SPCSSAttr *css = NULL;
244 gfloat value = 1.0; // default if nothing else found
245 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
246 css = sp_desktop_get_style(desktop, true);
247 } else {
248 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
249 if (tool_repr) {
250 css = sp_repr_css_attr_inherited(tool_repr, "style");
251 }
252 }
254 if (css) {
255 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
257 if (desktop->current && property) { // if there is style and the property in it,
258 if ( !sp_svg_number_read_f(property, &value) ) {
259 value = 1.0; // things failed. set back to the default
260 }
261 }
263 sp_repr_css_attr_unref(css);
264 }
266 return value;
267 }
268 double
269 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
270 {
271 SPCSSAttr *css = NULL;
272 gfloat value = 1.0; // default if nothing else found
273 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
274 css = sp_desktop_get_style(desktop, true);
275 } else {
276 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
277 if (tool_repr) {
278 css = sp_repr_css_attr_inherited(tool_repr, "style");
279 }
280 }
282 if (css) {
283 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
285 if (desktop->current && property) { // if there is style and the property in it,
286 if ( !sp_svg_number_read_f(property, &value) ) {
287 value = 1.0; // things failed. set back to the default
288 }
289 }
291 sp_repr_css_attr_unref(css);
292 }
294 return value;
295 }
296 guint32
297 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
298 {
299 SPCSSAttr *css = NULL;
300 guint32 r = 0; // if there's no color, return black
301 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
302 css = sp_desktop_get_style(desktop, true);
303 } else {
304 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
305 if (tool_repr) {
306 css = sp_repr_css_attr_inherited(tool_repr, "style");
307 }
308 }
310 if (css) {
311 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
313 if (desktop->current && property) { // if there is style and the property in it,
314 if (strncmp(property, "url", 3)) { // and if it's not url,
315 // read it
316 r = sp_svg_read_color(property, r);
317 }
318 }
320 sp_repr_css_attr_unref(css);
321 }
323 return r | 0xff;
324 }
325 /**
326 * Apply the desktop's current style or the tool style to repr.
327 */
328 void
329 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
330 {
331 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
332 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
333 sp_repr_css_set(repr, css_current, "style");
334 } else {
335 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
336 if (tool_repr) {
337 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
338 sp_repr_css_set(repr, css, "style");
339 sp_repr_css_attr_unref(css);
340 }
341 }
342 if (css_current) {
343 sp_repr_css_attr_unref(css_current);
344 }
345 }
347 /**
348 * Returns the font size (in SVG pixels) of the text tool style (if text
349 * tool uses its own style) or desktop style (otherwise).
350 */
351 double
352 sp_desktop_get_font_size_tool(SPDesktop *desktop)
353 {
354 (void)desktop; // TODO cleanup
355 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
356 gchar const *style_str = NULL;
357 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
358 style_str = desktop_style;
359 } else {
360 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
361 if (tool_repr) {
362 style_str = tool_repr->attribute("style");
363 }
364 }
366 double ret = 12;
367 if (style_str) {
368 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
369 sp_style_merge_from_style_string(style, style_str);
370 ret = style->font_size.computed;
371 sp_style_unref(style);
372 }
373 return ret;
374 }
376 /** Determine average stroke width, simple method */
377 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
378 gdouble
379 stroke_average_width (GSList const *objects)
380 {
381 if (g_slist_length ((GSList *) objects) == 0)
382 return NR_HUGE;
384 gdouble avgwidth = 0.0;
385 bool notstroked = true;
386 int n_notstroked = 0;
388 for (GSList const *l = objects; l != NULL; l = l->next) {
389 if (!SP_IS_ITEM (l->data))
390 continue;
392 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
394 SPObject *object = SP_OBJECT(l->data);
396 if ( object->style->stroke.isNone() ) {
397 ++n_notstroked; // do not count nonstroked objects
398 continue;
399 } else {
400 notstroked = false;
401 }
403 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
404 }
406 if (notstroked)
407 return NR_HUGE;
409 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
410 }
412 /**
413 * Write to style_res the average fill or stroke of list of objects, if applicable.
414 */
415 int
416 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
417 {
418 if (g_slist_length(objects) == 0) {
419 /* No objects, set empty */
420 return QUERY_STYLE_NOTHING;
421 }
423 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
424 bool paintImpossible = true;
425 paint_res->set = TRUE;
427 gfloat c[4];
428 c[0] = c[1] = c[2] = c[3] = 0.0;
429 gint num = 0;
431 gfloat prev[3];
432 prev[0] = prev[1] = prev[2] = 0.0;
433 bool same_color = true;
435 for (GSList const *i = objects; i != NULL; i = i->next) {
436 SPObject *obj = SP_OBJECT (i->data);
437 SPStyle *style = SP_OBJECT_STYLE (obj);
438 if (!style) continue;
440 SPIPaint *paint = isfill? &style->fill : &style->stroke;
442 // We consider paint "effectively set" for anything within text hierarchy
443 SPObject *parent = SP_OBJECT_PARENT (obj);
444 bool paint_effectively_set =
445 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
446 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
447 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
449 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
451 if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
452 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
453 }
455 if (paint_res->set && paint->set && paint_res->isPaintserver()) {
456 // both previous paint and this paint were a server, see if the servers are compatible
458 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
459 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
461 if (SP_IS_LINEARGRADIENT (server_res)) {
463 if (!SP_IS_LINEARGRADIENT(server))
464 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
466 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
467 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
468 if (vector_res != vector)
469 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
471 } else if (SP_IS_RADIALGRADIENT (server_res)) {
473 if (!SP_IS_RADIALGRADIENT(server))
474 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
476 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
477 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
478 if (vector_res != vector)
479 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
481 } else if (SP_IS_PATTERN (server_res)) {
483 if (!SP_IS_PATTERN(server))
484 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
486 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
487 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
488 if (pat_res != pat)
489 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
490 }
491 }
493 // 2. Sum color, copy server from paint to paint_res
495 if (paint_res->set && paint_effectively_set && paint->isColor()) {
496 gfloat d[3];
497 sp_color_get_rgb_floatv (&paint->value.color, d);
499 // Check if this color is the same as previous
500 if (paintImpossible) {
501 prev[0] = d[0];
502 prev[1] = d[1];
503 prev[2] = d[2];
504 paint_res->setColor(d[0], d[1], d[2]);
505 } else {
506 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
507 same_color = false;
508 }
510 // average color
511 c[0] += d[0];
512 c[1] += d[1];
513 c[2] += d[2];
514 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
515 num ++;
516 }
518 paintImpossible = false;
519 if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
520 if (isfill) {
521 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
522 } else {
523 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
524 }
525 }
526 paint_res->set = paint_effectively_set;
527 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
528 }
530 // After all objects processed, divide the color if necessary and return
531 if (paint_res->set && paint_res->isColor()) { // set the color
532 g_assert (num >= 1);
534 c[0] /= num;
535 c[1] /= num;
536 c[2] /= num;
537 c[3] /= num;
538 paint_res->setColor(c[0], c[1], c[2]);
539 if (isfill) {
540 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
541 } else {
542 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
543 }
544 if (num > 1) {
545 if (same_color)
546 return QUERY_STYLE_MULTIPLE_SAME;
547 else
548 return QUERY_STYLE_MULTIPLE_AVERAGED;
549 } else {
550 return QUERY_STYLE_SINGLE;
551 }
552 }
554 // Not color
555 if (g_slist_length(objects) > 1) {
556 return QUERY_STYLE_MULTIPLE_SAME;
557 } else {
558 return QUERY_STYLE_SINGLE;
559 }
560 }
562 /**
563 * Write to style_res the average opacity of a list of objects.
564 */
565 int
566 objects_query_opacity (GSList *objects, SPStyle *style_res)
567 {
568 if (g_slist_length(objects) == 0) {
569 /* No objects, set empty */
570 return QUERY_STYLE_NOTHING;
571 }
573 gdouble opacity_sum = 0;
574 gdouble opacity_prev = -1;
575 bool same_opacity = true;
576 guint opacity_items = 0;
579 for (GSList const *i = objects; i != NULL; i = i->next) {
580 SPObject *obj = SP_OBJECT (i->data);
581 SPStyle *style = SP_OBJECT_STYLE (obj);
582 if (!style) continue;
584 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
585 opacity_sum += opacity;
586 if (opacity_prev != -1 && opacity != opacity_prev)
587 same_opacity = false;
588 opacity_prev = opacity;
589 opacity_items ++;
590 }
591 if (opacity_items > 1)
592 opacity_sum /= opacity_items;
594 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
596 if (opacity_items == 0) {
597 return QUERY_STYLE_NOTHING;
598 } else if (opacity_items == 1) {
599 return QUERY_STYLE_SINGLE;
600 } else {
601 if (same_opacity)
602 return QUERY_STYLE_MULTIPLE_SAME;
603 else
604 return QUERY_STYLE_MULTIPLE_AVERAGED;
605 }
606 }
608 /**
609 * Write to style_res the average stroke width of a list of objects.
610 */
611 int
612 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
613 {
614 if (g_slist_length(objects) == 0) {
615 /* No objects, set empty */
616 return QUERY_STYLE_NOTHING;
617 }
619 gdouble avgwidth = 0.0;
621 gdouble prev_sw = -1;
622 bool same_sw = true;
624 int n_stroked = 0;
626 for (GSList const *i = objects; i != NULL; i = i->next) {
627 SPObject *obj = SP_OBJECT (i->data);
628 if (!SP_IS_ITEM(obj)) continue;
629 SPStyle *style = SP_OBJECT_STYLE (obj);
630 if (!style) continue;
632 if ( style->stroke.isNone() ) {
633 continue;
634 }
636 n_stroked ++;
638 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
639 double sw = style->stroke_width.computed * i2d.expansion();
641 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
642 same_sw = false;
643 prev_sw = sw;
645 avgwidth += sw;
646 }
648 if (n_stroked > 1)
649 avgwidth /= (n_stroked);
651 style_res->stroke_width.computed = avgwidth;
652 style_res->stroke_width.set = true;
654 if (n_stroked == 0) {
655 return QUERY_STYLE_NOTHING;
656 } else if (n_stroked == 1) {
657 return QUERY_STYLE_SINGLE;
658 } else {
659 if (same_sw)
660 return QUERY_STYLE_MULTIPLE_SAME;
661 else
662 return QUERY_STYLE_MULTIPLE_AVERAGED;
663 }
664 }
666 /**
667 * Write to style_res the average miter limit of a list of objects.
668 */
669 int
670 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
671 {
672 if (g_slist_length(objects) == 0) {
673 /* No objects, set empty */
674 return QUERY_STYLE_NOTHING;
675 }
677 gdouble avgml = 0.0;
678 int n_stroked = 0;
680 gdouble prev_ml = -1;
681 bool same_ml = true;
683 for (GSList const *i = objects; i != NULL; i = i->next) {
684 SPObject *obj = SP_OBJECT (i->data);
685 if (!SP_IS_ITEM(obj)) continue;
686 SPStyle *style = SP_OBJECT_STYLE (obj);
687 if (!style) continue;
689 if ( style->stroke.isNone() ) {
690 continue;
691 }
693 n_stroked ++;
695 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
696 same_ml = false;
697 prev_ml = style->stroke_miterlimit.value;
699 avgml += style->stroke_miterlimit.value;
700 }
702 if (n_stroked > 1)
703 avgml /= (n_stroked);
705 style_res->stroke_miterlimit.value = avgml;
706 style_res->stroke_miterlimit.set = true;
708 if (n_stroked == 0) {
709 return QUERY_STYLE_NOTHING;
710 } else if (n_stroked == 1) {
711 return QUERY_STYLE_SINGLE;
712 } else {
713 if (same_ml)
714 return QUERY_STYLE_MULTIPLE_SAME;
715 else
716 return QUERY_STYLE_MULTIPLE_AVERAGED;
717 }
718 }
720 /**
721 * Write to style_res the stroke cap of a list of objects.
722 */
723 int
724 objects_query_strokecap (GSList *objects, SPStyle *style_res)
725 {
726 if (g_slist_length(objects) == 0) {
727 /* No objects, set empty */
728 return QUERY_STYLE_NOTHING;
729 }
731 int cap = -1;
732 gdouble prev_cap = -1;
733 bool same_cap = true;
734 int n_stroked = 0;
736 for (GSList const *i = objects; i != NULL; i = i->next) {
737 SPObject *obj = SP_OBJECT (i->data);
738 if (!SP_IS_ITEM(obj)) continue;
739 SPStyle *style = SP_OBJECT_STYLE (obj);
740 if (!style) continue;
742 if ( style->stroke.isNone() ) {
743 continue;
744 }
746 n_stroked ++;
748 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
749 same_cap = false;
750 prev_cap = style->stroke_linecap.value;
752 cap = style->stroke_linecap.value;
753 }
755 style_res->stroke_linecap.value = cap;
756 style_res->stroke_linecap.set = true;
758 if (n_stroked == 0) {
759 return QUERY_STYLE_NOTHING;
760 } else if (n_stroked == 1) {
761 return QUERY_STYLE_SINGLE;
762 } else {
763 if (same_cap)
764 return QUERY_STYLE_MULTIPLE_SAME;
765 else
766 return QUERY_STYLE_MULTIPLE_DIFFERENT;
767 }
768 }
770 /**
771 * Write to style_res the stroke join of a list of objects.
772 */
773 int
774 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
775 {
776 if (g_slist_length(objects) == 0) {
777 /* No objects, set empty */
778 return QUERY_STYLE_NOTHING;
779 }
781 int join = -1;
782 gdouble prev_join = -1;
783 bool same_join = true;
784 int n_stroked = 0;
786 for (GSList const *i = objects; i != NULL; i = i->next) {
787 SPObject *obj = SP_OBJECT (i->data);
788 if (!SP_IS_ITEM(obj)) continue;
789 SPStyle *style = SP_OBJECT_STYLE (obj);
790 if (!style) continue;
792 if ( style->stroke.isNone() ) {
793 continue;
794 }
796 n_stroked ++;
798 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
799 same_join = false;
800 prev_join = style->stroke_linejoin.value;
802 join = style->stroke_linejoin.value;
803 }
805 style_res->stroke_linejoin.value = join;
806 style_res->stroke_linejoin.set = true;
808 if (n_stroked == 0) {
809 return QUERY_STYLE_NOTHING;
810 } else if (n_stroked == 1) {
811 return QUERY_STYLE_SINGLE;
812 } else {
813 if (same_join)
814 return QUERY_STYLE_MULTIPLE_SAME;
815 else
816 return QUERY_STYLE_MULTIPLE_DIFFERENT;
817 }
818 }
820 /**
821 * Write to style_res the average font size and spacing of objects.
822 */
823 int
824 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
825 {
826 bool different = false;
828 double size = 0;
829 double letterspacing = 0;
830 double linespacing = 0;
831 bool linespacing_normal = false;
832 bool letterspacing_normal = false;
834 double size_prev = 0;
835 double letterspacing_prev = 0;
836 double linespacing_prev = 0;
838 /// \todo FIXME: add word spacing, kerns? rotates?
840 int texts = 0;
842 for (GSList const *i = objects; i != NULL; i = i->next) {
843 SPObject *obj = SP_OBJECT (i->data);
845 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
846 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
847 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
848 continue;
850 SPStyle *style = SP_OBJECT_STYLE (obj);
851 if (!style) continue;
853 texts ++;
854 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
856 if (style->letter_spacing.normal) {
857 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
858 letterspacing_normal = true;
859 } else {
860 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
861 letterspacing_normal = false;
862 }
864 double linespacing_current;
865 if (style->line_height.normal) {
866 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
867 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
868 linespacing_normal = true;
869 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
870 linespacing_current = style->line_height.value;
871 linespacing_normal = false;
872 } else { // we need % here
873 linespacing_current = style->line_height.computed / style->font_size.computed;
874 linespacing_normal = false;
875 }
876 linespacing += linespacing_current;
878 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
879 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
880 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
881 different = true;
882 }
884 size_prev = style->font_size.computed;
885 letterspacing_prev = style->letter_spacing.computed;
886 linespacing_prev = linespacing_current;
888 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
889 style_res->text_anchor.computed = style->text_anchor.computed;
890 style_res->writing_mode.computed = style->writing_mode.computed;
891 }
893 if (texts == 0)
894 return QUERY_STYLE_NOTHING;
896 if (texts > 1) {
897 size /= texts;
898 letterspacing /= texts;
899 linespacing /= texts;
900 }
902 style_res->font_size.computed = size;
903 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
905 style_res->letter_spacing.normal = letterspacing_normal;
906 style_res->letter_spacing.computed = letterspacing;
908 style_res->line_height.normal = linespacing_normal;
909 style_res->line_height.computed = linespacing;
910 style_res->line_height.value = linespacing;
911 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
913 if (texts > 1) {
914 if (different) {
915 return QUERY_STYLE_MULTIPLE_AVERAGED;
916 } else {
917 return QUERY_STYLE_MULTIPLE_SAME;
918 }
919 } else {
920 return QUERY_STYLE_SINGLE;
921 }
922 }
924 /**
925 * Write to style_res the average font style of objects.
926 */
927 int
928 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
929 {
930 bool different = false;
931 bool set = false;
933 int texts = 0;
935 for (GSList const *i = objects; i != NULL; i = i->next) {
936 SPObject *obj = SP_OBJECT (i->data);
938 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
939 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
940 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
941 continue;
943 SPStyle *style = SP_OBJECT_STYLE (obj);
944 if (!style) continue;
946 texts ++;
948 if (set &&
949 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
950 different = true; // different styles
951 }
953 set = TRUE;
954 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
955 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
956 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
957 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
958 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
959 }
961 if (texts == 0 || !set)
962 return QUERY_STYLE_NOTHING;
964 if (texts > 1) {
965 if (different) {
966 return QUERY_STYLE_MULTIPLE_DIFFERENT;
967 } else {
968 return QUERY_STYLE_MULTIPLE_SAME;
969 }
970 } else {
971 return QUERY_STYLE_SINGLE;
972 }
973 }
975 /**
976 * Write to style_res the average font family of objects.
977 */
978 int
979 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
980 {
981 bool different = false;
982 int texts = 0;
984 if (style_res->text->font_family.value) {
985 g_free(style_res->text->font_family.value);
986 style_res->text->font_family.value = NULL;
987 }
988 style_res->text->font_family.set = FALSE;
990 for (GSList const *i = objects; i != NULL; i = i->next) {
991 SPObject *obj = SP_OBJECT (i->data);
993 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
994 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
995 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
996 continue;
998 SPStyle *style = SP_OBJECT_STYLE (obj);
999 if (!style) continue;
1001 texts ++;
1003 if (style_res->text->font_family.value && style->text->font_family.value &&
1004 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1005 different = true; // different fonts
1006 }
1008 if (style_res->text->font_family.value) {
1009 g_free(style_res->text->font_family.value);
1010 style_res->text->font_family.value = NULL;
1011 }
1013 style_res->text->font_family.set = TRUE;
1014 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1015 }
1017 if (texts == 0 || !style_res->text->font_family.set)
1018 return QUERY_STYLE_NOTHING;
1020 if (texts > 1) {
1021 if (different) {
1022 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1023 } else {
1024 return QUERY_STYLE_MULTIPLE_SAME;
1025 }
1026 } else {
1027 return QUERY_STYLE_SINGLE;
1028 }
1029 }
1031 int
1032 objects_query_blend (GSList *objects, SPStyle *style_res)
1033 {
1034 const int empty_prev = -2;
1035 const int complex_filter = 5;
1036 int blend = 0;
1037 float blend_prev = empty_prev;
1038 bool same_blend = true;
1039 guint items = 0;
1041 for (GSList const *i = objects; i != NULL; i = i->next) {
1042 SPObject *obj = SP_OBJECT (i->data);
1043 SPStyle *style = SP_OBJECT_STYLE (obj);
1044 if(!style || !SP_IS_ITEM(obj)) continue;
1046 items++;
1048 //if object has a filter
1049 if (style->filter.set && style->getFilter()) {
1050 int blurcount = 0;
1051 int blendcount = 0;
1053 // determine whether filter is simple (blend and/or blur) or complex
1054 for(SPObject *primitive_obj = style->getFilter()->children;
1055 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1056 primitive_obj = primitive_obj->next) {
1057 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1058 if(SP_IS_FEBLEND(primitive))
1059 ++blendcount;
1060 else if(SP_IS_GAUSSIANBLUR(primitive))
1061 ++blurcount;
1062 else {
1063 blurcount = complex_filter;
1064 break;
1065 }
1066 }
1068 // simple filter
1069 if(blurcount == 1 || blendcount == 1) {
1070 for(SPObject *primitive_obj = style->getFilter()->children;
1071 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1072 primitive_obj = primitive_obj->next) {
1073 if(SP_IS_FEBLEND(primitive_obj)) {
1074 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1075 blend = spblend->blend_mode;
1076 }
1077 }
1078 }
1079 else {
1080 blend = complex_filter;
1081 }
1082 }
1083 // defaults to blend mode = "normal"
1084 else {
1085 blend = 0;
1086 }
1088 if(blend_prev != empty_prev && blend_prev != blend)
1089 same_blend = false;
1090 blend_prev = blend;
1091 }
1093 if (items > 0) {
1094 style_res->filter_blend_mode.value = blend;
1095 }
1097 if (items == 0) {
1098 return QUERY_STYLE_NOTHING;
1099 } else if (items == 1) {
1100 return QUERY_STYLE_SINGLE;
1101 } else {
1102 if(same_blend)
1103 return QUERY_STYLE_MULTIPLE_SAME;
1104 else
1105 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1106 }
1107 }
1109 /**
1110 * Write to style_res the average blurring of a list of objects.
1111 */
1112 int
1113 objects_query_blur (GSList *objects, SPStyle *style_res)
1114 {
1115 if (g_slist_length(objects) == 0) {
1116 /* No objects, set empty */
1117 return QUERY_STYLE_NOTHING;
1118 }
1120 float blur_sum = 0;
1121 float blur_prev = -1;
1122 bool same_blur = true;
1123 guint blur_items = 0;
1124 guint items = 0;
1126 for (GSList const *i = objects; i != NULL; i = i->next) {
1127 SPObject *obj = SP_OBJECT (i->data);
1128 SPStyle *style = SP_OBJECT_STYLE (obj);
1129 if (!style) continue;
1130 if (!SP_IS_ITEM(obj)) continue;
1132 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1134 items ++;
1136 //if object has a filter
1137 if (style->filter.set && style->getFilter()) {
1138 //cycle through filter primitives
1139 SPObject *primitive_obj = style->getFilter()->children;
1140 while (primitive_obj) {
1141 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1142 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1144 //if primitive is gaussianblur
1145 if(SP_IS_GAUSSIANBLUR(primitive)) {
1146 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1147 float num = spblur->stdDeviation.getNumber();
1148 blur_sum += num * NR::expansion(i2d);
1149 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
1150 same_blur = false;
1151 blur_prev = num;
1152 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1153 blur_items ++;
1154 }
1155 }
1156 primitive_obj = primitive_obj->next;
1157 }
1158 }
1159 }
1161 if (items > 0) {
1162 if (blur_items > 0)
1163 blur_sum /= blur_items;
1164 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1165 }
1167 if (items == 0) {
1168 return QUERY_STYLE_NOTHING;
1169 } else if (items == 1) {
1170 return QUERY_STYLE_SINGLE;
1171 } else {
1172 if (same_blur)
1173 return QUERY_STYLE_MULTIPLE_SAME;
1174 else
1175 return QUERY_STYLE_MULTIPLE_AVERAGED;
1176 }
1177 }
1179 /**
1180 * Query the given list of objects for the given property, write
1181 * the result to style, return appropriate flag.
1182 */
1183 int
1184 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1185 {
1186 if (property == QUERY_STYLE_PROPERTY_FILL) {
1187 return objects_query_fillstroke (list, style, true);
1188 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1189 return objects_query_fillstroke (list, style, false);
1191 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1192 return objects_query_strokewidth (list, style);
1193 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1194 return objects_query_miterlimit (list, style);
1195 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1196 return objects_query_strokecap (list, style);
1197 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1198 return objects_query_strokejoin (list, style);
1200 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1201 return objects_query_opacity (list, style);
1203 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1204 return objects_query_fontfamily (list, style);
1205 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1206 return objects_query_fontstyle (list, style);
1207 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1208 return objects_query_fontnumbers (list, style);
1210 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1211 return objects_query_blend (list, style);
1212 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1213 return objects_query_blur (list, style);
1214 }
1215 return QUERY_STYLE_NOTHING;
1216 }
1219 /**
1220 * Query the subselection (if any) or selection on the given desktop for the given property, write
1221 * the result to style, return appropriate flag.
1222 */
1223 int
1224 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1225 {
1226 int ret = desktop->_query_style_signal.emit(style, property);
1228 if (ret != QUERY_STYLE_NOTHING)
1229 return ret; // subselection returned a style, pass it on
1231 // otherwise, do querying and averaging over selection
1232 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1233 }
1235 /**
1236 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1237 * the properties returned QUERY_STYLE_NOTHING.
1238 */
1239 bool
1240 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1241 {
1242 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1243 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1244 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1245 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1246 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1247 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1248 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1249 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1250 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1251 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1252 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1254 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);
1255 }
1258 /*
1259 Local Variables:
1260 mode:c++
1261 c-file-style:"stroustrup"
1262 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1263 indent-tabs-mode:nil
1264 fill-column:99
1265 End:
1266 */
1267 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :