Code

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