Code

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