1 #define __SP_DESKTOP_STYLE_C__
3 /** \file
4 * Desktop style management
5 *
6 * Authors:
7 * bulia byak
8 * verbalshadow
9 *
10 * Copyright (C) 2004, 2006 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "desktop.h"
16 #include "color-rgba.h"
17 #include "svg/css-ostringstream.h"
18 #include "svg/svg.h"
19 #include "svg/svg-color.h"
20 #include "selection.h"
21 #include "inkscape.h"
22 #include "style.h"
23 #include "prefs-utils.h"
24 #include "sp-use.h"
25 #include "sp-feblend.h"
26 #include "sp-filter.h"
27 #include "sp-filter-reference.h"
28 #include "sp-gaussian-blur.h"
29 #include "sp-flowtext.h"
30 #include "sp-flowregion.h"
31 #include "sp-flowdiv.h"
32 #include "sp-linear-gradient.h"
33 #include "sp-pattern.h"
34 #include "sp-radial-gradient.h"
35 #include "sp-textpath.h"
36 #include "sp-tref.h"
37 #include "sp-tspan.h"
38 #include "xml/repr.h"
39 #include "libnrtype/font-style-to-pos.h"
40 #include "sp-path.h"
42 #include "desktop-style.h"
43 #include "svg/svg-icc-color.h"
45 /**
46 * Set color on selection on desktop.
47 */
48 void
49 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
50 {
51 /// \todo relative color setting
52 if (is_relative) {
53 g_warning("FIXME: relative color setting not yet implemented");
54 return;
55 }
57 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
58 gchar b[64];
59 sp_svg_write_color(b, sizeof(b), rgba);
60 SPCSSAttr *css = sp_repr_css_attr_new();
61 if (fill) {
62 sp_repr_css_set_property(css, "fill", b);
63 Inkscape::CSSOStringStream osalpha;
64 osalpha << color[3];
65 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
66 } else {
67 sp_repr_css_set_property(css, "stroke", b);
68 Inkscape::CSSOStringStream osalpha;
69 osalpha << color[3];
70 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
71 }
73 sp_desktop_set_style(desktop, css);
75 sp_repr_css_attr_unref(css);
76 }
78 /**
79 * Apply style on object and children, recursively.
80 */
81 void
82 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
83 {
84 // non-items should not have style
85 if (!SP_IS_ITEM(o))
86 return;
88 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
89 // but must always inherit from the parent text. Same for textPath.
90 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
92 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
93 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
94 // flowtext).
96 if (!(skip_lines
97 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
98 || SP_IS_FLOWDIV(o)
99 || SP_IS_FLOWPARA(o)
100 || SP_IS_TEXTPATH(o))
101 && !SP_OBJECT_REPR(o)->attribute("style"))
102 &&
103 !(SP_IS_FLOWREGION(o) ||
104 SP_IS_FLOWREGIONEXCLUDE(o) ||
105 (SP_IS_USE(o) &&
106 SP_OBJECT_PARENT(o) &&
107 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
108 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
109 )
110 )
111 )
112 ) {
114 SPCSSAttr *css_set = sp_repr_css_attr_new();
115 sp_repr_css_merge(css_set, css);
117 // Scale the style by the inverse of the accumulated parent transform in the paste context.
118 {
119 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
120 double const ex(NR::expansion(local));
121 if ( ( ex != 0. )
122 && ( ex != 1. ) ) {
123 sp_css_attr_scale(css_set, 1/ex);
124 }
125 }
127 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
129 sp_repr_css_attr_unref(css_set);
130 }
132 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
133 if (SP_IS_USE(o))
134 return;
136 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
137 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
138 // Unset properties which are accumulating and thus should not be set recursively.
139 // 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.
140 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
141 sp_repr_css_merge(css_recurse, css);
142 sp_repr_css_set_property(css_recurse, "opacity", NULL);
143 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
144 sp_repr_css_attr_unref(css_recurse);
145 } else {
146 sp_desktop_apply_css_recursive(child, css, skip_lines);
147 }
148 }
149 }
151 /**
152 * Apply style on selection on desktop.
153 */
154 void
155 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
156 {
157 if (write_current) {
158 // 1. Set internal value
159 sp_repr_css_merge(desktop->current, css);
161 // 1a. Write to prefs; make a copy and unset any URIs first
162 SPCSSAttr *css_write = sp_repr_css_attr_new();
163 sp_repr_css_merge(css_write, css);
164 sp_css_attr_unset_uris(css_write);
165 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
166 for (const GSList *i = desktop->selection->itemList(); i != NULL; i = i->next) {
167 /* last used styles for 3D box faces are stored separately */
168 if (SP_IS_PATH (i->data)) {
169 const char * descr = SP_OBJECT_REPR (G_OBJECT (i->data))->attribute ("inkscape:box3dface");
170 if (descr != NULL) {
171 gchar *style_grp = g_strconcat ("desktop.", descr, NULL);
172 sp_repr_css_change(inkscape_get_repr(INKSCAPE, style_grp), css_write, "style");
173 g_free (style_grp);
174 }
175 }
176 }
177 sp_repr_css_attr_unref(css_write);
178 }
180 if (!change)
181 return;
183 // 2. Emit signal
184 bool intercepted = desktop->_set_style_signal.emit(css);
186 /** \todo
187 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
188 * rect corners, font size for the object's own transform so that pasting
189 * fills does not depend on preserve/optimize.
190 */
192 // 3. If nobody has intercepted the signal, apply the style to the selection
193 if (!intercepted) {
194 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
195 /// \todo if the style is text-only, apply only to texts?
196 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
197 }
198 }
199 }
201 /**
202 * Return the desktop's current style.
203 */
204 SPCSSAttr *
205 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
206 {
207 SPCSSAttr *css = sp_repr_css_attr_new();
208 sp_repr_css_merge(css, desktop->current);
209 if (!css->attributeList()) {
210 sp_repr_css_attr_unref(css);
211 return NULL;
212 } else {
213 if (!with_text) {
214 css = sp_css_attr_unset_text(css);
215 }
216 return css;
217 }
218 }
220 /**
221 * Return the desktop's current color.
222 */
223 guint32
224 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
225 {
226 guint32 r = 0; // if there's no color, return black
227 gchar const *property = sp_repr_css_property(desktop->current,
228 is_fill ? "fill" : "stroke",
229 "#000");
231 if (desktop->current && property) { // if there is style and the property in it,
232 if (strncmp(property, "url", 3)) { // and if it's not url,
233 // read it
234 r = sp_svg_read_color(property, r);
235 }
236 }
238 return r;
239 }
241 double
242 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
243 {
244 SPCSSAttr *css = NULL;
245 gfloat value = 1.0; // default if nothing else found
246 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
247 css = sp_desktop_get_style(desktop, true);
248 } else {
249 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
250 if (tool_repr) {
251 css = sp_repr_css_attr_inherited(tool_repr, "style");
252 }
253 }
255 if (css) {
256 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
258 if (desktop->current && property) { // if there is style and the property in it,
259 if ( !sp_svg_number_read_f(property, &value) ) {
260 value = 1.0; // things failed. set back to the default
261 }
262 }
264 sp_repr_css_attr_unref(css);
265 }
267 return value;
268 }
269 double
270 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
271 {
272 SPCSSAttr *css = NULL;
273 gfloat value = 1.0; // default if nothing else found
274 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
275 css = sp_desktop_get_style(desktop, true);
276 } else {
277 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
278 if (tool_repr) {
279 css = sp_repr_css_attr_inherited(tool_repr, "style");
280 }
281 }
283 if (css) {
284 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-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 }
290 }
292 sp_repr_css_attr_unref(css);
293 }
295 return value;
296 }
297 guint32
298 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
299 {
300 SPCSSAttr *css = NULL;
301 guint32 r = 0; // if there's no color, return black
302 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
303 css = sp_desktop_get_style(desktop, true);
304 } else {
305 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
306 if (tool_repr) {
307 css = sp_repr_css_attr_inherited(tool_repr, "style");
308 }
309 }
311 if (css) {
312 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
314 if (desktop->current && property) { // if there is style and the property in it,
315 if (strncmp(property, "url", 3)) { // and if it's not url,
316 // read it
317 r = sp_svg_read_color(property, r);
318 }
319 }
321 sp_repr_css_attr_unref(css);
322 }
324 return r | 0xff;
325 }
326 /**
327 * Apply the desktop's current style or the tool style to repr.
328 */
329 void
330 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
331 {
332 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
333 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
334 sp_repr_css_set(repr, css_current, "style");
335 } else {
336 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
337 if (tool_repr) {
338 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
339 sp_repr_css_set(repr, css, "style");
340 sp_repr_css_attr_unref(css);
341 }
342 }
343 if (css_current) {
344 sp_repr_css_attr_unref(css_current);
345 }
346 }
348 /**
349 * Returns the font size (in SVG pixels) of the text tool style (if text
350 * tool uses its own style) or desktop style (otherwise).
351 */
352 double
353 sp_desktop_get_font_size_tool(SPDesktop *desktop)
354 {
355 (void)desktop; // TODO cleanup
356 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
357 gchar const *style_str = NULL;
358 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
359 style_str = desktop_style;
360 } else {
361 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
362 if (tool_repr) {
363 style_str = tool_repr->attribute("style");
364 }
365 }
367 double ret = 12;
368 if (style_str) {
369 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
370 sp_style_merge_from_style_string(style, style_str);
371 ret = style->font_size.computed;
372 sp_style_unref(style);
373 }
374 return ret;
375 }
377 /** Determine average stroke width, simple method */
378 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
379 gdouble
380 stroke_average_width (GSList const *objects)
381 {
382 if (g_slist_length ((GSList *) objects) == 0)
383 return NR_HUGE;
385 gdouble avgwidth = 0.0;
386 bool notstroked = true;
387 int n_notstroked = 0;
389 for (GSList const *l = objects; l != NULL; l = l->next) {
390 if (!SP_IS_ITEM (l->data))
391 continue;
393 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
395 SPObject *object = SP_OBJECT(l->data);
397 if ( object->style->stroke.isNone() ) {
398 ++n_notstroked; // do not count nonstroked objects
399 continue;
400 } else {
401 notstroked = false;
402 }
404 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
405 }
407 if (notstroked)
408 return NR_HUGE;
410 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
411 }
413 /**
414 * Write to style_res the average fill or stroke of list of objects, if applicable.
415 */
416 int
417 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
418 {
419 if (g_slist_length(objects) == 0) {
420 /* No objects, set empty */
421 return QUERY_STYLE_NOTHING;
422 }
424 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
425 bool paintImpossible = true;
426 paint_res->set = TRUE;
428 SVGICCColor* iccColor = 0;
429 bool iccSeen = false;
430 gfloat c[4];
431 c[0] = c[1] = c[2] = c[3] = 0.0;
432 gint num = 0;
434 gfloat prev[3];
435 prev[0] = prev[1] = prev[2] = 0.0;
436 bool same_color = true;
438 for (GSList const *i = objects; i != NULL; i = i->next) {
439 SPObject *obj = SP_OBJECT (i->data);
440 SPStyle *style = SP_OBJECT_STYLE (obj);
441 if (!style) continue;
443 SPIPaint *paint = isfill? &style->fill : &style->stroke;
445 // We consider paint "effectively set" for anything within text hierarchy
446 SPObject *parent = SP_OBJECT_PARENT (obj);
447 bool paint_effectively_set =
448 paint->set || (SP_IS_TEXT(parent) || SP_IS_TEXTPATH(parent) || SP_IS_TSPAN(parent)
449 || SP_IS_FLOWTEXT(parent) || SP_IS_FLOWDIV(parent) || SP_IS_FLOWPARA(parent)
450 || SP_IS_FLOWTSPAN(parent) || SP_IS_FLOWLINE(parent));
452 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
454 if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
455 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
456 }
458 if (paint_res->set && paint->set && paint_res->isPaintserver()) {
459 // both previous paint and this paint were a server, see if the servers are compatible
461 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
462 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
464 if (SP_IS_LINEARGRADIENT (server_res)) {
466 if (!SP_IS_LINEARGRADIENT(server))
467 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
469 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
470 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
471 if (vector_res != vector)
472 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
474 } else if (SP_IS_RADIALGRADIENT (server_res)) {
476 if (!SP_IS_RADIALGRADIENT(server))
477 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
479 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
480 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
481 if (vector_res != vector)
482 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
484 } else if (SP_IS_PATTERN (server_res)) {
486 if (!SP_IS_PATTERN(server))
487 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
489 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
490 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
491 if (pat_res != pat)
492 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
493 }
494 }
496 // 2. Sum color, copy server from paint to paint_res
498 if (paint_res->set && paint_effectively_set && paint->isColor()) {
499 gfloat d[3];
500 sp_color_get_rgb_floatv (&paint->value.color, d);
502 // Check if this color is the same as previous
503 if (paintImpossible) {
504 prev[0] = d[0];
505 prev[1] = d[1];
506 prev[2] = d[2];
507 paint_res->setColor(d[0], d[1], d[2]);
508 iccColor = paint->value.color.icc;
509 iccSeen = true;
510 } else {
511 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2]))
512 same_color = false;
513 if ( iccSeen ) {
514 if(paint->value.color.icc) {
515 // TODO fix this
516 iccColor = 0;
517 }
518 }
519 }
521 // average color
522 c[0] += d[0];
523 c[1] += d[1];
524 c[2] += d[2];
525 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
526 num ++;
527 }
529 paintImpossible = false;
530 paint_res->colorSet = paint->colorSet;
531 paint_res->currentcolor = paint->currentcolor;
532 if (paint_res->set && paint_effectively_set && paint->isPaintserver()) { // copy the server
533 if (isfill) {
534 sp_style_set_to_uri_string (style_res, true, style->getFillURI());
535 } else {
536 sp_style_set_to_uri_string (style_res, false, style->getStrokeURI());
537 }
538 }
539 paint_res->set = paint_effectively_set;
540 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
541 }
543 // After all objects processed, divide the color if necessary and return
544 if (paint_res->set && paint_res->isColor()) { // set the color
545 g_assert (num >= 1);
547 c[0] /= num;
548 c[1] /= num;
549 c[2] /= num;
550 c[3] /= num;
551 paint_res->setColor(c[0], c[1], c[2]);
552 if (isfill) {
553 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
554 } else {
555 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
556 }
559 if ( iccSeen && iccColor ) {
560 // TODO check for existing
561 SVGICCColor* tmp = new SVGICCColor(*iccColor);
562 paint_res->value.color.icc = tmp;
563 }
565 if (num > 1) {
566 if (same_color)
567 return QUERY_STYLE_MULTIPLE_SAME;
568 else
569 return QUERY_STYLE_MULTIPLE_AVERAGED;
570 } else {
571 return QUERY_STYLE_SINGLE;
572 }
573 }
575 // Not color
576 if (g_slist_length(objects) > 1) {
577 return QUERY_STYLE_MULTIPLE_SAME;
578 } else {
579 return QUERY_STYLE_SINGLE;
580 }
581 }
583 /**
584 * Write to style_res the average opacity of a list of objects.
585 */
586 int
587 objects_query_opacity (GSList *objects, SPStyle *style_res)
588 {
589 if (g_slist_length(objects) == 0) {
590 /* No objects, set empty */
591 return QUERY_STYLE_NOTHING;
592 }
594 gdouble opacity_sum = 0;
595 gdouble opacity_prev = -1;
596 bool same_opacity = true;
597 guint opacity_items = 0;
600 for (GSList const *i = objects; i != NULL; i = i->next) {
601 SPObject *obj = SP_OBJECT (i->data);
602 SPStyle *style = SP_OBJECT_STYLE (obj);
603 if (!style) continue;
605 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
606 opacity_sum += opacity;
607 if (opacity_prev != -1 && opacity != opacity_prev)
608 same_opacity = false;
609 opacity_prev = opacity;
610 opacity_items ++;
611 }
612 if (opacity_items > 1)
613 opacity_sum /= opacity_items;
615 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
617 if (opacity_items == 0) {
618 return QUERY_STYLE_NOTHING;
619 } else if (opacity_items == 1) {
620 return QUERY_STYLE_SINGLE;
621 } else {
622 if (same_opacity)
623 return QUERY_STYLE_MULTIPLE_SAME;
624 else
625 return QUERY_STYLE_MULTIPLE_AVERAGED;
626 }
627 }
629 /**
630 * Write to style_res the average stroke width of a list of objects.
631 */
632 int
633 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
634 {
635 if (g_slist_length(objects) == 0) {
636 /* No objects, set empty */
637 return QUERY_STYLE_NOTHING;
638 }
640 gdouble avgwidth = 0.0;
642 gdouble prev_sw = -1;
643 bool same_sw = true;
645 int n_stroked = 0;
647 for (GSList const *i = objects; i != NULL; i = i->next) {
648 SPObject *obj = SP_OBJECT (i->data);
649 if (!SP_IS_ITEM(obj)) continue;
650 SPStyle *style = SP_OBJECT_STYLE (obj);
651 if (!style) continue;
653 if ( style->stroke.isNone() ) {
654 continue;
655 }
657 n_stroked ++;
659 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
660 double sw = style->stroke_width.computed * i2d.expansion();
662 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
663 same_sw = false;
664 prev_sw = sw;
666 avgwidth += sw;
667 }
669 if (n_stroked > 1)
670 avgwidth /= (n_stroked);
672 style_res->stroke_width.computed = avgwidth;
673 style_res->stroke_width.set = true;
675 if (n_stroked == 0) {
676 return QUERY_STYLE_NOTHING;
677 } else if (n_stroked == 1) {
678 return QUERY_STYLE_SINGLE;
679 } else {
680 if (same_sw)
681 return QUERY_STYLE_MULTIPLE_SAME;
682 else
683 return QUERY_STYLE_MULTIPLE_AVERAGED;
684 }
685 }
687 /**
688 * Write to style_res the average miter limit of a list of objects.
689 */
690 int
691 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
692 {
693 if (g_slist_length(objects) == 0) {
694 /* No objects, set empty */
695 return QUERY_STYLE_NOTHING;
696 }
698 gdouble avgml = 0.0;
699 int n_stroked = 0;
701 gdouble prev_ml = -1;
702 bool same_ml = true;
704 for (GSList const *i = objects; i != NULL; i = i->next) {
705 SPObject *obj = SP_OBJECT (i->data);
706 if (!SP_IS_ITEM(obj)) continue;
707 SPStyle *style = SP_OBJECT_STYLE (obj);
708 if (!style) continue;
710 if ( style->stroke.isNone() ) {
711 continue;
712 }
714 n_stroked ++;
716 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
717 same_ml = false;
718 prev_ml = style->stroke_miterlimit.value;
720 avgml += style->stroke_miterlimit.value;
721 }
723 if (n_stroked > 1)
724 avgml /= (n_stroked);
726 style_res->stroke_miterlimit.value = avgml;
727 style_res->stroke_miterlimit.set = true;
729 if (n_stroked == 0) {
730 return QUERY_STYLE_NOTHING;
731 } else if (n_stroked == 1) {
732 return QUERY_STYLE_SINGLE;
733 } else {
734 if (same_ml)
735 return QUERY_STYLE_MULTIPLE_SAME;
736 else
737 return QUERY_STYLE_MULTIPLE_AVERAGED;
738 }
739 }
741 /**
742 * Write to style_res the stroke cap of a list of objects.
743 */
744 int
745 objects_query_strokecap (GSList *objects, SPStyle *style_res)
746 {
747 if (g_slist_length(objects) == 0) {
748 /* No objects, set empty */
749 return QUERY_STYLE_NOTHING;
750 }
752 int cap = -1;
753 gdouble prev_cap = -1;
754 bool same_cap = true;
755 int n_stroked = 0;
757 for (GSList const *i = objects; i != NULL; i = i->next) {
758 SPObject *obj = SP_OBJECT (i->data);
759 if (!SP_IS_ITEM(obj)) continue;
760 SPStyle *style = SP_OBJECT_STYLE (obj);
761 if (!style) continue;
763 if ( style->stroke.isNone() ) {
764 continue;
765 }
767 n_stroked ++;
769 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
770 same_cap = false;
771 prev_cap = style->stroke_linecap.value;
773 cap = style->stroke_linecap.value;
774 }
776 style_res->stroke_linecap.value = cap;
777 style_res->stroke_linecap.set = true;
779 if (n_stroked == 0) {
780 return QUERY_STYLE_NOTHING;
781 } else if (n_stroked == 1) {
782 return QUERY_STYLE_SINGLE;
783 } else {
784 if (same_cap)
785 return QUERY_STYLE_MULTIPLE_SAME;
786 else
787 return QUERY_STYLE_MULTIPLE_DIFFERENT;
788 }
789 }
791 /**
792 * Write to style_res the stroke join of a list of objects.
793 */
794 int
795 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
796 {
797 if (g_slist_length(objects) == 0) {
798 /* No objects, set empty */
799 return QUERY_STYLE_NOTHING;
800 }
802 int join = -1;
803 gdouble prev_join = -1;
804 bool same_join = true;
805 int n_stroked = 0;
807 for (GSList const *i = objects; i != NULL; i = i->next) {
808 SPObject *obj = SP_OBJECT (i->data);
809 if (!SP_IS_ITEM(obj)) continue;
810 SPStyle *style = SP_OBJECT_STYLE (obj);
811 if (!style) continue;
813 if ( style->stroke.isNone() ) {
814 continue;
815 }
817 n_stroked ++;
819 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
820 same_join = false;
821 prev_join = style->stroke_linejoin.value;
823 join = style->stroke_linejoin.value;
824 }
826 style_res->stroke_linejoin.value = join;
827 style_res->stroke_linejoin.set = true;
829 if (n_stroked == 0) {
830 return QUERY_STYLE_NOTHING;
831 } else if (n_stroked == 1) {
832 return QUERY_STYLE_SINGLE;
833 } else {
834 if (same_join)
835 return QUERY_STYLE_MULTIPLE_SAME;
836 else
837 return QUERY_STYLE_MULTIPLE_DIFFERENT;
838 }
839 }
841 /**
842 * Write to style_res the average font size and spacing of objects.
843 */
844 int
845 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
846 {
847 bool different = false;
849 double size = 0;
850 double letterspacing = 0;
851 double linespacing = 0;
852 bool linespacing_normal = false;
853 bool letterspacing_normal = false;
855 double size_prev = 0;
856 double letterspacing_prev = 0;
857 double linespacing_prev = 0;
859 /// \todo FIXME: add word spacing, kerns? rotates?
861 int texts = 0;
863 for (GSList const *i = objects; i != NULL; i = i->next) {
864 SPObject *obj = SP_OBJECT (i->data);
866 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
867 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
868 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
869 continue;
871 SPStyle *style = SP_OBJECT_STYLE (obj);
872 if (!style) continue;
874 texts ++;
875 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
877 if (style->letter_spacing.normal) {
878 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
879 letterspacing_normal = true;
880 } else {
881 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
882 letterspacing_normal = false;
883 }
885 double linespacing_current;
886 if (style->line_height.normal) {
887 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
888 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
889 linespacing_normal = true;
890 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
891 linespacing_current = style->line_height.value;
892 linespacing_normal = false;
893 } else { // we need % here
894 linespacing_current = style->line_height.computed / style->font_size.computed;
895 linespacing_normal = false;
896 }
897 linespacing += linespacing_current;
899 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
900 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
901 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
902 different = true;
903 }
905 size_prev = style->font_size.computed;
906 letterspacing_prev = style->letter_spacing.computed;
907 linespacing_prev = linespacing_current;
909 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
910 style_res->text_anchor.computed = style->text_anchor.computed;
911 style_res->writing_mode.computed = style->writing_mode.computed;
912 }
914 if (texts == 0)
915 return QUERY_STYLE_NOTHING;
917 if (texts > 1) {
918 size /= texts;
919 letterspacing /= texts;
920 linespacing /= texts;
921 }
923 style_res->font_size.computed = size;
924 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
926 style_res->letter_spacing.normal = letterspacing_normal;
927 style_res->letter_spacing.computed = letterspacing;
929 style_res->line_height.normal = linespacing_normal;
930 style_res->line_height.computed = linespacing;
931 style_res->line_height.value = linespacing;
932 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
934 if (texts > 1) {
935 if (different) {
936 return QUERY_STYLE_MULTIPLE_AVERAGED;
937 } else {
938 return QUERY_STYLE_MULTIPLE_SAME;
939 }
940 } else {
941 return QUERY_STYLE_SINGLE;
942 }
943 }
945 /**
946 * Write to style_res the average font style of objects.
947 */
948 int
949 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
950 {
951 bool different = false;
952 bool set = false;
954 int texts = 0;
956 for (GSList const *i = objects; i != NULL; i = i->next) {
957 SPObject *obj = SP_OBJECT (i->data);
959 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
960 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
961 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
962 continue;
964 SPStyle *style = SP_OBJECT_STYLE (obj);
965 if (!style) continue;
967 texts ++;
969 if (set &&
970 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
971 different = true; // different styles
972 }
974 set = TRUE;
975 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
976 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
977 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
978 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
979 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
980 }
982 if (texts == 0 || !set)
983 return QUERY_STYLE_NOTHING;
985 if (texts > 1) {
986 if (different) {
987 return QUERY_STYLE_MULTIPLE_DIFFERENT;
988 } else {
989 return QUERY_STYLE_MULTIPLE_SAME;
990 }
991 } else {
992 return QUERY_STYLE_SINGLE;
993 }
994 }
996 /**
997 * Write to style_res the average font family of objects.
998 */
999 int
1000 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
1001 {
1002 bool different = false;
1003 int texts = 0;
1005 if (style_res->text->font_family.value) {
1006 g_free(style_res->text->font_family.value);
1007 style_res->text->font_family.value = NULL;
1008 }
1009 style_res->text->font_family.set = FALSE;
1011 for (GSList const *i = objects; i != NULL; i = i->next) {
1012 SPObject *obj = SP_OBJECT (i->data);
1014 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
1015 && !SP_IS_TSPAN(obj) && !SP_IS_TREF(obj) && !SP_IS_TEXTPATH(obj)
1016 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
1017 continue;
1019 SPStyle *style = SP_OBJECT_STYLE (obj);
1020 if (!style) continue;
1022 texts ++;
1024 if (style_res->text->font_family.value && style->text->font_family.value &&
1025 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
1026 different = true; // different fonts
1027 }
1029 if (style_res->text->font_family.value) {
1030 g_free(style_res->text->font_family.value);
1031 style_res->text->font_family.value = NULL;
1032 }
1034 style_res->text->font_family.set = TRUE;
1035 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
1036 }
1038 if (texts == 0 || !style_res->text->font_family.set)
1039 return QUERY_STYLE_NOTHING;
1041 if (texts > 1) {
1042 if (different) {
1043 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1044 } else {
1045 return QUERY_STYLE_MULTIPLE_SAME;
1046 }
1047 } else {
1048 return QUERY_STYLE_SINGLE;
1049 }
1050 }
1052 int
1053 objects_query_blend (GSList *objects, SPStyle *style_res)
1054 {
1055 const int empty_prev = -2;
1056 const int complex_filter = 5;
1057 int blend = 0;
1058 float blend_prev = empty_prev;
1059 bool same_blend = true;
1060 guint items = 0;
1062 for (GSList const *i = objects; i != NULL; i = i->next) {
1063 SPObject *obj = SP_OBJECT (i->data);
1064 SPStyle *style = SP_OBJECT_STYLE (obj);
1065 if(!style || !SP_IS_ITEM(obj)) continue;
1067 items++;
1069 //if object has a filter
1070 if (style->filter.set && style->getFilter()) {
1071 int blurcount = 0;
1072 int blendcount = 0;
1074 // determine whether filter is simple (blend and/or blur) or complex
1075 for(SPObject *primitive_obj = style->getFilter()->children;
1076 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1077 primitive_obj = primitive_obj->next) {
1078 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1079 if(SP_IS_FEBLEND(primitive))
1080 ++blendcount;
1081 else if(SP_IS_GAUSSIANBLUR(primitive))
1082 ++blurcount;
1083 else {
1084 blurcount = complex_filter;
1085 break;
1086 }
1087 }
1089 // simple filter
1090 if(blurcount == 1 || blendcount == 1) {
1091 for(SPObject *primitive_obj = style->getFilter()->children;
1092 primitive_obj && SP_IS_FILTER_PRIMITIVE(primitive_obj);
1093 primitive_obj = primitive_obj->next) {
1094 if(SP_IS_FEBLEND(primitive_obj)) {
1095 SPFeBlend *spblend = SP_FEBLEND(primitive_obj);
1096 blend = spblend->blend_mode;
1097 }
1098 }
1099 }
1100 else {
1101 blend = complex_filter;
1102 }
1103 }
1104 // defaults to blend mode = "normal"
1105 else {
1106 blend = 0;
1107 }
1109 if(blend_prev != empty_prev && blend_prev != blend)
1110 same_blend = false;
1111 blend_prev = blend;
1112 }
1114 if (items > 0) {
1115 style_res->filter_blend_mode.value = blend;
1116 }
1118 if (items == 0) {
1119 return QUERY_STYLE_NOTHING;
1120 } else if (items == 1) {
1121 return QUERY_STYLE_SINGLE;
1122 } else {
1123 if(same_blend)
1124 return QUERY_STYLE_MULTIPLE_SAME;
1125 else
1126 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1127 }
1128 }
1130 /**
1131 * Write to style_res the average blurring of a list of objects.
1132 */
1133 int
1134 objects_query_blur (GSList *objects, SPStyle *style_res)
1135 {
1136 if (g_slist_length(objects) == 0) {
1137 /* No objects, set empty */
1138 return QUERY_STYLE_NOTHING;
1139 }
1141 float blur_sum = 0;
1142 float blur_prev = -1;
1143 bool same_blur = true;
1144 guint blur_items = 0;
1145 guint items = 0;
1147 for (GSList const *i = objects; i != NULL; i = i->next) {
1148 SPObject *obj = SP_OBJECT (i->data);
1149 SPStyle *style = SP_OBJECT_STYLE (obj);
1150 if (!style) continue;
1151 if (!SP_IS_ITEM(obj)) continue;
1153 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
1155 items ++;
1157 //if object has a filter
1158 if (style->filter.set && style->getFilter()) {
1159 //cycle through filter primitives
1160 SPObject *primitive_obj = style->getFilter()->children;
1161 while (primitive_obj) {
1162 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
1163 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
1165 //if primitive is gaussianblur
1166 if(SP_IS_GAUSSIANBLUR(primitive)) {
1167 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1168 float num = spblur->stdDeviation.getNumber();
1169 blur_sum += num * NR::expansion(i2d);
1170 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
1171 same_blur = false;
1172 blur_prev = num;
1173 //TODO: deal with opt number, for the moment it's not necessary to the ui.
1174 blur_items ++;
1175 }
1176 }
1177 primitive_obj = primitive_obj->next;
1178 }
1179 }
1180 }
1182 if (items > 0) {
1183 if (blur_items > 0)
1184 blur_sum /= blur_items;
1185 style_res->filter_gaussianBlur_deviation.value = blur_sum;
1186 }
1188 if (items == 0) {
1189 return QUERY_STYLE_NOTHING;
1190 } else if (items == 1) {
1191 return QUERY_STYLE_SINGLE;
1192 } else {
1193 if (same_blur)
1194 return QUERY_STYLE_MULTIPLE_SAME;
1195 else
1196 return QUERY_STYLE_MULTIPLE_AVERAGED;
1197 }
1198 }
1200 /**
1201 * Query the given list of objects for the given property, write
1202 * the result to style, return appropriate flag.
1203 */
1204 int
1205 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1206 {
1207 if (property == QUERY_STYLE_PROPERTY_FILL) {
1208 return objects_query_fillstroke (list, style, true);
1209 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1210 return objects_query_fillstroke (list, style, false);
1212 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1213 return objects_query_strokewidth (list, style);
1214 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1215 return objects_query_miterlimit (list, style);
1216 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1217 return objects_query_strokecap (list, style);
1218 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1219 return objects_query_strokejoin (list, style);
1221 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1222 return objects_query_opacity (list, style);
1224 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1225 return objects_query_fontfamily (list, style);
1226 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1227 return objects_query_fontstyle (list, style);
1228 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1229 return objects_query_fontnumbers (list, style);
1231 } else if (property == QUERY_STYLE_PROPERTY_BLEND) {
1232 return objects_query_blend (list, style);
1233 } else if (property == QUERY_STYLE_PROPERTY_BLUR) {
1234 return objects_query_blur (list, style);
1235 }
1236 return QUERY_STYLE_NOTHING;
1237 }
1240 /**
1241 * Query the subselection (if any) or selection on the given desktop for the given property, write
1242 * the result to style, return appropriate flag.
1243 */
1244 int
1245 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1246 {
1247 int ret = desktop->_query_style_signal.emit(style, property);
1249 if (ret != QUERY_STYLE_NOTHING)
1250 return ret; // subselection returned a style, pass it on
1252 // otherwise, do querying and averaging over selection
1253 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1254 }
1256 /**
1257 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1258 * the properties returned QUERY_STYLE_NOTHING.
1259 */
1260 bool
1261 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1262 {
1263 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1264 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1265 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1266 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1267 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1268 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1269 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1270 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1271 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1272 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1273 int result_blur = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_BLUR);
1275 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);
1276 }
1279 /*
1280 Local Variables:
1281 mode:c++
1282 c-file-style:"stroustrup"
1283 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1284 indent-tabs-mode:nil
1285 fill-column:99
1286 End:
1287 */
1288 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :