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.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 /**
222 * Apply the desktop's current style or the tool style to repr.
223 */
224 void
225 sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, char const *tool, bool with_text)
226 {
227 SPCSSAttr *css_current = sp_desktop_get_style(desktop, with_text);
228 if ((prefs_get_int_attribute(tool, "usecurrent", 0) != 0) && css_current) {
229 sp_repr_css_set(repr, css_current, "style");
230 } else {
231 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, tool);
232 if (tool_repr) {
233 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
234 sp_repr_css_set(repr, css, "style");
235 sp_repr_css_attr_unref(css);
236 }
237 }
238 if (css_current) {
239 sp_repr_css_attr_unref(css_current);
240 }
241 }
243 /**
244 * Returns the font size (in SVG pixels) of the text tool style (if text
245 * tool uses its own style) or desktop style (otherwise).
246 */
247 double
248 sp_desktop_get_font_size_tool(SPDesktop *desktop)
249 {
250 gchar const *desktop_style = inkscape_get_repr(INKSCAPE, "desktop")->attribute("style");
251 gchar const *style_str = NULL;
252 if ((prefs_get_int_attribute("tools.text", "usecurrent", 0) != 0) && desktop_style) {
253 style_str = desktop_style;
254 } else {
255 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.text");
256 if (tool_repr) {
257 style_str = tool_repr->attribute("style");
258 }
259 }
261 double ret = 12;
262 if (style_str) {
263 SPStyle *style = sp_style_new();
264 sp_style_merge_from_style_string(style, style_str);
265 ret = style->font_size.computed;
266 sp_style_unref(style);
267 }
268 return ret;
269 }
271 /** Determine average stroke width, simple method */
272 // see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
273 gdouble
274 stroke_average_width (GSList const *objects)
275 {
276 if (g_slist_length ((GSList *) objects) == 0)
277 return NR_HUGE;
279 gdouble avgwidth = 0.0;
280 bool notstroked = true;
281 int n_notstroked = 0;
283 for (GSList const *l = objects; l != NULL; l = l->next) {
284 if (!SP_IS_ITEM (l->data))
285 continue;
287 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(l->data));
289 SPObject *object = SP_OBJECT(l->data);
291 if ( object->style->stroke.type == SP_PAINT_TYPE_NONE ) {
292 ++n_notstroked; // do not count nonstroked objects
293 continue;
294 } else {
295 notstroked = false;
296 }
298 avgwidth += SP_OBJECT_STYLE (object)->stroke_width.computed * i2d.expansion();
299 }
301 if (notstroked)
302 return NR_HUGE;
304 return avgwidth / (g_slist_length ((GSList *) objects) - n_notstroked);
305 }
307 /**
308 * Write to style_res the average fill or stroke of list of objects, if applicable.
309 */
310 int
311 objects_query_fillstroke (GSList *objects, SPStyle *style_res, bool const isfill)
312 {
313 if (g_slist_length(objects) == 0) {
314 /* No objects, set empty */
315 return QUERY_STYLE_NOTHING;
316 }
318 SPIPaint *paint_res = isfill? &style_res->fill : &style_res->stroke;
319 paint_res->type = SP_PAINT_TYPE_IMPOSSIBLE;
320 paint_res->set = TRUE;
322 gfloat c[4];
323 c[0] = c[1] = c[2] = c[3] = 0.0;
324 gint num = 0;
326 gfloat prev[4];
327 prev[0] = prev[1] = prev[2] = prev[3] = 0.0;
328 bool same_color = true;
330 for (GSList const *i = objects; i != NULL; i = i->next) {
331 SPObject *obj = SP_OBJECT (i->data);
332 SPStyle *style = SP_OBJECT_STYLE (obj);
333 if (!style) continue;
335 SPIPaint *paint = isfill? &style->fill : &style->stroke;
337 // We consider paint "effectively set" for anything within text hierarchy
338 SPObject *parent = SP_OBJECT_PARENT (obj);
339 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));
341 // 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
343 if ((paint_res->type != SP_PAINT_TYPE_IMPOSSIBLE) && (paint->type != paint_res->type || (paint_res->set != paint_effectively_set))) {
344 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
345 }
347 if (paint_res->set && paint->set && paint_res->type == SP_PAINT_TYPE_PAINTSERVER) {
348 // both previous paint and this paint were a server, see if the servers are compatible
350 SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
351 SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER (style) : SP_STYLE_STROKE_SERVER (style);
353 if (SP_IS_LINEARGRADIENT (server_res)) {
355 if (!SP_IS_LINEARGRADIENT(server))
356 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
358 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
359 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
360 if (vector_res != vector)
361 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
363 } else if (SP_IS_RADIALGRADIENT (server_res)) {
365 if (!SP_IS_RADIALGRADIENT(server))
366 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
368 SPGradient *vector = sp_gradient_get_vector ( SP_GRADIENT (server), FALSE );
369 SPGradient *vector_res = sp_gradient_get_vector ( SP_GRADIENT (server_res), FALSE );
370 if (vector_res != vector)
371 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
373 } else if (SP_IS_PATTERN (server_res)) {
375 if (!SP_IS_PATTERN(server))
376 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
378 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
379 SPPattern *pat_res = pattern_getroot (SP_PATTERN (server_res));
380 if (pat_res != pat)
381 return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
382 }
383 }
385 // 2. Sum color, copy server from paint to paint_res
387 if (paint_res->set && paint_effectively_set && paint->type == SP_PAINT_TYPE_COLOR) {
389 gfloat d[3];
390 sp_color_get_rgb_floatv (&paint->value.color, d);
392 // Check if this color is the same as previous
393 if (paint_res->type == SP_PAINT_TYPE_IMPOSSIBLE) {
394 prev[0] = d[0];
395 prev[1] = d[1];
396 prev[2] = d[2];
397 prev[3] = d[3];
398 } else {
399 if (same_color && (prev[0] != d[0] || prev[1] != d[1] || prev[2] != d[2] || prev[3] != d[3]))
400 same_color = false;
401 }
403 // average color
404 c[0] += d[0];
405 c[1] += d[1];
406 c[2] += d[2];
407 c[3] += SP_SCALE24_TO_FLOAT (isfill? style->fill_opacity.value : style->stroke_opacity.value);
408 num ++;
409 }
411 if (paint_res->set && paint->set && paint->type == SP_PAINT_TYPE_PAINTSERVER) { // copy the server
412 if (isfill) {
413 SP_STYLE_FILL_SERVER (style_res) = SP_STYLE_FILL_SERVER (style);
414 } else {
415 SP_STYLE_STROKE_SERVER (style_res) = SP_STYLE_STROKE_SERVER (style);
416 }
417 }
418 paint_res->type = paint->type;
419 paint_res->set = paint_effectively_set;
420 style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
421 }
423 // After all objects processed, divide the color if necessary and return
424 if (paint_res->set && paint_res->type == SP_PAINT_TYPE_COLOR) { // set the color
425 g_assert (num >= 1);
427 c[0] /= num;
428 c[1] /= num;
429 c[2] /= num;
430 c[3] /= num;
431 sp_color_set_rgb_float(&paint_res->value.color, c[0], c[1], c[2]);
432 if (isfill) {
433 style_res->fill_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
434 } else {
435 style_res->stroke_opacity.value = SP_SCALE24_FROM_FLOAT (c[3]);
436 }
437 if (num > 1) {
438 if (same_color)
439 return QUERY_STYLE_MULTIPLE_SAME;
440 else
441 return QUERY_STYLE_MULTIPLE_AVERAGED;
442 } else {
443 return QUERY_STYLE_SINGLE;
444 }
445 }
447 // Not color
448 if (g_slist_length(objects) > 1) {
449 return QUERY_STYLE_MULTIPLE_SAME;
450 } else {
451 return QUERY_STYLE_SINGLE;
452 }
453 }
455 /**
456 * Write to style_res the average opacity of a list of objects.
457 */
458 int
459 objects_query_opacity (GSList *objects, SPStyle *style_res)
460 {
461 if (g_slist_length(objects) == 0) {
462 /* No objects, set empty */
463 return QUERY_STYLE_NOTHING;
464 }
466 gdouble opacity_sum = 0;
467 gdouble opacity_prev = -1;
468 bool same_opacity = true;
469 guint opacity_items = 0;
472 for (GSList const *i = objects; i != NULL; i = i->next) {
473 SPObject *obj = SP_OBJECT (i->data);
474 SPStyle *style = SP_OBJECT_STYLE (obj);
475 if (!style) continue;
477 double opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
478 opacity_sum += opacity;
479 if (opacity_prev != -1 && opacity != opacity_prev)
480 same_opacity = false;
481 opacity_prev = opacity;
482 opacity_items ++;
483 }
484 if (opacity_items > 1)
485 opacity_sum /= opacity_items;
487 style_res->opacity.value = SP_SCALE24_FROM_FLOAT(opacity_sum);
489 if (opacity_items == 0) {
490 return QUERY_STYLE_NOTHING;
491 } else if (opacity_items == 1) {
492 return QUERY_STYLE_SINGLE;
493 } else {
494 if (same_opacity)
495 return QUERY_STYLE_MULTIPLE_SAME;
496 else
497 return QUERY_STYLE_MULTIPLE_AVERAGED;
498 }
499 }
501 /**
502 * Write to style_res the average stroke width of a list of objects.
503 */
504 int
505 objects_query_strokewidth (GSList *objects, SPStyle *style_res)
506 {
507 if (g_slist_length(objects) == 0) {
508 /* No objects, set empty */
509 return QUERY_STYLE_NOTHING;
510 }
512 gdouble avgwidth = 0.0;
514 gdouble prev_sw = -1;
515 bool same_sw = true;
517 int n_stroked = 0;
519 for (GSList const *i = objects; i != NULL; i = i->next) {
520 SPObject *obj = SP_OBJECT (i->data);
521 if (!SP_IS_ITEM(obj)) continue;
522 SPStyle *style = SP_OBJECT_STYLE (obj);
523 if (!style) continue;
525 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
526 continue;
527 }
529 n_stroked ++;
531 NR::Matrix i2d = sp_item_i2d_affine (SP_ITEM(obj));
532 double sw = style->stroke_width.computed * i2d.expansion();
534 if (prev_sw != -1 && fabs(sw - prev_sw) > 1e-3)
535 same_sw = false;
536 prev_sw = sw;
538 avgwidth += sw;
539 }
541 if (n_stroked > 1)
542 avgwidth /= (n_stroked);
544 style_res->stroke_width.computed = avgwidth;
545 style_res->stroke_width.set = true;
547 if (n_stroked == 0) {
548 return QUERY_STYLE_NOTHING;
549 } else if (n_stroked == 1) {
550 return QUERY_STYLE_SINGLE;
551 } else {
552 if (same_sw)
553 return QUERY_STYLE_MULTIPLE_SAME;
554 else
555 return QUERY_STYLE_MULTIPLE_AVERAGED;
556 }
557 }
559 /**
560 * Write to style_res the average miter limit of a list of objects.
561 */
562 int
563 objects_query_miterlimit (GSList *objects, SPStyle *style_res)
564 {
565 if (g_slist_length(objects) == 0) {
566 /* No objects, set empty */
567 return QUERY_STYLE_NOTHING;
568 }
570 gdouble avgml = 0.0;
571 int n_stroked = 0;
573 gdouble prev_ml = -1;
574 bool same_ml = true;
576 for (GSList const *i = objects; i != NULL; i = i->next) {
577 SPObject *obj = SP_OBJECT (i->data);
578 if (!SP_IS_ITEM(obj)) continue;
579 SPStyle *style = SP_OBJECT_STYLE (obj);
580 if (!style) continue;
582 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
583 continue;
584 }
586 n_stroked ++;
588 if (prev_ml != -1 && fabs(style->stroke_miterlimit.value - prev_ml) > 1e-3)
589 same_ml = false;
590 prev_ml = style->stroke_miterlimit.value;
592 avgml += style->stroke_miterlimit.value;
593 }
595 if (n_stroked > 1)
596 avgml /= (n_stroked);
598 style_res->stroke_miterlimit.value = avgml;
599 style_res->stroke_miterlimit.set = true;
601 if (n_stroked == 0) {
602 return QUERY_STYLE_NOTHING;
603 } else if (n_stroked == 1) {
604 return QUERY_STYLE_SINGLE;
605 } else {
606 if (same_ml)
607 return QUERY_STYLE_MULTIPLE_SAME;
608 else
609 return QUERY_STYLE_MULTIPLE_AVERAGED;
610 }
611 }
613 /**
614 * Write to style_res the stroke cap of a list of objects.
615 */
616 int
617 objects_query_strokecap (GSList *objects, SPStyle *style_res)
618 {
619 if (g_slist_length(objects) == 0) {
620 /* No objects, set empty */
621 return QUERY_STYLE_NOTHING;
622 }
624 int cap = -1;
625 gdouble prev_cap = -1;
626 bool same_cap = true;
627 int n_stroked = 0;
629 for (GSList const *i = objects; i != NULL; i = i->next) {
630 SPObject *obj = SP_OBJECT (i->data);
631 if (!SP_IS_ITEM(obj)) continue;
632 SPStyle *style = SP_OBJECT_STYLE (obj);
633 if (!style) continue;
635 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
636 continue;
637 }
639 n_stroked ++;
641 if (prev_cap != -1 && style->stroke_linecap.value != prev_cap)
642 same_cap = false;
643 prev_cap = style->stroke_linecap.value;
645 cap = style->stroke_linecap.value;
646 }
648 style_res->stroke_linecap.value = cap;
649 style_res->stroke_linecap.set = true;
651 if (n_stroked == 0) {
652 return QUERY_STYLE_NOTHING;
653 } else if (n_stroked == 1) {
654 return QUERY_STYLE_SINGLE;
655 } else {
656 if (same_cap)
657 return QUERY_STYLE_MULTIPLE_SAME;
658 else
659 return QUERY_STYLE_MULTIPLE_DIFFERENT;
660 }
661 }
663 /**
664 * Write to style_res the stroke join of a list of objects.
665 */
666 int
667 objects_query_strokejoin (GSList *objects, SPStyle *style_res)
668 {
669 if (g_slist_length(objects) == 0) {
670 /* No objects, set empty */
671 return QUERY_STYLE_NOTHING;
672 }
674 int join = -1;
675 gdouble prev_join = -1;
676 bool same_join = true;
677 int n_stroked = 0;
679 for (GSList const *i = objects; i != NULL; i = i->next) {
680 SPObject *obj = SP_OBJECT (i->data);
681 if (!SP_IS_ITEM(obj)) continue;
682 SPStyle *style = SP_OBJECT_STYLE (obj);
683 if (!style) continue;
685 if ( style->stroke.type == SP_PAINT_TYPE_NONE ) {
686 continue;
687 }
689 n_stroked ++;
691 if (prev_join != -1 && style->stroke_linejoin.value != prev_join)
692 same_join = false;
693 prev_join = style->stroke_linejoin.value;
695 join = style->stroke_linejoin.value;
696 }
698 style_res->stroke_linejoin.value = join;
699 style_res->stroke_linejoin.set = true;
701 if (n_stroked == 0) {
702 return QUERY_STYLE_NOTHING;
703 } else if (n_stroked == 1) {
704 return QUERY_STYLE_SINGLE;
705 } else {
706 if (same_join)
707 return QUERY_STYLE_MULTIPLE_SAME;
708 else
709 return QUERY_STYLE_MULTIPLE_DIFFERENT;
710 }
711 }
713 /**
714 * Write to style_res the average font size and spacing of objects.
715 */
716 int
717 objects_query_fontnumbers (GSList *objects, SPStyle *style_res)
718 {
719 bool different = false;
721 double size = 0;
722 double letterspacing = 0;
723 double linespacing = 0;
724 bool linespacing_normal = false;
725 bool letterspacing_normal = false;
727 double size_prev = 0;
728 double letterspacing_prev = 0;
729 double linespacing_prev = 0;
731 /// \todo FIXME: add word spacing, kerns? rotates?
733 int texts = 0;
735 for (GSList const *i = objects; i != NULL; i = i->next) {
736 SPObject *obj = SP_OBJECT (i->data);
738 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
739 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
740 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
741 continue;
743 SPStyle *style = SP_OBJECT_STYLE (obj);
744 if (!style) continue;
746 texts ++;
747 size += style->font_size.computed * NR::expansion(sp_item_i2d_affine(SP_ITEM(obj))); /// \todo FIXME: we assume non-% units here
749 if (style->letter_spacing.normal) {
750 if (!different && (letterspacing_prev == 0 || letterspacing_prev == letterspacing))
751 letterspacing_normal = true;
752 } else {
753 letterspacing += style->letter_spacing.computed; /// \todo FIXME: we assume non-% units here
754 letterspacing_normal = false;
755 }
757 double linespacing_current;
758 if (style->line_height.normal) {
759 linespacing_current = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
760 if (!different && (linespacing_prev == 0 || linespacing_prev == linespacing_current))
761 linespacing_normal = true;
762 } else if (style->line_height.unit == SP_CSS_UNIT_PERCENT || style->font_size.computed == 0) {
763 linespacing_current = style->line_height.value;
764 linespacing_normal = false;
765 } else { // we need % here
766 linespacing_current = style->line_height.computed / style->font_size.computed;
767 linespacing_normal = false;
768 }
769 linespacing += linespacing_current;
771 if ((size_prev != 0 && style->font_size.computed != size_prev) ||
772 (letterspacing_prev != 0 && style->letter_spacing.computed != letterspacing_prev) ||
773 (linespacing_prev != 0 && linespacing_current != linespacing_prev)) {
774 different = true;
775 }
777 size_prev = style->font_size.computed;
778 letterspacing_prev = style->letter_spacing.computed;
779 linespacing_prev = linespacing_current;
781 // FIXME: we must detect MULTIPLE_DIFFERENT for these too
782 style_res->text_anchor.computed = style->text_anchor.computed;
783 style_res->writing_mode.computed = style->writing_mode.computed;
784 }
786 if (texts == 0)
787 return QUERY_STYLE_NOTHING;
789 if (texts > 1) {
790 size /= texts;
791 letterspacing /= texts;
792 linespacing /= texts;
793 }
795 style_res->font_size.computed = size;
796 style_res->font_size.type = SP_FONT_SIZE_LENGTH;
798 style_res->letter_spacing.normal = letterspacing_normal;
799 style_res->letter_spacing.computed = letterspacing;
801 style_res->line_height.normal = linespacing_normal;
802 style_res->line_height.computed = linespacing;
803 style_res->line_height.value = linespacing;
804 style_res->line_height.unit = SP_CSS_UNIT_PERCENT;
806 if (texts > 1) {
807 if (different) {
808 return QUERY_STYLE_MULTIPLE_AVERAGED;
809 } else {
810 return QUERY_STYLE_MULTIPLE_SAME;
811 }
812 } else {
813 return QUERY_STYLE_SINGLE;
814 }
815 }
817 /**
818 * Write to style_res the average font style of objects.
819 */
820 int
821 objects_query_fontstyle (GSList *objects, SPStyle *style_res)
822 {
823 bool different = false;
824 bool set = false;
826 int texts = 0;
828 for (GSList const *i = objects; i != NULL; i = i->next) {
829 SPObject *obj = SP_OBJECT (i->data);
831 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
832 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
833 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
834 continue;
836 SPStyle *style = SP_OBJECT_STYLE (obj);
837 if (!style) continue;
839 texts ++;
841 if (set &&
842 font_style_to_pos(*style_res).signature() != font_style_to_pos(*style).signature() ) {
843 different = true; // different styles
844 }
846 set = TRUE;
847 style_res->font_weight.value = style_res->font_weight.computed = style->font_weight.computed;
848 style_res->font_style.value = style_res->font_style.computed = style->font_style.computed;
849 style_res->font_stretch.value = style_res->font_stretch.computed = style->font_stretch.computed;
850 style_res->font_variant.value = style_res->font_variant.computed = style->font_variant.computed;
851 }
853 if (texts == 0 || !set)
854 return QUERY_STYLE_NOTHING;
856 if (texts > 1) {
857 if (different) {
858 return QUERY_STYLE_MULTIPLE_DIFFERENT;
859 } else {
860 return QUERY_STYLE_MULTIPLE_SAME;
861 }
862 } else {
863 return QUERY_STYLE_SINGLE;
864 }
865 }
867 /**
868 * Write to style_res the average font family of objects.
869 */
870 int
871 objects_query_fontfamily (GSList *objects, SPStyle *style_res)
872 {
873 bool different = false;
874 int texts = 0;
876 if (style_res->text->font_family.value) {
877 g_free(style_res->text->font_family.value);
878 style_res->text->font_family.value = NULL;
879 }
880 style_res->text->font_family.set = FALSE;
882 for (GSList const *i = objects; i != NULL; i = i->next) {
883 SPObject *obj = SP_OBJECT (i->data);
885 if (!SP_IS_TEXT(obj) && !SP_IS_FLOWTEXT(obj)
886 && !SP_IS_TSPAN(obj) && !SP_IS_TEXTPATH(obj)
887 && !SP_IS_FLOWDIV(obj) && !SP_IS_FLOWPARA(obj) && !SP_IS_FLOWTSPAN(obj))
888 continue;
890 SPStyle *style = SP_OBJECT_STYLE (obj);
891 if (!style) continue;
893 texts ++;
895 if (style_res->text->font_family.value && style->text->font_family.value &&
896 strcmp (style_res->text->font_family.value, style->text->font_family.value)) {
897 different = true; // different fonts
898 }
900 if (style_res->text->font_family.value) {
901 g_free(style_res->text->font_family.value);
902 style_res->text->font_family.value = NULL;
903 }
905 style_res->text->font_family.set = TRUE;
906 style_res->text->font_family.value = g_strdup(style->text->font_family.value);
907 }
909 if (texts == 0 || !style_res->text->font_family.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 * Query the given list of objects for the given property, write
925 * the result to style, return appropriate flag.
926 */
927 int
928 sp_desktop_query_style_from_list (GSList *list, SPStyle *style, int property)
929 {
930 if (property == QUERY_STYLE_PROPERTY_FILL) {
931 return objects_query_fillstroke (list, style, true);
932 } else if (property == QUERY_STYLE_PROPERTY_STROKE) {
933 return objects_query_fillstroke (list, style, false);
935 } else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
936 return objects_query_strokewidth (list, style);
937 } else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
938 return objects_query_miterlimit (list, style);
939 } else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
940 return objects_query_strokecap (list, style);
941 } else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
942 return objects_query_strokejoin (list, style);
944 } else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
945 return objects_query_opacity (list, style);
947 } else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
948 return objects_query_fontfamily (list, style);
949 } else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
950 return objects_query_fontstyle (list, style);
951 } else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
952 return objects_query_fontnumbers (list, style);
953 }
955 return QUERY_STYLE_NOTHING;
956 }
959 /**
960 * Query the subselection (if any) or selection on the given desktop for the given property, write
961 * the result to style, return appropriate flag.
962 */
963 int
964 sp_desktop_query_style(SPDesktop *desktop, SPStyle *style, int property)
965 {
966 int ret = desktop->_query_style_signal.emit(style, property);
968 if (ret != QUERY_STYLE_NOTHING)
969 return ret; // subselection returned a style, pass it on
971 // otherwise, do querying and averaging over selection
972 return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
973 }
975 /**
976 * Do the same as sp_desktop_query_style for all (defined) style properties, return true if none of
977 * the properties returned QUERY_STYLE_NOTHING.
978 */
979 bool
980 sp_desktop_query_style_all (SPDesktop *desktop, SPStyle *query)
981 {
982 int result_family = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
983 int result_fstyle = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
984 int result_fnumbers = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);
985 int result_fill = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_FILL);
986 int result_stroke = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKE);
987 int result_strokewidth = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
988 int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
989 int result_strokecap = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKECAP);
990 int result_strokejoin = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
991 int result_opacity = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
993 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);
994 }
997 /*
998 Local Variables:
999 mode:c++
1000 c-file-style:"stroustrup"
1001 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1002 indent-tabs-mode:nil
1003 fill-column:99
1004 End:
1005 */
1006 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :