Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / style.cpp
1 #define __SP_STYLE_C__
3 /** @file
4  * @brief 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 "xml/simple-document.h"
45 #include "unit-constants.h"
46 #include "2geom/isnan.h"
47 #include "macros.h"
48 #include "preferences.h"
50 #include "sp-filter-reference.h"
52 #include <sigc++/functors/ptr_fun.h>
53 #include <sigc++/adaptors/bind.h>
55 using Inkscape::CSSOStringStream;
56 using std::vector;
58 #define BMAX 8192
60 class SPStyleEnum;
62 /*#########################
63 ## FORWARD DECLARATIONS
64 #########################*/
65 static void sp_style_clear(SPStyle *style);
67 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
69 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
70 static void sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent);
71 static void sp_style_read_dash(SPStyle *style, gchar const *str);
73 static SPTextStyle *sp_text_style_new(void);
74 static void sp_text_style_clear(SPTextStyle *ts);
75 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
76 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
77 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
78 static void sp_style_privatize_text(SPStyle *style);
80 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
81 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
82 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
83 static void sp_style_read_istring(SPIString *val, gchar const *str);
84 static void sp_style_read_ilength(SPILength *val, gchar const *str);
85 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
86 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
87 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
88 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
89 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
90 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
92 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
93 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
94 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
95 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
97 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
98 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
99 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
100 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
101 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
102 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
103 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
104 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);
105 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);
106 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
108 static void sp_style_filter_clear(SPStyle *style);
110 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
111 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
113 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
114 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
116 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
118 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
119 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
121 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
123 struct SPStyleEnum {
124     gchar const *key;
125     gint value;
126 };
128 static SPStyleEnum const enum_fill_rule[] = {
129     {"nonzero", SP_WIND_RULE_NONZERO},
130     {"evenodd", SP_WIND_RULE_EVENODD},
131     {NULL, -1}
132 };
134 static SPStyleEnum const enum_stroke_linecap[] = {
135     {"butt", SP_STROKE_LINECAP_BUTT},
136     {"round", SP_STROKE_LINECAP_ROUND},
137     {"square", SP_STROKE_LINECAP_SQUARE},
138     {NULL, -1}
139 };
141 static SPStyleEnum const enum_stroke_linejoin[] = {
142     {"miter", SP_STROKE_LINEJOIN_MITER},
143     {"round", SP_STROKE_LINEJOIN_ROUND},
144     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
145     {NULL, -1}
146 };
148 static SPStyleEnum const enum_font_style[] = {
149     {"normal", SP_CSS_FONT_STYLE_NORMAL},
150     {"italic", SP_CSS_FONT_STYLE_ITALIC},
151     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
152     {NULL, -1}
153 };
155 static SPStyleEnum const enum_font_size[] = {
156     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
157     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
158     {"small", SP_CSS_FONT_SIZE_SMALL},
159     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
160     {"large", SP_CSS_FONT_SIZE_LARGE},
161     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
162     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
163     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
164     {"larger", SP_CSS_FONT_SIZE_LARGER},
165     {NULL, -1}
166 };
168 static SPStyleEnum const enum_font_variant[] = {
169     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
170     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
171     {NULL, -1}
172 };
174 static SPStyleEnum const enum_font_weight[] = {
175     {"100", SP_CSS_FONT_WEIGHT_100},
176     {"200", SP_CSS_FONT_WEIGHT_200},
177     {"300", SP_CSS_FONT_WEIGHT_300},
178     {"400", SP_CSS_FONT_WEIGHT_400},
179     {"500", SP_CSS_FONT_WEIGHT_500},
180     {"600", SP_CSS_FONT_WEIGHT_600},
181     {"700", SP_CSS_FONT_WEIGHT_700},
182     {"800", SP_CSS_FONT_WEIGHT_800},
183     {"900", SP_CSS_FONT_WEIGHT_900},
184     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
185     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
186     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
187     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
188     {NULL, -1}
189 };
191 static SPStyleEnum const enum_font_stretch[] = {
192     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
193     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
194     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
195     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
196     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
197     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
198     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
199     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
200     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
201     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
202     {"wider", SP_CSS_FONT_STRETCH_WIDER},
203     {NULL, -1}
204 };
206 static SPStyleEnum const enum_text_align[] = {
207     {"start", SP_CSS_TEXT_ALIGN_START},
208     {"end", SP_CSS_TEXT_ALIGN_END},
209     {"left", SP_CSS_TEXT_ALIGN_LEFT},
210     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
211     {"center", SP_CSS_TEXT_ALIGN_CENTER},
212     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
213     {NULL, -1}
214 };
216 static SPStyleEnum const enum_text_transform[] = {
217     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
218     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
219     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
220     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
221     {NULL, -1}
222 };
224 static SPStyleEnum const enum_text_anchor[] = {
225     {"start", SP_CSS_TEXT_ANCHOR_START},
226     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
227     {"end", SP_CSS_TEXT_ANCHOR_END},
228     {NULL, -1}
229 };
231 static SPStyleEnum const enum_direction[] = {
232     {"ltr", SP_CSS_DIRECTION_LTR},
233     {"rtl", SP_CSS_DIRECTION_RTL},
234     {NULL, -1}
235 };
237 static SPStyleEnum const enum_block_progression[] = {
238     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
239     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
240     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
241     {NULL, -1}
242 };
244 static SPStyleEnum const enum_writing_mode[] = {
245     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
246      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
247      * value is lr-tb rather than lr.
248      *
249      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
250      * sharing enumerators for different strings may be a bug (once we support ecma script).
251      */
252     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
253     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
254     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
255     {"lr", SP_CSS_WRITING_MODE_LR_TB},
256     {"rl", SP_CSS_WRITING_MODE_RL_TB},
257     {"tb", SP_CSS_WRITING_MODE_TB_RL},
258     {NULL, -1}
259 };
261 static SPStyleEnum const enum_visibility[] = {
262     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
263     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
264     {"visible", SP_CSS_VISIBILITY_VISIBLE},
265     {NULL, -1}
266 };
268 static SPStyleEnum const enum_overflow[] = {
269     {"visible", SP_CSS_OVERFLOW_VISIBLE},
270     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
271     {"scroll", SP_CSS_OVERFLOW_SCROLL},
272     {"auto", SP_CSS_OVERFLOW_AUTO},
273     {NULL, -1}
274 };
276 static SPStyleEnum const enum_display[] = {
277     {"none",      SP_CSS_DISPLAY_NONE},
278     {"inline",    SP_CSS_DISPLAY_INLINE},
279     {"block",     SP_CSS_DISPLAY_BLOCK},
280     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
281     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
282     {"compact",   SP_CSS_DISPLAY_COMPACT},
283     {"marker",    SP_CSS_DISPLAY_MARKER},
284     {"table",     SP_CSS_DISPLAY_TABLE},
285     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
286     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
287     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
288     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
289     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
290     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
291     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
292     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
293     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
294     {NULL, -1}
295 };
297 static SPStyleEnum const enum_shape_rendering[] = {
298     {"auto", 0},
299     {"optimizeSpeed", 0},
300     {"crispEdges", 0},
301     {"geometricPrecision", 0},
302     {NULL, -1}
303 };
305 static SPStyleEnum const enum_color_rendering[] = {
306     {"auto", 0},
307     {"optimizeSpeed", 0},
308     {"optimizeQuality", 0},
309     {NULL, -1}
310 };
312 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
314 static SPStyleEnum const enum_text_rendering[] = {
315     {"auto", 0},
316     {"optimizeSpeed", 0},
317     {"optimizeLegibility", 0},
318     {"geometricPrecision", 0},
319     {NULL, -1}
320 };
322 static SPStyleEnum const enum_enable_background[] = {
323     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
324     {"new", SP_CSS_BACKGROUND_NEW},
325     {NULL, -1}
326 };
328 /**
329  * Release callback.
330  */
331 static void
332 sp_style_object_release(SPObject *object, SPStyle *style)
334     (void)object; // TODO
335     style->object = NULL;
338 /**
339  * Emit style modified signal on style's object if the filter changed.
340  */
341 static void
342 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
344     (void)flags; // TODO
345     SPFilter *filter=static_cast<SPFilter *>(obj);
346     if (style->getFilter() == filter)
347     {
348         if (style->object) {
349             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
350         }
351     }
354 /**
355  * Gets called when the filter is (re)attached to the style
356  */
357 static void
358 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
360     if (old_ref) {
361         style->filter_modified_connection.disconnect();
362     }
363     if ( SP_IS_FILTER(ref))
364     {
365         style->filter_modified_connection = 
366            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
367     }
369     sp_style_filter_ref_modified(ref, 0, style);
372 /**
373  * Emit style modified signal on style's object if server is style's fill
374  * or stroke paint server.
375  */
376 static void
377 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
379     (void)flags; // TODO
380     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
382     if ((style->fill.isPaintserver())
383         && style->getFillPaintServer() == server)
384     {
385         if (style->object) {
386             /** \todo
387              * fixme: I do not know, whether it is optimal - we are
388              * forcing reread of everything (Lauris)
389              */
390             /** \todo
391              * fixme: We have to use object_modified flag, because parent
392              * flag is only available downstreams.
393              */
394             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
395         }
396     } else if ((style->stroke.isPaintserver())
397         && style->getStrokePaintServer() == server)
398     {
399         if (style->object) {
400             /// \todo fixme:
401             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
402         }
403     } else if (server) {
404         g_assert_not_reached();
405     }
408 /**
409  * Gets called when the paintserver is (re)attached to the style
410  */
411 static void
412 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
414     if (old_ref) {
415         style->fill_ps_modified_connection.disconnect();
416     }
417     if (SP_IS_PAINT_SERVER(ref)) {
418         style->fill_ps_modified_connection = 
419            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
420     }
422     sp_style_paint_server_ref_modified(ref, 0, style);
425 /**
426  * Gets called when the paintserver is (re)attached to the style
427  */
428 static void
429 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
431     if (old_ref) {
432         style->stroke_ps_modified_connection.disconnect();
433     }
434     if (SP_IS_PAINT_SERVER(ref)) {
435         style->stroke_ps_modified_connection = 
436           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
437     }
439     sp_style_paint_server_ref_modified(ref, 0, style);
442 /**
443  * Returns a new SPStyle object with settings as per sp_style_clear().
444  */
445 SPStyle *
446 sp_style_new(SPDocument *document)
448     SPStyle *const style = g_new0(SPStyle, 1);
450     style->refcount = 1;
451     style->object = NULL;
452     style->document = document;
453     style->text = sp_text_style_new();
454     style->text_private = TRUE;
456     sp_style_clear(style);
458     style->cloned = false;
460     new (&style->release_connection) sigc::connection();
461     new (&style->filter_modified_connection) sigc::connection();
462     new (&style->fill_ps_modified_connection) sigc::connection();
463     new (&style->stroke_ps_modified_connection) sigc::connection();
465     return style;
469 /**
470  * Creates a new SPStyle object, and attaches it to the specified SPObject.
471  */
472 SPStyle *
473 sp_style_new_from_object(SPObject *object)
475     g_return_val_if_fail(object != NULL, NULL);
476     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
478     SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
479     style->object = object;
480     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
482     if (object && SP_OBJECT_IS_CLONED(object)) {
483         style->cloned = true;
484     }
486     return style;
490 /**
491  * Increase refcount of style.
492  */
493 SPStyle *
494 sp_style_ref(SPStyle *style)
496     g_return_val_if_fail(style != NULL, NULL);
497     g_return_val_if_fail(style->refcount > 0, NULL);
499     style->refcount += 1;
501     return style;
505 /**
506  * Decrease refcount of style with possible destruction.
507  */
508 SPStyle *
509 sp_style_unref(SPStyle *style)
511     g_return_val_if_fail(style != NULL, NULL);
512     g_return_val_if_fail(style->refcount > 0, NULL);
514     style->refcount -= 1;
516     if (style->refcount < 1) {
517         style->release_connection.disconnect();
518         style->release_connection.~connection();
519         if (style->text) sp_text_style_unref(style->text);
521          if (style->fill.value.href) {
522              style->fill_ps_modified_connection.disconnect();
523              delete style->fill.value.href;
524              style->fill.value.href = NULL;
525          }
526          if (style->stroke.value.href) {
527              style->stroke_ps_modified_connection.disconnect();
528              delete style->stroke.value.href;
529              style->stroke.value.href = NULL;
530          }
531          if (style->filter.href) {
532              style->filter_modified_connection.disconnect();
533              delete style->filter.href;
534              style->filter.href = NULL;
535          }
537         style->filter_modified_connection.~connection();
538         style->fill_ps_modified_connection.~connection();
539         style->stroke_ps_modified_connection.~connection();
541         style->fill.clear();
542         style->stroke.clear();
543         sp_style_filter_clear(style);
545         g_free(style->stroke_dash.dash);
546         g_free(style);
547     }
549     return NULL;
552 /**
553  *  Reads the various style parameters for an object from repr.
554  */
555 static void
556 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
558     g_assert(style != NULL);
559     g_assert(repr != NULL);
560     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
562     sp_style_clear(style);
564     if (object && SP_OBJECT_IS_CLONED(object)) {
565         style->cloned = true;
566     }
568     /* 1. Style attribute */
569     gchar const *val = repr->attribute("style");
570     if (val != NULL) {
571         sp_style_merge_from_style_string(style, val);
572     }
574     if (object) {
575         sp_style_merge_from_object_stylesheet(style, object);
576     } else {
577         /** \todo No stylesheet information. Find out under what circumstances
578          * this occurs, and handle accordingly.  (If we really wanted to, we
579          * could probably get stylesheets by going through repr->doc.)
580          */
581     }
583     /* 2. Presentation attributes */
584     /* CSS2 */
585     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
586     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
587     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
588     /* Font */
589     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
590     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
591     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
592     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
593     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
594     /* Text (css2 chapter 16) */
595     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
596     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
597     if (!style->text_decoration.set) {
598         val = repr->attribute("text-decoration");
599         if (val) {
600             sp_style_read_itextdecoration(&style->text_decoration, val);
601         }
602     }
603     if (!style->line_height.set) {
604         val = repr->attribute("line-height");
605         if (val) {
606             sp_style_read_ilengthornormal(&style->line_height, val);
607         }
608     }
609     if (!style->letter_spacing.set) {
610         val = repr->attribute("letter-spacing");
611         if (val) {
612             sp_style_read_ilengthornormal(&style->letter_spacing, val);
613         }
614     }
615     if (!style->word_spacing.set) {
616         val = repr->attribute("word-spacing");
617         if (val) {
618             sp_style_read_ilengthornormal(&style->word_spacing, val);
619         }
620     }
621     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
622     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
623     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
625     /* SVG */
626     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
627                             enum_writing_mode, true);
628     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
629                             enum_text_anchor, true);
631     /* opacity */
632     if (!style->opacity.set) {
633         val = repr->attribute("opacity");
634         if (val) {
635             sp_style_read_iscale24(&style->opacity, val);
636         }
637     }
638     /* color */
639     if (!style->color.set) {
640         val = repr->attribute("color");
641         if (val) {
642             sp_style_read_icolor(&style->color, val, style, ( object
643                                                               ? SP_OBJECT_DOCUMENT(object)
644                                                               : NULL ));
645         }
646     }
647     /* fill */
648     if (!style->fill.set) {
649         val = repr->attribute("fill");
650         if (val) {
651             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
652         }
653     }
654     /* fill-opacity */
655     if (!style->fill_opacity.set) {
656         val = repr->attribute("fill-opacity");
657         if (val) {
658             sp_style_read_iscale24(&style->fill_opacity, val);
659         }
660     }
661     /* fill-rule */
662     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
663     /* stroke */
664     if (!style->stroke.set) {
665         val = repr->attribute("stroke");
666         if (val) {
667             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
668         }
669     }
670     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
671     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
672     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
673     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
675     /* markers */
676     if (!style->marker[SP_MARKER_LOC].set) {
677         val = repr->attribute("marker");
678         if (val) {
679             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
680         }
681     }
682     if (!style->marker[SP_MARKER_LOC_START].set) {
683         val = repr->attribute("marker-start");
684         if (val) {
685             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
686         }
687     }
688     if (!style->marker[SP_MARKER_LOC_MID].set) {
689         val = repr->attribute("marker-mid");
690         if (val) {
691             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
692         }
693     }
694     if (!style->marker[SP_MARKER_LOC_END].set) {
695         val = repr->attribute("marker-end");
696         if (val) {
697             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
698         }
699     }
701     /* stroke-opacity */
702     if (!style->stroke_opacity.set) {
703         val = repr->attribute("stroke-opacity");
704         if (val) {
705             sp_style_read_iscale24(&style->stroke_opacity, val);
706         }
707     }
708     if (!style->stroke_dasharray_set) {
709         val = repr->attribute("stroke-dasharray");
710         if (val) {
711             sp_style_read_dash(style, val);
712         }
713     }
715     if (!style->stroke_dashoffset_set) {
716         val = repr->attribute("stroke-dashoffset");
717         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
718             style->stroke_dashoffset_set = TRUE;
719         } else if (val && !strcmp(val, "inherit")) {
720             style->stroke_dashoffset_set = TRUE;
721             style->stroke_dashoffset_inherit = TRUE;
722         } else {
723             style->stroke_dashoffset_set = FALSE;
724         }
725     }
726     
727     /* -inkscape-font-specification */
728     if (!style->text_private || !style->text->font_specification.set) {
729         val = repr->attribute("-inkscape-font-specification");
730         if (val) {
731             if (!style->text_private) sp_style_privatize_text(style);
732             gchar *val_unquoted = attribute_unquote(val);
733             sp_style_read_istring(&style->text->font_specification, val_unquoted);
734             if (val_unquoted) g_free (val_unquoted);
735         }
736     }
738     /* font-family */
739     if (!style->text_private || !style->text->font_family.set) {
740         val = repr->attribute("font-family");
741         if (val) {
742             if (!style->text_private) sp_style_privatize_text(style);
743             gchar *val_unquoted = attribute_unquote(val);
744             sp_style_read_istring(&style->text->font_family, val_unquoted);
745             if (val_unquoted) g_free (val_unquoted);
746         }
747     }
749     /* filter effects */
750     if (!style->filter.set) {
751         val = repr->attribute("filter");
752         if (val) {
753                       sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
754         }
755     }
756     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
757                             "enable-background", enum_enable_background, true);
759     /* 3. Merge from parent */
760     if (object) {
761         if (object->parent) {
762             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
763         }
764     } else {
765         if (sp_repr_parent(repr)) {
766             /// \todo fixme: This is not the prettiest thing (Lauris)
767             SPStyle *parent = sp_style_new(NULL);
768             sp_style_read(parent, NULL, sp_repr_parent(repr));
769             sp_style_merge_from_parent(style, parent);
770             sp_style_unref(parent);
771         }
772     }
776 /**
777  * Read style properties from object's repr.
778  *
779  * 1. Reset existing object style
780  * 2. Load current effective object style
781  * 3. Load i attributes from immediate parent (which has to be up-to-date)
782  */
783 void
784 sp_style_read_from_object(SPStyle *style, SPObject *object)
786     g_return_if_fail(style != NULL);
787     g_return_if_fail(object != NULL);
788     g_return_if_fail(SP_IS_OBJECT(object));
790     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
791     g_return_if_fail(repr != NULL);
793     sp_style_read(style, object, repr);
797 /**
798  * Read style properties from preferences.
799  * @param style The style to write to
800  * @param path Preferences directory from which the style should be read
801  */
802 void
803 sp_style_read_from_prefs(SPStyle *style, Glib::ustring const &path)
805     g_return_if_fail(style != NULL);
806     g_return_if_fail(path != "");
807     
808     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
810     // not optimal: we reconstruct the node based on the prefs, then pass it to
811     // sp_style_read for actual processing.
812     Inkscape::XML::SimpleDocument *tempdoc = new Inkscape::XML::SimpleDocument;
813     Inkscape::XML::Node *tempnode = tempdoc->createElement("temp");
814     
815     std::vector<Inkscape::Preferences::Entry> attrs = prefs->getAllEntries(path);
816     for (std::vector<Inkscape::Preferences::Entry>::iterator i = attrs.begin(); i != attrs.end(); ++i) {
817         tempnode->setAttribute(i->getEntryName().data(), i->getString().data());
818     }
820     sp_style_read(style, NULL, tempnode);
821     
822     Inkscape::GC::release(tempnode);
823     Inkscape::GC::release(tempdoc);
824     delete tempdoc;
829 /**
830  *
831  */
832 static void
833 sp_style_privatize_text(SPStyle *style)
835     SPTextStyle *text = style->text;
836     style->text = sp_text_style_duplicate_unset(style->text);
837     sp_text_style_unref(text);
838     style->text_private = TRUE;
842 /**
843  * Merge property into style.
844  *
845  * Should be called in order of highest to lowest precedence.
846  * E.g. for a single style string, call from the last declaration to the first,
847  * as CSS says that later declarations override earlier ones.
848  *
849  * \pre val != NULL.
850  */
851 static void
852 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
854     g_return_if_fail(val != NULL);
856     switch (id) {
857         case SP_PROP_INKSCAPE_FONT_SPEC:
858             if (!style->text_private) sp_style_privatize_text(style);
859             if (!style->text->font_specification.set) {
860                 gchar *val_unquoted = attribute_unquote(val);
861                 sp_style_read_istring(&style->text->font_specification, val_unquoted);
862                 if (val_unquoted) g_free (val_unquoted);
863             }
864             break;
865         /* CSS2 */
866         /* Font */
867         case SP_PROP_FONT_FAMILY:
868             if (!style->text_private) sp_style_privatize_text(style);
869             if (!style->text->font_family.set) {
870                 gchar *val_unquoted = attribute_unquote(val);
871                 sp_style_read_istring(&style->text->font_family, val_unquoted);
872                 if (val_unquoted) g_free (val_unquoted);
873             }
874             break;
875         case SP_PROP_FONT_SIZE:
876             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
877             break;
878         case SP_PROP_FONT_SIZE_ADJUST:
879             if (strcmp(val, "none") != 0) {
880                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
881             }
882             break;
883         case SP_PROP_FONT_STYLE:
884             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
885             break;
886         case SP_PROP_FONT_VARIANT:
887             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
888             break;
889         case SP_PROP_FONT_WEIGHT:
890             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
891             break;
892         case SP_PROP_FONT_STRETCH:
893             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
894             break;
895         case SP_PROP_FONT:
896             if (!style->text_private) sp_style_privatize_text(style);
897             if (!style->text->font.set) {
898                 g_free(style->text->font.value);
899                 style->text->font.value = g_strdup(val);
900                 style->text->font.set = TRUE;
901                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
902             }
903             break;
904             /* Text */
905         case SP_PROP_TEXT_INDENT:
906             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
907             break;
908         case SP_PROP_TEXT_ALIGN:
909             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
910             break;
911         case SP_PROP_TEXT_DECORATION:
912             if (!style->text_decoration.set) {
913                 sp_style_read_itextdecoration(&style->text_decoration, val);
914             }
915             break;
916         case SP_PROP_LINE_HEIGHT:
917             if (!style->line_height.set) {
918                 sp_style_read_ilengthornormal(&style->line_height, val);
919             }
920             break;
921         case SP_PROP_LETTER_SPACING:
922             if (!style->letter_spacing.set) {
923                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
924             }
925             break;
926         case SP_PROP_WORD_SPACING:
927             if (!style->word_spacing.set) {
928                 sp_style_read_ilengthornormal(&style->word_spacing, val);
929             }
930             break;
931         case SP_PROP_TEXT_TRANSFORM:
932             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
933             break;
934             /* Text (css3) */
935         case SP_PROP_DIRECTION:
936             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
937             break;
938         case SP_PROP_BLOCK_PROGRESSION:
939             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
940             break;
941         case SP_PROP_WRITING_MODE:
942             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
943             break;
944         case SP_PROP_TEXT_ANCHOR:
945             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
946             break;
947             /* Text (unimplemented) */
948         case SP_PROP_TEXT_RENDERING: {
949             /* Ignore the hint. */
950             SPIEnum dummy;
951             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
952             break;
953         }
954         case SP_PROP_ALIGNMENT_BASELINE:
955             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
956             break;
957         case SP_PROP_BASELINE_SHIFT:
958             g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
959             break;
960         case SP_PROP_DOMINANT_BASELINE:
961             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
962             break;
963         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
964             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
965             break;
966         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
967             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
968             break;
969         case SP_PROP_KERNING:
970             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
971             break;
972             /* Misc */
973         case SP_PROP_CLIP:
974             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
975             break;
976         case SP_PROP_COLOR:
977             if (!style->color.set) {
978                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
979             }
980             break;
981         case SP_PROP_CURSOR:
982             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
983             break;
984         case SP_PROP_DISPLAY:
985             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
986             break;
987         case SP_PROP_OVERFLOW:
988             /** \todo
989              * FIXME: not supported properly yet, we just read and write it,
990              * but act as if it is always "display".
991              */
992             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
993             break;
994         case SP_PROP_VISIBILITY:
995             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
996             break;
997             /* SVG */
998             /* Clip/Mask */
999         case SP_PROP_CLIP_PATH:
1000             /** \todo
1001              * This is a workaround. Inkscape only supports 'clip-path' as SVG attribute, not as
1002              * style property. By having both CSS and SVG attribute set, editing of clip-path
1003              * will fail, since CSS always overwrites SVG attributes.
1004              * Fixes Bug #324849
1005              */
1006             g_warning("attribute 'clip-path' given as CSS");
1007             style->object->repr->setAttribute("clip-path", val);
1008             break;
1009         case SP_PROP_CLIP_RULE:
1010             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
1011             break;
1012         case SP_PROP_MASK:
1013             /** \todo
1014              * See comment for SP_PROP_CLIP_PATH
1015              */
1016             g_warning("attribute 'mask' given as CSS");
1017             style->object->repr->setAttribute("mask", val);
1018             break;
1019         case SP_PROP_OPACITY:
1020             if (!style->opacity.set) {
1021                 sp_style_read_iscale24(&style->opacity, val);
1022             }
1023             break;
1024         case SP_PROP_ENABLE_BACKGROUND:
1025             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
1026                                     enum_enable_background, true);
1027             break;
1028             /* Filter */
1029         case SP_PROP_FILTER:
1030             if (!style->filter.set && !style->filter.inherit) {
1031                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1032             }
1033             break;
1034         case SP_PROP_FLOOD_COLOR:
1035             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
1036             break;
1037         case SP_PROP_FLOOD_OPACITY:
1038             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
1039             break;
1040         case SP_PROP_LIGHTING_COLOR:
1041             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
1042             break;
1043             /* Gradient */
1044         case SP_PROP_STOP_COLOR:
1045             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
1046             break;
1047         case SP_PROP_STOP_OPACITY:
1048             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
1049             break;
1050             /* Interactivity */
1051         case SP_PROP_POINTER_EVENTS:
1052             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
1053             break;
1054             /* Paint */
1055         case SP_PROP_COLOR_INTERPOLATION:
1056             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1057             break;
1058         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1059             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1060             break;
1061         case SP_PROP_COLOR_PROFILE:
1062             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1063             break;
1064         case SP_PROP_COLOR_RENDERING: {
1065             /* Ignore the hint. */
1066             SPIEnum dummy;
1067             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1068             break;
1069         }
1070         case SP_PROP_FILL:
1071             if (!style->fill.set) {
1072                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1073             }
1074             break;
1075         case SP_PROP_FILL_OPACITY:
1076             if (!style->fill_opacity.set) {
1077                 sp_style_read_iscale24(&style->fill_opacity, val);
1078             }
1079             break;
1080         case SP_PROP_FILL_RULE:
1081             if (!style->fill_rule.set) {
1082                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1083             }
1084             break;
1085         case SP_PROP_IMAGE_RENDERING: {
1086             /* Ignore the hint. */
1087             SPIEnum dummy;
1088             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1089             break;
1090         }
1091         case SP_PROP_MARKER:
1092             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1093             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1094             if (!style->marker[SP_MARKER_LOC].set) {
1095                 g_free(style->marker[SP_MARKER_LOC].value);
1096                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1097                 style->marker[SP_MARKER_LOC].set = TRUE;
1098                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1099             }
1100             break;
1102         case SP_PROP_MARKER_START:
1103             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1104             if (!style->marker[SP_MARKER_LOC_START].set) {
1105                 g_free(style->marker[SP_MARKER_LOC_START].value);
1106                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1107                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1108                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1109             }
1110             break;
1111         case SP_PROP_MARKER_MID:
1112             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1113             if (!style->marker[SP_MARKER_LOC_MID].set) {
1114                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1115                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1116                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1117                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1118             }
1119             break;
1120         case SP_PROP_MARKER_END:
1121             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1122             if (!style->marker[SP_MARKER_LOC_END].set) {
1123                 g_free(style->marker[SP_MARKER_LOC_END].value);
1124                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1125                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1126                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1127             }
1128             break;
1130         case SP_PROP_SHAPE_RENDERING: {
1131             /* Ignore the hint. */
1132             SPIEnum dummy;
1133             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1134             break;
1135         }
1137         case SP_PROP_STROKE:
1138             if (!style->stroke.set) {
1139                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1140             }
1141             break;
1142         case SP_PROP_STROKE_WIDTH:
1143             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1144             break;
1145         case SP_PROP_STROKE_DASHARRAY:
1146             if (!style->stroke_dasharray_set) {
1147                 sp_style_read_dash(style, val);
1148             }
1149             break;
1150         case SP_PROP_STROKE_DASHOFFSET:
1151             if (!style->stroke_dashoffset_set) {
1152                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1153                     style->stroke_dashoffset_set = TRUE;
1154                 } else if (val && !strcmp(val, "inherit")) {
1155                     style->stroke_dashoffset_set = TRUE;
1156                     style->stroke_dashoffset_inherit = TRUE;
1157                 } else {
1158                     style->stroke_dashoffset_set = FALSE;
1159                 }
1160             }
1161             break;
1162         case SP_PROP_STROKE_LINECAP:
1163             if (!style->stroke_linecap.set) {
1164                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1165             }
1166             break;
1167         case SP_PROP_STROKE_LINEJOIN:
1168             if (!style->stroke_linejoin.set) {
1169                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1170             }
1171             break;
1172         case SP_PROP_STROKE_MITERLIMIT:
1173             if (!style->stroke_miterlimit.set) {
1174                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1175             }
1176             break;
1177         case SP_PROP_STROKE_OPACITY:
1178             if (!style->stroke_opacity.set) {
1179                 sp_style_read_iscale24(&style->stroke_opacity, val);
1180             }
1181             break;
1183         default:
1184             g_warning("Invalid style property id: %d value: %s", id, val);
1185             break;
1186     }
1189 static void
1190 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1192     /** \todo Ensure that property is lcased, as per
1193      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1194      * Should probably be done in libcroco.
1195      */
1196     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1197     if (prop_idx != SP_ATTR_INVALID) {
1198         /** \todo
1199          * effic: Test whether the property is already set before trying to
1200          * convert to string. Alternatively, set from CRTerm directly rather
1201          * than converting to string.
1202          */
1203         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1204         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1205         sp_style_merge_property(style, prop_idx, str_value);
1206         g_free(str_value);
1207     }
1210 static void
1211 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1213 #if 0 /* forwards */
1214     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1215         CRDeclaration *decl = NULL;
1216         cr_prop_list_get_decl(cur, &decl);
1217         sp_style_merge_style_from_decl(style, decl);
1218     }
1219 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1220     if (props) {
1221         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1222         CRDeclaration *decl = NULL;
1223         cr_prop_list_get_decl(props, &decl);
1224         sp_style_merge_style_from_decl(style, decl);
1225     }
1226 #endif
1229 /**
1230  * \pre decl_list != NULL
1231  */
1232 static void
1233 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1235     // read the decls from end to start, using head recursion, so that latter declarations override
1236     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1237     // because sp_style_merge_style_from_decl only sets properties that are unset
1238     if (decl_list->next) {
1239         sp_style_merge_from_decl_list(style, decl_list->next);
1240     }
1241     sp_style_merge_style_from_decl(style, decl_list);
1244 static CRSelEng *
1245 sp_repr_sel_eng()
1247     CRSelEng *const ret = cr_sel_eng_new();
1248     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1250     /** \todo
1251      * Check whether we need to register any pseudo-class handlers.
1252      * libcroco has its own default handlers for first-child and lang.
1253      *
1254      * We probably want handlers for link and arguably visited (though
1255      * inkscape can't visit links at the time of writing).  hover etc.
1256      * more useful in inkview than the editor inkscape.
1257      *
1258      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1259      * the following should be honoured, at least by inkview:
1260      * :hover, :active, :focus, :visited, :link.
1261      */
1263     g_assert(ret);
1264     return ret;
1267 static void
1268 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1270     static CRSelEng *sel_eng = NULL;
1271     if (!sel_eng) {
1272         sel_eng = sp_repr_sel_eng();
1273     }
1275     CRPropList *props = NULL;
1276     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1277                                                                      object->document->style_cascade,
1278                                                                      object->repr,
1279                                                                      &props);
1280     g_return_if_fail(status == CR_OK);
1281     /// \todo Check what errors can occur, and handle them properly.
1282     if (props) {
1283         sp_style_merge_from_props(style, props);
1284         cr_prop_list_destroy(props);
1285     }
1288 /**
1289  * Parses a style="..." string and merges it with an existing SPStyle.
1290  */
1291 void
1292 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1294     /*
1295      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1296      * ``When CSS styling is used, CSS inline style is specified by including
1297      * semicolon-separated property declarations of the form "name : value"
1298      * within the style attribute''.
1299      *
1300      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1301      * Why does it say "including", what else is allowed in the style
1302      * attribute value?
1303      */
1305     CRDeclaration *const decl_list
1306         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1307     if (decl_list) {
1308         sp_style_merge_from_decl_list(style, decl_list);
1309         cr_declaration_destroy(decl_list);
1310     }
1313 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1314 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1316 static void
1317 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1319     /* 'font-size' */
1320     if (!child.set || child.inherit) {
1321         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1322         child.computed = parent.computed;
1323     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1324         /** \todo
1325          * fixme: SVG and CSS do not specify clearly, whether we should use
1326          * user or screen coordinates (Lauris)
1327          */
1328         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1329             child.computed = font_size_table[child.value];
1330         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1331             child.computed = parent.computed / 1.2;
1332         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1333             child.computed = parent.computed * 1.2;
1334         } else {
1335             /* Illegal value */
1336         }
1337     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1338         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1339          * rather than viewport. */
1340         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1341     }
1344 /**
1345  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1346  * calculating from) corresponding computed values of \a parent.
1347  *
1348  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1349  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1350  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1351  * about what the computed value is (not obvious for lengths).
1352  *
1353  * \pre \a parent's computed values are already up-to-date.
1354  */
1355 void
1356 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1358     g_return_if_fail(style != NULL);
1360     /** \todo
1361      * fixme: Check for existing callers that might pass null parent.
1362      * This should probably be g_return_if_fail, or else we should make a
1363      * best attempt to set computed values correctly without having a parent
1364      * (i.e., by assuming parent has initial values).
1365      */
1366     if (!parent)
1367         return;
1369     /* CSS2 */
1370     /* Font */
1371     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1373     /* 'font-style' */
1374     if (!style->font_style.set || style->font_style.inherit) {
1375         style->font_style.computed = parent->font_style.computed;
1376     }
1378     /* 'font-variant' */
1379     if (!style->font_variant.set || style->font_variant.inherit) {
1380         style->font_variant.computed = parent->font_variant.computed;
1381     }
1383     /* 'font-weight' */
1384     if (!style->font_weight.set || style->font_weight.inherit) {
1385         style->font_weight.computed = parent->font_weight.computed;
1386     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1387         /** \todo
1388          * fixme: This is unconditional, i.e., happens even if parent not
1389          * present.
1390          */
1391         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1392     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1393         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1394     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1395         unsigned const parent_val = parent->font_weight.computed;
1396         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1397         // strictly, 'bolder' and 'lighter' should go to the next weight
1398         // expressible in the current font family, but that's difficult to
1399         // find out, so jumping by 3 seems an appropriate approximation
1400         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1401                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1402                                        : parent_val - 3);
1403         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1404     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1405         unsigned const parent_val = parent->font_weight.computed;
1406         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1407         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1408                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1409                                        : parent_val + 3);
1410         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1411     }
1413     /* 'font-stretch' */
1414     if (!style->font_stretch.set || style->font_stretch.inherit) {
1415         style->font_stretch.computed = parent->font_stretch.computed;
1416     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1417         unsigned const parent_val = parent->font_stretch.computed;
1418         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1419                                         ? parent_val
1420                                         : parent_val - 1);
1421         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1422     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1423         unsigned const parent_val = parent->font_stretch.computed;
1424         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1425         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1426                                         ? parent_val
1427                                         : parent_val + 1);
1428         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1429     }
1431     /* text (css2) */
1432     if (!style->text_indent.set || style->text_indent.inherit) {
1433         style->text_indent.computed = parent->text_indent.computed;
1434     }
1436     if (!style->text_align.set || style->text_align.inherit) {
1437         style->text_align.computed = parent->text_align.computed;
1438     }
1440     if (!style->text_decoration.set || style->text_decoration.inherit) {
1441         style->text_decoration.underline = parent->text_decoration.underline;
1442         style->text_decoration.overline = parent->text_decoration.overline;
1443         style->text_decoration.line_through = parent->text_decoration.line_through;
1444         style->text_decoration.blink = parent->text_decoration.blink;
1445     }
1447     if (!style->line_height.set || style->line_height.inherit) {
1448         style->line_height.computed = parent->line_height.computed;
1449         style->line_height.normal = parent->line_height.normal;
1450     }
1452     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1453         style->letter_spacing.computed = parent->letter_spacing.computed;
1454         style->letter_spacing.normal = parent->letter_spacing.normal;
1455     }
1457     if (!style->word_spacing.set || style->word_spacing.inherit) {
1458         style->word_spacing.computed = parent->word_spacing.computed;
1459         style->word_spacing.normal = parent->word_spacing.normal;
1460     }
1462     if (!style->text_transform.set || style->text_transform.inherit) {
1463         style->text_transform.computed = parent->text_transform.computed;
1464     }
1466     if (!style->direction.set || style->direction.inherit) {
1467         style->direction.computed = parent->direction.computed;
1468     }
1470     if (!style->block_progression.set || style->block_progression.inherit) {
1471         style->block_progression.computed = parent->block_progression.computed;
1472     }
1474     if (!style->writing_mode.set || style->writing_mode.inherit) {
1475         style->writing_mode.computed = parent->writing_mode.computed;
1476     }
1478     if (!style->text_anchor.set || style->text_anchor.inherit) {
1479         style->text_anchor.computed = parent->text_anchor.computed;
1480     }
1482     if (style->opacity.inherit) {
1483         style->opacity.value = parent->opacity.value;
1484     }
1486     /* Color */
1487     if (!style->color.set || style->color.inherit) {
1488         sp_style_merge_ipaint(style, &style->color, &parent->color);
1489     }
1491     /* Fill */
1492     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1493         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1494     }
1496     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1497         style->fill_opacity.value = parent->fill_opacity.value;
1498     }
1500     if (!style->fill_rule.set || style->fill_rule.inherit) {
1501         style->fill_rule.computed = parent->fill_rule.computed;
1502     }
1504     /* Stroke */
1505     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1506         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1507     }
1509     if (!style->stroke_width.set || style->stroke_width.inherit) {
1510         style->stroke_width.computed = parent->stroke_width.computed;
1511     } else {
1512         /* Update computed value for any change in font inherited from parent. */
1513         double const em = style->font_size.computed;
1514         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1515             style->stroke_width.computed = style->stroke_width.value * em;
1516         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1517             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1518             style->stroke_width.computed = style->stroke_width.value * ex;
1519         }
1520     }
1522     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1523         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1524     }
1526     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1527         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1528     }
1530     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1531         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1532     }
1534     if (!style->stroke_dasharray_set || style->stroke_dasharray_inherit) {
1535         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1536         if (style->stroke_dash.n_dash > 0) {
1537             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1538             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1539         }
1540     }
1542     if (!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) {
1543         style->stroke_dash.offset = parent->stroke_dash.offset;
1544     }
1546     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1547         style->stroke_opacity.value = parent->stroke_opacity.value;
1548     }
1550     if (style->text && parent->text) {
1551         if (!style->text->font_family.set || style->text->font_family.inherit) {
1552             g_free(style->text->font_family.value);
1553             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1554         }
1555     }
1557     if (style->text && parent->text) {
1558         if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1559             g_free(style->text->font_specification.value);
1560             style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1561         }
1562     }
1564     /* Markers - Free the old value and make copy of the new */
1565     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1566         if (!style->marker[i].set || style->marker[i].inherit) {
1567             g_free(style->marker[i].value);
1568             style->marker[i].value = g_strdup(parent->marker[i].value);
1569         }
1570     }
1572     /* Filter effects */
1573     if (style->filter.inherit) {
1574         sp_style_merge_ifilter(style, &parent->filter);
1575     }
1577     if(style->enable_background.inherit) {
1578         style->enable_background.value = parent->enable_background.value;
1579     }
1582 template <typename T>
1583 static void
1584 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1586     if ( ( !(child.set) || child.inherit )
1587          && parent.set && !(parent.inherit) )
1588     {
1589         child = parent;
1590     }
1593 /**
1594  * Copy SPIString from parent to child.
1595  */
1596 static void
1597 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1599     if ( ( !(child.set) || child.inherit )
1600          && parent.set && !(parent.inherit) )
1601     {
1602         g_free(child.value);
1603         child.value = g_strdup(parent.value);
1604         child.set = parent.set;
1605         child.inherit = parent.inherit;
1606     }
1609 static void
1610 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1611                                             SPIPaint &child, SPIPaint const &parent)
1613     /** \todo
1614      * I haven't given this much attention.  See comments below about
1615      * currentColor, colorProfile, and relative URIs.
1616      */
1617     if (!child.set || child.inherit) {
1618         sp_style_merge_ipaint(style, &child, &parent);
1619         child.set = parent.set;
1620         child.inherit = parent.inherit;
1621     }
1624 static void
1625 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1626                                                unsigned const max_computed_val,
1627                                                unsigned const smaller_val)
1629     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1630        then zero or more other absolute values, then smaller_val then larger_val. */
1631     unsigned const min_computed_val = 0;
1632     unsigned const larger_val = smaller_val + 1;
1633     g_return_if_fail(min_computed_val < max_computed_val);
1634     g_return_if_fail(max_computed_val < smaller_val);
1636     if (parent.set && !parent.inherit) {
1637         if (!child.set || child.inherit) {
1638             child.value = parent.value;
1639             child.set = parent.set;  // i.e. true
1640             child.inherit = parent.inherit;  // i.e. false
1641         } else if (child.value < smaller_val) {
1642             /* Child has absolute value: leave as is. */
1643         } else if ( ( child.value == smaller_val
1644                       && parent.value == larger_val )
1645                     || ( parent.value == smaller_val
1646                          && child.value == larger_val ) )
1647         {
1648             child.set = false;
1649             /*
1650              * Note that this can result in a change in computed value in the
1651              * rare case that the parent's setting was a no-op (i.e. if the
1652              * parent's parent's computed value was already ultra-condensed or
1653              * ultra-expanded).  However, I'd guess that the change is for the
1654              * better: I'd guess that if the properties were specified
1655              * relatively, then the intent is to counteract parent's effect.
1656              * I.e. I believe this is the best code even in that case.
1657              */
1658         } else if (child.value == parent.value) {
1659             /* Leave as is. */
1660             /** \todo
1661              * It's unclear what to do if style and parent specify the same
1662              * relative directive (narrower or wider).  We can either convert
1663              * to absolute specification or coalesce to a single relative
1664              * request (of half the strength of the original pair).
1665              *
1666              * Converting to a single level of relative specification is a
1667              * better choice if the newly-unlinked clone is itself cloned to
1668              * other contexts (inheriting different font stretchiness): it
1669              * would preserve the effect that it will be narrower than
1670              * the inherited context in each case.  The disadvantage is that
1671              * it will ~certainly affect the computed value of the
1672              * newly-unlinked clone.
1673              */
1674         } else {
1675             unsigned const parent_val = parent.computed;
1676             child.value = ( child.value == smaller_val
1677                             ? ( parent_val == min_computed_val
1678                                 ? parent_val
1679                                 : parent_val - 1 )
1680                             : ( parent_val == max_computed_val
1681                                 ? parent_val
1682                                 : parent_val + 1 ) );
1683             g_assert(child.value <= max_computed_val);
1684             child.inherit = false;
1685             g_assert(child.set);
1686         }
1687     }
1690 template <typename LengthT>
1691 static void
1692 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1693                                              double const parent_child_em_ratio)
1695     if ( ( !(child.set) || child.inherit )
1696          && parent.set && !(parent.inherit) )
1697     {
1698         child = parent;
1699         switch (parent.unit) {
1700             case SP_CSS_UNIT_EM:
1701             case SP_CSS_UNIT_EX:
1702                 child.value *= parent_child_em_ratio;
1703                 /** \todo
1704                  * fixme: Have separate ex ratio parameter.
1705                  * Get x height from libnrtype or pango.
1706                  */
1707                 if (!IS_FINITE(child.value)) {
1708                     child.value = child.computed;
1709                     child.unit = SP_CSS_UNIT_NONE;
1710                 }
1711                 break;
1713             default:
1714                 break;
1715         }
1716     }
1719 static double
1720 get_relative_font_size_frac(SPIFontSize const &font_size)
1722     switch (font_size.type) {
1723         case SP_FONT_SIZE_LITERAL: {
1724             switch (font_size.value) {
1725                 case SP_CSS_FONT_SIZE_SMALLER:
1726                     return 5.0 / 6.0;
1728                 case SP_CSS_FONT_SIZE_LARGER:
1729                     return 6.0 / 5.0;
1731                 default:
1732                     g_assert_not_reached();
1733             }
1734         }
1736         case SP_FONT_SIZE_PERCENTAGE:
1737             return SP_F8_16_TO_FLOAT(font_size.value);
1739         case SP_FONT_SIZE_LENGTH:
1740             g_assert_not_reached();
1741     }
1742     g_assert_not_reached();
1745 /**
1746  * Combine \a style and \a parent style specifications into a single style specification that
1747  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1748  *
1749  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1750  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1751  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1752  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1753  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1754  *
1755  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1756  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1757  * style was previously up-to-date wrt \a parent.
1758  *
1759  * \pre \a parent's computed values are already up-to-date.
1760  *   (\a style's computed values needn't be up-to-date.)
1761  */
1762 void
1763 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1765     /** \note
1766      * The general rule for each property is as follows:
1767      *
1768      *   If style is set to an absolute value, then leave it as is.
1769      *
1770      *   Otherwise (i.e. if style has a relative value):
1771      *
1772      *     If parent is set to an absolute value, then set style to the computed value.
1773      *
1774      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1775      */
1777     /* We do font-size first, to ensure that em size is up-to-date. */
1778     /** \todo
1779      * fixme: We'll need to have more font-related things up the top once
1780      * we're getting x-height from pango or libnrtype.
1781      */
1783     /* Some things that allow relative specifications. */
1784     {
1785         /* font-size.  Note that we update the computed font-size of style,
1786            to assist in em calculations later in this function. */
1787         if (parent->font_size.set && !parent->font_size.inherit) {
1788             if (!style->font_size.set || style->font_size.inherit) {
1789                 /* font_size inherits the computed value, so we can use the parent value
1790                  * verbatim. */
1791                 style->font_size = parent->font_size;
1792             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1793                 /* Child already has absolute size (stored in computed value), so do nothing. */
1794             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1795                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1796                 /* Child already has absolute size, but we ensure that the computed value
1797                    is up-to-date. */
1798                 unsigned const ix = style->font_size.value;
1799                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1800                 style->font_size.computed = font_size_table[ix];
1801             } else {
1802                 /* Child has relative size. */
1803                 double const child_frac(get_relative_font_size_frac(style->font_size));
1804                 style->font_size.set = true;
1805                 style->font_size.inherit = false;
1806                 style->font_size.computed = parent->font_size.computed * child_frac;
1808                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1809                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1810                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1811                 {
1812                     /* Absolute value. */
1813                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1814                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1815                 } else {
1816                     /* Relative value. */
1817                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1818                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1819                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1820                 }
1821             }
1822         }
1824         /* 'font-stretch' */
1825         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1826                                                        parent->font_stretch,
1827                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1828                                                        SP_CSS_FONT_STRETCH_NARROWER);
1830         /* font-weight */
1831         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1832                                                        parent->font_weight,
1833                                                        SP_CSS_FONT_WEIGHT_900,
1834                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1835     }
1838     /* Enum values that don't have any relative settings (other than `inherit'). */
1839     {
1840         SPIEnum SPStyle::*const fields[] = {
1841             //nyi: SPStyle::clip_rule,
1842             //nyi: SPStyle::color_interpolation,
1843             //nyi: SPStyle::color_interpolation_filters,
1844             //nyi: SPStyle::color_rendering,
1845             &SPStyle::direction,
1846             &SPStyle::fill_rule,
1847             &SPStyle::font_style,
1848             &SPStyle::font_variant,
1849             //nyi: SPStyle::image_rendering,
1850             //nyi: SPStyle::pointer_events,
1851             //nyi: SPStyle::shape_rendering,
1852             &SPStyle::stroke_linecap,
1853             &SPStyle::stroke_linejoin,
1854             &SPStyle::text_anchor,
1855             //nyi: &SPStyle::text_rendering,
1856             &SPStyle::visibility,
1857             &SPStyle::writing_mode
1858         };
1860         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1861             SPIEnum SPStyle::*const fld = fields[i];
1862             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1863         }
1864     }
1866     /* A few other simple inheritance properties. */
1867     {
1868         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1869         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1870         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1872         /** \todo
1873          * We currently treat text-decoration as if it were a simple inherited
1874          * property (fixme). This code may need changing once we do the
1875          * special fill/stroke inheritance mentioned by the spec.
1876          */
1877         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1878                                                                  parent->text_decoration);
1880         //nyi: font-size-adjust,  // <number> | none | inherit
1881         //nyi: glyph-orientation-horizontal,
1882         //nyi: glyph-orientation-vertical,
1883     }
1885     /* Properties that involve length but are easy in other respects. */
1886     {
1887         /* The difficulty with lengths is that font-relative units need adjusting if the font
1888          * varies between parent & child.
1889          *
1890          * Lengths specified in the existing child can stay as they are: its computed font
1891          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1892          * size.
1893          *
1894          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1895          * ratio of em or ex size between parent & child.
1896          */
1897         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1899         SPILength SPStyle::*const lfields[] = {
1900             &SPStyle::stroke_width,
1901             &SPStyle::text_indent
1902         };
1903         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1904             SPILength SPStyle::*fld = lfields[i];
1905             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1906                                                                     parent->*fld,
1907                                                                     parent_child_em_ratio);
1908         }
1910         SPILengthOrNormal SPStyle::*const nfields[] = {
1911             &SPStyle::letter_spacing,
1912             &SPStyle::line_height,
1913             &SPStyle::word_spacing
1914         };
1915         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1916             SPILengthOrNormal SPStyle::*fld = nfields[i];
1917             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1918                                                                             parent->*fld,
1919                                                                             parent_child_em_ratio);
1920         }
1922         //nyi: &SPStyle::kerning: length or `auto'
1924         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1925            can accept units. */
1926     }
1928     /* Properties that involve a URI but are easy in other respects. */
1929     {
1930         /** \todo
1931          * Could cause problems if original object was in another document
1932          * and it used a relative URL.  (At the time of writing, we don't
1933          * allow hrefs to other documents, so this isn't a problem yet.)
1934          * Paint properties also allow URIs.
1935          */
1936         //nyi: cursor,   // may involve change in effect, but we can't do much better
1937         //nyi: color-profile,
1939         // Markers (marker-start, marker-mid, marker-end).
1940         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1941             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1942         }
1943     }
1945     /* CSS2 */
1946     /* Font */
1948     if (style->text && parent->text) {
1949         sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
1950                                                      parent->text->font_specification);
1951         
1952         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1953                                                      parent->text->font_family);
1954     }
1957     /* Properties that don't inherit by default.  Most of these need special handling. */
1958     {
1959         /*
1960          * opacity's effect is cumulative; we set the new value to the combined effect.  The
1961          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
1962          * fill-opacity are quite different from opacity, and don't need any special handling.)
1963          *
1964          * Cases:
1965          * - parent & child were each previously unset, in which case the effective
1966          *   opacity value is 1.0, and style should remain unset.
1967          * - parent was previously unset (so computed opacity value of 1.0)
1968          *   and child was set to inherit.  The merged child should
1969          *   get a value of 1.0, and shouldn't inherit (lest the new parent
1970          *   has a different opacity value).  Given that opacity's default
1971          *   value is 1.0 (rather than inherit), we might as well have the
1972          *   merged child's opacity be unset.
1973          * - parent was previously unset (so opacity 1.0), and child was set to a number.
1974          *   The merged child should retain its existing settings (though it doesn't matter
1975          *   if we make it unset if that number was 1.0).
1976          * - parent was inherit and child was unset.  Merged child should be set to inherit.
1977          * - parent was inherit and child was inherit.  (We can't in general reproduce this
1978          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
1979          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1980          *   child's value should be set to the product of the two, i.e. the square of the
1981          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
1982          *   is more important to retain the effective opacity than to retain the inheriting
1983          *   effect, and assumes that the inheriting effect either isn't important enough to create
1984          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
1985          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1986          *   closer to maintaining the effect.
1987          * - parent was inherit and child was set to a numerical value.  If the child's value
1988          *   was 1.0, then the merged child should have the same settings as the parent.
1989          *   If the child's value was 0, then the merged child should also be set to 0.
1990          *   If the child's value was anything else, then we do the same as for the inherit/inherit
1991          *   case above: have the merged child set to the product of the two opacities and not
1992          *   marked as inherit, for the same reasons as for that case.
1993          * - parent was set to a value, and child was unset.  The merged child should have
1994          *   parent's settings.
1995          * - parent was set to a value, and child was inherit.  The merged child should
1996          *   be set to the product, i.e. the square of the parent's value.
1997          * - parent & child are each set to a value.  The merged child should be set to the
1998          *   product.
1999          */
2000         if ( !style->opacity.set
2001              || ( !style->opacity.inherit
2002                   && style->opacity.value == SP_SCALE24_MAX ) )
2003         {
2004             style->opacity = parent->opacity;
2005         } else {
2006             /* Ensure that style's computed value is up-to-date. */
2007             if (style->opacity.inherit) {
2008                 style->opacity.value = parent->opacity.value;
2009             }
2011             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
2012             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
2013                                                   parent->opacity.value);
2015             style->opacity.inherit = (parent->opacity.inherit
2016                                       && style->opacity.inherit
2017                                       && (parent->opacity.value == 0 ||
2018                                           parent->opacity.value == SP_SCALE24_MAX));
2019             style->opacity.set = ( style->opacity.inherit
2020                                    || style->opacity.value < SP_SCALE24_MAX );
2021         }
2023         /* display is in principle similar to opacity, but implementation is easier. */
2024         if ( parent->display.set && !parent->display.inherit
2025              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
2026             style->display.value = SP_CSS_DISPLAY_NONE;
2027             style->display.set = true;
2028             style->display.inherit = false;
2029         } else if (style->display.inherit) {
2030             style->display.value = parent->display.value;
2031             style->display.set = parent->display.set;
2032             style->display.inherit = parent->display.inherit;
2033         } else {
2034             /* Leave as is.  (display doesn't inherit by default.) */
2035         }
2037         /* enable-background - this is rather complicated, because
2038          * it is valid only when applied to container elements.
2039          * Let's check a simple case anyhow. */
2040         if (parent->enable_background.set
2041             && !parent->enable_background.inherit
2042             && style->enable_background.inherit)
2043         {
2044             style->enable_background.set = true;
2045             style->enable_background.inherit = false;
2046             style->enable_background.value = parent->enable_background.value;
2047         }
2049         if (!style->filter.set || style->filter.inherit)
2050         {
2051             sp_style_merge_ifilter(style, &parent->filter);
2052         }
2054         /** \todo
2055          * fixme: Check that we correctly handle all properties that don't
2056          * inherit by default (as shown in
2057          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2058          */
2059     }
2061     /* SPIPaint properties (including color). */
2062     {
2063         /** \todo
2064          * Think about the issues involved if specified as currentColor or
2065          * if specified relative to colorProfile, and if the currentColor or
2066          * colorProfile differs between parent \& child.  See also comments
2067          * elsewhere in this function about URIs.
2068          */
2069         SPIPaint SPStyle::*const fields[] = {
2070             &SPStyle::color,
2071             &SPStyle::fill,
2072             &SPStyle::stroke
2073         };
2074         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2075             SPIPaint SPStyle::*const fld = fields[i];
2076             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2077         }
2078     }
2080     /* Things from SVG 1.2 or CSS3. */
2081     {
2082         /* Note: If we ever support setting string values for text-align then we'd need strdup
2083          * handling here. */
2084         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2086         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2087         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2088     }
2090     /* Note: this will need length handling once dasharray supports units. */
2091     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2092          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2093     {
2094         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2095         if (style->stroke_dash.n_dash > 0) {
2096             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2097             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2098         }
2099         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2100         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2101     }
2103     /* Note: this will need length handling once dasharray_offset supports units. */
2104     if ((!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) && parent->stroke_dashoffset_set && !parent->stroke_dashoffset_inherit) {
2105         style->stroke_dash.offset = parent->stroke_dash.offset;
2106         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2107         style->stroke_dashoffset_inherit = parent->stroke_dashoffset_inherit;
2108         /* TODO: Try to
2109          * represent it as a normal SPILength; though will need to do something about existing
2110          * users of stroke_dash.offset and stroke_dashoffset_set. */
2111     }
2115 static void
2116 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2118     // it may be that this style's SPIPaint has not yet created its URIReference;
2119     // now that we have a document, we can create it here
2120     if (!paint->value.href && document) {
2121         paint->value.href = new SPPaintServerReference(document);
2122         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));
2123     }
2125     if (paint->value.href && paint->value.href->getObject())
2126         paint->value.href->detach();
2128     if (paint->value.href) {
2129         try {
2130             paint->value.href->attach(*uri);
2131         } catch (Inkscape::BadURIException &e) {
2132             g_warning("%s", e.what());
2133             paint->value.href->detach();
2134         }
2135     }
2138 static void
2139 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2141     try {
2142         const Inkscape::URI IURI(uri);
2143         sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2144     } catch (...) {
2145         g_warning("URI failed to parse: %s", uri);
2146     }
2149 void
2150 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2152     sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2155 /**
2156  *
2157  */
2158 static void
2159 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2161     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2162         bool isset = paint->set;
2163         paint->clear();
2164         paint->set = isset;
2165         paint->currentcolor = TRUE;
2166         paint->setColor(style->color.value.color);
2167         return;
2168     }
2170     paint->clear();
2171     if ( parent->isPaintserver() ) {
2172         if (parent->value.href) {
2173             sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2174         } else {
2175             g_warning("Expected paint server not found.");
2176         }
2177     } else if ( parent->isColor() ) {
2178         paint->setColor( parent->value.color );
2179     } else if ( parent->isNoneSet() ) {
2180         paint->noneSet = TRUE;
2181     } else if ( parent->isNone() ) {
2182         //
2183     } else {
2184         g_assert_not_reached();
2185     }
2189 /**
2190  * Merge filter style from parent.
2191  * Filter effects do not inherit by default
2192  */
2193 static void
2194 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2196     // FIXME:
2197     // instead of just copying over, we need to _really merge_ the two filters by combining their
2198     // filter primitives
2200     sp_style_filter_clear(style);
2201     style->filter.set = parent->set;
2202     style->filter.inherit = parent->inherit;
2204     if (style->filter.href && style->filter.href->getObject())
2205        style->filter.href->detach();
2207     // it may be that this style has not yet created its SPFilterReference
2208     if (!style->filter.href && style->object && SP_OBJECT_DOCUMENT(style->object)) {
2209             style->filter.href = new SPFilterReference(SP_OBJECT_DOCUMENT(style->object));
2210             style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2211     }
2213     if (style->filter.href && parent->href && parent->href->getObject()) {
2214         try {
2215             style->filter.href->attach(*parent->href->getURI());
2216         } catch (Inkscape::BadURIException &e) {
2217             g_warning("%s", e.what());
2218             style->filter.href->detach();
2219         }
2220     }
2223 /**
2224  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2225  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2226  * complete cascaded style to style_clipboard. When you need a CSS string
2227  * for an object in the document tree, you normally call
2228  * sp_style_write_difference instead to take into account the object's parent.
2229  *
2230  * \pre style != NULL.
2231  * \pre flags in {IFSET, ALWAYS}.
2232  * \post ret != NULL.
2233  */
2234 gchar *
2235 sp_style_write_string(SPStyle const *const style, guint const flags)
2237     /** \todo
2238      * Merge with write_difference, much duplicate code!
2239      */
2240     g_return_val_if_fail(style != NULL, NULL);
2241     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2242                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2243                          NULL);
2245     gchar c[BMAX];
2246     gchar *p = c;
2247     *p = '\0';
2249     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2250     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2251     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2252     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2253     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2255     /* Text */
2256     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2257     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2258     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2259     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2260     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2261     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2262     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2263     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2264     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2265     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2267     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2269     /// \todo fixme: Per type methods need default flag too (lauris)
2271     if (style->opacity.value != SP_SCALE24_MAX) {
2272         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2273     }
2275     if (!style->color.noneSet) { // CSS does not permit "none" for color
2276         p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2277     }
2279     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2280     // if fill:none, skip writing fill properties
2281     if (!style->fill.noneSet) {
2282         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2283         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2284     }
2286     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2288     // stroke width affects markers, so write it if there's stroke OR any markers
2289     if (!style->stroke.noneSet || 
2290         style->marker[SP_MARKER_LOC].set ||
2291         style->marker[SP_MARKER_LOC_START].set ||
2292         style->marker[SP_MARKER_LOC_MID].set || 
2293         style->marker[SP_MARKER_LOC_END].set) {
2294         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2295     }
2297     // if stroke:none, skip writing stroke properties
2298     if (!style->stroke.noneSet) {
2299         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2300         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2301         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2302         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2304         /** \todo fixme: */
2305         if ((flags == SP_STYLE_FLAG_ALWAYS)
2306             || style->stroke_dasharray_set)
2307         {
2308             if (style->stroke_dasharray_inherit) {
2309                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2310             } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2311                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2312                 gint i;
2313                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2314                     Inkscape::CSSOStringStream os;
2315                     if (i) {
2316                         os << ", ";
2317                     }
2318                     os << style->stroke_dash.dash[i];
2319                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2320                 }
2321                 if (p < c + BMAX) {
2322                     *p++ = ';';
2323                 }
2324             } else {
2325                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2326             }
2327         }
2329         /** \todo fixme: */
2330         if (style->stroke_dashoffset_set) {
2331             if (style->stroke_dashoffset_inherit) {
2332                 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2333             } else {
2334                 Inkscape::CSSOStringStream os;
2335                 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2336                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2337             }
2338         } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2339             p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2340         }
2341     }
2343     bool marker_none = false;
2344     gchar *master = style->marker[SP_MARKER_LOC].value;
2345     if (style->marker[SP_MARKER_LOC].set) {
2346         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2347     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2348         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2349         marker_none = true;
2350     }
2351     if (style->marker[SP_MARKER_LOC_START].set 
2352        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_START].value))) {
2353         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2354     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2355         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2356     }
2357     if (style->marker[SP_MARKER_LOC_MID].set
2358        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_MID].value))) {
2359         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2360     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2361         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2362     }
2363     if (style->marker[SP_MARKER_LOC_END].set
2364        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_END].value))) {
2365         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2366     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2367         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2368     }
2370     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2371     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2372     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2374     /* filter: */
2375     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2377     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2379     /* fixme: */
2380     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2382     /* Get rid of trailing `;'. */
2383     if (p != c) {
2384         --p;
2385         if (*p == ';') {
2386             *p = '\0';
2387         }
2388     }
2390     return g_strdup(c);
2394 #define STYLE_BUF_MAX
2397 /**
2398  * Dumps style to CSS string, see sp_style_write_string()
2399  *
2400  * \pre from != NULL.
2401  * \pre to != NULL.
2402  * \post ret != NULL.
2403  */
2404 gchar *
2405 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2407     g_return_val_if_fail(from != NULL, NULL);
2408     g_return_val_if_fail(to != NULL, NULL);
2410     gchar c[BMAX], *p = c;
2411     *p = '\0';
2413     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2414     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2415     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2416     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2417     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2419     /* Text */
2420     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2421     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2422     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2423     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2424     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2425     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2426     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2427     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2428     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2429     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2431     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2433     /// \todo fixme: Per type methods need default flag too
2434     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2435         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2436     }
2438     if (!from->color.noneSet) { // CSS does not permit "none" for color
2439         p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2440     }
2442     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2443     // if fill:none, skip writing fill properties
2444     if (!from->fill.noneSet) {
2445         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2446         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2447     }
2449     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2451     // stroke width affects markers, so write it if there's stroke OR any markers
2452     if (!from->stroke.noneSet || 
2453         from->marker[SP_MARKER_LOC].set ||
2454         from->marker[SP_MARKER_LOC_START].set ||
2455         from->marker[SP_MARKER_LOC_MID].set || 
2456         from->marker[SP_MARKER_LOC_END].set) {
2457         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2458     }
2460     // if stroke:none, skip writing stroke properties
2461     if (!from->stroke.noneSet) {
2462         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2463                                   &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2464         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2465                                   &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2466         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2467                                    &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2468         /** \todo fixme: */
2469         if (from->stroke_dasharray_set) {
2470             if (from->stroke_dasharray_inherit) {
2471                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2472             } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2473                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2474                 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2475                     Inkscape::CSSOStringStream os;
2476                     if (i) {
2477                         os << ", ";
2478                     }
2479                     os << from->stroke_dash.dash[i];
2480                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2481                 }
2482                 p += g_snprintf(p, c + BMAX - p, ";");
2483             }
2484         }
2485         /* fixme: */
2486         if (from->stroke_dashoffset_set) {
2487             if (from->stroke_dashoffset_inherit) {
2488                 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2489             } else {
2490                 Inkscape::CSSOStringStream os;
2491                 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2492                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2493             }
2494         }
2495         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2496     }
2498     /* markers */
2499     gchar *master = from->marker[SP_MARKER_LOC].value;
2500     if (master != NULL) {
2501         p += g_snprintf(p, c + BMAX - p, "marker:%s;", master);
2502     }
2503     if (from->marker[SP_MARKER_LOC_START].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_START].value))) {
2504         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2505     }
2506     if (from->marker[SP_MARKER_LOC_MID].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_MID].value))) {
2507         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2508     }
2509     if (from->marker[SP_MARKER_LOC_END].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_END].value))) {
2510         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2511     }
2513     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2514     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2515     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2517     /* filter: */
2518     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2520     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2522     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2524     /** \todo
2525      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2526      * flag is mainly only for attributes that don't handle explicit unset well.
2527      * We may need to revisit the behaviour of this routine.
2528      */
2530     /* Get rid of trailing `;'. */
2531     if (p != c) {
2532         --p;
2533         if (*p == ';') {
2534             *p = '\0';
2535         }
2536     }
2538     return g_strdup(c);
2543 /**
2544  * Reset all style properties.
2545  */
2546 static void
2547 sp_style_clear(SPStyle *style)
2549     g_return_if_fail(style != NULL);
2551     style->fill.clear();
2552     style->stroke.clear();
2553     sp_style_filter_clear(style);
2555     if (style->fill.value.href) {
2556         delete style->fill.value.href;
2557         style->fill.value.href = NULL;
2558     }
2559     if (style->stroke.value.href) {
2560         delete style->stroke.value.href;
2561         style->stroke.value.href = NULL;
2562     }
2563     if (style->filter.href) {
2564         delete style->filter.href;
2565         style->filter.href = NULL;
2566     }
2568     if (style->stroke_dash.dash) {
2569         g_free(style->stroke_dash.dash);
2570     }
2572     style->stroke_dasharray_inherit = FALSE;
2573     style->stroke_dashoffset_inherit = FALSE;
2575     /** \todo fixme: Do that text manipulation via parents */
2576     SPObject *object = style->object;
2577     SPDocument *document = style->document;
2578     gint const refcount = style->refcount;
2579     SPTextStyle *text = style->text;
2580     unsigned const text_private = style->text_private;
2582     memset(style, 0, sizeof(SPStyle));
2584     style->refcount = refcount;
2585     style->object = object;
2586     style->document = document;
2588     if (document) {
2589         style->filter.href = new SPFilterReference(document);
2590         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2592         style->fill.value.href = new SPPaintServerReference(document);
2593         style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2595         style->stroke.value.href = new SPPaintServerReference(document);
2596         style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2597     }
2599     style->text = text;
2600     style->text_private = text_private;
2602     style->text->font_specification.set = FALSE;
2603     style->text->font.set = FALSE;
2604     style->text->font_family.set = FALSE;
2606     style->font_size.set = FALSE;
2607     style->font_size.type = SP_FONT_SIZE_LITERAL;
2608     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2609     style->font_size.computed = 12.0;
2610     style->font_style.set = FALSE;
2611     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2612     style->font_variant.set = FALSE;
2613     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2614     style->font_weight.set = FALSE;
2615     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2616     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2617     style->font_stretch.set = FALSE;
2618     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2620     /* text */
2621     style->text_indent.set = FALSE;
2622     style->text_indent.unit = SP_CSS_UNIT_NONE;
2623     style->text_indent.computed = 0.0;
2625     style->text_align.set = FALSE;
2626     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2628     style->text_decoration.set = FALSE;
2629     style->text_decoration.underline = FALSE;
2630     style->text_decoration.overline = FALSE;
2631     style->text_decoration.line_through = FALSE;
2632     style->text_decoration.blink = FALSE;
2634     style->line_height.set = FALSE;
2635     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2636     style->line_height.normal = TRUE;
2637     style->line_height.value = style->line_height.computed = 1.0;
2639     style->letter_spacing.set = FALSE;
2640     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2641     style->letter_spacing.normal = TRUE;
2642     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2644     style->word_spacing.set = FALSE;
2645     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2646     style->word_spacing.normal = TRUE;
2647     style->word_spacing.value = style->word_spacing.computed = 0.0;
2649     style->text_transform.set = FALSE;
2650     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2652     style->direction.set = FALSE;
2653     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2655     style->block_progression.set = FALSE;
2656     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2658     style->writing_mode.set = FALSE;
2659     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2662     style->text_anchor.set = FALSE;
2663     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2666     style->opacity.value = SP_SCALE24_MAX;
2667     style->visibility.set = FALSE;
2668     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2669     style->display.set = FALSE;
2670     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2671     style->overflow.set = FALSE;
2672     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2674     style->color.clear();
2675     style->color.setColor(0.0, 0.0, 0.0);
2677     style->fill.clear();
2678     style->fill.setColor(0.0, 0.0, 0.0);
2679     style->fill_opacity.value = SP_SCALE24_MAX;
2680     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2682     style->stroke.clear();
2683     style->stroke_opacity.value = SP_SCALE24_MAX;
2685     style->stroke_width.set = FALSE;
2686     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2687     style->stroke_width.computed = 1.0;
2689     style->stroke_linecap.set = FALSE;
2690     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2691     style->stroke_linejoin.set = FALSE;
2692     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2694     style->stroke_miterlimit.set = FALSE;
2695     style->stroke_miterlimit.value = 4.0;
2697     style->stroke_dash.n_dash = 0;
2698     style->stroke_dash.dash = NULL;
2699     style->stroke_dash.offset = 0.0;
2701     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2702         g_free(style->marker[i].value);
2703         style->marker[i].set = FALSE;
2704     }
2706     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2707     style->enable_background.set = false;
2708     style->enable_background.inherit = false;
2713 /**
2714  *
2715  */
2716 static void
2717 sp_style_read_dash(SPStyle *style, gchar const *str)
2719     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2720     style->stroke_dasharray_set = TRUE;
2722     if (strcmp(str, "inherit") == 0) {
2723         style->stroke_dasharray_inherit = true;
2724         return;
2725     }
2726     style->stroke_dasharray_inherit = false;
2728     NRVpathDash &dash = style->stroke_dash;
2729     g_free(dash.dash);
2730     dash.dash = NULL;
2732     if (strcmp(str, "none") == 0) {
2733         dash.n_dash = 0;
2734         return;
2735     }
2737     gint n_dash = 0;
2738     gdouble d[64];
2739     gchar *e = NULL;
2741     bool LineSolid=true;
2742     while (e != str && n_dash < 64) {
2743         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2744         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2745         if (d[n_dash] > 0.00000001)
2746             LineSolid = false;
2747         if (e != str) {
2748             n_dash += 1;
2749             str = e;
2750         }
2751         while (str && *str && !isalnum(*str)) str += 1;
2752     }
2754     if (LineSolid) {
2755         dash.n_dash = 0;
2756         return;
2757     }
2759     if (n_dash > 0) {
2760         dash.dash = g_new(gdouble, n_dash);
2761         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2762         dash.n_dash = n_dash;
2763     }
2767 /*#########################
2768 ## SPTextStyle operations
2769 #########################*/
2772 /**
2773  * Return new SPTextStyle object with default settings.
2774  */
2775 static SPTextStyle *
2776 sp_text_style_new()
2778     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2779     ts->refcount = 1;
2780     sp_text_style_clear(ts);
2782     ts->font_specification.value = g_strdup("Bitstream Vera Sans");
2783     ts->font.value = g_strdup("Bitstream Vera Sans");
2784     ts->font_family.value = g_strdup("Bitstream Vera Sans");
2786     return ts;
2790 /**
2791  * Clear text style settings.
2792  */
2793 static void
2794 sp_text_style_clear(SPTextStyle *ts)
2796     ts->font_specification.set = FALSE;
2797     ts->font.set = FALSE;
2798     ts->font_family.set = FALSE;
2803 /**
2804  * Reduce refcount of text style and possibly free it.
2805  */
2806 static SPTextStyle *
2807 sp_text_style_unref(SPTextStyle *st)
2809     st->refcount -= 1;
2811     if (st->refcount < 1) {
2812         g_free(st->font_specification.value);
2813         g_free(st->font.value);
2814         g_free(st->font_family.value);
2815         g_free(st);
2816     }
2818     return NULL;
2823 /**
2824  * Return duplicate of text style.
2825  */
2826 static SPTextStyle *
2827 sp_text_style_duplicate_unset(SPTextStyle *st)
2829     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2830     nt->refcount = 1;
2832     nt->font_specification.value = g_strdup(st->font_specification.value);
2833     nt->font.value = g_strdup(st->font.value);
2834     nt->font_family.value = g_strdup(st->font_family.value);
2836     return nt;
2841 /**
2842  * Write SPTextStyle object into string.
2843  */
2844 static guint
2845 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2847     gint d = 0;
2849     // We do not do diffing for text style
2850     if (flags == SP_STYLE_FLAG_IFDIFF)
2851         flags = SP_STYLE_FLAG_IFSET;
2853     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2854     d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2855     return d;
2860 /* The following sp_tyle_read_* functions ignore invalid values, as per
2861  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2862  *
2863  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2864  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2865  * pairs, in which case SVG's error-handling rules
2866  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2867  */
2870 /**
2871  * Set SPIFloat object from string.
2872  */
2873 static void
2874 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2876     if (!strcmp(str, "inherit")) {
2877         val->set = TRUE;
2878         val->inherit = TRUE;
2879     } else {
2880         gfloat value;
2881         if (sp_svg_number_read_f(str, &value)) {
2882             val->set = TRUE;
2883             val->inherit = FALSE;
2884             val->value = value;
2885         }
2886     }
2891 /**
2892  * Set SPIScale24 object from string.
2893  */
2894 static void
2895 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2897     if (!strcmp(str, "inherit")) {
2898         val->set = TRUE;
2899         val->inherit = TRUE;
2900     } else {
2901         gfloat value;
2902         if (sp_svg_number_read_f(str, &value)) {
2903             val->set = TRUE;
2904             val->inherit = FALSE;
2905             value = CLAMP(value, 0.0, 1.0);
2906             val->value = SP_SCALE24_FROM_FLOAT(value);
2907         }
2908     }
2911 /**
2912  * Reads a style value and performs lookup based on the given style value enumerations.
2913  */
2914 static void
2915 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2916                     bool const can_explicitly_inherit)
2918     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2919         val->set = TRUE;
2920         val->inherit = TRUE;
2921     } else {
2922         for (unsigned i = 0; dict[i].key; i++) {
2923             if (!strcmp(str, dict[i].key)) {
2924                 val->set = TRUE;
2925                 val->inherit = FALSE;
2926                 val->value = dict[i].value;
2927                 /* Save copying for values not needing it */
2928                 val->computed = val->value;
2929                 break;
2930             }
2931         }
2932     }
2937 /**
2938  * Set SPIString object from string.
2939  */
2940 static void
2941 sp_style_read_istring(SPIString *val, gchar const *str)
2943     g_free(val->value);
2945     if (!strcmp(str, "inherit")) {
2946         val->set = TRUE;
2947         val->inherit = TRUE;
2948         val->value = NULL;
2949     } else {
2950         val->set = TRUE;
2951         val->inherit = FALSE;
2952         val->value = g_strdup(str);
2953     }
2958 /**
2959  * Set SPILength object from string.
2960  */
2961 static void
2962 sp_style_read_ilength(SPILength *val, gchar const *str)
2964     if (!strcmp(str, "inherit")) {
2965         val->set = TRUE;
2966         val->inherit = TRUE;
2967     } else {
2968         gdouble value;
2969         gchar *e;
2970         /** \todo fixme: Move this to standard place (Lauris) */
2971         value = g_ascii_strtod(str, &e);
2972         if ((gchar const *) e != str) {
2973             /** \todo
2974              * Allow the number of px per inch to vary (document preferences,
2975              * X server or whatever).  E.g. don't fill in computed here, do
2976              * it at the same time as percentage units are done.
2977              */
2978             if (!*e) {
2979                 /* Userspace */
2980                 val->unit = SP_CSS_UNIT_NONE;
2981                 val->computed = value;
2982             } else if (!strcmp(e, "px")) {
2983                 /* Userspace */
2984                 val->unit = SP_CSS_UNIT_PX;
2985                 val->computed = value;
2986             } else if (!strcmp(e, "pt")) {
2987                 /* Userspace / DEVICESCALE */
2988                 val->unit = SP_CSS_UNIT_PT;
2989                 val->computed = value * PX_PER_PT;
2990             } else if (!strcmp(e, "pc")) {
2991                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2992                 val->unit = SP_CSS_UNIT_PC;
2993                 val->computed = value * PX_PER_PT * 12;
2994             } else if (!strcmp(e, "mm")) {
2995                 val->unit = SP_CSS_UNIT_MM;
2996                 val->computed = value * PX_PER_MM;
2997             } else if (!strcmp(e, "cm")) {
2998                 val->unit = SP_CSS_UNIT_CM;
2999                 val->computed = value * PX_PER_CM;
3000             } else if (!strcmp(e, "in")) {
3001                 val->unit = SP_CSS_UNIT_IN;
3002                 val->computed = value * PX_PER_IN;
3003             } else if (!strcmp(e, "em")) {
3004                 /* EM square */
3005                 val->unit = SP_CSS_UNIT_EM;
3006                 val->value = value;
3007             } else if (!strcmp(e, "ex")) {
3008                 /* ex square */
3009                 val->unit = SP_CSS_UNIT_EX;
3010                 val->value = value;
3011             } else if (!strcmp(e, "%")) {
3012                 /* Percentage */
3013                 val->unit = SP_CSS_UNIT_PERCENT;
3014                 val->value = value * 0.01;
3015             } else {
3016                 /* Invalid */
3017                 return;
3018             }
3019             val->set = TRUE;
3020             val->inherit = FALSE;
3021         }
3022     }
3025 /**
3026  * Set SPILengthOrNormal object from string.
3027  */
3028 static void
3029 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
3031     if (!strcmp(str, "normal")) {
3032         val->set = TRUE;
3033         val->inherit = FALSE;
3034         val->normal = TRUE;
3035         val->unit = SP_CSS_UNIT_NONE;
3036         val->value = val->computed = 0.0;
3037     } else {
3038         SPILength length;
3039         sp_style_read_ilength(&length, str);
3040         val->set = length.set;
3041         val->inherit = length.inherit;
3042         val->normal = FALSE;
3043         val->unit = length.unit;
3044         val->value = length.value;
3045         val->computed = length.computed;
3046     }
3049 /**
3050  * Set SPITextDecoration object from string.
3051  */
3052 static void
3053 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
3055     if (!strcmp(str, "inherit")) {
3056         val->set = TRUE;
3057         val->inherit = TRUE;
3058     } else if (!strcmp(str, "none")) {
3059         val->set = TRUE;
3060         val->inherit = FALSE;
3061         val->underline = FALSE;
3062         val->overline = FALSE;
3063         val->line_through = FALSE;
3064         val->blink = FALSE;
3065     } else {
3066         bool found_underline = false;
3067         bool found_overline = false;
3068         bool found_line_through = false;
3069         bool found_blink = false;
3070         for ( ; *str ; str++ ) {
3071             if (*str == ' ') continue;
3072             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
3073                 found_underline = true;
3074                 str += 9;
3075             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
3076                 found_overline = true;
3077                 str += 8;
3078             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
3079                 found_line_through = true;
3080                 str += 12;
3081             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
3082                 found_blink = true;
3083                 str += 5;
3084             } else {
3085                 return;  // invalid value
3086             }
3087         }
3088         if (!(found_underline || found_overline || found_line_through || found_blink)) {
3089             return;  // invalid value: empty
3090         }
3091         val->set = TRUE;
3092         val->inherit = FALSE;
3093         val->underline = found_underline;
3094         val->overline = found_overline;
3095         val->line_through = found_line_through;
3096         val->blink = found_blink;
3097     }
3100 /**
3101  * Set SPIPaint object from string containing an integer value.
3102  * \param document Ignored
3103  */
3104 static void
3105 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3107     (void)style; // TODO
3108     (void)document; // TODO
3109     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
3110     if (!strcmp(str, "inherit")) {
3111         paint->set = TRUE;
3112         paint->inherit = TRUE;
3113     } else {
3114         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
3115         if (rgb0 != 0xff) {
3116             paint->setColor(rgb0);
3117             paint->set = TRUE;
3118             paint->inherit = FALSE;
3119         }
3120     }
3124 /**
3125  * Set SPIPaint object from string.
3126  *
3127  * \pre paint == \&style.fill || paint == \&style.stroke.
3128  */
3129 static void
3130 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3132     while (g_ascii_isspace(*str)) {
3133         ++str;
3134     }
3136     paint->clear();
3138     if (streq(str, "inherit")) {
3139         paint->set = TRUE;
3140         paint->inherit = TRUE;
3141     } else {
3142         if ( strneq(str, "url", 3) ) {
3143             gchar *uri = extract_uri( str, &str );
3144             while ( g_ascii_isspace(*str) ) {
3145                 ++str;
3146             }
3147             // TODO check on and comment the comparrison "paint != &style->color".
3148             if ( uri && *uri && (paint != &style->color) ) {
3149                 paint->set = TRUE;
3151                 // it may be that this style's SPIPaint has not yet created its URIReference;
3152                 // now that we have a document, we can create it here
3153                 if (!paint->value.href && document) {
3154                     paint->value.href = new SPPaintServerReference(document);
3155                     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));
3156                 }
3158                 // TODO check what this does in light of move away from union
3159                 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3160             }
3161             g_free( uri );
3162         }
3164         if (streq(str, "currentColor") && paint != &style->color) {
3165             paint->set = TRUE;
3166             paint->currentcolor = TRUE;
3167         } else if (streq(str, "none") && paint != &style->color) {
3168             paint->set = TRUE;
3169             paint->noneSet = TRUE;
3170         } else {
3171             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3172             if (rgb0 != 0xff) {
3173                 paint->setColor( rgb0 );
3174                 paint->set = TRUE;
3176                 while (g_ascii_isspace(*str)) {
3177                     ++str;
3178                 }
3179                 if (strneq(str, "icc-color(", 10)) {
3180                     SVGICCColor* tmp = new SVGICCColor();
3181                     if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3182                         delete tmp;
3183                         tmp = 0;
3184                     }
3185                     paint->value.color.icc = tmp;
3186                 }
3187             }
3188         }
3189     }
3194 /**
3195  * Set SPIFontSize object from string.
3196  */
3197 static void
3198 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3200     if (!strcmp(str, "inherit")) {
3201         val->set = TRUE;
3202         val->inherit = TRUE;
3203     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3204         for (unsigned i = 0; enum_font_size[i].key; i++) {
3205             if (!strcmp(str, enum_font_size[i].key)) {
3206                 val->set = TRUE;
3207                 val->inherit = FALSE;
3208                 val->type = SP_FONT_SIZE_LITERAL;
3209                 val->value = enum_font_size[i].value;
3210                 return;
3211             }
3212         }
3213         /* Invalid */
3214         return;
3215     } else {
3216         gdouble value;
3217         gchar *e;
3218         /* fixme: Move this to standard place (Lauris) */
3219         value = g_ascii_strtod(str, &e);
3220         if ((gchar const *) e != str) {
3221             if (!*e) {
3222                 /* Userspace */
3223             } else if (!strcmp(e, "px")) {
3224                 /* Userspace */
3225             } else if (!strcmp(e, "pt")) {
3226                 /* Userspace * DEVICESCALE */
3227                 value *= PX_PER_PT;
3228             } else if (!strcmp(e, "pc")) {
3229                 /* 12pt */
3230                 value *= PX_PER_PT * 12.0;
3231             } else if (!strcmp(e, "mm")) {
3232                 value *= PX_PER_MM;
3233             } else if (!strcmp(e, "cm")) {
3234                 value *= PX_PER_CM;
3235             } else if (!strcmp(e, "in")) {
3236                 value *= PX_PER_IN;
3237             } else if (!strcmp(e, "%")) {
3238                 /* Percentage */
3239                 val->set = TRUE;
3240                 val->inherit = FALSE;
3241                 val->type = SP_FONT_SIZE_PERCENTAGE;
3242                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3243                 return;
3244             } else {
3245                 /* Invalid */
3246                 return;
3247             }
3248             /* Length */
3249             val->set = TRUE;
3250             val->inherit = FALSE;
3251             val->type = SP_FONT_SIZE_LENGTH;
3252             val->computed = value;
3253             return;
3254         }
3255     }
3260 /**
3261  * Set SPIFilter object from string.
3262  */
3263 static void
3264 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3266     SPIFilter *f = &(style->filter);
3267     /* Try all possible values: inherit, none, uri */
3268     if (streq(str, "inherit")) {
3269         f->set = TRUE;
3270         f->inherit = TRUE;
3271         if (f->href && f->href->getObject())
3272             f->href->detach(); 
3273     } else if(streq(str, "none")) {
3274         f->set = TRUE;
3275         f->inherit = FALSE;
3276         if (f->href && f->href->getObject())
3277            f->href->detach(); 
3278     } else if (strneq(str, "url", 3)) {
3279         char *uri = extract_uri(str);
3280         if(uri == NULL || uri[0] == '\0') {
3281             g_warning("Specified filter url is empty");
3282             f->set = TRUE;
3283             f->inherit = FALSE;
3284             return;
3285         }
3286         f->set = TRUE;
3287         f->inherit = FALSE;
3288         if (f->href && f->href->getObject())
3289             f->href->detach();
3291         // it may be that this style has not yet created its SPFilterReference;
3292         // now that we have a document, we can create it here
3293         if (!f->href && document) {
3294             f->href = new SPFilterReference(document);
3295             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3296         }
3298         try {
3299             f->href->attach(Inkscape::URI(uri));
3300         } catch (Inkscape::BadURIException &e) {
3301             g_warning("%s", e.what());
3302             f->href->detach();
3303         }
3304         g_free (uri);
3306     } else {
3307         /* We shouldn't reach this if SVG input is well-formed */
3308         f->set = FALSE;
3309         f->inherit = FALSE;
3310         if (f->href && f->href->getObject())
3311             f->href->detach(); 
3312     }
3315 /**
3316  * Set SPIEnum object from repr attribute.
3317  */
3318 static void
3319 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3320                     gchar const *key, SPStyleEnum const *dict,
3321                     bool const can_explicitly_inherit)
3323     gchar const *str = repr->attribute(key);
3324     if (str) {
3325         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3326     }
3331 /**
3332  * Set SPILength object from repr attribute.
3333  */
3334 static void
3335 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3337     gchar const *str = repr->attribute(key);
3338     if (str) {
3339         sp_style_read_ilength(val, str);
3340     }
3343 /**
3344  * Set SPIFontSize object from repr attribute.
3345  */
3346 static void
3347 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3349     gchar const *str = repr->attribute(key);
3350     if (str) {
3351         sp_style_read_ifontsize(val, str);
3352     }
3356 /**
3357  * Set SPIFloat object from repr attribute.
3358  */
3359 static void
3360 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3362     gchar const *str = repr->attribute(key);
3363     if (str) {
3364         sp_style_read_ifloat(val, str);
3365     }
3369 /**
3370  * Write SPIFloat object into string.
3371  */
3372 static gint
3373 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3374                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3376     Inkscape::CSSOStringStream os;
3378     if ((flags & SP_STYLE_FLAG_ALWAYS)
3379         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3380         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3381             && (!base->set || (val->value != base->value))))
3382     {
3383         if (val->inherit) {
3384             return g_snprintf(p, len, "%s:inherit;", key);
3385         } else {
3386             os << key << ":" << val->value << ";";
3387             return g_strlcpy(p, os.str().c_str(), len);
3388         }
3389     }
3390     return 0;
3394 /**
3395  * Write SPIScale24 object into string.
3396  */
3397 static gint
3398 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3399                         SPIScale24 const *const val, SPIScale24 const *const base,
3400                         guint const flags)
3402     Inkscape::CSSOStringStream os;
3404     if ((flags & SP_STYLE_FLAG_ALWAYS)
3405         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3406         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3407             && (!base->set || (val->value != base->value))))
3408     {
3409         if (val->inherit) {
3410             return g_snprintf(p, len, "%s:inherit;", key);
3411         } else {
3412             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3413             return g_strlcpy(p, os.str().c_str(), len);
3414         }
3415     }
3416     return 0;
3420 /**
3421  * Write SPIEnum object into string.
3422  */
3423 static gint
3424 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3425                      SPStyleEnum const *const dict,
3426                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3428     if ((flags & SP_STYLE_FLAG_ALWAYS)
3429         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3430         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3431             && (!base->set || (val->computed != base->computed))))
3432     {
3433         if (val->inherit) {
3434             return g_snprintf(p, len, "%s:inherit;", key);
3435         }
3436         for (unsigned i = 0; dict[i].key; i++) {
3437             if (dict[i].value == static_cast< gint > (val->value) ) {
3438                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3439             }
3440         }
3441     }
3442     return 0;
3447 /**
3448  * Write SPIString object into string.
3449  */
3450 static gint
3451 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3452                        SPIString const *const val, SPIString const *const base, guint const flags)
3454     if ((flags & SP_STYLE_FLAG_ALWAYS)
3455         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3456         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3457             && (!base->set || strcmp(val->value, base->value))))
3458     {
3459         if (val->inherit) {
3460             return g_snprintf(p, len, "%s:inherit;", key);
3461         } else {
3462             gchar *val_quoted = css2_escape_quote(val->value);
3463             if (val_quoted) {
3464                 return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3465                 g_free (val_quoted);
3466             }
3467         }
3468     }
3469     return 0;
3473 /**
3474  *
3475  */
3476 static bool
3477 sp_length_differ(SPILength const *const a, SPILength const *const b)
3479     if (a->unit != b->unit) {
3480         if (a->unit == SP_CSS_UNIT_EM) return true;
3481         if (a->unit == SP_CSS_UNIT_EX) return true;
3482         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3483         if (b->unit == SP_CSS_UNIT_EM) return true;
3484         if (b->unit == SP_CSS_UNIT_EX) return true;
3485         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3486     }
3488     return (a->computed != b->computed);
3493 /**
3494  * Write SPILength object into string.
3495  */
3496 static gint
3497 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3498                        SPILength const *const val, SPILength const *const base, guint const flags)
3500     Inkscape::CSSOStringStream os;
3502     if ((flags & SP_STYLE_FLAG_ALWAYS)
3503         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3504         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3505             && (!base->set || sp_length_differ(val, base))))
3506     {
3507         if (val->inherit) {
3508             return g_snprintf(p, len, "%s:inherit;", key);
3509         } else {
3510             switch (val->unit) {
3511                 case SP_CSS_UNIT_NONE:
3512                     os << key << ":" << val->computed << ";";
3513                     return g_strlcpy(p, os.str().c_str(), len);
3514                     break;
3515                 case SP_CSS_UNIT_PX:
3516                     os << key << ":" << val->computed << "px;";
3517                     return g_strlcpy(p, os.str().c_str(), len);
3518                     break;
3519                 case SP_CSS_UNIT_PT:
3520                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3521                     return g_strlcpy(p, os.str().c_str(), len);
3522                     break;
3523                 case SP_CSS_UNIT_PC:
3524                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3525                     return g_strlcpy(p, os.str().c_str(), len);
3526                     break;
3527                 case SP_CSS_UNIT_MM:
3528                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3529                     return g_strlcpy(p, os.str().c_str(), len);
3530                     break;
3531                 case SP_CSS_UNIT_CM:
3532                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3533                     return g_strlcpy(p, os.str().c_str(), len);
3534                     break;
3535                 case SP_CSS_UNIT_IN:
3536                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3537                     return g_strlcpy(p, os.str().c_str(), len);
3538                     break;
3539                 case SP_CSS_UNIT_EM:
3540                     os << key << ":" << val->value << "em;";
3541                     return g_strlcpy(p, os.str().c_str(), len);
3542                     break;
3543                 case SP_CSS_UNIT_EX:
3544                     os << key << ":" << val->value << "ex;";
3545                     return g_strlcpy(p, os.str().c_str(), len);
3546                     break;
3547                 case SP_CSS_UNIT_PERCENT:
3548                     os << key << ":" << (val->value * 100.0) << "%;";
3549                     return g_strlcpy(p, os.str().c_str(), len);
3550                     break;
3551                 default:
3552                     /* Invalid */
3553                     break;
3554             }
3555         }
3556     }
3557     return 0;
3561 /**
3562  *
3563  */
3564 static bool
3565 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3567     if (a->normal != b->normal) return true;
3568     if (a->normal) return false;
3570     if (a->unit != b->unit) {
3571         if (a->unit == SP_CSS_UNIT_EM) return true;
3572         if (a->unit == SP_CSS_UNIT_EX) return true;
3573         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3574         if (b->unit == SP_CSS_UNIT_EM) return true;
3575         if (b->unit == SP_CSS_UNIT_EX) return true;
3576         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3577     }
3579     return (a->computed != b->computed);
3582 /**
3583  * Write SPILengthOrNormal object into string.
3584  */
3585 static gint
3586 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3587                                SPILengthOrNormal const *const val,
3588                                SPILengthOrNormal const *const base,
3589                                guint const flags)
3591     if ((flags & SP_STYLE_FLAG_ALWAYS)
3592         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3593         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3594             && (!base->set || sp_lengthornormal_differ(val, base))))
3595     {
3596         if (val->normal) {
3597             return g_snprintf(p, len, "%s:normal;", key);
3598         } else {
3599             SPILength length;
3600             length.set = val->set;
3601             length.inherit = val->inherit;
3602             length.unit = val->unit;
3603             length.value = val->value;
3604             length.computed = val->computed;
3605             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3606         }
3607     }
3608     return 0;
3611 /**
3612  *
3613  */
3614 static bool
3615 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3617     return    a->underline != b->underline
3618            || a->overline != b->overline
3619            || a->line_through != b->line_through
3620            || a->blink != b->blink;
3623 /**
3624  * Write SPITextDecoration object into string.
3625  */
3626 static gint
3627 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3628                                SPITextDecoration const *const val,
3629                                SPITextDecoration const *const base,
3630                                guint const flags)
3632     Inkscape::CSSOStringStream os;
3634     if ((flags & SP_STYLE_FLAG_ALWAYS)
3635         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3636         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3637             && (!base->set || sp_textdecoration_differ(val, base))))
3638     {
3639         if (val->inherit) {
3640             return g_snprintf(p, len, "%s:inherit;", key);
3641         } else {
3642             os << key << ":";
3643             if (val->underline || val->overline || val->line_through || val->blink) {
3644                 if (val->underline) os << " underline";
3645                 if (val->overline) os << " overline";
3646                 if (val->line_through) os << " line-through";
3647                 if (val->blink) os << " blink";
3648             } else
3649                 os << "none";
3650             os << ";";
3651             return g_strlcpy(p, os.str().c_str(), len);
3652         }
3653     }
3654     return 0;
3657 /**
3658  *
3659  */
3660 static bool
3661 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3663     if ( (a->isColor() != b->isColor())
3664          || (a->isPaintserver() != b->isPaintserver())
3665          || (a->set != b->set)
3666          || (a->currentcolor != b->currentcolor)
3667          || (a->inherit!= b->inherit) ) {
3668         return true;
3669     }
3671     // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3673     if ( a->isPaintserver() ) {
3674         return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3675     }
3677     if ( a->isColor() ) {
3678         return !( (a->value.color == b->value.color)
3679                  && ((a->value.color.icc == b->value.color.icc)
3680                      || (a->value.color.icc && b->value.color.icc
3681                          && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3682                          && (a->value.color.icc->colors == b->value.color.icc->colors))));
3683     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3684      * in the string representation. */
3685     }
3687     return false;
3692 /**
3693  * Write SPIPaint object into string.
3694  */
3695 static gint
3696 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3697                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3699     int retval = 0;
3701     if ((flags & SP_STYLE_FLAG_ALWAYS)
3702         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3703         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3704             && (!base->set || sp_paint_differ(paint, base))))
3705     {
3706         CSSOStringStream css;
3708         if (paint->inherit) {
3709             css << "inherit";
3710         } else {
3711             if ( paint->value.href && paint->value.href->getURI() ) {
3712                 const gchar* uri = paint->value.href->getURI()->toString();
3713                 css << "url(" << uri << ")";
3714             }
3716             if ( paint->noneSet ) {
3717                 if ( !css.str().empty() ) {
3718                     css << " ";
3719                 }
3720                 css << "none";
3721             }
3723             if ( paint->currentcolor ) {
3724                 if ( !css.str().empty() ) {
3725                     css << " ";
3726                 }
3727                 css << "currentColor";
3728             }
3730             if ( paint->colorSet && !paint->currentcolor ) {
3731                 if ( !css.str().empty() ) {
3732                     css << " ";
3733                 }
3734                 char color_buf[8];
3735                 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3736                 css << color_buf;
3737             }
3739             if (paint->value.color.icc && !paint->currentcolor) {
3740                 if ( !css.str().empty() ) {
3741                     css << " ";
3742                 }
3743                 css << "icc-color(" << paint->value.color.icc->colorProfile;
3744                 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3745                          iEnd(paint->value.color.icc->colors.end());
3746                      i != iEnd; ++i) {
3747                     css << ", " << *i;
3748                 }
3749                 css << ')';
3750             }
3751         }
3753         if ( !css.str().empty() ) {
3754             retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3755         }
3756     }
3758     return retval;
3762 /**
3763  *
3764  */
3765 static bool
3766 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3768     if (a->type != b->type)
3769         return true;
3770     if (a->type == SP_FONT_SIZE_LENGTH) {
3771         if (a->computed != b->computed)
3772             return true;
3773     } else {
3774         if (a->value != b->value)
3775             return true;
3776     }
3777     return false;
3781 /**
3782  * Write SPIFontSize object into string.
3783  */
3784 static gint
3785 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3786                          SPIFontSize const *const val, SPIFontSize const *const base,
3787                          guint const flags)
3789     if ((flags & SP_STYLE_FLAG_ALWAYS)
3790         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3791         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3792             && (!base->set || sp_fontsize_differ(val, base))))
3793     {
3794         if (val->inherit) {
3795             return g_snprintf(p, len, "%s:inherit;", key);
3796         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3797             for (unsigned i = 0; enum_font_size[i].key; i++) {
3798                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3799                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3800                 }
3801             }
3802         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3803             Inkscape::CSSOStringStream os;
3804             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3805             return g_strlcpy(p, os.str().c_str(), len);
3806         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3807             Inkscape::CSSOStringStream os;
3808             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3809             return g_strlcpy(p, os.str().c_str(), len);
3810         }
3811     }
3812     return 0;
3816 /**
3817  * Write SPIFilter object into string.
3818  */
3819 static gint
3820 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3821                          SPIFilter const *const val, SPIFilter const *const base,
3822                          guint const flags)
3824     (void)base; // TODO
3825     if ((flags & SP_STYLE_FLAG_ALWAYS)
3826         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3827         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3828     {
3829         if (val->inherit) {
3830             return g_snprintf(p, len, "%s:inherit;", key);
3831         } else if (val->href && val->href->getURI()) {
3832             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
3833         }
3834     }
3837     return 0;
3841 void SPIPaint::clear()
3843     set = false;
3844     inherit = false;
3845     currentcolor = false;
3846     colorSet = false;
3847     noneSet = false;
3848     value.color.set( 0 );
3849     if ( value.href && value.href->getObject() )
3850     {
3851         value.href->detach();
3852     }
3856 /**
3857  * Clear filter object, and disconnect style from paintserver (if present).
3858  */
3859 static void
3860 sp_style_filter_clear(SPStyle *style)
3862     if (style->filter.href && style->filter.href->getObject())
3863         style->filter.href->detach();
3867 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3869 void
3870 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3872     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3874     if (repr == NULL) return;
3876     SPCSSAttr *css = sp_repr_css_attr_new();
3877     if (linked) {
3878         gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3879         sp_repr_css_set_property(css, property, val);
3880         g_free(val);
3881     } else {
3882         sp_repr_css_unset_property(css, "filter");
3883     }
3885     if (recursive) {
3886         sp_repr_css_change_recursive(repr, css, "style");
3887     } else {
3888         sp_repr_css_change(repr, css, "style");
3889     }
3890     sp_repr_css_attr_unref(css);
3894 /**
3895  * Clear all style property attributes in object.
3896  */
3897 void
3898 sp_style_unset_property_attrs(SPObject *o)
3900     if (!o) return;
3902     SPStyle *style = SP_OBJECT_STYLE(o);
3903     if (!style) return;
3905     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3906     if (!repr) return;
3908     if (style->opacity.set) {
3909         repr->setAttribute("opacity", NULL);
3910     }
3911     if (style->color.set) {
3912         repr->setAttribute("color", NULL);
3913     }
3914     if (style->fill.set) {
3915         repr->setAttribute("fill", NULL);
3916     }
3917     if (style->fill_opacity.set) {
3918         repr->setAttribute("fill-opacity", NULL);
3919     }
3920     if (style->fill_rule.set) {
3921         repr->setAttribute("fill-rule", NULL);
3922     }
3923     if (style->stroke.set) {
3924         repr->setAttribute("stroke", NULL);
3925     }
3926     if (style->stroke_width.set) {
3927         repr->setAttribute("stroke-width", NULL);
3928     }
3929     if (style->stroke_linecap.set) {
3930         repr->setAttribute("stroke-linecap", NULL);
3931     }
3932     if (style->stroke_linejoin.set) {
3933         repr->setAttribute("stroke-linejoin", NULL);
3934     }
3935     if (style->marker[SP_MARKER_LOC].set) {
3936         repr->setAttribute("marker", NULL);
3937     }
3938     if (style->marker[SP_MARKER_LOC_START].set) {
3939         repr->setAttribute("marker-start", NULL);
3940     }
3941     if (style->marker[SP_MARKER_LOC_MID].set) {
3942         repr->setAttribute("marker-mid", NULL);
3943     }
3944     if (style->marker[SP_MARKER_LOC_END].set) {
3945         repr->setAttribute("marker-end", NULL);
3946     }
3947     if (style->stroke_opacity.set) {
3948         repr->setAttribute("stroke-opacity", NULL);
3949     }
3950     if (style->stroke_dasharray_set) {
3951         repr->setAttribute("stroke-dasharray", NULL);
3952     }
3953     if (style->stroke_dashoffset_set) {
3954         repr->setAttribute("stroke-dashoffset", NULL);
3955     }
3956     if (style->text_private && style->text->font_specification.set) {
3957         repr->setAttribute("-inkscape-font-specification", NULL);
3958     }
3959     if (style->text_private && style->text->font_family.set) {
3960         repr->setAttribute("font-family", NULL);
3961     }
3962     if (style->text_anchor.set) {
3963         repr->setAttribute("text-anchor", NULL);
3964     }
3965     if (style->writing_mode.set) {
3966         repr->setAttribute("writing_mode", NULL);
3967     }
3968     if (style->filter.set) {
3969         repr->setAttribute("filter", NULL);
3970     }
3971     if (style->enable_background.set) {
3972         repr->setAttribute("enable-background", NULL);
3973     }
3976 /**
3977  * \pre style != NULL.
3978  * \pre flags in {IFSET, ALWAYS}.
3979  */
3980 SPCSSAttr *
3981 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3983     g_return_val_if_fail(style != NULL, NULL);
3984     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3985                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3986                          NULL);
3987     gchar *style_str = sp_style_write_string(style, flags);
3988     SPCSSAttr *css = sp_repr_css_attr_new();
3989     sp_repr_css_attr_add_from_string(css, style_str);
3990     g_free(style_str);
3991     return css;
3995 /**
3996  * \pre object != NULL
3997  * \pre flags in {IFSET, ALWAYS}.
3998  */
3999 SPCSSAttr *
4000 sp_css_attr_from_object(SPObject *object, guint const flags)
4002     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4003                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
4004                          NULL);
4005     SPStyle const *const style = SP_OBJECT_STYLE(object);
4006     if (style == NULL)
4007         return NULL;
4008     return sp_css_attr_from_style(style, flags);
4011 /**
4012  * Unset any text-related properties
4013  */
4014 SPCSSAttr *
4015 sp_css_attr_unset_text(SPCSSAttr *css)
4017     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
4018     sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
4019     sp_repr_css_set_property(css, "font-size", NULL);
4020     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
4021     sp_repr_css_set_property(css, "font-style", NULL);
4022     sp_repr_css_set_property(css, "font-variant", NULL);
4023     sp_repr_css_set_property(css, "font-weight", NULL);
4024     sp_repr_css_set_property(css, "font-stretch", NULL);
4025     sp_repr_css_set_property(css, "font-family", NULL);
4026     sp_repr_css_set_property(css, "text-indent", NULL);
4027     sp_repr_css_set_property(css, "text-align", NULL);
4028     sp_repr_css_set_property(css, "text-decoration", NULL);
4029     sp_repr_css_set_property(css, "line-height", NULL);
4030     sp_repr_css_set_property(css, "letter-spacing", NULL);
4031     sp_repr_css_set_property(css, "word-spacing", NULL);
4032     sp_repr_css_set_property(css, "text-transform", NULL);
4033     sp_repr_css_set_property(css, "direction", NULL);
4034     sp_repr_css_set_property(css, "block-progression", NULL);
4035     sp_repr_css_set_property(css, "writing-mode", NULL);
4036     sp_repr_css_set_property(css, "text-anchor", NULL);
4037     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
4038     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
4039     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
4040     sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
4042     return css;
4045 bool
4046 is_url(char const *p)
4048     if (p == NULL)
4049         return false;
4050 /** \todo
4051  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
4052  * in property values must start with 'url('.
4053  */
4054     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
4057 /**
4058  * Unset any properties that contain URI values.
4059  *
4060  * Used for storing style that will be reused across documents when carrying
4061  * the referenced defs is impractical.
4062  */
4063 SPCSSAttr *
4064 sp_css_attr_unset_uris(SPCSSAttr *css)
4066 // All properties that may hold <uri> or <paint> according to SVG 1.1
4067     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
4068     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
4069     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
4070     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
4071     if (is_url(sp_repr_css_property(css, "marker", NULL))) sp_repr_css_set_property(css, "marker", NULL);
4072     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
4073     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
4074     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
4075     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
4076     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
4077     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
4079     return css;
4082 /**
4083  * Scale a single-value property.
4084  */
4085 void
4086 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
4087                                   double ex, bool only_with_units = false)
4089     gchar const *w = sp_repr_css_property(css, property, NULL);
4090     if (w) {
4091         gchar *units = NULL;
4092         double wd = g_ascii_strtod(w, &units) * ex;
4093         if (w == units) {// nothing converted, non-numeric value
4094             return;
4095         }
4096         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
4097             // only_with_units, but no units found, so do nothing.
4098             return;
4099         }
4100         Inkscape::CSSOStringStream os;
4101         os << wd << units; // reattach units
4102         sp_repr_css_set_property(css, property, os.str().c_str());
4103     }
4106 /**
4107  * Scale a list-of-values property.
4108  */
4109 void
4110 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
4112     gchar const *string = sp_repr_css_property(css, property, NULL);
4113     if (string) {
4114         Inkscape::CSSOStringStream os;
4115         gchar **a = g_strsplit(string, ",", 10000);
4116         bool first = true;
4117         for (gchar **i = a; i != NULL; i++) {
4118             gchar *w = *i;
4119             if (w == NULL)
4120                 break;
4121             gchar *units = NULL;
4122             double wd = g_ascii_strtod(w, &units) * ex;
4123             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4124                 g_strfreev(a);
4125                 return;
4126             }
4127             if (!first) {
4128                 os << ",";
4129             }
4130             os << wd << units; // reattach units
4131             first = false;
4132         }
4133         sp_repr_css_set_property(css, property, os.str().c_str());
4134         g_strfreev(a);
4135     }
4138 /**
4139  * Scale any properties that may hold <length> by ex.
4140  */
4141 SPCSSAttr *
4142 sp_css_attr_scale(SPCSSAttr *css, double ex)
4144     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4145     sp_css_attr_scale_property_single(css, "stroke-width", ex);
4146     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
4147     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4148     sp_css_attr_scale_property_single(css, "font-size", ex);
4149     sp_css_attr_scale_property_single(css, "kerning", ex);
4150     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4151     sp_css_attr_scale_property_single(css, "word-spacing", ex);
4152     sp_css_attr_scale_property_single(css, "line-height", ex, true);
4154     return css;
4158 /**
4159  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4160  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4161  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4162  * it parsed for us. So this function is only used to remove those quotes and for
4163  * presentation attributes, without any unescaping. (XML unescaping
4164  * (&amp; etc) is done by XML parser.)
4165  */
4166 gchar *
4167 attribute_unquote(gchar const *val)
4169     if (val) {
4170         if (*val == '\'' || *val == '"') {
4171             int l = strlen(val);
4172             if (l >= 2) {
4173                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4174                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4175                     return (g_strndup (val+1, l-2));
4176                 }
4177             }
4178         }
4179     }
4181     return (val? g_strdup (val) : NULL);
4184 /**
4185  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4186  */
4187 gchar *
4188 css2_escape_quote(gchar const *val) {
4190     Glib::ustring t;
4191     bool quote = false;
4192     bool last_was_space = false;
4194     for (gchar const *i = val; *i; i++) {
4195         bool is_space = ( *i == ' ' );
4196         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4197             // ASCII alphanumeric, - and _ don't require quotes
4198             t.push_back(*i);
4199         } else if ( is_space && !last_was_space ) {
4200             // non-consecutive spaces don't require quotes
4201             t.push_back(*i);
4202         } else if (*i=='\'') {
4203             // single quotes require escaping and quotes
4204             t.push_back('\\');
4205             t.push_back(*i);
4206             quote = true;
4207         } else {
4208             // everything else requires quotes
4209             t.push_back(*i);
4210             quote = true;
4211         }
4212         if (i == val && !g_ascii_isalpha(*i)) {
4213             // a non-ASCII/non-alpha initial character requires quotes
4214             quote = true;
4215         }
4216         last_was_space = is_space;
4217     }
4219     if (last_was_space) {
4220         // a trailing space requires quotes
4221         quote = true;
4222     }
4224     if (quote) {
4225         // we use single quotes so the result can be stored in an XML
4226         // attribute without incurring un-aesthetic additional quoting
4227         // (our XML emitter always uses double quotes)
4228         t.insert(t.begin(), '\'');
4229         t.push_back('\'');
4230     }
4232     return (t.empty() ? NULL : g_strdup (t.c_str()));
4235 /*
4236   Local Variables:
4237   mode:c++
4238   c-file-style:"stroustrup"
4239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4240   indent-tabs-mode:nil
4241   fill-column:99
4242   End:
4243 */
4244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :