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