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-flowtext.h"
28 #include "sp-flowregion.h"
29 #include "sp-flowdiv.h"
30 #include "sp-linear-gradient.h"
31 #include "sp-radial-gradient.h"
32 #include "sp-pattern.h"
33 #include "xml/repr.h"
34 #include "libnrtype/font-style-to-pos.h"
37 #include "desktop-style.h"
39 /**
40 * Set color on selection on desktop.
41 */
42 void
43 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
44 {
45 /// \todo relative color setting
46 if (is_relative) {
47 g_warning("FIXME: relative color setting not yet implemented");
48 return;
49 }
51 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
52 gchar b[64];
53 sp_svg_write_color(b, 64, rgba);
54 SPCSSAttr *css = sp_repr_css_attr_new();
55 if (fill) {
56 sp_repr_css_set_property(css, "fill", b);
57 Inkscape::CSSOStringStream osalpha;
58 osalpha << color[3];
59 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
60 } else {
61 sp_repr_css_set_property(css, "stroke", b);
62 Inkscape::CSSOStringStream osalpha;
63 osalpha << color[3];
64 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
65 }
67 sp_desktop_set_style(desktop, css);
69 sp_repr_css_attr_unref(css);
70 }
72 /**
73 * Apply style on object and children, recursively.
74 */
75 void
76 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
77 {
78 // non-items should not have style
79 if (!SP_IS_ITEM(o))
80 return;
82 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
83 // but must always inherit from the parent text. Same for textPath.
84 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
86 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
87 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
88 // flowtext).
90 if (!(skip_lines
91 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
92 || SP_IS_FLOWDIV(o)
93 || SP_IS_FLOWPARA(o)
94 || SP_IS_TEXTPATH(o))
95 && !SP_OBJECT_REPR(o)->attribute("style"))
96 &&
97 !(SP_IS_FLOWREGION(o) ||
98 SP_IS_FLOWREGIONEXCLUDE(o) ||
99 (SP_IS_USE(o) &&
100 SP_OBJECT_PARENT(o) &&
101 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
102 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
103 )
104 )
105 )
106 ) {
108 SPCSSAttr *css_set = sp_repr_css_attr_new();
109 sp_repr_css_merge(css_set, css);
111 // Scale the style by the inverse of the accumulated parent transform in the paste context.
112 {
113 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
114 double const ex(NR::expansion(local));
115 if ( ( ex != 0. )
116 && ( ex != 1. ) ) {
117 sp_css_attr_scale(css_set, 1/ex);
118 }
119 }
121 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
123 sp_repr_css_attr_unref(css_set);
124 }
126 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
127 if (SP_IS_USE(o))
128 return;
130 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
131 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
132 // Unset properties which are accumulating and thus should not be set recursively.
133 // 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.
134 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
135 sp_repr_css_merge(css_recurse, css);
136 sp_repr_css_set_property(css_recurse, "opacity", NULL);
137 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
138 sp_repr_css_attr_unref(css_recurse);
139 } else {
140 sp_desktop_apply_css_recursive(child, css, skip_lines);
141 }
142 }
143 }
145 /**
146 * Apply style on selection on desktop.
147 */
148 void
149 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
150 {
151 if (write_current) {
152 // 1. Set internal value
153 sp_repr_css_merge(desktop->current, css);
155 // 1a. Write to prefs; make a copy and unset any URIs first
156 SPCSSAttr *css_write = sp_repr_css_attr_new();
157 sp_repr_css_merge(css_write, css);
158 sp_css_attr_unset_uris(css_write);
159 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
160 sp_repr_css_attr_unref(css_write);
161 }
163 if (!change)
164 return;
166 // 2. Emit signal
167 bool intercepted = desktop->_set_style_signal.emit(css);
169 /** \todo
170 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
171 * rect corners, font size for the object's own transform so that pasting
172 * fills does not depend on preserve/optimize.
173 */
175 // 3. If nobody has intercepted the signal, apply the style to the selection
176 if (!intercepted) {
177 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
178 /// \todo if the style is text-only, apply only to texts?
179 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
180 }
181 }
182 }
184 /**
185 * Return the desktop's current style.
186 */
187 SPCSSAttr *
188 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
189 {
190 SPCSSAttr *css = sp_repr_css_attr_new();
191 sp_repr_css_merge(css, desktop->current);
192 if (!css->attributeList()) {
193 sp_repr_css_attr_unref(css);
194 return NULL;
195 } else {
196 if (!with_text) {
197 css = sp_css_attr_unset_text(css);
198 }
199 return css;
200 }
201 }
203 /**
204 * Return the desktop's current color.
205 */
206 guint32
207 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
208 {
209 guint32 r = 0; // if there's no color, return black
210 gchar const *property = sp_repr_css_property(desktop->current,
211 is_fill ? "fill" : "stroke",
212 "#000");
214 if (desktop->current && property) { // if there is style and the property in it,
215 if (strncmp(property, "url", 3)) { // and if it's not url,
216 // read it
217 r = sp_svg_read_color(property, r);
218 }
219 }
221 return r;
222 }
224 double
225 sp_desktop_get_master_opacity_tool(SPDesktop *desktop, char const *tool)
226 {
227 SPCSSAttr *css = NULL;
228 gfloat value = 1.0; // default if nothing else found
229 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
230 css = sp_desktop_get_style(desktop, true);
231 } else {
232 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
233 if (tool_repr) {
234 css = sp_repr_css_attr_inherited(tool_repr, "style");
235 }
236 }
238 if (css) {
239 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
241 if (desktop->current && property) { // if there is style and the property in it,
242 if ( !sp_svg_number_read_f(property, &value) ) {
243 value = 1.0; // things failed. set back to the default
244 }
245 }
247 sp_repr_css_attr_unref(css);
248 }
250 return value;
251 }
252 double
253 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
254 {
255 SPCSSAttr *css = NULL;
256 gfloat value = 1.0; // default if nothing else found
257 if (prefs_get_double_attribute(tool, "usecurrent", 0) != 0) {
258 css = sp_desktop_get_style(desktop, true);
259 } else {
260 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
261 if (tool_repr) {
262 css = sp_repr_css_attr_inherited(tool_repr, "style");
263 }
264 }
266 if (css) {
267 gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
269 if (desktop->current && property) { // if there is style and the property in it,
270 if ( !sp_svg_number_read_f(property, &value) ) {
271 value = 1.0; // things failed. set back to the default
272 }
273 }
275 sp_repr_css_attr_unref(css);
276 }
278 return value;
279 }
280 guint32
281 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
282 {
283 SPCSSAttr *css = NULL;
284 guint32 r = 0; // if there's no color, return black
285 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
286 css = sp_desktop_get_style(desktop, true);
287 } else {
288 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
289 if (tool_repr) {
290 css = sp_repr_css_attr_inherited(tool_repr, "style");
291 }
292 }
294 if (css) {
295 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
297 if (desktop->current && property) { // if there is style and the property in it,
298 if (strncmp(property, "url", 3)) { // and if it's not url,
299 // read it
300 r = sp_svg_read_color(property, r);
301 }
302 }
304 sp_repr_css_attr_unref(css);
305 }
307 return r | 0xff;
308 }
309 /**
310 * Apply the desktop's current style or the tool style to repr.
311 */
312 void
313 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
314 {
315 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
316 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
317 sp_repr_css_set(repr, css_current, "style");
318 } else {
319 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
320 if (tool_repr) {
321 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
322 sp_repr_css_set(repr, css, "style");
323 sp_repr_css_attr_unref(css);
324 }
325 }
326 if (css_current) {
327 sp_repr_css_attr_unref(css_current);
328 }
329 }
331 /**
332 * Returns the font size (in SVG pixels) of the text tool style (if text
333 * tool uses its own style) or desktop style (otherwise).
334 */
335 double
336 sp_desktop_get_font_size_tool(SPDesktop *desktop)
337 {
338 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
339 gchar const *style_str = NULL;
340 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
341 style_str = desktop_style;
342 } else {
343 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
344 if (tool_repr) {
345 style_str = tool_repr->attribute("style");
346 }
347 }
349 double ret = 12;
350 if (style_str) {
351 SPStyle *style = sp_style_new();
352 sp_style_merge_from_style_string(style, style_str);
353 ret = style->font_size.computed;
354 sp_style_unref(style);
355 }
356 return ret;
357 }
359 /** Determine average stroke width, simple method */
360 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
361 gdouble
362 stroke_average_width (GSList const *objects)
363 {
364 if (g_slist_length ((GSList *) objects) == 0)
365 return NR_HUGE;
367 gdouble avgwidth = 0.0;
368 bool notstroked = true;
369 int n_notstroked = 0;
371 for (GSList const *l = objects; l != NULL; l = l->next) {
372 if (!SP_IS_ITEM (l->data))
373 continue;
375 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
377 SPObject *object = SP_OBJECT(l->data);
379 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
380 ++n_notstroked; // do not count nonstroked objects
381 continue;
382 } else {
383 notstroked = false;
384 }
386 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
387 }
389 if (notstroked)
390 return NR_HUGE;
392 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
393 }
395 /**
396 * Write to style_res the average fill or stroke of list of objects, if applicable.
397 */
398 int
399 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
400 {
401 if (g_slist_length(objects) == 0) {
402 /* No objects, set empty */
403 return QUERY_STYLE_NOTHING;
404 }
406 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
407 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
408 paint_res->set = TRUE;
410 gfloat c[4];
411 c[0] = c[1] = c[2] = c[3] = 0.0;
412 gint num = 0;
414 gfloat prev[4];
415 prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
416 bool same_color = true;
418 for (GSList const *i = objects; i != NULL; i = i->next) {
419 SPObject *obj = SP_OBJECT (i->data);
420 SPStyle *style = SP_OBJECT_STYLE (obj);
421 if (!style) continue;
423 SPIPaint *paint = isfill? &style->fill : &style->stroke;
425 // We consider paint "effectively set" for anything within text hierarchy
426 SPObject *parent = SP_OBJECT_PARENT (obj);
427 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));
429 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
431 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
432 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
433 }
435 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
436 // both previous paint and this paint were a server, see if the servers are compatible
438 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
439 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
441 if (SP_IS_LINEARGRADIENT (server_res)) {
443 if (!SP_IS_LINEARGRADIENT(server))
444 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
446 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
447 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
448 if (vector_res != vector)
449 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
451 } else if (SP_IS_RADIALGRADIENT (server_res)) {
453 if (!SP_IS_RADIALGRADIENT(server))
454 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
456 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
457 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
458 if (vector_res != vector)
459 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
461 } else if (SP_IS_PATTERN (server_res)) {
463 if (!SP_IS_PATTERN(server))
464 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
466 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
467 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
468 if (pat_res != pat)
469 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
470 }
471 }
473 // 2. Sum color, copy server from paint to paint_res
475 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
477 gfloat d[3];
478 sp_color_get_rgb_floatv (&paint->value.color, d);
480 // Check if this color is the same as previous
481 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
482 prev[0] = d[0];
483 prev[1] = d[1];
484 prev[2] = d[2];
485 prev[3] = d[3];
486 } else {
487 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
488 same_color = false;
489 }
491 // average color
492 c[0] += d[0];
493 c[1] += d[1];
494 c[2] += d[2];
495 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
496 num ++;
497 }
499 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
500 if (isfill) {
501 SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
502 } else {
503 SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
504 }
505 }
506 paint_res->type = paint->type;
507 paint_res->set = paint_effectively_set;
508 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
509 }
511 // After all objects processed, divide the color if necessary and return
512 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
513 g_assert (num >= 1);
515 c[0] /= num;
516 c[1] /= num;
517 c[2] /= num;
518 c[3] /= num;
519 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
520 if (isfill) {
521 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
522 } else {
523 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
524 }
525 if (num > 1) {
526 if (same_color)
527 return QUERY_STYLE_MULTIPLE_SAME;
528 else
529 return QUERY_STYLE_MULTIPLE_AVERAGED;
530 } else {
531 return QUERY_STYLE_SINGLE;
532 }
533 }
535 // Not color
536 if (g_slist_length(objects) > 1) {
537 return QUERY_STYLE_MULTIPLE_SAME;
538 } else {
539 return QUERY_STYLE_SINGLE;
540 }
541 }
543 /**
544 * Write to style_res the average opacity of a list of objects.
545 */
546 int
547 objects_query_opacity (GSList *objects, SPStyle *style_res)
548 {
549 if (g_slist_length(objects) == 0) {
550 /* No objects, set empty */
551 return QUERY_STYLE_NOTHING;
552 }
554 gdouble opacity_sum = 0;
555 gdouble opacity_prev = -1;
556 bool same_opacity = true;
557 guint opacity_items = 0;
560 for (GSList const *i = objects; i != NULL; i = i->next) {
561 SPObject *obj = SP_OBJECT (i->data);
562 SPStyle *style = SP_OBJECT_STYLE (obj);
563 if (!style) continue;
565 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
566 opacity_sum += opacity;
567 if (opacity_prev != -1 && opacity != opacity_prev)
568 same_opacity = false;
569 opacity_prev = opacity;
570 opacity_items ++;
571 }
572 if (opacity_items > 1)
573 opacity_sum /= opacity_items;
575 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
577 if (opacity_items == 0) {
578 return QUERY_STYLE_NOTHING;
579 } else if (opacity_items == 1) {
580 return QUERY_STYLE_SINGLE;
581 } else {
582 if (same_opacity)
583 return QUERY_STYLE_MULTIPLE_SAME;
584 else
585 return QUERY_STYLE_MULTIPLE_AVERAGED;
586 }
587 }
589 /**
590 * Write to style_res the average stroke width of a list of objects.
591 */
592 int
593 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
594 {
595 if (g_slist_length(objects) == 0) {
596 /* No objects, set empty */
597 return QUERY_STYLE_NOTHING;
598 }
600 gdouble avgwidth = 0.0;
602 gdouble prev_sw = -1;
603 bool same_sw = true;
605 int n_stroked = 0;
607 for (GSList const *i = objects; i != NULL; i = i->next) {
608 SPObject *obj = SP_OBJECT (i->data);
609 if (!SP_IS_ITEM(obj)) continue;
610 SPStyle *style = SP_OBJECT_STYLE (obj);
611 if (!style) continue;
613 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
614 continue;
615 }
617 n_stroked ++;
619 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
620 double sw = style->stroke_width.computed * i2d.expansion();
622 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
623 same_sw = false;
624 prev_sw = sw;
626 avgwidth += sw;
627 }
629 if (n_stroked > 1)
630 avgwidth /= (n_stroked);
632 style_res->stroke_width.computed = avgwidth;
633 style_res->stroke_width.set = true;
635 if (n_stroked == 0) {
636 return QUERY_STYLE_NOTHING;
637 } else if (n_stroked == 1) {
638 return QUERY_STYLE_SINGLE;
639 } else {
640 if (same_sw)
641 return QUERY_STYLE_MULTIPLE_SAME;
642 else
643 return QUERY_STYLE_MULTIPLE_AVERAGED;
644 }
645 }
647 /**
648 * Write to style_res the average miter limit of a list of objects.
649 */
650 int
651 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
652 {
653 if (g_slist_length(objects) == 0) {
654 /* No objects, set empty */
655 return QUERY_STYLE_NOTHING;
656 }
658 gdouble avgml = 0.0;
659 int n_stroked = 0;
661 gdouble prev_ml = -1;
662 bool same_ml = true;
664 for (GSList const *i = objects; i != NULL; i = i->next) {
665 SPObject *obj = SP_OBJECT (i->data);
666 if (!SP_IS_ITEM(obj)) continue;
667 SPStyle *style = SP_OBJECT_STYLE (obj);
668 if (!style) continue;
670 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
671 continue;
672 }
674 n_stroked ++;
676 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
677 same_ml = false;
678 prev_ml = style->stroke_miterlimit.value;
680 avgml += style->stroke_miterlimit.value;
681 }
683 if (n_stroked > 1)
684 avgml /= (n_stroked);
686 style_res->stroke_miterlimit.value = avgml;
687 style_res->stroke_miterlimit.set = true;
689 if (n_stroked == 0) {
690 return QUERY_STYLE_NOTHING;
691 } else if (n_stroked == 1) {
692 return QUERY_STYLE_SINGLE;
693 } else {
694 if (same_ml)
695 return QUERY_STYLE_MULTIPLE_SAME;
696 else
697 return QUERY_STYLE_MULTIPLE_AVERAGED;
698 }
699 }
701 /**
702 * Write to style_res the stroke cap of a list of objects.
703 */
704 int
705 objects_query_strokecap (GSList *objects, SPStyle *style_res)
706 {
707 if (g_slist_length(objects) == 0) {
708 /* No objects, set empty */
709 return QUERY_STYLE_NOTHING;
710 }
712 int cap = -1;
713 gdouble prev_cap = -1;
714 bool same_cap = true;
715 int n_stroked = 0;
717 for (GSList const *i = objects; i != NULL; i = i->next) {
718 SPObject *obj = SP_OBJECT (i->data);
719 if (!SP_IS_ITEM(obj)) continue;
720 SPStyle *style = SP_OBJECT_STYLE (obj);
721 if (!style) continue;
723 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
724 continue;
725 }
727 n_stroked ++;
729 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
730 same_cap = false;
731 prev_cap = style->stroke_linecap.value;
733 cap = style->stroke_linecap.value;
734 }
736 style_res->stroke_linecap.value = cap;
737 style_res->stroke_linecap.set = true;
739 if (n_stroked == 0) {
740 return QUERY_STYLE_NOTHING;
741 } else if (n_stroked == 1) {
742 return QUERY_STYLE_SINGLE;
743 } else {
744 if (same_cap)
745 return QUERY_STYLE_MULTIPLE_SAME;
746 else
747 return QUERY_STYLE_MULTIPLE_DIFFERENT;
748 }
749 }
751 /**
752 * Write to style_res the stroke join of a list of objects.
753 */
754 int
755 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
756 {
757 if (g_slist_length(objects) == 0) {
758 /* No objects, set empty */
759 return QUERY_STYLE_NOTHING;
760 }
762 int join = -1;
763 gdouble prev_join = -1;
764 bool same_join = true;
765 int n_stroked = 0;
767 for (GSList const *i = objects; i != NULL; i = i->next) {
768 SPObject *obj = SP_OBJECT (i->data);
769 if (!SP_IS_ITEM(obj)) continue;
770 SPStyle *style = SP_OBJECT_STYLE (obj);
771 if (!style) continue;
773 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
774 continue;
775 }
777 n_stroked ++;
779 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
780 same_join = false;
781 prev_join = style->stroke_linejoin.value;
783 join = style->stroke_linejoin.value;
784 }
786 style_res->stroke_linejoin.value = join;
787 style_res->stroke_linejoin.set = true;
789 if (n_stroked == 0) {
790 return QUERY_STYLE_NOTHING;
791 } else if (n_stroked == 1) {
792 return QUERY_STYLE_SINGLE;
793 } else {
794 if (same_join)
795 return QUERY_STYLE_MULTIPLE_SAME;
796 else
797 return QUERY_STYLE_MULTIPLE_DIFFERENT;
798 }
799 }
801 /**
802 * Write to style_res the average font size and spacing of objects.
803 */
804 int
805 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
806 {
807 bool different = false;
809 double size = 0;
810 double letterspacing = 0;
811 double linespacing = 0;
812 bool linespacing_normal = false;
813 bool letterspacing_normal = false;
815 double size_prev = 0;
816 double letterspacing_prev = 0;
817 double linespacing_prev = 0;
819 /// \todo FIXME: add word spacing, kerns? rotates?
821 int texts = 0;
823 for (GSList const *i = objects; i != NULL; i = i->next) {
824 SPObject *obj = SP_OBJECT (i->data);
826 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
827 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
828 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
829 continue;
831 SPStyle *style = SP_OBJECT_STYLE (obj);
832 if (!style) continue;
834 texts ++;
835 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
837 if (style->letter_spacing.normal) {
838 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
839 letterspacing_normal = true;
840 } else {
841 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
842 letterspacing_normal = false;
843 }
845 double linespacing_current;
846 if (style->line_height.normal) {
847 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
848 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
849 linespacing_normal = true;
850 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
851 linespacing_current = style->line_height.value;
852 linespacing_normal = false;
853 } else { // we need % here
854 linespacing_current = style->line_height.computed / style->font_size.computed;
855 linespacing_normal = false;
856 }
857 linespacing += linespacing_current;
859 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
860 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
861 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
862 different = true;
863 }
865 size_prev = style->font_size.computed;
866 letterspacing_prev = style->letter_spacing.computed;
867 linespacing_prev = linespacing_current;
869 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
870 style_res->text_anchor.computed = style->text_anchor.computed;
871 style_res->writing_mode.computed = style->writing_mode.computed;
872 }
874 if (texts == 0)
875 return QUERY_STYLE_NOTHING;
877 if (texts > 1) {
878 size /= texts;
879 letterspacing /= texts;
880 linespacing /= texts;
881 }
883 style_res->font_size.computed = size;
884 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
886 style_res->letter_spacing.normal = letterspacing_normal;
887 style_res->letter_spacing.computed = letterspacing;
889 style_res->line_height.normal = linespacing_normal;
890 style_res->line_height.computed = linespacing;
891 style_res->line_height.value = linespacing;
892 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
894 if (texts > 1) {
895 if (different) {
896 return QUERY_STYLE_MULTIPLE_AVERAGED;
897 } else {
898 return QUERY_STYLE_MULTIPLE_SAME;
899 }
900 } else {
901 return QUERY_STYLE_SINGLE;
902 }
903 }
905 /**
906 * Write to style_res the average font style of objects.
907 */
908 int
909 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
910 {
911 bool different = false;
912 bool set = false;
914 int texts = 0;
916 for (GSList const *i = objects; i != NULL; i = i->next) {
917 SPObject *obj = SP_OBJECT (i->data);
919 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
920 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
921 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
922 continue;
924 SPStyle *style = SP_OBJECT_STYLE (obj);
925 if (!style) continue;
927 texts ++;
929 if (set &&
930 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
931 different = true; // different styles
932 }
934 set = TRUE;
935 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
936 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
937 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
938 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
939 style_res->text_align.value = style_res->text_align.computed = style->text_align.computed;
940 }
942 if (texts == 0 || !set)
943 return QUERY_STYLE_NOTHING;
945 if (texts > 1) {
946 if (different) {
947 return QUERY_STYLE_MULTIPLE_DIFFERENT;
948 } else {
949 return QUERY_STYLE_MULTIPLE_SAME;
950 }
951 } else {
952 return QUERY_STYLE_SINGLE;
953 }
954 }
956 /**
957 * Write to style_res the average font family of objects.
958 */
959 int
960 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
961 {
962 bool different = false;
963 int texts = 0;
965 if (style_res->text->font_family.value) {
966 g_free(style_res->text->font_family.value);
967 style_res->text->font_family.value = NULL;
968 }
969 style_res->text->font_family.set = FALSE;
971 for (GSList const *i = objects; i != NULL; i = i->next) {
972 SPObject *obj = SP_OBJECT (i->data);
974 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
975 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
976 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
977 continue;
979 SPStyle *style = SP_OBJECT_STYLE (obj);
980 if (!style) continue;
982 texts ++;
984 if (style_res->text->font_family.value && style->text->font_family.value &&
985 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
986 different = true; // different fonts
987 }
989 if (style_res->text->font_family.value) {
990 g_free(style_res->text->font_family.value);
991 style_res->text->font_family.value = NULL;
992 }
994 style_res->text->font_family.set = TRUE;
995 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
996 }
998 if (texts == 0 || !style_res->text->font_family.set)
999 return QUERY_STYLE_NOTHING;
1001 if (texts > 1) {
1002 if (different) {
1003 return QUERY_STYLE_MULTIPLE_DIFFERENT;
1004 } else {
1005 return QUERY_STYLE_MULTIPLE_SAME;
1006 }
1007 } else {
1008 return QUERY_STYLE_SINGLE;
1009 }
1010 }
1012 /**
1013 * Query the given list of objects for the given property, write
1014 * the result to style, return appropriate flag.
1015 */
1016 int
1017 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1018 {
1019 if (property == QUERY_STYLE_PROPERTY_FILL) {
1020 return objects_query_fillstroke (list, style, true);
1021 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1022 return objects_query_fillstroke (list, style, false);
1024 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1025 return objects_query_strokewidth (list, style);
1026 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1027 return objects_query_miterlimit (list, style);
1028 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1029 return objects_query_strokecap (list, style);
1030 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1031 return objects_query_strokejoin (list, style);
1033 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1034 return objects_query_opacity (list, style);
1036 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1037 return objects_query_fontfamily (list, style);
1038 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1039 return objects_query_fontstyle (list, style);
1040 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1041 return objects_query_fontnumbers (list, style);
1042 }
1044 return QUERY_STYLE_NOTHING;
1045 }
1048 /**
1049 * Query the subselection (if any) or selection on the given desktop for the given property, write
1050 * the result to style, return appropriate flag.
1051 */
1052 int
1053 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1054 {
1055 int ret = desktop->_query_style_signal.emit(style, property);
1057 if (ret != QUERY_STYLE_NOTHING)
1058 return ret; // subselection returned a style, pass it on
1060 // otherwise, do querying and averaging over selection
1061 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1062 }
1064 /**
1065 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1066 * the properties returned QUERY_STYLE_NOTHING.
1067 */
1068 bool
1069 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1070 {
1071 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1072 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1073 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1074 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1075 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1076 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1077 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1078 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1079 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1080 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1082 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);
1083 }
1086 /*
1087 Local Variables:
1088 mode:c++
1089 c-file-style:"stroustrup"
1090 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1091 indent-tabs-mode:nil
1092 fill-column:99
1093 End:
1094 */
1095 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :