Code

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