Code

* src/2geom/isnan.h, src/libcola/cola.cpp, src/style.cpp, src/seltrans.cpp,
[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 <cstring>
23 #include <string>
25 #include "libcroco/cr-sel-eng.h"
26 #include "xml/croco-node-iface.h"
28 #include "svg/svg.h"
29 #include "svg/svg-color.h"
30 #include "svg/svg-icc-color.h"
32 #include "display/canvas-bpath.h"
33 #include "attributes.h"
34 #include "document.h"
35 #include "extract-uri.h"
36 #include "uri-references.h"
37 #include "uri.h"
38 #include "sp-paint-server.h"
39 #include "streq.h"
40 #include "strneq.h"
41 #include "style.h"
42 #include "svg/css-ostringstream.h"
43 #include "xml/repr.h"
44 #include "unit-constants.h"
45 #include "2geom/isnan.h"
46 #include "macros.h"
48 #include "sp-filter-reference.h"
50 #include <sigc++/functors/ptr_fun.h>
51 #include <sigc++/adaptors/bind.h>
53 using Inkscape::CSSOStringStream;
54 using std::vector;
56 #define BMAX 8192
58 class SPStyleEnum;
60 /*#########################
61 ## FORWARD DECLARATIONS
62 #########################*/
63 static void sp_style_clear(SPStyle *style);
65 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
67 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
68 static void sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent);
69 static void sp_style_read_dash(SPStyle *style, gchar const *str);
71 static SPTextStyle *sp_text_style_new(void);
72 static void sp_text_style_clear(SPTextStyle *ts);
73 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
74 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
75 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
76 static void sp_style_privatize_text(SPStyle *style);
78 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
79 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
80 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
81 static void sp_style_read_istring(SPIString *val, gchar const *str);
82 static void sp_style_read_ilength(SPILength *val, gchar const *str);
83 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
84 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
85 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
86 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
87 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
88 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
90 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
91 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
92 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
93 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
95 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
96 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
97 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
98 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
99 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
100 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
101 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
102 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);
103 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);
104 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
106 static void sp_style_filter_clear(SPStyle *style);
108 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
109 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
111 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
112 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
114 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
116 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
117 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
119 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
121 struct SPStyleEnum {
122     gchar const *key;
123     gint value;
124 };
126 static SPStyleEnum const enum_fill_rule[] = {
127     {"nonzero", SP_WIND_RULE_NONZERO},
128     {"evenodd", SP_WIND_RULE_EVENODD},
129     {NULL, -1}
130 };
132 static SPStyleEnum const enum_stroke_linecap[] = {
133     {"butt", SP_STROKE_LINECAP_BUTT},
134     {"round", SP_STROKE_LINECAP_ROUND},
135     {"square", SP_STROKE_LINECAP_SQUARE},
136     {NULL, -1}
137 };
139 static SPStyleEnum const enum_stroke_linejoin[] = {
140     {"miter", SP_STROKE_LINEJOIN_MITER},
141     {"round", SP_STROKE_LINEJOIN_ROUND},
142     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
143     {NULL, -1}
144 };
146 static SPStyleEnum const enum_font_style[] = {
147     {"normal", SP_CSS_FONT_STYLE_NORMAL},
148     {"italic", SP_CSS_FONT_STYLE_ITALIC},
149     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
150     {NULL, -1}
151 };
153 static SPStyleEnum const enum_font_size[] = {
154     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
155     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
156     {"small", SP_CSS_FONT_SIZE_SMALL},
157     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
158     {"large", SP_CSS_FONT_SIZE_LARGE},
159     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
160     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
161     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
162     {"larger", SP_CSS_FONT_SIZE_LARGER},
163     {NULL, -1}
164 };
166 static SPStyleEnum const enum_font_variant[] = {
167     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
168     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
169     {NULL, -1}
170 };
172 static SPStyleEnum const enum_font_weight[] = {
173     {"100", SP_CSS_FONT_WEIGHT_100},
174     {"200", SP_CSS_FONT_WEIGHT_200},
175     {"300", SP_CSS_FONT_WEIGHT_300},
176     {"400", SP_CSS_FONT_WEIGHT_400},
177     {"500", SP_CSS_FONT_WEIGHT_500},
178     {"600", SP_CSS_FONT_WEIGHT_600},
179     {"700", SP_CSS_FONT_WEIGHT_700},
180     {"800", SP_CSS_FONT_WEIGHT_800},
181     {"900", SP_CSS_FONT_WEIGHT_900},
182     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
183     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
184     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
185     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
186     {NULL, -1}
187 };
189 static SPStyleEnum const enum_font_stretch[] = {
190     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
191     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
192     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
193     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
194     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
195     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
196     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
197     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
198     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
199     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
200     {"wider", SP_CSS_FONT_STRETCH_WIDER},
201     {NULL, -1}
202 };
204 static SPStyleEnum const enum_text_align[] = {
205     {"start", SP_CSS_TEXT_ALIGN_START},
206     {"end", SP_CSS_TEXT_ALIGN_END},
207     {"left", SP_CSS_TEXT_ALIGN_LEFT},
208     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
209     {"center", SP_CSS_TEXT_ALIGN_CENTER},
210     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
211     {NULL, -1}
212 };
214 static SPStyleEnum const enum_text_transform[] = {
215     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
216     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
217     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
218     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
219     {NULL, -1}
220 };
222 static SPStyleEnum const enum_text_anchor[] = {
223     {"start", SP_CSS_TEXT_ANCHOR_START},
224     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
225     {"end", SP_CSS_TEXT_ANCHOR_END},
226     {NULL, -1}
227 };
229 static SPStyleEnum const enum_direction[] = {
230     {"ltr", SP_CSS_DIRECTION_LTR},
231     {"rtl", SP_CSS_DIRECTION_RTL},
232     {NULL, -1}
233 };
235 static SPStyleEnum const enum_block_progression[] = {
236     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
237     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
238     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
239     {NULL, -1}
240 };
242 static SPStyleEnum const enum_writing_mode[] = {
243     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
244      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
245      * value is lr-tb rather than lr.
246      *
247      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
248      * sharing enumerators for different strings may be a bug (once we support ecma script).
249      */
250     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
251     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
252     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
253     {"lr", SP_CSS_WRITING_MODE_LR_TB},
254     {"rl", SP_CSS_WRITING_MODE_RL_TB},
255     {"tb", SP_CSS_WRITING_MODE_TB_RL},
256     {NULL, -1}
257 };
259 static SPStyleEnum const enum_visibility[] = {
260     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
261     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
262     {"visible", SP_CSS_VISIBILITY_VISIBLE},
263     {NULL, -1}
264 };
266 static SPStyleEnum const enum_overflow[] = {
267     {"visible", SP_CSS_OVERFLOW_VISIBLE},
268     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
269     {"scroll", SP_CSS_OVERFLOW_SCROLL},
270     {"auto", SP_CSS_OVERFLOW_AUTO},
271     {NULL, -1}
272 };
274 static SPStyleEnum const enum_display[] = {
275     {"none",      SP_CSS_DISPLAY_NONE},
276     {"inline",    SP_CSS_DISPLAY_INLINE},
277     {"block",     SP_CSS_DISPLAY_BLOCK},
278     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
279     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
280     {"compact",   SP_CSS_DISPLAY_COMPACT},
281     {"marker",    SP_CSS_DISPLAY_MARKER},
282     {"table",     SP_CSS_DISPLAY_TABLE},
283     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
284     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
285     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
286     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
287     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
288     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
289     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
290     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
291     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
292     {NULL, -1}
293 };
295 static SPStyleEnum const enum_shape_rendering[] = {
296     {"auto", 0},
297     {"optimizeSpeed", 0},
298     {"crispEdges", 0},
299     {"geometricPrecision", 0},
300     {NULL, -1}
301 };
303 static SPStyleEnum const enum_color_rendering[] = {
304     {"auto", 0},
305     {"optimizeSpeed", 0},
306     {"optimizeQuality", 0},
307     {NULL, -1}
308 };
310 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
312 static SPStyleEnum const enum_text_rendering[] = {
313     {"auto", 0},
314     {"optimizeSpeed", 0},
315     {"optimizeLegibility", 0},
316     {"geometricPrecision", 0},
317     {NULL, -1}
318 };
320 static SPStyleEnum const enum_enable_background[] = {
321     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
322     {"new", SP_CSS_BACKGROUND_NEW},
323     {NULL, -1}
324 };
326 /**
327  * Release callback.
328  */
329 static void
330 sp_style_object_release(SPObject *object, SPStyle *style)
332     (void)object; // TODO
333     style->object = NULL;
336 /**
337  * Emit style modified signal on style's object if the filter changed.
338  */
339 static void
340 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
342     (void)flags; // TODO
343     SPFilter *filter=static_cast<SPFilter *>(obj);
344     if (style->getFilter() == filter)
345     {
346         if (style->object) {
347             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
348         }
349     }
352 /**
353  * Gets called when the filter is (re)attached to the style
354  */
355 static void
356 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
358     if (old_ref) {
359         style->filter_modified_connection.disconnect();
360     }
361     if ( SP_IS_FILTER(ref))
362     {
363         style->filter_modified_connection = 
364            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
365     }
367     sp_style_filter_ref_modified(ref, 0, style);
370 /**
371  * Emit style modified signal on style's object if server is style's fill
372  * or stroke paint server.
373  */
374 static void
375 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
377     (void)flags; // TODO
378     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
380     if ((style->fill.isPaintserver())
381         && style->getFillPaintServer() == server)
382     {
383         if (style->object) {
384             /** \todo
385              * fixme: I do not know, whether it is optimal - we are
386              * forcing reread of everything (Lauris)
387              */
388             /** \todo
389              * fixme: We have to use object_modified flag, because parent
390              * flag is only available downstreams.
391              */
392             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
393         }
394     } else if ((style->stroke.isPaintserver())
395         && style->getStrokePaintServer() == server)
396     {
397         if (style->object) {
398             /// \todo fixme:
399             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
400         }
401     } else if (server) {
402         g_assert_not_reached();
403     }
406 /**
407  * Gets called when the paintserver is (re)attached to the style
408  */
409 static void
410 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
412     if (old_ref) {
413         style->fill_ps_modified_connection.disconnect();
414     }
415     if (SP_IS_PAINT_SERVER(ref)) {
416         style->fill_ps_modified_connection = 
417            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
418     }
420     sp_style_paint_server_ref_modified(ref, 0, style);
423 /**
424  * Gets called when the paintserver is (re)attached to the style
425  */
426 static void
427 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
429     if (old_ref) {
430         style->stroke_ps_modified_connection.disconnect();
431     }
432     if (SP_IS_PAINT_SERVER(ref)) {
433         style->stroke_ps_modified_connection = 
434           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
435     }
437     sp_style_paint_server_ref_modified(ref, 0, style);
440 /**
441  * Returns a new SPStyle object with settings as per sp_style_clear().
442  */
443 SPStyle *
444 sp_style_new(SPDocument *document)
446     SPStyle *const style = g_new0(SPStyle, 1);
448     style->refcount = 1;
449     style->object = NULL;
450     style->document = document;
451     style->text = sp_text_style_new();
452     style->text_private = TRUE;
454     sp_style_clear(style);
456     style->cloned = false;
458     new (&style->release_connection) sigc::connection();
459     new (&style->filter_modified_connection) sigc::connection();
460     new (&style->fill_ps_modified_connection) sigc::connection();
461     new (&style->stroke_ps_modified_connection) sigc::connection();
463     return style;
467 /**
468  * Creates a new SPStyle object, and attaches it to the specified SPObject.
469  */
470 SPStyle *
471 sp_style_new_from_object(SPObject *object)
473     g_return_val_if_fail(object != NULL, NULL);
474     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
476     SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
477     style->object = object;
478     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
480     if (object && SP_OBJECT_IS_CLONED(object)) {
481         style->cloned = true;
482     }
484     return style;
488 /**
489  * Increase refcount of style.
490  */
491 SPStyle *
492 sp_style_ref(SPStyle *style)
494     g_return_val_if_fail(style != NULL, NULL);
495     g_return_val_if_fail(style->refcount > 0, NULL);
497     style->refcount += 1;
499     return style;
503 /**
504  * Decrease refcount of style with possible destruction.
505  */
506 SPStyle *
507 sp_style_unref(SPStyle *style)
509     g_return_val_if_fail(style != NULL, NULL);
510     g_return_val_if_fail(style->refcount > 0, NULL);
512     style->refcount -= 1;
514     if (style->refcount < 1) {
515         style->release_connection.disconnect();
516         style->release_connection.~connection();
517         if (style->text) sp_text_style_unref(style->text);
519          if (style->fill.value.href) {
520              style->fill_ps_modified_connection.disconnect();
521              delete style->fill.value.href;
522              style->fill.value.href = NULL;
523          }
524          if (style->stroke.value.href) {
525              style->stroke_ps_modified_connection.disconnect();
526              delete style->stroke.value.href;
527              style->stroke.value.href = NULL;
528          }
529          if (style->filter.href) {
530              style->filter_modified_connection.disconnect();
531              delete style->filter.href;
532              style->filter.href = NULL;
533          }
535         style->filter_modified_connection.~connection();
536         style->fill_ps_modified_connection.~connection();
537         style->stroke_ps_modified_connection.~connection();
539         style->fill.clear();
540         style->stroke.clear();
541         sp_style_filter_clear(style);
543         g_free(style->stroke_dash.dash);
544         g_free(style);
545     }
547     return NULL;
550 /**
551  *  Reads the various style parameters for an object from repr.
552  */
553 static void
554 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
556     g_assert(style != NULL);
557     g_assert(repr != NULL);
558     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
560     sp_style_clear(style);
562     if (object && SP_OBJECT_IS_CLONED(object)) {
563         style->cloned = true;
564     }
566     /* 1. Style attribute */
567     gchar const *val = repr->attribute("style");
568     if (val != NULL) {
569         sp_style_merge_from_style_string(style, val);
570     }
572     if (object) {
573         sp_style_merge_from_object_stylesheet(style, object);
574     } else {
575         /** \todo No stylesheet information. Find out under what circumstances
576          * this occurs, and handle accordingly.  (If we really wanted to, we
577          * could probably get stylesheets by going through repr->doc.)
578          */
579     }
581     /* 2. Presentation attributes */
582     /* CSS2 */
583     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
584     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
585     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
586     /* Font */
587     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
588     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
589     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
590     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
591     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
592     /* Text (css2 chapter 16) */
593     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
594     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
595     if (!style->text_decoration.set) {
596         val = repr->attribute("text-decoration");
597         if (val) {
598             sp_style_read_itextdecoration(&style->text_decoration, val);
599         }
600     }
601     if (!style->line_height.set) {
602         val = repr->attribute("line-height");
603         if (val) {
604             sp_style_read_ilengthornormal(&style->line_height, val);
605         }
606     }
607     if (!style->letter_spacing.set) {
608         val = repr->attribute("letter-spacing");
609         if (val) {
610             sp_style_read_ilengthornormal(&style->letter_spacing, val);
611         }
612     }
613     if (!style->word_spacing.set) {
614         val = repr->attribute("word-spacing");
615         if (val) {
616             sp_style_read_ilengthornormal(&style->word_spacing, val);
617         }
618     }
619     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
620     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
621     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
623     /* SVG */
624     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
625                             enum_writing_mode, true);
626     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
627                             enum_text_anchor, true);
629     /* opacity */
630     if (!style->opacity.set) {
631         val = repr->attribute("opacity");
632         if (val) {
633             sp_style_read_iscale24(&style->opacity, val);
634         }
635     }
636     /* color */
637     if (!style->color.set) {
638         val = repr->attribute("color");
639         if (val) {
640             sp_style_read_icolor(&style->color, val, style, ( object
641                                                               ? SP_OBJECT_DOCUMENT(object)
642                                                               : NULL ));
643         }
644     }
645     /* fill */
646     if (!style->fill.set) {
647         val = repr->attribute("fill");
648         if (val) {
649             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
650         }
651     }
652     /* fill-opacity */
653     if (!style->fill_opacity.set) {
654         val = repr->attribute("fill-opacity");
655         if (val) {
656             sp_style_read_iscale24(&style->fill_opacity, val);
657         }
658     }
659     /* fill-rule */
660     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
661     /* stroke */
662     if (!style->stroke.set) {
663         val = repr->attribute("stroke");
664         if (val) {
665             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
666         }
667     }
668     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
669     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
670     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
671     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
673     /* markers */
674     if (!style->marker[SP_MARKER_LOC].set) {
675         val = repr->attribute("marker");
676         if (val) {
677             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
678         }
679     }
680     if (!style->marker[SP_MARKER_LOC_START].set) {
681         val = repr->attribute("marker-start");
682         if (val) {
683             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
684         }
685     }
686     if (!style->marker[SP_MARKER_LOC_MID].set) {
687         val = repr->attribute("marker-mid");
688         if (val) {
689             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
690         }
691     }
692     if (!style->marker[SP_MARKER_LOC_END].set) {
693         val = repr->attribute("marker-end");
694         if (val) {
695             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
696         }
697     }
699     /* stroke-opacity */
700     if (!style->stroke_opacity.set) {
701         val = repr->attribute("stroke-opacity");
702         if (val) {
703             sp_style_read_iscale24(&style->stroke_opacity, val);
704         }
705     }
706     if (!style->stroke_dasharray_set) {
707         val = repr->attribute("stroke-dasharray");
708         if (val) {
709             sp_style_read_dash(style, val);
710         }
711     }
712     if (!style->stroke_dashoffset_set) {
713         /* fixme */
714         val = repr->attribute("stroke-dashoffset");
715         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
716             style->stroke_dashoffset_set = TRUE;
717         } else {
718             style->stroke_dashoffset_set = FALSE;
719         }
720     }
721     
722     /* -inkscape-font-specification */
723     if (!style->text_private || !style->text->font_specification.set) {
724         val = repr->attribute("-inkscape-font-specification");
725         if (val) {
726             if (!style->text_private) sp_style_privatize_text(style);
727             gchar *val_unquoted = attribute_unquote(val);
728             sp_style_read_istring(&style->text->font_specification, val_unquoted);
729             if (val_unquoted) g_free (val_unquoted);
730         }
731     }
733     /* font-family */
734     if (!style->text_private || !style->text->font_family.set) {
735         val = repr->attribute("font-family");
736         if (val) {
737             if (!style->text_private) sp_style_privatize_text(style);
738             gchar *val_unquoted = attribute_unquote(val);
739             sp_style_read_istring(&style->text->font_family, val_unquoted);
740             if (val_unquoted) g_free (val_unquoted);
741         }
742     }
744     /* filter effects */
745     if (!style->filter.set) {
746         val = repr->attribute("filter");
747         if (val) {
748                       sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
749         }
750     }
751     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
752                             "enable-background", enum_enable_background, true);
754     /* 3. Merge from parent */
755     if (object) {
756         if (object->parent) {
757             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
758         }
759     } else {
760         if (sp_repr_parent(repr)) {
761             /// \todo fixme: This is not the prettiest thing (Lauris)
762             SPStyle *parent = sp_style_new(NULL);
763             sp_style_read(parent, NULL, sp_repr_parent(repr));
764             sp_style_merge_from_parent(style, parent);
765             sp_style_unref(parent);
766         }
767     }
771 /**
772  * Read style properties from object's repr.
773  *
774  * 1. Reset existing object style
775  * 2. Load current effective object style
776  * 3. Load i attributes from immediate parent (which has to be up-to-date)
777  */
778 void
779 sp_style_read_from_object(SPStyle *style, SPObject *object)
781     g_return_if_fail(style != NULL);
782     g_return_if_fail(object != NULL);
783     g_return_if_fail(SP_IS_OBJECT(object));
785     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
786     g_return_if_fail(repr != NULL);
788     sp_style_read(style, object, repr);
792 /**
793  * Read style properties from repr only.
794  */
795 void
796 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
798     g_return_if_fail(style != NULL);
799     g_return_if_fail(repr != NULL);
801     sp_style_read(style, NULL, repr);
806 /**
807  *
808  */
809 static void
810 sp_style_privatize_text(SPStyle *style)
812     SPTextStyle *text = style->text;
813     style->text = sp_text_style_duplicate_unset(style->text);
814     sp_text_style_unref(text);
815     style->text_private = TRUE;
819 /**
820  * Merge property into style.
821  *
822  * Should be called in order of highest to lowest precedence.
823  * E.g. for a single style string, call from the last declaration to the first,
824  * as CSS says that later declarations override earlier ones.
825  *
826  * \pre val != NULL.
827  */
828 static void
829 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
831     g_return_if_fail(val != NULL);
833     switch (id) {
834         case SP_PROP_INKSCAPE_FONT_SPEC:
835             if (!style->text_private) sp_style_privatize_text(style);
836             if (!style->text->font_specification.set) {
837                 gchar *val_unquoted = attribute_unquote(val);
838                 sp_style_read_istring(&style->text->font_specification, val_unquoted);
839                 if (val_unquoted) g_free (val_unquoted);
840             }
841             break;
842         /* CSS2 */
843         /* Font */
844         case SP_PROP_FONT_FAMILY:
845             if (!style->text_private) sp_style_privatize_text(style);
846             if (!style->text->font_family.set) {
847                 gchar *val_unquoted = attribute_unquote(val);
848                 sp_style_read_istring(&style->text->font_family, val_unquoted);
849                 if (val_unquoted) g_free (val_unquoted);
850             }
851             break;
852         case SP_PROP_FONT_SIZE:
853             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
854             break;
855         case SP_PROP_FONT_SIZE_ADJUST:
856             if (strcmp(val, "none") != 0) {
857                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
858             }
859             break;
860         case SP_PROP_FONT_STYLE:
861             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
862             break;
863         case SP_PROP_FONT_VARIANT:
864             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
865             break;
866         case SP_PROP_FONT_WEIGHT:
867             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
868             break;
869         case SP_PROP_FONT_STRETCH:
870             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
871             break;
872         case SP_PROP_FONT:
873             if (!style->text_private) sp_style_privatize_text(style);
874             if (!style->text->font.set) {
875                 g_free(style->text->font.value);
876                 style->text->font.value = g_strdup(val);
877                 style->text->font.set = TRUE;
878                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
879             }
880             break;
881             /* Text */
882         case SP_PROP_TEXT_INDENT:
883             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
884             break;
885         case SP_PROP_TEXT_ALIGN:
886             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
887             break;
888         case SP_PROP_TEXT_DECORATION:
889             if (!style->text_decoration.set) {
890                 sp_style_read_itextdecoration(&style->text_decoration, val);
891             }
892             break;
893         case SP_PROP_LINE_HEIGHT:
894             if (!style->line_height.set) {
895                 sp_style_read_ilengthornormal(&style->line_height, val);
896             }
897             break;
898         case SP_PROP_LETTER_SPACING:
899             if (!style->letter_spacing.set) {
900                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
901             }
902             break;
903         case SP_PROP_WORD_SPACING:
904             if (!style->word_spacing.set) {
905                 sp_style_read_ilengthornormal(&style->word_spacing, val);
906             }
907             break;
908         case SP_PROP_TEXT_TRANSFORM:
909             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
910             break;
911             /* Text (css3) */
912         case SP_PROP_DIRECTION:
913             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
914             break;
915         case SP_PROP_BLOCK_PROGRESSION:
916             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
917             break;
918         case SP_PROP_WRITING_MODE:
919             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
920             break;
921         case SP_PROP_TEXT_ANCHOR:
922             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
923             break;
924             /* Text (unimplemented) */
925         case SP_PROP_TEXT_RENDERING: {
926             /* Ignore the hint. */
927             SPIEnum dummy;
928             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
929             break;
930         }
931         case SP_PROP_ALIGNMENT_BASELINE:
932             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
933             break;
934         case SP_PROP_BASELINE_SHIFT:
935             g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
936             break;
937         case SP_PROP_DOMINANT_BASELINE:
938             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
939             break;
940         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
941             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
942             break;
943         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
944             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
945             break;
946         case SP_PROP_KERNING:
947             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
948             break;
949             /* Misc */
950         case SP_PROP_CLIP:
951             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
952             break;
953         case SP_PROP_COLOR:
954             if (!style->color.set) {
955                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
956             }
957             break;
958         case SP_PROP_CURSOR:
959             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
960             break;
961         case SP_PROP_DISPLAY:
962             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
963             break;
964         case SP_PROP_OVERFLOW:
965             /** \todo
966              * FIXME: not supported properly yet, we just read and write it,
967              * but act as if it is always "display".
968              */
969             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
970             break;
971         case SP_PROP_VISIBILITY:
972             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
973             break;
974             /* SVG */
975             /* Clip/Mask */
976         case SP_PROP_CLIP_PATH:
977             g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
978             break;
979         case SP_PROP_CLIP_RULE:
980             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
981             break;
982         case SP_PROP_MASK:
983             g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
984             break;
985         case SP_PROP_OPACITY:
986             if (!style->opacity.set) {
987                 sp_style_read_iscale24(&style->opacity, val);
988             }
989             break;
990         case SP_PROP_ENABLE_BACKGROUND:
991             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
992                                     enum_enable_background, true);
993             break;
994             /* Filter */
995         case SP_PROP_FILTER:
996             if (!style->filter.set && !style->filter.inherit) {
997                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
998             }
999             break;
1000         case SP_PROP_FLOOD_COLOR:
1001             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
1002             break;
1003         case SP_PROP_FLOOD_OPACITY:
1004             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
1005             break;
1006         case SP_PROP_LIGHTING_COLOR:
1007             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
1008             break;
1009             /* Gradient */
1010         case SP_PROP_STOP_COLOR:
1011             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
1012             break;
1013         case SP_PROP_STOP_OPACITY:
1014             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
1015             break;
1016             /* Interactivity */
1017         case SP_PROP_POINTER_EVENTS:
1018             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
1019             break;
1020             /* Paint */
1021         case SP_PROP_COLOR_INTERPOLATION:
1022             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1023             break;
1024         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1025             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1026             break;
1027         case SP_PROP_COLOR_PROFILE:
1028             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1029             break;
1030         case SP_PROP_COLOR_RENDERING: {
1031             /* Ignore the hint. */
1032             SPIEnum dummy;
1033             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1034             break;
1035         }
1036         case SP_PROP_FILL:
1037             if (!style->fill.set) {
1038                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1039             }
1040             break;
1041         case SP_PROP_FILL_OPACITY:
1042             if (!style->fill_opacity.set) {
1043                 sp_style_read_iscale24(&style->fill_opacity, val);
1044             }
1045             break;
1046         case SP_PROP_FILL_RULE:
1047             if (!style->fill_rule.set) {
1048                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1049             }
1050             break;
1051         case SP_PROP_IMAGE_RENDERING: {
1052             /* Ignore the hint. */
1053             SPIEnum dummy;
1054             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1055             break;
1056         }
1057         case SP_PROP_MARKER:
1058             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1059             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1060             if (!style->marker[SP_MARKER_LOC].set) {
1061                 g_free(style->marker[SP_MARKER_LOC].value);
1062                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1063                 style->marker[SP_MARKER_LOC].set = TRUE;
1064                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1065             }
1066             break;
1068         case SP_PROP_MARKER_START:
1069             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1070             if (!style->marker[SP_MARKER_LOC_START].set) {
1071                 g_free(style->marker[SP_MARKER_LOC_START].value);
1072                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1073                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1074                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1075             }
1076             break;
1077         case SP_PROP_MARKER_MID:
1078             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1079             if (!style->marker[SP_MARKER_LOC_MID].set) {
1080                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1081                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1082                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1083                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1084             }
1085             break;
1086         case SP_PROP_MARKER_END:
1087             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1088             if (!style->marker[SP_MARKER_LOC_END].set) {
1089                 g_free(style->marker[SP_MARKER_LOC_END].value);
1090                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1091                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1092                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1093             }
1094             break;
1096         case SP_PROP_SHAPE_RENDERING: {
1097             /* Ignore the hint. */
1098             SPIEnum dummy;
1099             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1100             break;
1101         }
1103         case SP_PROP_STROKE:
1104             if (!style->stroke.set) {
1105                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1106             }
1107             break;
1108         case SP_PROP_STROKE_WIDTH:
1109             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1110             break;
1111         case SP_PROP_STROKE_DASHARRAY:
1112             if (!style->stroke_dasharray_set) {
1113                 sp_style_read_dash(style, val);
1114             }
1115             break;
1116         case SP_PROP_STROKE_DASHOFFSET:
1117             if (!style->stroke_dashoffset_set) {
1118                 /* fixme */
1119                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1120                     style->stroke_dashoffset_set = TRUE;
1121                 } else {
1122                     style->stroke_dashoffset_set = FALSE;
1123                 }
1124             }
1125             break;
1126         case SP_PROP_STROKE_LINECAP:
1127             if (!style->stroke_linecap.set) {
1128                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1129             }
1130             break;
1131         case SP_PROP_STROKE_LINEJOIN:
1132             if (!style->stroke_linejoin.set) {
1133                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1134             }
1135             break;
1136         case SP_PROP_STROKE_MITERLIMIT:
1137             if (!style->stroke_miterlimit.set) {
1138                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1139             }
1140             break;
1141         case SP_PROP_STROKE_OPACITY:
1142             if (!style->stroke_opacity.set) {
1143                 sp_style_read_iscale24(&style->stroke_opacity, val);
1144             }
1145             break;
1147         default:
1148             g_warning("Invalid style property id: %d value: %s", id, val);
1149             break;
1150     }
1153 static void
1154 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1156     /** \todo Ensure that property is lcased, as per
1157      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1158      * Should probably be done in libcroco.
1159      */
1160     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1161     if (prop_idx != SP_ATTR_INVALID) {
1162         /** \todo
1163          * effic: Test whether the property is already set before trying to
1164          * convert to string. Alternatively, set from CRTerm directly rather
1165          * than converting to string.
1166          */
1167         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1168         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1169         sp_style_merge_property(style, prop_idx, str_value);
1170         g_free(str_value);
1171     }
1174 static void
1175 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1177 #if 0 /* forwards */
1178     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1179         CRDeclaration *decl = NULL;
1180         cr_prop_list_get_decl(cur, &decl);
1181         sp_style_merge_style_from_decl(style, decl);
1182     }
1183 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1184     if (props) {
1185         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1186         CRDeclaration *decl = NULL;
1187         cr_prop_list_get_decl(props, &decl);
1188         sp_style_merge_style_from_decl(style, decl);
1189     }
1190 #endif
1193 /**
1194  * \pre decl_list != NULL
1195  */
1196 static void
1197 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1199     // read the decls from end to start, using head recursion, so that latter declarations override
1200     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1201     // because sp_style_merge_style_from_decl only sets properties that are unset
1202     if (decl_list->next) {
1203         sp_style_merge_from_decl_list(style, decl_list->next);
1204     }
1205     sp_style_merge_style_from_decl(style, decl_list);
1208 static CRSelEng *
1209 sp_repr_sel_eng()
1211     CRSelEng *const ret = cr_sel_eng_new();
1212     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1214     /** \todo
1215      * Check whether we need to register any pseudo-class handlers.
1216      * libcroco has its own default handlers for first-child and lang.
1217      *
1218      * We probably want handlers for link and arguably visited (though
1219      * inkscape can't visit links at the time of writing).  hover etc.
1220      * more useful in inkview than the editor inkscape.
1221      *
1222      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1223      * the following should be honoured, at least by inkview:
1224      * :hover, :active, :focus, :visited, :link.
1225      */
1227     g_assert(ret);
1228     return ret;
1231 static void
1232 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1234     static CRSelEng *sel_eng = NULL;
1235     if (!sel_eng) {
1236         sel_eng = sp_repr_sel_eng();
1237     }
1239     CRPropList *props = NULL;
1240     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1241                                                                      object->document->style_cascade,
1242                                                                      object->repr,
1243                                                                      &props);
1244     g_return_if_fail(status == CR_OK);
1245     /// \todo Check what errors can occur, and handle them properly.
1246     if (props) {
1247         sp_style_merge_from_props(style, props);
1248         cr_prop_list_destroy(props);
1249     }
1252 /**
1253  * Parses a style="..." string and merges it with an existing SPStyle.
1254  */
1255 void
1256 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1258     /*
1259      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1260      * ``When CSS styling is used, CSS inline style is specified by including
1261      * semicolon-separated property declarations of the form "name : value"
1262      * within the style attribute''.
1263      *
1264      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1265      * Why does it say "including", what else is allowed in the style
1266      * attribute value?
1267      */
1269     CRDeclaration *const decl_list
1270         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1271     if (decl_list) {
1272         sp_style_merge_from_decl_list(style, decl_list);
1273         cr_declaration_destroy(decl_list);
1274     }
1277 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1278 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1280 static void
1281 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1283     /* 'font-size' */
1284     if (!child.set || child.inherit) {
1285         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1286         child.computed = parent.computed;
1287     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1288         /** \todo
1289          * fixme: SVG and CSS do not specify clearly, whether we should use
1290          * user or screen coordinates (Lauris)
1291          */
1292         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1293             child.computed = font_size_table[child.value];
1294         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1295             child.computed = parent.computed / 1.2;
1296         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1297             child.computed = parent.computed * 1.2;
1298         } else {
1299             /* Illegal value */
1300         }
1301     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1302         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1303          * rather than viewport. */
1304         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1305     }
1308 /**
1309  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1310  * calculating from) corresponding computed values of \a parent.
1311  *
1312  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1313  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1314  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1315  * about what the computed value is (not obvious for lengths).
1316  *
1317  * \pre \a parent's computed values are already up-to-date.
1318  */
1319 void
1320 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1322     g_return_if_fail(style != NULL);
1324     /** \todo
1325      * fixme: Check for existing callers that might pass null parent.
1326      * This should probably be g_return_if_fail, or else we should make a
1327      * best attempt to set computed values correctly without having a parent
1328      * (i.e., by assuming parent has initial values).
1329      */
1330     if (!parent)
1331         return;
1333     /* CSS2 */
1334     /* Font */
1335     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1337     /* 'font-style' */
1338     if (!style->font_style.set || style->font_style.inherit) {
1339         style->font_style.computed = parent->font_style.computed;
1340     }
1342     /* 'font-variant' */
1343     if (!style->font_variant.set || style->font_variant.inherit) {
1344         style->font_variant.computed = parent->font_variant.computed;
1345     }
1347     /* 'font-weight' */
1348     if (!style->font_weight.set || style->font_weight.inherit) {
1349         style->font_weight.computed = parent->font_weight.computed;
1350     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1351         /** \todo
1352          * fixme: This is unconditional, i.e., happens even if parent not
1353          * present.
1354          */
1355         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1356     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1357         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1358     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1359         unsigned const parent_val = parent->font_weight.computed;
1360         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1361         // strictly, 'bolder' and 'lighter' should go to the next weight
1362         // expressible in the current font family, but that's difficult to
1363         // find out, so jumping by 3 seems an appropriate approximation
1364         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1365                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1366                                        : parent_val - 3);
1367         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1368     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1369         unsigned const parent_val = parent->font_weight.computed;
1370         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1371         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1372                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1373                                        : parent_val + 3);
1374         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1375     }
1377     /* 'font-stretch' */
1378     if (!style->font_stretch.set || style->font_stretch.inherit) {
1379         style->font_stretch.computed = parent->font_stretch.computed;
1380     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1381         unsigned const parent_val = parent->font_stretch.computed;
1382         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1383                                         ? parent_val
1384                                         : parent_val - 1);
1385         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1386     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1387         unsigned const parent_val = parent->font_stretch.computed;
1388         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1389         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1390                                         ? parent_val
1391                                         : parent_val + 1);
1392         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1393     }
1395     /* text (css2) */
1396     if (!style->text_indent.set || style->text_indent.inherit) {
1397         style->text_indent.computed = parent->text_indent.computed;
1398     }
1400     if (!style->text_align.set || style->text_align.inherit) {
1401         style->text_align.computed = parent->text_align.computed;
1402     }
1404     if (!style->text_decoration.set || style->text_decoration.inherit) {
1405         style->text_decoration.underline = parent->text_decoration.underline;
1406         style->text_decoration.overline = parent->text_decoration.overline;
1407         style->text_decoration.line_through = parent->text_decoration.line_through;
1408         style->text_decoration.blink = parent->text_decoration.blink;
1409     }
1411     if (!style->line_height.set || style->line_height.inherit) {
1412         style->line_height.computed = parent->line_height.computed;
1413         style->line_height.normal = parent->line_height.normal;
1414     }
1416     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1417         style->letter_spacing.computed = parent->letter_spacing.computed;
1418         style->letter_spacing.normal = parent->letter_spacing.normal;
1419     }
1421     if (!style->word_spacing.set || style->word_spacing.inherit) {
1422         style->word_spacing.computed = parent->word_spacing.computed;
1423         style->word_spacing.normal = parent->word_spacing.normal;
1424     }
1426     if (!style->text_transform.set || style->text_transform.inherit) {
1427         style->text_transform.computed = parent->text_transform.computed;
1428     }
1430     if (!style->direction.set || style->direction.inherit) {
1431         style->direction.computed = parent->direction.computed;
1432     }
1434     if (!style->block_progression.set || style->block_progression.inherit) {
1435         style->block_progression.computed = parent->block_progression.computed;
1436     }
1438     if (!style->writing_mode.set || style->writing_mode.inherit) {
1439         style->writing_mode.computed = parent->writing_mode.computed;
1440     }
1442     if (!style->text_anchor.set || style->text_anchor.inherit) {
1443         style->text_anchor.computed = parent->text_anchor.computed;
1444     }
1446     if (style->opacity.inherit) {
1447         style->opacity.value = parent->opacity.value;
1448     }
1450     /* Color */
1451     if (!style->color.set || style->color.inherit) {
1452         sp_style_merge_ipaint(style, &style->color, &parent->color);
1453     }
1455     /* Fill */
1456     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1457         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1458     }
1460     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1461         style->fill_opacity.value = parent->fill_opacity.value;
1462     }
1464     if (!style->fill_rule.set || style->fill_rule.inherit) {
1465         style->fill_rule.computed = parent->fill_rule.computed;
1466     }
1468     /* Stroke */
1469     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1470         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1471     }
1473     if (!style->stroke_width.set || style->stroke_width.inherit) {
1474         style->stroke_width.computed = parent->stroke_width.computed;
1475     } else {
1476         /* Update computed value for any change in font inherited from parent. */
1477         double const em = style->font_size.computed;
1478         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1479             style->stroke_width.computed = style->stroke_width.value * em;
1480         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1481             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1482             style->stroke_width.computed = style->stroke_width.value * ex;
1483         }
1484     }
1486     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1487         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1488     }
1490     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1491         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1492     }
1494     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1495         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1496     }
1498     if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1499         /** \todo
1500          * This code looks wrong.  Why does the logic differ from the
1501          * above properties? Similarly dashoffset below.
1502          */
1503         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1504         if (style->stroke_dash.n_dash > 0) {
1505             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1506             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1507         }
1508     }
1510     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1511         style->stroke_dash.offset = parent->stroke_dash.offset;
1512     }
1514     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1515         style->stroke_opacity.value = parent->stroke_opacity.value;
1516     }
1518     if (style->text && parent->text) {
1519         if (!style->text->font_family.set || style->text->font_family.inherit) {
1520             g_free(style->text->font_family.value);
1521             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1522         }
1523     }
1525     if (style->text && parent->text) {
1526         if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1527             g_free(style->text->font_specification.value);
1528             style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1529         }
1530     }
1532     /* Markers - Free the old value and make copy of the new */
1533     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1534         if (!style->marker[i].set || style->marker[i].inherit) {
1535             g_free(style->marker[i].value);
1536             style->marker[i].value = g_strdup(parent->marker[i].value);
1537         }
1538     }
1540     /* Filter effects */
1541     if (style->filter.inherit) {
1542         sp_style_merge_ifilter(style, &parent->filter);
1543     }
1545     if(style->enable_background.inherit) {
1546         style->enable_background.value = parent->enable_background.value;
1547     }
1550 template <typename T>
1551 static void
1552 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1554     if ( ( !(child.set) || child.inherit )
1555          && parent.set && !(parent.inherit) )
1556     {
1557         child = parent;
1558     }
1561 /**
1562  * Copy SPIString from parent to child.
1563  */
1564 static void
1565 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1567     if ( ( !(child.set) || child.inherit )
1568          && parent.set && !(parent.inherit) )
1569     {
1570         g_free(child.value);
1571         child.value = g_strdup(parent.value);
1572         child.set = parent.set;
1573         child.inherit = parent.inherit;
1574     }
1577 static void
1578 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1579                                             SPIPaint &child, SPIPaint const &parent)
1581     /** \todo
1582      * I haven't given this much attention.  See comments below about
1583      * currentColor, colorProfile, and relative URIs.
1584      */
1585     if (!child.set || child.inherit) {
1586         sp_style_merge_ipaint(style, &child, &parent);
1587         child.set = parent.set;
1588         child.inherit = parent.inherit;
1589     }
1592 static void
1593 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1594                                                unsigned const max_computed_val,
1595                                                unsigned const smaller_val)
1597     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1598        then zero or more other absolute values, then smaller_val then larger_val. */
1599     unsigned const min_computed_val = 0;
1600     unsigned const larger_val = smaller_val + 1;
1601     g_return_if_fail(min_computed_val < max_computed_val);
1602     g_return_if_fail(max_computed_val < smaller_val);
1604     if (parent.set && !parent.inherit) {
1605         if (!child.set || child.inherit) {
1606             child.value = parent.value;
1607             child.set = parent.set;  // i.e. true
1608             child.inherit = parent.inherit;  // i.e. false
1609         } else if (child.value < smaller_val) {
1610             /* Child has absolute value: leave as is. */
1611         } else if ( ( child.value == smaller_val
1612                       && parent.value == larger_val )
1613                     || ( parent.value == smaller_val
1614                          && child.value == larger_val ) )
1615         {
1616             child.set = false;
1617             /*
1618              * Note that this can result in a change in computed value in the
1619              * rare case that the parent's setting was a no-op (i.e. if the
1620              * parent's parent's computed value was already ultra-condensed or
1621              * ultra-expanded).  However, I'd guess that the change is for the
1622              * better: I'd guess that if the properties were specified
1623              * relatively, then the intent is to counteract parent's effect.
1624              * I.e. I believe this is the best code even in that case.
1625              */
1626         } else if (child.value == parent.value) {
1627             /* Leave as is. */
1628             /** \todo
1629              * It's unclear what to do if style and parent specify the same
1630              * relative directive (narrower or wider).  We can either convert
1631              * to absolute specification or coalesce to a single relative
1632              * request (of half the strength of the original pair).
1633              *
1634              * Converting to a single level of relative specification is a
1635              * better choice if the newly-unlinked clone is itself cloned to
1636              * other contexts (inheriting different font stretchiness): it
1637              * would preserve the effect that it will be narrower than
1638              * the inherited context in each case.  The disadvantage is that
1639              * it will ~certainly affect the computed value of the
1640              * newly-unlinked clone.
1641              */
1642         } else {
1643             unsigned const parent_val = parent.computed;
1644             child.value = ( child.value == smaller_val
1645                             ? ( parent_val == min_computed_val
1646                                 ? parent_val
1647                                 : parent_val - 1 )
1648                             : ( parent_val == max_computed_val
1649                                 ? parent_val
1650                                 : parent_val + 1 ) );
1651             g_assert(child.value <= max_computed_val);
1652             child.inherit = false;
1653             g_assert(child.set);
1654         }
1655     }
1658 template <typename LengthT>
1659 static void
1660 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1661                                              double const parent_child_em_ratio)
1663     if ( ( !(child.set) || child.inherit )
1664          && parent.set && !(parent.inherit) )
1665     {
1666         child = parent;
1667         switch (parent.unit) {
1668             case SP_CSS_UNIT_EM:
1669             case SP_CSS_UNIT_EX:
1670                 child.value *= parent_child_em_ratio;
1671                 /** \todo
1672                  * fixme: Have separate ex ratio parameter.
1673                  * Get x height from libnrtype or pango.
1674                  */
1675                 if (!IS_FINITE(child.value)) {
1676                     child.value = child.computed;
1677                     child.unit = SP_CSS_UNIT_NONE;
1678                 }
1679                 break;
1681             default:
1682                 break;
1683         }
1684     }
1687 static double
1688 get_relative_font_size_frac(SPIFontSize const &font_size)
1690     switch (font_size.type) {
1691         case SP_FONT_SIZE_LITERAL: {
1692             switch (font_size.value) {
1693                 case SP_CSS_FONT_SIZE_SMALLER:
1694                     return 5.0 / 6.0;
1696                 case SP_CSS_FONT_SIZE_LARGER:
1697                     return 6.0 / 5.0;
1699                 default:
1700                     g_assert_not_reached();
1701             }
1702         }
1704         case SP_FONT_SIZE_PERCENTAGE:
1705             return SP_F8_16_TO_FLOAT(font_size.value);
1707         case SP_FONT_SIZE_LENGTH:
1708             g_assert_not_reached();
1709     }
1710     g_assert_not_reached();
1713 /**
1714  * Combine \a style and \a parent style specifications into a single style specification that
1715  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1716  *
1717  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1718  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1719  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1720  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1721  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1722  *
1723  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1724  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1725  * style was previously up-to-date wrt \a parent.
1726  *
1727  * \pre \a parent's computed values are already up-to-date.
1728  *   (\a style's computed values needn't be up-to-date.)
1729  */
1730 void
1731 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1733     /** \note
1734      * The general rule for each property is as follows:
1735      *
1736      *   If style is set to an absolute value, then leave it as is.
1737      *
1738      *   Otherwise (i.e. if style has a relative value):
1739      *
1740      *     If parent is set to an absolute value, then set style to the computed value.
1741      *
1742      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1743      */
1745     /* We do font-size first, to ensure that em size is up-to-date. */
1746     /** \todo
1747      * fixme: We'll need to have more font-related things up the top once
1748      * we're getting x-height from pango or libnrtype.
1749      */
1751     /* Some things that allow relative specifications. */
1752     {
1753         /* font-size.  Note that we update the computed font-size of style,
1754            to assist in em calculations later in this function. */
1755         if (parent->font_size.set && !parent->font_size.inherit) {
1756             if (!style->font_size.set || style->font_size.inherit) {
1757                 /* font_size inherits the computed value, so we can use the parent value
1758                  * verbatim. */
1759                 style->font_size = parent->font_size;
1760             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1761                 /* Child already has absolute size (stored in computed value), so do nothing. */
1762             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1763                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1764                 /* Child already has absolute size, but we ensure that the computed value
1765                    is up-to-date. */
1766                 unsigned const ix = style->font_size.value;
1767                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1768                 style->font_size.computed = font_size_table[ix];
1769             } else {
1770                 /* Child has relative size. */
1771                 double const child_frac(get_relative_font_size_frac(style->font_size));
1772                 style->font_size.set = true;
1773                 style->font_size.inherit = false;
1774                 style->font_size.computed = parent->font_size.computed * child_frac;
1776                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1777                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1778                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1779                 {
1780                     /* Absolute value. */
1781                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1782                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1783                 } else {
1784                     /* Relative value. */
1785                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1786                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1787                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1788                 }
1789             }
1790         }
1792         /* 'font-stretch' */
1793         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1794                                                        parent->font_stretch,
1795                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1796                                                        SP_CSS_FONT_STRETCH_NARROWER);
1798         /* font-weight */
1799         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1800                                                        parent->font_weight,
1801                                                        SP_CSS_FONT_WEIGHT_900,
1802                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1803     }
1806     /* Enum values that don't have any relative settings (other than `inherit'). */
1807     {
1808         SPIEnum SPStyle::*const fields[] = {
1809             //nyi: SPStyle::clip_rule,
1810             //nyi: SPStyle::color_interpolation,
1811             //nyi: SPStyle::color_interpolation_filters,
1812             //nyi: SPStyle::color_rendering,
1813             &SPStyle::direction,
1814             &SPStyle::fill_rule,
1815             &SPStyle::font_style,
1816             &SPStyle::font_variant,
1817             //nyi: SPStyle::image_rendering,
1818             //nyi: SPStyle::pointer_events,
1819             //nyi: SPStyle::shape_rendering,
1820             &SPStyle::stroke_linecap,
1821             &SPStyle::stroke_linejoin,
1822             &SPStyle::text_anchor,
1823             //nyi: &SPStyle::text_rendering,
1824             &SPStyle::visibility,
1825             &SPStyle::writing_mode
1826         };
1828         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1829             SPIEnum SPStyle::*const fld = fields[i];
1830             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1831         }
1832     }
1834     /* A few other simple inheritance properties. */
1835     {
1836         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1837         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1838         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1840         /** \todo
1841          * We currently treat text-decoration as if it were a simple inherited
1842          * property (fixme). This code may need changing once we do the
1843          * special fill/stroke inheritance mentioned by the spec.
1844          */
1845         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1846                                                                  parent->text_decoration);
1848         //nyi: font-size-adjust,  // <number> | none | inherit
1849         //nyi: glyph-orientation-horizontal,
1850         //nyi: glyph-orientation-vertical,
1851     }
1853     /* Properties that involve length but are easy in other respects. */
1854     {
1855         /* The difficulty with lengths is that font-relative units need adjusting if the font
1856          * varies between parent & child.
1857          *
1858          * Lengths specified in the existing child can stay as they are: its computed font
1859          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1860          * size.
1861          *
1862          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1863          * ratio of em or ex size between parent & child.
1864          */
1865         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1867         SPILength SPStyle::*const lfields[] = {
1868             &SPStyle::stroke_width,
1869             &SPStyle::text_indent
1870         };
1871         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1872             SPILength SPStyle::*fld = lfields[i];
1873             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1874                                                                     parent->*fld,
1875                                                                     parent_child_em_ratio);
1876         }
1878         SPILengthOrNormal SPStyle::*const nfields[] = {
1879             &SPStyle::letter_spacing,
1880             &SPStyle::line_height,
1881             &SPStyle::word_spacing
1882         };
1883         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1884             SPILengthOrNormal SPStyle::*fld = nfields[i];
1885             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1886                                                                             parent->*fld,
1887                                                                             parent_child_em_ratio);
1888         }
1890         //nyi: &SPStyle::kerning: length or `auto'
1892         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1893            can accept units. */
1894     }
1896     /* Properties that involve a URI but are easy in other respects. */
1897     {
1898         /** \todo
1899          * Could cause problems if original object was in another document
1900          * and it used a relative URL.  (At the time of writing, we don't
1901          * allow hrefs to other documents, so this isn't a problem yet.)
1902          * Paint properties also allow URIs.
1903          */
1904         //nyi: cursor,   // may involve change in effect, but we can't do much better
1905         //nyi: color-profile,
1907         // Markers (marker-start, marker-mid, marker-end).
1908         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1909             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1910         }
1911     }
1913     /* CSS2 */
1914     /* Font */
1916     if (style->text && parent->text) {
1917         sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
1918                                                      parent->text->font_specification);
1919         
1920         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1921                                                      parent->text->font_family);
1922     }
1925     /* Properties that don't inherit by default.  Most of these need special handling. */
1926     {
1927         /*
1928          * opacity's effect is cumulative; we set the new value to the combined effect.  The
1929          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
1930          * fill-opacity are quite different from opacity, and don't need any special handling.)
1931          *
1932          * Cases:
1933          * - parent & child were each previously unset, in which case the effective
1934          *   opacity value is 1.0, and style should remain unset.
1935          * - parent was previously unset (so computed opacity value of 1.0)
1936          *   and child was set to inherit.  The merged child should
1937          *   get a value of 1.0, and shouldn't inherit (lest the new parent
1938          *   has a different opacity value).  Given that opacity's default
1939          *   value is 1.0 (rather than inherit), we might as well have the
1940          *   merged child's opacity be unset.
1941          * - parent was previously unset (so opacity 1.0), and child was set to a number.
1942          *   The merged child should retain its existing settings (though it doesn't matter
1943          *   if we make it unset if that number was 1.0).
1944          * - parent was inherit and child was unset.  Merged child should be set to inherit.
1945          * - parent was inherit and child was inherit.  (We can't in general reproduce this
1946          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
1947          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1948          *   child's value should be set to the product of the two, i.e. the square of the
1949          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
1950          *   is more important to retain the effective opacity than to retain the inheriting
1951          *   effect, and assumes that the inheriting effect either isn't important enough to create
1952          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
1953          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1954          *   closer to maintaining the effect.
1955          * - parent was inherit and child was set to a numerical value.  If the child's value
1956          *   was 1.0, then the merged child should have the same settings as the parent.
1957          *   If the child's value was 0, then the merged child should also be set to 0.
1958          *   If the child's value was anything else, then we do the same as for the inherit/inherit
1959          *   case above: have the merged child set to the product of the two opacities and not
1960          *   marked as inherit, for the same reasons as for that case.
1961          * - parent was set to a value, and child was unset.  The merged child should have
1962          *   parent's settings.
1963          * - parent was set to a value, and child was inherit.  The merged child should
1964          *   be set to the product, i.e. the square of the parent's value.
1965          * - parent & child are each set to a value.  The merged child should be set to the
1966          *   product.
1967          */
1968         if ( !style->opacity.set
1969              || ( !style->opacity.inherit
1970                   && style->opacity.value == SP_SCALE24_MAX ) )
1971         {
1972             style->opacity = parent->opacity;
1973         } else {
1974             /* Ensure that style's computed value is up-to-date. */
1975             if (style->opacity.inherit) {
1976                 style->opacity.value = parent->opacity.value;
1977             }
1979             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1980             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1981                                                   parent->opacity.value);
1983             style->opacity.inherit = (parent->opacity.inherit
1984                                       && style->opacity.inherit
1985                                       && (parent->opacity.value == 0 ||
1986                                           parent->opacity.value == SP_SCALE24_MAX));
1987             style->opacity.set = ( style->opacity.inherit
1988                                    || style->opacity.value < SP_SCALE24_MAX );
1989         }
1991         /* display is in principle similar to opacity, but implementation is easier. */
1992         if ( parent->display.set && !parent->display.inherit
1993              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1994             style->display.value = SP_CSS_DISPLAY_NONE;
1995             style->display.set = true;
1996             style->display.inherit = false;
1997         } else if (style->display.inherit) {
1998             style->display.value = parent->display.value;
1999             style->display.set = parent->display.set;
2000             style->display.inherit = parent->display.inherit;
2001         } else {
2002             /* Leave as is.  (display doesn't inherit by default.) */
2003         }
2005         /* enable-background - this is rather complicated, because
2006          * it is valid only when applied to container elements.
2007          * Let's check a simple case anyhow. */
2008         if (parent->enable_background.set
2009             && !parent->enable_background.inherit
2010             && style->enable_background.inherit)
2011         {
2012             style->enable_background.set = true;
2013             style->enable_background.inherit = false;
2014             style->enable_background.value = parent->enable_background.value;
2015         }
2017         if (!style->filter.set || style->filter.inherit)
2018         {
2019             // FIXME:
2020             // instead of just copying over, we need to _really merge_ the two filters by combining their
2021             // filter primitives
2022             sp_style_merge_ifilter(style, &parent->filter);
2023         }
2025         /** \todo
2026          * fixme: Check that we correctly handle all properties that don't
2027          * inherit by default (as shown in
2028          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2029          */
2030     }
2032     /* SPIPaint properties (including color). */
2033     {
2034         /** \todo
2035          * Think about the issues involved if specified as currentColor or
2036          * if specified relative to colorProfile, and if the currentColor or
2037          * colorProfile differs between parent \& child.  See also comments
2038          * elsewhere in this function about URIs.
2039          */
2040         SPIPaint SPStyle::*const fields[] = {
2041             &SPStyle::color,
2042             &SPStyle::fill,
2043             &SPStyle::stroke
2044         };
2045         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2046             SPIPaint SPStyle::*const fld = fields[i];
2047             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2048         }
2049     }
2051     /* Things from SVG 1.2 or CSS3. */
2052     {
2053         /* Note: If we ever support setting string values for text-align then we'd need strdup
2054          * handling here. */
2055         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2057         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2058         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2059     }
2061     /* Note: this will need length handling once dasharray supports units. */
2062     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2063          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2064     {
2065         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2066         if (style->stroke_dash.n_dash > 0) {
2067             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2068             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2069         }
2070         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2071         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2072     }
2074     /* Note: this will need length handling once dasharray_offset supports units. */
2075     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
2076         style->stroke_dash.offset = parent->stroke_dash.offset;
2077         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2078         /* fixme: we don't currently allow stroke-dashoffset to be `inherit'.  TODO: Try to
2079          * represent it as a normal SPILength; though will need to do something about existing
2080          * users of stroke_dash.offset and stroke_dashoffset_set. */
2081     }
2085 static void
2086 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2088     // it may be that this style's SPIPaint has not yet created its URIReference;
2089     // now that we have a document, we can create it here
2090     if (!paint->value.href && document) {
2091         paint->value.href = new SPPaintServerReference(document);
2092         paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
2093     }
2095     if (paint->value.href && paint->value.href->getObject())
2096         paint->value.href->detach();
2098     if (paint->value.href) {
2099         try {
2100             paint->value.href->attach(*uri);
2101         } catch (Inkscape::BadURIException &e) {
2102             g_warning("%s", e.what());
2103             paint->value.href->detach();
2104         }
2105     }
2108 static void
2109 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2111     try {
2112         const Inkscape::URI IURI(uri);
2113         sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2114     } catch (...) {
2115         g_warning("URI failed to parse: %s", uri);
2116     }
2119 void
2120 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2122     sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2125 /**
2126  *
2127  */
2128 static void
2129 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2131     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2132         bool isset = paint->set;
2133         paint->clear();
2134         paint->set = isset;
2135         paint->currentcolor = TRUE;
2136         paint->setColor(style->color.value.color);
2137         return;
2138     }
2140     paint->clear();
2141     if ( parent->isPaintserver() ) {
2142         if (parent->value.href) {
2143             sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2144         } else {
2145             g_warning("Expected paint server not found.");
2146         }
2147     } else if ( parent->isColor() ) {
2148         paint->setColor( parent->value.color );
2149     } else if ( parent->isNoneSet() ) {
2150         paint->noneSet = TRUE;
2151     } else if ( parent->isNone() ) {
2152         //
2153     } else {
2154         g_assert_not_reached();
2155     }
2159 /**
2160  * Merge filter style from parent.
2161  * Filter effects do not inherit by default
2162  */
2163 static void
2164 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2166     sp_style_filter_clear(style);
2167     style->filter.set = parent->set;
2168     style->filter.inherit = parent->inherit;
2170     if (style->filter.href && style->filter.href->getObject())
2171        style->filter.href->detach();
2173     if (style->filter.href && parent->href) {
2174         try {
2175             style->filter.href->attach(*parent->href->getURI());
2176         } catch (Inkscape::BadURIException &e) {
2177             g_warning("%s", e.what());
2178             style->filter.href->detach();
2179         }
2180     }
2183 /**
2184  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2185  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2186  * complete cascaded style to style_clipboard. When you need a CSS string
2187  * for an object in the document tree, you normally call
2188  * sp_style_write_difference instead to take into account the object's parent.
2189  *
2190  * \pre style != NULL.
2191  * \pre flags in {IFSET, ALWAYS}.
2192  * \post ret != NULL.
2193  */
2194 gchar *
2195 sp_style_write_string(SPStyle const *const style, guint const flags)
2197     /** \todo
2198      * Merge with write_difference, much duplicate code!
2199      */
2200     g_return_val_if_fail(style != NULL, NULL);
2201     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2202                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2203                          NULL);
2205     gchar c[BMAX];
2206     gchar *p = c;
2207     *p = '\0';
2209     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2210     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2211     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2212     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2213     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2215     /* Text */
2216     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2217     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2218     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2219     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2220     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2221     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2222     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2223     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2224     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2225     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2227     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2229     /// \todo fixme: Per type methods need default flag too (lauris)
2230     p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2231     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2233     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2234     // if fill:none, skip writing fill properties
2235     if (!style->fill.noneSet) {
2236         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2237         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2238     }
2240     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2242     // stroke width affects markers, so write it if there's stroke OR any markers
2243     if (!style->stroke.noneSet || 
2244         style->marker[SP_MARKER_LOC].set ||
2245         style->marker[SP_MARKER_LOC_START].set ||
2246         style->marker[SP_MARKER_LOC_MID].set || 
2247         style->marker[SP_MARKER_LOC_END].set) {
2248         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2249     }
2251     // if stroke:none, skip writing stroke properties
2252     if (!style->stroke.noneSet) {
2253         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2254         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2255         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2256         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2258         /** \todo fixme: */
2259         if ((flags == SP_STYLE_FLAG_ALWAYS)
2260             || style->stroke_dasharray_set)
2261         {
2262             if (style->stroke_dasharray_inherit) {
2263                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2264             } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2265                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2266                 gint i;
2267                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2268                     Inkscape::CSSOStringStream os;
2269                     if (i) {
2270                         os << ", ";
2271                     }
2272                     os << style->stroke_dash.dash[i];
2273                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2274                 }
2275                 if (p < c + BMAX) {
2276                     *p++ = ';';
2277                 }
2278             } else {
2279                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2280             }
2281         }
2283         /** \todo fixme: */
2284         if (style->stroke_dashoffset_set) {
2285             Inkscape::CSSOStringStream os;
2286             os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2287             p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2288         } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2289             p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2290         }
2291     }
2293     if (style->marker[SP_MARKER_LOC].set) {
2294         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2295     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2296         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2297     }
2298     if (style->marker[SP_MARKER_LOC_START].set) {
2299         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2300     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2301         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2302     }
2303     if (style->marker[SP_MARKER_LOC_MID].set) {
2304         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2305     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2306         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2307     }
2308     if (style->marker[SP_MARKER_LOC_END].set) {
2309         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2310     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2311         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2312     }
2314     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2315     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2316     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2318     /* filter: */
2319     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2321     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2323     /* fixme: */
2324     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2326     /* Get rid of trailing `;'. */
2327     if (p != c) {
2328         --p;
2329         if (*p == ';') {
2330             *p = '\0';
2331         }
2332     }
2334     return g_strdup(c);
2338 #define STYLE_BUF_MAX
2341 /**
2342  * Dumps style to CSS string, see sp_style_write_string()
2343  *
2344  * \pre from != NULL.
2345  * \pre to != NULL.
2346  * \post ret != NULL.
2347  */
2348 gchar *
2349 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2351     g_return_val_if_fail(from != NULL, NULL);
2352     g_return_val_if_fail(to != NULL, NULL);
2354     gchar c[BMAX], *p = c;
2355     *p = '\0';
2357     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2358     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2359     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2360     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2361     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2363     /* Text */
2364     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2365     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2366     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2367     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2368     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2369     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2370     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2371     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2372     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2373     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2375     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2377     /// \todo fixme: Per type methods need default flag too
2378     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2379         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2380     }
2381     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2383     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2384     // if fill:none, skip writing fill properties
2385     if (!from->fill.noneSet) {
2386         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2387         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2388     }
2390     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2392     // stroke width affects markers, so write it if there's stroke OR any markers
2393     if (!from->stroke.noneSet || 
2394         from->marker[SP_MARKER_LOC].set ||
2395         from->marker[SP_MARKER_LOC_START].set ||
2396         from->marker[SP_MARKER_LOC_MID].set || 
2397         from->marker[SP_MARKER_LOC_END].set) {
2398         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2399     }
2401     // if stroke:none, skip writing stroke properties
2402     if (!from->stroke.noneSet) {
2403         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2404                                   &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2405         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2406                                   &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2407         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2408                                    &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2409         /** \todo fixme: */
2410         if (from->stroke_dasharray_set) {
2411             if (from->stroke_dasharray_inherit) {
2412                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2413             } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2414                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2415                 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2416                     Inkscape::CSSOStringStream os;
2417                     if (i) {
2418                         os << ", ";
2419                     }
2420                     os << from->stroke_dash.dash[i];
2421                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2422                 }
2423                 p += g_snprintf(p, c + BMAX - p, ";");
2424             }
2425         }
2426         /* fixme: */
2427         if (from->stroke_dashoffset_set) {
2428             Inkscape::CSSOStringStream os;
2429             os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2430             p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2431         }
2432         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2433     }
2435     /* markers */
2436     if (from->marker[SP_MARKER_LOC].value != NULL) {
2437         p += g_snprintf(p, c + BMAX - p, "marker:%s;",       from->marker[SP_MARKER_LOC].value);
2438     }
2439     if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2440         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2441     }
2442     if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2443         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2444     }
2445     if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2446         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2447     }
2449     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2450     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2451     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2453     /* filter: */
2454     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2456     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2458     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2460     /** \todo
2461      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2462      * flag is mainly only for attributes that don't handle explicit unset well.
2463      * We may need to revisit the behaviour of this routine.
2464      */
2466     /* Get rid of trailing `;'. */
2467     if (p != c) {
2468         --p;
2469         if (*p == ';') {
2470             *p = '\0';
2471         }
2472     }
2474     return g_strdup(c);
2479 /**
2480  * Reset all style properties.
2481  */
2482 static void
2483 sp_style_clear(SPStyle *style)
2485     g_return_if_fail(style != NULL);
2487     style->fill.clear();
2488     style->stroke.clear();
2489     sp_style_filter_clear(style);
2491     if (style->fill.value.href)
2492         delete style->fill.value.href;
2493     if (style->stroke.value.href)
2494         delete style->stroke.value.href;
2495     if (style->filter.href)
2496         delete style->filter.href;
2498     if (style->stroke_dash.dash) {
2499         g_free(style->stroke_dash.dash);
2500     }
2502     /** \todo fixme: Do that text manipulation via parents */
2503     SPObject *object = style->object;
2504     SPDocument *document = style->document;
2505     gint const refcount = style->refcount;
2506     SPTextStyle *text = style->text;
2507     unsigned const text_private = style->text_private;
2509     memset(style, 0, sizeof(SPStyle));
2511     style->refcount = refcount;
2512     style->object = object;
2513     style->document = document;
2515     if (document) {
2516         style->filter.href = new SPFilterReference(document);
2517         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2519         style->fill.value.href = new SPPaintServerReference(document);
2520         style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2522         style->stroke.value.href = new SPPaintServerReference(document);
2523         style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2524     }
2526     style->text = text;
2527     style->text_private = text_private;
2529     style->text->font_specification.set = FALSE;
2530     style->text->font.set = FALSE;
2531     style->text->font_family.set = FALSE;
2533     style->font_size.set = FALSE;
2534     style->font_size.type = SP_FONT_SIZE_LITERAL;
2535     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2536     style->font_size.computed = 12.0;
2537     style->font_style.set = FALSE;
2538     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2539     style->font_variant.set = FALSE;
2540     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2541     style->font_weight.set = FALSE;
2542     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2543     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2544     style->font_stretch.set = FALSE;
2545     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2547     /* text */
2548     style->text_indent.set = FALSE;
2549     style->text_indent.unit = SP_CSS_UNIT_NONE;
2550     style->text_indent.computed = 0.0;
2552     style->text_align.set = FALSE;
2553     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2555     style->text_decoration.set = FALSE;
2556     style->text_decoration.underline = FALSE;
2557     style->text_decoration.overline = FALSE;
2558     style->text_decoration.line_through = FALSE;
2559     style->text_decoration.blink = FALSE;
2561     style->line_height.set = FALSE;
2562     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2563     style->line_height.normal = TRUE;
2564     style->line_height.value = style->line_height.computed = 1.0;
2566     style->letter_spacing.set = FALSE;
2567     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2568     style->letter_spacing.normal = TRUE;
2569     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2571     style->word_spacing.set = FALSE;
2572     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2573     style->word_spacing.normal = TRUE;
2574     style->word_spacing.value = style->word_spacing.computed = 0.0;
2576     style->text_transform.set = FALSE;
2577     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2579     style->direction.set = FALSE;
2580     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2582     style->block_progression.set = FALSE;
2583     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2585     style->writing_mode.set = FALSE;
2586     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2589     style->text_anchor.set = FALSE;
2590     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2593     style->opacity.value = SP_SCALE24_MAX;
2594     style->visibility.set = FALSE;
2595     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2596     style->display.set = FALSE;
2597     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2598     style->overflow.set = FALSE;
2599     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2601     style->color.clear();
2602     style->color.setColor(0.0, 0.0, 0.0);
2604     style->fill.clear();
2605     style->fill.setColor(0.0, 0.0, 0.0);
2606     style->fill_opacity.value = SP_SCALE24_MAX;
2607     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2609     style->stroke.clear();
2610     style->stroke_opacity.value = SP_SCALE24_MAX;
2612     style->stroke_width.set = FALSE;
2613     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2614     style->stroke_width.computed = 1.0;
2616     style->stroke_linecap.set = FALSE;
2617     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2618     style->stroke_linejoin.set = FALSE;
2619     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2621     style->stroke_miterlimit.set = FALSE;
2622     style->stroke_miterlimit.value = 4.0;
2624     style->stroke_dash.n_dash = 0;
2625     style->stroke_dash.dash = NULL;
2626     style->stroke_dash.offset = 0.0;
2628     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2629         g_free(style->marker[i].value);
2630         style->marker[i].set = FALSE;
2631     }
2633     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2634     style->enable_background.set = false;
2635     style->enable_background.inherit = false;
2640 /**
2641  *
2642  */
2643 static void
2644 sp_style_read_dash(SPStyle *style, gchar const *str)
2646     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2647     style->stroke_dasharray_set = TRUE;
2649     if (strcmp(str, "inherit") == 0) {
2650         style->stroke_dasharray_inherit = true;
2651         return;
2652     }
2653     style->stroke_dasharray_inherit = false;
2655     NRVpathDash &dash = style->stroke_dash;
2656     g_free(dash.dash);
2657     dash.dash = NULL;
2659     if (strcmp(str, "none") == 0) {
2660         dash.n_dash = 0;
2661         return;
2662     }
2664     gint n_dash = 0;
2665     gdouble d[64];
2666     gchar *e = NULL;
2668     bool LineSolid=true;
2669     while (e != str && n_dash < 64) {
2670         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2671         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2672         if (d[n_dash] > 0.00000001)
2673             LineSolid = false;
2674         if (e != str) {
2675             n_dash += 1;
2676             str = e;
2677         }
2678         while (str && *str && !isalnum(*str)) str += 1;
2679     }
2681     if (LineSolid) {
2682         dash.n_dash = 0;
2683         return;
2684     }
2686     if (n_dash > 0) {
2687         dash.dash = g_new(gdouble, n_dash);
2688         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2689         dash.n_dash = n_dash;
2690     }
2694 /*#########################
2695 ## SPTextStyle operations
2696 #########################*/
2699 /**
2700  * Return new SPTextStyle object with default settings.
2701  */
2702 static SPTextStyle *
2703 sp_text_style_new()
2705     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2706     ts->refcount = 1;
2707     sp_text_style_clear(ts);
2709     ts->font_specification.value = g_strdup("Bitstream Vera Sans");
2710     ts->font.value = g_strdup("Bitstream Vera Sans");
2711     ts->font_family.value = g_strdup("Bitstream Vera Sans");
2713     return ts;
2717 /**
2718  * Clear text style settings.
2719  */
2720 static void
2721 sp_text_style_clear(SPTextStyle *ts)
2723     ts->font_specification.set = FALSE;
2724     ts->font.set = FALSE;
2725     ts->font_family.set = FALSE;
2730 /**
2731  * Reduce refcount of text style and possibly free it.
2732  */
2733 static SPTextStyle *
2734 sp_text_style_unref(SPTextStyle *st)
2736     st->refcount -= 1;
2738     if (st->refcount < 1) {
2739         g_free(st->font_specification.value);
2740         g_free(st->font.value);
2741         g_free(st->font_family.value);
2742         g_free(st);
2743     }
2745     return NULL;
2750 /**
2751  * Return duplicate of text style.
2752  */
2753 static SPTextStyle *
2754 sp_text_style_duplicate_unset(SPTextStyle *st)
2756     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2757     nt->refcount = 1;
2759     nt->font_specification.value = g_strdup(st->font_specification.value);
2760     nt->font.value = g_strdup(st->font.value);
2761     nt->font_family.value = g_strdup(st->font_family.value);
2763     return nt;
2768 /**
2769  * Write SPTextStyle object into string.
2770  */
2771 static guint
2772 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2774     gint d = 0;
2776     // We do not do diffing for text style
2777     if (flags == SP_STYLE_FLAG_IFDIFF)
2778         flags = SP_STYLE_FLAG_IFSET;
2780     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2781     d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2782     return d;
2787 /* The following sp_tyle_read_* functions ignore invalid values, as per
2788  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2789  *
2790  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2791  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2792  * pairs, in which case SVG's error-handling rules
2793  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2794  */
2797 /**
2798  * Set SPIFloat object from string.
2799  */
2800 static void
2801 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2803     if (!strcmp(str, "inherit")) {
2804         val->set = TRUE;
2805         val->inherit = TRUE;
2806     } else {
2807         gfloat value;
2808         if (sp_svg_number_read_f(str, &value)) {
2809             val->set = TRUE;
2810             val->inherit = FALSE;
2811             val->value = value;
2812         }
2813     }
2818 /**
2819  * Set SPIScale24 object from string.
2820  */
2821 static void
2822 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2824     if (!strcmp(str, "inherit")) {
2825         val->set = TRUE;
2826         val->inherit = TRUE;
2827     } else {
2828         gfloat value;
2829         if (sp_svg_number_read_f(str, &value)) {
2830             val->set = TRUE;
2831             val->inherit = FALSE;
2832             value = CLAMP(value, 0.0, 1.0);
2833             val->value = SP_SCALE24_FROM_FLOAT(value);
2834         }
2835     }
2838 /**
2839  * Reads a style value and performs lookup based on the given style value enumerations.
2840  */
2841 static void
2842 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2843                     bool const can_explicitly_inherit)
2845     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2846         val->set = TRUE;
2847         val->inherit = TRUE;
2848     } else {
2849         for (unsigned i = 0; dict[i].key; i++) {
2850             if (!strcmp(str, dict[i].key)) {
2851                 val->set = TRUE;
2852                 val->inherit = FALSE;
2853                 val->value = dict[i].value;
2854                 /* Save copying for values not needing it */
2855                 val->computed = val->value;
2856                 break;
2857             }
2858         }
2859     }
2864 /**
2865  * Set SPIString object from string.
2866  */
2867 static void
2868 sp_style_read_istring(SPIString *val, gchar const *str)
2870     g_free(val->value);
2872     if (!strcmp(str, "inherit")) {
2873         val->set = TRUE;
2874         val->inherit = TRUE;
2875         val->value = NULL;
2876     } else {
2877         val->set = TRUE;
2878         val->inherit = FALSE;
2879         val->value = g_strdup(str);
2880     }
2885 /**
2886  * Set SPILength object from string.
2887  */
2888 static void
2889 sp_style_read_ilength(SPILength *val, gchar const *str)
2891     if (!strcmp(str, "inherit")) {
2892         val->set = TRUE;
2893         val->inherit = TRUE;
2894     } else {
2895         gdouble value;
2896         gchar *e;
2897         /** \todo fixme: Move this to standard place (Lauris) */
2898         value = g_ascii_strtod(str, &e);
2899         if ((gchar const *) e != str) {
2900             /** \todo
2901              * Allow the number of px per inch to vary (document preferences,
2902              * X server or whatever).  E.g. don't fill in computed here, do
2903              * it at the same time as percentage units are done.
2904              */
2905             if (!*e) {
2906                 /* Userspace */
2907                 val->unit = SP_CSS_UNIT_NONE;
2908                 val->computed = value;
2909             } else if (!strcmp(e, "px")) {
2910                 /* Userspace */
2911                 val->unit = SP_CSS_UNIT_PX;
2912                 val->computed = value;
2913             } else if (!strcmp(e, "pt")) {
2914                 /* Userspace / DEVICESCALE */
2915                 val->unit = SP_CSS_UNIT_PT;
2916                 val->computed = value * PX_PER_PT;
2917             } else if (!strcmp(e, "pc")) {
2918                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2919                 val->unit = SP_CSS_UNIT_PC;
2920                 val->computed = value * PX_PER_PT * 12;
2921             } else if (!strcmp(e, "mm")) {
2922                 val->unit = SP_CSS_UNIT_MM;
2923                 val->computed = value * PX_PER_MM;
2924             } else if (!strcmp(e, "cm")) {
2925                 val->unit = SP_CSS_UNIT_CM;
2926                 val->computed = value * PX_PER_CM;
2927             } else if (!strcmp(e, "in")) {
2928                 val->unit = SP_CSS_UNIT_IN;
2929                 val->computed = value * PX_PER_IN;
2930             } else if (!strcmp(e, "em")) {
2931                 /* EM square */
2932                 val->unit = SP_CSS_UNIT_EM;
2933                 val->value = value;
2934             } else if (!strcmp(e, "ex")) {
2935                 /* ex square */
2936                 val->unit = SP_CSS_UNIT_EX;
2937                 val->value = value;
2938             } else if (!strcmp(e, "%")) {
2939                 /* Percentage */
2940                 val->unit = SP_CSS_UNIT_PERCENT;
2941                 val->value = value * 0.01;
2942             } else {
2943                 /* Invalid */
2944                 return;
2945             }
2946             val->set = TRUE;
2947             val->inherit = FALSE;
2948         }
2949     }
2952 /**
2953  * Set SPILengthOrNormal object from string.
2954  */
2955 static void
2956 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2958     if (!strcmp(str, "normal")) {
2959         val->set = TRUE;
2960         val->inherit = FALSE;
2961         val->normal = TRUE;
2962         val->unit = SP_CSS_UNIT_NONE;
2963         val->value = val->computed = 0.0;
2964     } else {
2965         SPILength length;
2966         sp_style_read_ilength(&length, str);
2967         val->set = length.set;
2968         val->inherit = length.inherit;
2969         val->normal = FALSE;
2970         val->unit = length.unit;
2971         val->value = length.value;
2972         val->computed = length.computed;
2973     }
2976 /**
2977  * Set SPITextDecoration object from string.
2978  */
2979 static void
2980 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2982     if (!strcmp(str, "inherit")) {
2983         val->set = TRUE;
2984         val->inherit = TRUE;
2985     } else if (!strcmp(str, "none")) {
2986         val->set = TRUE;
2987         val->inherit = FALSE;
2988         val->underline = FALSE;
2989         val->overline = FALSE;
2990         val->line_through = FALSE;
2991         val->blink = FALSE;
2992     } else {
2993         bool found_underline = false;
2994         bool found_overline = false;
2995         bool found_line_through = false;
2996         bool found_blink = false;
2997         for ( ; *str ; str++ ) {
2998             if (*str == ' ') continue;
2999             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
3000                 found_underline = true;
3001                 str += 9;
3002             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
3003                 found_overline = true;
3004                 str += 8;
3005             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
3006                 found_line_through = true;
3007                 str += 12;
3008             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
3009                 found_blink = true;
3010                 str += 5;
3011             } else {
3012                 return;  // invalid value
3013             }
3014         }
3015         if (!(found_underline || found_overline || found_line_through || found_blink)) {
3016             return;  // invalid value: empty
3017         }
3018         val->set = TRUE;
3019         val->inherit = FALSE;
3020         val->underline = found_underline;
3021         val->overline = found_overline;
3022         val->line_through = found_line_through;
3023         val->blink = found_blink;
3024     }
3027 /**
3028  * Set SPIPaint object from string containing an integer value.
3029  * \param document Ignored
3030  */
3031 static void
3032 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3034     (void)style; // TODO
3035     (void)document; // TODO
3036     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
3037     if (!strcmp(str, "inherit")) {
3038         paint->set = TRUE;
3039         paint->inherit = TRUE;
3040     } else {
3041         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
3042         if (rgb0 != 0xff) {
3043             paint->setColor(rgb0);
3044             paint->set = TRUE;
3045             paint->inherit = FALSE;
3046         }
3047     }
3051 /**
3052  * Set SPIPaint object from string.
3053  *
3054  * \pre paint == \&style.fill || paint == \&style.stroke.
3055  */
3056 static void
3057 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3059     while (g_ascii_isspace(*str)) {
3060         ++str;
3061     }
3063     paint->clear();
3065     if (streq(str, "inherit")) {
3066         paint->set = TRUE;
3067         paint->inherit = TRUE;
3068     } else {
3069         if ( strneq(str, "url", 3) ) {
3070             gchar *uri = extract_uri( str, &str );
3071             while ( g_ascii_isspace(*str) ) {
3072                 ++str;
3073             }
3074             // TODO check on and comment the comparrison "paint != &style->color".
3075             if ( uri && *uri && (paint != &style->color) ) {
3076                 paint->set = TRUE;
3078                 // it may be that this style's SPIPaint has not yet created its URIReference;
3079                 // now that we have a document, we can create it here
3080                 if (!paint->value.href && document) {
3081                     paint->value.href = new SPPaintServerReference(document);
3082                     paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
3083                 }
3085                 // TODO check what this does in light of move away from union
3086                 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3087             }
3088             g_free( uri );
3089         }
3091         if (streq(str, "currentColor") && paint != &style->color) {
3092             paint->set = TRUE;
3093             paint->currentcolor = TRUE;
3094         } else if (streq(str, "none") && paint != &style->color) {
3095             paint->set = TRUE;
3096             paint->noneSet = TRUE;
3097         } else {
3098             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3099             if (rgb0 != 0xff) {
3100                 paint->setColor( rgb0 );
3101                 paint->set = TRUE;
3103                 while (g_ascii_isspace(*str)) {
3104                     ++str;
3105                 }
3106                 if (strneq(str, "icc-color(", 10)) {
3107                     SVGICCColor* tmp = new SVGICCColor();
3108                     if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3109                         delete tmp;
3110                         tmp = 0;
3111                     }
3112                     paint->value.color.icc = tmp;
3113                 }
3114             }
3115         }
3116     }
3121 /**
3122  * Set SPIFontSize object from string.
3123  */
3124 static void
3125 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3127     if (!strcmp(str, "inherit")) {
3128         val->set = TRUE;
3129         val->inherit = TRUE;
3130     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3131         for (unsigned i = 0; enum_font_size[i].key; i++) {
3132             if (!strcmp(str, enum_font_size[i].key)) {
3133                 val->set = TRUE;
3134                 val->inherit = FALSE;
3135                 val->type = SP_FONT_SIZE_LITERAL;
3136                 val->value = enum_font_size[i].value;
3137                 return;
3138             }
3139         }
3140         /* Invalid */
3141         return;
3142     } else {
3143         gdouble value;
3144         gchar *e;
3145         /* fixme: Move this to standard place (Lauris) */
3146         value = g_ascii_strtod(str, &e);
3147         if ((gchar const *) e != str) {
3148             if (!*e) {
3149                 /* Userspace */
3150             } else if (!strcmp(e, "px")) {
3151                 /* Userspace */
3152             } else if (!strcmp(e, "pt")) {
3153                 /* Userspace * DEVICESCALE */
3154                 value *= PX_PER_PT;
3155             } else if (!strcmp(e, "pc")) {
3156                 /* 12pt */
3157                 value *= PX_PER_PT * 12.0;
3158             } else if (!strcmp(e, "mm")) {
3159                 value *= PX_PER_MM;
3160             } else if (!strcmp(e, "cm")) {
3161                 value *= PX_PER_CM;
3162             } else if (!strcmp(e, "in")) {
3163                 value *= PX_PER_IN;
3164             } else if (!strcmp(e, "%")) {
3165                 /* Percentage */
3166                 val->set = TRUE;
3167                 val->inherit = FALSE;
3168                 val->type = SP_FONT_SIZE_PERCENTAGE;
3169                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3170                 return;
3171             } else {
3172                 /* Invalid */
3173                 return;
3174             }
3175             /* Length */
3176             val->set = TRUE;
3177             val->inherit = FALSE;
3178             val->type = SP_FONT_SIZE_LENGTH;
3179             val->computed = value;
3180             return;
3181         }
3182     }
3187 /**
3188  * Set SPIFilter object from string.
3189  */
3190 static void
3191 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3193     SPIFilter *f = &(style->filter);
3194     /* Try all possible values: inherit, none, uri */
3195     if (streq(str, "inherit")) {
3196         f->set = TRUE;
3197         f->inherit = TRUE;
3198         if (f->href && f->href->getObject())
3199             f->href->detach(); 
3200     } else if(streq(str, "none")) {
3201         f->set = TRUE;
3202         f->inherit = FALSE;
3203         if (f->href && f->href->getObject())
3204            f->href->detach(); 
3205     } else if (strneq(str, "url", 3)) {
3206         char *uri = extract_uri(str);
3207         if(uri == NULL || uri[0] == '\0') {
3208             g_warning("Specified filter url is empty");
3209             f->set = TRUE;
3210             f->inherit = FALSE;
3211             return;
3212         }
3213         f->set = TRUE;
3214         f->inherit = FALSE;
3215         if (f->href && f->href->getObject())
3216             f->href->detach();
3218         // it may be that this style has not yet created its SPFilterReference;
3219         // now that we have a document, we can create it here
3220         if (!f->href && document) {
3221             f->href = new SPFilterReference(document);
3222             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3223         }
3225         try {
3226             f->href->attach(Inkscape::URI(uri));
3227         } catch (Inkscape::BadURIException &e) {
3228             g_warning("%s", e.what());
3229             f->href->detach();
3230         }
3231         g_free (uri);
3233     } else {
3234         /* We shouldn't reach this if SVG input is well-formed */
3235         f->set = FALSE;
3236         f->inherit = FALSE;
3237         if (f->href && f->href->getObject())
3238             f->href->detach(); 
3239     }
3242 /**
3243  * Set SPIEnum object from repr attribute.
3244  */
3245 static void
3246 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3247                     gchar const *key, SPStyleEnum const *dict,
3248                     bool const can_explicitly_inherit)
3250     gchar const *str = repr->attribute(key);
3251     if (str) {
3252         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3253     }
3258 /**
3259  * Set SPILength object from repr attribute.
3260  */
3261 static void
3262 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3264     gchar const *str = repr->attribute(key);
3265     if (str) {
3266         sp_style_read_ilength(val, str);
3267     }
3270 /**
3271  * Set SPIFontSize object from repr attribute.
3272  */
3273 static void
3274 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3276     gchar const *str = repr->attribute(key);
3277     if (str) {
3278         sp_style_read_ifontsize(val, str);
3279     }
3283 /**
3284  * Set SPIFloat object from repr attribute.
3285  */
3286 static void
3287 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3289     gchar const *str = repr->attribute(key);
3290     if (str) {
3291         sp_style_read_ifloat(val, str);
3292     }
3296 /**
3297  * Write SPIFloat object into string.
3298  */
3299 static gint
3300 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3301                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3303     Inkscape::CSSOStringStream os;
3305     if ((flags & SP_STYLE_FLAG_ALWAYS)
3306         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3307         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3308             && (!base->set || (val->value != base->value))))
3309     {
3310         if (val->inherit) {
3311             return g_snprintf(p, len, "%s:inherit;", key);
3312         } else {
3313             os << key << ":" << val->value << ";";
3314             return g_strlcpy(p, os.str().c_str(), len);
3315         }
3316     }
3317     return 0;
3321 /**
3322  * Write SPIScale24 object into string.
3323  */
3324 static gint
3325 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3326                         SPIScale24 const *const val, SPIScale24 const *const base,
3327                         guint const flags)
3329     Inkscape::CSSOStringStream os;
3331     if ((flags & SP_STYLE_FLAG_ALWAYS)
3332         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3333         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3334             && (!base->set || (val->value != base->value))))
3335     {
3336         if (val->inherit) {
3337             return g_snprintf(p, len, "%s:inherit;", key);
3338         } else {
3339             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3340             return g_strlcpy(p, os.str().c_str(), len);
3341         }
3342     }
3343     return 0;
3347 /**
3348  * Write SPIEnum object into string.
3349  */
3350 static gint
3351 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3352                      SPStyleEnum const *const dict,
3353                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3355     if ((flags & SP_STYLE_FLAG_ALWAYS)
3356         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3357         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3358             && (!base->set || (val->computed != base->computed))))
3359     {
3360         if (val->inherit) {
3361             return g_snprintf(p, len, "%s:inherit;", key);
3362         }
3363         for (unsigned i = 0; dict[i].key; i++) {
3364             if (dict[i].value == static_cast< gint > (val->value) ) {
3365                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3366             }
3367         }
3368     }
3369     return 0;
3374 /**
3375  * Write SPIString object into string.
3376  */
3377 static gint
3378 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3379                        SPIString const *const val, SPIString const *const base, guint const flags)
3381     if ((flags & SP_STYLE_FLAG_ALWAYS)
3382         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3383         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3384             && (!base->set || strcmp(val->value, base->value))))
3385     {
3386         if (val->inherit) {
3387             return g_snprintf(p, len, "%s:inherit;", key);
3388         } else {
3389             gchar *val_quoted = css2_escape_quote(val->value);
3390             return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3391             g_free (val_quoted);
3392         }
3393     }
3394     return 0;
3398 /**
3399  *
3400  */
3401 static bool
3402 sp_length_differ(SPILength const *const a, SPILength const *const b)
3404     if (a->unit != b->unit) {
3405         if (a->unit == SP_CSS_UNIT_EM) return true;
3406         if (a->unit == SP_CSS_UNIT_EX) return true;
3407         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3408         if (b->unit == SP_CSS_UNIT_EM) return true;
3409         if (b->unit == SP_CSS_UNIT_EX) return true;
3410         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3411     }
3413     return (a->computed != b->computed);
3418 /**
3419  * Write SPILength object into string.
3420  */
3421 static gint
3422 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3423                        SPILength const *const val, SPILength const *const base, guint const flags)
3425     Inkscape::CSSOStringStream os;
3427     if ((flags & SP_STYLE_FLAG_ALWAYS)
3428         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3429         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3430             && (!base->set || sp_length_differ(val, base))))
3431     {
3432         if (val->inherit) {
3433             return g_snprintf(p, len, "%s:inherit;", key);
3434         } else {
3435             switch (val->unit) {
3436                 case SP_CSS_UNIT_NONE:
3437                     os << key << ":" << val->computed << ";";
3438                     return g_strlcpy(p, os.str().c_str(), len);
3439                     break;
3440                 case SP_CSS_UNIT_PX:
3441                     os << key << ":" << val->computed << "px;";
3442                     return g_strlcpy(p, os.str().c_str(), len);
3443                     break;
3444                 case SP_CSS_UNIT_PT:
3445                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3446                     return g_strlcpy(p, os.str().c_str(), len);
3447                     break;
3448                 case SP_CSS_UNIT_PC:
3449                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3450                     return g_strlcpy(p, os.str().c_str(), len);
3451                     break;
3452                 case SP_CSS_UNIT_MM:
3453                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3454                     return g_strlcpy(p, os.str().c_str(), len);
3455                     break;
3456                 case SP_CSS_UNIT_CM:
3457                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3458                     return g_strlcpy(p, os.str().c_str(), len);
3459                     break;
3460                 case SP_CSS_UNIT_IN:
3461                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3462                     return g_strlcpy(p, os.str().c_str(), len);
3463                     break;
3464                 case SP_CSS_UNIT_EM:
3465                     os << key << ":" << val->value << "em;";
3466                     return g_strlcpy(p, os.str().c_str(), len);
3467                     break;
3468                 case SP_CSS_UNIT_EX:
3469                     os << key << ":" << val->value << "ex;";
3470                     return g_strlcpy(p, os.str().c_str(), len);
3471                     break;
3472                 case SP_CSS_UNIT_PERCENT:
3473                     os << key << ":" << (val->value * 100.0) << "%;";
3474                     return g_strlcpy(p, os.str().c_str(), len);
3475                     break;
3476                 default:
3477                     /* Invalid */
3478                     break;
3479             }
3480         }
3481     }
3482     return 0;
3486 /**
3487  *
3488  */
3489 static bool
3490 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3492     if (a->normal != b->normal) return true;
3493     if (a->normal) return false;
3495     if (a->unit != b->unit) {
3496         if (a->unit == SP_CSS_UNIT_EM) return true;
3497         if (a->unit == SP_CSS_UNIT_EX) return true;
3498         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3499         if (b->unit == SP_CSS_UNIT_EM) return true;
3500         if (b->unit == SP_CSS_UNIT_EX) return true;
3501         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3502     }
3504     return (a->computed != b->computed);
3507 /**
3508  * Write SPILengthOrNormal object into string.
3509  */
3510 static gint
3511 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3512                                SPILengthOrNormal const *const val,
3513                                SPILengthOrNormal const *const base,
3514                                guint const flags)
3516     if ((flags & SP_STYLE_FLAG_ALWAYS)
3517         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3518         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3519             && (!base->set || sp_lengthornormal_differ(val, base))))
3520     {
3521         if (val->normal) {
3522             return g_snprintf(p, len, "%s:normal;", key);
3523         } else {
3524             SPILength length;
3525             length.set = val->set;
3526             length.inherit = val->inherit;
3527             length.unit = val->unit;
3528             length.value = val->value;
3529             length.computed = val->computed;
3530             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3531         }
3532     }
3533     return 0;
3536 /**
3537  *
3538  */
3539 static bool
3540 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3542     return    a->underline != b->underline
3543            || a->overline != b->overline
3544            || a->line_through != b->line_through
3545            || a->blink != b->blink;
3548 /**
3549  * Write SPITextDecoration object into string.
3550  */
3551 static gint
3552 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3553                                SPITextDecoration const *const val,
3554                                SPITextDecoration const *const base,
3555                                guint const flags)
3557     Inkscape::CSSOStringStream os;
3559     if ((flags & SP_STYLE_FLAG_ALWAYS)
3560         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3561         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3562             && (!base->set || sp_textdecoration_differ(val, base))))
3563     {
3564         if (val->inherit) {
3565             return g_snprintf(p, len, "%s:inherit;", key);
3566         } else {
3567             os << key << ":";
3568             if (val->underline || val->overline || val->line_through || val->blink) {
3569                 if (val->underline) os << " underline";
3570                 if (val->overline) os << " overline";
3571                 if (val->line_through) os << " line-through";
3572                 if (val->blink) os << " blink";
3573             } else
3574                 os << "none";
3575             os << ";";
3576             return g_strlcpy(p, os.str().c_str(), len);
3577         }
3578     }
3579     return 0;
3582 /**
3583  *
3584  */
3585 static bool
3586 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3588     if ( (a->isColor() != b->isColor())
3589          || (a->isPaintserver() != b->isPaintserver())
3590          || (a->set != b->set)
3591          || (a->currentcolor != b->currentcolor)
3592          || (a->inherit!= b->inherit) ) {
3593         return true;
3594     }
3596     // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3598     if ( a->isPaintserver() ) {
3599         return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3600     }
3602     if ( a->isColor() ) {
3603         return !( (a->value.color == b->value.color)
3604                  && ((a->value.color.icc == b->value.color.icc)
3605                      || (a->value.color.icc && b->value.color.icc
3606                          && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3607                          && (a->value.color.icc->colors == b->value.color.icc->colors))));
3608     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3609      * in the string representation. */
3610     }
3612     return false;
3617 /**
3618  * Write SPIPaint object into string.
3619  */
3620 static gint
3621 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3622                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3624     int retval = 0;
3626     if ((flags & SP_STYLE_FLAG_ALWAYS)
3627         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3628         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3629             && (!base->set || sp_paint_differ(paint, base))))
3630     {
3631         CSSOStringStream css;
3633         if (paint->inherit) {
3634             css << "inherit";
3635         } else {
3636             if ( paint->value.href && paint->value.href->getURI() ) {
3637                 const gchar* uri = paint->value.href->getURI()->toString();
3638                 css << "url(" << uri << ")";
3639             }
3641             if ( paint->noneSet ) {
3642                 if ( !css.str().empty() ) {
3643                     css << " ";
3644                 }
3645                 css << "none";
3646             }
3648             if ( paint->currentcolor ) {
3649                 if ( !css.str().empty() ) {
3650                     css << " ";
3651                 }
3652                 css << "currentColor";
3653             }
3655             if ( paint->colorSet && !paint->currentcolor ) {
3656                 if ( !css.str().empty() ) {
3657                     css << " ";
3658                 }
3659                 char color_buf[8];
3660                 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3661                 css << color_buf;
3662             }
3664             if (paint->value.color.icc && !paint->currentcolor) {
3665                 if ( !css.str().empty() ) {
3666                     css << " ";
3667                 }
3668                 css << "icc-color(" << paint->value.color.icc->colorProfile;
3669                 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3670                          iEnd(paint->value.color.icc->colors.end());
3671                      i != iEnd; ++i) {
3672                     css << ", " << *i;
3673                 }
3674                 css << ')';
3675             }
3676         }
3678         if ( !css.str().empty() ) {
3679             retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3680         }
3681     }
3683     return retval;
3687 /**
3688  *
3689  */
3690 static bool
3691 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3693     if (a->type != b->type)
3694         return true;
3695     if (a->type == SP_FONT_SIZE_LENGTH) {
3696         if (a->computed != b->computed)
3697             return true;
3698     } else {
3699         if (a->value != b->value)
3700             return true;
3701     }
3702     return false;
3706 /**
3707  * Write SPIFontSize object into string.
3708  */
3709 static gint
3710 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3711                          SPIFontSize const *const val, SPIFontSize const *const base,
3712                          guint const flags)
3714     if ((flags & SP_STYLE_FLAG_ALWAYS)
3715         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3716         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3717             && (!base->set || sp_fontsize_differ(val, base))))
3718     {
3719         if (val->inherit) {
3720             return g_snprintf(p, len, "%s:inherit;", key);
3721         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3722             for (unsigned i = 0; enum_font_size[i].key; i++) {
3723                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3724                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3725                 }
3726             }
3727         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3728             Inkscape::CSSOStringStream os;
3729             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3730             return g_strlcpy(p, os.str().c_str(), len);
3731         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3732             Inkscape::CSSOStringStream os;
3733             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3734             return g_strlcpy(p, os.str().c_str(), len);
3735         }
3736     }
3737     return 0;
3741 /**
3742  * Write SPIFilter object into string.
3743  */
3744 static gint
3745 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3746                          SPIFilter const *const val, SPIFilter const *const base,
3747                          guint const flags)
3749     (void)base; // TODO
3750     if ((flags & SP_STYLE_FLAG_ALWAYS)
3751         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3752         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3753     {
3754         if (val->inherit) {
3755             return g_snprintf(p, len, "%s:inherit;", key);
3756         } else if (val->href && val->href->getURI()) {
3757             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
3758         }
3759     }
3762     return 0;
3766 void SPIPaint::clear()
3768     set = false;
3769     inherit = false;
3770     currentcolor = false;
3771     colorSet = false;
3772     noneSet = false;
3773     value.color.set( 0 );
3774     if ( value.href && value.href->getObject() )
3775     {
3776         value.href->detach();
3777     }
3781 /**
3782  * Clear filter object, and disconnect style from paintserver (if present).
3783  */
3784 static void
3785 sp_style_filter_clear(SPStyle *style)
3787     if (style->filter.href && style->filter.href->getObject())
3788         style->filter.href->detach();
3792 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3794 void
3795 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3797     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3799     if (repr == NULL) return;
3801     g_return_if_fail(linked != NULL);
3803     gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3805     SPCSSAttr *css = sp_repr_css_attr_new();
3806     sp_repr_css_set_property(css, property, val);
3807     g_free(val);
3808     if (recursive) {
3809         sp_repr_css_change_recursive(repr, css, "style");
3810     } else {
3811         sp_repr_css_change(repr, css, "style");
3812     }
3813     sp_repr_css_attr_unref(css);
3817 /**
3818  * Clear all style property attributes in object.
3819  */
3820 void
3821 sp_style_unset_property_attrs(SPObject *o)
3823     if (!o) return;
3825     SPStyle *style = SP_OBJECT_STYLE(o);
3826     if (!style) return;
3828     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3829     if (!repr) return;
3831     if (style->opacity.set) {
3832         repr->setAttribute("opacity", NULL);
3833     }
3834     if (style->color.set) {
3835         repr->setAttribute("color", NULL);
3836     }
3837     if (style->fill.set) {
3838         repr->setAttribute("fill", NULL);
3839     }
3840     if (style->fill_opacity.set) {
3841         repr->setAttribute("fill-opacity", NULL);
3842     }
3843     if (style->fill_rule.set) {
3844         repr->setAttribute("fill-rule", NULL);
3845     }
3846     if (style->stroke.set) {
3847         repr->setAttribute("stroke", NULL);
3848     }
3849     if (style->stroke_width.set) {
3850         repr->setAttribute("stroke-width", NULL);
3851     }
3852     if (style->stroke_linecap.set) {
3853         repr->setAttribute("stroke-linecap", NULL);
3854     }
3855     if (style->stroke_linejoin.set) {
3856         repr->setAttribute("stroke-linejoin", NULL);
3857     }
3858     if (style->marker[SP_MARKER_LOC].set) {
3859         repr->setAttribute("marker", NULL);
3860     }
3861     if (style->marker[SP_MARKER_LOC_START].set) {
3862         repr->setAttribute("marker-start", NULL);
3863     }
3864     if (style->marker[SP_MARKER_LOC_MID].set) {
3865         repr->setAttribute("marker-mid", NULL);
3866     }
3867     if (style->marker[SP_MARKER_LOC_END].set) {
3868         repr->setAttribute("marker-end", NULL);
3869     }
3870     if (style->stroke_opacity.set) {
3871         repr->setAttribute("stroke-opacity", NULL);
3872     }
3873     if (style->stroke_dasharray_set) {
3874         repr->setAttribute("stroke-dasharray", NULL);
3875     }
3876     if (style->stroke_dashoffset_set) {
3877         repr->setAttribute("stroke-dashoffset", NULL);
3878     }
3879     if (style->text_private && style->text->font_specification.set) {
3880         repr->setAttribute("-inkscape-font-specification", NULL);
3881     }
3882     if (style->text_private && style->text->font_family.set) {
3883         repr->setAttribute("font-family", NULL);
3884     }
3885     if (style->text_anchor.set) {
3886         repr->setAttribute("text-anchor", NULL);
3887     }
3888     if (style->writing_mode.set) {
3889         repr->setAttribute("writing_mode", NULL);
3890     }
3891     if (style->filter.set) {
3892         repr->setAttribute("filter", NULL);
3893     }
3894     if (style->enable_background.set) {
3895         repr->setAttribute("enable-background", NULL);
3896     }
3899 /**
3900  * \pre style != NULL.
3901  * \pre flags in {IFSET, ALWAYS}.
3902  */
3903 SPCSSAttr *
3904 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3906     g_return_val_if_fail(style != NULL, NULL);
3907     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3908                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3909                          NULL);
3910     gchar *style_str = sp_style_write_string(style, flags);
3911     SPCSSAttr *css = sp_repr_css_attr_new();
3912     sp_repr_css_attr_add_from_string(css, style_str);
3913     g_free(style_str);
3914     return css;
3918 /**
3919  * \pre object != NULL
3920  * \pre flags in {IFSET, ALWAYS}.
3921  */
3922 SPCSSAttr *
3923 sp_css_attr_from_object(SPObject *object, guint const flags)
3925     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3926                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3927                          NULL);
3928     SPStyle const *const style = SP_OBJECT_STYLE(object);
3929     if (style == NULL)
3930         return NULL;
3931     return sp_css_attr_from_style(style, flags);
3934 /**
3935  * Unset any text-related properties
3936  */
3937 SPCSSAttr *
3938 sp_css_attr_unset_text(SPCSSAttr *css)
3940     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3941     sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
3942     sp_repr_css_set_property(css, "font-size", NULL);
3943     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3944     sp_repr_css_set_property(css, "font-style", NULL);
3945     sp_repr_css_set_property(css, "font-variant", NULL);
3946     sp_repr_css_set_property(css, "font-weight", NULL);
3947     sp_repr_css_set_property(css, "font-stretch", NULL);
3948     sp_repr_css_set_property(css, "font-family", NULL);
3949     sp_repr_css_set_property(css, "text-indent", NULL);
3950     sp_repr_css_set_property(css, "text-align", NULL);
3951     sp_repr_css_set_property(css, "text-decoration", NULL);
3952     sp_repr_css_set_property(css, "line-height", NULL);
3953     sp_repr_css_set_property(css, "letter-spacing", NULL);
3954     sp_repr_css_set_property(css, "word-spacing", NULL);
3955     sp_repr_css_set_property(css, "text-transform", NULL);
3956     sp_repr_css_set_property(css, "direction", NULL);
3957     sp_repr_css_set_property(css, "block-progression", NULL);
3958     sp_repr_css_set_property(css, "writing-mode", NULL);
3959     sp_repr_css_set_property(css, "text-anchor", NULL);
3960     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3961     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3962     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3963     sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3965     return css;
3968 bool
3969 is_url(char const *p)
3971     if (p == NULL)
3972         return false;
3973 /** \todo
3974  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3975  * in property values must start with 'url('.
3976  */
3977     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3980 /**
3981  * Unset any properties that contain URI values.
3982  *
3983  * Used for storing style that will be reused across documents when carrying
3984  * the referenced defs is impractical.
3985  */
3986 SPCSSAttr *
3987 sp_css_attr_unset_uris(SPCSSAttr *css)
3989 // All properties that may hold <uri> or <paint> according to SVG 1.1
3990     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3991     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3992     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3993     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3994     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3995     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3996     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3997     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3998     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3999     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
4001     return css;
4004 /**
4005  * Scale a single-value property.
4006  */
4007 void
4008 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
4009                                   double ex, bool only_with_units = false)
4011     gchar const *w = sp_repr_css_property(css, property, NULL);
4012     if (w) {
4013         gchar *units = NULL;
4014         double wd = g_ascii_strtod(w, &units) * ex;
4015         if (w == units) {// nothing converted, non-numeric value
4016             return;
4017         }
4018         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
4019             // only_with_units, but no units found, so do nothing.
4020             return;
4021         }
4022         Inkscape::CSSOStringStream os;
4023         os << wd << units; // reattach units
4024         sp_repr_css_set_property(css, property, os.str().c_str());
4025     }
4028 /**
4029  * Scale a list-of-values property.
4030  */
4031 void
4032 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
4034     gchar const *string = sp_repr_css_property(css, property, NULL);
4035     if (string) {
4036         Inkscape::CSSOStringStream os;
4037         gchar **a = g_strsplit(string, ",", 10000);
4038         bool first = true;
4039         for (gchar **i = a; i != NULL; i++) {
4040             gchar *w = *i;
4041             if (w == NULL)
4042                 break;
4043             gchar *units = NULL;
4044             double wd = g_ascii_strtod(w, &units) * ex;
4045             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4046                 g_strfreev(a);
4047                 return;
4048             }
4049             if (!first) {
4050                 os << ",";
4051             }
4052             os << wd << units; // reattach units
4053             first = false;
4054         }
4055         sp_repr_css_set_property(css, property, os.str().c_str());
4056         g_strfreev(a);
4057     }
4060 /**
4061  * Scale any properties that may hold <length> by ex.
4062  */
4063 SPCSSAttr *
4064 sp_css_attr_scale(SPCSSAttr *css, double ex)
4066     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4067     sp_css_attr_scale_property_single(css, "stroke-width", ex);
4068     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
4069     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4070     sp_css_attr_scale_property_single(css, "font-size", ex);
4071     sp_css_attr_scale_property_single(css, "kerning", ex);
4072     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4073     sp_css_attr_scale_property_single(css, "word-spacing", ex);
4074     sp_css_attr_scale_property_single(css, "line-height", ex, true);
4076     return css;
4080 /**
4081  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4082  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4083  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4084  * it parsed for us. So this function is only used to remove those quotes and for
4085  * presentation attributes, without any unescaping. (XML unescaping
4086  * (&amp; etc) is done by XML parser.)
4087  */
4088 gchar *
4089 attribute_unquote(gchar const *val)
4091     if (val) {
4092         if (*val == '\'' || *val == '"') {
4093             int l = strlen(val);
4094             if (l >= 2) {
4095                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4096                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4097                     return (g_strndup (val+1, l-2));
4098                 }
4099             }
4100         }
4101     }
4103     return (val? g_strdup (val) : NULL);
4106 /**
4107  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4108  */
4109 gchar *
4110 css2_escape_quote(gchar const *val) {
4112     Glib::ustring t;
4113     bool quote = false;
4114     bool last_was_space = false;
4116     for (gchar const *i = val; *i; i++) {
4117         bool is_space = ( *i == ' ' );
4118         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4119             // ASCII alphanumeric, - and _ don't require quotes
4120             t.push_back(*i);
4121         } else if ( is_space && !last_was_space ) {
4122             // non-consecutive spaces don't require quotes
4123             t.push_back(*i);
4124         } else if (*i=='\'') {
4125             // single quotes require escaping and quotes
4126             t.push_back('\\');
4127             t.push_back(*i);
4128             quote = true;
4129         } else {
4130             // everything else requires quotes
4131             t.push_back(*i);
4132             quote = true;
4133         }
4134         if (i == val && !g_ascii_isalpha(*i)) {
4135             // a non-ASCII/non-alpha initial character requires quotes
4136             quote = true;
4137         }
4138         last_was_space = is_space;
4139     }
4141     if (last_was_space) {
4142         // a trailing space requires quotes
4143         quote = true;
4144     }
4146     if (quote) {
4147         // we use single quotes so the result can be stored in an XML
4148         // attribute without incurring un-aesthetic additional quoting
4149         // (our XML emitter always uses double quotes)
4150         t.insert(t.begin(), '\'');
4151         t.push_back('\'');
4152     }
4154     return (t.empty() ? NULL : g_strdup (t.c_str()));
4157 /*
4158   Local Variables:
4159   mode:c++
4160   c-file-style:"stroustrup"
4161   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4162   indent-tabs-mode:nil
4163   fill-column:99
4164   End:
4165 */
4166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :