1 #define __SP_STYLE_C__
3 /** \file
4 * SVG stylesheets implementation.
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Peter Moulder <pmoulder@mail.csse.monash.edu.au>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 2001-2002 Lauris Kaplinski
12 * Copyright (C) 2001 Ximian, Inc.
13 * Copyright (C) 2005 Monash University
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include "libcroco/cr-sel-eng.h"
23 #include "xml/croco-node-iface.h"
25 #include "svg/svg.h"
26 #include "svg/svg-color.h"
27 #include "svg/svg-icc-color.h"
29 #include "display/canvas-bpath.h"
30 #include "attributes.h"
31 #include "document.h"
32 #include "extract-uri.h"
33 #include "marker-status.h"
34 #include "uri-references.h"
35 #include "sp-paint-server.h"
36 #include "streq.h"
37 #include "strneq.h"
38 #include "style.h"
39 #include "svg/css-ostringstream.h"
40 #include "xml/repr.h"
41 #include "unit-constants.h"
42 #include "isnan.h"
44 #include <sigc++/functors/ptr_fun.h>
45 #include <sigc++/adaptors/bind.h>
47 using Inkscape::CSSOStringStream;
48 using std::vector;
50 namespace Inkscape {
52 /**
53 * Parses a CSS url() specification; temporary hack until
54 * style stuff is redone.
55 * \param string the CSS string to parse
56 * \return a newly-allocated URL string (or NULL); free with g_free()
57 */
58 gchar *parse_css_url(gchar const *string) {
59 if (!string)
60 return NULL;
62 gchar const *iter = string;
63 for ( ; g_ascii_isspace(*iter) ; iter = g_utf8_next_char(iter) );
64 if (strncmp(iter, "url(", 4))
65 return NULL;
66 iter += 4;
68 gchar const end_char = *iter;
69 if ( *iter == '"' || *iter == '\'' ) {
70 iter += 1;
71 }
73 GString *temp = g_string_new(NULL);
74 for ( ; *iter ; iter = g_utf8_next_char(iter) ) {
75 if ( *iter == '(' || *iter == ')' ||
76 *iter == '"' || *iter == '\'' ||
77 g_ascii_isspace(*iter) ||
78 g_ascii_iscntrl(*iter) )
79 {
80 break;
81 }
82 if ( *iter == '\\' ) {
83 iter = g_utf8_next_char(iter);
84 }
85 if ( *iter & (gchar)0x80 ) {
86 break;
87 } else {
88 g_string_append_c(temp, *iter);
89 }
90 }
92 if ( *iter == end_char && end_char != ')' ) {
93 iter = g_utf8_next_char(iter);
94 }
95 gchar *result;
96 if ( *iter == ')' ) {
97 result = temp->str;
98 g_string_free(temp, FALSE);
99 } else {
100 result = NULL;
101 g_string_free(temp, TRUE);
102 }
104 return result;
105 }
107 }
109 #define BMAX 8192
111 class SPStyleEnum;
113 /*#########################
114 ## FORWARD DECLARATIONS
115 #########################*/
116 static void sp_style_clear(SPStyle *style);
118 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
120 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
121 static void sp_style_merge_ifilter(SPIFilter *child, SPIFilter const *parent);
122 static void sp_style_read_dash(SPStyle *style, gchar const *str);
124 static SPTextStyle *sp_text_style_new(void);
125 static void sp_text_style_clear(SPTextStyle *ts);
126 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
127 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
128 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
129 static void sp_style_privatize_text(SPStyle *style);
131 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
132 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
133 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
134 static void sp_style_read_istring(SPIString *val, gchar const *str);
135 static void sp_style_read_ilength(SPILength *val, gchar const *str);
136 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
137 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
138 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
139 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
140 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
141 static void sp_style_read_ifilter(SPIFilter *f, gchar const *str, SPDocument *document);
143 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
144 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
145 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
146 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
148 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
149 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
150 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
151 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
152 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
153 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
154 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
155 static gint sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key, SPILengthOrNormal const *const val, SPILengthOrNormal const *const base, guint const flags);
156 static gint sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key, SPITextDecoration const *const val, SPITextDecoration const *const base, guint const flags);
157 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
159 static void css2_unescape_unquote(SPIString *val);
161 static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint);
163 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
164 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
166 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
167 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
169 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
171 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
172 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
174 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
176 struct SPStyleEnum {
177 gchar const *key;
178 gint value;
179 };
181 static SPStyleEnum const enum_fill_rule[] = {
182 {"nonzero", SP_WIND_RULE_NONZERO},
183 {"evenodd", SP_WIND_RULE_EVENODD},
184 {NULL, -1}
185 };
187 static SPStyleEnum const enum_stroke_linecap[] = {
188 {"butt", SP_STROKE_LINECAP_BUTT},
189 {"round", SP_STROKE_LINECAP_ROUND},
190 {"square", SP_STROKE_LINECAP_SQUARE},
191 {NULL, -1}
192 };
194 static SPStyleEnum const enum_stroke_linejoin[] = {
195 {"miter", SP_STROKE_LINEJOIN_MITER},
196 {"round", SP_STROKE_LINEJOIN_ROUND},
197 {"bevel", SP_STROKE_LINEJOIN_BEVEL},
198 {NULL, -1}
199 };
201 static SPStyleEnum const enum_font_style[] = {
202 {"normal", SP_CSS_FONT_STYLE_NORMAL},
203 {"italic", SP_CSS_FONT_STYLE_ITALIC},
204 {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
205 {NULL, -1}
206 };
208 static SPStyleEnum const enum_font_size[] = {
209 {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
210 {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
211 {"small", SP_CSS_FONT_SIZE_SMALL},
212 {"medium", SP_CSS_FONT_SIZE_MEDIUM},
213 {"large", SP_CSS_FONT_SIZE_LARGE},
214 {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
215 {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
216 {"smaller", SP_CSS_FONT_SIZE_SMALLER},
217 {"larger", SP_CSS_FONT_SIZE_LARGER},
218 {NULL, -1}
219 };
221 static SPStyleEnum const enum_font_variant[] = {
222 {"normal", SP_CSS_FONT_VARIANT_NORMAL},
223 {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
224 {NULL, -1}
225 };
227 static SPStyleEnum const enum_font_weight[] = {
228 {"100", SP_CSS_FONT_WEIGHT_100},
229 {"200", SP_CSS_FONT_WEIGHT_200},
230 {"300", SP_CSS_FONT_WEIGHT_300},
231 {"400", SP_CSS_FONT_WEIGHT_400},
232 {"500", SP_CSS_FONT_WEIGHT_500},
233 {"600", SP_CSS_FONT_WEIGHT_600},
234 {"700", SP_CSS_FONT_WEIGHT_700},
235 {"800", SP_CSS_FONT_WEIGHT_800},
236 {"900", SP_CSS_FONT_WEIGHT_900},
237 {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
238 {"bold", SP_CSS_FONT_WEIGHT_BOLD},
239 {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
240 {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
241 {NULL, -1}
242 };
244 static SPStyleEnum const enum_font_stretch[] = {
245 {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
246 {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
247 {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
248 {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
249 {"normal", SP_CSS_FONT_STRETCH_NORMAL},
250 {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
251 {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
252 {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
253 {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
254 {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
255 {"wider", SP_CSS_FONT_STRETCH_WIDER},
256 {NULL, -1}
257 };
259 static SPStyleEnum const enum_text_align[] = {
260 {"start", SP_CSS_TEXT_ALIGN_START},
261 {"end", SP_CSS_TEXT_ALIGN_END},
262 {"left", SP_CSS_TEXT_ALIGN_LEFT},
263 {"right", SP_CSS_TEXT_ALIGN_RIGHT},
264 {"center", SP_CSS_TEXT_ALIGN_CENTER},
265 {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
266 {NULL, -1}
267 };
269 static SPStyleEnum const enum_text_transform[] = {
270 {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
271 {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
272 {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
273 {"none", SP_CSS_TEXT_TRANSFORM_NONE},
274 {NULL, -1}
275 };
277 static SPStyleEnum const enum_text_anchor[] = {
278 {"start", SP_CSS_TEXT_ANCHOR_START},
279 {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
280 {"end", SP_CSS_TEXT_ANCHOR_END},
281 {NULL, -1}
282 };
284 static SPStyleEnum const enum_direction[] = {
285 {"ltr", SP_CSS_DIRECTION_LTR},
286 {"rtl", SP_CSS_DIRECTION_RTL},
287 {NULL, -1}
288 };
290 static SPStyleEnum const enum_block_progression[] = {
291 {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
292 {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
293 {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
294 {NULL, -1}
295 };
297 static SPStyleEnum const enum_writing_mode[] = {
298 /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
299 * input file said lr. We prefer writing lr-tb on the grounds that the spec says the initial
300 * value is lr-tb rather than lr.
301 *
302 * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
303 * sharing enumerators for different strings may be a bug (once we support ecma script).
304 */
305 {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
306 {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
307 {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
308 {"lr", SP_CSS_WRITING_MODE_LR_TB},
309 {"rl", SP_CSS_WRITING_MODE_RL_TB},
310 {"tb", SP_CSS_WRITING_MODE_TB_RL},
311 {NULL, -1}
312 };
314 static SPStyleEnum const enum_visibility[] = {
315 {"hidden", SP_CSS_VISIBILITY_HIDDEN},
316 {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
317 {"visible", SP_CSS_VISIBILITY_VISIBLE},
318 {NULL, -1}
319 };
321 static SPStyleEnum const enum_overflow[] = {
322 {"visible", SP_CSS_OVERFLOW_VISIBLE},
323 {"hidden", SP_CSS_OVERFLOW_HIDDEN},
324 {"scroll", SP_CSS_OVERFLOW_SCROLL},
325 {"auto", SP_CSS_OVERFLOW_AUTO},
326 {NULL, -1}
327 };
329 static SPStyleEnum const enum_display[] = {
330 {"none", SP_CSS_DISPLAY_NONE},
331 {"inline", SP_CSS_DISPLAY_INLINE},
332 {"block", SP_CSS_DISPLAY_BLOCK},
333 {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
334 {"run-in", SP_CSS_DISPLAY_RUN_IN},
335 {"compact", SP_CSS_DISPLAY_COMPACT},
336 {"marker", SP_CSS_DISPLAY_MARKER},
337 {"table", SP_CSS_DISPLAY_TABLE},
338 {"inline-table", SP_CSS_DISPLAY_INLINE_TABLE},
339 {"table-row-group", SP_CSS_DISPLAY_TABLE_ROW_GROUP},
340 {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
341 {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
342 {"table-row", SP_CSS_DISPLAY_TABLE_ROW},
343 {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
344 {"table-column", SP_CSS_DISPLAY_TABLE_COLUMN},
345 {"table-cell", SP_CSS_DISPLAY_TABLE_CELL},
346 {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
347 {NULL, -1}
348 };
350 static SPStyleEnum const enum_shape_rendering[] = {
351 {"auto", 0},
352 {"optimizeSpeed", 0},
353 {"crispEdges", 0},
354 {"geometricPrecision", 0},
355 {NULL, -1}
356 };
358 static SPStyleEnum const enum_color_rendering[] = {
359 {"auto", 0},
360 {"optimizeSpeed", 0},
361 {"optimizeQuality", 0},
362 {NULL, -1}
363 };
365 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
367 static SPStyleEnum const enum_text_rendering[] = {
368 {"auto", 0},
369 {"optimizeSpeed", 0},
370 {"optimizeLegibility", 0},
371 {"geometricPrecision", 0},
372 {NULL, -1}
373 };
375 static SPStyleEnum const enum_enable_background[] = {
376 {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
377 {"new", SP_CSS_BACKGROUND_NEW},
378 {NULL, -1}
379 };
381 /**
382 * Release callback.
383 */
384 static void
385 sp_style_object_release(SPObject *object, SPStyle *style)
386 {
387 style->object = NULL;
388 }
393 /**
394 * Returns a new SPStyle object with settings as per sp_style_clear().
395 */
396 SPStyle *
397 sp_style_new()
398 {
399 SPStyle *const style = g_new0(SPStyle, 1);
401 style->refcount = 1;
402 style->object = NULL;
403 style->text = sp_text_style_new();
404 style->text_private = TRUE;
406 sp_style_clear(style);
408 style->cloned = false;
410 style->fill_hreffed = false;
411 style->stroke_hreffed = false;
413 new (&style->release_connection) sigc::connection();
415 new (&style->fill_release_connection) sigc::connection();
416 new (&style->fill_modified_connection) sigc::connection();
418 new (&style->stroke_release_connection) sigc::connection();
419 new (&style->stroke_modified_connection) sigc::connection();
421 return style;
422 }
425 /**
426 * Creates a new SPStyle object, and attaches it to the specified SPObject.
427 */
428 SPStyle *
429 sp_style_new_from_object(SPObject *object)
430 {
431 g_return_val_if_fail(object != NULL, NULL);
432 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
434 SPStyle *style = sp_style_new();
435 style->object = object;
436 style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
438 if (object && SP_OBJECT_IS_CLONED(object)) {
439 style->cloned = true;
440 }
442 return style;
443 }
446 /**
447 * Increase refcount of style.
448 */
449 SPStyle *
450 sp_style_ref(SPStyle *style)
451 {
452 g_return_val_if_fail(style != NULL, NULL);
453 g_return_val_if_fail(style->refcount > 0, NULL);
455 style->refcount += 1;
457 return style;
458 }
461 /**
462 * Decrease refcount of style with possible destruction.
463 */
464 SPStyle *
465 sp_style_unref(SPStyle *style)
466 {
467 g_return_val_if_fail(style != NULL, NULL);
468 g_return_val_if_fail(style->refcount > 0, NULL);
470 style->refcount -= 1;
472 if (style->refcount < 1) {
473 style->release_connection.disconnect();
474 style->release_connection.~connection();
475 if (style->text) sp_text_style_unref(style->text);
476 sp_style_paint_clear(style, &style->fill);
477 sp_style_paint_clear(style, &style->stroke);
478 style->fill_release_connection.disconnect();
479 style->fill_release_connection.~connection();
480 style->fill_modified_connection.disconnect();
481 style->fill_modified_connection.~connection();
482 style->stroke_release_connection.disconnect();
483 style->stroke_release_connection.~connection();
484 style->stroke_modified_connection.disconnect();
485 style->stroke_modified_connection.~connection();
486 g_free(style->stroke_dash.dash);
487 g_free(style);
488 }
490 return NULL;
491 }
493 /**
494 * Reads the various style parameters for an object from repr.
495 */
496 static void
497 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
498 {
499 g_assert(style != NULL);
500 g_assert(repr != NULL);
501 g_assert(!object || (SP_OBJECT_REPR(object) == repr));
503 sp_style_clear(style);
505 if (object && SP_OBJECT_IS_CLONED(object)) {
506 style->cloned = true;
507 }
509 /* 1. Style itself */
510 gchar const *val = repr->attribute("style");
511 if (val != NULL) {
512 sp_style_merge_from_style_string(style, val);
513 }
515 if (object) {
516 sp_style_merge_from_object_stylesheet(style, object);
517 } else {
518 /** \todo No stylesheet information. Find out under what circumstances
519 * this occurs, and handle accordingly. (If we really wanted to, we
520 * could probably get stylesheets by going through repr->doc.)
521 */
522 }
524 /* 2. Presentation attributes */
525 /* CSS2 */
526 SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
527 SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
528 SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
529 /* Font */
530 SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
531 SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
532 SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
533 SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
534 SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
535 /* Text (css2 chapter 16) */
536 SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
537 SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
538 if (!style->text_decoration.set) {
539 val = repr->attribute("text-decoration");
540 if (val) {
541 sp_style_read_itextdecoration(&style->text_decoration, val);
542 }
543 }
544 if (!style->line_height.set) {
545 val = repr->attribute("line-height");
546 if (val) {
547 sp_style_read_ilengthornormal(&style->line_height, val);
548 }
549 }
550 if (!style->letter_spacing.set) {
551 val = repr->attribute("letter-spacing");
552 if (val) {
553 sp_style_read_ilengthornormal(&style->letter_spacing, val);
554 }
555 }
556 if (!style->word_spacing.set) {
557 val = repr->attribute("word-spacing");
558 if (val) {
559 sp_style_read_ilengthornormal(&style->word_spacing, val);
560 }
561 }
562 SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
563 SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
564 SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
566 /* SVG */
567 SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
568 enum_writing_mode, true);
569 SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
570 enum_text_anchor, true);
572 /* opacity */
573 if (!style->opacity.set) {
574 val = repr->attribute("opacity");
575 if (val) {
576 sp_style_read_iscale24(&style->opacity, val);
577 }
578 }
579 /* color */
580 if (!style->color.set) {
581 val = repr->attribute("color");
582 if (val) {
583 sp_style_read_icolor(&style->color, val, style, ( object
584 ? SP_OBJECT_DOCUMENT(object)
585 : NULL ));
586 }
587 }
588 /* fill */
589 if (!style->fill.set) {
590 val = repr->attribute("fill");
591 if (val) {
592 sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
593 }
594 }
595 /* fill-opacity */
596 if (!style->fill_opacity.set) {
597 val = repr->attribute("fill-opacity");
598 if (val) {
599 sp_style_read_iscale24(&style->fill_opacity, val);
600 }
601 }
602 /* fill-rule */
603 SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
604 /* stroke */
605 if (!style->stroke.set) {
606 val = repr->attribute("stroke");
607 if (val) {
608 sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
609 }
610 }
611 SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
612 SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
613 SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
614 SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
616 /* markers */
617 if (!style->marker[SP_MARKER_LOC].set) {
618 val = repr->attribute("marker");
619 if (val) {
620 sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
621 }
622 }
623 if (!style->marker[SP_MARKER_LOC_START].set) {
624 val = repr->attribute("marker-start");
625 if (val) {
626 sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
627 }
628 }
629 if (!style->marker[SP_MARKER_LOC_MID].set) {
630 val = repr->attribute("marker-mid");
631 if (val) {
632 sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
633 }
634 }
635 if (!style->marker[SP_MARKER_LOC_END].set) {
636 val = repr->attribute("marker-end");
637 if (val) {
638 sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
639 }
640 }
642 /* stroke-opacity */
643 if (!style->stroke_opacity.set) {
644 val = repr->attribute("stroke-opacity");
645 if (val) {
646 sp_style_read_iscale24(&style->stroke_opacity, val);
647 }
648 }
649 if (!style->stroke_dasharray_set) {
650 val = repr->attribute("stroke-dasharray");
651 if (val) {
652 sp_style_read_dash(style, val);
653 }
654 }
655 if (!style->stroke_dashoffset_set) {
656 /* fixme */
657 val = repr->attribute("stroke-dashoffset");
658 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
659 style->stroke_dashoffset_set = TRUE;
660 } else {
661 style->stroke_dashoffset_set = FALSE;
662 }
663 }
665 /* font-family */
666 if (!style->text_private || !style->text->font_family.set) {
667 val = repr->attribute("font-family");
668 if (val) {
669 if (!style->text_private) sp_style_privatize_text(style);
670 sp_style_read_istring(&style->text->font_family, val);
671 css2_unescape_unquote(&style->text->font_family);
672 }
673 }
675 /* filter effects */
676 if (!style->filter.set) {
677 val = repr->attribute("filter");
678 if (val) {
679 sp_style_read_ifilter(&style->filter, val,
680 (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
681 }
682 }
683 SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
684 "enable-background", enum_enable_background, true);
686 /* 3. Merge from parent */
687 if (object) {
688 if (object->parent) {
689 sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
690 }
691 } else {
692 if (sp_repr_parent(repr)) {
693 /// \todo fixme: This is not the prettiest thing (Lauris)
694 SPStyle *parent = sp_style_new();
695 sp_style_read(parent, NULL, sp_repr_parent(repr));
696 sp_style_merge_from_parent(style, parent);
697 sp_style_unref(parent);
698 }
699 }
700 }
703 /**
704 * Read style properties from object's repr.
705 *
706 * 1. Reset existing object style
707 * 2. Load current effective object style
708 * 3. Load i attributes from immediate parent (which has to be up-to-date)
709 */
710 void
711 sp_style_read_from_object(SPStyle *style, SPObject *object)
712 {
713 g_return_if_fail(style != NULL);
714 g_return_if_fail(object != NULL);
715 g_return_if_fail(SP_IS_OBJECT(object));
717 Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
718 g_return_if_fail(repr != NULL);
720 sp_style_read(style, object, repr);
721 }
724 /**
725 * Read style properties from repr only.
726 */
727 void
728 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
729 {
730 g_return_if_fail(style != NULL);
731 g_return_if_fail(repr != NULL);
733 sp_style_read(style, NULL, repr);
734 }
738 /**
739 *
740 */
741 static void
742 sp_style_privatize_text(SPStyle *style)
743 {
744 SPTextStyle *text = style->text;
745 style->text = sp_text_style_duplicate_unset(style->text);
746 sp_text_style_unref(text);
747 style->text_private = TRUE;
748 }
751 /**
752 * Merge property into style.
753 *
754 * Should be called in order of highest to lowest precedence.
755 * E.g. for a single style string, call from the last declaration to the first,
756 * as CSS says that later declarations override earlier ones.
757 *
758 * \pre val != NULL.
759 */
760 static void
761 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
762 {
763 g_return_if_fail(val != NULL);
765 switch (id) {
766 /* CSS2 */
767 /* Font */
768 case SP_PROP_FONT_FAMILY:
769 if (!style->text_private) sp_style_privatize_text(style);
770 if (!style->text->font_family.set) {
771 sp_style_read_istring(&style->text->font_family, val);
772 css2_unescape_unquote(&style->text->font_family);
773 }
774 break;
775 case SP_PROP_FONT_SIZE:
776 SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
777 break;
778 case SP_PROP_FONT_SIZE_ADJUST:
779 if (strcmp(val, "none") != 0) {
780 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
781 }
782 break;
783 case SP_PROP_FONT_STYLE:
784 SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
785 break;
786 case SP_PROP_FONT_VARIANT:
787 SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
788 break;
789 case SP_PROP_FONT_WEIGHT:
790 SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
791 break;
792 case SP_PROP_FONT_STRETCH:
793 SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
794 break;
795 case SP_PROP_FONT:
796 if (!style->text_private) sp_style_privatize_text(style);
797 if (!style->text->font.set) {
798 g_free(style->text->font.value);
799 style->text->font.value = g_strdup(val);
800 style->text->font.set = TRUE;
801 style->text->font.inherit = (val && !strcmp(val, "inherit"));
802 }
803 break;
804 /* Text */
805 case SP_PROP_TEXT_INDENT:
806 SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
807 break;
808 case SP_PROP_TEXT_ALIGN:
809 SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
810 break;
811 case SP_PROP_TEXT_DECORATION:
812 if (!style->text_decoration.set) {
813 sp_style_read_itextdecoration(&style->text_decoration, val);
814 }
815 break;
816 case SP_PROP_LINE_HEIGHT:
817 if (!style->line_height.set) {
818 sp_style_read_ilengthornormal(&style->line_height, val);
819 }
820 break;
821 case SP_PROP_LETTER_SPACING:
822 if (!style->letter_spacing.set) {
823 sp_style_read_ilengthornormal(&style->letter_spacing, val);
824 }
825 break;
826 case SP_PROP_WORD_SPACING:
827 if (!style->word_spacing.set) {
828 sp_style_read_ilengthornormal(&style->word_spacing, val);
829 }
830 break;
831 case SP_PROP_TEXT_TRANSFORM:
832 SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
833 break;
834 /* Text (css3) */
835 case SP_PROP_DIRECTION:
836 SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
837 break;
838 case SP_PROP_BLOCK_PROGRESSION:
839 SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
840 break;
841 case SP_PROP_WRITING_MODE:
842 SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
843 break;
844 case SP_PROP_TEXT_ANCHOR:
845 SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
846 break;
847 /* Text (unimplemented) */
848 case SP_PROP_TEXT_RENDERING: {
849 /* Ignore the hint. */
850 SPIEnum dummy;
851 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
852 break;
853 }
854 case SP_PROP_ALIGNMENT_BASELINE:
855 g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
856 break;
857 case SP_PROP_BASELINE_SHIFT:
858 g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
859 break;
860 case SP_PROP_DOMINANT_BASELINE:
861 g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
862 break;
863 case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
864 g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
865 break;
866 case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
867 g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
868 break;
869 case SP_PROP_KERNING:
870 g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
871 break;
872 /* Misc */
873 case SP_PROP_CLIP:
874 g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
875 break;
876 case SP_PROP_COLOR:
877 if (!style->color.set) {
878 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
879 }
880 break;
881 case SP_PROP_CURSOR:
882 g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
883 break;
884 case SP_PROP_DISPLAY:
885 SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
886 break;
887 case SP_PROP_OVERFLOW:
888 /** \todo
889 * FIXME: not supported properly yet, we just read and write it,
890 * but act as if it is always "display".
891 */
892 SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
893 break;
894 case SP_PROP_VISIBILITY:
895 SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
896 break;
897 /* SVG */
898 /* Clip/Mask */
899 case SP_PROP_CLIP_PATH:
900 g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
901 break;
902 case SP_PROP_CLIP_RULE:
903 g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
904 break;
905 case SP_PROP_MASK:
906 g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
907 break;
908 case SP_PROP_OPACITY:
909 if (!style->opacity.set) {
910 sp_style_read_iscale24(&style->opacity, val);
911 }
912 break;
913 case SP_PROP_ENABLE_BACKGROUND:
914 SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
915 enum_enable_background, true);
916 break;
917 /* Filter */
918 case SP_PROP_FILTER:
919 if (!style->filter.set && !style->filter.inherit) {
920 sp_style_read_ifilter(&style->filter, val, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
921 }
922 break;
923 case SP_PROP_FLOOD_COLOR:
924 g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
925 break;
926 case SP_PROP_FLOOD_OPACITY:
927 g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
928 break;
929 case SP_PROP_LIGHTING_COLOR:
930 g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
931 break;
932 /* Gradient */
933 case SP_PROP_STOP_COLOR:
934 g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
935 break;
936 case SP_PROP_STOP_OPACITY:
937 g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
938 break;
939 /* Interactivity */
940 case SP_PROP_POINTER_EVENTS:
941 g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
942 break;
943 /* Paint */
944 case SP_PROP_COLOR_INTERPOLATION:
945 g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
946 break;
947 case SP_PROP_COLOR_INTERPOLATION_FILTERS:
948 g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
949 break;
950 case SP_PROP_COLOR_PROFILE:
951 g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
952 break;
953 case SP_PROP_COLOR_RENDERING: {
954 /* Ignore the hint. */
955 SPIEnum dummy;
956 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
957 break;
958 }
959 case SP_PROP_FILL:
960 if (!style->fill.set) {
961 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
962 }
963 break;
964 case SP_PROP_FILL_OPACITY:
965 if (!style->fill_opacity.set) {
966 sp_style_read_iscale24(&style->fill_opacity, val);
967 }
968 break;
969 case SP_PROP_FILL_RULE:
970 if (!style->fill_rule.set) {
971 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
972 }
973 break;
974 case SP_PROP_IMAGE_RENDERING: {
975 /* Ignore the hint. */
976 SPIEnum dummy;
977 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
978 break;
979 }
980 case SP_PROP_MARKER:
981 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
982 /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
983 marker_status("Setting SP_PROP_MARKER");
984 if (!style->marker[SP_MARKER_LOC].set) {
985 g_free(style->marker[SP_MARKER_LOC].value);
986 style->marker[SP_MARKER_LOC].value = g_strdup(val);
987 style->marker[SP_MARKER_LOC].set = TRUE;
988 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
989 }
990 break;
992 case SP_PROP_MARKER_START:
993 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
994 marker_status("Setting SP_PROP_MARKER_START");
995 if (!style->marker[SP_MARKER_LOC_START].set) {
996 g_free(style->marker[SP_MARKER_LOC_START].value);
997 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
998 style->marker[SP_MARKER_LOC_START].set = TRUE;
999 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1000 }
1001 break;
1002 case SP_PROP_MARKER_MID:
1003 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1004 marker_status("Setting SP_PROP_MARKER_MID");
1005 if (!style->marker[SP_MARKER_LOC_MID].set) {
1006 g_free(style->marker[SP_MARKER_LOC_MID].value);
1007 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1008 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1009 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1010 }
1011 break;
1012 case SP_PROP_MARKER_END:
1013 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1014 marker_status("Setting SP_PROP_MARKER_END");
1015 if (!style->marker[SP_MARKER_LOC_END].set) {
1016 g_free(style->marker[SP_MARKER_LOC_END].value);
1017 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1018 style->marker[SP_MARKER_LOC_END].set = TRUE;
1019 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1020 }
1021 break;
1023 case SP_PROP_SHAPE_RENDERING: {
1024 /* Ignore the hint. */
1025 SPIEnum dummy;
1026 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1027 break;
1028 }
1030 case SP_PROP_STROKE:
1031 if (!style->stroke.set) {
1032 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1033 }
1034 break;
1035 case SP_PROP_STROKE_WIDTH:
1036 SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1037 break;
1038 case SP_PROP_STROKE_DASHARRAY:
1039 if (!style->stroke_dasharray_set) {
1040 sp_style_read_dash(style, val);
1041 }
1042 break;
1043 case SP_PROP_STROKE_DASHOFFSET:
1044 if (!style->stroke_dashoffset_set) {
1045 /* fixme */
1046 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1047 style->stroke_dashoffset_set = TRUE;
1048 } else {
1049 style->stroke_dashoffset_set = FALSE;
1050 }
1051 }
1052 break;
1053 case SP_PROP_STROKE_LINECAP:
1054 if (!style->stroke_linecap.set) {
1055 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1056 }
1057 break;
1058 case SP_PROP_STROKE_LINEJOIN:
1059 if (!style->stroke_linejoin.set) {
1060 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1061 }
1062 break;
1063 case SP_PROP_STROKE_MITERLIMIT:
1064 if (!style->stroke_miterlimit.set) {
1065 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1066 }
1067 break;
1068 case SP_PROP_STROKE_OPACITY:
1069 if (!style->stroke_opacity.set) {
1070 sp_style_read_iscale24(&style->stroke_opacity, val);
1071 }
1072 break;
1074 default:
1075 g_warning("Invalid style property id: %d value: %s", id, val);
1076 break;
1077 }
1078 }
1080 static void
1081 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1082 {
1083 /** \todo Ensure that property is lcased, as per
1084 * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1085 * Should probably be done in libcroco.
1086 */
1087 unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1088 if (prop_idx != SP_ATTR_INVALID) {
1089 /** \todo
1090 * effic: Test whether the property is already set before trying to
1091 * convert to string. Alternatively, set from CRTerm directly rather
1092 * than converting to string.
1093 */
1094 guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1095 gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1096 sp_style_merge_property(style, prop_idx, str_value);
1097 g_free(str_value);
1098 }
1099 }
1101 static void
1102 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1103 {
1104 #if 0 /* forwards */
1105 for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1106 CRDeclaration *decl = NULL;
1107 cr_prop_list_get_decl(cur, &decl);
1108 sp_style_merge_style_from_decl(style, decl);
1109 }
1110 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1111 if (props) {
1112 sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1113 CRDeclaration *decl = NULL;
1114 cr_prop_list_get_decl(props, &decl);
1115 sp_style_merge_style_from_decl(style, decl);
1116 }
1117 #endif
1118 }
1120 /**
1121 * \pre decl_list != NULL
1122 */
1123 static void
1124 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1125 {
1126 if (decl_list->next) {
1127 sp_style_merge_from_decl_list(style, decl_list->next);
1128 }
1129 sp_style_merge_style_from_decl(style, decl_list);
1130 }
1132 static CRSelEng *
1133 sp_repr_sel_eng()
1134 {
1135 CRSelEng *const ret = cr_sel_eng_new();
1136 cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1138 /** \todo
1139 * Check whether we need to register any pseudo-class handlers.
1140 * libcroco has its own default handlers for first-child and lang.
1141 *
1142 * We probably want handlers for link and arguably visited (though
1143 * inkscape can't visit links at the time of writing). hover etc.
1144 * more useful in inkview than the editor inkscape.
1145 *
1146 * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1147 * the following should be honoured, at least by inkview:
1148 * :hover, :active, :focus, :visited, :link.
1149 */
1151 g_assert(ret);
1152 return ret;
1153 }
1155 static void
1156 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1157 {
1158 static CRSelEng *sel_eng = NULL;
1159 if (!sel_eng) {
1160 sel_eng = sp_repr_sel_eng();
1161 }
1163 CRPropList *props = NULL;
1164 CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1165 object->document->style_cascade,
1166 object->repr,
1167 &props);
1168 g_return_if_fail(status == CR_OK);
1169 /// \todo Check what errors can occur, and handle them properly.
1170 if (props) {
1171 sp_style_merge_from_props(style, props);
1172 cr_prop_list_destroy(props);
1173 }
1174 }
1176 /**
1177 * Parses a style="..." string and merges it with an existing SPStyle.
1178 */
1179 void
1180 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1181 {
1182 /*
1183 * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1184 * ``When CSS styling is used, CSS inline style is specified by including
1185 * semicolon-separated property declarations of the form "name : value"
1186 * within the style attribute''.
1187 *
1188 * That's fairly ambiguous. Is a `value' allowed to contain semicolons?
1189 * Why does it say "including", what else is allowed in the style
1190 * attribute value?
1191 */
1193 CRDeclaration *const decl_list
1194 = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1195 if (decl_list) {
1196 sp_style_merge_from_decl_list(style, decl_list);
1197 cr_declaration_destroy(decl_list);
1198 }
1199 }
1201 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1202 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1204 static void
1205 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1206 {
1207 /* 'font-size' */
1208 if (!child.set || child.inherit) {
1209 /* Inherit the computed value. Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1210 child.computed = parent.computed;
1211 } else if (child.type == SP_FONT_SIZE_LITERAL) {
1212 /** \todo
1213 * fixme: SVG and CSS do not specify clearly, whether we should use
1214 * user or screen coordinates (Lauris)
1215 */
1216 if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1217 child.computed = font_size_table[child.value];
1218 } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1219 child.computed = parent.computed / 1.2;
1220 } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1221 child.computed = parent.computed * 1.2;
1222 } else {
1223 /* Illegal value */
1224 }
1225 } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1226 /* Unlike most other lengths, percentage for font size is relative to parent computed value
1227 * rather than viewport. */
1228 child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1229 }
1230 }
1232 /**
1233 * Sets computed values in \a style, which may involve inheriting from (or in some other way
1234 * calculating from) corresponding computed values of \a parent.
1235 *
1236 * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1237 * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1238 * inherit a value. http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1239 * about what the computed value is (not obvious for lengths).
1240 *
1241 * \pre \a parent's computed values are already up-to-date.
1242 */
1243 void
1244 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1245 {
1246 g_return_if_fail(style != NULL);
1248 /** \todo
1249 * fixme: Check for existing callers that might pass null parent.
1250 * This should probably be g_return_if_fail, or else we should make a
1251 * best attempt to set computed values correctly without having a parent
1252 * (i.e., by assuming parent has initial values).
1253 */
1254 if (!parent)
1255 return;
1257 /* CSS2 */
1258 /* Font */
1259 sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1261 /* 'font-style' */
1262 if (!style->font_style.set || style->font_style.inherit) {
1263 style->font_style.computed = parent->font_style.computed;
1264 }
1266 /* 'font-variant' */
1267 if (!style->font_variant.set || style->font_variant.inherit) {
1268 style->font_variant.computed = parent->font_variant.computed;
1269 }
1271 /* 'font-weight' */
1272 if (!style->font_weight.set || style->font_weight.inherit) {
1273 style->font_weight.computed = parent->font_weight.computed;
1274 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1275 /** \todo
1276 * fixme: This is unconditional, i.e., happens even if parent not
1277 * present.
1278 */
1279 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1280 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1281 style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1282 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1283 unsigned const parent_val = parent->font_weight.computed;
1284 g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1285 // strictly, 'bolder' and 'lighter' should go to the next weight
1286 // expressible in the current font family, but that's difficult to
1287 // find out, so jumping by 3 seems an appropriate approximation
1288 style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1289 ? (unsigned)SP_CSS_FONT_WEIGHT_100
1290 : parent_val - 3);
1291 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1292 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1293 unsigned const parent_val = parent->font_weight.computed;
1294 g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1295 style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1296 ? (unsigned)SP_CSS_FONT_WEIGHT_900
1297 : parent_val + 3);
1298 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1299 }
1301 /* 'font-stretch' */
1302 if (!style->font_stretch.set || style->font_stretch.inherit) {
1303 style->font_stretch.computed = parent->font_stretch.computed;
1304 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1305 unsigned const parent_val = parent->font_stretch.computed;
1306 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1307 ? parent_val
1308 : parent_val - 1);
1309 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1310 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1311 unsigned const parent_val = parent->font_stretch.computed;
1312 g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1313 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1314 ? parent_val
1315 : parent_val + 1);
1316 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1317 }
1319 /* text (css2) */
1320 if (!style->text_indent.set || style->text_indent.inherit) {
1321 style->text_indent.computed = parent->text_indent.computed;
1322 }
1324 if (!style->text_align.set || style->text_align.inherit) {
1325 style->text_align.computed = parent->text_align.computed;
1326 }
1328 if (!style->text_decoration.set || style->text_decoration.inherit) {
1329 style->text_decoration.underline = parent->text_decoration.underline;
1330 style->text_decoration.overline = parent->text_decoration.overline;
1331 style->text_decoration.line_through = parent->text_decoration.line_through;
1332 style->text_decoration.blink = parent->text_decoration.blink;
1333 }
1335 if (!style->line_height.set || style->line_height.inherit) {
1336 style->line_height.computed = parent->line_height.computed;
1337 style->line_height.normal = parent->line_height.normal;
1338 }
1340 if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1341 style->letter_spacing.computed = parent->letter_spacing.computed;
1342 style->letter_spacing.normal = parent->letter_spacing.normal;
1343 }
1345 if (!style->word_spacing.set || style->word_spacing.inherit) {
1346 style->word_spacing.computed = parent->word_spacing.computed;
1347 style->word_spacing.normal = parent->word_spacing.normal;
1348 }
1350 if (!style->text_transform.set || style->text_transform.inherit) {
1351 style->text_transform.computed = parent->text_transform.computed;
1352 }
1354 if (!style->direction.set || style->direction.inherit) {
1355 style->direction.computed = parent->direction.computed;
1356 }
1358 if (!style->block_progression.set || style->block_progression.inherit) {
1359 style->block_progression.computed = parent->block_progression.computed;
1360 }
1362 if (!style->writing_mode.set || style->writing_mode.inherit) {
1363 style->writing_mode.computed = parent->writing_mode.computed;
1364 }
1366 if (!style->text_anchor.set || style->text_anchor.inherit) {
1367 style->text_anchor.computed = parent->text_anchor.computed;
1368 }
1370 if (style->opacity.inherit) {
1371 style->opacity.value = parent->opacity.value;
1372 }
1374 /* Color */
1375 if (!style->color.set || style->color.inherit) {
1376 sp_style_merge_ipaint(style, &style->color, &parent->color);
1377 }
1379 /* Fill */
1380 if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1381 sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1382 }
1384 if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1385 style->fill_opacity.value = parent->fill_opacity.value;
1386 }
1388 if (!style->fill_rule.set || style->fill_rule.inherit) {
1389 style->fill_rule.computed = parent->fill_rule.computed;
1390 }
1392 /* Stroke */
1393 if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1394 sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1395 }
1397 if (!style->stroke_width.set || style->stroke_width.inherit) {
1398 style->stroke_width.computed = parent->stroke_width.computed;
1399 } else {
1400 /* Update computed value for any change in font inherited from parent. */
1401 double const em = style->font_size.computed;
1402 if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1403 style->stroke_width.computed = style->stroke_width.value * em;
1404 } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1405 double const ex = em * 0.5; // fixme: Get x height from libnrtype or pango.
1406 style->stroke_width.computed = style->stroke_width.value * ex;
1407 }
1408 }
1410 if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1411 style->stroke_linecap.computed = parent->stroke_linecap.computed;
1412 }
1414 if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1415 style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1416 }
1418 if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1419 style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1420 }
1422 if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1423 /** \todo
1424 * This code looks wrong. Why does the logic differ from the
1425 * above properties? Similarly dashoffset below.
1426 */
1427 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1428 if (style->stroke_dash.n_dash > 0) {
1429 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1430 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1431 }
1432 }
1434 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1435 style->stroke_dash.offset = parent->stroke_dash.offset;
1436 }
1438 if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1439 style->stroke_opacity.value = parent->stroke_opacity.value;
1440 }
1442 if (style->text && parent->text) {
1443 if (!style->text->font_family.set || style->text->font_family.inherit) {
1444 g_free(style->text->font_family.value);
1445 style->text->font_family.value = g_strdup(parent->text->font_family.value);
1446 }
1447 }
1449 /* Markers - Free the old value and make copy of the new */
1450 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1451 if (!style->marker[i].set || style->marker[i].inherit) {
1452 g_free(style->marker[i].value);
1453 style->marker[i].value = g_strdup(parent->marker[i].value);
1454 }
1455 }
1457 /* Filter effects */
1458 if(style->filter.set && style->filter.inherit) {
1459 sp_style_merge_ifilter(&style->filter, &parent->filter);
1460 }
1462 if(style->enable_background.inherit) {
1463 style->enable_background.value = parent->enable_background.value;
1464 }
1465 }
1467 template <typename T>
1468 static void
1469 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1470 {
1471 if ( ( !(child.set) || child.inherit )
1472 && parent.set && !(parent.inherit) )
1473 {
1474 child = parent;
1475 }
1476 }
1478 /**
1479 * Copy SPIString from parent to child.
1480 */
1481 static void
1482 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1483 {
1484 if ( ( !(child.set) || child.inherit )
1485 && parent.set && !(parent.inherit) )
1486 {
1487 g_free(child.value);
1488 child.value = g_strdup(parent.value);
1489 child.set = parent.set;
1490 child.inherit = parent.inherit;
1491 }
1492 }
1494 static void
1495 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1496 SPIPaint &child, SPIPaint const &parent)
1497 {
1498 /** \todo
1499 * I haven't given this much attention. See comments below about
1500 * currentColor, colorProfile, and relative URIs.
1501 */
1502 if (!child.set || child.inherit || child.currentcolor) {
1503 sp_style_merge_ipaint(style, &child, &parent);
1504 child.set = parent.set;
1505 child.inherit = parent.inherit;
1506 }
1507 }
1509 static void
1510 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1511 unsigned const max_computed_val,
1512 unsigned const smaller_val)
1513 {
1514 /* We assume that min computed val is 0, contiguous up to max_computed_val,
1515 then zero or more other absolute values, then smaller_val then larger_val. */
1516 unsigned const min_computed_val = 0;
1517 unsigned const larger_val = smaller_val + 1;
1518 g_return_if_fail(min_computed_val < max_computed_val);
1519 g_return_if_fail(max_computed_val < smaller_val);
1521 if (parent.set && !parent.inherit) {
1522 if (!child.set || child.inherit) {
1523 child.value = parent.value;
1524 child.set = parent.set; // i.e. true
1525 child.inherit = parent.inherit; // i.e. false
1526 } else if (child.value < smaller_val) {
1527 /* Child has absolute value: leave as is. */
1528 } else if ( ( child.value == smaller_val
1529 && parent.value == larger_val )
1530 || ( parent.value == smaller_val
1531 && child.value == larger_val ) )
1532 {
1533 child.set = false;
1534 /*
1535 * Note that this can result in a change in computed value in the
1536 * rare case that the parent's setting was a no-op (i.e. if the
1537 * parent's parent's computed value was already ultra-condensed or
1538 * ultra-expanded). However, I'd guess that the change is for the
1539 * better: I'd guess that if the properties were specified
1540 * relatively, then the intent is to counteract parent's effect.
1541 * I.e. I believe this is the best code even in that case.
1542 */
1543 } else if (child.value == parent.value) {
1544 /* Leave as is. */
1545 /** \todo
1546 * It's unclear what to do if style and parent specify the same
1547 * relative directive (narrower or wider). We can either convert
1548 * to absolute specification or coalesce to a single relative
1549 * request (of half the strength of the original pair).
1550 *
1551 * Converting to a single level of relative specification is a
1552 * better choice if the newly-unlinked clone is itself cloned to
1553 * other contexts (inheriting different font stretchiness): it
1554 * would preserve the effect that it will be narrower than
1555 * the inherited context in each case. The disadvantage is that
1556 * it will ~certainly affect the computed value of the
1557 * newly-unlinked clone.
1558 */
1559 } else {
1560 unsigned const parent_val = parent.computed;
1561 child.value = ( child.value == smaller_val
1562 ? ( parent_val == min_computed_val
1563 ? parent_val
1564 : parent_val - 1 )
1565 : ( parent_val == max_computed_val
1566 ? parent_val
1567 : parent_val + 1 ) );
1568 g_assert(child.value <= max_computed_val);
1569 child.inherit = false;
1570 g_assert(child.set);
1571 }
1572 }
1573 }
1575 template <typename LengthT>
1576 static void
1577 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1578 double const parent_child_em_ratio)
1579 {
1580 if ( ( !(child.set) || child.inherit )
1581 && parent.set && !(parent.inherit) )
1582 {
1583 child = parent;
1584 switch (parent.unit) {
1585 case SP_CSS_UNIT_EM:
1586 case SP_CSS_UNIT_EX:
1587 child.value *= parent_child_em_ratio;
1588 /** \todo
1589 * fixme: Have separate ex ratio parameter.
1590 * Get x height from libnrtype or pango.
1591 */
1592 if (!isFinite(child.value)) {
1593 child.value = child.computed;
1594 child.unit = SP_CSS_UNIT_NONE;
1595 }
1596 break;
1598 default:
1599 break;
1600 }
1601 }
1602 }
1604 static double
1605 get_relative_font_size_frac(SPIFontSize const &font_size)
1606 {
1607 switch (font_size.type) {
1608 case SP_FONT_SIZE_LITERAL: {
1609 switch (font_size.value) {
1610 case SP_CSS_FONT_SIZE_SMALLER:
1611 return 5.0 / 6.0;
1613 case SP_CSS_FONT_SIZE_LARGER:
1614 return 6.0 / 5.0;
1616 default:
1617 g_assert_not_reached();
1618 }
1619 }
1621 case SP_FONT_SIZE_PERCENTAGE:
1622 return SP_F8_16_TO_FLOAT(font_size.value);
1624 case SP_FONT_SIZE_LENGTH:
1625 g_assert_not_reached();
1626 }
1627 g_assert_not_reached();
1628 }
1630 /**
1631 * Combine \a style and \a parent style specifications into a single style specification that
1632 * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1633 *
1634 * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1635 * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1636 * trying to retain the same visual appearance once the parent is removed. Interesting cases are
1637 * when there is unusual interaction with the parent's value (opacity, display) or when the value
1638 * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1639 *
1640 * Doesn't update computed values of \a style. For correctness, you should subsequently call
1641 * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1642 * style was previously up-to-date wrt \a parent.
1643 *
1644 * \pre \a parent's computed values are already up-to-date.
1645 * (\a style's computed values needn't be up-to-date.)
1646 */
1647 void
1648 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1649 {
1650 /** \note
1651 * The general rule for each property is as follows:
1652 *
1653 * If style is set to an absolute value, then leave it as is.
1654 *
1655 * Otherwise (i.e. if style has a relative value):
1656 *
1657 * If parent is set to an absolute value, then set style to the computed value.
1658 *
1659 * Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1660 */
1662 /* We do font-size first, to ensure that em size is up-to-date. */
1663 /** \todo
1664 * fixme: We'll need to have more font-related things up the top once
1665 * we're getting x-height from pango or libnrtype.
1666 */
1668 /* Some things that allow relative specifications. */
1669 {
1670 /* font-size. Note that we update the computed font-size of style,
1671 to assist in em calculations later in this function. */
1672 if (parent->font_size.set && !parent->font_size.inherit) {
1673 if (!style->font_size.set || style->font_size.inherit) {
1674 /* font_size inherits the computed value, so we can use the parent value
1675 * verbatim. */
1676 style->font_size = parent->font_size;
1677 } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1678 /* Child already has absolute size (stored in computed value), so do nothing. */
1679 } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1680 && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1681 /* Child already has absolute size, but we ensure that the computed value
1682 is up-to-date. */
1683 unsigned const ix = style->font_size.value;
1684 g_assert(ix < G_N_ELEMENTS(font_size_table));
1685 style->font_size.computed = font_size_table[ix];
1686 } else {
1687 /* Child has relative size. */
1688 double const child_frac(get_relative_font_size_frac(style->font_size));
1689 style->font_size.set = true;
1690 style->font_size.inherit = false;
1691 style->font_size.computed = parent->font_size.computed * child_frac;
1693 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1694 && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1695 || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1696 {
1697 /* Absolute value. */
1698 style->font_size.type = SP_FONT_SIZE_LENGTH;
1699 /* .value is unused for SP_FONT_SIZE_LENGTH. */
1700 } else {
1701 /* Relative value. */
1702 double const parent_frac(get_relative_font_size_frac(parent->font_size));
1703 style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1704 style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1705 }
1706 }
1707 }
1709 /* 'font-stretch' */
1710 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1711 parent->font_stretch,
1712 SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1713 SP_CSS_FONT_STRETCH_NARROWER);
1715 /* font-weight */
1716 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1717 parent->font_weight,
1718 SP_CSS_FONT_WEIGHT_900,
1719 SP_CSS_FONT_WEIGHT_LIGHTER);
1720 }
1723 /* Enum values that don't have any relative settings (other than `inherit'). */
1724 {
1725 SPIEnum SPStyle::*const fields[] = {
1726 //nyi: SPStyle::clip_rule,
1727 //nyi: SPStyle::color_interpolation,
1728 //nyi: SPStyle::color_interpolation_filters,
1729 //nyi: SPStyle::color_rendering,
1730 &SPStyle::direction,
1731 &SPStyle::fill_rule,
1732 &SPStyle::font_style,
1733 &SPStyle::font_variant,
1734 //nyi: SPStyle::image_rendering,
1735 //nyi: SPStyle::pointer_events,
1736 //nyi: SPStyle::shape_rendering,
1737 &SPStyle::stroke_linecap,
1738 &SPStyle::stroke_linejoin,
1739 &SPStyle::text_anchor,
1740 //nyi: &SPStyle::text_rendering,
1741 &SPStyle::visibility,
1742 &SPStyle::writing_mode
1743 };
1745 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1746 SPIEnum SPStyle::*const fld = fields[i];
1747 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1748 }
1749 }
1751 /* A few other simple inheritance properties. */
1752 {
1753 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1754 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1755 sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1757 /** \todo
1758 * We currently treat text-decoration as if it were a simple inherited
1759 * property (fixme). This code may need changing once we do the
1760 * special fill/stroke inheritance mentioned by the spec.
1761 */
1762 sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1763 parent->text_decoration);
1765 //nyi: font-size-adjust, // <number> | none | inherit
1766 //nyi: glyph-orientation-horizontal,
1767 //nyi: glyph-orientation-vertical,
1768 }
1770 /* Properties that involve length but are easy in other respects. */
1771 {
1772 /* The difficulty with lengths is that font-relative units need adjusting if the font
1773 * varies between parent & child.
1774 *
1775 * Lengths specified in the existing child can stay as they are: its computed font
1776 * specification should stay unchanged, so em & ex lengths should continue to mean the same
1777 * size.
1778 *
1779 * Lengths specified in the dying parent in em or ex need to be scaled according to the
1780 * ratio of em or ex size between parent & child.
1781 */
1782 double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1784 SPILength SPStyle::*const lfields[] = {
1785 &SPStyle::stroke_width,
1786 &SPStyle::text_indent
1787 };
1788 for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1789 SPILength SPStyle::*fld = lfields[i];
1790 sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1791 parent->*fld,
1792 parent_child_em_ratio);
1793 }
1795 SPILengthOrNormal SPStyle::*const nfields[] = {
1796 &SPStyle::letter_spacing,
1797 &SPStyle::line_height,
1798 &SPStyle::word_spacing
1799 };
1800 for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1801 SPILengthOrNormal SPStyle::*fld = nfields[i];
1802 sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1803 parent->*fld,
1804 parent_child_em_ratio);
1805 }
1807 //nyi: &SPStyle::kerning: length or `auto'
1809 /* fixme: Move stroke-dash and stroke-dash-offset here once they
1810 can accept units. */
1811 }
1813 /* Properties that involve a URI but are easy in other respects. */
1814 {
1815 /** \todo
1816 * Could cause problems if original object was in another document
1817 * and it used a relative URL. (At the time of writing, we don't
1818 * allow hrefs to other documents, so this isn't a problem yet.)
1819 * Paint properties also allow URIs.
1820 */
1821 //nyi: cursor, // may involve change in effect, but we can't do much better
1822 //nyi: color-profile,
1824 // Markers (marker-start, marker-mid, marker-end).
1825 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1826 sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1827 }
1828 }
1830 /* CSS2 */
1831 /* Font */
1833 if (style->text && parent->text) {
1834 sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1835 parent->text->font_family);
1836 }
1838 /* Properties that don't inherit by default. Most of these need special handling. */
1839 {
1840 /*
1841 * opacity's effect is cumulative; we set the new value to the combined effect. The
1842 * default value for opacity is 1.0, not inherit. (Note that stroke-opacity and
1843 * fill-opacity are quite different from opacity, and don't need any special handling.)
1844 *
1845 * Cases:
1846 * - parent & child were each previously unset, in which case the effective
1847 * opacity value is 1.0, and style should remain unset.
1848 * - parent was previously unset (so computed opacity value of 1.0)
1849 * and child was set to inherit. The merged child should
1850 * get a value of 1.0, and shouldn't inherit (lest the new parent
1851 * has a different opacity value). Given that opacity's default
1852 * value is 1.0 (rather than inherit), we might as well have the
1853 * merged child's opacity be unset.
1854 * - parent was previously unset (so opacity 1.0), and child was set to a number.
1855 * The merged child should retain its existing settings (though it doesn't matter
1856 * if we make it unset if that number was 1.0).
1857 * - parent was inherit and child was unset. Merged child should be set to inherit.
1858 * - parent was inherit and child was inherit. (We can't in general reproduce this
1859 * effect (short of introducing a new group), but setting opacity to inherit is rare.)
1860 * If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1861 * child's value should be set to the product of the two, i.e. the square of the
1862 * inherited value, and should not be marked as inherit. (This decision assumes that it
1863 * is more important to retain the effective opacity than to retain the inheriting
1864 * effect, and assumes that the inheriting effect either isn't important enough to create
1865 * a group or isn't common enough to bother maintaining the code to create a group.) If
1866 * the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1867 * closer to maintaining the effect.
1868 * - parent was inherit and child was set to a numerical value. If the child's value
1869 * was 1.0, then the merged child should have the same settings as the parent.
1870 * If the child's value was 0, then the merged child should also be set to 0.
1871 * If the child's value was anything else, then we do the same as for the inherit/inherit
1872 * case above: have the merged child set to the product of the two opacities and not
1873 * marked as inherit, for the same reasons as for that case.
1874 * - parent was set to a value, and child was unset. The merged child should have
1875 * parent's settings.
1876 * - parent was set to a value, and child was inherit. The merged child should
1877 * be set to the product, i.e. the square of the parent's value.
1878 * - parent & child are each set to a value. The merged child should be set to the
1879 * product.
1880 */
1881 if ( !style->opacity.set
1882 || ( !style->opacity.inherit
1883 && style->opacity.value == SP_SCALE24_MAX ) )
1884 {
1885 style->opacity = parent->opacity;
1886 } else {
1887 /* Ensure that style's computed value is up-to-date. */
1888 if (style->opacity.inherit) {
1889 style->opacity.value = parent->opacity.value;
1890 }
1892 /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1893 style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1894 parent->opacity.value);
1896 style->opacity.inherit = (parent->opacity.inherit
1897 && style->opacity.inherit
1898 && (parent->opacity.value == 0 ||
1899 parent->opacity.value == SP_SCALE24_MAX));
1900 style->opacity.set = ( style->opacity.inherit
1901 || style->opacity.value < SP_SCALE24_MAX );
1902 }
1904 /* display is in principle similar to opacity, but implementation is easier. */
1905 if ( parent->display.set && !parent->display.inherit
1906 && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1907 style->display.value = SP_CSS_DISPLAY_NONE;
1908 style->display.set = true;
1909 style->display.inherit = false;
1910 } else if (style->display.inherit) {
1911 style->display.value = parent->display.value;
1912 style->display.set = parent->display.set;
1913 style->display.inherit = parent->display.inherit;
1914 } else {
1915 /* Leave as is. (display doesn't inherit by default.) */
1916 }
1918 /* enable-background - this is rather complicated, because
1919 * it is valid only when applied to container elements.
1920 * Let's check a simple case anyhow. */
1921 if (parent->enable_background.set
1922 && !parent->enable_background.inherit
1923 && style->enable_background.inherit)
1924 {
1925 style->enable_background.set = true;
1926 style->enable_background.inherit = false;
1927 style->enable_background.value = parent->enable_background.value;
1928 }
1930 /** \todo
1931 * fixme: Check that we correctly handle all properties that don't
1932 * inherit by default (as shown in
1933 * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1934 */
1935 }
1937 /* SPIPaint properties (including color). */
1938 {
1939 /** \todo
1940 * Think about the issues involved if specified as currentColor or
1941 * if specified relative to colorProfile, and if the currentColor or
1942 * colorProfile differs between parent \& child. See also comments
1943 * elsewhere in this function about URIs.
1944 */
1945 SPIPaint SPStyle::*const fields[] = {
1946 &SPStyle::color,
1947 &SPStyle::fill,
1948 &SPStyle::stroke
1949 };
1950 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1951 SPIPaint SPStyle::*const fld = fields[i];
1952 sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
1953 }
1954 }
1956 /* Things from SVG 1.2 or CSS3. */
1957 {
1958 /* Note: If we ever support setting string values for text-align then we'd need strdup
1959 * handling here. */
1960 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
1962 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
1963 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
1964 }
1966 /* Note: this will need length handling once dasharray supports units. */
1967 if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
1968 && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
1969 {
1970 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1971 if (style->stroke_dash.n_dash > 0) {
1972 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1973 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1974 }
1975 style->stroke_dasharray_set = parent->stroke_dasharray_set;
1976 style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
1977 }
1979 /* Note: this will need length handling once dasharray_offset supports units. */
1980 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1981 style->stroke_dash.offset = parent->stroke_dash.offset;
1982 style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
1983 /* fixme: we don't currently allow stroke-dashoffset to be `inherit'. TODO: Try to
1984 * represent it as a normal SPILength; though will need to do something about existing
1985 * users of stroke_dash.offset and stroke_dashoffset_set. */
1986 }
1988 /* TODO: deal with filters */
1989 }
1993 /**
1994 * Disconnects from possible fill and stroke paint servers.
1995 */
1996 static void
1997 sp_style_paint_server_release(SPObject *obj, SPStyle *style)
1998 {
1999 SPPaintServer *server=static_cast<SPPaintServer *>(obj);
2000 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2001 && (server == style->fill.value.paint.server))
2002 {
2003 sp_style_paint_clear(style, &style->fill);
2004 }
2006 if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2007 && (server == style->stroke.value.paint.server))
2008 {
2009 sp_style_paint_clear(style, &style->stroke);
2010 }
2011 }
2016 /**
2017 * Emit style modified signal on style's object if server is style's fill
2018 * or stroke paint server.
2019 */
2020 static void
2021 sp_style_paint_server_modified(SPObject *obj, guint flags, SPStyle *style)
2022 {
2023 SPPaintServer *server = static_cast<SPPaintServer *>(obj);
2024 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2025 && (server == style->fill.value.paint.server))
2026 {
2027 if (style->object) {
2028 /** \todo
2029 * fixme: I do not know, whether it is optimal - we are
2030 * forcing reread of everything (Lauris)
2031 */
2032 /** \todo
2033 * fixme: We have to use object_modified flag, because parent
2034 * flag is only available downstreams.
2035 */
2036 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2037 }
2038 } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2039 && (server == style->stroke.value.paint.server))
2040 {
2041 if (style->object) {
2042 /// \todo fixme:
2043 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2044 }
2045 } else {
2046 g_assert_not_reached();
2047 }
2048 }
2052 /**
2053 *
2054 */
2055 static void
2056 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2057 {
2058 sp_style_paint_clear(style, paint);
2060 if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2061 paint->currentcolor = TRUE;
2062 paint->type = SP_PAINT_TYPE_COLOR;
2063 sp_color_copy(&paint->value.color, &style->color.value.color);
2064 return;
2065 }
2067 paint->type = parent->type;
2068 switch (paint->type) {
2069 case SP_PAINT_TYPE_COLOR:
2070 sp_color_copy(&paint->value.color, &parent->value.color);
2071 break;
2072 case SP_PAINT_TYPE_PAINTSERVER:
2073 paint->value.paint.server = parent->value.paint.server;
2074 paint->value.paint.uri = parent->value.paint.uri;
2075 if (paint->value.paint.server) {
2076 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
2077 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2078 if (paint == &style->fill) {
2079 style->fill_hreffed = true;
2080 } else {
2081 assert(paint == &style->stroke);
2082 style->stroke_hreffed = true;
2083 }
2084 }
2085 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
2086 SPObject *server = paint->value.paint.server;
2087 sigc::connection release_connection
2088 = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
2089 sigc::connection modified_connection
2090 = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
2091 if (paint == &style->fill) {
2092 style->fill_release_connection = release_connection;
2093 style->fill_modified_connection = modified_connection;
2094 } else {
2095 assert(paint == &style->stroke);
2096 style->stroke_release_connection = release_connection;
2097 style->stroke_modified_connection = modified_connection;
2098 }
2099 }
2100 }
2101 break;
2102 case SP_PAINT_TYPE_NONE:
2103 break;
2104 default:
2105 g_assert_not_reached();
2106 break;
2107 }
2108 }
2111 /**
2112 * Merge filter style from parent.
2113 * Filter effects do not inherit by default
2114 */
2115 static void
2116 sp_style_merge_ifilter(SPIFilter *child, SPIFilter const *parent)
2117 {
2118 child->set = parent->set;
2119 child->inherit = parent->inherit;
2120 child->filter = parent->filter;
2121 child->uri = parent->uri;
2122 }
2124 /**
2125 * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2126 * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2127 * complete cascaded style to style_clipboard. When you need a CSS string
2128 * for an object in the document tree, you normally call
2129 * sp_style_write_difference instead to take into account the object's parent.
2130 *
2131 * \pre style != NULL.
2132 * \pre flags in {IFSET, ALWAYS}.
2133 * \post ret != NULL.
2134 */
2135 gchar *
2136 sp_style_write_string(SPStyle const *const style, guint const flags)
2137 {
2138 /** \todo
2139 * Merge with write_difference, much duplicate code!
2140 */
2141 g_return_val_if_fail(style != NULL, NULL);
2142 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2143 (flags == SP_STYLE_FLAG_ALWAYS) ),
2144 NULL);
2146 gchar c[BMAX];
2147 gchar *p = c;
2148 *p = '\0';
2150 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2151 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2152 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2153 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2154 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2156 /* Text */
2157 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2158 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2159 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2160 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2161 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2162 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2163 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2164 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2165 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2166 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2168 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2170 /// \todo fixme: Per type methods need default flag too (lauris)
2171 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2172 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2173 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2174 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2175 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2176 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2177 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2178 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2179 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2181 marker_status("sp_style_write_string: Writing markers");
2182 if (style->marker[SP_MARKER_LOC].set) {
2183 p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2184 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2185 p += g_snprintf(p, c + BMAX - p, "marker:none;");
2186 }
2187 if (style->marker[SP_MARKER_LOC_START].set) {
2188 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2189 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2190 p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2191 }
2192 if (style->marker[SP_MARKER_LOC_MID].set) {
2193 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2194 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2195 p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2196 }
2197 if (style->marker[SP_MARKER_LOC_END].set) {
2198 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2199 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2200 p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2201 }
2203 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2205 /** \todo fixme: */
2206 if ((flags == SP_STYLE_FLAG_ALWAYS)
2207 || style->stroke_dasharray_set)
2208 {
2209 if (style->stroke_dasharray_inherit) {
2210 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2211 } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2212 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2213 gint i;
2214 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2215 Inkscape::CSSOStringStream os;
2216 if (i) {
2217 os << ", ";
2218 }
2219 os << style->stroke_dash.dash[i];
2220 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2221 }
2222 if (p < c + BMAX) {
2223 *p++ = ';';
2224 }
2225 } else {
2226 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2227 }
2228 }
2230 /** \todo fixme: */
2231 if (style->stroke_dashoffset_set) {
2232 Inkscape::CSSOStringStream os;
2233 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2234 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2235 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2236 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2237 }
2239 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2241 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2242 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2243 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2245 /* filter: */
2246 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2248 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2250 /* fixme: */
2251 p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2253 /* Get rid of trailing `;'. */
2254 if (p != c) {
2255 --p;
2256 if (*p == ';') {
2257 *p = '\0';
2258 }
2259 }
2261 return g_strdup(c);
2262 }
2265 #define STYLE_BUF_MAX
2268 /**
2269 * Dumps style to CSS string, see sp_style_write_string()
2270 *
2271 * \pre from != NULL.
2272 * \pre to != NULL.
2273 * \post ret != NULL.
2274 */
2275 gchar *
2276 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2277 {
2278 g_return_val_if_fail(from != NULL, NULL);
2279 g_return_val_if_fail(to != NULL, NULL);
2281 gchar c[BMAX], *p = c;
2282 *p = '\0';
2284 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2285 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2286 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2287 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2288 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2290 /* Text */
2291 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2292 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2293 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2294 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2295 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2296 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2297 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2298 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2299 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2300 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2302 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2304 /// \todo fixme: Per type methods need default flag too
2305 if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2306 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2307 }
2308 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2309 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2310 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2311 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2312 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2313 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2314 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2315 &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2316 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2317 &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2318 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2319 &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2320 /** \todo fixme: */
2321 if (from->stroke_dasharray_set) {
2322 if (from->stroke_dasharray_inherit) {
2323 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2324 } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2325 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2326 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2327 Inkscape::CSSOStringStream os;
2328 if (i) {
2329 os << ", ";
2330 }
2331 os << from->stroke_dash.dash[i];
2332 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2333 }
2334 p += g_snprintf(p, c + BMAX - p, ";");
2335 }
2336 }
2337 /* fixme: */
2338 if (from->stroke_dashoffset_set) {
2339 Inkscape::CSSOStringStream os;
2340 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2341 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2342 }
2343 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2345 /* markers */
2346 marker_status("sp_style_write_difference: Writing markers");
2347 if (from->marker[SP_MARKER_LOC].value != NULL) {
2348 p += g_snprintf(p, c + BMAX - p, "marker:%s;", from->marker[SP_MARKER_LOC].value);
2349 }
2350 if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2351 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2352 }
2353 if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2354 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", from->marker[SP_MARKER_LOC_MID].value);
2355 }
2356 if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2357 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", from->marker[SP_MARKER_LOC_END].value);
2358 }
2360 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2361 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2362 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2364 /* filter: */
2365 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2367 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2369 p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2371 /** \todo
2372 * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2373 * flag is mainly only for attributes that don't handle explicit unset well.
2374 * We may need to revisit the behaviour of this routine.
2375 */
2377 /* Get rid of trailing `;'. */
2378 if (p != c) {
2379 --p;
2380 if (*p == ';') {
2381 *p = '\0';
2382 }
2383 }
2385 return g_strdup(c);
2386 }
2390 /**
2391 * Reset all style properties.
2392 */
2393 static void
2394 sp_style_clear(SPStyle *style)
2395 {
2396 g_return_if_fail(style != NULL);
2398 sp_style_paint_clear(style, &style->fill);
2399 sp_style_paint_clear(style, &style->stroke);
2400 if (style->stroke_dash.dash) {
2401 g_free(style->stroke_dash.dash);
2402 }
2404 /** \todo fixme: Do that text manipulation via parents */
2405 SPObject *object = style->object;
2406 gint const refcount = style->refcount;
2407 SPTextStyle *text = style->text;
2408 unsigned const text_private = style->text_private;
2409 memset(style, 0, sizeof(SPStyle));
2410 style->refcount = refcount;
2411 style->object = object;
2412 style->text = text;
2413 style->text_private = text_private;
2414 /* fixme: */
2415 style->text->font.set = FALSE;
2416 style->text->font_family.set = FALSE;
2418 style->font_size.set = FALSE;
2419 style->font_size.type = SP_FONT_SIZE_LITERAL;
2420 style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2421 style->font_size.computed = 12.0;
2422 style->font_style.set = FALSE;
2423 style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2424 style->font_variant.set = FALSE;
2425 style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2426 style->font_weight.set = FALSE;
2427 style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2428 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2429 style->font_stretch.set = FALSE;
2430 style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2432 /* text */
2433 style->text_indent.set = FALSE;
2434 style->text_indent.unit = SP_CSS_UNIT_NONE;
2435 style->text_indent.computed = 0.0;
2437 style->text_align.set = FALSE;
2438 style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2440 style->text_decoration.set = FALSE;
2441 style->text_decoration.underline = FALSE;
2442 style->text_decoration.overline = FALSE;
2443 style->text_decoration.line_through = FALSE;
2444 style->text_decoration.blink = FALSE;
2446 style->line_height.set = FALSE;
2447 style->line_height.unit = SP_CSS_UNIT_PERCENT;
2448 style->line_height.normal = TRUE;
2449 style->line_height.value = style->line_height.computed = 1.0;
2451 style->letter_spacing.set = FALSE;
2452 style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2453 style->letter_spacing.normal = TRUE;
2454 style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2456 style->word_spacing.set = FALSE;
2457 style->word_spacing.unit = SP_CSS_UNIT_NONE;
2458 style->word_spacing.normal = TRUE;
2459 style->word_spacing.value = style->word_spacing.computed = 0.0;
2461 style->text_transform.set = FALSE;
2462 style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2464 style->direction.set = FALSE;
2465 style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2467 style->block_progression.set = FALSE;
2468 style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2470 style->writing_mode.set = FALSE;
2471 style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2474 style->text_anchor.set = FALSE;
2475 style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2478 style->opacity.value = SP_SCALE24_MAX;
2479 style->visibility.set = FALSE;
2480 style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2481 style->display.set = FALSE;
2482 style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2483 style->overflow.set = FALSE;
2484 style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2486 style->color.type = SP_PAINT_TYPE_COLOR;
2487 sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
2489 style->fill.type = SP_PAINT_TYPE_COLOR;
2490 sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
2491 style->fill_opacity.value = SP_SCALE24_MAX;
2492 style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2494 style->stroke.type = SP_PAINT_TYPE_NONE;
2495 style->stroke.set = FALSE;
2496 sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
2497 style->stroke_opacity.value = SP_SCALE24_MAX;
2499 style->stroke_width.set = FALSE;
2500 style->stroke_width.unit = SP_CSS_UNIT_NONE;
2501 style->stroke_width.computed = 1.0;
2503 style->stroke_linecap.set = FALSE;
2504 style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2505 style->stroke_linejoin.set = FALSE;
2506 style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2508 style->stroke_miterlimit.set = FALSE;
2509 style->stroke_miterlimit.value = 4.0;
2511 style->stroke_dash.n_dash = 0;
2512 style->stroke_dash.dash = NULL;
2513 style->stroke_dash.offset = 0.0;
2515 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2516 g_free(style->marker[i].value);
2517 style->marker[i].set = FALSE;
2518 }
2520 style->filter.set = FALSE;
2521 //Are these really needed?
2522 style->filter.inherit = FALSE;
2523 style->filter.uri = NULL;
2524 style->filter.filter = FALSE;
2526 style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2527 style->enable_background.set = false;
2528 style->enable_background.inherit = false;
2529 }
2533 /**
2534 *
2535 */
2536 static void
2537 sp_style_read_dash(SPStyle *style, gchar const *str)
2538 {
2539 /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2540 style->stroke_dasharray_set = TRUE;
2542 if (strcmp(str, "inherit") == 0) {
2543 style->stroke_dasharray_inherit = true;
2544 return;
2545 }
2546 style->stroke_dasharray_inherit = false;
2548 NRVpathDash &dash = style->stroke_dash;
2549 g_free(dash.dash);
2550 dash.dash = NULL;
2552 if (strcmp(str, "none") == 0) {
2553 dash.n_dash = 0;
2554 return;
2555 }
2557 gint n_dash = 0;
2558 gdouble d[64];
2559 gchar *e = NULL;
2561 bool LineSolid=true;
2562 while (e != str && n_dash < 64) {
2563 /* TODO: Should allow <length> rather than just a unitless (px) number. */
2564 d[n_dash] = g_ascii_strtod(str, (char **) &e);
2565 if (d[n_dash] > 0.00000001)
2566 LineSolid = false;
2567 if (e != str) {
2568 n_dash += 1;
2569 str = e;
2570 }
2571 while (str && *str && !isalnum(*str)) str += 1;
2572 }
2574 if (LineSolid) {
2575 dash.n_dash = 0;
2576 return;
2577 }
2579 if (n_dash > 0) {
2580 dash.dash = g_new(gdouble, n_dash);
2581 memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2582 dash.n_dash = n_dash;
2583 }
2584 }
2587 /*#########################
2588 ## SPTextStyle operations
2589 #########################*/
2592 /**
2593 * Return new SPTextStyle object with default settings.
2594 */
2595 static SPTextStyle *
2596 sp_text_style_new()
2597 {
2598 SPTextStyle *ts = g_new0(SPTextStyle, 1);
2599 ts->refcount = 1;
2600 sp_text_style_clear(ts);
2602 ts->font.value = g_strdup("Bitstream Vera Sans");
2603 ts->font_family.value = g_strdup("Bitstream Vera Sans");
2605 return ts;
2606 }
2609 /**
2610 * Clear text style settings.
2611 */
2612 static void
2613 sp_text_style_clear(SPTextStyle *ts)
2614 {
2615 ts->font.set = FALSE;
2616 ts->font_family.set = FALSE;
2617 }
2621 /**
2622 * Reduce refcount of text style and possibly free it.
2623 */
2624 static SPTextStyle *
2625 sp_text_style_unref(SPTextStyle *st)
2626 {
2627 st->refcount -= 1;
2629 if (st->refcount < 1) {
2630 g_free(st->font.value);
2631 g_free(st->font_family.value);
2632 g_free(st);
2633 }
2635 return NULL;
2636 }
2640 /**
2641 * Return duplicate of text style.
2642 */
2643 static SPTextStyle *
2644 sp_text_style_duplicate_unset(SPTextStyle *st)
2645 {
2646 SPTextStyle *nt = g_new0(SPTextStyle, 1);
2647 nt->refcount = 1;
2649 nt->font.value = g_strdup(st->font.value);
2650 nt->font_family.value = g_strdup(st->font_family.value);
2652 return nt;
2653 }
2657 /**
2658 * Write SPTextStyle object into string.
2659 */
2660 static guint
2661 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2662 {
2663 gint d = 0;
2665 // We do not do diffing for text style
2666 if (flags == SP_STYLE_FLAG_IFDIFF)
2667 flags = SP_STYLE_FLAG_IFSET;
2669 d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2670 return d;
2671 }
2675 /* The following sp_tyle_read_* functions ignore invalid values, as per
2676 * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2677 *
2678 * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2679 * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2680 * pairs, in which case SVG's error-handling rules
2681 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2682 */
2685 /**
2686 * Set SPIFloat object from string.
2687 */
2688 static void
2689 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2690 {
2691 if (!strcmp(str, "inherit")) {
2692 val->set = TRUE;
2693 val->inherit = TRUE;
2694 } else {
2695 gfloat value;
2696 if (sp_svg_number_read_f(str, &value)) {
2697 val->set = TRUE;
2698 val->inherit = FALSE;
2699 val->value = value;
2700 }
2701 }
2702 }
2706 /**
2707 * Set SPIScale24 object from string.
2708 */
2709 static void
2710 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2711 {
2712 if (!strcmp(str, "inherit")) {
2713 val->set = TRUE;
2714 val->inherit = TRUE;
2715 } else {
2716 gfloat value;
2717 if (sp_svg_number_read_f(str, &value)) {
2718 val->set = TRUE;
2719 val->inherit = FALSE;
2720 value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
2721 val->value = SP_SCALE24_FROM_FLOAT(value);
2722 }
2723 }
2724 }
2726 /**
2727 * Reads a style value and performs lookup based on the given style value enumerations.
2728 */
2729 static void
2730 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2731 bool const can_explicitly_inherit)
2732 {
2733 if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2734 val->set = TRUE;
2735 val->inherit = TRUE;
2736 } else {
2737 for (unsigned i = 0; dict[i].key; i++) {
2738 if (!strcmp(str, dict[i].key)) {
2739 val->set = TRUE;
2740 val->inherit = FALSE;
2741 val->value = dict[i].value;
2742 /* Save copying for values not needing it */
2743 val->computed = val->value;
2744 break;
2745 }
2746 }
2747 }
2748 }
2752 /**
2753 * Set SPIString object from string.
2754 */
2755 static void
2756 sp_style_read_istring(SPIString *val, gchar const *str)
2757 {
2758 g_free(val->value);
2760 if (!strcmp(str, "inherit")) {
2761 val->set = TRUE;
2762 val->inherit = TRUE;
2763 val->value = NULL;
2764 } else {
2765 val->set = TRUE;
2766 val->inherit = FALSE;
2767 val->value = g_strdup(str);
2768 }
2769 }
2773 /**
2774 * Set SPILength object from string.
2775 */
2776 static void
2777 sp_style_read_ilength(SPILength *val, gchar const *str)
2778 {
2779 if (!strcmp(str, "inherit")) {
2780 val->set = TRUE;
2781 val->inherit = TRUE;
2782 } else {
2783 gdouble value;
2784 gchar *e;
2785 /** \todo fixme: Move this to standard place (Lauris) */
2786 value = g_ascii_strtod(str, &e);
2787 if ((gchar const *) e != str) {
2788 /** \todo
2789 * Allow the number of px per inch to vary (document preferences,
2790 * X server or whatever). E.g. don't fill in computed here, do
2791 * it at the same time as percentage units are done.
2792 */
2793 if (!*e) {
2794 /* Userspace */
2795 val->unit = SP_CSS_UNIT_NONE;
2796 val->computed = value;
2797 } else if (!strcmp(e, "px")) {
2798 /* Userspace */
2799 val->unit = SP_CSS_UNIT_PX;
2800 val->computed = value;
2801 } else if (!strcmp(e, "pt")) {
2802 /* Userspace / DEVICESCALE */
2803 val->unit = SP_CSS_UNIT_PT;
2804 val->computed = value * PX_PER_PT;
2805 } else if (!strcmp(e, "pc")) {
2806 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2807 val->unit = SP_CSS_UNIT_PC;
2808 val->computed = value * PX_PER_PT * 12;
2809 } else if (!strcmp(e, "mm")) {
2810 val->unit = SP_CSS_UNIT_MM;
2811 val->computed = value * PX_PER_MM;
2812 } else if (!strcmp(e, "cm")) {
2813 val->unit = SP_CSS_UNIT_CM;
2814 val->computed = value * PX_PER_CM;
2815 } else if (!strcmp(e, "in")) {
2816 val->unit = SP_CSS_UNIT_IN;
2817 val->computed = value * PX_PER_IN;
2818 } else if (!strcmp(e, "em")) {
2819 /* EM square */
2820 val->unit = SP_CSS_UNIT_EM;
2821 val->value = value;
2822 } else if (!strcmp(e, "ex")) {
2823 /* ex square */
2824 val->unit = SP_CSS_UNIT_EX;
2825 val->value = value;
2826 } else if (!strcmp(e, "%")) {
2827 /* Percentage */
2828 val->unit = SP_CSS_UNIT_PERCENT;
2829 val->value = value * 0.01;
2830 } else {
2831 /* Invalid */
2832 return;
2833 }
2834 val->set = TRUE;
2835 val->inherit = FALSE;
2836 }
2837 }
2838 }
2840 /**
2841 * Set SPILengthOrNormal object from string.
2842 */
2843 static void
2844 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2845 {
2846 if (!strcmp(str, "normal")) {
2847 val->set = TRUE;
2848 val->inherit = FALSE;
2849 val->normal = TRUE;
2850 val->unit = SP_CSS_UNIT_NONE;
2851 val->value = val->computed = 0.0;
2852 } else {
2853 SPILength length;
2854 sp_style_read_ilength(&length, str);
2855 val->set = length.set;
2856 val->inherit = length.inherit;
2857 val->normal = FALSE;
2858 val->unit = length.unit;
2859 val->value = length.value;
2860 val->computed = length.computed;
2861 }
2862 }
2864 /**
2865 * Set SPITextDecoration object from string.
2866 */
2867 static void
2868 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2869 {
2870 if (!strcmp(str, "inherit")) {
2871 val->set = TRUE;
2872 val->inherit = TRUE;
2873 } else if (!strcmp(str, "none")) {
2874 val->set = TRUE;
2875 val->inherit = FALSE;
2876 val->underline = FALSE;
2877 val->overline = FALSE;
2878 val->line_through = FALSE;
2879 val->blink = FALSE;
2880 } else {
2881 bool found_underline = false;
2882 bool found_overline = false;
2883 bool found_line_through = false;
2884 bool found_blink = false;
2885 for ( ; *str ; str++ ) {
2886 if (*str == ' ') continue;
2887 if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2888 found_underline = true;
2889 str += 9;
2890 } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2891 found_overline = true;
2892 str += 8;
2893 } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2894 found_line_through = true;
2895 str += 12;
2896 } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2897 found_blink = true;
2898 str += 5;
2899 } else {
2900 return; // invalid value
2901 }
2902 }
2903 if (!(found_underline || found_overline || found_line_through || found_blink)) {
2904 return; // invalid value: empty
2905 }
2906 val->set = TRUE;
2907 val->inherit = FALSE;
2908 val->underline = found_underline;
2909 val->overline = found_overline;
2910 val->line_through = found_line_through;
2911 val->blink = found_blink;
2912 }
2913 }
2915 /**
2916 * Set SPIPaint object from string containing an integer value.
2917 * \param document Ignored
2918 */
2919 static void
2920 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2921 {
2922 paint->currentcolor = FALSE; /* currentColor not a valid <color>. */
2923 if (!strcmp(str, "inherit")) {
2924 paint->set = TRUE;
2925 paint->inherit = TRUE;
2926 } else {
2927 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2928 if (rgb0 != 0xff) {
2929 paint->type = SP_PAINT_TYPE_COLOR;
2930 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2931 paint->set = TRUE;
2932 paint->inherit = FALSE;
2933 }
2934 }
2935 }
2938 /**
2939 * Set SPIPaint object from string.
2940 *
2941 * \pre paint == \&style.fill || paint == \&style.stroke.
2942 */
2943 static void
2944 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2945 {
2946 while (isspace(*str)) {
2947 ++str;
2948 }
2950 if (streq(str, "inherit")) {
2951 paint->set = TRUE;
2952 paint->inherit = TRUE;
2953 paint->currentcolor = FALSE;
2954 } else if (streq(str, "currentColor") && paint != &style->color) {
2955 paint->set = TRUE;
2956 paint->inherit = FALSE;
2957 paint->currentcolor = TRUE;
2958 } else if (streq(str, "none") && paint != &style->color) {
2959 paint->type = SP_PAINT_TYPE_NONE;
2960 paint->set = TRUE;
2961 paint->inherit = FALSE;
2962 paint->currentcolor = FALSE;
2963 } else if (strneq(str, "url", 3) && paint != &style->color) {
2964 // this is alloc'd uri, but seems to be shared with a parent
2965 // potentially, so it's not any easy g_free case...
2966 paint->value.paint.uri = extract_uri(str);
2967 if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
2968 paint->type = SP_PAINT_TYPE_NONE;
2969 return;
2970 }
2971 paint->type = SP_PAINT_TYPE_PAINTSERVER;
2972 paint->set = TRUE;
2973 paint->inherit = FALSE;
2974 paint->currentcolor = FALSE;
2975 if (document) {
2976 SPObject *ps = sp_uri_reference_resolve(document, str);
2977 if (ps && SP_IS_PAINT_SERVER(ps)) {
2978 paint->value.paint.server = SP_PAINT_SERVER(ps);
2979 if (style->object && !style->cloned) {
2980 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2981 if (paint == &style->fill) {
2982 style->fill_hreffed = true;
2983 } else {
2984 assert(paint == &style->stroke);
2985 style->stroke_hreffed = true;
2986 }
2987 }
2988 if (style->object || style->cloned) {
2989 SPObject *server = paint->value.paint.server;
2990 sigc::connection release_connection
2991 = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
2992 sigc::connection modified_connection
2993 = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
2994 if (paint == &style->fill) {
2995 style->fill_release_connection = release_connection;
2996 style->fill_modified_connection = modified_connection;
2997 } else {
2998 assert(paint == &style->stroke);
2999 style->stroke_release_connection = release_connection;
3000 style->stroke_modified_connection = modified_connection;
3001 }
3002 }
3003 } else {
3004 paint->value.paint.server = NULL;
3005 }
3006 }
3007 } else {
3008 guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3009 if (rgb0 != 0xff) {
3010 paint->type = SP_PAINT_TYPE_COLOR;
3011 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
3012 paint->set = TRUE;
3013 paint->inherit = FALSE;
3014 paint->currentcolor = FALSE;
3016 while (g_ascii_isspace(*str)) {
3017 ++str;
3018 }
3019 if (strneq(str, "icc-color(", 10)) {
3020 SVGICCColor* tmp = new SVGICCColor();
3021 if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3022 delete tmp;
3023 tmp = 0;
3024 }
3025 paint->iccColor = tmp;
3026 }
3027 }
3028 }
3029 }
3033 /**
3034 * Set SPIFontSize object from string.
3035 */
3036 static void
3037 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3038 {
3039 if (!strcmp(str, "inherit")) {
3040 val->set = TRUE;
3041 val->inherit = TRUE;
3042 } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3043 for (unsigned i = 0; enum_font_size[i].key; i++) {
3044 if (!strcmp(str, enum_font_size[i].key)) {
3045 val->set = TRUE;
3046 val->inherit = FALSE;
3047 val->type = SP_FONT_SIZE_LITERAL;
3048 val->value = enum_font_size[i].value;
3049 return;
3050 }
3051 }
3052 /* Invalid */
3053 return;
3054 } else {
3055 gdouble value;
3056 gchar *e;
3057 /* fixme: Move this to standard place (Lauris) */
3058 value = g_ascii_strtod(str, &e);
3059 if ((gchar const *) e != str) {
3060 if (!*e) {
3061 /* Userspace */
3062 } else if (!strcmp(e, "px")) {
3063 /* Userspace */
3064 } else if (!strcmp(e, "pt")) {
3065 /* Userspace * DEVICESCALE */
3066 value *= PX_PER_PT;
3067 } else if (!strcmp(e, "pc")) {
3068 /* 12pt */
3069 value *= PX_PER_PT * 12.0;
3070 } else if (!strcmp(e, "mm")) {
3071 value *= PX_PER_MM;
3072 } else if (!strcmp(e, "cm")) {
3073 value *= PX_PER_CM;
3074 } else if (!strcmp(e, "in")) {
3075 value *= PX_PER_IN;
3076 } else if (!strcmp(e, "%")) {
3077 /* Percentage */
3078 val->set = TRUE;
3079 val->inherit = FALSE;
3080 val->type = SP_FONT_SIZE_PERCENTAGE;
3081 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3082 return;
3083 } else {
3084 /* Invalid */
3085 return;
3086 }
3087 /* Length */
3088 val->set = TRUE;
3089 val->inherit = FALSE;
3090 val->type = SP_FONT_SIZE_LENGTH;
3091 val->computed = value;
3092 return;
3093 }
3094 }
3095 }
3099 /**
3100 * Set SPIFilter object from string.
3101 */
3102 static void
3103 sp_style_read_ifilter(SPIFilter *f, gchar const *str, SPDocument *document)
3104 {
3105 /* Try all possible values: inherit, none, uri */
3106 if (streq(str, "inherit")) {
3107 f->set = TRUE;
3108 f->inherit = TRUE;
3109 f->filter = NULL;
3110 } else if(streq(str, "none")) {
3111 f->set = TRUE;
3112 f->inherit = FALSE;
3113 f->filter = NULL;
3114 } else if (strneq(str, "url", 3)) {
3115 f->uri = extract_uri(str);
3116 if(f->uri == NULL || f->uri[0] == '\0') {
3117 g_warning("Specified filter url is empty");
3118 f->set = TRUE;
3119 f->inherit = FALSE;
3120 f->filter = NULL;
3121 return;
3122 }
3123 f->set = TRUE;
3124 f->inherit = FALSE;
3125 f->filter = NULL;
3126 if (document) {
3127 SPObject *obj;
3128 obj = sp_uri_reference_resolve(document, str);
3129 if (SP_IS_FILTER(obj)) {
3130 f->filter = SP_FILTER(obj);
3131 //g_signal_connect(G_OBJECT(f->filter), "release",
3132 // G_CALLBACK(sp_style_filter_release), style);
3133 //g_signal_connect(G_OBJECT(f->filter), "modified",
3134 // G_CALLBACK(sp_style_filter_modified), style);
3135 } else {
3136 g_warning("Element '%s' not found or is not a filter", f->uri);
3137 }
3138 }
3140 } else {
3141 /* We shouldn't reach this if SVG input is well-formed */
3142 f->set = FALSE;
3143 f->inherit = FALSE;
3144 f->filter = NULL;
3145 f->uri = NULL;
3146 }
3147 }
3149 /**
3150 * Set SPIEnum object from repr attribute.
3151 */
3152 static void
3153 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3154 gchar const *key, SPStyleEnum const *dict,
3155 bool const can_explicitly_inherit)
3156 {
3157 gchar const *str = repr->attribute(key);
3158 if (str) {
3159 sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3160 }
3161 }
3165 /**
3166 * Set SPILength object from repr attribute.
3167 */
3168 static void
3169 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3170 {
3171 gchar const *str = repr->attribute(key);
3172 if (str) {
3173 sp_style_read_ilength(val, str);
3174 }
3175 }
3177 /**
3178 * Set SPIFontSize object from repr attribute.
3179 */
3180 static void
3181 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3182 {
3183 gchar const *str = repr->attribute(key);
3184 if (str) {
3185 sp_style_read_ifontsize(val, str);
3186 }
3187 }
3190 /**
3191 * Set SPIFloat object from repr attribute.
3192 */
3193 static void
3194 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3195 {
3196 gchar const *str = repr->attribute(key);
3197 if (str) {
3198 sp_style_read_ifloat(val, str);
3199 }
3200 }
3203 /**
3204 * Write SPIFloat object into string.
3205 */
3206 static gint
3207 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3208 SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3209 {
3210 Inkscape::CSSOStringStream os;
3212 if ((flags & SP_STYLE_FLAG_ALWAYS)
3213 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3214 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3215 && (!base->set || (val->value != base->value))))
3216 {
3217 if (val->inherit) {
3218 return g_snprintf(p, len, "%s:inherit;", key);
3219 } else {
3220 os << key << ":" << val->value << ";";
3221 return g_strlcpy(p, os.str().c_str(), len);
3222 }
3223 }
3224 return 0;
3225 }
3228 /**
3229 * Write SPIScale24 object into string.
3230 */
3231 static gint
3232 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3233 SPIScale24 const *const val, SPIScale24 const *const base,
3234 guint const flags)
3235 {
3236 Inkscape::CSSOStringStream os;
3238 if ((flags & SP_STYLE_FLAG_ALWAYS)
3239 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3240 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3241 && (!base->set || (val->value != base->value))))
3242 {
3243 if (val->inherit) {
3244 return g_snprintf(p, len, "%s:inherit;", key);
3245 } else {
3246 os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3247 return g_strlcpy(p, os.str().c_str(), len);
3248 }
3249 }
3250 return 0;
3251 }
3254 /**
3255 * Write SPIEnum object into string.
3256 */
3257 static gint
3258 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3259 SPStyleEnum const *const dict,
3260 SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3261 {
3262 if ((flags & SP_STYLE_FLAG_ALWAYS)
3263 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3264 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3265 && (!base->set || (val->computed != base->computed))))
3266 {
3267 if (val->inherit) {
3268 return g_snprintf(p, len, "%s:inherit;", key);
3269 }
3270 for (unsigned i = 0; dict[i].key; i++) {
3271 if (dict[i].value == static_cast< gint > (val->value) ) {
3272 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3273 }
3274 }
3275 }
3276 return 0;
3277 }
3281 /**
3282 * Write SPIString object into string.
3283 */
3284 static gint
3285 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3286 SPIString const *const val, SPIString const *const base, guint const flags)
3287 {
3288 if ((flags & SP_STYLE_FLAG_ALWAYS)
3289 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3290 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3291 && (!base->set || strcmp(val->value, base->value))))
3292 {
3293 if (val->inherit) {
3294 return g_snprintf(p, len, "%s:inherit;", key);
3295 } else {
3296 return g_snprintf(p, len, "%s:%s;", key, val->value);
3297 }
3298 }
3299 return 0;
3300 }
3303 /**
3304 *
3305 */
3306 static bool
3307 sp_length_differ(SPILength const *const a, SPILength const *const b)
3308 {
3309 if (a->unit != b->unit) {
3310 if (a->unit == SP_CSS_UNIT_EM) return true;
3311 if (a->unit == SP_CSS_UNIT_EX) return true;
3312 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3313 if (b->unit == SP_CSS_UNIT_EM) return true;
3314 if (b->unit == SP_CSS_UNIT_EX) return true;
3315 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3316 }
3318 return (a->computed != b->computed);
3319 }
3323 /**
3324 * Write SPILength object into string.
3325 */
3326 static gint
3327 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3328 SPILength const *const val, SPILength const *const base, guint const flags)
3329 {
3330 Inkscape::CSSOStringStream os;
3332 if ((flags & SP_STYLE_FLAG_ALWAYS)
3333 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3334 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3335 && (!base->set || sp_length_differ(val, base))))
3336 {
3337 if (val->inherit) {
3338 return g_snprintf(p, len, "%s:inherit;", key);
3339 } else {
3340 switch (val->unit) {
3341 case SP_CSS_UNIT_NONE:
3342 os << key << ":" << val->computed << ";";
3343 return g_strlcpy(p, os.str().c_str(), len);
3344 break;
3345 case SP_CSS_UNIT_PX:
3346 os << key << ":" << val->computed << "px;";
3347 return g_strlcpy(p, os.str().c_str(), len);
3348 break;
3349 case SP_CSS_UNIT_PT:
3350 os << key << ":" << val->computed * PT_PER_PX << "pt;";
3351 return g_strlcpy(p, os.str().c_str(), len);
3352 break;
3353 case SP_CSS_UNIT_PC:
3354 os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3355 return g_strlcpy(p, os.str().c_str(), len);
3356 break;
3357 case SP_CSS_UNIT_MM:
3358 os << key << ":" << val->computed * MM_PER_PX << "mm;";
3359 return g_strlcpy(p, os.str().c_str(), len);
3360 break;
3361 case SP_CSS_UNIT_CM:
3362 os << key << ":" << val->computed * CM_PER_PX << "cm;";
3363 return g_strlcpy(p, os.str().c_str(), len);
3364 break;
3365 case SP_CSS_UNIT_IN:
3366 os << key << ":" << val->computed * IN_PER_PX << "in;";
3367 return g_strlcpy(p, os.str().c_str(), len);
3368 break;
3369 case SP_CSS_UNIT_EM:
3370 os << key << ":" << val->value << "em;";
3371 return g_strlcpy(p, os.str().c_str(), len);
3372 break;
3373 case SP_CSS_UNIT_EX:
3374 os << key << ":" << val->value << "ex;";
3375 return g_strlcpy(p, os.str().c_str(), len);
3376 break;
3377 case SP_CSS_UNIT_PERCENT:
3378 os << key << ":" << (val->value * 100.0) << "%;";
3379 return g_strlcpy(p, os.str().c_str(), len);
3380 break;
3381 default:
3382 /* Invalid */
3383 break;
3384 }
3385 }
3386 }
3387 return 0;
3388 }
3391 /**
3392 *
3393 */
3394 static bool
3395 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3396 {
3397 if (a->normal != b->normal) return true;
3398 if (a->normal) return false;
3400 if (a->unit != b->unit) {
3401 if (a->unit == SP_CSS_UNIT_EM) return true;
3402 if (a->unit == SP_CSS_UNIT_EX) return true;
3403 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3404 if (b->unit == SP_CSS_UNIT_EM) return true;
3405 if (b->unit == SP_CSS_UNIT_EX) return true;
3406 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3407 }
3409 return (a->computed != b->computed);
3410 }
3412 /**
3413 * Write SPILengthOrNormal object into string.
3414 */
3415 static gint
3416 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3417 SPILengthOrNormal const *const val,
3418 SPILengthOrNormal const *const base,
3419 guint const flags)
3420 {
3421 if ((flags & SP_STYLE_FLAG_ALWAYS)
3422 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3423 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3424 && (!base->set || sp_lengthornormal_differ(val, base))))
3425 {
3426 if (val->normal) {
3427 return g_snprintf(p, len, "%s:normal;", key);
3428 } else {
3429 SPILength length;
3430 length.set = val->set;
3431 length.inherit = val->inherit;
3432 length.unit = val->unit;
3433 length.value = val->value;
3434 length.computed = val->computed;
3435 return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3436 }
3437 }
3438 return 0;
3439 }
3441 /**
3442 *
3443 */
3444 static bool
3445 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3446 {
3447 return a->underline != b->underline
3448 || a->overline != b->overline
3449 || a->line_through != b->line_through
3450 || a->blink != b->blink;
3451 }
3453 /**
3454 * Write SPITextDecoration object into string.
3455 */
3456 static gint
3457 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3458 SPITextDecoration const *const val,
3459 SPITextDecoration const *const base,
3460 guint const flags)
3461 {
3462 Inkscape::CSSOStringStream os;
3464 if ((flags & SP_STYLE_FLAG_ALWAYS)
3465 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3466 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3467 && (!base->set || sp_textdecoration_differ(val, base))))
3468 {
3469 if (val->inherit) {
3470 return g_snprintf(p, len, "%s:inherit;", key);
3471 } else {
3472 os << key << ":";
3473 if (val->underline || val->overline || val->line_through || val->blink) {
3474 if (val->underline) os << " underline";
3475 if (val->overline) os << " overline";
3476 if (val->line_through) os << " line-through";
3477 if (val->blink) os << " blink";
3478 } else
3479 os << "none";
3480 os << ";";
3481 return g_strlcpy(p, os.str().c_str(), len);
3482 }
3483 }
3484 return 0;
3485 }
3487 /**
3488 *
3489 */
3490 static bool
3491 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3492 {
3493 if (a->type != b->type)
3494 return true;
3495 if (a->type == SP_PAINT_TYPE_COLOR)
3496 return !(sp_color_is_equal(&a->value.color, &b->value.color)
3497 && ((a->iccColor == b->iccColor)
3498 || (a->iccColor && b->iccColor
3499 && (a->iccColor->colorProfile == b->iccColor->colorProfile)
3500 && (a->iccColor->colors == b->iccColor->colors))));
3501 /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3502 * in the string representation. */
3503 if (a->type == SP_PAINT_TYPE_PAINTSERVER)
3504 return (a->value.paint.server != b->value.paint.server);
3505 return false;
3506 }
3510 /**
3511 * Write SPIPaint object into string.
3512 */
3513 static gint
3514 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3515 SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3516 {
3517 if ((flags & SP_STYLE_FLAG_ALWAYS)
3518 || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3519 || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3520 && (!base->set || sp_paint_differ(paint, base))))
3521 {
3522 if (paint->inherit) {
3523 return g_snprintf(b, len, "%s:inherit;", key);
3524 } else if (paint->currentcolor) {
3525 return g_snprintf(b, len, "%s:currentColor;", key);
3526 } else {
3527 switch (paint->type) {
3528 case SP_PAINT_TYPE_COLOR: {
3529 char color_buf[8];
3530 sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
3531 if (paint->iccColor) {
3532 CSSOStringStream css;
3533 css << color_buf << " icc-color(" << paint->iccColor->colorProfile;
3534 for (vector<double>::const_iterator i(paint->iccColor->colors.begin()),
3535 iEnd(paint->iccColor->colors.end());
3536 i != iEnd; ++i) {
3537 css << ", " << *i;
3538 }
3539 css << ')';
3540 return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
3541 } else {
3542 return g_snprintf(b, len, "%s:%s;", key, color_buf);
3543 }
3544 }
3545 case SP_PAINT_TYPE_PAINTSERVER:
3546 return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
3547 default:
3548 break;
3549 }
3550 return g_snprintf(b, len, "%s:none;", key);
3551 }
3552 }
3553 return 0;
3554 }
3557 /**
3558 *
3559 */
3560 static bool
3561 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3562 {
3563 if (a->type != b->type)
3564 return true;
3565 if (a->type == SP_FONT_SIZE_LENGTH) {
3566 if (a->computed != b->computed)
3567 return true;
3568 } else {
3569 if (a->value != b->value)
3570 return true;
3571 }
3572 return false;
3573 }
3576 /**
3577 * Write SPIFontSize object into string.
3578 */
3579 static gint
3580 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3581 SPIFontSize const *const val, SPIFontSize const *const base,
3582 guint const flags)
3583 {
3584 if ((flags & SP_STYLE_FLAG_ALWAYS)
3585 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3586 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3587 && (!base->set || sp_fontsize_differ(val, base))))
3588 {
3589 if (val->inherit) {
3590 return g_snprintf(p, len, "%s:inherit;", key);
3591 } else if (val->type == SP_FONT_SIZE_LITERAL) {
3592 for (unsigned i = 0; enum_font_size[i].key; i++) {
3593 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3594 return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3595 }
3596 }
3597 } else if (val->type == SP_FONT_SIZE_LENGTH) {
3598 Inkscape::CSSOStringStream os;
3599 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
3600 return g_strlcpy(p, os.str().c_str(), len);
3601 } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3602 Inkscape::CSSOStringStream os;
3603 os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3604 return g_strlcpy(p, os.str().c_str(), len);
3605 }
3606 }
3607 return 0;
3608 }
3611 /**
3612 * Write SPIFilter object into string.
3613 */
3614 static gint
3615 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3616 SPIFilter const *const val, SPIFilter const *const base,
3617 guint const flags)
3618 {
3619 if ((flags & SP_STYLE_FLAG_ALWAYS)
3620 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3621 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3622 {
3623 if (val->inherit) {
3624 return g_snprintf(p, len, "%s:inherit;", key);
3625 } else if (val->uri) {
3626 return g_snprintf(p, len, "%s:url(%s);", key, val->uri);
3627 }
3628 }
3631 return 0;
3632 }
3635 /**
3636 * Clear paint object, and disconnect style from paintserver (if present).
3637 */
3638 static void
3639 sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
3640 {
3641 if ((paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
3642 if (paint == &style->fill) {
3643 if (style->fill_hreffed) {
3644 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3645 style->fill_hreffed = false;
3646 }
3647 style->fill_release_connection.disconnect();
3648 style->fill_modified_connection.disconnect();
3649 } else {
3650 assert(paint == &style->stroke); // Only fill & stroke can have a paint server.
3651 if (style->stroke_hreffed) {
3652 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3653 style->stroke_hreffed = false;
3654 }
3655 style->stroke_release_connection.disconnect();
3656 style->stroke_modified_connection.disconnect();
3657 }
3659 paint->value.paint.server = NULL;
3660 }
3661 paint->value.paint.uri = NULL;
3662 paint->type = SP_PAINT_TYPE_NONE;
3663 delete paint->iccColor;
3664 paint->iccColor = NULL;
3665 }
3667 /**
3668 * Clear all style property attributes in object.
3669 */
3670 void
3671 sp_style_unset_property_attrs(SPObject *o)
3672 {
3673 if (!o) return;
3675 SPStyle *style = SP_OBJECT_STYLE(o);
3676 if (!style) return;
3678 Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3679 if (!repr) return;
3681 if (style->opacity.set) {
3682 repr->setAttribute("opacity", NULL);
3683 }
3684 if (style->color.set) {
3685 repr->setAttribute("color", NULL);
3686 }
3687 if (style->fill.set) {
3688 repr->setAttribute("fill", NULL);
3689 }
3690 if (style->fill_opacity.set) {
3691 repr->setAttribute("fill-opacity", NULL);
3692 }
3693 if (style->fill_rule.set) {
3694 repr->setAttribute("fill-rule", NULL);
3695 }
3696 if (style->stroke.set) {
3697 repr->setAttribute("stroke", NULL);
3698 }
3699 if (style->stroke_width.set) {
3700 repr->setAttribute("stroke-width", NULL);
3701 }
3702 if (style->stroke_linecap.set) {
3703 repr->setAttribute("stroke-linecap", NULL);
3704 }
3705 if (style->stroke_linejoin.set) {
3706 repr->setAttribute("stroke-linejoin", NULL);
3707 }
3708 if (style->marker[SP_MARKER_LOC].set) {
3709 repr->setAttribute("marker", NULL);
3710 }
3711 if (style->marker[SP_MARKER_LOC_START].set) {
3712 repr->setAttribute("marker-start", NULL);
3713 }
3714 if (style->marker[SP_MARKER_LOC_MID].set) {
3715 repr->setAttribute("marker-mid", NULL);
3716 }
3717 if (style->marker[SP_MARKER_LOC_END].set) {
3718 repr->setAttribute("marker-end", NULL);
3719 }
3720 if (style->stroke_opacity.set) {
3721 repr->setAttribute("stroke-opacity", NULL);
3722 }
3723 if (style->stroke_dasharray_set) {
3724 repr->setAttribute("stroke-dasharray", NULL);
3725 }
3726 if (style->stroke_dashoffset_set) {
3727 repr->setAttribute("stroke-dashoffset", NULL);
3728 }
3729 if (style->text_private && style->text->font_family.set) {
3730 repr->setAttribute("font-family", NULL);
3731 }
3732 if (style->text_anchor.set) {
3733 repr->setAttribute("text-anchor", NULL);
3734 }
3735 if (style->writing_mode.set) {
3736 repr->setAttribute("writing_mode", NULL);
3737 }
3738 if (style->filter.set) {
3739 repr->setAttribute("filter", NULL);
3740 }
3741 if (style->enable_background.set) {
3742 repr->setAttribute("enable-background", NULL);
3743 }
3744 }
3746 /**
3747 * \pre style != NULL.
3748 * \pre flags in {IFSET, ALWAYS}.
3749 */
3750 SPCSSAttr *
3751 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3752 {
3753 g_return_val_if_fail(style != NULL, NULL);
3754 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3755 (flags == SP_STYLE_FLAG_ALWAYS) ),
3756 NULL);
3757 gchar *style_str = sp_style_write_string(style, flags);
3758 SPCSSAttr *css = sp_repr_css_attr_new();
3759 sp_repr_css_attr_add_from_string(css, style_str);
3760 g_free(style_str);
3761 return css;
3762 }
3765 /**
3766 * \pre object != NULL
3767 * \pre flags in {IFSET, ALWAYS}.
3768 */
3769 SPCSSAttr *
3770 sp_css_attr_from_object(SPObject *object, guint const flags)
3771 {
3772 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3773 (flags == SP_STYLE_FLAG_ALWAYS) ),
3774 NULL);
3775 SPStyle const *const style = SP_OBJECT_STYLE(object);
3776 if (style == NULL)
3777 return NULL;
3778 return sp_css_attr_from_style(style, flags);
3779 }
3781 /**
3782 * Unset any text-related properties
3783 */
3784 SPCSSAttr *
3785 sp_css_attr_unset_text(SPCSSAttr *css)
3786 {
3787 sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3788 sp_repr_css_set_property(css, "font-size", NULL);
3789 sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3790 sp_repr_css_set_property(css, "font-style", NULL);
3791 sp_repr_css_set_property(css, "font-variant", NULL);
3792 sp_repr_css_set_property(css, "font-weight", NULL);
3793 sp_repr_css_set_property(css, "font-stretch", NULL);
3794 sp_repr_css_set_property(css, "font-family", NULL);
3795 sp_repr_css_set_property(css, "text-indent", NULL);
3796 sp_repr_css_set_property(css, "text-align", NULL);
3797 sp_repr_css_set_property(css, "text-decoration", NULL);
3798 sp_repr_css_set_property(css, "line-height", NULL);
3799 sp_repr_css_set_property(css, "letter-spacing", NULL);
3800 sp_repr_css_set_property(css, "word-spacing", NULL);
3801 sp_repr_css_set_property(css, "text-transform", NULL);
3802 sp_repr_css_set_property(css, "direction", NULL);
3803 sp_repr_css_set_property(css, "block-progression", NULL);
3804 sp_repr_css_set_property(css, "writing-mode", NULL);
3805 sp_repr_css_set_property(css, "text-anchor", NULL);
3806 sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3807 sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3808 sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3809 sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3811 return css;
3812 }
3814 bool
3815 is_url(char const *p)
3816 {
3817 if (p == NULL)
3818 return false;
3819 /** \todo
3820 * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3821 * in property values must start with 'url('.
3822 */
3823 return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3824 }
3826 /**
3827 * Unset any properties that contain URI values.
3828 *
3829 * Used for storing style that will be reused across documents when carrying
3830 * the referenced defs is impractical.
3831 */
3832 SPCSSAttr *
3833 sp_css_attr_unset_uris(SPCSSAttr *css)
3834 {
3835 // All properties that may hold <uri> or <paint> according to SVG 1.1
3836 if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3837 if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3838 if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3839 if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3840 if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3841 if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3842 if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3843 if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3844 if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3845 if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3847 return css;
3848 }
3850 /**
3851 * Scale a single-value property.
3852 */
3853 void
3854 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3855 double ex, bool only_with_units = false)
3856 {
3857 gchar const *w = sp_repr_css_property(css, property, NULL);
3858 if (w) {
3859 gchar *units = NULL;
3860 double wd = g_ascii_strtod(w, &units) * ex;
3861 if (w == units) {// nothing converted, non-numeric value
3862 return;
3863 }
3864 if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3865 // only_with_units, but no units found, so do nothing.
3866 return;
3867 }
3868 Inkscape::CSSOStringStream os;
3869 os << wd << units; // reattach units
3870 sp_repr_css_set_property(css, property, os.str().c_str());
3871 }
3872 }
3874 /**
3875 * Scale a list-of-values property.
3876 */
3877 void
3878 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3879 {
3880 gchar const *string = sp_repr_css_property(css, property, NULL);
3881 if (string) {
3882 Inkscape::CSSOStringStream os;
3883 gchar **a = g_strsplit(string, ",", 10000);
3884 bool first = true;
3885 for (gchar **i = a; i != NULL; i++) {
3886 gchar *w = *i;
3887 if (w == NULL)
3888 break;
3889 gchar *units = NULL;
3890 double wd = g_ascii_strtod(w, &units) * ex;
3891 if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3892 g_strfreev(a);
3893 return;
3894 }
3895 if (!first) {
3896 os << ",";
3897 }
3898 os << wd << units; // reattach units
3899 first = false;
3900 }
3901 sp_repr_css_set_property(css, property, os.str().c_str());
3902 g_strfreev(a);
3903 }
3904 }
3906 /**
3907 * Scale any properties that may hold <length> by ex.
3908 */
3909 SPCSSAttr *
3910 sp_css_attr_scale(SPCSSAttr *css, double ex)
3911 {
3912 sp_css_attr_scale_property_single(css, "baseline-shift", ex);
3913 sp_css_attr_scale_property_single(css, "stroke-width", ex);
3914 sp_css_attr_scale_property_list (css, "stroke-dasharray", ex);
3915 sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
3916 sp_css_attr_scale_property_single(css, "font-size", ex);
3917 sp_css_attr_scale_property_single(css, "kerning", ex);
3918 sp_css_attr_scale_property_single(css, "letter-spacing", ex);
3919 sp_css_attr_scale_property_single(css, "word-spacing", ex);
3920 sp_css_attr_scale_property_single(css, "line-height", ex, true);
3922 return css;
3923 }
3926 /**
3927 * Remove quotes from SPIString object value.
3928 *
3929 * \todo FIXME: now used for font family, but perhaps this should apply to
3930 * ALL strings (check CSS spec), in which case this should be part of
3931 * read_istring.
3932 */
3933 static void
3934 css2_unescape_unquote(SPIString *val)
3935 {
3936 if (val->set && val->value && strlen(val->value) >= 2) {
3938 /// \todo unescape all \-escaped chars
3940 int l = strlen(val->value);
3941 if ( ( val->value[0] == '"' && val->value[l - 1] == '"' ) ||
3942 ( val->value[0] == '\'' && val->value[l - 1] == '\'' ) ) {
3943 memcpy(val->value, val->value + 1, l - 2);
3944 val->value[l - 2] = '\0';
3945 }
3946 }
3947 }
3950 /*
3951 Local Variables:
3952 mode:c++
3953 c-file-style:"stroustrup"
3954 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3955 indent-tabs-mode:nil
3956 fill-column:99
3957 End:
3958 */
3959 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :