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"
41 #include "desktop-style.h"
43 /**
44 * Set color on selection on desktop.
45 */
46 void
47 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
48 {
49 /// \todo relative color setting
50 if (is_relative) {
51 g_warning("FIXME: relative color setting not yet implemented");
52 return;
53 }
55 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
56 gchar b[64];
57 sp_svg_write_color(b, 64, rgba);
58 SPCSSAttr *css = sp_repr_css_attr_new();
59 if (fill) {
60 sp_repr_css_set_property(css, "fill", b);
61 Inkscape::CSSOStringStream osalpha;
62 osalpha << color[3];
63 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
64 } else {
65 sp_repr_css_set_property(css, "stroke", b);
66 Inkscape::CSSOStringStream osalpha;
67 osalpha << color[3];
68 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
69 }
71 sp_desktop_set_style(desktop, css);
73 sp_repr_css_attr_unref(css);
74 }
76 /**
77 * Apply style on object and children, recursively.
78 */
79 void
80 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
81 {
82 // non-items should not have style
83 if (!SP_IS_ITEM(o))
84 return;
86 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
87 // but must always inherit from the parent text. Same for textPath.
88 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
90 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
91 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
92 // flowtext).
94 if (!(skip_lines
95 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
96 || SP_IS_FLOWDIV(o)
97 || SP_IS_FLOWPARA(o)
98 || SP_IS_TEXTPATH(o))
99 && !SP_OBJECT_REPR(o)->attribute("style"))
100 &&
101 !(SP_IS_FLOWREGION(o) ||
102 SP_IS_FLOWREGIONEXCLUDE(o) ||
103 (SP_IS_USE(o) &&
104 SP_OBJECT_PARENT(o) &&
105 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
106 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
107 )
108 )
109 )
110 ) {
112 SPCSSAttr *css_set = sp_repr_css_attr_new();
113 sp_repr_css_merge(css_set, css);
115 // Scale the style by the inverse of the accumulated parent transform in the paste context.
116 {
117 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
118 double const ex(NR::expansion(local));
119 if ( ( ex != 0. )
120 && ( ex != 1. ) ) {
121 sp_css_attr_scale(css_set, 1/ex);
122 }
123 }
125 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
127 sp_repr_css_attr_unref(css_set);
128 }
130 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
131 if (SP_IS_USE(o))
132 return;
134 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
135 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
136 // Unset properties which are accumulating and thus should not be set recursively.
137 // 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.
138 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
139 sp_repr_css_merge(css_recurse, css);
140 sp_repr_css_set_property(css_recurse, "opacity", NULL);
141 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
142 sp_repr_css_attr_unref(css_recurse);
143 } else {
144 sp_desktop_apply_css_recursive(child, css, skip_lines);
145 }
146 }
147 }
149 /**
150 * Apply style on selection on desktop.
151 */
152 void
153 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
154 {
155 if (write_current) {
156 // 1. Set internal value
157 sp_repr_css_merge(desktop->current, css);
159 // 1a. Write to prefs; make a copy and unset any URIs first
160 SPCSSAttr *css_write = sp_repr_css_attr_new();
161 sp_repr_css_merge(css_write, css);
162 sp_css_attr_unset_uris(css_write);
163 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
164 sp_repr_css_attr_unref(css_write);
165 }
167 if (!change)
168 return;
170 // 2. Emit signal
171 bool intercepted = desktop->_set_style_signal.emit(css);
173 /** \todo
174 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
175 * rect corners, font size for the object's own transform so that pasting
176 * fills does not depend on preserve/optimize.
177 */
179 // 3. If nobody has intercepted the signal, apply the style to the selection
180 if (!intercepted) {
181 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
182 /// \todo if the style is text-only, apply only to texts?
183 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
184 }
185 }
186 }
188 /**
189 * Return the desktop's current style.
190 */
191 SPCSSAttr *
192 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
193 {
194 SPCSSAttr *css = sp_repr_css_attr_new();
195 sp_repr_css_merge(css, desktop->current);
196 if (!css->attributeList()) {
197 sp_repr_css_attr_unref(css);
198 return NULL;
199 } else {
200 if (!with_text) {
201 css = sp_css_attr_unset_text(css);
202 }
203 return css;
204 }
205 }
207 /**
208 * Return the desktop's current color.
209 */
210 guint32
211 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
212 {
213 guint32 r = 0; // if there's no color, return black
214 gchar const *property = sp_repr_css_property(desktop->current,
215 is_fill ? "fill" : "stroke",
216 "#000");
218 if (desktop->current && property) { // if there is style and the property in it,
219 if (strncmp(property, "url", 3)) { // and if it's not url,
220 // read it
221 r = sp_svg_read_color(property, r);
222 }
223 }
225 return r;
226 }
228 double
229 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
230 {
231 SPCSSAttr *css = NULL;
232 gfloat value = 1.0; // default if nothing else found
233 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
234 css = sp_desktop_get_style(desktop, true);
235 } else {
236 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
237 if (tool_repr) {
238 css = sp_repr_css_attr_inherited(tool_repr, "style");
239 }
240 }
242 if (css) {
243 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
245 if (desktop->current && property) { // if there is style and the property in it,
246 if ( !sp_svg_number_read_f(property, &value) ) {
247 value = 1.0; // things failed. set back to the default
248 }
249 }
251 sp_repr_css_attr_unref(css);
252 }
254 return value;
255 }
256 double
257 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
258 {
259 SPCSSAttr *css = NULL;
260 gfloat value = 1.0; // default if nothing else found
261 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
262 css = sp_desktop_get_style(desktop, true);
263 } else {
264 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
265 if (tool_repr) {
266 css = sp_repr_css_attr_inherited(tool_repr, "style");
267 }
268 }
270 if (css) {
271 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
273 if (desktop->current && property) { // if there is style and the property in it,
274 if ( !sp_svg_number_read_f(property, &value) ) {
275 value = 1.0; // things failed. set back to the default
276 }
277 }
279 sp_repr_css_attr_unref(css);
280 }
282 return value;
283 }
284 guint32
285 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
286 {
287 SPCSSAttr *css = NULL;
288 guint32 r = 0; // if there's no color, return black
289 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
290 css = sp_desktop_get_style(desktop, true);
291 } else {
292 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
293 if (tool_repr) {
294 css = sp_repr_css_attr_inherited(tool_repr, "style");
295 }
296 }
298 if (css) {
299 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
301 if (desktop->current && property) { // if there is style and the property in it,
302 if (strncmp(property, "url", 3)) { // and if it's not url,
303 // read it
304 r = sp_svg_read_color(property, r);
305 }
306 }
308 sp_repr_css_attr_unref(css);
309 }
311 return r | 0xff;
312 }
313 /**
314 * Apply the desktop's current style or the tool style to repr.
315 */
316 void
317 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
318 {
319 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
320 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
321 sp_repr_css_set(repr, css_current, "style");
322 } else {
323 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
324 if (tool_repr) {
325 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
326 sp_repr_css_set(repr, css, "style");
327 sp_repr_css_attr_unref(css);
328 }
329 }
330 if (css_current) {
331 sp_repr_css_attr_unref(css_current);
332 }
333 }
335 /**
336 * Returns the font size (in SVG pixels) of the text tool style (if text
337 * tool uses its own style) or desktop style (otherwise).
338 */
339 double
340 sp_desktop_get_font_size_tool(SPDesktop *desktop)
341 {
342 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
343 gchar const *style_str = NULL;
344 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
345 style_str = desktop_style;
346 } else {
347 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
348 if (tool_repr) {
349 style_str = tool_repr->attribute("style");
350 }
351 }
353 double ret = 12;
354 if (style_str) {
355 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
356 sp_style_merge_from_style_string(style, style_str);
357 ret = style->font_size.computed;
358 sp_style_unref(style);
359 }
360 return ret;
361 }
363 /** Determine average stroke width, simple method */
364 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
365 gdouble
366 stroke_average_width (GSList const *objects)
367 {
368 if (g_slist_length ((GSList *) objects) == 0)
369 return NR_HUGE;
371 gdouble avgwidth = 0.0;
372 bool notstroked = true;
373 int n_notstroked = 0;
375 for (GSList const *l = objects; l != NULL; l = l->next) {
376 if (!SP_IS_ITEM (l->data))
377 continue;
379 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
381 SPObject *object = SP_OBJECT(l->data);
383 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
384 ++n_notstroked; // do not count nonstroked objects
385 continue;
386 } else {
387 notstroked = false;
388 }
390 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
391 }
393 if (notstroked)
394 return NR_HUGE;
396 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
397 }
399 /**
400 * Write to style_res the average fill or stroke of list of objects, if applicable.
401 */
402 int
403 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
404 {
405 if (g_slist_length(objects) == 0) {
406 /* No objects, set empty */
407 return QUERY_STYLE_NOTHING;
408 }
410 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
411 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
412 paint_res->set = TRUE;
414 gfloat c[4];
415 c[0] = c[1] = c[2] = c[3] = 0.0;
416 gint num = 0;
418 gfloat prev[3];
419 prev[0] = prev[1] = prev[2] = 0.0;
420 bool same_color = true;
422 for (GSList const *i = objects; i != NULL; i = i->next) {
423 SPObject *obj = SP_OBJECT (i->data);
424 SPStyle *style = SP_OBJECT_STYLE (obj);
425 if (!style) continue;
427 SPIPaint *paint = isfill? &style->fill : &style->stroke;
429 // We consider paint "effectively set" for anything within text hierarchy
430 SPObject *parent = SP_OBJECT_PARENT (obj);
431 bool paint_effectively_set =
432 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
433 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
434 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
436 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
438 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
439 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
440 }
442 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
443 // both previous paint and this paint were a server, see if the servers are compatible
445 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
446 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
448 if (SP_IS_LINEARGRADIENT (server_res)) {
450 if (!SP_IS_LINEARGRADIENT(server))
451 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
453 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
454 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
455 if (vector_res != vector)
456 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
458 } else if (SP_IS_RADIALGRADIENT (server_res)) {
460 if (!SP_IS_RADIALGRADIENT(server))
461 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
463 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
464 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
465 if (vector_res != vector)
466 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
468 } else if (SP_IS_PATTERN (server_res)) {
470 if (!SP_IS_PATTERN(server))
471 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
473 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
474 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
475 if (pat_res != pat)
476 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
477 }
478 }
480 // 2. Sum color, copy server from paint to paint_res
482 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
484 gfloat d[3];
485 sp_color_get_rgb_floatv (&paint->value.color, d);
487 // Check if this color is the same as previous
488 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
489 prev[0] = d[0];
490 prev[1] = d[1];
491 prev[2] = d[2];
492 } else {
493 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
494 same_color = false;
495 }
497 // average color
498 c[0] += d[0];
499 c[1] += d[1];
500 c[2] += d[2];
501 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
502 num ++;
503 }
505 paint_res->type = paint->type;
506 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
507 if (isfill) {
508 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
509 } else {
510 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
511 }
512 }
513 paint_res->set = paint_effectively_set;
514 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
515 }
517 // After all objects processed, divide the color if necessary and return
518 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
519 g_assert (num >= 1);
521 c[0] /= num;
522 c[1] /= num;
523 c[2] /= num;
524 c[3] /= num;
525 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
526 if (isfill) {
527 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
528 } else {
529 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
530 }
531 if (num > 1) {
532 if (same_color)
533 return QUERY_STYLE_MULTIPLE_SAME;
534 else
535 return QUERY_STYLE_MULTIPLE_AVERAGED;
536 } else {
537 return QUERY_STYLE_SINGLE;
538 }
539 }
541 // Not color
542 if (g_slist_length(objects) > 1) {
543 return QUERY_STYLE_MULTIPLE_SAME;
544 } else {
545 return QUERY_STYLE_SINGLE;
546 }
547 }
549 /**
550 * Write to style_res the average opacity of a list of objects.
551 */
552 int
553 objects_query_opacity (GSList *objects, SPStyle *style_res)
554 {
555 if (g_slist_length(objects) == 0) {
556 /* No objects, set empty */
557 return QUERY_STYLE_NOTHING;
558 }
560 gdouble opacity_sum = 0;
561 gdouble opacity_prev = -1;
562 bool same_opacity = true;
563 guint opacity_items = 0;
566 for (GSList const *i = objects; i != NULL; i = i->next) {
567 SPObject *obj = SP_OBJECT (i->data);
568 SPStyle *style = SP_OBJECT_STYLE (obj);
569 if (!style) continue;
571 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
572 opacity_sum += opacity;
573 if (opacity_prev != -1 && opacity != opacity_prev)
574 same_opacity = false;
575 opacity_prev = opacity;
576 opacity_items ++;
577 }
578 if (opacity_items > 1)
579 opacity_sum /= opacity_items;
581 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
583 if (opacity_items == 0) {
584 return QUERY_STYLE_NOTHING;
585 } else if (opacity_items == 1) {
586 return QUERY_STYLE_SINGLE;
587 } else {
588 if (same_opacity)
589 return QUERY_STYLE_MULTIPLE_SAME;
590 else
591 return QUERY_STYLE_MULTIPLE_AVERAGED;
592 }
593 }
595 /**
596 * Write to style_res the average stroke width of a list of objects.
597 */
598 int
599 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
600 {
601 if (g_slist_length(objects) == 0) {
602 /* No objects, set empty */
603 return QUERY_STYLE_NOTHING;
604 }
606 gdouble avgwidth = 0.0;
608 gdouble prev_sw = -1;
609 bool same_sw = true;
611 int n_stroked = 0;
613 for (GSList const *i = objects; i != NULL; i = i->next) {
614 SPObject *obj = SP_OBJECT (i->data);
615 if (!SP_IS_ITEM(obj)) continue;
616 SPStyle *style = SP_OBJECT_STYLE (obj);
617 if (!style) continue;
619 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
620 continue;
621 }
623 n_stroked ++;
625 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
626 double sw = style->stroke_width.computed * i2d.expansion();
628 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
629 same_sw = false;
630 prev_sw = sw;
632 avgwidth += sw;
633 }
635 if (n_stroked > 1)
636 avgwidth /= (n_stroked);
638 style_res->stroke_width.computed = avgwidth;
639 style_res->stroke_width.set = true;
641 if (n_stroked == 0) {
642 return QUERY_STYLE_NOTHING;
643 } else if (n_stroked == 1) {
644 return QUERY_STYLE_SINGLE;
645 } else {
646 if (same_sw)
647 return QUERY_STYLE_MULTIPLE_SAME;
648 else
649 return QUERY_STYLE_MULTIPLE_AVERAGED;
650 }
651 }
653 /**
654 * Write to style_res the average miter limit of a list of objects.
655 */
656 int
657 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
658 {
659 if (g_slist_length(objects) == 0) {
660 /* No objects, set empty */
661 return QUERY_STYLE_NOTHING;
662 }
664 gdouble avgml = 0.0;
665 int n_stroked = 0;
667 gdouble prev_ml = -1;
668 bool same_ml = true;
670 for (GSList const *i = objects; i != NULL; i = i->next) {
671 SPObject *obj = SP_OBJECT (i->data);
672 if (!SP_IS_ITEM(obj)) continue;
673 SPStyle *style = SP_OBJECT_STYLE (obj);
674 if (!style) continue;
676 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
677 continue;
678 }
680 n_stroked ++;
682 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
683 same_ml = false;
684 prev_ml = style->stroke_miterlimit.value;
686 avgml += style->stroke_miterlimit.value;
687 }
689 if (n_stroked > 1)
690 avgml /= (n_stroked);
692 style_res->stroke_miterlimit.value = avgml;
693 style_res->stroke_miterlimit.set = true;
695 if (n_stroked == 0) {
696 return QUERY_STYLE_NOTHING;
697 } else if (n_stroked == 1) {
698 return QUERY_STYLE_SINGLE;
699 } else {
700 if (same_ml)
701 return QUERY_STYLE_MULTIPLE_SAME;
702 else
703 return QUERY_STYLE_MULTIPLE_AVERAGED;
704 }
705 }
707 /**
708 * Write to style_res the stroke cap of a list of objects.
709 */
710 int
711 objects_query_strokecap (GSList *objects, SPStyle *style_res)
712 {
713 if (g_slist_length(objects) == 0) {
714 /* No objects, set empty */
715 return QUERY_STYLE_NOTHING;
716 }
718 int cap = -1;
719 gdouble prev_cap = -1;
720 bool same_cap = true;
721 int n_stroked = 0;
723 for (GSList const *i = objects; i != NULL; i = i->next) {
724 SPObject *obj = SP_OBJECT (i->data);
725 if (!SP_IS_ITEM(obj)) continue;
726 SPStyle *style = SP_OBJECT_STYLE (obj);
727 if (!style) continue;
729 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
730 continue;
731 }
733 n_stroked ++;
735 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
736 same_cap = false;
737 prev_cap = style->stroke_linecap.value;
739 cap = style->stroke_linecap.value;
740 }
742 style_res->stroke_linecap.value = cap;
743 style_res->stroke_linecap.set = true;
745 if (n_stroked == 0) {
746 return QUERY_STYLE_NOTHING;
747 } else if (n_stroked == 1) {
748 return QUERY_STYLE_SINGLE;
749 } else {
750 if (same_cap)
751 return QUERY_STYLE_MULTIPLE_SAME;
752 else
753 return QUERY_STYLE_MULTIPLE_DIFFERENT;
754 }
755 }
757 /**
758 * Write to style_res the stroke join of a list of objects.
759 */
760 int
761 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
762 {
763 if (g_slist_length(objects) == 0) {
764 /* No objects, set empty */
765 return QUERY_STYLE_NOTHING;
766 }
768 int join = -1;
769 gdouble prev_join = -1;
770 bool same_join = true;
771 int n_stroked = 0;
773 for (GSList const *i = objects; i != NULL; i = i->next) {
774 SPObject *obj = SP_OBJECT (i->data);
775 if (!SP_IS_ITEM(obj)) continue;
776 SPStyle *style = SP_OBJECT_STYLE (obj);
777 if (!style) continue;
779 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
780 continue;
781 }
783 n_stroked ++;
785 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
786 same_join = false;
787 prev_join = style->stroke_linejoin.value;
789 join = style->stroke_linejoin.value;
790 }
792 style_res->stroke_linejoin.value = join;
793 style_res->stroke_linejoin.set = true;
795 if (n_stroked == 0) {
796 return QUERY_STYLE_NOTHING;
797 } else if (n_stroked == 1) {
798 return QUERY_STYLE_SINGLE;
799 } else {
800 if (same_join)
801 return QUERY_STYLE_MULTIPLE_SAME;
802 else
803 return QUERY_STYLE_MULTIPLE_DIFFERENT;
804 }
805 }
807 /**
808 * Write to style_res the average font size and spacing of objects.
809 */
810 int
811 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
812 {
813 bool different = false;
815 double size = 0;
816 double letterspacing = 0;
817 double linespacing = 0;
818 bool linespacing_normal = false;
819 bool letterspacing_normal = false;
821 double size_prev = 0;
822 double letterspacing_prev = 0;
823 double linespacing_prev = 0;
825 /// \todo FIXME: add word spacing, kerns? rotates?
827 int texts = 0;
829 for (GSList const *i = objects; i != NULL; i = i->next) {
830 SPObject *obj = SP_OBJECT (i->data);
832 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
833 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
834 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
835 continue;
837 SPStyle *style = SP_OBJECT_STYLE (obj);
838 if (!style) continue;
840 texts ++;
841 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
843 if (style->letter_spacing.normal) {
844 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
845 letterspacing_normal = true;
846 } else {
847 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
848 letterspacing_normal = false;
849 }
851 double linespacing_current;
852 if (style->line_height.normal) {
853 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
854 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
855 linespacing_normal = true;
856 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
857 linespacing_current = style->line_height.value;
858 linespacing_normal = false;
859 } else { // we need % here
860 linespacing_current = style->line_height.computed / style->font_size.computed;
861 linespacing_normal = false;
862 }
863 linespacing += linespacing_current;
865 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
866 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
867 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
868 different = true;
869 }
871 size_prev = style->font_size.computed;
872 letterspacing_prev = style->letter_spacing.computed;
873 linespacing_prev = linespacing_current;
875 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
876 style_res->text_anchor.computed = style->text_anchor.computed;
877 style_res->writing_mode.computed = style->writing_mode.computed;
878 }
880 if (texts == 0)
881 return QUERY_STYLE_NOTHING;
883 if (texts > 1) {
884 size /= texts;
885 letterspacing /= texts;
886 linespacing /= texts;
887 }
889 style_res->font_size.computed = size;
890 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
892 style_res->letter_spacing.normal = letterspacing_normal;
893 style_res->letter_spacing.computed = letterspacing;
895 style_res->line_height.normal = linespacing_normal;
896 style_res->line_height.computed = linespacing;
897 style_res->line_height.value = linespacing;
898 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
900 if (texts > 1) {
901 if (different) {
902 return QUERY_STYLE_MULTIPLE_AVERAGED;
903 } else {
904 return QUERY_STYLE_MULTIPLE_SAME;
905 }
906 } else {
907 return QUERY_STYLE_SINGLE;
908 }
909 }
911 /**
912 * Write to style_res the average font style of objects.
913 */
914 int
915 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
916 {
917 bool different = false;
918 bool set = false;
920 int texts = 0;
922 for (GSList const *i = objects; i != NULL; i = i->next) {
923 SPObject *obj = SP_OBJECT (i->data);
925 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
926 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
927 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
928 continue;
930 SPStyle *style = SP_OBJECT_STYLE (obj);
931 if (!style) continue;
933 texts ++;
935 if (set &&
936 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
937 different = true; // different styles
938 }
940 set = TRUE;
941 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
942 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
943 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
944 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
945 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
946 }
948 if (texts == 0 || !set)
949 return QUERY_STYLE_NOTHING;
951 if (texts > 1) {
952 if (different) {
953 return QUERY_STYLE_MULTIPLE_DIFFERENT;
954 } else {
955 return QUERY_STYLE_MULTIPLE_SAME;
956 }
957 } else {
958 return QUERY_STYLE_SINGLE;
959 }
960 }
962 /**
963 * Write to style_res the average font family of objects.
964 */
965 int
966 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
967 {
968 bool different = false;
969 int texts = 0;
971 if (style_res->text->font_family.value) {
972 g_free(style_res->text->font_family.value);
973 style_res->text->font_family.value = NULL;
974 }
975 style_res->text->font_family.set = FALSE;
977 for (GSList const *i = objects; i != NULL; i = i->next) {
978 SPObject *obj = SP_OBJECT (i->data);
980 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
981 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
982 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
983 continue;
985 SPStyle *style = SP_OBJECT_STYLE (obj);
986 if (!style) continue;
988 texts ++;
990 if (style_res->text->font_family.value && style->text->font_family.value &&
991 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
992 different = true; // different fonts
993 }
995 if (style_res->text->font_family.value) {
996 g_free(style_res->text->font_family.value);
997 style_res->text->font_family.value = NULL;
998 }
1000 style_res->text->font_family.set = TRUE;
1001 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1002 }
1004 if (texts == 0 || !style_res->text->font_family.set)
1005 return QUERY_STYLE_NOTHING;
1007 if (texts > 1) {
1008 if (different) {
1009 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1010 } else {
1011 return QUERY_STYLE_MULTIPLE_SAME;
1012 }
1013 } else {
1014 return QUERY_STYLE_SINGLE;
1015 }
1016 }
1018 int
1019 objects_query_blend (GSList *objects, SPStyle *style_res)
1020 {
1021 const int empty_prev = -2;
1022 const int complex_filter = 5;
1023 int blend = 0;
1024 float blend_prev = empty_prev;
1025 bool same_blend = true;
1026 guint items = 0;
1028 for (GSList const *i = objects; i != NULL; i = i->next) {
1029 SPObject *obj = SP_OBJECT (i->data);
1030 SPStyle *style = SP_OBJECT_STYLE (obj);
1031 if(!style || !SP_IS_ITEM(obj)) continue;
1033 items++;
1035 //if object has a filter
1036 if (style->filter.set && style->getFilter()) {
1037 int blurcount = 0;
1038 int blendcount = 0;
1040 // determine whether filter is simple (blend and/or blur) or complex
1041 for(SPObject *primitive_obj = style->getFilter()->children;
1042 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1043 primitive_obj = primitive_obj->next) {
1044 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1045 if(SP_IS_FEBLEND(primitive))
1046 ++blendcount;
1047 else if(SP_IS_GAUSSIANBLUR(primitive))
1048 ++blurcount;
1049 else {
1050 blurcount = complex_filter;
1051 break;
1052 }
1053 }
1055 // simple filter
1056 if(blurcount == 1 || blendcount == 1) {
1057 for(SPObject *primitive_obj = style->getFilter()->children;
1058 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1059 primitive_obj = primitive_obj->next) {
1060 if(SP_IS_FEBLEND(primitive_obj)) {
1061 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1062 blend = spblend->blend_mode;
1063 }
1064 }
1065 }
1066 else {
1067 blend = complex_filter;
1068 }
1069 }
1070 // defaults to blend mode = "normal"
1071 else {
1072 blend = 0;
1073 }
1075 if(blend_prev != empty_prev && blend_prev != blend)
1076 same_blend = false;
1077 blend_prev = blend;
1078 }
1080 if (items > 0) {
1081 style_res->filter_blend_mode.value = blend;
1082 }
1084 if (items == 0) {
1085 return QUERY_STYLE_NOTHING;
1086 } else if (items == 1) {
1087 return QUERY_STYLE_SINGLE;
1088 } else {
1089 if(same_blend)
1090 return QUERY_STYLE_MULTIPLE_SAME;
1091 else
1092 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1093 }
1094 }
1096 /**
1097 * Write to style_res the average blurring of a list of objects.
1098 */
1099 int
1100 objects_query_blur (GSList *objects, SPStyle *style_res)
1101 {
1102 if (g_slist_length(objects) == 0) {
1103 /* No objects, set empty */
1104 return QUERY_STYLE_NOTHING;
1105 }
1107 float blur_sum = 0;
1108 float blur_prev = -1;
1109 bool same_blur = true;
1110 guint blur_items = 0;
1111 guint items = 0;
1113 for (GSList const *i = objects; i != NULL; i = i->next) {
1114 SPObject *obj = SP_OBJECT (i->data);
1115 SPStyle *style = SP_OBJECT_STYLE (obj);
1116 if (!style) continue;
1117 if (!SP_IS_ITEM(obj)) continue;
1119 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1121 items ++;
1123 //if object has a filter
1124 if (style->filter.set && style->getFilter()) {
1125 //cycle through filter primitives
1126 SPObject *primitive_obj = style->getFilter()->children;
1127 while (primitive_obj) {
1128 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1129 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1131 //if primitive is gaussianblur
1132 if(SP_IS_GAUSSIANBLUR(primitive)) {
1133 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1134 float num = spblur->stdDeviation.getNumber();
1135 blur_sum += num * NR::expansion(i2d);
1136 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
1137 same_blur = false;
1138 blur_prev = num;
1139 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1140 blur_items ++;
1141 }
1142 }
1143 primitive_obj = primitive_obj->next;
1144 }
1145 }
1146 }
1148 if (items > 0) {
1149 if (blur_items > 0)
1150 blur_sum /= blur_items;
1151 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1152 }
1154 if (items == 0) {
1155 return QUERY_STYLE_NOTHING;
1156 } else if (items == 1) {
1157 return QUERY_STYLE_SINGLE;
1158 } else {
1159 if (same_blur)
1160 return QUERY_STYLE_MULTIPLE_SAME;
1161 else
1162 return QUERY_STYLE_MULTIPLE_AVERAGED;
1163 }
1164 }
1166 /**
1167 * Query the given list of objects for the given property, write
1168 * the result to style, return appropriate flag.
1169 */
1170 int
1171 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1172 {
1173 if (property == QUERY_STYLE_PROPERTY_FILL) {
1174 return objects_query_fillstroke (list, style, true);
1175 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1176 return objects_query_fillstroke (list, style, false);
1178 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1179 return objects_query_strokewidth (list, style);
1180 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1181 return objects_query_miterlimit (list, style);
1182 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1183 return objects_query_strokecap (list, style);
1184 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1185 return objects_query_strokejoin (list, style);
1187 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1188 return objects_query_opacity (list, style);
1190 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1191 return objects_query_fontfamily (list, style);
1192 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1193 return objects_query_fontstyle (list, style);
1194 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1195 return objects_query_fontnumbers (list, style);
1197 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1198 return objects_query_blend (list, style);
1199 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1200 return objects_query_blur (list, style);
1201 }
1202 return QUERY_STYLE_NOTHING;
1203 }
1206 /**
1207 * Query the subselection (if any) or selection on the given desktop for the given property, write
1208 * the result to style, return appropriate flag.
1209 */
1210 int
1211 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1212 {
1213 int ret = desktop->_query_style_signal.emit(style, property);
1215 if (ret != QUERY_STYLE_NOTHING)
1216 return ret; // subselection returned a style, pass it on
1218 // otherwise, do querying and averaging over selection
1219 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1220 }
1222 /**
1223 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1224 * the properties returned QUERY_STYLE_NOTHING.
1225 */
1226 bool
1227 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1228 {
1229 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1230 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1231 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1232 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1233 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1234 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1235 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1236 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1237 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1238 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1239 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1241 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);
1242 }
1245 /*
1246 Local Variables:
1247 mode:c++
1248 c-file-style:"stroustrup"
1249 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1250 indent-tabs-mode:nil
1251 fill-column:99
1252 End:
1253 */
1254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :