1634db367e4907fea28018e790c369ab2e53e4d9
1 #define __SP_DESKTOP_STYLE_C__
3 /** \file
4 * Desktop style management
5 *
6 * Authors:
7 * bulia byak
8 * verbalshadow
9 *
10 * Copyright (C) 2004, 2006 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "desktop.h"
16 #include "color-rgba.h"
17 #include "svg/css-ostringstream.h"
18 #include "svg/svg.h"
19 #include "svg/svg-color.h"
20 #include "selection.h"
21 #include "sp-tspan.h"
22 #include "sp-textpath.h"
23 #include "inkscape.h"
24 #include "style.h"
25 #include "prefs-utils.h"
26 #include "sp-use.h"
27 #include "sp-feblend.h"
28 #include "sp-filter.h"
29 #include "sp-filter-reference.h"
30 #include "sp-gaussian-blur.h"
31 #include "sp-flowtext.h"
32 #include "sp-flowregion.h"
33 #include "sp-flowdiv.h"
34 #include "sp-linear-gradient.h"
35 #include "sp-radial-gradient.h"
36 #include "sp-pattern.h"
37 #include "xml/repr.h"
38 #include "libnrtype/font-style-to-pos.h"
40 #include "desktop-style.h"
42 /**
43 * Set color on selection on desktop.
44 */
45 void
46 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
47 {
48 /// \todo relative color setting
49 if (is_relative) {
50 g_warning("FIXME: relative color setting not yet implemented");
51 return;
52 }
54 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
55 gchar b[64];
56 sp_svg_write_color(b, 64, rgba);
57 SPCSSAttr *css = sp_repr_css_attr_new();
58 if (fill) {
59 sp_repr_css_set_property(css, "fill", b);
60 Inkscape::CSSOStringStream osalpha;
61 osalpha << color[3];
62 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
63 } else {
64 sp_repr_css_set_property(css, "stroke", b);
65 Inkscape::CSSOStringStream osalpha;
66 osalpha << color[3];
67 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
68 }
70 sp_desktop_set_style(desktop, css);
72 sp_repr_css_attr_unref(css);
73 }
75 /**
76 * Apply style on object and children, recursively.
77 */
78 void
79 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
80 {
81 // non-items should not have style
82 if (!SP_IS_ITEM(o))
83 return;
85 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
86 // but must always inherit from the parent text. Same for textPath.
87 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
89 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
90 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
91 // flowtext).
93 if (!(skip_lines
94 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
95 || SP_IS_FLOWDIV(o)
96 || SP_IS_FLOWPARA(o)
97 || SP_IS_TEXTPATH(o))
98 && !SP_OBJECT_REPR(o)->attribute("style"))
99 &&
100 !(SP_IS_FLOWREGION(o) ||
101 SP_IS_FLOWREGIONEXCLUDE(o) ||
102 (SP_IS_USE(o) &&
103 SP_OBJECT_PARENT(o) &&
104 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
105 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
106 )
107 )
108 )
109 ) {
111 SPCSSAttr *css_set = sp_repr_css_attr_new();
112 sp_repr_css_merge(css_set, css);
114 // Scale the style by the inverse of the accumulated parent transform in the paste context.
115 {
116 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
117 double const ex(NR::expansion(local));
118 if ( ( ex != 0. )
119 && ( ex != 1. ) ) {
120 sp_css_attr_scale(css_set, 1/ex);
121 }
122 }
124 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
126 sp_repr_css_attr_unref(css_set);
127 }
129 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
130 if (SP_IS_USE(o))
131 return;
133 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
134 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
135 // Unset properties which are accumulating and thus should not be set recursively.
136 // 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.
137 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
138 sp_repr_css_merge(css_recurse, css);
139 sp_repr_css_set_property(css_recurse, "opacity", NULL);
140 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
141 sp_repr_css_attr_unref(css_recurse);
142 } else {
143 sp_desktop_apply_css_recursive(child, css, skip_lines);
144 }
145 }
146 }
148 /**
149 * Apply style on selection on desktop.
150 */
151 void
152 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
153 {
154 if (write_current) {
155 // 1. Set internal value
156 sp_repr_css_merge(desktop->current, css);
158 // 1a. Write to prefs; make a copy and unset any URIs first
159 SPCSSAttr *css_write = sp_repr_css_attr_new();
160 sp_repr_css_merge(css_write, css);
161 sp_css_attr_unset_uris(css_write);
162 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
163 sp_repr_css_attr_unref(css_write);
164 }
166 if (!change)
167 return;
169 // 2. Emit signal
170 bool intercepted = desktop->_set_style_signal.emit(css);
172 /** \todo
173 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
174 * rect corners, font size for the object's own transform so that pasting
175 * fills does not depend on preserve/optimize.
176 */
178 // 3. If nobody has intercepted the signal, apply the style to the selection
179 if (!intercepted) {
180 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
181 /// \todo if the style is text-only, apply only to texts?
182 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
183 }
184 }
185 }
187 /**
188 * Return the desktop's current style.
189 */
190 SPCSSAttr *
191 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
192 {
193 SPCSSAttr *css = sp_repr_css_attr_new();
194 sp_repr_css_merge(css, desktop->current);
195 if (!css->attributeList()) {
196 sp_repr_css_attr_unref(css);
197 return NULL;
198 } else {
199 if (!with_text) {
200 css = sp_css_attr_unset_text(css);
201 }
202 return css;
203 }
204 }
206 /**
207 * Return the desktop's current color.
208 */
209 guint32
210 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
211 {
212 guint32 r = 0; // if there's no color, return black
213 gchar const *property = sp_repr_css_property(desktop->current,
214 is_fill ? "fill" : "stroke",
215 "#000");
217 if (desktop->current && property) { // if there is style and the property in it,
218 if (strncmp(property, "url", 3)) { // and if it's not url,
219 // read it
220 r = sp_svg_read_color(property, r);
221 }
222 }
224 return r;
225 }
227 double
228 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
229 {
230 SPCSSAttr *css = NULL;
231 gfloat value = 1.0; // default if nothing else found
232 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
233 css = sp_desktop_get_style(desktop, true);
234 } else {
235 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
236 if (tool_repr) {
237 css = sp_repr_css_attr_inherited(tool_repr, "style");
238 }
239 }
241 if (css) {
242 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
244 if (desktop->current && property) { // if there is style and the property in it,
245 if ( !sp_svg_number_read_f(property, &value) ) {
246 value = 1.0; // things failed. set back to the default
247 }
248 }
250 sp_repr_css_attr_unref(css);
251 }
253 return value;
254 }
255 double
256 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
257 {
258 SPCSSAttr *css = NULL;
259 gfloat value = 1.0; // default if nothing else found
260 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
261 css = sp_desktop_get_style(desktop, true);
262 } else {
263 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
264 if (tool_repr) {
265 css = sp_repr_css_attr_inherited(tool_repr, "style");
266 }
267 }
269 if (css) {
270 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
272 if (desktop->current && property) { // if there is style and the property in it,
273 if ( !sp_svg_number_read_f(property, &value) ) {
274 value = 1.0; // things failed. set back to the default
275 }
276 }
278 sp_repr_css_attr_unref(css);
279 }
281 return value;
282 }
283 guint32
284 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
285 {
286 SPCSSAttr *css = NULL;
287 guint32 r = 0; // if there's no color, return black
288 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
289 css = sp_desktop_get_style(desktop, true);
290 } else {
291 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
292 if (tool_repr) {
293 css = sp_repr_css_attr_inherited(tool_repr, "style");
294 }
295 }
297 if (css) {
298 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
300 if (desktop->current && property) { // if there is style and the property in it,
301 if (strncmp(property, "url", 3)) { // and if it's not url,
302 // read it
303 r = sp_svg_read_color(property, r);
304 }
305 }
307 sp_repr_css_attr_unref(css);
308 }
310 return r | 0xff;
311 }
312 /**
313 * Apply the desktop's current style or the tool style to repr.
314 */
315 void
316 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
317 {
318 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
319 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
320 sp_repr_css_set(repr, css_current, "style");
321 } else {
322 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
323 if (tool_repr) {
324 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
325 sp_repr_css_set(repr, css, "style");
326 sp_repr_css_attr_unref(css);
327 }
328 }
329 if (css_current) {
330 sp_repr_css_attr_unref(css_current);
331 }
332 }
334 /**
335 * Returns the font size (in SVG pixels) of the text tool style (if text
336 * tool uses its own style) or desktop style (otherwise).
337 */
338 double
339 sp_desktop_get_font_size_tool(SPDesktop *desktop)
340 {
341 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
342 gchar const *style_str = NULL;
343 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
344 style_str = desktop_style;
345 } else {
346 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
347 if (tool_repr) {
348 style_str = tool_repr->attribute("style");
349 }
350 }
352 double ret = 12;
353 if (style_str) {
354 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
355 sp_style_merge_from_style_string(style, style_str);
356 ret = style->font_size.computed;
357 sp_style_unref(style);
358 }
359 return ret;
360 }
362 /** Determine average stroke width, simple method */
363 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
364 gdouble
365 stroke_average_width (GSList const *objects)
366 {
367 if (g_slist_length ((GSList *) objects) == 0)
368 return NR_HUGE;
370 gdouble avgwidth = 0.0;
371 bool notstroked = true;
372 int n_notstroked = 0;
374 for (GSList const *l = objects; l != NULL; l = l->next) {
375 if (!SP_IS_ITEM (l->data))
376 continue;
378 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
380 SPObject *object = SP_OBJECT(l->data);
382 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
383 ++n_notstroked; // do not count nonstroked objects
384 continue;
385 } else {
386 notstroked = false;
387 }
389 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
390 }
392 if (notstroked)
393 return NR_HUGE;
395 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
396 }
398 /**
399 * Write to style_res the average fill or stroke of list of objects, if applicable.
400 */
401 int
402 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
403 {
404 if (g_slist_length(objects) == 0) {
405 /* No objects, set empty */
406 return QUERY_STYLE_NOTHING;
407 }
409 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
410 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
411 paint_res->set = TRUE;
413 gfloat c[4];
414 c[0] = c[1] = c[2] = c[3] = 0.0;
415 gint num = 0;
417 gfloat prev[3];
418 prev[0] = prev[1] = prev[2] = 0.0;
419 bool same_color = true;
421 for (GSList const *i = objects; i != NULL; i = i->next) {
422 SPObject *obj = SP_OBJECT (i->data);
423 SPStyle *style = SP_OBJECT_STYLE (obj);
424 if (!style) continue;
426 SPIPaint *paint = isfill? &style->fill : &style->stroke;
428 // We consider paint "effectively set" for anything within text hierarchy
429 SPObject *parent = SP_OBJECT_PARENT (obj);
430 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));
432 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
434 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
435 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
436 }
438 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
439 // both previous paint and this paint were a server, see if the servers are compatible
441 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
442 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
444 if (SP_IS_LINEARGRADIENT (server_res)) {
446 if (!SP_IS_LINEARGRADIENT(server))
447 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
449 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
450 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
451 if (vector_res != vector)
452 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
454 } else if (SP_IS_RADIALGRADIENT (server_res)) {
456 if (!SP_IS_RADIALGRADIENT(server))
457 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
459 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
460 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
461 if (vector_res != vector)
462 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
464 } else if (SP_IS_PATTERN (server_res)) {
466 if (!SP_IS_PATTERN(server))
467 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
469 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
470 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
471 if (pat_res != pat)
472 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
473 }
474 }
476 // 2. Sum color, copy server from paint to paint_res
478 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
480 gfloat d[3];
481 sp_color_get_rgb_floatv (&paint->value.color, d);
483 // Check if this color is the same as previous
484 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
485 prev[0] = d[0];
486 prev[1] = d[1];
487 prev[2] = d[2];
488 } else {
489 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
490 same_color = false;
491 }
493 // average color
494 c[0] += d[0];
495 c[1] += d[1];
496 c[2] += d[2];
497 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
498 num ++;
499 }
501 paint_res->type = paint->type;
502 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
503 if (isfill) {
504 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
505 } else {
506 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
507 }
508 }
509 paint_res->set = paint_effectively_set;
510 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
511 }
513 // After all objects processed, divide the color if necessary and return
514 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
515 g_assert (num >= 1);
517 c[0] /= num;
518 c[1] /= num;
519 c[2] /= num;
520 c[3] /= num;
521 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
522 if (isfill) {
523 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
524 } else {
525 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
526 }
527 if (num > 1) {
528 if (same_color)
529 return QUERY_STYLE_MULTIPLE_SAME;
530 else
531 return QUERY_STYLE_MULTIPLE_AVERAGED;
532 } else {
533 return QUERY_STYLE_SINGLE;
534 }
535 }
537 // Not color
538 if (g_slist_length(objects) > 1) {
539 return QUERY_STYLE_MULTIPLE_SAME;
540 } else {
541 return QUERY_STYLE_SINGLE;
542 }
543 }
545 /**
546 * Write to style_res the average opacity of a list of objects.
547 */
548 int
549 objects_query_opacity (GSList *objects, SPStyle *style_res)
550 {
551 if (g_slist_length(objects) == 0) {
552 /* No objects, set empty */
553 return QUERY_STYLE_NOTHING;
554 }
556 gdouble opacity_sum = 0;
557 gdouble opacity_prev = -1;
558 bool same_opacity = true;
559 guint opacity_items = 0;
562 for (GSList const *i = objects; i != NULL; i = i->next) {
563 SPObject *obj = SP_OBJECT (i->data);
564 SPStyle *style = SP_OBJECT_STYLE (obj);
565 if (!style) continue;
567 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
568 opacity_sum += opacity;
569 if (opacity_prev != -1 && opacity != opacity_prev)
570 same_opacity = false;
571 opacity_prev = opacity;
572 opacity_items ++;
573 }
574 if (opacity_items > 1)
575 opacity_sum /= opacity_items;
577 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
579 if (opacity_items == 0) {
580 return QUERY_STYLE_NOTHING;
581 } else if (opacity_items == 1) {
582 return QUERY_STYLE_SINGLE;
583 } else {
584 if (same_opacity)
585 return QUERY_STYLE_MULTIPLE_SAME;
586 else
587 return QUERY_STYLE_MULTIPLE_AVERAGED;
588 }
589 }
591 /**
592 * Write to style_res the average stroke width of a list of objects.
593 */
594 int
595 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
596 {
597 if (g_slist_length(objects) == 0) {
598 /* No objects, set empty */
599 return QUERY_STYLE_NOTHING;
600 }
602 gdouble avgwidth = 0.0;
604 gdouble prev_sw = -1;
605 bool same_sw = true;
607 int n_stroked = 0;
609 for (GSList const *i = objects; i != NULL; i = i->next) {
610 SPObject *obj = SP_OBJECT (i->data);
611 if (!SP_IS_ITEM(obj)) continue;
612 SPStyle *style = SP_OBJECT_STYLE (obj);
613 if (!style) continue;
615 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
616 continue;
617 }
619 n_stroked ++;
621 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
622 double sw = style->stroke_width.computed * i2d.expansion();
624 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
625 same_sw = false;
626 prev_sw = sw;
628 avgwidth += sw;
629 }
631 if (n_stroked > 1)
632 avgwidth /= (n_stroked);
634 style_res->stroke_width.computed = avgwidth;
635 style_res->stroke_width.set = true;
637 if (n_stroked == 0) {
638 return QUERY_STYLE_NOTHING;
639 } else if (n_stroked == 1) {
640 return QUERY_STYLE_SINGLE;
641 } else {
642 if (same_sw)
643 return QUERY_STYLE_MULTIPLE_SAME;
644 else
645 return QUERY_STYLE_MULTIPLE_AVERAGED;
646 }
647 }
649 /**
650 * Write to style_res the average miter limit of a list of objects.
651 */
652 int
653 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
654 {
655 if (g_slist_length(objects) == 0) {
656 /* No objects, set empty */
657 return QUERY_STYLE_NOTHING;
658 }
660 gdouble avgml = 0.0;
661 int n_stroked = 0;
663 gdouble prev_ml = -1;
664 bool same_ml = true;
666 for (GSList const *i = objects; i != NULL; i = i->next) {
667 SPObject *obj = SP_OBJECT (i->data);
668 if (!SP_IS_ITEM(obj)) continue;
669 SPStyle *style = SP_OBJECT_STYLE (obj);
670 if (!style) continue;
672 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
673 continue;
674 }
676 n_stroked ++;
678 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
679 same_ml = false;
680 prev_ml = style->stroke_miterlimit.value;
682 avgml += style->stroke_miterlimit.value;
683 }
685 if (n_stroked > 1)
686 avgml /= (n_stroked);
688 style_res->stroke_miterlimit.value = avgml;
689 style_res->stroke_miterlimit.set = true;
691 if (n_stroked == 0) {
692 return QUERY_STYLE_NOTHING;
693 } else if (n_stroked == 1) {
694 return QUERY_STYLE_SINGLE;
695 } else {
696 if (same_ml)
697 return QUERY_STYLE_MULTIPLE_SAME;
698 else
699 return QUERY_STYLE_MULTIPLE_AVERAGED;
700 }
701 }
703 /**
704 * Write to style_res the stroke cap of a list of objects.
705 */
706 int
707 objects_query_strokecap (GSList *objects, SPStyle *style_res)
708 {
709 if (g_slist_length(objects) == 0) {
710 /* No objects, set empty */
711 return QUERY_STYLE_NOTHING;
712 }
714 int cap = -1;
715 gdouble prev_cap = -1;
716 bool same_cap = true;
717 int n_stroked = 0;
719 for (GSList const *i = objects; i != NULL; i = i->next) {
720 SPObject *obj = SP_OBJECT (i->data);
721 if (!SP_IS_ITEM(obj)) continue;
722 SPStyle *style = SP_OBJECT_STYLE (obj);
723 if (!style) continue;
725 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
726 continue;
727 }
729 n_stroked ++;
731 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
732 same_cap = false;
733 prev_cap = style->stroke_linecap.value;
735 cap = style->stroke_linecap.value;
736 }
738 style_res->stroke_linecap.value = cap;
739 style_res->stroke_linecap.set = true;
741 if (n_stroked == 0) {
742 return QUERY_STYLE_NOTHING;
743 } else if (n_stroked == 1) {
744 return QUERY_STYLE_SINGLE;
745 } else {
746 if (same_cap)
747 return QUERY_STYLE_MULTIPLE_SAME;
748 else
749 return QUERY_STYLE_MULTIPLE_DIFFERENT;
750 }
751 }
753 /**
754 * Write to style_res the stroke join of a list of objects.
755 */
756 int
757 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
758 {
759 if (g_slist_length(objects) == 0) {
760 /* No objects, set empty */
761 return QUERY_STYLE_NOTHING;
762 }
764 int join = -1;
765 gdouble prev_join = -1;
766 bool same_join = true;
767 int n_stroked = 0;
769 for (GSList const *i = objects; i != NULL; i = i->next) {
770 SPObject *obj = SP_OBJECT (i->data);
771 if (!SP_IS_ITEM(obj)) continue;
772 SPStyle *style = SP_OBJECT_STYLE (obj);
773 if (!style) continue;
775 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
776 continue;
777 }
779 n_stroked ++;
781 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
782 same_join = false;
783 prev_join = style->stroke_linejoin.value;
785 join = style->stroke_linejoin.value;
786 }
788 style_res->stroke_linejoin.value = join;
789 style_res->stroke_linejoin.set = true;
791 if (n_stroked == 0) {
792 return QUERY_STYLE_NOTHING;
793 } else if (n_stroked == 1) {
794 return QUERY_STYLE_SINGLE;
795 } else {
796 if (same_join)
797 return QUERY_STYLE_MULTIPLE_SAME;
798 else
799 return QUERY_STYLE_MULTIPLE_DIFFERENT;
800 }
801 }
803 /**
804 * Write to style_res the average font size and spacing of objects.
805 */
806 int
807 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
808 {
809 bool different = false;
811 double size = 0;
812 double letterspacing = 0;
813 double linespacing = 0;
814 bool linespacing_normal = false;
815 bool letterspacing_normal = false;
817 double size_prev = 0;
818 double letterspacing_prev = 0;
819 double linespacing_prev = 0;
821 /// \todo FIXME: add word spacing, kerns? rotates?
823 int texts = 0;
825 for (GSList const *i = objects; i != NULL; i = i->next) {
826 SPObject *obj = SP_OBJECT (i->data);
828 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
829 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
830 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
831 continue;
833 SPStyle *style = SP_OBJECT_STYLE (obj);
834 if (!style) continue;
836 texts ++;
837 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
839 if (style->letter_spacing.normal) {
840 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
841 letterspacing_normal = true;
842 } else {
843 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
844 letterspacing_normal = false;
845 }
847 double linespacing_current;
848 if (style->line_height.normal) {
849 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
850 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
851 linespacing_normal = true;
852 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
853 linespacing_current = style->line_height.value;
854 linespacing_normal = false;
855 } else { // we need % here
856 linespacing_current = style->line_height.computed / style->font_size.computed;
857 linespacing_normal = false;
858 }
859 linespacing += linespacing_current;
861 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
862 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
863 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
864 different = true;
865 }
867 size_prev = style->font_size.computed;
868 letterspacing_prev = style->letter_spacing.computed;
869 linespacing_prev = linespacing_current;
871 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
872 style_res->text_anchor.computed = style->text_anchor.computed;
873 style_res->writing_mode.computed = style->writing_mode.computed;
874 }
876 if (texts == 0)
877 return QUERY_STYLE_NOTHING;
879 if (texts > 1) {
880 size /= texts;
881 letterspacing /= texts;
882 linespacing /= texts;
883 }
885 style_res->font_size.computed = size;
886 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
888 style_res->letter_spacing.normal = letterspacing_normal;
889 style_res->letter_spacing.computed = letterspacing;
891 style_res->line_height.normal = linespacing_normal;
892 style_res->line_height.computed = linespacing;
893 style_res->line_height.value = linespacing;
894 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
896 if (texts > 1) {
897 if (different) {
898 return QUERY_STYLE_MULTIPLE_AVERAGED;
899 } else {
900 return QUERY_STYLE_MULTIPLE_SAME;
901 }
902 } else {
903 return QUERY_STYLE_SINGLE;
904 }
905 }
907 /**
908 * Write to style_res the average font style of objects.
909 */
910 int
911 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
912 {
913 bool different = false;
914 bool set = false;
916 int texts = 0;
918 for (GSList const *i = objects; i != NULL; i = i->next) {
919 SPObject *obj = SP_OBJECT (i->data);
921 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
922 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
923 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
924 continue;
926 SPStyle *style = SP_OBJECT_STYLE (obj);
927 if (!style) continue;
929 texts ++;
931 if (set &&
932 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
933 different = true; // different styles
934 }
936 set = TRUE;
937 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
938 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
939 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
940 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
941 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
942 }
944 if (texts == 0 || !set)
945 return QUERY_STYLE_NOTHING;
947 if (texts > 1) {
948 if (different) {
949 return QUERY_STYLE_MULTIPLE_DIFFERENT;
950 } else {
951 return QUERY_STYLE_MULTIPLE_SAME;
952 }
953 } else {
954 return QUERY_STYLE_SINGLE;
955 }
956 }
958 /**
959 * Write to style_res the average font family of objects.
960 */
961 int
962 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
963 {
964 bool different = false;
965 int texts = 0;
967 if (style_res->text->font_family.value) {
968 g_free(style_res->text->font_family.value);
969 style_res->text->font_family.value = NULL;
970 }
971 style_res->text->font_family.set = FALSE;
973 for (GSList const *i = objects; i != NULL; i = i->next) {
974 SPObject *obj = SP_OBJECT (i->data);
976 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
977 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
978 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
979 continue;
981 SPStyle *style = SP_OBJECT_STYLE (obj);
982 if (!style) continue;
984 texts ++;
986 if (style_res->text->font_family.value && style->text->font_family.value &&
987 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
988 different = true; // different fonts
989 }
991 if (style_res->text->font_family.value) {
992 g_free(style_res->text->font_family.value);
993 style_res->text->font_family.value = NULL;
994 }
996 style_res->text->font_family.set = TRUE;
997 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
998 }
1000 if (texts == 0 || !style_res->text->font_family.set)
1001 return QUERY_STYLE_NOTHING;
1003 if (texts > 1) {
1004 if (different) {
1005 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1006 } else {
1007 return QUERY_STYLE_MULTIPLE_SAME;
1008 }
1009 } else {
1010 return QUERY_STYLE_SINGLE;
1011 }
1012 }
1014 int
1015 objects_query_blend (GSList *objects, SPStyle *style_res)
1016 {
1017 const int empty_prev = -2;
1018 const int complex_filter = 5;
1019 int blend = 0;
1020 float blend_prev = empty_prev;
1021 bool same_blend = true;
1022 guint items = 0;
1024 for (GSList const *i = objects; i != NULL; i = i->next) {
1025 SPObject *obj = SP_OBJECT (i->data);
1026 SPStyle *style = SP_OBJECT_STYLE (obj);
1027 if(!style || !SP_IS_ITEM(obj)) continue;
1029 items++;
1031 //if object has a filter
1032 if (style->filter.set && style->getFilter()) {
1033 int blurcount = 0;
1034 int blendcount = 0;
1036 // determine whether filter is simple (blend and/or blur) or complex
1037 for(SPObject *primitive_obj = style->getFilter()->children;
1038 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1039 primitive_obj = primitive_obj->next) {
1040 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1041 if(SP_IS_FEBLEND(primitive))
1042 ++blendcount;
1043 else if(SP_IS_GAUSSIANBLUR(primitive))
1044 ++blurcount;
1045 else {
1046 blurcount = complex_filter;
1047 break;
1048 }
1049 }
1051 // simple filter
1052 if(blurcount == 1 || blendcount == 1) {
1053 for(SPObject *primitive_obj = style->getFilter()->children;
1054 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1055 primitive_obj = primitive_obj->next) {
1056 if(SP_IS_FEBLEND(primitive_obj)) {
1057 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1058 blend = spblend->blend_mode;
1059 }
1060 }
1061 }
1062 else {
1063 blend = complex_filter;
1064 }
1065 }
1066 // defaults to blend mode = "normal"
1067 else {
1068 blend = 0;
1069 }
1071 if(blend_prev != empty_prev && blend_prev != blend)
1072 same_blend = false;
1073 blend_prev = blend;
1074 }
1076 if (items > 0) {
1077 style_res->filter_blend_mode.value = blend;
1078 }
1080 if (items == 0) {
1081 return QUERY_STYLE_NOTHING;
1082 } else if (items == 1) {
1083 return QUERY_STYLE_SINGLE;
1084 } else {
1085 if(same_blend)
1086 return QUERY_STYLE_MULTIPLE_SAME;
1087 else
1088 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1089 }
1090 }
1092 /**
1093 * Write to style_res the average blurring of a list of objects.
1094 */
1095 int
1096 objects_query_blur (GSList *objects, SPStyle *style_res)
1097 {
1098 if (g_slist_length(objects) == 0) {
1099 /* No objects, set empty */
1100 return QUERY_STYLE_NOTHING;
1101 }
1103 float blur_sum = 0;
1104 float blur_prev = -1;
1105 bool same_blur = true;
1106 guint blur_items = 0;
1107 guint items = 0;
1109 for (GSList const *i = objects; i != NULL; i = i->next) {
1110 SPObject *obj = SP_OBJECT (i->data);
1111 SPStyle *style = SP_OBJECT_STYLE (obj);
1112 if (!style) continue;
1113 if (!SP_IS_ITEM(obj)) continue;
1115 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1117 items ++;
1119 //if object has a filter
1120 if (style->filter.set && style->getFilter()) {
1121 //cycle through filter primitives
1122 SPObject *primitive_obj = style->getFilter()->children;
1123 while (primitive_obj) {
1124 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1125 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1127 //if primitive is gaussianblur
1128 if(SP_IS_GAUSSIANBLUR(primitive)) {
1129 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1130 float num = spblur->stdDeviation.getNumber();
1131 blur_sum += num * NR::expansion(i2d);
1132 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
1133 same_blur = false;
1134 blur_prev = num;
1135 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1136 blur_items ++;
1137 }
1138 }
1139 primitive_obj = primitive_obj->next;
1140 }
1141 }
1142 }
1144 if (items > 0) {
1145 if (blur_items > 0)
1146 blur_sum /= blur_items;
1147 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1148 }
1150 if (items == 0) {
1151 return QUERY_STYLE_NOTHING;
1152 } else if (items == 1) {
1153 return QUERY_STYLE_SINGLE;
1154 } else {
1155 if (same_blur)
1156 return QUERY_STYLE_MULTIPLE_SAME;
1157 else
1158 return QUERY_STYLE_MULTIPLE_AVERAGED;
1159 }
1160 }
1162 /**
1163 * Query the given list of objects for the given property, write
1164 * the result to style, return appropriate flag.
1165 */
1166 int
1167 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1168 {
1169 if (property == QUERY_STYLE_PROPERTY_FILL) {
1170 return objects_query_fillstroke (list, style, true);
1171 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1172 return objects_query_fillstroke (list, style, false);
1174 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1175 return objects_query_strokewidth (list, style);
1176 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1177 return objects_query_miterlimit (list, style);
1178 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1179 return objects_query_strokecap (list, style);
1180 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1181 return objects_query_strokejoin (list, style);
1183 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1184 return objects_query_opacity (list, style);
1186 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1187 return objects_query_fontfamily (list, style);
1188 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1189 return objects_query_fontstyle (list, style);
1190 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1191 return objects_query_fontnumbers (list, style);
1193 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1194 return objects_query_blend (list, style);
1195 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1196 return objects_query_blur (list, style);
1197 }
1198 return QUERY_STYLE_NOTHING;
1199 }
1202 /**
1203 * Query the subselection (if any) or selection on the given desktop for the given property, write
1204 * the result to style, return appropriate flag.
1205 */
1206 int
1207 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1208 {
1209 int ret = desktop->_query_style_signal.emit(style, property);
1211 if (ret != QUERY_STYLE_NOTHING)
1212 return ret; // subselection returned a style, pass it on
1214 // otherwise, do querying and averaging over selection
1215 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1216 }
1218 /**
1219 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1220 * the properties returned QUERY_STYLE_NOTHING.
1221 */
1222 bool
1223 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1224 {
1225 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1226 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1227 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1228 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1229 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1230 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1231 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1232 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1233 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1234 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1235 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1237 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);
1238 }
1241 /*
1242 Local Variables:
1243 mode:c++
1244 c-file-style:"stroustrup"
1245 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1246 indent-tabs-mode:nil
1247 fill-column:99
1248 End:
1249 */
1250 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :