Code

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