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 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 double
251 sp_desktop_get_opacity_tool(SPDesktop *desktop, char const *tool, bool is_fill)
252 {
253 SPCSSAttr *css = NULL;
254 gfloat value = 1.0; // default if nothing else found
255 if (prefs_get_double_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 = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
264 if (desktop->current && property) { // if there is style and the property in it,
265 if ( !sp_svg_number_read_f(property, &value) ) {
266 value = 1.0; // things failed. set back to the default
267 }
268 }
270 if (css) {
271 sp_repr_css_attr_unref(css);
272 }
274 return value;
275 }
276 guint32
277 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
278 {
279 SPCSSAttr *css = NULL;
280 guint32 r = 0; // if there's no color, return black
281 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
282 css = sp_desktop_get_style(desktop, true);
283 } else {
284 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
285 css = sp_repr_css_attr_inherited(tool_repr, "style");
286 }
288 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
290 if (desktop->current && property) { // if there is style and the property in it,
291 if (strncmp(property, "url", 3)) { // and if it's not url,
292 // read it
293 r = sp_svg_read_color(property, r);
294 }
295 }
297 if (css) {
298 sp_repr_css_attr_unref(css);
299 }
301 return r | 0xff;
302 }
303 /**
304 * Apply the desktop's current style or the tool style to repr.
305 */
306 void
307 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
308 {
309 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
310 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
311 sp_repr_css_set(repr, css_current, "style");
312 } else {
313 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
314 if (tool_repr) {
315 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
316 sp_repr_css_set(repr, css, "style");
317 sp_repr_css_attr_unref(css);
318 }
319 }
320 if (css_current) {
321 sp_repr_css_attr_unref(css_current);
322 }
323 }
325 /**
326 * Returns the font size (in SVG pixels) of the text tool style (if text
327 * tool uses its own style) or desktop style (otherwise).
328 */
329 double
330 sp_desktop_get_font_size_tool(SPDesktop *desktop)
331 {
332 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
333 gchar const *style_str = NULL;
334 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
335 style_str = desktop_style;
336 } else {
337 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
338 if (tool_repr) {
339 style_str = tool_repr->attribute("style");
340 }
341 }
343 double ret = 12;
344 if (style_str) {
345 SPStyle *style = sp_style_new();
346 sp_style_merge_from_style_string(style, style_str);
347 ret = style->font_size.computed;
348 sp_style_unref(style);
349 }
350 return ret;
351 }
353 /** Determine average stroke width, simple method */
354 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
355 gdouble
356 stroke_average_width (GSList const *objects)
357 {
358 if (g_slist_length ((GSList *) objects) == 0)
359 return NR_HUGE;
361 gdouble avgwidth = 0.0;
362 bool notstroked = true;
363 int n_notstroked = 0;
365 for (GSList const *l = objects; l != NULL; l = l->next) {
366 if (!SP_IS_ITEM (l->data))
367 continue;
369 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
371 SPObject *object = SP_OBJECT(l->data);
373 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
374 ++n_notstroked; // do not count nonstroked objects
375 continue;
376 } else {
377 notstroked = false;
378 }
380 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
381 }
383 if (notstroked)
384 return NR_HUGE;
386 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
387 }
389 /**
390 * Write to style_res the average fill or stroke of list of objects, if applicable.
391 */
392 int
393 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
394 {
395 if (g_slist_length(objects) == 0) {
396 /* No objects, set empty */
397 return QUERY_STYLE_NOTHING;
398 }
400 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
401 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
402 paint_res->set = TRUE;
404 gfloat c[4];
405 c[0] = c[1] = c[2] = c[3] = 0.0;
406 gint num = 0;
408 gfloat prev[4];
409 prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
410 bool same_color = true;
412 for (GSList const *i = objects; i != NULL; i = i->next) {
413 SPObject *obj = SP_OBJECT (i->data);
414 SPStyle *style = SP_OBJECT_STYLE (obj);
415 if (!style) continue;
417 SPIPaint *paint = isfill? &style->fill : &style->stroke;
419 // We consider paint "effectively set" for anything within text hierarchy
420 SPObject *parent = SP_OBJECT_PARENT (obj);
421 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));
423 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
425 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
426 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
427 }
429 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
430 // both previous paint and this paint were a server, see if the servers are compatible
432 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
433 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
435 if (SP_IS_LINEARGRADIENT (server_res)) {
437 if (!SP_IS_LINEARGRADIENT(server))
438 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
440 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
441 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
442 if (vector_res != vector)
443 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
445 } else if (SP_IS_RADIALGRADIENT (server_res)) {
447 if (!SP_IS_RADIALGRADIENT(server))
448 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
450 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
451 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
452 if (vector_res != vector)
453 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
455 } else if (SP_IS_PATTERN (server_res)) {
457 if (!SP_IS_PATTERN(server))
458 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
460 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
461 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
462 if (pat_res != pat)
463 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
464 }
465 }
467 // 2. Sum color, copy server from paint to paint_res
469 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
471 gfloat d[3];
472 sp_color_get_rgb_floatv (&paint->value.color, d);
474 // Check if this color is the same as previous
475 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
476 prev[0] = d[0];
477 prev[1] = d[1];
478 prev[2] = d[2];
479 prev[3] = d[3];
480 } else {
481 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
482 same_color = false;
483 }
485 // average color
486 c[0] += d[0];
487 c[1] += d[1];
488 c[2] += d[2];
489 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
490 num ++;
491 }
493 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
494 if (isfill) {
495 SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
496 } else {
497 SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
498 }
499 }
500 paint_res->type = paint->type;
501 paint_res->set = paint_effectively_set;
502 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
503 }
505 // After all objects processed, divide the color if necessary and return
506 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
507 g_assert (num >= 1);
509 c[0] /= num;
510 c[1] /= num;
511 c[2] /= num;
512 c[3] /= num;
513 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
514 if (isfill) {
515 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
516 } else {
517 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
518 }
519 if (num > 1) {
520 if (same_color)
521 return QUERY_STYLE_MULTIPLE_SAME;
522 else
523 return QUERY_STYLE_MULTIPLE_AVERAGED;
524 } else {
525 return QUERY_STYLE_SINGLE;
526 }
527 }
529 // Not color
530 if (g_slist_length(objects) > 1) {
531 return QUERY_STYLE_MULTIPLE_SAME;
532 } else {
533 return QUERY_STYLE_SINGLE;
534 }
535 }
537 /**
538 * Write to style_res the average opacity of a list of objects.
539 */
540 int
541 objects_query_opacity (GSList *objects, SPStyle *style_res)
542 {
543 if (g_slist_length(objects) == 0) {
544 /* No objects, set empty */
545 return QUERY_STYLE_NOTHING;
546 }
548 gdouble opacity_sum = 0;
549 gdouble opacity_prev = -1;
550 bool same_opacity = true;
551 guint opacity_items = 0;
554 for (GSList const *i = objects; i != NULL; i = i->next) {
555 SPObject *obj = SP_OBJECT (i->data);
556 SPStyle *style = SP_OBJECT_STYLE (obj);
557 if (!style) continue;
559 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
560 opacity_sum += opacity;
561 if (opacity_prev != -1 && opacity != opacity_prev)
562 same_opacity = false;
563 opacity_prev = opacity;
564 opacity_items ++;
565 }
566 if (opacity_items > 1)
567 opacity_sum /= opacity_items;
569 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
571 if (opacity_items == 0) {
572 return QUERY_STYLE_NOTHING;
573 } else if (opacity_items == 1) {
574 return QUERY_STYLE_SINGLE;
575 } else {
576 if (same_opacity)
577 return QUERY_STYLE_MULTIPLE_SAME;
578 else
579 return QUERY_STYLE_MULTIPLE_AVERAGED;
580 }
581 }
583 /**
584 * Write to style_res the average stroke width of a list of objects.
585 */
586 int
587 objects_query_strokewidth (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 avgwidth = 0.0;
596 gdouble prev_sw = -1;
597 bool same_sw = true;
599 int n_stroked = 0;
601 for (GSList const *i = objects; i != NULL; i = i->next) {
602 SPObject *obj = SP_OBJECT (i->data);
603 if (!SP_IS_ITEM(obj)) continue;
604 SPStyle *style = SP_OBJECT_STYLE (obj);
605 if (!style) continue;
607 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
608 continue;
609 }
611 n_stroked ++;
613 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
614 double sw = style->stroke_width.computed * i2d.expansion();
616 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
617 same_sw = false;
618 prev_sw = sw;
620 avgwidth += sw;
621 }
623 if (n_stroked > 1)
624 avgwidth /= (n_stroked);
626 style_res->stroke_width.computed = avgwidth;
627 style_res->stroke_width.set = true;
629 if (n_stroked == 0) {
630 return QUERY_STYLE_NOTHING;
631 } else if (n_stroked == 1) {
632 return QUERY_STYLE_SINGLE;
633 } else {
634 if (same_sw)
635 return QUERY_STYLE_MULTIPLE_SAME;
636 else
637 return QUERY_STYLE_MULTIPLE_AVERAGED;
638 }
639 }
641 /**
642 * Write to style_res the average miter limit of a list of objects.
643 */
644 int
645 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
646 {
647 if (g_slist_length(objects) == 0) {
648 /* No objects, set empty */
649 return QUERY_STYLE_NOTHING;
650 }
652 gdouble avgml = 0.0;
653 int n_stroked = 0;
655 gdouble prev_ml = -1;
656 bool same_ml = true;
658 for (GSList const *i = objects; i != NULL; i = i->next) {
659 SPObject *obj = SP_OBJECT (i->data);
660 if (!SP_IS_ITEM(obj)) continue;
661 SPStyle *style = SP_OBJECT_STYLE (obj);
662 if (!style) continue;
664 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
665 continue;
666 }
668 n_stroked ++;
670 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
671 same_ml = false;
672 prev_ml = style->stroke_miterlimit.value;
674 avgml += style->stroke_miterlimit.value;
675 }
677 if (n_stroked > 1)
678 avgml /= (n_stroked);
680 style_res->stroke_miterlimit.value = avgml;
681 style_res->stroke_miterlimit.set = true;
683 if (n_stroked == 0) {
684 return QUERY_STYLE_NOTHING;
685 } else if (n_stroked == 1) {
686 return QUERY_STYLE_SINGLE;
687 } else {
688 if (same_ml)
689 return QUERY_STYLE_MULTIPLE_SAME;
690 else
691 return QUERY_STYLE_MULTIPLE_AVERAGED;
692 }
693 }
695 /**
696 * Write to style_res the stroke cap of a list of objects.
697 */
698 int
699 objects_query_strokecap (GSList *objects, SPStyle *style_res)
700 {
701 if (g_slist_length(objects) == 0) {
702 /* No objects, set empty */
703 return QUERY_STYLE_NOTHING;
704 }
706 int cap = -1;
707 gdouble prev_cap = -1;
708 bool same_cap = true;
709 int n_stroked = 0;
711 for (GSList const *i = objects; i != NULL; i = i->next) {
712 SPObject *obj = SP_OBJECT (i->data);
713 if (!SP_IS_ITEM(obj)) continue;
714 SPStyle *style = SP_OBJECT_STYLE (obj);
715 if (!style) continue;
717 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
718 continue;
719 }
721 n_stroked ++;
723 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
724 same_cap = false;
725 prev_cap = style->stroke_linecap.value;
727 cap = style->stroke_linecap.value;
728 }
730 style_res->stroke_linecap.value = cap;
731 style_res->stroke_linecap.set = true;
733 if (n_stroked == 0) {
734 return QUERY_STYLE_NOTHING;
735 } else if (n_stroked == 1) {
736 return QUERY_STYLE_SINGLE;
737 } else {
738 if (same_cap)
739 return QUERY_STYLE_MULTIPLE_SAME;
740 else
741 return QUERY_STYLE_MULTIPLE_DIFFERENT;
742 }
743 }
745 /**
746 * Write to style_res the stroke join of a list of objects.
747 */
748 int
749 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
750 {
751 if (g_slist_length(objects) == 0) {
752 /* No objects, set empty */
753 return QUERY_STYLE_NOTHING;
754 }
756 int join = -1;
757 gdouble prev_join = -1;
758 bool same_join = true;
759 int n_stroked = 0;
761 for (GSList const *i = objects; i != NULL; i = i->next) {
762 SPObject *obj = SP_OBJECT (i->data);
763 if (!SP_IS_ITEM(obj)) continue;
764 SPStyle *style = SP_OBJECT_STYLE (obj);
765 if (!style) continue;
767 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
768 continue;
769 }
771 n_stroked ++;
773 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
774 same_join = false;
775 prev_join = style->stroke_linejoin.value;
777 join = style->stroke_linejoin.value;
778 }
780 style_res->stroke_linejoin.value = join;
781 style_res->stroke_linejoin.set = true;
783 if (n_stroked == 0) {
784 return QUERY_STYLE_NOTHING;
785 } else if (n_stroked == 1) {
786 return QUERY_STYLE_SINGLE;
787 } else {
788 if (same_join)
789 return QUERY_STYLE_MULTIPLE_SAME;
790 else
791 return QUERY_STYLE_MULTIPLE_DIFFERENT;
792 }
793 }
795 /**
796 * Write to style_res the average font size and spacing of objects.
797 */
798 int
799 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
800 {
801 bool different = false;
803 double size = 0;
804 double letterspacing = 0;
805 double linespacing = 0;
806 bool linespacing_normal = false;
807 bool letterspacing_normal = false;
809 double size_prev = 0;
810 double letterspacing_prev = 0;
811 double linespacing_prev = 0;
813 /// \todo FIXME: add word spacing, kerns? rotates?
815 int texts = 0;
817 for (GSList const *i = objects; i != NULL; i = i->next) {
818 SPObject *obj = SP_OBJECT (i->data);
820 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
821 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
822 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
823 continue;
825 SPStyle *style = SP_OBJECT_STYLE (obj);
826 if (!style) continue;
828 texts ++;
829 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
831 if (style->letter_spacing.normal) {
832 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
833 letterspacing_normal = true;
834 } else {
835 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
836 letterspacing_normal = false;
837 }
839 double linespacing_current;
840 if (style->line_height.normal) {
841 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
842 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
843 linespacing_normal = true;
844 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
845 linespacing_current = style->line_height.value;
846 linespacing_normal = false;
847 } else { // we need % here
848 linespacing_current = style->line_height.computed / style->font_size.computed;
849 linespacing_normal = false;
850 }
851 linespacing += linespacing_current;
853 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
854 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
855 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
856 different = true;
857 }
859 size_prev = style->font_size.computed;
860 letterspacing_prev = style->letter_spacing.computed;
861 linespacing_prev = linespacing_current;
863 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
864 style_res->text_anchor.computed = style->text_anchor.computed;
865 style_res->writing_mode.computed = style->writing_mode.computed;
866 }
868 if (texts == 0)
869 return QUERY_STYLE_NOTHING;
871 if (texts > 1) {
872 size /= texts;
873 letterspacing /= texts;
874 linespacing /= texts;
875 }
877 style_res->font_size.computed = size;
878 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
880 style_res->letter_spacing.normal = letterspacing_normal;
881 style_res->letter_spacing.computed = letterspacing;
883 style_res->line_height.normal = linespacing_normal;
884 style_res->line_height.computed = linespacing;
885 style_res->line_height.value = linespacing;
886 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
888 if (texts > 1) {
889 if (different) {
890 return QUERY_STYLE_MULTIPLE_AVERAGED;
891 } else {
892 return QUERY_STYLE_MULTIPLE_SAME;
893 }
894 } else {
895 return QUERY_STYLE_SINGLE;
896 }
897 }
899 /**
900 * Write to style_res the average font style of objects.
901 */
902 int
903 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
904 {
905 bool different = false;
906 bool set = false;
908 int texts = 0;
910 for (GSList const *i = objects; i != NULL; i = i->next) {
911 SPObject *obj = SP_OBJECT (i->data);
913 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
914 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
915 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
916 continue;
918 SPStyle *style = SP_OBJECT_STYLE (obj);
919 if (!style) continue;
921 texts ++;
923 if (set &&
924 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
925 different = true; // different styles
926 }
928 set = TRUE;
929 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
930 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
931 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
932 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
933 }
935 if (texts == 0 || !set)
936 return QUERY_STYLE_NOTHING;
938 if (texts > 1) {
939 if (different) {
940 return QUERY_STYLE_MULTIPLE_DIFFERENT;
941 } else {
942 return QUERY_STYLE_MULTIPLE_SAME;
943 }
944 } else {
945 return QUERY_STYLE_SINGLE;
946 }
947 }
949 /**
950 * Write to style_res the average font family of objects.
951 */
952 int
953 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
954 {
955 bool different = false;
956 int texts = 0;
958 if (style_res->text->font_family.value) {
959 g_free(style_res->text->font_family.value);
960 style_res->text->font_family.value = NULL;
961 }
962 style_res->text->font_family.set = FALSE;
964 for (GSList const *i = objects; i != NULL; i = i->next) {
965 SPObject *obj = SP_OBJECT (i->data);
967 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
968 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
969 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
970 continue;
972 SPStyle *style = SP_OBJECT_STYLE (obj);
973 if (!style) continue;
975 texts ++;
977 if (style_res->text->font_family.value && style->text->font_family.value &&
978 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
979 different = true; // different fonts
980 }
982 if (style_res->text->font_family.value) {
983 g_free(style_res->text->font_family.value);
984 style_res->text->font_family.value = NULL;
985 }
987 style_res->text->font_family.set = TRUE;
988 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
989 }
991 if (texts == 0 || !style_res->text->font_family.set)
992 return QUERY_STYLE_NOTHING;
994 if (texts > 1) {
995 if (different) {
996 return QUERY_STYLE_MULTIPLE_DIFFERENT;
997 } else {
998 return QUERY_STYLE_MULTIPLE_SAME;
999 }
1000 } else {
1001 return QUERY_STYLE_SINGLE;
1002 }
1003 }
1005 /**
1006 * Query the given list of objects for the given property, write
1007 * the result to style, return appropriate flag.
1008 */
1009 int
1010 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
1011 {
1012 if (property == QUERY_STYLE_PROPERTY_FILL) {
1013 return objects_query_fillstroke (list, style, true);
1014 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
1015 return objects_query_fillstroke (list, style, false);
1017 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
1018 return objects_query_strokewidth (list, style);
1019 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
1020 return objects_query_miterlimit (list, style);
1021 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
1022 return objects_query_strokecap (list, style);
1023 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
1024 return objects_query_strokejoin (list, style);
1026 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
1027 return objects_query_opacity (list, style);
1029 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
1030 return objects_query_fontfamily (list, style);
1031 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
1032 return objects_query_fontstyle (list, style);
1033 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
1034 return objects_query_fontnumbers (list, style);
1035 }
1037 return QUERY_STYLE_NOTHING;
1038 }
1041 /**
1042 * Query the subselection (if any) or selection on the given desktop for the given property, write
1043 * the result to style, return appropriate flag.
1044 */
1045 int
1046 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
1047 {
1048 int ret = desktop->_query_style_signal.emit(style, property);
1050 if (ret != QUERY_STYLE_NOTHING)
1051 return ret; // subselection returned a style, pass it on
1053 // otherwise, do querying and averaging over selection
1054 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
1055 }
1057 /**
1058 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
1059 * the properties returned QUERY_STYLE_NOTHING.
1060 */
1061 bool
1062 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1063 {
1064 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1065 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1066 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1067 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1068 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1069 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1070 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1071 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1072 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1073 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1075 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);
1076 }
1079 /*
1080 Local Variables:
1081 mode:c++
1082 c-file-style:"stroustrup"
1083 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1084 indent-tabs-mode:nil
1085 fill-column:99
1086 End:
1087 */
1088 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :