58209aaa3c70d4eb41b440535fd5ae8ec2f7ed9b
1 #define __SP_DESKTOP_STYLE_C__
3 /** \file
4 * Desktop style management
5 *
6 * Authors:
7 * bulia byak
8 *
9 * Copyright (C) 2004 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "desktop.h"
15 #include "color-rgba.h"
16 #include "svg/css-ostringstream.h"
17 #include "svg/svg-color.h"
18 #include "selection.h"
19 #include "sp-tspan.h"
20 #include "sp-textpath.h"
21 #include "inkscape.h"
22 #include "style.h"
23 #include "prefs-utils.h"
24 #include "sp-use.h"
25 #include "sp-flowtext.h"
26 #include "sp-flowregion.h"
27 #include "sp-flowdiv.h"
28 #include "sp-linear-gradient.h"
29 #include "sp-radial-gradient.h"
30 #include "sp-pattern.h"
31 #include "xml/repr.h"
32 #include "libnrtype/font-style-to-pos.h"
34 #include "desktop-style.h"
36 /**
37 * Set color on selection on desktop.
38 */
39 void
40 sp_desktop_set_color(SPDesktop *desktop, ColorRGBA const &color, bool is_relative, bool fill)
41 {
42 /// \todo relative color setting
43 if (is_relative) {
44 g_warning("FIXME: relative color setting not yet implemented");
45 return;
46 }
48 guint32 rgba = SP_RGBA32_F_COMPOSE(color[0], color[1], color[2], color[3]);
49 gchar b[64];
50 sp_svg_write_color(b, 64, rgba);
51 SPCSSAttr *css = sp_repr_css_attr_new();
52 if (fill) {
53 sp_repr_css_set_property(css, "fill", b);
54 Inkscape::CSSOStringStream osalpha;
55 osalpha << color[3];
56 sp_repr_css_set_property(css, "fill-opacity", osalpha.str().c_str());
57 } else {
58 sp_repr_css_set_property(css, "stroke", b);
59 Inkscape::CSSOStringStream osalpha;
60 osalpha << color[3];
61 sp_repr_css_set_property(css, "stroke-opacity", osalpha.str().c_str());
62 }
64 sp_desktop_set_style(desktop, css);
66 sp_repr_css_attr_unref(css);
67 }
69 /**
70 * Apply style on object and children, recursively.
71 */
72 void
73 sp_desktop_apply_css_recursive(SPObject *o, SPCSSAttr *css, bool skip_lines)
74 {
75 // non-items should not have style
76 if (!SP_IS_ITEM(o))
77 return;
79 // 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
80 // but must always inherit from the parent text. Same for textPath.
81 // However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
83 // 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
84 // it, be it clone or not; it's just styleless shape (because that's how Inkscape does
85 // flowtext).
87 if (!(skip_lines
88 && ((SP_IS_TSPAN(o) && SP_TSPAN(o)->role == SP_TSPAN_ROLE_LINE)
89 || SP_IS_FLOWDIV(o)
90 || SP_IS_FLOWPARA(o)
91 || SP_IS_TEXTPATH(o))
92 && !SP_OBJECT_REPR(o)->attribute("style"))
93 &&
94 !(SP_IS_FLOWREGION(o) ||
95 SP_IS_FLOWREGIONEXCLUDE(o) ||
96 (SP_IS_USE(o) &&
97 SP_OBJECT_PARENT(o) &&
98 (SP_IS_FLOWREGION(SP_OBJECT_PARENT(o)) ||
99 SP_IS_FLOWREGIONEXCLUDE(SP_OBJECT_PARENT(o))
100 )
101 )
102 )
103 ) {
105 SPCSSAttr *css_set = sp_repr_css_attr_new();
106 sp_repr_css_merge(css_set, css);
108 // Scale the style by the inverse of the accumulated parent transform in the paste context.
109 {
110 NR::Matrix const local(sp_item_i2doc_affine(SP_ITEM(o)));
111 double const ex(NR::expansion(local));
112 if ( ( ex != 0. )
113 && ( ex != 1. ) ) {
114 sp_css_attr_scale(css_set, 1/ex);
115 }
116 }
118 sp_repr_css_change(SP_OBJECT_REPR(o), css_set, "style");
120 sp_repr_css_attr_unref(css_set);
121 }
123 // setting style on child of clone spills into the clone original (via shared repr), don't do it!
124 if (SP_IS_USE(o))
125 return;
127 for (SPObject *child = sp_object_first_child(SP_OBJECT(o)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
128 if (sp_repr_css_property(css, "opacity", NULL) != NULL) {
129 // Unset properties which are accumulating and thus should not be set recursively.
130 // 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.
131 SPCSSAttr *css_recurse = sp_repr_css_attr_new();
132 sp_repr_css_merge(css_recurse, css);
133 sp_repr_css_set_property(css_recurse, "opacity", NULL);
134 sp_desktop_apply_css_recursive(child, css_recurse, skip_lines);
135 sp_repr_css_attr_unref(css_recurse);
136 } else {
137 sp_desktop_apply_css_recursive(child, css, skip_lines);
138 }
139 }
140 }
142 /**
143 * Apply style on selection on desktop.
144 */
145 void
146 sp_desktop_set_style(SPDesktop *desktop, SPCSSAttr *css, bool change, bool write_current)
147 {
148 if (write_current) {
149 // 1. Set internal value
150 sp_repr_css_merge(desktop->current, css);
152 // 1a. Write to prefs; make a copy and unset any URIs first
153 SPCSSAttr *css_write = sp_repr_css_attr_new();
154 sp_repr_css_merge(css_write, css);
155 sp_css_attr_unset_uris(css_write);
156 sp_repr_css_change(inkscape_get_repr(INKSCAPE, "desktop"), css_write, "style");
157 sp_repr_css_attr_unref(css_write);
158 }
160 if (!change)
161 return;
163 // 2. Emit signal
164 bool intercepted = desktop->_set_style_signal.emit(css);
166 /** \todo
167 * FIXME: in set_style, compensate pattern and gradient fills, stroke width,
168 * rect corners, font size for the object's own transform so that pasting
169 * fills does not depend on preserve/optimize.
170 */
172 // 3. If nobody has intercepted the signal, apply the style to the selection
173 if (!intercepted) {
174 for (GSList const *i = desktop->selection->itemList(); i != NULL; i = i->next) {
175 /// \todo if the style is text-only, apply only to texts?
176 sp_desktop_apply_css_recursive(SP_OBJECT(i->data), css, true);
177 }
178 }
179 }
181 /**
182 * Return the desktop's current style.
183 */
184 SPCSSAttr *
185 sp_desktop_get_style(SPDesktop *desktop, bool with_text)
186 {
187 SPCSSAttr *css = sp_repr_css_attr_new();
188 sp_repr_css_merge(css, desktop->current);
189 if (!css->attributeList()) {
190 sp_repr_css_attr_unref(css);
191 return NULL;
192 } else {
193 if (!with_text) {
194 css = sp_css_attr_unset_text(css);
195 }
196 return css;
197 }
198 }
200 /**
201 * Return the desktop's current color.
202 */
203 guint32
204 sp_desktop_get_color(SPDesktop *desktop, bool is_fill)
205 {
206 guint32 r = 0; // if there's no color, return black
207 gchar const *property = sp_repr_css_property(desktop->current,
208 is_fill ? "fill" : "stroke",
209 "#000");
211 if (desktop->current && property) { // if there is style and the property in it,
212 if (strncmp(property, "url", 3)) { // and if it's not url,
213 // read it
214 r = sp_svg_read_color(property, r);
215 }
216 }
218 return r;
219 }
221 guint32
222 sp_desktop_get_color_tool(SPDesktop *desktop, char const *tool, bool is_fill)
223 {
224 SPCSSAttr *css = NULL;
225 guint32 r = 0; // if there's no color, return black
226 if (prefs_get_int_attribute(tool, "usecurrent", 0) != 0) {
227 css = sp_desktop_get_style(desktop, is_fill);
228 } else {
229 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
230 css = sp_repr_css_attr_inherited(tool_repr, "style");
231 }
233 gchar const *property = sp_repr_css_property(css, is_fill ? "fill" : "stroke", "#000");
235 if (desktop->current && property) { // if there is style and the property in it,
236 if (strncmp(property, "url", 3)) { // and if it's not url,
237 // read it
238 r = sp_svg_read_color(property, r);
239 }
240 }
241 return r | 0xff;
242 }
243 /**
244 * Apply the desktop's current style or the tool style to repr.
245 */
246 void
247 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
248 {
249 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
250 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
251 sp_repr_css_set(repr, css_current, "style");
252 } else {
253 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
254 if (tool_repr) {
255 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
256 sp_repr_css_set(repr, css, "style");
257 sp_repr_css_attr_unref(css);
258 }
259 }
260 if (css_current) {sp_repr_css_attr_unref(css_current);
261 }
262 }
264 /**
265 * Returns the font size (in SVG pixels) of the text tool style (if text
266 * tool uses its own style) or desktop style (otherwise).
267 */
268 double
269 sp_desktop_get_font_size_tool(SPDesktop *desktop)
270 {
271 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
272 gchar const *style_str = NULL;
273 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
274 style_str = desktop_style;
275 } else {
276 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
277 if (tool_repr) {
278 style_str = tool_repr->attribute("style");
279 }
280 }
282 double ret = 12;
283 if (style_str) {
284 SPStyle *style = sp_style_new();
285 sp_style_merge_from_style_string(style, style_str);
286 ret = style->font_size.computed;
287 sp_style_unref(style);
288 }
289 return ret;
290 }
292 /** Determine average stroke width, simple method */
293 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
294 gdouble
295 stroke_average_width (GSList const *objects)
296 {
297 if (g_slist_length ((GSList *) objects) == 0)
298 return NR_HUGE;
300 gdouble avgwidth = 0.0;
301 bool notstroked = true;
302 int n_notstroked = 0;
304 for (GSList const *l = objects; l != NULL; l = l->next) {
305 if (!SP_IS_ITEM (l->data))
306 continue;
308 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
310 SPObject *object = SP_OBJECT(l->data);
312 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
313 ++n_notstroked; // do not count nonstroked objects
314 continue;
315 } else {
316 notstroked = false;
317 }
319 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
320 }
322 if (notstroked)
323 return NR_HUGE;
325 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
326 }
328 /**
329 * Write to style_res the average fill or stroke of list of objects, if applicable.
330 */
331 int
332 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
333 {
334 if (g_slist_length(objects) == 0) {
335 /* No objects, set empty */
336 return QUERY_STYLE_NOTHING;
337 }
339 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
340 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
341 paint_res->set = TRUE;
343 gfloat c[4];
344 c[0] = c[1] = c[2] = c[3] = 0.0;
345 gint num = 0;
347 gfloat prev[4];
348 prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
349 bool same_color = true;
351 for (GSList const *i = objects; i != NULL; i = i->next) {
352 SPObject *obj = SP_OBJECT (i->data);
353 SPStyle *style = SP_OBJECT_STYLE (obj);
354 if (!style) continue;
356 SPIPaint *paint = isfill? &style->fill : &style->stroke;
358 // We consider paint "effectively set" for anything within text hierarchy
359 SPObject *parent = SP_OBJECT_PARENT (obj);
360 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));
362 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
364 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
365 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
366 }
368 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
369 // both previous paint and this paint were a server, see if the servers are compatible
371 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
372 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
374 if (SP_IS_LINEARGRADIENT (server_res)) {
376 if (!SP_IS_LINEARGRADIENT(server))
377 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
379 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
380 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
381 if (vector_res != vector)
382 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
384 } else if (SP_IS_RADIALGRADIENT (server_res)) {
386 if (!SP_IS_RADIALGRADIENT(server))
387 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
389 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
390 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
391 if (vector_res != vector)
392 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
394 } else if (SP_IS_PATTERN (server_res)) {
396 if (!SP_IS_PATTERN(server))
397 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
399 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
400 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
401 if (pat_res != pat)
402 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
403 }
404 }
406 // 2. Sum color, copy server from paint to paint_res
408 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
410 gfloat d[3];
411 sp_color_get_rgb_floatv (&paint->value.color, d);
413 // Check if this color is the same as previous
414 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
415 prev[0] = d[0];
416 prev[1] = d[1];
417 prev[2] = d[2];
418 prev[3] = d[3];
419 } else {
420 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
421 same_color = false;
422 }
424 // average color
425 c[0] += d[0];
426 c[1] += d[1];
427 c[2] += d[2];
428 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
429 num ++;
430 }
432 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
433 if (isfill) {
434 SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
435 } else {
436 SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
437 }
438 }
439 paint_res->type = paint->type;
440 paint_res->set = paint_effectively_set;
441 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
442 }
444 // After all objects processed, divide the color if necessary and return
445 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
446 g_assert (num >= 1);
448 c[0] /= num;
449 c[1] /= num;
450 c[2] /= num;
451 c[3] /= num;
452 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
453 if (isfill) {
454 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
455 } else {
456 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
457 }
458 if (num > 1) {
459 if (same_color)
460 return QUERY_STYLE_MULTIPLE_SAME;
461 else
462 return QUERY_STYLE_MULTIPLE_AVERAGED;
463 } else {
464 return QUERY_STYLE_SINGLE;
465 }
466 }
468 // Not color
469 if (g_slist_length(objects) > 1) {
470 return QUERY_STYLE_MULTIPLE_SAME;
471 } else {
472 return QUERY_STYLE_SINGLE;
473 }
474 }
476 /**
477 * Write to style_res the average opacity of a list of objects.
478 */
479 int
480 objects_query_opacity (GSList *objects, SPStyle *style_res)
481 {
482 if (g_slist_length(objects) == 0) {
483 /* No objects, set empty */
484 return QUERY_STYLE_NOTHING;
485 }
487 gdouble opacity_sum = 0;
488 gdouble opacity_prev = -1;
489 bool same_opacity = true;
490 guint opacity_items = 0;
493 for (GSList const *i = objects; i != NULL; i = i->next) {
494 SPObject *obj = SP_OBJECT (i->data);
495 SPStyle *style = SP_OBJECT_STYLE (obj);
496 if (!style) continue;
498 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
499 opacity_sum += opacity;
500 if (opacity_prev != -1 && opacity != opacity_prev)
501 same_opacity = false;
502 opacity_prev = opacity;
503 opacity_items ++;
504 }
505 if (opacity_items > 1)
506 opacity_sum /= opacity_items;
508 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
510 if (opacity_items == 0) {
511 return QUERY_STYLE_NOTHING;
512 } else if (opacity_items == 1) {
513 return QUERY_STYLE_SINGLE;
514 } else {
515 if (same_opacity)
516 return QUERY_STYLE_MULTIPLE_SAME;
517 else
518 return QUERY_STYLE_MULTIPLE_AVERAGED;
519 }
520 }
522 /**
523 * Write to style_res the average stroke width of a list of objects.
524 */
525 int
526 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
527 {
528 if (g_slist_length(objects) == 0) {
529 /* No objects, set empty */
530 return QUERY_STYLE_NOTHING;
531 }
533 gdouble avgwidth = 0.0;
535 gdouble prev_sw = -1;
536 bool same_sw = true;
538 int n_stroked = 0;
540 for (GSList const *i = objects; i != NULL; i = i->next) {
541 SPObject *obj = SP_OBJECT (i->data);
542 if (!SP_IS_ITEM(obj)) continue;
543 SPStyle *style = SP_OBJECT_STYLE (obj);
544 if (!style) continue;
546 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
547 continue;
548 }
550 n_stroked ++;
552 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
553 double sw = style->stroke_width.computed * i2d.expansion();
555 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
556 same_sw = false;
557 prev_sw = sw;
559 avgwidth += sw;
560 }
562 if (n_stroked > 1)
563 avgwidth /= (n_stroked);
565 style_res->stroke_width.computed = avgwidth;
566 style_res->stroke_width.set = true;
568 if (n_stroked == 0) {
569 return QUERY_STYLE_NOTHING;
570 } else if (n_stroked == 1) {
571 return QUERY_STYLE_SINGLE;
572 } else {
573 if (same_sw)
574 return QUERY_STYLE_MULTIPLE_SAME;
575 else
576 return QUERY_STYLE_MULTIPLE_AVERAGED;
577 }
578 }
580 /**
581 * Write to style_res the average miter limit of a list of objects.
582 */
583 int
584 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
585 {
586 if (g_slist_length(objects) == 0) {
587 /* No objects, set empty */
588 return QUERY_STYLE_NOTHING;
589 }
591 gdouble avgml = 0.0;
592 int n_stroked = 0;
594 gdouble prev_ml = -1;
595 bool same_ml = true;
597 for (GSList const *i = objects; i != NULL; i = i->next) {
598 SPObject *obj = SP_OBJECT (i->data);
599 if (!SP_IS_ITEM(obj)) continue;
600 SPStyle *style = SP_OBJECT_STYLE (obj);
601 if (!style) continue;
603 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
604 continue;
605 }
607 n_stroked ++;
609 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
610 same_ml = false;
611 prev_ml = style->stroke_miterlimit.value;
613 avgml += style->stroke_miterlimit.value;
614 }
616 if (n_stroked > 1)
617 avgml /= (n_stroked);
619 style_res->stroke_miterlimit.value = avgml;
620 style_res->stroke_miterlimit.set = true;
622 if (n_stroked == 0) {
623 return QUERY_STYLE_NOTHING;
624 } else if (n_stroked == 1) {
625 return QUERY_STYLE_SINGLE;
626 } else {
627 if (same_ml)
628 return QUERY_STYLE_MULTIPLE_SAME;
629 else
630 return QUERY_STYLE_MULTIPLE_AVERAGED;
631 }
632 }
634 /**
635 * Write to style_res the stroke cap of a list of objects.
636 */
637 int
638 objects_query_strokecap (GSList *objects, SPStyle *style_res)
639 {
640 if (g_slist_length(objects) == 0) {
641 /* No objects, set empty */
642 return QUERY_STYLE_NOTHING;
643 }
645 int cap = -1;
646 gdouble prev_cap = -1;
647 bool same_cap = true;
648 int n_stroked = 0;
650 for (GSList const *i = objects; i != NULL; i = i->next) {
651 SPObject *obj = SP_OBJECT (i->data);
652 if (!SP_IS_ITEM(obj)) continue;
653 SPStyle *style = SP_OBJECT_STYLE (obj);
654 if (!style) continue;
656 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
657 continue;
658 }
660 n_stroked ++;
662 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
663 same_cap = false;
664 prev_cap = style->stroke_linecap.value;
666 cap = style->stroke_linecap.value;
667 }
669 style_res->stroke_linecap.value = cap;
670 style_res->stroke_linecap.set = true;
672 if (n_stroked == 0) {
673 return QUERY_STYLE_NOTHING;
674 } else if (n_stroked == 1) {
675 return QUERY_STYLE_SINGLE;
676 } else {
677 if (same_cap)
678 return QUERY_STYLE_MULTIPLE_SAME;
679 else
680 return QUERY_STYLE_MULTIPLE_DIFFERENT;
681 }
682 }
684 /**
685 * Write to style_res the stroke join of a list of objects.
686 */
687 int
688 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
689 {
690 if (g_slist_length(objects) == 0) {
691 /* No objects, set empty */
692 return QUERY_STYLE_NOTHING;
693 }
695 int join = -1;
696 gdouble prev_join = -1;
697 bool same_join = true;
698 int n_stroked = 0;
700 for (GSList const *i = objects; i != NULL; i = i->next) {
701 SPObject *obj = SP_OBJECT (i->data);
702 if (!SP_IS_ITEM(obj)) continue;
703 SPStyle *style = SP_OBJECT_STYLE (obj);
704 if (!style) continue;
706 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
707 continue;
708 }
710 n_stroked ++;
712 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
713 same_join = false;
714 prev_join = style->stroke_linejoin.value;
716 join = style->stroke_linejoin.value;
717 }
719 style_res->stroke_linejoin.value = join;
720 style_res->stroke_linejoin.set = true;
722 if (n_stroked == 0) {
723 return QUERY_STYLE_NOTHING;
724 } else if (n_stroked == 1) {
725 return QUERY_STYLE_SINGLE;
726 } else {
727 if (same_join)
728 return QUERY_STYLE_MULTIPLE_SAME;
729 else
730 return QUERY_STYLE_MULTIPLE_DIFFERENT;
731 }
732 }
734 /**
735 * Write to style_res the average font size and spacing of objects.
736 */
737 int
738 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
739 {
740 bool different = false;
742 double size = 0;
743 double letterspacing = 0;
744 double linespacing = 0;
745 bool linespacing_normal = false;
746 bool letterspacing_normal = false;
748 double size_prev = 0;
749 double letterspacing_prev = 0;
750 double linespacing_prev = 0;
752 /// \todo FIXME: add word spacing, kerns? rotates?
754 int texts = 0;
756 for (GSList const *i = objects; i != NULL; i = i->next) {
757 SPObject *obj = SP_OBJECT (i->data);
759 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
760 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
761 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
762 continue;
764 SPStyle *style = SP_OBJECT_STYLE (obj);
765 if (!style) continue;
767 texts ++;
768 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
770 if (style->letter_spacing.normal) {
771 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
772 letterspacing_normal = true;
773 } else {
774 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
775 letterspacing_normal = false;
776 }
778 double linespacing_current;
779 if (style->line_height.normal) {
780 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
781 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
782 linespacing_normal = true;
783 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
784 linespacing_current = style->line_height.value;
785 linespacing_normal = false;
786 } else { // we need % here
787 linespacing_current = style->line_height.computed / style->font_size.computed;
788 linespacing_normal = false;
789 }
790 linespacing += linespacing_current;
792 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
793 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
794 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
795 different = true;
796 }
798 size_prev = style->font_size.computed;
799 letterspacing_prev = style->letter_spacing.computed;
800 linespacing_prev = linespacing_current;
802 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
803 style_res->text_anchor.computed = style->text_anchor.computed;
804 style_res->writing_mode.computed = style->writing_mode.computed;
805 }
807 if (texts == 0)
808 return QUERY_STYLE_NOTHING;
810 if (texts > 1) {
811 size /= texts;
812 letterspacing /= texts;
813 linespacing /= texts;
814 }
816 style_res->font_size.computed = size;
817 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
819 style_res->letter_spacing.normal = letterspacing_normal;
820 style_res->letter_spacing.computed = letterspacing;
822 style_res->line_height.normal = linespacing_normal;
823 style_res->line_height.computed = linespacing;
824 style_res->line_height.value = linespacing;
825 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
827 if (texts > 1) {
828 if (different) {
829 return QUERY_STYLE_MULTIPLE_AVERAGED;
830 } else {
831 return QUERY_STYLE_MULTIPLE_SAME;
832 }
833 } else {
834 return QUERY_STYLE_SINGLE;
835 }
836 }
838 /**
839 * Write to style_res the average font style of objects.
840 */
841 int
842 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
843 {
844 bool different = false;
845 bool set = false;
847 int texts = 0;
849 for (GSList const *i = objects; i != NULL; i = i->next) {
850 SPObject *obj = SP_OBJECT (i->data);
852 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
853 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
854 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
855 continue;
857 SPStyle *style = SP_OBJECT_STYLE (obj);
858 if (!style) continue;
860 texts ++;
862 if (set &&
863 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
864 different = true; // different styles
865 }
867 set = TRUE;
868 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
869 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
870 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
871 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
872 }
874 if (texts == 0 || !set)
875 return QUERY_STYLE_NOTHING;
877 if (texts > 1) {
878 if (different) {
879 return QUERY_STYLE_MULTIPLE_DIFFERENT;
880 } else {
881 return QUERY_STYLE_MULTIPLE_SAME;
882 }
883 } else {
884 return QUERY_STYLE_SINGLE;
885 }
886 }
888 /**
889 * Write to style_res the average font family of objects.
890 */
891 int
892 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
893 {
894 bool different = false;
895 int texts = 0;
897 if (style_res->text->font_family.value) {
898 g_free(style_res->text->font_family.value);
899 style_res->text->font_family.value = NULL;
900 }
901 style_res->text->font_family.set = FALSE;
903 for (GSList const *i = objects; i != NULL; i = i->next) {
904 SPObject *obj = SP_OBJECT (i->data);
906 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
907 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
908 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
909 continue;
911 SPStyle *style = SP_OBJECT_STYLE (obj);
912 if (!style) continue;
914 texts ++;
916 if (style_res->text->font_family.value && style->text->font_family.value &&
917 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
918 different = true; // different fonts
919 }
921 if (style_res->text->font_family.value) {
922 g_free(style_res->text->font_family.value);
923 style_res->text->font_family.value = NULL;
924 }
926 style_res->text->font_family.set = TRUE;
927 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
928 }
930 if (texts == 0 || !style_res->text->font_family.set)
931 return QUERY_STYLE_NOTHING;
933 if (texts > 1) {
934 if (different) {
935 return QUERY_STYLE_MULTIPLE_DIFFERENT;
936 } else {
937 return QUERY_STYLE_MULTIPLE_SAME;
938 }
939 } else {
940 return QUERY_STYLE_SINGLE;
941 }
942 }
944 /**
945 * Query the given list of objects for the given property, write
946 * the result to style, return appropriate flag.
947 */
948 int
949 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
950 {
951 if (property == QUERY_STYLE_PROPERTY_FILL) {
952 return objects_query_fillstroke (list, style, true);
953 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
954 return objects_query_fillstroke (list, style, false);
956 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
957 return objects_query_strokewidth (list, style);
958 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
959 return objects_query_miterlimit (list, style);
960 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
961 return objects_query_strokecap (list, style);
962 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
963 return objects_query_strokejoin (list, style);
965 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
966 return objects_query_opacity (list, style);
968 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
969 return objects_query_fontfamily (list, style);
970 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
971 return objects_query_fontstyle (list, style);
972 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
973 return objects_query_fontnumbers (list, style);
974 }
976 return QUERY_STYLE_NOTHING;
977 }
980 /**
981 * Query the subselection (if any) or selection on the given desktop for the given property, write
982 * the result to style, return appropriate flag.
983 */
984 int
985 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
986 {
987 int ret = desktop->_query_style_signal.emit(style, property);
989 if (ret != QUERY_STYLE_NOTHING)
990 return ret; // subselection returned a style, pass it on
992 // otherwise, do querying and averaging over selection
993 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
994 }
996 /**
997 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
998 * the properties returned QUERY_STYLE_NOTHING.
999 */
1000 bool
1001 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
1002 {
1003 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
1004 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
1005 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
1006 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
1007 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
1008 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1009 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1010 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
1011 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1012 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
1014 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);
1015 }
1018 /*
1019 Local Variables:
1020 mode:c++
1021 c-file-style:"stroustrup"
1022 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1023 indent-tabs-mode:nil
1024 fill-column:99
1025 End:
1026 */
1027 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :