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 <string>
16 #include <cstring>
18 #include "desktop.h"
19 #include "color-rgba.h"
20 #include "svg/css-ostringstream.h"
21 #include "svg/svg.h"
22 #include "svg/svg-color.h"
23 #include "selection.h"
24 #include "inkscape.h"
25 #include "style.h"
26 #include "preferences.h"
27 #include "sp-use.h"
28 #include "filters/blend.h"
29 #include "sp-filter.h"
30 #include "sp-filter-reference.h"
31 #include "sp-gaussian-blur.h"
32 #include "sp-flowtext.h"
33 #include "sp-flowregion.h"
34 #include "sp-flowdiv.h"
35 #include "sp-linear-gradient.h"
36 #include "sp-pattern.h"
37 #include "sp-radial-gradient.h"
38 #include "sp-textpath.h"
39 #include "sp-tref.h"
40 #include "sp-tspan.h"
41 #include "xml/repr.h"
42 #include "libnrtype/font-style-to-pos.h"
43 #include "sp-path.h"
44 #include "event-context.h"
46 #include "desktop-style.h"
47 #include "svg/svg-icc-color.h"
48 #include "box3d-side.h"
50 /**
51 * Set color on selection on desktop.
52 */
53 void
54 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
55 {
56 /// \todo relative color setting
57 if (is_relative) {
58 g_warning("FIXME: relative color setting not yet implemented");
59 return;
60 }
62 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
63 gchar b[64];
64 sp_svg_write_color(b, sizeof(b), rgba);
65 SPCSSAttr *css = sp_repr_css_attr_new();
66 if (fill) {
67 sp_repr_css_set_property(css, "fill", b);
68 Inkscape::CSSOStringStream osalpha;
69 osalpha << color[3];
70 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
71 } else {
72 sp_repr_css_set_property(css, "stroke", b);
73 Inkscape::CSSOStringStream osalpha;
74 osalpha << color[3];
75 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
76 }
78 sp_desktop_set_style(desktop, css);
80 sp_repr_css_attr_unref(css);
81 }
83 /**
84 * Apply style on object and children, recursively.
85 */
86 void
87 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
88 {
89 // non-items should not have style
90 if (!SP_IS_ITEM(o))
91 return;
93 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
94 // but must always inherit from the parent text. Same for textPath.
95 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
97 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
98 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
99 // flowtext).
101 if (!(skip_lines
102 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
103 || SP_IS_FLOWDIV(o)
104 || SP_IS_FLOWPARA(o)
105 || SP_IS_TEXTPATH(o))
106 && !SP_OBJECT_REPR(o)->attribute("style"))
107 &&
108 !(SP_IS_FLOWREGION(o) ||
109 SP_IS_FLOWREGIONEXCLUDE(o) ||
110 (SP_IS_USE(o) &&
111 SP_OBJECT_PARENT(o) &&
112 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
113 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
114 )
115 )
116 )
117 ) {
119 SPCSSAttr *css_set = sp_repr_css_attr_new();
120 sp_repr_css_merge(css_set, css);
122 // Scale the style by the inverse of the accumulated parent transform in the paste context.
123 {
124 Geom::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
125 double const ex(local.descrim());
126 if ( ( ex != 0. )
127 && ( ex != 1. ) ) {
128 sp_css_attr_scale(css_set, 1/ex);
129 }
130 }
132 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
134 sp_repr_css_attr_unref(css_set);
135 }
137 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
138 if (SP_IS_USE(o))
139 return;
141 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
142 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
143 // Unset properties which are accumulating and thus should not be set recursively.
144 // 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.
145 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
146 sp_repr_css_merge(css_recurse, css);
147 sp_repr_css_set_property(css_recurse, "opacity", NULL);
148 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
149 sp_repr_css_attr_unref(css_recurse);
150 } else {
151 sp_desktop_apply_css_recursive(child, css, skip_lines);
152 }
153 }
154 }
156 /**
157 * Apply style on selection on desktop.
158 */
159 void
160 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
161 {
162 if (write_current) {
163 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
164 // 1. Set internal value
165 sp_repr_css_merge(desktop->current, css);
167 // 1a. Write to prefs; make a copy and unset any URIs first
168 SPCSSAttr *css_write = sp_repr_css_attr_new();
169 sp_repr_css_merge(css_write, css);
170 sp_css_attr_unset_uris(css_write);
171 prefs->mergeStyle("/desktop/style", css_write);
173 for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
174 /* last used styles for 3D box faces are stored separately */
175 if (SP_IS_BOX3D_SIDE (i->data)) {
176 const char * descr = box3d_side_axes_string(SP_BOX3D_SIDE(i->data));
177 if (descr != NULL) {
178 prefs->mergeStyle(Glib::ustring("/desktop/") + descr + "/style", css_write);
179 }
180 }
181 }
182 sp_repr_css_attr_unref(css_write);
183 }
185 if (!change)
186 return;
188 // 2. Emit signal
189 bool intercepted = desktop->_set_style_signal.emit(css);
191 /** \todo
192 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
193 * rect corners, font size for the object's own transform so that pasting
194 * fills does not depend on preserve/optimize.
195 */
197 // 3. If nobody has intercepted the signal, apply the style to the selection
198 if (!intercepted) {
199 // If we have an event context, update its cursor (TODO: it could be neater to do this with the signal sent above, but what if the signal gets intercepted?)
200 if (desktop->event_context) {
201 sp_event_context_update_cursor(desktop->event_context);
202 }
204 // Remove text attributes if not text...
205 // Do this once in case a zillion objects are selected.
206 SPCSSAttr *css_no_text = sp_repr_css_attr_new();
207 sp_repr_css_merge(css_no_text, css);
208 css_no_text = sp_css_attr_unset_text(css_no_text);
210 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
212 // If not text, don't apply text attributes (can a group have text attributes?)
213 if ( SP_IS_TEXT(i->data) || SP_IS_FLOWTEXT(i->data)
214 || SP_IS_TSPAN(i->data) || SP_IS_TREF(i->data) || SP_IS_TEXTPATH(i->data)
215 || SP_IS_FLOWDIV(i->data) || SP_IS_FLOWPARA(i->data) || SP_IS_FLOWTSPAN(i->data)) {
217 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
219 } else {
221 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css_no_text, true);
223 }
224 }
225 sp_repr_css_attr_unref(css_no_text);
226 }
227 }
229 /**
230 * Return the desktop's current style.
231 */
232 SPCSSAttr *
233 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
234 {
235 SPCSSAttr *css = sp_repr_css_attr_new();
236 sp_repr_css_merge(css, desktop->current);
237 if (!css->attributeList()) {
238 sp_repr_css_attr_unref(css);
239 return NULL;
240 } else {
241 if (!with_text) {
242 css = sp_css_attr_unset_text(css);
243 }
244 return css;
245 }
246 }
248 /**
249 * Return the desktop's current color.
250 */
251 guint32
252 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
253 {
254 guint32 r = 0; // if there's no color, return black
255 gchar const *property = sp_repr_css_property(desktop->current,
256 is_fill ? "fill" : "stroke",
257 "#000");
259 if (desktop->current && property) { // if there is style and the property in it,
260 if (strncmp(property, "url", 3)) { // and if it's not url,
261 // read it
262 r = sp_svg_read_color(property, r);
263 }
264 }
266 return r;
267 }
269 double
270 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool *has_opacity)
271 {
272 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
273 SPCSSAttr *css = NULL;
274 gfloat value = 1.0; // default if nothing else found
275 if (has_opacity)
276 *has_opacity = false;
277 if (prefs->getBool(tool + "/usecurrent")) {
278 css = sp_desktop_get_style(desktop, true);
279 } else {
280 css = prefs->getStyle(tool + "/style");
281 }
283 if (css) {
284 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
286 if (desktop->current && property) { // if there is style and the property in it,
287 if ( !sp_svg_number_read_f(property, &value) ) {
288 value = 1.0; // things failed. set back to the default
289 } else {
290 if (has_opacity)
291 *has_opacity = false;
292 }
293 }
295 sp_repr_css_attr_unref(css);
296 }
298 return value;
299 }
300 double
301 sp_desktop_get_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill)
302 {
303 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
304 SPCSSAttr *css = NULL;
305 gfloat value = 1.0; // default if nothing else found
306 if (prefs->getBool(tool + "/usecurrent")) {
307 css = sp_desktop_get_style(desktop, true);
308 } else {
309 css = prefs->getStyle(tool + "/style");
310 }
312 if (css) {
313 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
315 if (desktop->current && property) { // if there is style and the property in it,
316 if ( !sp_svg_number_read_f(property, &value) ) {
317 value = 1.0; // things failed. set back to the default
318 }
319 }
321 sp_repr_css_attr_unref(css);
322 }
324 return value;
325 }
327 guint32
328 sp_desktop_get_color_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill, bool *has_color)
329 {
330 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
331 SPCSSAttr *css = NULL;
332 guint32 r = 0; // if there's no color, return black
333 if (has_color)
334 *has_color = false;
335 if (prefs->getBool(tool + "/usecurrent")) {
336 css = sp_desktop_get_style(desktop, true);
337 } else {
338 css = prefs->getStyle(tool + "/style");
339 }
341 if (css) {
342 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
344 if (desktop->current && property) { // if there is style and the property in it,
345 if (strncmp(property, "url", 3) && strncmp(property, "none", 4)) { // and if it's not url or none,
346 // read it
347 r = sp_svg_read_color(property, r);
348 if (has_color)
349 *has_color = true;
350 }
351 }
353 sp_repr_css_attr_unref(css);
354 }
356 return r | 0xff;
357 }
359 /**
360 * Apply the desktop's current style or the tool style to repr.
361 */
362 void
363 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, Glib::ustring const &tool_path, bool with_text)
364 {
365 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
366 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
368 if (prefs->getBool(tool_path + "/usecurrent") && css_current) {
369 sp_repr_css_set(repr, css_current, "style");
370 } else {
371 SPCSSAttr *css = prefs->getInheritedStyle(tool_path + "/style");
372 sp_repr_css_set(repr, css, "style");
373 sp_repr_css_attr_unref(css);
374 }
375 if (css_current) {
376 sp_repr_css_attr_unref(css_current);
377 }
378 }
380 /**
381 * Returns the font size (in SVG pixels) of the text tool style (if text
382 * tool uses its own style) or desktop style (otherwise).
383 */
384 double
385 sp_desktop_get_font_size_tool(SPDesktop *desktop)
386 {
387 (void)desktop; // TODO cleanup
388 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
389 Glib::ustring desktop_style = prefs->getString("/desktop/style");
390 Glib::ustring style_str;
391 if ((prefs->getBool("/tools/text/usecurrent")) && !desktop_style.empty()) {
392 style_str = desktop_style;
393 } else {
394 style_str = prefs->getString("/tools/text/style");
395 }
397 double ret = 12;
398 if (!style_str.empty()) {
399 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
400 sp_style_merge_from_style_string(style, style_str.data());
401 ret = style->font_size.computed;
402 sp_style_unref(style);
403 }
404 return ret;
405 }
407 /** Determine average stroke width, simple method */
408 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
409 gdouble
410 stroke_average_width (GSList const *objects)
411 {
412 if (g_slist_length ((GSList *) objects) == 0)
413 return NR_HUGE;
415 gdouble avgwidth = 0.0;
416 bool notstroked = true;
417 int n_notstroked = 0;
419 for (GSList const *l = objects; l != NULL; l = l->next) {
420 if (!SP_IS_ITEM (l->data))
421 continue;
423 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
425 SPObject *object = SP_OBJECT(l->data);
427 if ( object->style->stroke.isNone() ) {
428 ++n_notstroked; // do not count nonstroked objects
429 continue;
430 } else {
431 notstroked = false;
432 }
434 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.descrim();
435 }
437 if (notstroked)
438 return NR_HUGE;
440 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
441 }
443 static bool vectorsClose( std::vector<double> const &lhs, std::vector<double> const &rhs )
444 {
445 static double epsilon = 1e-6;
446 bool isClose = false;
447 if ( lhs.size() == rhs.size() ) {
448 isClose = true;
449 for ( size_t i = 0; (i < lhs.size()) && isClose; ++i ) {
450 isClose = fabs(lhs[i] - rhs[i]) < epsilon;
451 }
452 }
453 return isClose;
454 }
457 /**
458 * Write to style_res the average fill or stroke of list of objects, if applicable.
459 */
460 int
461 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
462 {
463 if (g_slist_length(objects) == 0) {
464 /* No objects, set empty */
465 return QUERY_STYLE_NOTHING;
466 }
468 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
469 bool paintImpossible = true;
470 paint_res->set = TRUE;
472 SVGICCColor* iccColor = 0;
474 bool iccSeen = false;
475 gfloat c[4];
476 c[0] = c[1] = c[2] = c[3] = 0.0;
477 gint num = 0;
479 gfloat prev[3];
480 prev[0] = prev[1] = prev[2] = 0.0;
481 bool same_color = true;
483 for (GSList const *i = objects; i != NULL; i = i->next) {
484 SPObject *obj = SP_OBJECT (i->data);
485 SPStyle *style = SP_OBJECT_STYLE (obj);
486 if (!style) continue;
488 SPIPaint *paint = isfill? &style->fill : &style->stroke;
490 // We consider paint "effectively set" for anything within text hierarchy
491 SPObject *parent = SP_OBJECT_PARENT (obj);
492 bool paint_effectively_set =
493 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
494 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
495 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
497 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
499 if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
500 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
501 }
503 if (paint_res->set && paint->set && paint_res->isPaintserver()) {
504 // both previous paint and this paint were a server, see if the servers are compatible
506 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
507 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
509 if (SP_IS_LINEARGRADIENT (server_res)) {
511 if (!SP_IS_LINEARGRADIENT(server))
512 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
514 SPGradient *vector = SP_GRADIENT(server)->getVector();
515 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
516 if (vector_res != vector)
517 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
519 } else if (SP_IS_RADIALGRADIENT (server_res)) {
521 if (!SP_IS_RADIALGRADIENT(server))
522 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
524 SPGradient *vector = SP_GRADIENT(server)->getVector();
525 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
526 if (vector_res != vector)
527 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
529 } else if (SP_IS_PATTERN (server_res)) {
531 if (!SP_IS_PATTERN(server))
532 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
534 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
535 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
536 if (pat_res != pat)
537 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
538 }
539 }
541 // 2. Sum color, copy server from paint to paint_res
543 if (paint_res->set && paint_effectively_set && paint->isColor()) {
544 gfloat d[3];
545 sp_color_get_rgb_floatv (&paint->value.color, d);
547 // Check if this color is the same as previous
548 if (paintImpossible) {
549 prev[0] = d[0];
550 prev[1] = d[1];
551 prev[2] = d[2];
552 paint_res->setColor(d[0], d[1], d[2]);
553 iccColor = paint->value.color.icc;
554 iccSeen = true;
555 } else {
556 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2])) {
557 same_color = false;
558 iccColor = 0;
559 }
560 if ( iccSeen && iccColor ) {
561 if ( !paint->value.color.icc
562 || (iccColor->colorProfile != paint->value.color.icc->colorProfile)
563 || !vectorsClose(iccColor->colors, paint->value.color.icc->colors) ) {
564 same_color = false;
565 iccColor = 0;
566 }
567 }
568 }
570 // average color
571 c[0] += d[0];
572 c[1] += d[1];
573 c[2] += d[2];
574 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
576 num ++;
577 }
579 paintImpossible = false;
580 paint_res->colorSet = paint->colorSet;
581 paint_res->currentcolor = paint->currentcolor;
582 if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
583 if (isfill) {
584 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
585 } else {
586 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
587 }
588 }
589 paint_res->set = paint_effectively_set;
590 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
591 }
593 // After all objects processed, divide the color if necessary and return
594 if (paint_res->set && paint_res->isColor()) { // set the color
595 g_assert (num >= 1);
597 c[0] /= num;
598 c[1] /= num;
599 c[2] /= num;
600 c[3] /= num;
601 paint_res->setColor(c[0], c[1], c[2]);
602 if (isfill) {
603 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
604 } else {
605 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
606 }
609 if ( iccSeen && iccColor ) {
610 // TODO check for existing
611 SVGICCColor* tmp = new SVGICCColor(*iccColor);
612 paint_res->value.color.icc = tmp;
613 }
615 if (num > 1) {
616 if (same_color)
617 return QUERY_STYLE_MULTIPLE_SAME;
618 else
619 return QUERY_STYLE_MULTIPLE_AVERAGED;
620 } else {
621 return QUERY_STYLE_SINGLE;
622 }
623 }
625 // Not color
626 if (g_slist_length(objects) > 1) {
627 return QUERY_STYLE_MULTIPLE_SAME;
628 } else {
629 return QUERY_STYLE_SINGLE;
630 }
631 }
633 /**
634 * Write to style_res the average opacity of a list of objects.
635 */
636 int
637 objects_query_opacity (GSList *objects, SPStyle *style_res)
638 {
639 if (g_slist_length(objects) == 0) {
640 /* No objects, set empty */
641 return QUERY_STYLE_NOTHING;
642 }
644 gdouble opacity_sum = 0;
645 gdouble opacity_prev = -1;
646 bool same_opacity = true;
647 guint opacity_items = 0;
650 for (GSList const *i = objects; i != NULL; i = i->next) {
651 SPObject *obj = SP_OBJECT (i->data);
652 SPStyle *style = SP_OBJECT_STYLE (obj);
653 if (!style) continue;
655 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
656 opacity_sum += opacity;
657 if (opacity_prev != -1 && opacity != opacity_prev)
658 same_opacity = false;
659 opacity_prev = opacity;
660 opacity_items ++;
661 }
662 if (opacity_items > 1)
663 opacity_sum /= opacity_items;
665 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
667 if (opacity_items == 0) {
668 return QUERY_STYLE_NOTHING;
669 } else if (opacity_items == 1) {
670 return QUERY_STYLE_SINGLE;
671 } else {
672 if (same_opacity)
673 return QUERY_STYLE_MULTIPLE_SAME;
674 else
675 return QUERY_STYLE_MULTIPLE_AVERAGED;
676 }
677 }
679 /**
680 * Write to style_res the average stroke width of a list of objects.
681 */
682 int
683 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
684 {
685 if (g_slist_length(objects) == 0) {
686 /* No objects, set empty */
687 return QUERY_STYLE_NOTHING;
688 }
690 gdouble avgwidth = 0.0;
692 gdouble prev_sw = -1;
693 bool same_sw = true;
694 bool noneSet = true; // is stroke set to none?
696 int n_stroked = 0;
698 for (GSList const *i = objects; i != NULL; i = i->next) {
699 SPObject *obj = SP_OBJECT (i->data);
700 if (!SP_IS_ITEM(obj)) continue;
701 SPStyle *style = SP_OBJECT_STYLE (obj);
702 if (!style) continue;
704 if ( style->stroke.isNone() && !(
705 style->marker[SP_MARKER_LOC].set || // stroke width affects markers, so if there's no stroke but only markers then we should
706 style->marker[SP_MARKER_LOC_START].set || // still calculate the stroke width
707 style->marker[SP_MARKER_LOC_MID].set ||
708 style->marker[SP_MARKER_LOC_END].set))
709 {
710 continue;
711 }
713 n_stroked ++;
715 noneSet &= style->stroke.isNone();
717 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
718 double sw = style->stroke_width.computed * i2d.descrim();
720 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
721 same_sw = false;
722 prev_sw = sw;
724 avgwidth += sw;
725 }
727 if (n_stroked > 1)
728 avgwidth /= (n_stroked);
730 style_res->stroke_width.computed = avgwidth;
731 style_res->stroke_width.set = true;
732 style_res->stroke.noneSet = noneSet; // Will only be true if none of the selected objects has it's stroke set.
734 if (n_stroked == 0) {
735 return QUERY_STYLE_NOTHING;
736 } else if (n_stroked == 1) {
737 return QUERY_STYLE_SINGLE;
738 } else {
739 if (same_sw)
740 return QUERY_STYLE_MULTIPLE_SAME;
741 else
742 return QUERY_STYLE_MULTIPLE_AVERAGED;
743 }
744 }
746 /**
747 * Write to style_res the average miter limit of a list of objects.
748 */
749 int
750 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
751 {
752 if (g_slist_length(objects) == 0) {
753 /* No objects, set empty */
754 return QUERY_STYLE_NOTHING;
755 }
757 gdouble avgml = 0.0;
758 int n_stroked = 0;
760 gdouble prev_ml = -1;
761 bool same_ml = true;
763 for (GSList const *i = objects; i != NULL; i = i->next) {
764 SPObject *obj = SP_OBJECT (i->data);
765 if (!SP_IS_ITEM(obj)) continue;
766 SPStyle *style = SP_OBJECT_STYLE (obj);
767 if (!style) continue;
769 if ( style->stroke.isNone() ) {
770 continue;
771 }
773 n_stroked ++;
775 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
776 same_ml = false;
777 prev_ml = style->stroke_miterlimit.value;
779 avgml += style->stroke_miterlimit.value;
780 }
782 if (n_stroked > 1)
783 avgml /= (n_stroked);
785 style_res->stroke_miterlimit.value = avgml;
786 style_res->stroke_miterlimit.set = true;
788 if (n_stroked == 0) {
789 return QUERY_STYLE_NOTHING;
790 } else if (n_stroked == 1) {
791 return QUERY_STYLE_SINGLE;
792 } else {
793 if (same_ml)
794 return QUERY_STYLE_MULTIPLE_SAME;
795 else
796 return QUERY_STYLE_MULTIPLE_AVERAGED;
797 }
798 }
800 /**
801 * Write to style_res the stroke cap of a list of objects.
802 */
803 int
804 objects_query_strokecap (GSList *objects, SPStyle *style_res)
805 {
806 if (g_slist_length(objects) == 0) {
807 /* No objects, set empty */
808 return QUERY_STYLE_NOTHING;
809 }
811 int cap = -1;
812 gdouble prev_cap = -1;
813 bool same_cap = true;
814 int n_stroked = 0;
816 for (GSList const *i = objects; i != NULL; i = i->next) {
817 SPObject *obj = SP_OBJECT (i->data);
818 if (!SP_IS_ITEM(obj)) continue;
819 SPStyle *style = SP_OBJECT_STYLE (obj);
820 if (!style) continue;
822 if ( style->stroke.isNone() ) {
823 continue;
824 }
826 n_stroked ++;
828 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
829 same_cap = false;
830 prev_cap = style->stroke_linecap.value;
832 cap = style->stroke_linecap.value;
833 }
835 style_res->stroke_linecap.value = cap;
836 style_res->stroke_linecap.set = true;
838 if (n_stroked == 0) {
839 return QUERY_STYLE_NOTHING;
840 } else if (n_stroked == 1) {
841 return QUERY_STYLE_SINGLE;
842 } else {
843 if (same_cap)
844 return QUERY_STYLE_MULTIPLE_SAME;
845 else
846 return QUERY_STYLE_MULTIPLE_DIFFERENT;
847 }
848 }
850 /**
851 * Write to style_res the stroke join of a list of objects.
852 */
853 int
854 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
855 {
856 if (g_slist_length(objects) == 0) {
857 /* No objects, set empty */
858 return QUERY_STYLE_NOTHING;
859 }
861 int join = -1;
862 gdouble prev_join = -1;
863 bool same_join = true;
864 int n_stroked = 0;
866 for (GSList const *i = objects; i != NULL; i = i->next) {
867 SPObject *obj = SP_OBJECT (i->data);
868 if (!SP_IS_ITEM(obj)) continue;
869 SPStyle *style = SP_OBJECT_STYLE (obj);
870 if (!style) continue;
872 if ( style->stroke.isNone() ) {
873 continue;
874 }
876 n_stroked ++;
878 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
879 same_join = false;
880 prev_join = style->stroke_linejoin.value;
882 join = style->stroke_linejoin.value;
883 }
885 style_res->stroke_linejoin.value = join;
886 style_res->stroke_linejoin.set = true;
888 if (n_stroked == 0) {
889 return QUERY_STYLE_NOTHING;
890 } else if (n_stroked == 1) {
891 return QUERY_STYLE_SINGLE;
892 } else {
893 if (same_join)
894 return QUERY_STYLE_MULTIPLE_SAME;
895 else
896 return QUERY_STYLE_MULTIPLE_DIFFERENT;
897 }
898 }
900 /**
901 * Write to style_res the average font size and spacing of objects.
902 */
903 int
904 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
905 {
906 bool different = false;
908 double size = 0;
909 double letterspacing = 0;
910 double wordspacing = 0;
911 double linespacing = 0;
912 bool letterspacing_normal = false;
913 bool wordspacing_normal = false;
914 bool linespacing_normal = false;
916 double size_prev = 0;
917 double letterspacing_prev = 0;
918 double wordspacing_prev = 0;
919 double linespacing_prev = 0;
921 int texts = 0;
923 for (GSList const *i = objects; i != NULL; i = i->next) {
924 SPObject *obj = SP_OBJECT (i->data);
926 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
927 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
928 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
929 continue;
931 SPStyle *style = SP_OBJECT_STYLE (obj);
932 if (!style) continue;
934 texts ++;
935 size += style->font_size.computed * Geom::Matrix(sp_item_i2d_affine(SP_ITEM(obj))).descrim(); /// \todo FIXME: we assume non-% units here
937 if (style->letter_spacing.normal) {
938 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
939 letterspacing_normal = true;
940 } else {
941 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
942 letterspacing_normal = false;
943 }
945 if (style->word_spacing.normal) {
946 if (!different && (wordspacing_prev == 0 || wordspacing_prev == wordspacing))
947 wordspacing_normal = true;
948 } else {
949 wordspacing += style->word_spacing.computed; /// \todo FIXME: we assume non-% units here
950 wordspacing_normal = false;
951 }
953 double linespacing_current;
954 if (style->line_height.normal) {
955 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
956 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
957 linespacing_normal = true;
958 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
959 linespacing_current = style->line_height.value;
960 linespacing_normal = false;
961 } else { // we need % here
962 linespacing_current = style->line_height.computed / style->font_size.computed;
963 linespacing_normal = false;
964 }
965 linespacing += linespacing_current;
967 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
968 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
969 (wordspacing_prev != 0 && style->word_spacing.computed != wordspacing_prev) ||
970 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
971 different = true;
972 }
974 size_prev = style->font_size.computed;
975 letterspacing_prev = style->letter_spacing.computed;
976 wordspacing_prev = style->word_spacing.computed;
977 linespacing_prev = linespacing_current;
979 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
980 style_res->text_anchor.computed = style->text_anchor.computed;
981 style_res->writing_mode.computed = style->writing_mode.computed;
982 }
984 if (texts == 0)
985 return QUERY_STYLE_NOTHING;
987 if (texts > 1) {
988 size /= texts;
989 letterspacing /= texts;
990 wordspacing /= texts;
991 linespacing /= texts;
992 }
994 style_res->font_size.computed = size;
995 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
997 style_res->letter_spacing.normal = letterspacing_normal;
998 style_res->letter_spacing.computed = letterspacing;
1000 style_res->word_spacing.normal = wordspacing_normal;
1001 style_res->word_spacing.computed = wordspacing;
1003 style_res->line_height.normal = linespacing_normal;
1004 style_res->line_height.computed = linespacing;
1005 style_res->line_height.value = linespacing;
1006 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
1008 if (texts > 1) {
1009 if (different) {
1010 return QUERY_STYLE_MULTIPLE_AVERAGED;
1011 } else {
1012 return QUERY_STYLE_MULTIPLE_SAME;
1013 }
1014 } else {
1015 return QUERY_STYLE_SINGLE;
1016 }
1017 }
1019 /**
1020 * Write to style_res the average font style of objects.
1021 */
1022 int
1023 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
1024 {
1025 bool different = false;
1026 bool set = false;
1028 int texts = 0;
1030 for (GSList const *i = objects; i != NULL; i = i->next) {
1031 SPObject *obj = SP_OBJECT (i->data);
1033 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1034 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1035 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1036 continue;
1038 SPStyle *style = SP_OBJECT_STYLE (obj);
1039 if (!style) continue;
1041 texts ++;
1043 if (set &&
1044 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
1045 different = true; // different styles
1046 }
1048 set = TRUE;
1049 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
1050 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
1051 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
1052 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
1053 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
1054 }
1056 if (texts == 0 || !set)
1057 return QUERY_STYLE_NOTHING;
1059 if (texts > 1) {
1060 if (different) {
1061 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1062 } else {
1063 return QUERY_STYLE_MULTIPLE_SAME;
1064 }
1065 } else {
1066 return QUERY_STYLE_SINGLE;
1067 }
1068 }
1070 /**
1071 * Write to style_res the baseline numbers.
1072 */
1073 int
1074 objects_query_baselines (GSList *objects, SPStyle *style_res)
1075 {
1076 bool different = false;
1078 // Only baseline-shift at the moment
1079 // We will return:
1080 // If baseline-shift is same for all objects:
1081 // The full baseline-shift data (used for subscripts and superscripts)
1082 // If baseline-shift is different:
1083 // The average baseline-shift (not implemented at the moment as this is complicated June 2010)
1084 SPIBaselineShift old;
1085 old.value = 0.0;
1086 old.computed = 0.0;
1088 // double baselineshift = 0.0;
1089 bool set = false;
1091 int texts = 0;
1093 for (GSList const *i = objects; i != NULL; i = i->next) {
1094 SPObject *obj = SP_OBJECT (i->data);
1096 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1097 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1098 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1099 continue;
1101 SPStyle *style = SP_OBJECT_STYLE (obj);
1102 if (!style) continue;
1104 texts ++;
1106 SPIBaselineShift current;
1107 if(style->baseline_shift.set) {
1109 current.set = style->baseline_shift.set;
1110 current.inherit = style->baseline_shift.inherit;
1111 current.type = style->baseline_shift.type;
1112 current.literal = style->baseline_shift.literal;
1113 current.value = style->baseline_shift.value;
1114 current.computed = style->baseline_shift.computed;
1116 if( set ) {
1117 if( current.set != old.set ||
1118 current.inherit != old.inherit ||
1119 current.type != old.type ||
1120 current.literal != old.literal ||
1121 current.value != old.value ||
1122 current.computed != old.computed ) {
1123 // Maybe this needs to be better thought out.
1124 different = true;
1125 }
1126 }
1128 set = true;
1130 old.set = current.set;
1131 old.inherit = current.inherit;
1132 old.type = current.type;
1133 old.literal = current.literal;
1134 old.value = current.value;
1135 old.computed = current.computed;
1136 }
1137 }
1139 if (different || !set ) {
1140 style_res->baseline_shift.set = false;
1141 style_res->baseline_shift.computed = 0.0;
1142 } else {
1143 style_res->baseline_shift.set = old.set;
1144 style_res->baseline_shift.inherit = old.inherit;
1145 style_res->baseline_shift.type = old.type;
1146 style_res->baseline_shift.literal = old.literal;
1147 style_res->baseline_shift.value = old.value;
1148 style_res->baseline_shift.computed = old.computed;
1149 }
1151 if (texts == 0 || !set)
1152 return QUERY_STYLE_NOTHING;
1154 if (texts > 1) {
1155 if (different) {
1156 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1157 } else {
1158 return QUERY_STYLE_MULTIPLE_SAME;
1159 }
1160 } else {
1161 return QUERY_STYLE_SINGLE;
1162 }
1163 }
1165 /**
1166 * Write to style_res the average font family of objects.
1167 */
1168 int
1169 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1170 {
1171 bool different = false;
1172 int texts = 0;
1174 if (style_res->text->font_family.value) {
1175 g_free(style_res->text->font_family.value);
1176 style_res->text->font_family.value = NULL;
1177 }
1178 style_res->text->font_family.set = FALSE;
1180 for (GSList const *i = objects; i != NULL; i = i->next) {
1181 SPObject *obj = SP_OBJECT (i->data);
1183 // std::cout << " " << SP_OBJECT_ID (i->data) << std::endl;
1184 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1185 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1186 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1187 continue;
1189 SPStyle *style = SP_OBJECT_STYLE (obj);
1190 if (!style) continue;
1192 texts ++;
1194 if (style_res->text->font_family.value && style->text->font_family.value &&
1195 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1196 different = true; // different fonts
1197 }
1199 if (style_res->text->font_family.value) {
1200 g_free(style_res->text->font_family.value);
1201 style_res->text->font_family.value = NULL;
1202 }
1204 style_res->text->font_family.set = TRUE;
1205 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1206 }
1208 if (texts == 0 || !style_res->text->font_family.set)
1209 return QUERY_STYLE_NOTHING;
1211 if (texts > 1) {
1212 if (different) {
1213 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1214 } else {
1215 return QUERY_STYLE_MULTIPLE_SAME;
1216 }
1217 } else {
1218 return QUERY_STYLE_SINGLE;
1219 }
1220 }
1222 int
1223 objects_query_fontspecification (GSList *objects, SPStyle *style_res)
1224 {
1225 bool different = false;
1226 int texts = 0;
1228 if (style_res->text->font_specification.value) {
1229 g_free(style_res->text->font_specification.value);
1230 style_res->text->font_specification.value = NULL;
1231 }
1232 style_res->text->font_specification.set = FALSE;
1234 for (GSList const *i = objects; i != NULL; i = i->next) {
1235 SPObject *obj = SP_OBJECT (i->data);
1237 // std::cout << " " << SP_OBJECT_ID (i->data) << std::endl;
1238 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1239 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1240 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1241 continue;
1243 SPStyle *style = SP_OBJECT_STYLE (obj);
1244 if (!style) continue;
1246 texts ++;
1248 if (style_res->text->font_specification.value && style_res->text->font_specification.set &&
1249 style->text->font_specification.value && style->text->font_specification.set &&
1250 strcmp (style_res->text->font_specification.value, style->text->font_specification.value)) {
1251 different = true; // different fonts
1252 }
1254 if (style->text->font_specification.set) {
1256 if (style_res->text->font_specification.value) {
1257 g_free(style_res->text->font_specification.value);
1258 style_res->text->font_specification.value = NULL;
1259 }
1261 style_res->text->font_specification.set = TRUE;
1262 style_res->text->font_specification.value = g_strdup(style->text->font_specification.value);
1263 }
1264 }
1266 if (texts == 0)
1267 return QUERY_STYLE_NOTHING;
1269 if (texts > 1) {
1270 if (different) {
1271 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1272 } else {
1273 return QUERY_STYLE_MULTIPLE_SAME;
1274 }
1275 } else {
1276 return QUERY_STYLE_SINGLE;
1277 }
1278 }
1280 int
1281 objects_query_blend (GSList *objects, SPStyle *style_res)
1282 {
1283 const int empty_prev = -2;
1284 const int complex_filter = 5;
1285 int blend = 0;
1286 float blend_prev = empty_prev;
1287 bool same_blend = true;
1288 guint items = 0;
1290 for (GSList const *i = objects; i != NULL; i = i->next) {
1291 SPObject *obj = SP_OBJECT (i->data);
1292 SPStyle *style = SP_OBJECT_STYLE (obj);
1293 if(!style || !SP_IS_ITEM(obj)) continue;
1295 items++;
1297 //if object has a filter
1298 if (style->filter.set && style->getFilter()) {
1299 int blurcount = 0;
1300 int blendcount = 0;
1302 // determine whether filter is simple (blend and/or blur) or complex
1303 for(SPObject *primitive_obj = style->getFilter()->children;
1304 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1305 primitive_obj = primitive_obj->next) {
1306 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1307 if(SP_IS_FEBLEND(primitive))
1308 ++blendcount;
1309 else if(SP_IS_GAUSSIANBLUR(primitive))
1310 ++blurcount;
1311 else {
1312 blurcount = complex_filter;
1313 break;
1314 }
1315 }
1317 // simple filter
1318 if(blurcount == 1 || blendcount == 1) {
1319 for(SPObject *primitive_obj = style->getFilter()->children;
1320 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1321 primitive_obj = primitive_obj->next) {
1322 if(SP_IS_FEBLEND(primitive_obj)) {
1323 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1324 blend = spblend->blend_mode;
1325 }
1326 }
1327 }
1328 else {
1329 blend = complex_filter;
1330 }
1331 }
1332 // defaults to blend mode = "normal"
1333 else {
1334 blend = 0;
1335 }
1337 if(blend_prev != empty_prev && blend_prev != blend)
1338 same_blend = false;
1339 blend_prev = blend;
1340 }
1342 if (items > 0) {
1343 style_res->filter_blend_mode.value = blend;
1344 }
1346 if (items == 0) {
1347 return QUERY_STYLE_NOTHING;
1348 } else if (items == 1) {
1349 return QUERY_STYLE_SINGLE;
1350 } else {
1351 if(same_blend)
1352 return QUERY_STYLE_MULTIPLE_SAME;
1353 else
1354 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1355 }
1356 }
1358 /**
1359 * Write to style_res the average blurring of a list of objects.
1360 */
1361 int
1362 objects_query_blur (GSList *objects, SPStyle *style_res)
1363 {
1364 if (g_slist_length(objects) == 0) {
1365 /* No objects, set empty */
1366 return QUERY_STYLE_NOTHING;
1367 }
1369 float blur_sum = 0;
1370 float blur_prev = -1;
1371 bool same_blur = true;
1372 guint blur_items = 0;
1373 guint items = 0;
1375 for (GSList const *i = objects; i != NULL; i = i->next) {
1376 SPObject *obj = SP_OBJECT (i->data);
1377 SPStyle *style = SP_OBJECT_STYLE (obj);
1378 if (!style) continue;
1379 if (!SP_IS_ITEM(obj)) continue;
1381 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1383 items ++;
1385 //if object has a filter
1386 if (style->filter.set && style->getFilter()) {
1387 //cycle through filter primitives
1388 SPObject *primitive_obj = style->getFilter()->children;
1389 while (primitive_obj) {
1390 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1391 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1393 //if primitive is gaussianblur
1394 if(SP_IS_GAUSSIANBLUR(primitive)) {
1395 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1396 float num = spblur->stdDeviation.getNumber();
1397 blur_sum += num * i2d.descrim();
1398 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
1399 same_blur = false;
1400 blur_prev = num;
1401 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1402 blur_items ++;
1403 }
1404 }
1405 primitive_obj = primitive_obj->next;
1406 }
1407 }
1408 }
1410 if (items > 0) {
1411 if (blur_items > 0)
1412 blur_sum /= blur_items;
1413 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1414 }
1416 if (items == 0) {
1417 return QUERY_STYLE_NOTHING;
1418 } else if (items == 1) {
1419 return QUERY_STYLE_SINGLE;
1420 } else {
1421 if (same_blur)
1422 return QUERY_STYLE_MULTIPLE_SAME;
1423 else
1424 return QUERY_STYLE_MULTIPLE_AVERAGED;
1425 }
1426 }
1428 /**
1429 * Query the given list of objects for the given property, write
1430 * the result to style, return appropriate flag.
1431 */
1432 int
1433 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1434 {
1435 if (property == QUERY_STYLE_PROPERTY_FILL) {
1436 return objects_query_fillstroke (list, style, true);
1437 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1438 return objects_query_fillstroke (list, style, false);
1440 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1441 return objects_query_strokewidth (list, style);
1442 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1443 return objects_query_miterlimit (list, style);
1444 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1445 return objects_query_strokecap (list, style);
1446 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1447 return objects_query_strokejoin (list, style);
1449 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1450 return objects_query_opacity (list, style);
1452 } else if (property == QUERY_STYLE_PROPERTY_FONT_SPECIFICATION) {
1453 return objects_query_fontspecification (list, style);
1454 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1455 return objects_query_fontfamily (list, style);
1456 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1457 return objects_query_fontstyle (list, style);
1458 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1459 return objects_query_fontnumbers (list, style);
1460 } else if (property == QUERY_STYLE_PROPERTY_BASELINES) {
1461 return objects_query_baselines (list, style);
1463 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1464 return objects_query_blend (list, style);
1465 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1466 return objects_query_blur (list, style);
1467 }
1468 return QUERY_STYLE_NOTHING;
1469 }
1472 /**
1473 * Query the subselection (if any) or selection on the given desktop for the given property, write
1474 * the result to style, return appropriate flag.
1475 */
1476 int
1477 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1478 {
1479 int ret = desktop->_query_style_signal.emit(style, property);
1481 if (ret != QUERY_STYLE_NOTHING)
1482 return ret; // subselection returned a style, pass it on
1484 // otherwise, do querying and averaging over selection
1485 if (desktop->selection != NULL) {
1486 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1487 }
1489 return QUERY_STYLE_NOTHING;
1490 }
1492 /**
1493 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if at
1494 * least one of the properties did not return QUERY_STYLE_NOTHING.
1495 */
1496 bool
1497 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1498 {
1499 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1500 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1501 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1502 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1503 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1504 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1505 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1506 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1507 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1508 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1509 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1511 return (result_family != QUERY_STYLE_NOTHING ||
1512 result_fstyle != QUERY_STYLE_NOTHING ||
1513 result_fnumbers != QUERY_STYLE_NOTHING ||
1514 result_fill != QUERY_STYLE_NOTHING ||
1515 result_stroke != QUERY_STYLE_NOTHING ||
1516 result_opacity != QUERY_STYLE_NOTHING ||
1517 result_strokewidth != QUERY_STYLE_NOTHING ||
1518 result_strokemiterlimit != QUERY_STYLE_NOTHING ||
1519 result_strokecap != QUERY_STYLE_NOTHING ||
1520 result_strokejoin != QUERY_STYLE_NOTHING ||
1521 result_blur != QUERY_STYLE_NOTHING);
1522 }
1525 /*
1526 Local Variables:
1527 mode:c++
1528 c-file-style:"stroustrup"
1529 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1530 indent-tabs-mode:nil
1531 fill-column:99
1532 End:
1533 */
1534 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :