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"
45 #include "desktop-style.h"
46 #include "svg/svg-icc-color.h"
47 #include "svg/svg-device-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) {
200 // Remove text attributes if not text...
201 // Do this once in case a zillion objects are selected.
202 SPCSSAttr *css_no_text = sp_repr_css_attr_new();
203 sp_repr_css_merge(css_no_text, css);
204 css_no_text = sp_css_attr_unset_text(css_no_text);
206 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
208 // If not text, don't apply text attributes (can a group have text attributes?)
209 if ( SP_IS_TEXT(i->data) || SP_IS_FLOWTEXT(i->data)
210 || SP_IS_TSPAN(i->data) || SP_IS_TREF(i->data) || SP_IS_TEXTPATH(i->data)
211 || SP_IS_FLOWDIV(i->data) || SP_IS_FLOWPARA(i->data) || SP_IS_FLOWTSPAN(i->data)) {
213 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
215 } else {
217 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css_no_text, true);
219 }
220 }
221 sp_repr_css_attr_unref(css_no_text);
222 }
223 }
225 /**
226 * Return the desktop's current style.
227 */
228 SPCSSAttr *
229 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
230 {
231 SPCSSAttr *css = sp_repr_css_attr_new();
232 sp_repr_css_merge(css, desktop->current);
233 if (!css->attributeList()) {
234 sp_repr_css_attr_unref(css);
235 return NULL;
236 } else {
237 if (!with_text) {
238 css = sp_css_attr_unset_text(css);
239 }
240 return css;
241 }
242 }
244 /**
245 * Return the desktop's current color.
246 */
247 guint32
248 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
249 {
250 guint32 r = 0; // if there's no color, return black
251 gchar const *property = sp_repr_css_property(desktop->current,
252 is_fill ? "fill" : "stroke",
253 "#000");
255 if (desktop->current && property) { // if there is style and the property in it,
256 if (strncmp(property, "url", 3)) { // and if it's not url,
257 // read it
258 r = sp_svg_read_color(property, r);
259 }
260 }
262 return r;
263 }
265 double
266 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool *has_opacity)
267 {
268 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
269 SPCSSAttr *css = NULL;
270 gfloat value = 1.0; // default if nothing else found
271 if (has_opacity)
272 *has_opacity = false;
273 if (prefs->getBool(tool + "/usecurrent")) {
274 css = sp_desktop_get_style(desktop, true);
275 } else {
276 css = prefs->getStyle(tool + "/style");
277 }
279 if (css) {
280 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
282 if (desktop->current && property) { // if there is style and the property in it,
283 if ( !sp_svg_number_read_f(property, &value) ) {
284 value = 1.0; // things failed. set back to the default
285 } else {
286 if (has_opacity)
287 *has_opacity = false;
288 }
289 }
291 sp_repr_css_attr_unref(css);
292 }
294 return value;
295 }
296 double
297 sp_desktop_get_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill)
298 {
299 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
300 SPCSSAttr *css = NULL;
301 gfloat value = 1.0; // default if nothing else found
302 if (prefs->getBool(tool + "/usecurrent")) {
303 css = sp_desktop_get_style(desktop, true);
304 } else {
305 css = prefs->getStyle(tool + "/style");
306 }
308 if (css) {
309 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
311 if (desktop->current && property) { // if there is style and the property in it,
312 if ( !sp_svg_number_read_f(property, &value) ) {
313 value = 1.0; // things failed. set back to the default
314 }
315 }
317 sp_repr_css_attr_unref(css);
318 }
320 return value;
321 }
323 guint32
324 sp_desktop_get_color_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill, bool *has_color)
325 {
326 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
327 SPCSSAttr *css = NULL;
328 guint32 r = 0; // if there's no color, return black
329 if (has_color)
330 *has_color = false;
331 if (prefs->getBool(tool + "/usecurrent")) {
332 css = sp_desktop_get_style(desktop, true);
333 } else {
334 css = prefs->getStyle(tool + "/style");
335 }
337 if (css) {
338 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
340 if (desktop->current && property) { // if there is style and the property in it,
341 if (strncmp(property, "url", 3) && strncmp(property, "none", 4)) { // and if it's not url or none,
342 // read it
343 r = sp_svg_read_color(property, r);
344 if (has_color)
345 *has_color = true;
346 }
347 }
349 sp_repr_css_attr_unref(css);
350 }
352 return r | 0xff;
353 }
355 /**
356 * Apply the desktop's current style or the tool style to repr.
357 */
358 void
359 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, Glib::ustring const &tool_path, bool with_text)
360 {
361 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
362 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
364 if (prefs->getBool(tool_path + "/usecurrent") && css_current) {
365 sp_repr_css_set(repr, css_current, "style");
366 } else {
367 SPCSSAttr *css = prefs->getInheritedStyle(tool_path + "/style");
368 sp_repr_css_set(repr, css, "style");
369 sp_repr_css_attr_unref(css);
370 }
371 if (css_current) {
372 sp_repr_css_attr_unref(css_current);
373 }
374 }
376 /**
377 * Returns the font size (in SVG pixels) of the text tool style (if text
378 * tool uses its own style) or desktop style (otherwise).
379 */
380 double
381 sp_desktop_get_font_size_tool(SPDesktop *desktop)
382 {
383 (void)desktop; // TODO cleanup
384 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
385 Glib::ustring desktop_style = prefs->getString("/desktop/style");
386 Glib::ustring style_str;
387 if ((prefs->getBool("/tools/text/usecurrent")) && !desktop_style.empty()) {
388 style_str = desktop_style;
389 } else {
390 style_str = prefs->getString("/tools/text/style");
391 }
393 double ret = 12;
394 if (!style_str.empty()) {
395 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
396 sp_style_merge_from_style_string(style, style_str.data());
397 ret = style->font_size.computed;
398 sp_style_unref(style);
399 }
400 return ret;
401 }
403 /** Determine average stroke width, simple method */
404 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
405 gdouble
406 stroke_average_width (GSList const *objects)
407 {
408 if (g_slist_length ((GSList *) objects) == 0)
409 return NR_HUGE;
411 gdouble avgwidth = 0.0;
412 bool notstroked = true;
413 int n_notstroked = 0;
415 for (GSList const *l = objects; l != NULL; l = l->next) {
416 if (!SP_IS_ITEM (l->data))
417 continue;
419 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
421 SPObject *object = SP_OBJECT(l->data);
423 if ( object->style->stroke.isNone() ) {
424 ++n_notstroked; // do not count nonstroked objects
425 continue;
426 } else {
427 notstroked = false;
428 }
430 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.descrim();
431 }
433 if (notstroked)
434 return NR_HUGE;
436 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
437 }
439 /**
440 * Write to style_res the average fill or stroke of list of objects, if applicable.
441 */
442 int
443 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
444 {
445 if (g_slist_length(objects) == 0) {
446 /* No objects, set empty */
447 return QUERY_STYLE_NOTHING;
448 }
450 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
451 bool paintImpossible = true;
452 paint_res->set = TRUE;
454 SVGICCColor* iccColor = 0;
455 SVGDeviceColor* devColor = 0;
457 bool iccSeen = false;
458 gfloat c[4];
459 c[0] = c[1] = c[2] = c[3] = 0.0;
460 gint num = 0;
462 gfloat prev[3];
463 prev[0] = prev[1] = prev[2] = 0.0;
464 bool same_color = true;
466 for (GSList const *i = objects; i != NULL; i = i->next) {
467 SPObject *obj = SP_OBJECT (i->data);
468 SPStyle *style = SP_OBJECT_STYLE (obj);
469 if (!style) continue;
471 SPIPaint *paint = isfill? &style->fill : &style->stroke;
473 // We consider paint "effectively set" for anything within text hierarchy
474 SPObject *parent = SP_OBJECT_PARENT (obj);
475 bool paint_effectively_set =
476 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
477 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
478 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
480 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
482 if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
483 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
484 }
486 if (paint_res->set && paint->set && paint_res->isPaintserver()) {
487 // both previous paint and this paint were a server, see if the servers are compatible
489 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
490 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
492 if (SP_IS_LINEARGRADIENT (server_res)) {
494 if (!SP_IS_LINEARGRADIENT(server))
495 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
497 SPGradient *vector = SP_GRADIENT(server)->getVector();
498 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
499 if (vector_res != vector)
500 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
502 } else if (SP_IS_RADIALGRADIENT (server_res)) {
504 if (!SP_IS_RADIALGRADIENT(server))
505 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
507 SPGradient *vector = SP_GRADIENT(server)->getVector();
508 SPGradient *vector_res = SP_GRADIENT(server_res)->getVector();
509 if (vector_res != vector)
510 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
512 } else if (SP_IS_PATTERN (server_res)) {
514 if (!SP_IS_PATTERN(server))
515 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
517 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
518 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
519 if (pat_res != pat)
520 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
521 }
522 }
524 // 2. Sum color, copy server from paint to paint_res
526 if (paint_res->set && paint_effectively_set && paint->isColor()) {
527 gfloat d[3];
528 sp_color_get_rgb_floatv (&paint->value.color, d);
530 // Check if this color is the same as previous
531 if (paintImpossible) {
532 prev[0] = d[0];
533 prev[1] = d[1];
534 prev[2] = d[2];
535 paint_res->setColor(d[0], d[1], d[2]);
536 iccColor = paint->value.color.icc;
537 iccSeen = true;
538 } else {
539 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
540 same_color = false;
541 if ( iccSeen ) {
542 if(paint->value.color.icc) {
543 // TODO fix this
544 iccColor = 0;
545 }
546 }
547 }
549 // average color
550 c[0] += d[0];
551 c[1] += d[1];
552 c[2] += d[2];
553 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
555 // average device color
556 unsigned int it;
557 if (i==objects /*if this is the first object in the GList*/
558 && paint->value.color.device){
559 devColor = new SVGDeviceColor(*paint->value.color.device);
560 for (it=0; it < paint->value.color.device->colors.size(); it++){
561 devColor->colors[it] = 0;
562 }
563 }
565 if (devColor && paint->value.color.device && paint->value.color.device->type == devColor->type){
566 for (it=0; it < paint->value.color.device->colors.size(); it++){
567 devColor->colors[it] += paint->value.color.device->colors[it];
568 }
569 }
571 num ++;
572 }
574 paintImpossible = false;
575 paint_res->colorSet = paint->colorSet;
576 paint_res->currentcolor = paint->currentcolor;
577 if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
578 if (isfill) {
579 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
580 } else {
581 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
582 }
583 }
584 paint_res->set = paint_effectively_set;
585 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
586 }
588 // After all objects processed, divide the color if necessary and return
589 if (paint_res->set && paint_res->isColor()) { // set the color
590 g_assert (num >= 1);
592 c[0] /= num;
593 c[1] /= num;
594 c[2] /= num;
595 c[3] /= num;
596 paint_res->setColor(c[0], c[1], c[2]);
597 if (isfill) {
598 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
599 } else {
600 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
601 }
604 if ( iccSeen && iccColor ) {
605 // TODO check for existing
606 SVGICCColor* tmp = new SVGICCColor(*iccColor);
607 paint_res->value.color.icc = tmp;
608 }
610 // divide and store the device-color
611 if (devColor){
612 for (unsigned int it=0; it < devColor->colors.size(); it++){
613 devColor->colors[it] /= num;
614 }
615 paint_res->value.color.device = devColor;
616 }
618 if (num > 1) {
619 if (same_color)
620 return QUERY_STYLE_MULTIPLE_SAME;
621 else
622 return QUERY_STYLE_MULTIPLE_AVERAGED;
623 } else {
624 return QUERY_STYLE_SINGLE;
625 }
626 }
628 // Not color
629 if (g_slist_length(objects) > 1) {
630 return QUERY_STYLE_MULTIPLE_SAME;
631 } else {
632 return QUERY_STYLE_SINGLE;
633 }
634 }
636 /**
637 * Write to style_res the average opacity of a list of objects.
638 */
639 int
640 objects_query_opacity (GSList *objects, SPStyle *style_res)
641 {
642 if (g_slist_length(objects) == 0) {
643 /* No objects, set empty */
644 return QUERY_STYLE_NOTHING;
645 }
647 gdouble opacity_sum = 0;
648 gdouble opacity_prev = -1;
649 bool same_opacity = true;
650 guint opacity_items = 0;
653 for (GSList const *i = objects; i != NULL; i = i->next) {
654 SPObject *obj = SP_OBJECT (i->data);
655 SPStyle *style = SP_OBJECT_STYLE (obj);
656 if (!style) continue;
658 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
659 opacity_sum += opacity;
660 if (opacity_prev != -1 && opacity != opacity_prev)
661 same_opacity = false;
662 opacity_prev = opacity;
663 opacity_items ++;
664 }
665 if (opacity_items > 1)
666 opacity_sum /= opacity_items;
668 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
670 if (opacity_items == 0) {
671 return QUERY_STYLE_NOTHING;
672 } else if (opacity_items == 1) {
673 return QUERY_STYLE_SINGLE;
674 } else {
675 if (same_opacity)
676 return QUERY_STYLE_MULTIPLE_SAME;
677 else
678 return QUERY_STYLE_MULTIPLE_AVERAGED;
679 }
680 }
682 /**
683 * Write to style_res the average stroke width of a list of objects.
684 */
685 int
686 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
687 {
688 if (g_slist_length(objects) == 0) {
689 /* No objects, set empty */
690 return QUERY_STYLE_NOTHING;
691 }
693 gdouble avgwidth = 0.0;
695 gdouble prev_sw = -1;
696 bool same_sw = true;
697 bool noneSet = true; // is stroke set to none?
699 int n_stroked = 0;
701 for (GSList const *i = objects; i != NULL; i = i->next) {
702 SPObject *obj = SP_OBJECT (i->data);
703 if (!SP_IS_ITEM(obj)) continue;
704 SPStyle *style = SP_OBJECT_STYLE (obj);
705 if (!style) continue;
707 if ( style->stroke.isNone() && !(
708 style->marker[SP_MARKER_LOC].set || // stroke width affects markers, so if there's no stroke but only markers then we should
709 style->marker[SP_MARKER_LOC_START].set || // still calculate the stroke width
710 style->marker[SP_MARKER_LOC_MID].set ||
711 style->marker[SP_MARKER_LOC_END].set))
712 {
713 continue;
714 }
716 n_stroked ++;
718 noneSet &= style->stroke.isNone();
720 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
721 double sw = style->stroke_width.computed * i2d.descrim();
723 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
724 same_sw = false;
725 prev_sw = sw;
727 avgwidth += sw;
728 }
730 if (n_stroked > 1)
731 avgwidth /= (n_stroked);
733 style_res->stroke_width.computed = avgwidth;
734 style_res->stroke_width.set = true;
735 style_res->stroke.noneSet = noneSet; // Will only be true if none of the selected objects has it's stroke set.
737 if (n_stroked == 0) {
738 return QUERY_STYLE_NOTHING;
739 } else if (n_stroked == 1) {
740 return QUERY_STYLE_SINGLE;
741 } else {
742 if (same_sw)
743 return QUERY_STYLE_MULTIPLE_SAME;
744 else
745 return QUERY_STYLE_MULTIPLE_AVERAGED;
746 }
747 }
749 /**
750 * Write to style_res the average miter limit of a list of objects.
751 */
752 int
753 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
754 {
755 if (g_slist_length(objects) == 0) {
756 /* No objects, set empty */
757 return QUERY_STYLE_NOTHING;
758 }
760 gdouble avgml = 0.0;
761 int n_stroked = 0;
763 gdouble prev_ml = -1;
764 bool same_ml = true;
766 for (GSList const *i = objects; i != NULL; i = i->next) {
767 SPObject *obj = SP_OBJECT (i->data);
768 if (!SP_IS_ITEM(obj)) continue;
769 SPStyle *style = SP_OBJECT_STYLE (obj);
770 if (!style) continue;
772 if ( style->stroke.isNone() ) {
773 continue;
774 }
776 n_stroked ++;
778 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
779 same_ml = false;
780 prev_ml = style->stroke_miterlimit.value;
782 avgml += style->stroke_miterlimit.value;
783 }
785 if (n_stroked > 1)
786 avgml /= (n_stroked);
788 style_res->stroke_miterlimit.value = avgml;
789 style_res->stroke_miterlimit.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_ml)
797 return QUERY_STYLE_MULTIPLE_SAME;
798 else
799 return QUERY_STYLE_MULTIPLE_AVERAGED;
800 }
801 }
803 /**
804 * Write to style_res the stroke cap of a list of objects.
805 */
806 int
807 objects_query_strokecap (GSList *objects, SPStyle *style_res)
808 {
809 if (g_slist_length(objects) == 0) {
810 /* No objects, set empty */
811 return QUERY_STYLE_NOTHING;
812 }
814 int cap = -1;
815 gdouble prev_cap = -1;
816 bool same_cap = true;
817 int n_stroked = 0;
819 for (GSList const *i = objects; i != NULL; i = i->next) {
820 SPObject *obj = SP_OBJECT (i->data);
821 if (!SP_IS_ITEM(obj)) continue;
822 SPStyle *style = SP_OBJECT_STYLE (obj);
823 if (!style) continue;
825 if ( style->stroke.isNone() ) {
826 continue;
827 }
829 n_stroked ++;
831 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
832 same_cap = false;
833 prev_cap = style->stroke_linecap.value;
835 cap = style->stroke_linecap.value;
836 }
838 style_res->stroke_linecap.value = cap;
839 style_res->stroke_linecap.set = true;
841 if (n_stroked == 0) {
842 return QUERY_STYLE_NOTHING;
843 } else if (n_stroked == 1) {
844 return QUERY_STYLE_SINGLE;
845 } else {
846 if (same_cap)
847 return QUERY_STYLE_MULTIPLE_SAME;
848 else
849 return QUERY_STYLE_MULTIPLE_DIFFERENT;
850 }
851 }
853 /**
854 * Write to style_res the stroke join of a list of objects.
855 */
856 int
857 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
858 {
859 if (g_slist_length(objects) == 0) {
860 /* No objects, set empty */
861 return QUERY_STYLE_NOTHING;
862 }
864 int join = -1;
865 gdouble prev_join = -1;
866 bool same_join = true;
867 int n_stroked = 0;
869 for (GSList const *i = objects; i != NULL; i = i->next) {
870 SPObject *obj = SP_OBJECT (i->data);
871 if (!SP_IS_ITEM(obj)) continue;
872 SPStyle *style = SP_OBJECT_STYLE (obj);
873 if (!style) continue;
875 if ( style->stroke.isNone() ) {
876 continue;
877 }
879 n_stroked ++;
881 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
882 same_join = false;
883 prev_join = style->stroke_linejoin.value;
885 join = style->stroke_linejoin.value;
886 }
888 style_res->stroke_linejoin.value = join;
889 style_res->stroke_linejoin.set = true;
891 if (n_stroked == 0) {
892 return QUERY_STYLE_NOTHING;
893 } else if (n_stroked == 1) {
894 return QUERY_STYLE_SINGLE;
895 } else {
896 if (same_join)
897 return QUERY_STYLE_MULTIPLE_SAME;
898 else
899 return QUERY_STYLE_MULTIPLE_DIFFERENT;
900 }
901 }
903 /**
904 * Write to style_res the average font size and spacing of objects.
905 */
906 int
907 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
908 {
909 bool different = false;
911 double size = 0;
912 double letterspacing = 0;
913 double wordspacing = 0;
914 double linespacing = 0;
915 bool letterspacing_normal = false;
916 bool wordspacing_normal = false;
917 bool linespacing_normal = false;
919 double size_prev = 0;
920 double letterspacing_prev = 0;
921 double wordspacing_prev = 0;
922 double linespacing_prev = 0;
924 int texts = 0;
926 for (GSList const *i = objects; i != NULL; i = i->next) {
927 SPObject *obj = SP_OBJECT (i->data);
929 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
930 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
931 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
932 continue;
934 SPStyle *style = SP_OBJECT_STYLE (obj);
935 if (!style) continue;
937 texts ++;
938 size += style->font_size.computed * Geom::Matrix(sp_item_i2d_affine(SP_ITEM(obj))).descrim(); /// \todo FIXME: we assume non-% units here
940 if (style->letter_spacing.normal) {
941 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
942 letterspacing_normal = true;
943 } else {
944 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
945 letterspacing_normal = false;
946 }
948 if (style->word_spacing.normal) {
949 if (!different && (wordspacing_prev == 0 || wordspacing_prev == wordspacing))
950 wordspacing_normal = true;
951 } else {
952 wordspacing += style->word_spacing.computed; /// \todo FIXME: we assume non-% units here
953 wordspacing_normal = false;
954 }
956 double linespacing_current;
957 if (style->line_height.normal) {
958 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
959 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
960 linespacing_normal = true;
961 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
962 linespacing_current = style->line_height.value;
963 linespacing_normal = false;
964 } else { // we need % here
965 linespacing_current = style->line_height.computed / style->font_size.computed;
966 linespacing_normal = false;
967 }
968 linespacing += linespacing_current;
970 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
971 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
972 (wordspacing_prev != 0 && style->word_spacing.computed != wordspacing_prev) ||
973 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
974 different = true;
975 }
977 size_prev = style->font_size.computed;
978 letterspacing_prev = style->letter_spacing.computed;
979 wordspacing_prev = style->word_spacing.computed;
980 linespacing_prev = linespacing_current;
982 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
983 style_res->text_anchor.computed = style->text_anchor.computed;
984 style_res->writing_mode.computed = style->writing_mode.computed;
985 }
987 if (texts == 0)
988 return QUERY_STYLE_NOTHING;
990 if (texts > 1) {
991 size /= texts;
992 letterspacing /= texts;
993 wordspacing /= texts;
994 linespacing /= texts;
995 }
997 style_res->font_size.computed = size;
998 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
1000 style_res->letter_spacing.normal = letterspacing_normal;
1001 style_res->letter_spacing.computed = letterspacing;
1003 style_res->word_spacing.normal = wordspacing_normal;
1004 style_res->word_spacing.computed = wordspacing;
1006 style_res->line_height.normal = linespacing_normal;
1007 style_res->line_height.computed = linespacing;
1008 style_res->line_height.value = linespacing;
1009 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
1011 if (texts > 1) {
1012 if (different) {
1013 return QUERY_STYLE_MULTIPLE_AVERAGED;
1014 } else {
1015 return QUERY_STYLE_MULTIPLE_SAME;
1016 }
1017 } else {
1018 return QUERY_STYLE_SINGLE;
1019 }
1020 }
1022 /**
1023 * Write to style_res the average font style of objects.
1024 */
1025 int
1026 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
1027 {
1028 bool different = false;
1029 bool set = false;
1031 int texts = 0;
1033 for (GSList const *i = objects; i != NULL; i = i->next) {
1034 SPObject *obj = SP_OBJECT (i->data);
1036 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1037 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1038 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1039 continue;
1041 SPStyle *style = SP_OBJECT_STYLE (obj);
1042 if (!style) continue;
1044 texts ++;
1046 if (set &&
1047 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
1048 different = true; // different styles
1049 }
1051 set = TRUE;
1052 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
1053 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
1054 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
1055 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
1056 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
1057 }
1059 if (texts == 0 || !set)
1060 return QUERY_STYLE_NOTHING;
1062 if (texts > 1) {
1063 if (different) {
1064 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1065 } else {
1066 return QUERY_STYLE_MULTIPLE_SAME;
1067 }
1068 } else {
1069 return QUERY_STYLE_SINGLE;
1070 }
1071 }
1073 /**
1074 * Write to style_res the baseline numbers.
1075 */
1076 int
1077 objects_query_baselines (GSList *objects, SPStyle *style_res)
1078 {
1079 bool different = false;
1081 // Only baseline-shift at the moment
1082 // We will return:
1083 // If baseline-shift is same for all objects:
1084 // The full baseline-shift data (used for subscripts and superscripts)
1085 // If baseline-shift is different:
1086 // The average baseline-shift (not implemented at the moment as this is complicated June 2010)
1087 SPIBaselineShift old;
1088 old.value = 0.0;
1089 old.computed = 0.0;
1091 // double baselineshift = 0.0;
1092 bool set = false;
1094 int texts = 0;
1096 for (GSList const *i = objects; i != NULL; i = i->next) {
1097 SPObject *obj = SP_OBJECT (i->data);
1099 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1100 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1101 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1102 continue;
1104 SPStyle *style = SP_OBJECT_STYLE (obj);
1105 if (!style) continue;
1107 texts ++;
1109 SPIBaselineShift current;
1110 if(style->baseline_shift.set) {
1112 current.set = style->baseline_shift.set;
1113 current.inherit = style->baseline_shift.inherit;
1114 current.type = style->baseline_shift.type;
1115 current.literal = style->baseline_shift.literal;
1116 current.value = style->baseline_shift.value;
1117 current.computed = style->baseline_shift.computed;
1119 if( set ) {
1120 if( current.set != old.set ||
1121 current.inherit != old.inherit ||
1122 current.type != old.type ||
1123 current.literal != old.literal ||
1124 current.value != old.value ||
1125 current.computed != old.computed ) {
1126 // Maybe this needs to be better thought out.
1127 different = true;
1128 }
1129 }
1131 set = true;
1133 old.set = current.set;
1134 old.inherit = current.inherit;
1135 old.type = current.type;
1136 old.literal = current.literal;
1137 old.value = current.value;
1138 old.computed = current.computed;
1139 }
1140 }
1142 if (different || !set ) {
1143 style_res->baseline_shift.set = false;
1144 style_res->baseline_shift.computed = 0.0;
1145 } else {
1146 style_res->baseline_shift.set = old.set;
1147 style_res->baseline_shift.inherit = old.inherit;
1148 style_res->baseline_shift.type = old.type;
1149 style_res->baseline_shift.literal = old.literal;
1150 style_res->baseline_shift.value = old.value;
1151 style_res->baseline_shift.computed = old.computed;
1152 }
1154 if (texts == 0 || !set)
1155 return QUERY_STYLE_NOTHING;
1157 if (texts > 1) {
1158 if (different) {
1159 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1160 } else {
1161 return QUERY_STYLE_MULTIPLE_SAME;
1162 }
1163 } else {
1164 return QUERY_STYLE_SINGLE;
1165 }
1166 }
1168 /**
1169 * Write to style_res the average font family of objects.
1170 */
1171 int
1172 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1173 {
1174 bool different = false;
1175 int texts = 0;
1177 if (style_res->text->font_family.value) {
1178 g_free(style_res->text->font_family.value);
1179 style_res->text->font_family.value = NULL;
1180 }
1181 style_res->text->font_family.set = FALSE;
1183 for (GSList const *i = objects; i != NULL; i = i->next) {
1184 SPObject *obj = SP_OBJECT (i->data);
1186 // std::cout << " " << SP_OBJECT_ID (i->data) << std::endl;
1187 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1188 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1189 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1190 continue;
1192 SPStyle *style = SP_OBJECT_STYLE (obj);
1193 if (!style) continue;
1195 texts ++;
1197 if (style_res->text->font_family.value && style->text->font_family.value &&
1198 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1199 different = true; // different fonts
1200 }
1202 if (style_res->text->font_family.value) {
1203 g_free(style_res->text->font_family.value);
1204 style_res->text->font_family.value = NULL;
1205 }
1207 style_res->text->font_family.set = TRUE;
1208 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1209 }
1211 if (texts == 0 || !style_res->text->font_family.set)
1212 return QUERY_STYLE_NOTHING;
1214 if (texts > 1) {
1215 if (different) {
1216 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1217 } else {
1218 return QUERY_STYLE_MULTIPLE_SAME;
1219 }
1220 } else {
1221 return QUERY_STYLE_SINGLE;
1222 }
1223 }
1225 int
1226 objects_query_fontspecification (GSList *objects, SPStyle *style_res)
1227 {
1228 bool different = false;
1229 int texts = 0;
1231 if (style_res->text->font_specification.value) {
1232 g_free(style_res->text->font_specification.value);
1233 style_res->text->font_specification.value = NULL;
1234 }
1235 style_res->text->font_specification.set = FALSE;
1237 for (GSList const *i = objects; i != NULL; i = i->next) {
1238 SPObject *obj = SP_OBJECT (i->data);
1240 // std::cout << " " << SP_OBJECT_ID (i->data) << std::endl;
1241 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1242 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1243 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1244 continue;
1246 SPStyle *style = SP_OBJECT_STYLE (obj);
1247 if (!style) continue;
1249 texts ++;
1251 if (style_res->text->font_specification.value && style_res->text->font_specification.set &&
1252 style->text->font_specification.value && style->text->font_specification.set &&
1253 strcmp (style_res->text->font_specification.value, style->text->font_specification.value)) {
1254 different = true; // different fonts
1255 }
1257 if (style->text->font_specification.set) {
1259 if (style_res->text->font_specification.value) {
1260 g_free(style_res->text->font_specification.value);
1261 style_res->text->font_specification.value = NULL;
1262 }
1264 style_res->text->font_specification.set = TRUE;
1265 style_res->text->font_specification.value = g_strdup(style->text->font_specification.value);
1266 }
1267 }
1269 if (texts == 0)
1270 return QUERY_STYLE_NOTHING;
1272 if (texts > 1) {
1273 if (different) {
1274 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1275 } else {
1276 return QUERY_STYLE_MULTIPLE_SAME;
1277 }
1278 } else {
1279 return QUERY_STYLE_SINGLE;
1280 }
1281 }
1283 int
1284 objects_query_blend (GSList *objects, SPStyle *style_res)
1285 {
1286 const int empty_prev = -2;
1287 const int complex_filter = 5;
1288 int blend = 0;
1289 float blend_prev = empty_prev;
1290 bool same_blend = true;
1291 guint items = 0;
1293 for (GSList const *i = objects; i != NULL; i = i->next) {
1294 SPObject *obj = SP_OBJECT (i->data);
1295 SPStyle *style = SP_OBJECT_STYLE (obj);
1296 if(!style || !SP_IS_ITEM(obj)) continue;
1298 items++;
1300 //if object has a filter
1301 if (style->filter.set && style->getFilter()) {
1302 int blurcount = 0;
1303 int blendcount = 0;
1305 // determine whether filter is simple (blend and/or blur) or complex
1306 for(SPObject *primitive_obj = style->getFilter()->children;
1307 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1308 primitive_obj = primitive_obj->next) {
1309 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1310 if(SP_IS_FEBLEND(primitive))
1311 ++blendcount;
1312 else if(SP_IS_GAUSSIANBLUR(primitive))
1313 ++blurcount;
1314 else {
1315 blurcount = complex_filter;
1316 break;
1317 }
1318 }
1320 // simple filter
1321 if(blurcount == 1 || blendcount == 1) {
1322 for(SPObject *primitive_obj = style->getFilter()->children;
1323 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1324 primitive_obj = primitive_obj->next) {
1325 if(SP_IS_FEBLEND(primitive_obj)) {
1326 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1327 blend = spblend->blend_mode;
1328 }
1329 }
1330 }
1331 else {
1332 blend = complex_filter;
1333 }
1334 }
1335 // defaults to blend mode = "normal"
1336 else {
1337 blend = 0;
1338 }
1340 if(blend_prev != empty_prev && blend_prev != blend)
1341 same_blend = false;
1342 blend_prev = blend;
1343 }
1345 if (items > 0) {
1346 style_res->filter_blend_mode.value = blend;
1347 }
1349 if (items == 0) {
1350 return QUERY_STYLE_NOTHING;
1351 } else if (items == 1) {
1352 return QUERY_STYLE_SINGLE;
1353 } else {
1354 if(same_blend)
1355 return QUERY_STYLE_MULTIPLE_SAME;
1356 else
1357 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1358 }
1359 }
1361 /**
1362 * Write to style_res the average blurring of a list of objects.
1363 */
1364 int
1365 objects_query_blur (GSList *objects, SPStyle *style_res)
1366 {
1367 if (g_slist_length(objects) == 0) {
1368 /* No objects, set empty */
1369 return QUERY_STYLE_NOTHING;
1370 }
1372 float blur_sum = 0;
1373 float blur_prev = -1;
1374 bool same_blur = true;
1375 guint blur_items = 0;
1376 guint items = 0;
1378 for (GSList const *i = objects; i != NULL; i = i->next) {
1379 SPObject *obj = SP_OBJECT (i->data);
1380 SPStyle *style = SP_OBJECT_STYLE (obj);
1381 if (!style) continue;
1382 if (!SP_IS_ITEM(obj)) continue;
1384 Geom::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1386 items ++;
1388 //if object has a filter
1389 if (style->filter.set && style->getFilter()) {
1390 //cycle through filter primitives
1391 SPObject *primitive_obj = style->getFilter()->children;
1392 while (primitive_obj) {
1393 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1394 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1396 //if primitive is gaussianblur
1397 if(SP_IS_GAUSSIANBLUR(primitive)) {
1398 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1399 float num = spblur->stdDeviation.getNumber();
1400 blur_sum += num * i2d.descrim();
1401 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
1402 same_blur = false;
1403 blur_prev = num;
1404 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1405 blur_items ++;
1406 }
1407 }
1408 primitive_obj = primitive_obj->next;
1409 }
1410 }
1411 }
1413 if (items > 0) {
1414 if (blur_items > 0)
1415 blur_sum /= blur_items;
1416 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1417 }
1419 if (items == 0) {
1420 return QUERY_STYLE_NOTHING;
1421 } else if (items == 1) {
1422 return QUERY_STYLE_SINGLE;
1423 } else {
1424 if (same_blur)
1425 return QUERY_STYLE_MULTIPLE_SAME;
1426 else
1427 return QUERY_STYLE_MULTIPLE_AVERAGED;
1428 }
1429 }
1431 /**
1432 * Query the given list of objects for the given property, write
1433 * the result to style, return appropriate flag.
1434 */
1435 int
1436 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1437 {
1438 if (property == QUERY_STYLE_PROPERTY_FILL) {
1439 return objects_query_fillstroke (list, style, true);
1440 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1441 return objects_query_fillstroke (list, style, false);
1443 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1444 return objects_query_strokewidth (list, style);
1445 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1446 return objects_query_miterlimit (list, style);
1447 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1448 return objects_query_strokecap (list, style);
1449 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1450 return objects_query_strokejoin (list, style);
1452 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1453 return objects_query_opacity (list, style);
1455 } else if (property == QUERY_STYLE_PROPERTY_FONT_SPECIFICATION) {
1456 return objects_query_fontspecification (list, style);
1457 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1458 return objects_query_fontfamily (list, style);
1459 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1460 return objects_query_fontstyle (list, style);
1461 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1462 return objects_query_fontnumbers (list, style);
1463 } else if (property == QUERY_STYLE_PROPERTY_BASELINES) {
1464 return objects_query_baselines (list, style);
1466 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1467 return objects_query_blend (list, style);
1468 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1469 return objects_query_blur (list, style);
1470 }
1471 return QUERY_STYLE_NOTHING;
1472 }
1475 /**
1476 * Query the subselection (if any) or selection on the given desktop for the given property, write
1477 * the result to style, return appropriate flag.
1478 */
1479 int
1480 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1481 {
1482 int ret = desktop->_query_style_signal.emit(style, property);
1484 if (ret != QUERY_STYLE_NOTHING)
1485 return ret; // subselection returned a style, pass it on
1487 // otherwise, do querying and averaging over selection
1488 if (desktop->selection != NULL) {
1489 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1490 }
1492 return QUERY_STYLE_NOTHING;
1493 }
1495 /**
1496 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if at
1497 * least one of the properties did not return QUERY_STYLE_NOTHING.
1498 */
1499 bool
1500 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1501 {
1502 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1503 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1504 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1505 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1506 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1507 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1508 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1509 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1510 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1511 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1512 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1514 return (result_family != QUERY_STYLE_NOTHING ||
1515 result_fstyle != QUERY_STYLE_NOTHING ||
1516 result_fnumbers != QUERY_STYLE_NOTHING ||
1517 result_fill != QUERY_STYLE_NOTHING ||
1518 result_stroke != QUERY_STYLE_NOTHING ||
1519 result_opacity != QUERY_STYLE_NOTHING ||
1520 result_strokewidth != QUERY_STYLE_NOTHING ||
1521 result_strokemiterlimit != QUERY_STYLE_NOTHING ||
1522 result_strokecap != QUERY_STYLE_NOTHING ||
1523 result_strokejoin != QUERY_STYLE_NOTHING ||
1524 result_blur != QUERY_STYLE_NOTHING);
1525 }
1528 /*
1529 Local Variables:
1530 mode:c++
1531 c-file-style:"stroustrup"
1532 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1533 indent-tabs-mode:nil
1534 fill-column:99
1535 End:
1536 */
1537 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :