cf8f67442d59941d824090486ee57d7e41af7b75
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_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 css = sp_repr_css_attr_inherited(tool_repr, "style");
234 }
236 gchar const *property = css ? sp_repr_css_property(css, "opacity", "1.000") : 0;
238 if (desktop->current && property) { // if there is style and the property in it,
239 if ( !sp_svg_number_read_f(property, &value) ) {
240 value = 1.0; // things failed. set back to the default
241 }
242 }
244 if (css) {
245 sp_repr_css_attr_unref(css);
246 }
248 return value;
249 }
250 guint32
251 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
252 {
253 SPCSSAttr *css = NULL;
254 guint32 r = 0; // if there's no color, return black
255 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
256 css = sp_desktop_get_style(desktop, true);
257 } else {
258 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
259 css = sp_repr_css_attr_inherited(tool_repr, "style");
260 }
262 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
264 if (desktop->current && property) { // if there is style and the property in it,
265 if (strncmp(property, "url", 3)) { // and if it's not url,
266 // read it
267 r = sp_svg_read_color(property, r);
268 }
269 }
271 if (css) {
272 sp_repr_css_attr_unref(css);
273 }
275 return r | 0xff;
276 }
277 /**
278 * Apply the desktop's current style or the tool style to repr.
279 */
280 void
281 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
282 {
283 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
284 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
285 sp_repr_css_set(repr, css_current, "style");
286 } else {
287 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
288 if (tool_repr) {
289 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
290 sp_repr_css_set(repr, css, "style");
291 sp_repr_css_attr_unref(css);
292 }
293 }
294 if (css_current) {
295 sp_repr_css_attr_unref(css_current);
296 }
297 }
299 /**
300 * Returns the font size (in SVG pixels) of the text tool style (if text
301 * tool uses its own style) or desktop style (otherwise).
302 */
303 double
304 sp_desktop_get_font_size_tool(SPDesktop *desktop)
305 {
306 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
307 gchar const *style_str = NULL;
308 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
309 style_str = desktop_style;
310 } else {
311 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
312 if (tool_repr) {
313 style_str = tool_repr->attribute("style");
314 }
315 }
317 double ret = 12;
318 if (style_str) {
319 SPStyle *style = sp_style_new();
320 sp_style_merge_from_style_string(style, style_str);
321 ret = style->font_size.computed;
322 sp_style_unref(style);
323 }
324 return ret;
325 }
327 /** Determine average stroke width, simple method */
328 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
329 gdouble
330 stroke_average_width (GSList const *objects)
331 {
332 if (g_slist_length ((GSList *) objects) == 0)
333 return NR_HUGE;
335 gdouble avgwidth = 0.0;
336 bool notstroked = true;
337 int n_notstroked = 0;
339 for (GSList const *l = objects; l != NULL; l = l->next) {
340 if (!SP_IS_ITEM (l->data))
341 continue;
343 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
345 SPObject *object = SP_OBJECT(l->data);
347 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
348 ++n_notstroked; // do not count nonstroked objects
349 continue;
350 } else {
351 notstroked = false;
352 }
354 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
355 }
357 if (notstroked)
358 return NR_HUGE;
360 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
361 }
363 /**
364 * Write to style_res the average fill or stroke of list of objects, if applicable.
365 */
366 int
367 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
368 {
369 if (g_slist_length(objects) == 0) {
370 /* No objects, set empty */
371 return QUERY_STYLE_NOTHING;
372 }
374 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
375 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
376 paint_res->set = TRUE;
378 gfloat c[4];
379 c[0] = c[1] = c[2] = c[3] = 0.0;
380 gint num = 0;
382 gfloat prev[4];
383 prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
384 bool same_color = true;
386 for (GSList const *i = objects; i != NULL; i = i->next) {
387 SPObject *obj = SP_OBJECT (i->data);
388 SPStyle *style = SP_OBJECT_STYLE (obj);
389 if (!style) continue;
391 SPIPaint *paint = isfill? &style->fill : &style->stroke;
393 // We consider paint "effectively set" for anything within text hierarchy
394 SPObject *parent = SP_OBJECT_PARENT (obj);
395 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));
397 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
399 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
400 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
401 }
403 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
404 // both previous paint and this paint were a server, see if the servers are compatible
406 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
407 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
409 if (SP_IS_LINEARGRADIENT (server_res)) {
411 if (!SP_IS_LINEARGRADIENT(server))
412 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
414 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
415 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
416 if (vector_res != vector)
417 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
419 } else if (SP_IS_RADIALGRADIENT (server_res)) {
421 if (!SP_IS_RADIALGRADIENT(server))
422 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
424 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
425 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
426 if (vector_res != vector)
427 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
429 } else if (SP_IS_PATTERN (server_res)) {
431 if (!SP_IS_PATTERN(server))
432 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
434 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
435 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
436 if (pat_res != pat)
437 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
438 }
439 }
441 // 2. Sum color, copy server from paint to paint_res
443 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
445 gfloat d[3];
446 sp_color_get_rgb_floatv (&paint->value.color, d);
448 // Check if this color is the same as previous
449 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
450 prev[0] = d[0];
451 prev[1] = d[1];
452 prev[2] = d[2];
453 prev[3] = d[3];
454 } else {
455 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
456 same_color = false;
457 }
459 // average color
460 c[0] += d[0];
461 c[1] += d[1];
462 c[2] += d[2];
463 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
464 num ++;
465 }
467 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
468 if (isfill) {
469 SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
470 } else {
471 SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
472 }
473 }
474 paint_res->type = paint->type;
475 paint_res->set = paint_effectively_set;
476 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
477 }
479 // After all objects processed, divide the color if necessary and return
480 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
481 g_assert (num >= 1);
483 c[0] /= num;
484 c[1] /= num;
485 c[2] /= num;
486 c[3] /= num;
487 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
488 if (isfill) {
489 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
490 } else {
491 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
492 }
493 if (num > 1) {
494 if (same_color)
495 return QUERY_STYLE_MULTIPLE_SAME;
496 else
497 return QUERY_STYLE_MULTIPLE_AVERAGED;
498 } else {
499 return QUERY_STYLE_SINGLE;
500 }
501 }
503 // Not color
504 if (g_slist_length(objects) > 1) {
505 return QUERY_STYLE_MULTIPLE_SAME;
506 } else {
507 return QUERY_STYLE_SINGLE;
508 }
509 }
511 /**
512 * Write to style_res the average opacity of a list of objects.
513 */
514 int
515 objects_query_opacity (GSList *objects, SPStyle *style_res)
516 {
517 if (g_slist_length(objects) == 0) {
518 /* No objects, set empty */
519 return QUERY_STYLE_NOTHING;
520 }
522 gdouble opacity_sum = 0;
523 gdouble opacity_prev = -1;
524 bool same_opacity = true;
525 guint opacity_items = 0;
528 for (GSList const *i = objects; i != NULL; i = i->next) {
529 SPObject *obj = SP_OBJECT (i->data);
530 SPStyle *style = SP_OBJECT_STYLE (obj);
531 if (!style) continue;
533 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
534 opacity_sum += opacity;
535 if (opacity_prev != -1 && opacity != opacity_prev)
536 same_opacity = false;
537 opacity_prev = opacity;
538 opacity_items ++;
539 }
540 if (opacity_items > 1)
541 opacity_sum /= opacity_items;
543 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
545 if (opacity_items == 0) {
546 return QUERY_STYLE_NOTHING;
547 } else if (opacity_items == 1) {
548 return QUERY_STYLE_SINGLE;
549 } else {
550 if (same_opacity)
551 return QUERY_STYLE_MULTIPLE_SAME;
552 else
553 return QUERY_STYLE_MULTIPLE_AVERAGED;
554 }
555 }
557 /**
558 * Write to style_res the average stroke width of a list of objects.
559 */
560 int
561 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
562 {
563 if (g_slist_length(objects) == 0) {
564 /* No objects, set empty */
565 return QUERY_STYLE_NOTHING;
566 }
568 gdouble avgwidth = 0.0;
570 gdouble prev_sw = -1;
571 bool same_sw = true;
573 int n_stroked = 0;
575 for (GSList const *i = objects; i != NULL; i = i->next) {
576 SPObject *obj = SP_OBJECT (i->data);
577 if (!SP_IS_ITEM(obj)) continue;
578 SPStyle *style = SP_OBJECT_STYLE (obj);
579 if (!style) continue;
581 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
582 continue;
583 }
585 n_stroked ++;
587 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
588 double sw = style->stroke_width.computed * i2d.expansion();
590 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
591 same_sw = false;
592 prev_sw = sw;
594 avgwidth += sw;
595 }
597 if (n_stroked > 1)
598 avgwidth /= (n_stroked);
600 style_res->stroke_width.computed = avgwidth;
601 style_res->stroke_width.set = true;
603 if (n_stroked == 0) {
604 return QUERY_STYLE_NOTHING;
605 } else if (n_stroked == 1) {
606 return QUERY_STYLE_SINGLE;
607 } else {
608 if (same_sw)
609 return QUERY_STYLE_MULTIPLE_SAME;
610 else
611 return QUERY_STYLE_MULTIPLE_AVERAGED;
612 }
613 }
615 /**
616 * Write to style_res the average miter limit of a list of objects.
617 */
618 int
619 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
620 {
621 if (g_slist_length(objects) == 0) {
622 /* No objects, set empty */
623 return QUERY_STYLE_NOTHING;
624 }
626 gdouble avgml = 0.0;
627 int n_stroked = 0;
629 gdouble prev_ml = -1;
630 bool same_ml = true;
632 for (GSList const *i = objects; i != NULL; i = i->next) {
633 SPObject *obj = SP_OBJECT (i->data);
634 if (!SP_IS_ITEM(obj)) continue;
635 SPStyle *style = SP_OBJECT_STYLE (obj);
636 if (!style) continue;
638 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
639 continue;
640 }
642 n_stroked ++;
644 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
645 same_ml = false;
646 prev_ml = style->stroke_miterlimit.value;
648 avgml += style->stroke_miterlimit.value;
649 }
651 if (n_stroked > 1)
652 avgml /= (n_stroked);
654 style_res->stroke_miterlimit.value = avgml;
655 style_res->stroke_miterlimit.set = true;
657 if (n_stroked == 0) {
658 return QUERY_STYLE_NOTHING;
659 } else if (n_stroked == 1) {
660 return QUERY_STYLE_SINGLE;
661 } else {
662 if (same_ml)
663 return QUERY_STYLE_MULTIPLE_SAME;
664 else
665 return QUERY_STYLE_MULTIPLE_AVERAGED;
666 }
667 }
669 /**
670 * Write to style_res the stroke cap of a list of objects.
671 */
672 int
673 objects_query_strokecap (GSList *objects, SPStyle *style_res)
674 {
675 if (g_slist_length(objects) == 0) {
676 /* No objects, set empty */
677 return QUERY_STYLE_NOTHING;
678 }
680 int cap = -1;
681 gdouble prev_cap = -1;
682 bool same_cap = true;
683 int n_stroked = 0;
685 for (GSList const *i = objects; i != NULL; i = i->next) {
686 SPObject *obj = SP_OBJECT (i->data);
687 if (!SP_IS_ITEM(obj)) continue;
688 SPStyle *style = SP_OBJECT_STYLE (obj);
689 if (!style) continue;
691 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
692 continue;
693 }
695 n_stroked ++;
697 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
698 same_cap = false;
699 prev_cap = style->stroke_linecap.value;
701 cap = style->stroke_linecap.value;
702 }
704 style_res->stroke_linecap.value = cap;
705 style_res->stroke_linecap.set = true;
707 if (n_stroked == 0) {
708 return QUERY_STYLE_NOTHING;
709 } else if (n_stroked == 1) {
710 return QUERY_STYLE_SINGLE;
711 } else {
712 if (same_cap)
713 return QUERY_STYLE_MULTIPLE_SAME;
714 else
715 return QUERY_STYLE_MULTIPLE_DIFFERENT;
716 }
717 }
719 /**
720 * Write to style_res the stroke join of a list of objects.
721 */
722 int
723 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
724 {
725 if (g_slist_length(objects) == 0) {
726 /* No objects, set empty */
727 return QUERY_STYLE_NOTHING;
728 }
730 int join = -1;
731 gdouble prev_join = -1;
732 bool same_join = true;
733 int n_stroked = 0;
735 for (GSList const *i = objects; i != NULL; i = i->next) {
736 SPObject *obj = SP_OBJECT (i->data);
737 if (!SP_IS_ITEM(obj)) continue;
738 SPStyle *style = SP_OBJECT_STYLE (obj);
739 if (!style) continue;
741 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
742 continue;
743 }
745 n_stroked ++;
747 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
748 same_join = false;
749 prev_join = style->stroke_linejoin.value;
751 join = style->stroke_linejoin.value;
752 }
754 style_res->stroke_linejoin.value = join;
755 style_res->stroke_linejoin.set = true;
757 if (n_stroked == 0) {
758 return QUERY_STYLE_NOTHING;
759 } else if (n_stroked == 1) {
760 return QUERY_STYLE_SINGLE;
761 } else {
762 if (same_join)
763 return QUERY_STYLE_MULTIPLE_SAME;
764 else
765 return QUERY_STYLE_MULTIPLE_DIFFERENT;
766 }
767 }
769 /**
770 * Write to style_res the average font size and spacing of objects.
771 */
772 int
773 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
774 {
775 bool different = false;
777 double size = 0;
778 double letterspacing = 0;
779 double linespacing = 0;
780 bool linespacing_normal = false;
781 bool letterspacing_normal = false;
783 double size_prev = 0;
784 double letterspacing_prev = 0;
785 double linespacing_prev = 0;
787 /// \todo FIXME: add word spacing, kerns? rotates?
789 int texts = 0;
791 for (GSList const *i = objects; i != NULL; i = i->next) {
792 SPObject *obj = SP_OBJECT (i->data);
794 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
795 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
796 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
797 continue;
799 SPStyle *style = SP_OBJECT_STYLE (obj);
800 if (!style) continue;
802 texts ++;
803 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
805 if (style->letter_spacing.normal) {
806 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
807 letterspacing_normal = true;
808 } else {
809 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
810 letterspacing_normal = false;
811 }
813 double linespacing_current;
814 if (style->line_height.normal) {
815 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
816 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
817 linespacing_normal = true;
818 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
819 linespacing_current = style->line_height.value;
820 linespacing_normal = false;
821 } else { // we need % here
822 linespacing_current = style->line_height.computed / style->font_size.computed;
823 linespacing_normal = false;
824 }
825 linespacing += linespacing_current;
827 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
828 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
829 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
830 different = true;
831 }
833 size_prev = style->font_size.computed;
834 letterspacing_prev = style->letter_spacing.computed;
835 linespacing_prev = linespacing_current;
837 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
838 style_res->text_anchor.computed = style->text_anchor.computed;
839 style_res->writing_mode.computed = style->writing_mode.computed;
840 }
842 if (texts == 0)
843 return QUERY_STYLE_NOTHING;
845 if (texts > 1) {
846 size /= texts;
847 letterspacing /= texts;
848 linespacing /= texts;
849 }
851 style_res->font_size.computed = size;
852 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
854 style_res->letter_spacing.normal = letterspacing_normal;
855 style_res->letter_spacing.computed = letterspacing;
857 style_res->line_height.normal = linespacing_normal;
858 style_res->line_height.computed = linespacing;
859 style_res->line_height.value = linespacing;
860 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
862 if (texts > 1) {
863 if (different) {
864 return QUERY_STYLE_MULTIPLE_AVERAGED;
865 } else {
866 return QUERY_STYLE_MULTIPLE_SAME;
867 }
868 } else {
869 return QUERY_STYLE_SINGLE;
870 }
871 }
873 /**
874 * Write to style_res the average font style of objects.
875 */
876 int
877 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
878 {
879 bool different = false;
880 bool set = false;
882 int texts = 0;
884 for (GSList const *i = objects; i != NULL; i = i->next) {
885 SPObject *obj = SP_OBJECT (i->data);
887 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
888 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
889 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
890 continue;
892 SPStyle *style = SP_OBJECT_STYLE (obj);
893 if (!style) continue;
895 texts ++;
897 if (set &&
898 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
899 different = true; // different styles
900 }
902 set = TRUE;
903 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
904 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
905 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
906 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
907 }
909 if (texts == 0 || !set)
910 return QUERY_STYLE_NOTHING;
912 if (texts > 1) {
913 if (different) {
914 return QUERY_STYLE_MULTIPLE_DIFFERENT;
915 } else {
916 return QUERY_STYLE_MULTIPLE_SAME;
917 }
918 } else {
919 return QUERY_STYLE_SINGLE;
920 }
921 }
923 /**
924 * Write to style_res the average font family of objects.
925 */
926 int
927 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
928 {
929 bool different = false;
930 int texts = 0;
932 if (style_res->text->font_family.value) {
933 g_free(style_res->text->font_family.value);
934 style_res->text->font_family.value = NULL;
935 }
936 style_res->text->font_family.set = FALSE;
938 for (GSList const *i = objects; i != NULL; i = i->next) {
939 SPObject *obj = SP_OBJECT (i->data);
941 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
942 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
943 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
944 continue;
946 SPStyle *style = SP_OBJECT_STYLE (obj);
947 if (!style) continue;
949 texts ++;
951 if (style_res->text->font_family.value && style->text->font_family.value &&
952 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
953 different = true; // different fonts
954 }
956 if (style_res->text->font_family.value) {
957 g_free(style_res->text->font_family.value);
958 style_res->text->font_family.value = NULL;
959 }
961 style_res->text->font_family.set = TRUE;
962 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
963 }
965 if (texts == 0 || !style_res->text->font_family.set)
966 return QUERY_STYLE_NOTHING;
968 if (texts > 1) {
969 if (different) {
970 return QUERY_STYLE_MULTIPLE_DIFFERENT;
971 } else {
972 return QUERY_STYLE_MULTIPLE_SAME;
973 }
974 } else {
975 return QUERY_STYLE_SINGLE;
976 }
977 }
979 /**
980 * Query the given list of objects for the given property, write
981 * the result to style, return appropriate flag.
982 */
983 int
984 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
985 {
986 if (property == QUERY_STYLE_PROPERTY_FILL) {
987 return objects_query_fillstroke (list, style, true);
988 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
989 return objects_query_fillstroke (list, style, false);
991 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
992 return objects_query_strokewidth (list, style);
993 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
994 return objects_query_miterlimit (list, style);
995 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
996 return objects_query_strokecap (list, style);
997 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
998 return objects_query_strokejoin (list, style);
1000 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1001 return objects_query_opacity (list, style);
1003 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1004 return objects_query_fontfamily (list, style);
1005 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1006 return objects_query_fontstyle (list, style);
1007 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1008 return objects_query_fontnumbers (list, style);
1009 }
1011 return QUERY_STYLE_NOTHING;
1012 }
1015 /**
1016 * Query the subselection (if any) or selection on the given desktop for the given property, write
1017 * the result to style, return appropriate flag.
1018 */
1019 int
1020 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1021 {
1022 int ret = desktop->_query_style_signal.emit(style, property);
1024 if (ret != QUERY_STYLE_NOTHING)
1025 return ret; // subselection returned a style, pass it on
1027 // otherwise, do querying and averaging over selection
1028 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1029 }
1031 /**
1032 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1033 * the properties returned QUERY_STYLE_NOTHING.
1034 */
1035 bool
1036 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1037 {
1038 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1039 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1040 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1041 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1042 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1043 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1044 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1045 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1046 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1047 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1049 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);
1050 }
1053 /*
1054 Local Variables:
1055 mode:c++
1056 c-file-style:"stroustrup"
1057 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1058 indent-tabs-mode:nil
1059 fill-column:99
1060 End:
1061 */
1062 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :