Code

ffc1fb0aeaf9eb6681eb8e352c851cc5f03e4ca5
[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_ibaselineshift(SPIBaselineShift *val, gchar const *str);
91 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
93 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
94 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
95 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
96 static void sp_style_read_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key);
97 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
99 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
100 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
101 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
102 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
103 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
104 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
105 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
106 static gint sp_style_write_ibaselineshift(gchar *p, gint len, gchar const *key, SPIBaselineShift const *val, SPIBaselineShift const *base, guint flags);
107 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);
108 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);
109 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
111 static void sp_style_filter_clear(SPStyle *style);
113 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
114 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
116 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
117 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
119 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
121 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
122 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
124 #define SPS_READ_IBASELINE_SHIFT_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ibaselineshift((v), (s));}
125 #define SPS_READ_PBASELINE_SHIFT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pbaselineshift((v), (r), (k));}
127 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
129 struct SPStyleEnum {
130     gchar const *key;
131     gint value;
132 };
134 static SPStyleEnum const enum_fill_rule[] = {
135     {"nonzero", SP_WIND_RULE_NONZERO},
136     {"evenodd", SP_WIND_RULE_EVENODD},
137     {NULL, -1}
138 };
140 static SPStyleEnum const enum_stroke_linecap[] = {
141     {"butt", SP_STROKE_LINECAP_BUTT},
142     {"round", SP_STROKE_LINECAP_ROUND},
143     {"square", SP_STROKE_LINECAP_SQUARE},
144     {NULL, -1}
145 };
147 static SPStyleEnum const enum_stroke_linejoin[] = {
148     {"miter", SP_STROKE_LINEJOIN_MITER},
149     {"round", SP_STROKE_LINEJOIN_ROUND},
150     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
151     {NULL, -1}
152 };
154 static SPStyleEnum const enum_font_style[] = {
155     {"normal", SP_CSS_FONT_STYLE_NORMAL},
156     {"italic", SP_CSS_FONT_STYLE_ITALIC},
157     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
158     {NULL, -1}
159 };
161 static SPStyleEnum const enum_font_size[] = {
162     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
163     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
164     {"small", SP_CSS_FONT_SIZE_SMALL},
165     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
166     {"large", SP_CSS_FONT_SIZE_LARGE},
167     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
168     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
169     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
170     {"larger", SP_CSS_FONT_SIZE_LARGER},
171     {NULL, -1}
172 };
174 static SPStyleEnum const enum_font_variant[] = {
175     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
176     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
177     {NULL, -1}
178 };
180 static SPStyleEnum const enum_font_weight[] = {
181     {"100", SP_CSS_FONT_WEIGHT_100},
182     {"200", SP_CSS_FONT_WEIGHT_200},
183     {"300", SP_CSS_FONT_WEIGHT_300},
184     {"400", SP_CSS_FONT_WEIGHT_400},
185     {"500", SP_CSS_FONT_WEIGHT_500},
186     {"600", SP_CSS_FONT_WEIGHT_600},
187     {"700", SP_CSS_FONT_WEIGHT_700},
188     {"800", SP_CSS_FONT_WEIGHT_800},
189     {"900", SP_CSS_FONT_WEIGHT_900},
190     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
191     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
192     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
193     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
194     {NULL, -1}
195 };
197 static SPStyleEnum const enum_font_stretch[] = {
198     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
199     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
200     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
201     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
202     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
203     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
204     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
205     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
206     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
207     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
208     {"wider", SP_CSS_FONT_STRETCH_WIDER},
209     {NULL, -1}
210 };
212 static SPStyleEnum const enum_text_align[] = {
213     {"start", SP_CSS_TEXT_ALIGN_START},
214     {"end", SP_CSS_TEXT_ALIGN_END},
215     {"left", SP_CSS_TEXT_ALIGN_LEFT},
216     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
217     {"center", SP_CSS_TEXT_ALIGN_CENTER},
218     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
219     {NULL, -1}
220 };
222 static SPStyleEnum const enum_text_transform[] = {
223     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
224     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
225     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
226     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
227     {NULL, -1}
228 };
230 static SPStyleEnum const enum_text_anchor[] = {
231     {"start", SP_CSS_TEXT_ANCHOR_START},
232     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
233     {"end", SP_CSS_TEXT_ANCHOR_END},
234     {NULL, -1}
235 };
237 static SPStyleEnum const enum_direction[] = {
238     {"ltr", SP_CSS_DIRECTION_LTR},
239     {"rtl", SP_CSS_DIRECTION_RTL},
240     {NULL, -1}
241 };
243 static SPStyleEnum const enum_block_progression[] = {
244     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
245     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
246     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
247     {NULL, -1}
248 };
250 static SPStyleEnum const enum_writing_mode[] = {
251     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
252      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
253      * value is lr-tb rather than lr.
254      *
255      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
256      * sharing enumerators for different strings may be a bug (once we support ecma script).
257      */
258     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
259     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
260     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
261     {"lr", SP_CSS_WRITING_MODE_LR_TB},
262     {"rl", SP_CSS_WRITING_MODE_RL_TB},
263     {"tb", SP_CSS_WRITING_MODE_TB_RL},
264     {NULL, -1}
265 };
267 static SPStyleEnum const enum_baseline_shift[] = {
268     {"baseline", SP_CSS_BASELINE_SHIFT_BASELINE},
269     {"sub",      SP_CSS_BASELINE_SHIFT_SUB},
270     {"super",    SP_CSS_BASELINE_SHIFT_SUPER},
271     {NULL, -1}
272 };
274 static SPStyleEnum const enum_visibility[] = {
275     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
276     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
277     {"visible", SP_CSS_VISIBILITY_VISIBLE},
278     {NULL, -1}
279 };
281 static SPStyleEnum const enum_overflow[] = {
282     {"visible", SP_CSS_OVERFLOW_VISIBLE},
283     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
284     {"scroll", SP_CSS_OVERFLOW_SCROLL},
285     {"auto", SP_CSS_OVERFLOW_AUTO},
286     {NULL, -1}
287 };
289 static SPStyleEnum const enum_display[] = {
290     {"none",      SP_CSS_DISPLAY_NONE},
291     {"inline",    SP_CSS_DISPLAY_INLINE},
292     {"block",     SP_CSS_DISPLAY_BLOCK},
293     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
294     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
295     {"compact",   SP_CSS_DISPLAY_COMPACT},
296     {"marker",    SP_CSS_DISPLAY_MARKER},
297     {"table",     SP_CSS_DISPLAY_TABLE},
298     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
299     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
300     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
301     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
302     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
303     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
304     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
305     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
306     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
307     {NULL, -1}
308 };
310 static SPStyleEnum const enum_shape_rendering[] = {
311     {"auto", 0},
312     {"optimizeSpeed", 0},
313     {"crispEdges", 0},
314     {"geometricPrecision", 0},
315     {NULL, -1}
316 };
318 static SPStyleEnum const enum_color_rendering[] = {
319     {"auto", 0},
320     {"optimizeSpeed", 0},
321     {"optimizeQuality", 0},
322     {NULL, -1}
323 };
325 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
327 static SPStyleEnum const enum_text_rendering[] = {
328     {"auto", 0},
329     {"optimizeSpeed", 0},
330     {"optimizeLegibility", 0},
331     {"geometricPrecision", 0},
332     {NULL, -1}
333 };
335 static SPStyleEnum const enum_enable_background[] = {
336     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
337     {"new", SP_CSS_BACKGROUND_NEW},
338     {NULL, -1}
339 };
341 /**
342  * Release callback.
343  */
344 static void
345 sp_style_object_release(SPObject *object, SPStyle *style)
347     (void)object; // TODO
348     style->object = NULL;
351 /**
352  * Emit style modified signal on style's object if the filter changed.
353  */
354 static void
355 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
357     (void)flags; // TODO
358     SPFilter *filter=static_cast<SPFilter *>(obj);
359     if (style->getFilter() == filter)
360     {
361         if (style->object) {
362             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
363         }
364     }
367 /**
368  * Gets called when the filter is (re)attached to the style
369  */
370 static void
371 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
373     if (old_ref) {
374         style->filter_modified_connection.disconnect();
375     }
376     if ( SP_IS_FILTER(ref))
377     {
378         style->filter_modified_connection =
379            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
380     }
382     sp_style_filter_ref_modified(ref, 0, style);
385 /**
386  * Emit style modified signal on style's object if server is style's fill
387  * or stroke paint server.
388  */
389 static void
390 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
392     (void)flags; // TODO
393     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
395     if ((style->fill.isPaintserver())
396         && style->getFillPaintServer() == server)
397     {
398         if (style->object) {
399             /** \todo
400              * fixme: I do not know, whether it is optimal - we are
401              * forcing reread of everything (Lauris)
402              */
403             /** \todo
404              * fixme: We have to use object_modified flag, because parent
405              * flag is only available downstreams.
406              */
407             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
408         }
409     } else if ((style->stroke.isPaintserver())
410         && style->getStrokePaintServer() == server)
411     {
412         if (style->object) {
413             /// \todo fixme:
414             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
415         }
416     } else if (server) {
417         g_assert_not_reached();
418     }
421 /**
422  * Gets called when the paintserver is (re)attached to the style
423  */
424 static void
425 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
427     if (old_ref) {
428         style->fill_ps_modified_connection.disconnect();
429     }
430     if (SP_IS_PAINT_SERVER(ref)) {
431         style->fill_ps_modified_connection =
432            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
433     }
435     sp_style_paint_server_ref_modified(ref, 0, style);
438 /**
439  * Gets called when the paintserver is (re)attached to the style
440  */
441 static void
442 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
444     if (old_ref) {
445         style->stroke_ps_modified_connection.disconnect();
446     }
447     if (SP_IS_PAINT_SERVER(ref)) {
448         style->stroke_ps_modified_connection =
449           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
450     }
452     sp_style_paint_server_ref_modified(ref, 0, style);
455 /**
456  * Returns a new SPStyle object with settings as per sp_style_clear().
457  */
458 SPStyle *
459 sp_style_new(SPDocument *document)
461     SPStyle *const style = g_new0(SPStyle, 1);
463     style->refcount = 1;
464     style->object = NULL;
465     style->document = document;
466     style->text = sp_text_style_new();
467     style->text_private = TRUE;
469     sp_style_clear(style);
471     style->cloned = false;
473     new (&style->release_connection) sigc::connection();
474     new (&style->filter_modified_connection) sigc::connection();
475     new (&style->fill_ps_modified_connection) sigc::connection();
476     new (&style->stroke_ps_modified_connection) sigc::connection();
478     return style;
482 /**
483  * Creates a new SPStyle object, and attaches it to the specified SPObject.
484  */
485 SPStyle *
486 sp_style_new_from_object(SPObject *object)
488     g_return_val_if_fail(object != NULL, NULL);
489     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
491     SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
492     style->object = object;
493     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
495     if (object && SP_OBJECT_IS_CLONED(object)) {
496         style->cloned = true;
497     }
499     return style;
503 /**
504  * Increase refcount of style.
505  */
506 SPStyle *
507 sp_style_ref(SPStyle *style)
509     g_return_val_if_fail(style != NULL, NULL);
510     g_return_val_if_fail(style->refcount > 0, NULL);
512     style->refcount += 1;
514     return style;
518 /**
519  * Decrease refcount of style with possible destruction.
520  */
521 SPStyle *
522 sp_style_unref(SPStyle *style)
524     g_return_val_if_fail(style != NULL, NULL);
525     g_return_val_if_fail(style->refcount > 0, NULL);
527     style->refcount -= 1;
529     if (style->refcount < 1) {
530         style->release_connection.disconnect();
531         style->release_connection.~connection();
532         if (style->text) sp_text_style_unref(style->text);
534          if (style->fill.value.href) {
535              style->fill_ps_modified_connection.disconnect();
536              delete style->fill.value.href;
537              style->fill.value.href = NULL;
538          }
539          if (style->stroke.value.href) {
540              style->stroke_ps_modified_connection.disconnect();
541              delete style->stroke.value.href;
542              style->stroke.value.href = NULL;
543          }
544          if (style->filter.href) {
545              style->filter_modified_connection.disconnect();
546              delete style->filter.href;
547              style->filter.href = NULL;
548          }
550         style->filter_modified_connection.~connection();
551         style->fill_ps_modified_connection.~connection();
552         style->stroke_ps_modified_connection.~connection();
554         style->fill.clear();
555         style->stroke.clear();
556         sp_style_filter_clear(style);
558         g_free(style->stroke_dash.dash);
559         g_free(style);
560     }
562     return NULL;
565 /**
566  *  Reads the various style parameters for an object from repr.
567  */
568 static void
569 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
571     g_assert(style != NULL);
572     g_assert(repr != NULL);
573     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
575     sp_style_clear(style);
577     if (object && SP_OBJECT_IS_CLONED(object)) {
578         style->cloned = true;
579     }
581     /* 1. Style attribute */
582     gchar const *val = repr->attribute("style");
583     if (val != NULL) {
584         sp_style_merge_from_style_string(style, val);
585     }
587     if (object) {
588         sp_style_merge_from_object_stylesheet(style, object);
589     } else {
590         /** \todo No stylesheet information. Find out under what circumstances
591          * this occurs, and handle accordingly.  (If we really wanted to, we
592          * could probably get stylesheets by going through repr->doc.)
593          */
594     }
596     /* 2. Presentation attributes */
597     /* CSS2 */
598     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
599     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
600     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
601     /* Font */
602     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
603     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
604     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
605     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
606     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
607     /* Text (css2 chapter 16) */
608     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
609     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
610     if (!style->text_decoration.set) {
611         val = repr->attribute("text-decoration");
612         if (val) {
613             sp_style_read_itextdecoration(&style->text_decoration, val);
614         }
615     }
616     if (!style->line_height.set) {
617         val = repr->attribute("line-height");
618         if (val) {
619             sp_style_read_ilengthornormal(&style->line_height, val);
620         }
621     }
622     if (!style->letter_spacing.set) {
623         val = repr->attribute("letter-spacing");
624         if (val) {
625             sp_style_read_ilengthornormal(&style->letter_spacing, val);
626         }
627     }
628     if (!style->word_spacing.set) {
629         val = repr->attribute("word-spacing");
630         if (val) {
631             sp_style_read_ilengthornormal(&style->word_spacing, val);
632         }
633     }
634     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
635     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
636     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
638     /* SVG */
639     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
640                             enum_writing_mode, true);
641     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
642                             enum_text_anchor, true);
643     SPS_READ_PBASELINE_SHIFT_IF_UNSET(&style->baseline_shift, repr, "baseline-shift");
645     /* opacity */
646     if (!style->opacity.set) {
647         val = repr->attribute("opacity");
648         if (val) {
649             sp_style_read_iscale24(&style->opacity, val);
650         }
651     }
652     /* color */
653     if (!style->color.set) {
654         val = repr->attribute("color");
655         if (val) {
656             sp_style_read_icolor(&style->color, val, style, ( object
657                                                               ? SP_OBJECT_DOCUMENT(object)
658                                                               : NULL ));
659         }
660     }
661     /* fill */
662     if (!style->fill.set) {
663         val = repr->attribute("fill");
664         if (val) {
665             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
666         }
667     }
668     /* fill-opacity */
669     if (!style->fill_opacity.set) {
670         val = repr->attribute("fill-opacity");
671         if (val) {
672             sp_style_read_iscale24(&style->fill_opacity, val);
673         }
674     }
675     /* fill-rule */
676     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
677     /* stroke */
678     if (!style->stroke.set) {
679         val = repr->attribute("stroke");
680         if (val) {
681             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
682         }
683     }
684     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
685     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
686     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
687     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
689     /* markers */
690     if (!style->marker[SP_MARKER_LOC].set) {
691         val = repr->attribute("marker");
692         if (val) {
693             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
694         }
695     }
696     if (!style->marker[SP_MARKER_LOC_START].set) {
697         val = repr->attribute("marker-start");
698         if (val) {
699             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
700         }
701     }
702     if (!style->marker[SP_MARKER_LOC_MID].set) {
703         val = repr->attribute("marker-mid");
704         if (val) {
705             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
706         }
707     }
708     if (!style->marker[SP_MARKER_LOC_END].set) {
709         val = repr->attribute("marker-end");
710         if (val) {
711             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
712         }
713     }
715     /* stroke-opacity */
716     if (!style->stroke_opacity.set) {
717         val = repr->attribute("stroke-opacity");
718         if (val) {
719             sp_style_read_iscale24(&style->stroke_opacity, val);
720         }
721     }
722     if (!style->stroke_dasharray_set) {
723         val = repr->attribute("stroke-dasharray");
724         if (val) {
725             sp_style_read_dash(style, val);
726         }
727     }
729     if (!style->stroke_dashoffset_set) {
730         val = repr->attribute("stroke-dashoffset");
731         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
732             style->stroke_dashoffset_set = TRUE;
733         } else if (val && !strcmp(val, "inherit")) {
734             style->stroke_dashoffset_set = TRUE;
735             style->stroke_dashoffset_inherit = TRUE;
736         } else {
737             style->stroke_dashoffset_set = FALSE;
738         }
739     }
741     /* -inkscape-font-specification */
742     if (!style->text_private || !style->text->font_specification.set) {
743         val = repr->attribute("-inkscape-font-specification");
744         if (val) {
745             if (!style->text_private) sp_style_privatize_text(style);
746             gchar *val_unquoted = attribute_unquote(val);
747             sp_style_read_istring(&style->text->font_specification, val_unquoted);
748             if (val_unquoted) g_free (val_unquoted);
749         }
750     }
752     /* font-family */
753     if (!style->text_private || !style->text->font_family.set) {
754         val = repr->attribute("font-family");
755         if (val) {
756             if (!style->text_private) sp_style_privatize_text(style);
757             gchar *val_unquoted = attribute_unquote(val);
758             sp_style_read_istring(&style->text->font_family, val_unquoted);
759             if (val_unquoted) g_free (val_unquoted);
760         }
761     }
763     /* filter effects */
764     if (!style->filter.set) {
765         val = repr->attribute("filter");
766         if (val) {
767             sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
768         }
769     }
770     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
771                             "enable-background", enum_enable_background, true);
773     /* 3. Merge from parent */
774     if (object) {
775         if (object->parent) {
776             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
777         }
778     } else {
779         if (sp_repr_parent(repr)) {
780             /// \todo fixme: This is not the prettiest thing (Lauris)
781             SPStyle *parent = sp_style_new(NULL);
782             sp_style_read(parent, NULL, sp_repr_parent(repr));
783             sp_style_merge_from_parent(style, parent);
784             sp_style_unref(parent);
785         }
786     }
790 /**
791  * Read style properties from object's repr.
792  *
793  * 1. Reset existing object style
794  * 2. Load current effective object style
795  * 3. Load i attributes from immediate parent (which has to be up-to-date)
796  */
797 void
798 sp_style_read_from_object(SPStyle *style, SPObject *object)
800     g_return_if_fail(style != NULL);
801     g_return_if_fail(object != NULL);
802     g_return_if_fail(SP_IS_OBJECT(object));
804     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
805     g_return_if_fail(repr != NULL);
807     sp_style_read(style, object, repr);
811 /**
812  * Read style properties from preferences.
813  * @param style The style to write to
814  * @param path Preferences directory from which the style should be read
815  */
816 void
817 sp_style_read_from_prefs(SPStyle *style, Glib::ustring const &path)
819     g_return_if_fail(style != NULL);
820     g_return_if_fail(path != "");
822     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
824     // not optimal: we reconstruct the node based on the prefs, then pass it to
825     // sp_style_read for actual processing.
826     Inkscape::XML::SimpleDocument *tempdoc = new Inkscape::XML::SimpleDocument;
827     Inkscape::XML::Node *tempnode = tempdoc->createElement("temp");
829     std::vector<Inkscape::Preferences::Entry> attrs = prefs->getAllEntries(path);
830     for (std::vector<Inkscape::Preferences::Entry>::iterator i = attrs.begin(); i != attrs.end(); ++i) {
831         tempnode->setAttribute(i->getEntryName().data(), i->getString().data());
832     }
834     sp_style_read(style, NULL, tempnode);
836     Inkscape::GC::release(tempnode);
837     Inkscape::GC::release(tempdoc);
838     delete tempdoc;
843 /**
844  *
845  */
846 static void
847 sp_style_privatize_text(SPStyle *style)
849     SPTextStyle *text = style->text;
850     style->text = sp_text_style_duplicate_unset(style->text);
851     sp_text_style_unref(text);
852     style->text_private = TRUE;
856 /**
857  * Merge property into style.
858  *
859  * Should be called in order of highest to lowest precedence.
860  * E.g. for a single style string, call from the last declaration to the first,
861  * as CSS says that later declarations override earlier ones.
862  *
863  * \pre val != NULL.
864  */
865 static void
866 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
868     g_return_if_fail(val != NULL);
870     switch (id) {
871         case SP_PROP_INKSCAPE_FONT_SPEC:
872             if (!style->text_private) sp_style_privatize_text(style);
873             if (!style->text->font_specification.set) {
874                 gchar *val_unquoted = attribute_unquote(val);
875                 sp_style_read_istring(&style->text->font_specification, val_unquoted);
876                 if (val_unquoted) g_free (val_unquoted);
877             }
878             break;
879         /* CSS2 */
880         /* Font */
881         case SP_PROP_FONT_FAMILY:
882             if (!style->text_private) sp_style_privatize_text(style);
883             if (!style->text->font_family.set) {
884                 gchar *val_unquoted = attribute_unquote(val);
885                 sp_style_read_istring(&style->text->font_family, val_unquoted);
886                 if (val_unquoted) g_free (val_unquoted);
887             }
888             break;
889         case SP_PROP_FONT_SIZE:
890             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
891             break;
892         case SP_PROP_FONT_SIZE_ADJUST:
893             if (strcmp(val, "none") != 0) {
894                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
895             }
896             break;
897         case SP_PROP_FONT_STYLE:
898             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
899             break;
900         case SP_PROP_FONT_VARIANT:
901             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
902             break;
903         case SP_PROP_FONT_WEIGHT:
904             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
905             break;
906         case SP_PROP_FONT_STRETCH:
907             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
908             break;
909         case SP_PROP_FONT:
910             if (!style->text_private) sp_style_privatize_text(style);
911             if (!style->text->font.set) {
912                 g_free(style->text->font.value);
913                 style->text->font.value = g_strdup(val);
914                 style->text->font.set = TRUE;
915                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
916             }
917             break;
918             /* Text */
919         case SP_PROP_TEXT_INDENT:
920             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
921             break;
922         case SP_PROP_TEXT_ALIGN:
923             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
924             break;
925         case SP_PROP_TEXT_DECORATION:
926             if (!style->text_decoration.set) {
927                 sp_style_read_itextdecoration(&style->text_decoration, val);
928             }
929             break;
930         case SP_PROP_LINE_HEIGHT:
931             if (!style->line_height.set) {
932                 sp_style_read_ilengthornormal(&style->line_height, val);
933             }
934             break;
935         case SP_PROP_LETTER_SPACING:
936             if (!style->letter_spacing.set) {
937                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
938             }
939             break;
940         case SP_PROP_WORD_SPACING:
941             if (!style->word_spacing.set) {
942                 sp_style_read_ilengthornormal(&style->word_spacing, val);
943             }
944             break;
945         case SP_PROP_TEXT_TRANSFORM:
946             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
947             break;
948             /* Text (css3) */
949         case SP_PROP_DIRECTION:
950             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
951             break;
952         case SP_PROP_BLOCK_PROGRESSION:
953             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
954             break;
955         case SP_PROP_WRITING_MODE:
956             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
957             break;
958         case SP_PROP_TEXT_ANCHOR:
959             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
960             break;
961         case SP_PROP_BASELINE_SHIFT:
962             SPS_READ_IBASELINE_SHIFT_IF_UNSET(&style->baseline_shift, val);
963             break;
964             /* Text (unimplemented) */
965         case SP_PROP_TEXT_RENDERING: {
966             /* Ignore the hint. */
967             SPIEnum dummy;
968             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
969             break;
970         }
971         case SP_PROP_ALIGNMENT_BASELINE:
972             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
973             break;
974         case SP_PROP_DOMINANT_BASELINE:
975             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
976             break;
977         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
978             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
979             break;
980         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
981             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
982             break;
983         case SP_PROP_KERNING:
984             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
985             break;
986             /* Misc */
987         case SP_PROP_CLIP:
988             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
989             break;
990         case SP_PROP_COLOR:
991             if (!style->color.set) {
992                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
993             }
994             break;
995         case SP_PROP_CURSOR:
996             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
997             break;
998         case SP_PROP_DISPLAY:
999             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
1000             break;
1001         case SP_PROP_OVERFLOW:
1002             /** \todo
1003              * FIXME: not supported properly yet, we just read and write it,
1004              * but act as if it is always "display".
1005              */
1006             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
1007             break;
1008         case SP_PROP_VISIBILITY:
1009             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
1010             break;
1011             /* SVG */
1012             /* Clip/Mask */
1013         case SP_PROP_CLIP_PATH:
1014             /** \todo
1015              * This is a workaround. Inkscape only supports 'clip-path' as SVG attribute, not as
1016              * style property. By having both CSS and SVG attribute set, editing of clip-path
1017              * will fail, since CSS always overwrites SVG attributes.
1018              * Fixes Bug #324849
1019              */
1020             g_warning("attribute 'clip-path' given as CSS");
1021             style->object->repr->setAttribute("clip-path", val);
1022             break;
1023         case SP_PROP_CLIP_RULE:
1024             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
1025             break;
1026         case SP_PROP_MASK:
1027             /** \todo
1028              * See comment for SP_PROP_CLIP_PATH
1029              */
1030             g_warning("attribute 'mask' given as CSS");
1031             style->object->repr->setAttribute("mask", val);
1032             break;
1033         case SP_PROP_OPACITY:
1034             if (!style->opacity.set) {
1035                 sp_style_read_iscale24(&style->opacity, val);
1036             }
1037             break;
1038         case SP_PROP_ENABLE_BACKGROUND:
1039             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
1040                                     enum_enable_background, true);
1041             break;
1042             /* Filter */
1043         case SP_PROP_FILTER:
1044             if (!style->filter.set && !style->filter.inherit) {
1045                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1046             }
1047             break;
1048         case SP_PROP_FLOOD_COLOR:
1049             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
1050             break;
1051         case SP_PROP_FLOOD_OPACITY:
1052             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
1053             break;
1054         case SP_PROP_LIGHTING_COLOR:
1055             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
1056             break;
1057             /* Gradient */
1058         case SP_PROP_STOP_COLOR:
1059             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
1060             break;
1061         case SP_PROP_STOP_OPACITY:
1062             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
1063             break;
1064             /* Interactivity */
1065         case SP_PROP_POINTER_EVENTS:
1066             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
1067             break;
1068             /* Paint */
1069         case SP_PROP_COLOR_INTERPOLATION:
1070             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1071             break;
1072         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1073             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1074             break;
1075         case SP_PROP_COLOR_PROFILE:
1076             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1077             break;
1078         case SP_PROP_COLOR_RENDERING: {
1079             /* Ignore the hint. */
1080             SPIEnum dummy;
1081             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1082             break;
1083         }
1084         case SP_PROP_FILL:
1085             if (!style->fill.set) {
1086                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1087             }
1088             break;
1089         case SP_PROP_FILL_OPACITY:
1090             if (!style->fill_opacity.set) {
1091                 sp_style_read_iscale24(&style->fill_opacity, val);
1092             }
1093             break;
1094         case SP_PROP_FILL_RULE:
1095             if (!style->fill_rule.set) {
1096                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1097             }
1098             break;
1099         case SP_PROP_IMAGE_RENDERING: {
1100             /* Ignore the hint. */
1101             SPIEnum dummy;
1102             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1103             break;
1104         }
1105         case SP_PROP_MARKER:
1106             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1107             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1108             if (!style->marker[SP_MARKER_LOC].set) {
1109                 g_free(style->marker[SP_MARKER_LOC].value);
1110                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1111                 style->marker[SP_MARKER_LOC].set = TRUE;
1112                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1113             }
1114             break;
1116         case SP_PROP_MARKER_START:
1117             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1118             if (!style->marker[SP_MARKER_LOC_START].set) {
1119                 g_free(style->marker[SP_MARKER_LOC_START].value);
1120                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1121                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1122                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1123             }
1124             break;
1125         case SP_PROP_MARKER_MID:
1126             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1127             if (!style->marker[SP_MARKER_LOC_MID].set) {
1128                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1129                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1130                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1131                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1132             }
1133             break;
1134         case SP_PROP_MARKER_END:
1135             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1136             if (!style->marker[SP_MARKER_LOC_END].set) {
1137                 g_free(style->marker[SP_MARKER_LOC_END].value);
1138                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1139                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1140                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1141             }
1142             break;
1144         case SP_PROP_SHAPE_RENDERING: {
1145             /* Ignore the hint. */
1146             SPIEnum dummy;
1147             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1148             break;
1149         }
1151         case SP_PROP_STROKE:
1152             if (!style->stroke.set) {
1153                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1154             }
1155             break;
1156         case SP_PROP_STROKE_WIDTH:
1157             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1158             break;
1159         case SP_PROP_STROKE_DASHARRAY:
1160             if (!style->stroke_dasharray_set) {
1161                 sp_style_read_dash(style, val);
1162             }
1163             break;
1164         case SP_PROP_STROKE_DASHOFFSET:
1165             if (!style->stroke_dashoffset_set) {
1166                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1167                     style->stroke_dashoffset_set = TRUE;
1168                 } else if (val && !strcmp(val, "inherit")) {
1169                     style->stroke_dashoffset_set = TRUE;
1170                     style->stroke_dashoffset_inherit = TRUE;
1171                 } else {
1172                     style->stroke_dashoffset_set = FALSE;
1173                 }
1174             }
1175             break;
1176         case SP_PROP_STROKE_LINECAP:
1177             if (!style->stroke_linecap.set) {
1178                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1179             }
1180             break;
1181         case SP_PROP_STROKE_LINEJOIN:
1182             if (!style->stroke_linejoin.set) {
1183                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1184             }
1185             break;
1186         case SP_PROP_STROKE_MITERLIMIT:
1187             if (!style->stroke_miterlimit.set) {
1188                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1189             }
1190             break;
1191         case SP_PROP_STROKE_OPACITY:
1192             if (!style->stroke_opacity.set) {
1193                 sp_style_read_iscale24(&style->stroke_opacity, val);
1194             }
1195             break;
1197         default:
1198             g_warning("Invalid style property id: %d value: %s", id, val);
1199             break;
1200     }
1203 static void
1204 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1206     /** \todo Ensure that property is lcased, as per
1207      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1208      * Should probably be done in libcroco.
1209      */
1210     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1211     if (prop_idx != SP_ATTR_INVALID) {
1212         /** \todo
1213          * effic: Test whether the property is already set before trying to
1214          * convert to string. Alternatively, set from CRTerm directly rather
1215          * than converting to string.
1216          */
1217         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1218         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1219         sp_style_merge_property(style, prop_idx, str_value);
1220         g_free(str_value);
1221     }
1224 static void
1225 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1227 #if 0 /* forwards */
1228     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1229         CRDeclaration *decl = NULL;
1230         cr_prop_list_get_decl(cur, &decl);
1231         sp_style_merge_style_from_decl(style, decl);
1232     }
1233 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1234     if (props) {
1235         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1236         CRDeclaration *decl = NULL;
1237         cr_prop_list_get_decl(props, &decl);
1238         sp_style_merge_style_from_decl(style, decl);
1239     }
1240 #endif
1243 /**
1244  * \pre decl_list != NULL
1245  */
1246 static void
1247 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1249     // read the decls from end to start, using head recursion, so that latter declarations override
1250     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1251     // because sp_style_merge_style_from_decl only sets properties that are unset
1252     if (decl_list->next) {
1253         sp_style_merge_from_decl_list(style, decl_list->next);
1254     }
1255     sp_style_merge_style_from_decl(style, decl_list);
1258 static CRSelEng *
1259 sp_repr_sel_eng()
1261     CRSelEng *const ret = cr_sel_eng_new();
1262     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1264     /** \todo
1265      * Check whether we need to register any pseudo-class handlers.
1266      * libcroco has its own default handlers for first-child and lang.
1267      *
1268      * We probably want handlers for link and arguably visited (though
1269      * inkscape can't visit links at the time of writing).  hover etc.
1270      * more useful in inkview than the editor inkscape.
1271      *
1272      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1273      * the following should be honoured, at least by inkview:
1274      * :hover, :active, :focus, :visited, :link.
1275      */
1277     g_assert(ret);
1278     return ret;
1281 static void
1282 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1284     static CRSelEng *sel_eng = NULL;
1285     if (!sel_eng) {
1286         sel_eng = sp_repr_sel_eng();
1287     }
1289     CRPropList *props = NULL;
1290     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1291                                                                      object->document->style_cascade,
1292                                                                      object->repr,
1293                                                                      &props);
1294     g_return_if_fail(status == CR_OK);
1295     /// \todo Check what errors can occur, and handle them properly.
1296     if (props) {
1297         sp_style_merge_from_props(style, props);
1298         cr_prop_list_destroy(props);
1299     }
1302 /**
1303  * Parses a style="..." string and merges it with an existing SPStyle.
1304  */
1305 void
1306 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1308     /*
1309      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1310      * ``When CSS styling is used, CSS inline style is specified by including
1311      * semicolon-separated property declarations of the form "name : value"
1312      * within the style attribute''.
1313      *
1314      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1315      * Why does it say "including", what else is allowed in the style
1316      * attribute value?
1317      */
1319     CRDeclaration *const decl_list
1320         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1321     if (decl_list) {
1322         sp_style_merge_from_decl_list(style, decl_list);
1323         cr_declaration_destroy(decl_list);
1324     }
1327 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1328 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1330 static void
1331 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1333     /* 'font-size' */
1334     if (!child.set || child.inherit) {
1335         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1336         child.computed = parent.computed;
1337     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1338         /** \todo
1339          * fixme: SVG and CSS do not specify clearly, whether we should use
1340          * user or screen coordinates (Lauris)
1341          */
1342         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1343             child.computed = font_size_table[child.value];
1344         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1345             child.computed = parent.computed / 1.2;
1346         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1347             child.computed = parent.computed * 1.2;
1348         } else {
1349             /* Illegal value */
1350         }
1351     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1352         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1353          * rather than viewport. */
1354         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1355     }
1358 // Some shifts are defined relative to parent.
1359 static void
1360 sp_style_merge_baseline_shift_from_parent(SPIBaselineShift &child, SPIBaselineShift const &parent,
1361                                           SPIFontSize const &pfont_size)
1363     /* 'baseline-shift' */
1364     if (!child.set || child.inherit) {
1365         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1366         child.computed = parent.computed;  // Does this make sense (applying a shift a second time)?
1367     } else if (child.type == SP_BASELINE_SHIFT_LITERAL) {
1368         if( child.literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
1369             child.computed = 0; // No change
1370         } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUB ) {
1371             // Should use subscript position from font relative to alphabetic baseline
1372             // OpenOffice, Adobe: -0.33, Word -0.14, LaTex about -0.2.
1373             child.computed = -0.2 * pfont_size.computed; 
1374         } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUPER ) {
1375             // Should use superscript position from font relative to alphabetic baseline
1376             // OpenOffice, Adobe: 0.33, Word 0.35, LaTex about 0.45.
1377             child.computed =  0.4 * pfont_size.computed; 
1378         } else {
1379             /* Illegal value */
1380         }
1381     } else if (child.type == SP_BASELINE_SHIFT_PERCENTAGE) {
1382         // Percentage for baseline shift is relative to computed "line-height"
1383         // which is just font-size (see SVG1.1 'font').
1384         child.computed = pfont_size.computed * child.value;
1385     } else if (child.type == SP_BASELINE_SHIFT_LENGTH) {
1386         switch (child.unit) {
1387             case SP_CSS_UNIT_EM:
1388                 child.computed = child.value * pfont_size.computed; 
1389                 break;
1390             case SP_CSS_UNIT_EX:
1391                 child.computed = child.value * 0.5 * pfont_size.computed; 
1392                 break;
1393             default:
1394                 /* No change */
1395                 break;
1396         }
1397     }
1398     // baseline-shifts are relative to parent baseline
1399     child.computed += parent.computed;
1402 /**
1403  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1404  * calculating from) corresponding computed values of \a parent.
1405  *
1406  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1407  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1408  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1409  * about what the computed value is (not obvious for lengths).
1410  *
1411  * \pre \a parent's computed values are already up-to-date.
1412  */
1413 void
1414 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1416     g_return_if_fail(style != NULL);
1418     /** \todo
1419      * fixme: Check for existing callers that might pass null parent.
1420      * This should probably be g_return_if_fail, or else we should make a
1421      * best attempt to set computed values correctly without having a parent
1422      * (i.e., by assuming parent has initial values).
1423      */
1424     if (!parent)
1425         return;
1427     /* CSS2 */
1428     /* Font */
1429     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1431     /* 'font-style' */
1432     if (!style->font_style.set || style->font_style.inherit) {
1433         style->font_style.computed = parent->font_style.computed;
1434     }
1436     /* 'font-variant' */
1437     if (!style->font_variant.set || style->font_variant.inherit) {
1438         style->font_variant.computed = parent->font_variant.computed;
1439     }
1441     /* 'font-weight' */
1442     if (!style->font_weight.set || style->font_weight.inherit) {
1443         style->font_weight.computed = parent->font_weight.computed;
1444     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1445         /** \todo
1446          * fixme: This is unconditional, i.e., happens even if parent not
1447          * present.
1448          */
1449         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1450     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1451         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1452     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1453         unsigned const parent_val = parent->font_weight.computed;
1454         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1455         // strictly, 'bolder' and 'lighter' should go to the next weight
1456         // expressible in the current font family, but that's difficult to
1457         // find out, so jumping by 3 seems an appropriate approximation
1458         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1459                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1460                                        : parent_val - 3);
1461         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1462     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1463         unsigned const parent_val = parent->font_weight.computed;
1464         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1465         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1466                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1467                                        : parent_val + 3);
1468         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1469     }
1471     /* 'font-stretch' */
1472     if (!style->font_stretch.set || style->font_stretch.inherit) {
1473         style->font_stretch.computed = parent->font_stretch.computed;
1474     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1475         unsigned const parent_val = parent->font_stretch.computed;
1476         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1477                                         ? parent_val
1478                                         : parent_val - 1);
1479         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1480     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1481         unsigned const parent_val = parent->font_stretch.computed;
1482         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1483         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1484                                         ? parent_val
1485                                         : parent_val + 1);
1486         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1487     }
1489     /* text (css2) */
1490     if (!style->text_indent.set || style->text_indent.inherit) {
1491         style->text_indent.computed = parent->text_indent.computed;
1492     }
1494     if (!style->text_align.set || style->text_align.inherit) {
1495         style->text_align.computed = parent->text_align.computed;
1496     }
1498     if (!style->text_decoration.set || style->text_decoration.inherit) {
1499         style->text_decoration.underline = parent->text_decoration.underline;
1500         style->text_decoration.overline = parent->text_decoration.overline;
1501         style->text_decoration.line_through = parent->text_decoration.line_through;
1502         style->text_decoration.blink = parent->text_decoration.blink;
1503     }
1505     if (!style->line_height.set || style->line_height.inherit) {
1506         style->line_height.value = parent->line_height.value;
1507         style->line_height.computed = parent->line_height.computed;
1508         style->line_height.normal = parent->line_height.normal;
1509     }
1511     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1512         style->letter_spacing.value = parent->letter_spacing.value;
1513         style->letter_spacing.computed = parent->letter_spacing.computed;
1514         style->letter_spacing.normal = parent->letter_spacing.normal;
1515     }
1517     if (!style->word_spacing.set || style->word_spacing.inherit) {
1518         style->word_spacing.value = parent->word_spacing.value;
1519         style->word_spacing.computed = parent->word_spacing.computed;
1520         style->word_spacing.normal = parent->word_spacing.normal;
1521     }
1523     if (!style->text_transform.set || style->text_transform.inherit) {
1524         style->text_transform.computed = parent->text_transform.computed;
1525     }
1527     if (!style->direction.set || style->direction.inherit) {
1528         style->direction.computed = parent->direction.computed;
1529     }
1531     if (!style->block_progression.set || style->block_progression.inherit) {
1532         style->block_progression.computed = parent->block_progression.computed;
1533     }
1535     if (!style->writing_mode.set || style->writing_mode.inherit) {
1536         style->writing_mode.computed = parent->writing_mode.computed;
1537     }
1539     if (!style->text_anchor.set || style->text_anchor.inherit) {
1540         style->text_anchor.computed = parent->text_anchor.computed;
1541     }
1543     /* Baseline Shift... Some shifts are relative to parent. */
1544     sp_style_merge_baseline_shift_from_parent(style->baseline_shift, parent->baseline_shift,
1545                                               parent->font_size);
1547     if (style->opacity.inherit) {
1548         style->opacity.value = parent->opacity.value;
1549     }
1551     /* Color */
1552     if (!style->color.set || style->color.inherit) {
1553         sp_style_merge_ipaint(style, &style->color, &parent->color);
1554     }
1556     /* Fill */
1557     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1558         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1559     }
1561     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1562         style->fill_opacity.value = parent->fill_opacity.value;
1563     }
1565     if (!style->fill_rule.set || style->fill_rule.inherit) {
1566         style->fill_rule.computed = parent->fill_rule.computed;
1567     }
1569     /* Stroke */
1570     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1571         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1572     }
1574     if (!style->stroke_width.set || style->stroke_width.inherit) {
1575         style->stroke_width.computed = parent->stroke_width.computed;
1576     } else {
1577         /* Update computed value for any change in font inherited from parent. */
1578         double const em = style->font_size.computed;
1579         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1580             style->stroke_width.computed = style->stroke_width.value * em;
1581         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1582             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1583             style->stroke_width.computed = style->stroke_width.value * ex;
1584         }
1585     }
1587     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1588         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1589     }
1591     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1592         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1593     }
1595     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1596         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1597     }
1599     if (!style->stroke_dasharray_set || style->stroke_dasharray_inherit) {
1600         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1601         if (style->stroke_dash.n_dash > 0) {
1602             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1603             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1604         }
1605     }
1607     if (!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) {
1608         style->stroke_dash.offset = parent->stroke_dash.offset;
1609     }
1611     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1612         style->stroke_opacity.value = parent->stroke_opacity.value;
1613     }
1615     if (style->text && parent->text) {
1616         if (!style->text->font_family.set || style->text->font_family.inherit) {
1617             g_free(style->text->font_family.value);
1618             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1619         }
1620     }
1622     if (style->text && parent->text) {
1623         if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1624             g_free(style->text->font_specification.value);
1625             style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1626         }
1627     }
1629     /* Markers - Free the old value and make copy of the new */
1630     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1631         if (!style->marker[i].set || style->marker[i].inherit) {
1632             g_free(style->marker[i].value);
1633             style->marker[i].value = g_strdup(parent->marker[i].value);
1634         }
1635     }
1637     /* Filter effects */
1638     if (style->filter.inherit) {
1639         sp_style_merge_ifilter(style, &parent->filter);
1640     }
1642     if(style->enable_background.inherit) {
1643         style->enable_background.value = parent->enable_background.value;
1644     }
1647 template <typename T>
1648 static void
1649 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1651     if ( ( !(child.set) || child.inherit )
1652          && parent.set && !(parent.inherit) )
1653     {
1654         child = parent;
1655     }
1658 /**
1659  * Copy SPIString from parent to child.
1660  */
1661 static void
1662 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1664     if ( ( !(child.set) || child.inherit )
1665          && parent.set && !(parent.inherit) )
1666     {
1667         g_free(child.value);
1668         child.value = g_strdup(parent.value);
1669         child.set = parent.set;
1670         child.inherit = parent.inherit;
1671     }
1674 static void
1675 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1676                                             SPIPaint &child, SPIPaint const &parent)
1678     /** \todo
1679      * I haven't given this much attention.  See comments below about
1680      * currentColor, colorProfile, and relative URIs.
1681      */
1682     if (!child.set || child.inherit) {
1683         sp_style_merge_ipaint(style, &child, &parent);
1684         child.set = parent.set;
1685         child.inherit = parent.inherit;
1686     }
1689 static void
1690 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1691                                                unsigned const max_computed_val,
1692                                                unsigned const smaller_val)
1694     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1695        then zero or more other absolute values, then smaller_val then larger_val. */
1696     unsigned const min_computed_val = 0;
1697     unsigned const larger_val = smaller_val + 1;
1698     g_return_if_fail(min_computed_val < max_computed_val);
1699     g_return_if_fail(max_computed_val < smaller_val);
1701     if (parent.set && !parent.inherit) {
1702         if (!child.set || child.inherit) {
1703             child.value = parent.value;
1704             child.set = parent.set;  // i.e. true
1705             child.inherit = parent.inherit;  // i.e. false
1706         } else if (child.value < smaller_val) {
1707             /* Child has absolute value: leave as is. */
1708         } else if ( ( child.value == smaller_val
1709                       && parent.value == larger_val )
1710                     || ( parent.value == smaller_val
1711                          && child.value == larger_val ) )
1712         {
1713             child.set = false;
1714             /*
1715              * Note that this can result in a change in computed value in the
1716              * rare case that the parent's setting was a no-op (i.e. if the
1717              * parent's parent's computed value was already ultra-condensed or
1718              * ultra-expanded).  However, I'd guess that the change is for the
1719              * better: I'd guess that if the properties were specified
1720              * relatively, then the intent is to counteract parent's effect.
1721              * I.e. I believe this is the best code even in that case.
1722              */
1723         } else if (child.value == parent.value) {
1724             /* Leave as is. */
1725             /** \todo
1726              * It's unclear what to do if style and parent specify the same
1727              * relative directive (narrower or wider).  We can either convert
1728              * to absolute specification or coalesce to a single relative
1729              * request (of half the strength of the original pair).
1730              *
1731              * Converting to a single level of relative specification is a
1732              * better choice if the newly-unlinked clone is itself cloned to
1733              * other contexts (inheriting different font stretchiness): it
1734              * would preserve the effect that it will be narrower than
1735              * the inherited context in each case.  The disadvantage is that
1736              * it will ~certainly affect the computed value of the
1737              * newly-unlinked clone.
1738              */
1739         } else {
1740             unsigned const parent_val = parent.computed;
1741             child.value = ( child.value == smaller_val
1742                             ? ( parent_val == min_computed_val
1743                                 ? parent_val
1744                                 : parent_val - 1 )
1745                             : ( parent_val == max_computed_val
1746                                 ? parent_val
1747                                 : parent_val + 1 ) );
1748             g_assert(child.value <= max_computed_val);
1749             child.inherit = false;
1750             g_assert(child.set);
1751         }
1752     }
1755 template <typename LengthT>
1756 static void
1757 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1758                                              double const parent_child_em_ratio)
1760     if ( ( !(child.set) || child.inherit )
1761          && parent.set && !(parent.inherit) )
1762     {
1763         child = parent;
1764         switch (parent.unit) {
1765             case SP_CSS_UNIT_EM:
1766             case SP_CSS_UNIT_EX:
1767                 child.value *= parent_child_em_ratio;
1768                 /** \todo
1769                  * fixme: Have separate ex ratio parameter.
1770                  * Get x height from libnrtype or pango.
1771                  */
1772                 if (!IS_FINITE(child.value)) {
1773                     child.value = child.computed;
1774                     child.unit = SP_CSS_UNIT_NONE;
1775                 }
1776                 break;
1778             default:
1779                 break;
1780         }
1781     }
1784 static double
1785 get_relative_font_size_frac(SPIFontSize const &font_size)
1787     switch (font_size.type) {
1788         case SP_FONT_SIZE_LITERAL: {
1789             switch (font_size.value) {
1790                 case SP_CSS_FONT_SIZE_SMALLER:
1791                     return 5.0 / 6.0;
1793                 case SP_CSS_FONT_SIZE_LARGER:
1794                     return 6.0 / 5.0;
1796                 default:
1797                     g_assert_not_reached();
1798             }
1799         }
1801         case SP_FONT_SIZE_PERCENTAGE:
1802             return SP_F8_16_TO_FLOAT(font_size.value);
1804         case SP_FONT_SIZE_LENGTH:
1805             g_assert_not_reached();
1806     }
1807     g_assert_not_reached();
1810 /**
1811  * Combine \a style and \a parent style specifications into a single style specification that
1812  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1813  *
1814  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1815  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1816  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1817  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1818  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1819  *
1820  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1821  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1822  * style was previously up-to-date wrt \a parent.
1823  *
1824  * \pre \a parent's computed values are already up-to-date.
1825  *   (\a style's computed values needn't be up-to-date.)
1826  */
1827 void
1828 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1830     /** \note
1831      * The general rule for each property is as follows:
1832      *
1833      *   If style is set to an absolute value, then leave it as is.
1834      *
1835      *   Otherwise (i.e. if style has a relative value):
1836      *
1837      *     If parent is set to an absolute value, then set style to the computed value.
1838      *
1839      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1840      */
1842     /* We do font-size first, to ensure that em size is up-to-date. */
1843     /** \todo
1844      * fixme: We'll need to have more font-related things up the top once
1845      * we're getting x-height from pango or libnrtype.
1846      */
1848     /* Some things that allow relative specifications. */
1849     {
1850         /* font-size.  Note that we update the computed font-size of style,
1851            to assist in em calculations later in this function. */
1852         if (parent->font_size.set && !parent->font_size.inherit) {
1853             if (!style->font_size.set || style->font_size.inherit) {
1854                 /* font_size inherits the computed value, so we can use the parent value
1855                  * verbatim. */
1856                 style->font_size = parent->font_size;
1857             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1858                 /* Child already has absolute size (stored in computed value), so do nothing. */
1859             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1860                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1861                 /* Child already has absolute size, but we ensure that the computed value
1862                    is up-to-date. */
1863                 unsigned const ix = style->font_size.value;
1864                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1865                 style->font_size.computed = font_size_table[ix];
1866             } else {
1867                 /* Child has relative size. */
1868                 double const child_frac(get_relative_font_size_frac(style->font_size));
1869                 style->font_size.set = true;
1870                 style->font_size.inherit = false;
1871                 style->font_size.computed = parent->font_size.computed * child_frac;
1873                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1874                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1875                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1876                 {
1877                     /* Absolute value. */
1878                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1879                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1880                 } else {
1881                     /* Relative value. */
1882                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1883                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1884                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1885                 }
1886             }
1887         }
1889         /* 'font-stretch' */
1890         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1891                                                        parent->font_stretch,
1892                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1893                                                        SP_CSS_FONT_STRETCH_NARROWER);
1895         /* font-weight */
1896         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1897                                                        parent->font_weight,
1898                                                        SP_CSS_FONT_WEIGHT_900,
1899                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1900     }
1903     /* Enum values that don't have any relative settings (other than `inherit'). */
1904     {
1905         SPIEnum SPStyle::*const fields[] = {
1906             //nyi: SPStyle::clip_rule,
1907             //nyi: SPStyle::color_interpolation,
1908             //nyi: SPStyle::color_interpolation_filters,
1909             //nyi: SPStyle::color_rendering,
1910             &SPStyle::direction,
1911             &SPStyle::fill_rule,
1912             &SPStyle::font_style,
1913             &SPStyle::font_variant,
1914             //nyi: SPStyle::image_rendering,
1915             //nyi: SPStyle::pointer_events,
1916             //nyi: SPStyle::shape_rendering,
1917             &SPStyle::stroke_linecap,
1918             &SPStyle::stroke_linejoin,
1919             &SPStyle::text_anchor,
1920             //nyi: &SPStyle::text_rendering,
1921             &SPStyle::visibility,
1922             &SPStyle::writing_mode
1923         };
1925         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1926             SPIEnum SPStyle::*const fld = fields[i];
1927             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1928         }
1929     }
1931     /* A few other simple inheritance properties. */
1932     {
1933         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1934         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1935         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1937         /** \todo
1938          * We currently treat text-decoration as if it were a simple inherited
1939          * property (fixme). This code may need changing once we do the
1940          * special fill/stroke inheritance mentioned by the spec.
1941          */
1942         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1943                                                                  parent->text_decoration);
1945         //nyi: font-size-adjust,  // <number> | none | inherit
1946         //nyi: glyph-orientation-horizontal,
1947         //nyi: glyph-orientation-vertical,
1948     }
1950     /* Properties that involve length but are easy in other respects. */
1951     {
1952         /* The difficulty with lengths is that font-relative units need adjusting if the font
1953          * varies between parent & child.
1954          *
1955          * Lengths specified in the existing child can stay as they are: its computed font
1956          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1957          * size.
1958          *
1959          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1960          * ratio of em or ex size between parent & child.
1961          */
1962         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1964         SPILength SPStyle::*const lfields[] = {
1965             &SPStyle::stroke_width,
1966             &SPStyle::text_indent
1967         };
1968         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1969             SPILength SPStyle::*fld = lfields[i];
1970             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1971                                                                     parent->*fld,
1972                                                                     parent_child_em_ratio);
1973         }
1975         SPILengthOrNormal SPStyle::*const nfields[] = {
1976             &SPStyle::letter_spacing,
1977             &SPStyle::line_height,
1978             &SPStyle::word_spacing
1979         };
1980         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1981             SPILengthOrNormal SPStyle::*fld = nfields[i];
1982             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1983                                                                             parent->*fld,
1984                                                                             parent_child_em_ratio);
1985         }
1987         //nyi: &SPStyle::kerning: length or `auto'
1989         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1990            can accept units. */
1991     }
1993     /* Properties that involve a URI but are easy in other respects. */
1994     {
1995         /** \todo
1996          * Could cause problems if original object was in another document
1997          * and it used a relative URL.  (At the time of writing, we don't
1998          * allow hrefs to other documents, so this isn't a problem yet.)
1999          * Paint properties also allow URIs.
2000          */
2001         //nyi: cursor,   // may involve change in effect, but we can't do much better
2002         //nyi: color-profile,
2004         // Markers (marker-start, marker-mid, marker-end).
2005         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2006             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
2007         }
2008     }
2010     /* CSS2 */
2011     /* Font */
2013     if (style->text && parent->text) {
2014         sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
2015                                                      parent->text->font_specification);
2017         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
2018                                                      parent->text->font_family);
2019     }
2022     /* Properties that don't inherit by default.  Most of these need special handling. */
2023     {
2024         /*
2025          * opacity's effect is cumulative; we set the new value to the combined effect.  The
2026          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
2027          * fill-opacity are quite different from opacity, and don't need any special handling.)
2028          *
2029          * Cases:
2030          * - parent & child were each previously unset, in which case the effective
2031          *   opacity value is 1.0, and style should remain unset.
2032          * - parent was previously unset (so computed opacity value of 1.0)
2033          *   and child was set to inherit.  The merged child should
2034          *   get a value of 1.0, and shouldn't inherit (lest the new parent
2035          *   has a different opacity value).  Given that opacity's default
2036          *   value is 1.0 (rather than inherit), we might as well have the
2037          *   merged child's opacity be unset.
2038          * - parent was previously unset (so opacity 1.0), and child was set to a number.
2039          *   The merged child should retain its existing settings (though it doesn't matter
2040          *   if we make it unset if that number was 1.0).
2041          * - parent was inherit and child was unset.  Merged child should be set to inherit.
2042          * - parent was inherit and child was inherit.  (We can't in general reproduce this
2043          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
2044          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
2045          *   child's value should be set to the product of the two, i.e. the square of the
2046          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
2047          *   is more important to retain the effective opacity than to retain the inheriting
2048          *   effect, and assumes that the inheriting effect either isn't important enough to create
2049          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
2050          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
2051          *   closer to maintaining the effect.
2052          * - parent was inherit and child was set to a numerical value.  If the child's value
2053          *   was 1.0, then the merged child should have the same settings as the parent.
2054          *   If the child's value was 0, then the merged child should also be set to 0.
2055          *   If the child's value was anything else, then we do the same as for the inherit/inherit
2056          *   case above: have the merged child set to the product of the two opacities and not
2057          *   marked as inherit, for the same reasons as for that case.
2058          * - parent was set to a value, and child was unset.  The merged child should have
2059          *   parent's settings.
2060          * - parent was set to a value, and child was inherit.  The merged child should
2061          *   be set to the product, i.e. the square of the parent's value.
2062          * - parent & child are each set to a value.  The merged child should be set to the
2063          *   product.
2064          */
2065         if ( !style->opacity.set
2066              || ( !style->opacity.inherit
2067                   && style->opacity.value == SP_SCALE24_MAX ) )
2068         {
2069             style->opacity = parent->opacity;
2070         } else {
2071             /* Ensure that style's computed value is up-to-date. */
2072             if (style->opacity.inherit) {
2073                 style->opacity.value = parent->opacity.value;
2074             }
2076             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
2077             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
2078                                                   parent->opacity.value);
2080             style->opacity.inherit = (parent->opacity.inherit
2081                                       && style->opacity.inherit
2082                                       && (parent->opacity.value == 0 ||
2083                                           parent->opacity.value == SP_SCALE24_MAX));
2084             style->opacity.set = ( style->opacity.inherit
2085                                    || style->opacity.value < SP_SCALE24_MAX );
2086         }
2088         /* display is in principle similar to opacity, but implementation is easier. */
2089         if ( parent->display.set && !parent->display.inherit
2090              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
2091             style->display.value = SP_CSS_DISPLAY_NONE;
2092             style->display.set = true;
2093             style->display.inherit = false;
2094         } else if (style->display.inherit) {
2095             style->display.value = parent->display.value;
2096             style->display.set = parent->display.set;
2097             style->display.inherit = parent->display.inherit;
2098         } else {
2099             /* Leave as is.  (display doesn't inherit by default.) */
2100         }
2102         /* enable-background - this is rather complicated, because
2103          * it is valid only when applied to container elements.
2104          * Let's check a simple case anyhow. */
2105         if (parent->enable_background.set
2106             && !parent->enable_background.inherit
2107             && style->enable_background.inherit)
2108         {
2109             style->enable_background.set = true;
2110             style->enable_background.inherit = false;
2111             style->enable_background.value = parent->enable_background.value;
2112         }
2114         if (!style->filter.set || style->filter.inherit)
2115         {
2116             sp_style_merge_ifilter(style, &parent->filter);
2117         }
2119         /** \todo
2120          * fixme: Check that we correctly handle all properties that don't
2121          * inherit by default (as shown in
2122          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2123          */
2124     }
2126     /* SPIPaint properties (including color). */
2127     {
2128         /** \todo
2129          * Think about the issues involved if specified as currentColor or
2130          * if specified relative to colorProfile, and if the currentColor or
2131          * colorProfile differs between parent \& child.  See also comments
2132          * elsewhere in this function about URIs.
2133          */
2134         SPIPaint SPStyle::*const fields[] = {
2135             &SPStyle::color,
2136             &SPStyle::fill,
2137             &SPStyle::stroke
2138         };
2139         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2140             SPIPaint SPStyle::*const fld = fields[i];
2141             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2142         }
2143     }
2145     /* Things from SVG 1.2 or CSS3. */
2146     {
2147         /* Note: If we ever support setting string values for text-align then we'd need strdup
2148          * handling here. */
2149         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2151         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2152         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2153     }
2155     /* Note: this will need length handling once dasharray supports units. */
2156     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2157          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2158     {
2159         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2160         if (style->stroke_dash.n_dash > 0) {
2161             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2162             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2163         }
2164         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2165         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2166     }
2168     /* Note: this will need length handling once dasharray_offset supports units. */
2169     if ((!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) && parent->stroke_dashoffset_set && !parent->stroke_dashoffset_inherit) {
2170         style->stroke_dash.offset = parent->stroke_dash.offset;
2171         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2172         style->stroke_dashoffset_inherit = parent->stroke_dashoffset_inherit;
2173         /* TODO: Try to
2174          * represent it as a normal SPILength; though will need to do something about existing
2175          * users of stroke_dash.offset and stroke_dashoffset_set. */
2176     }
2180 static void
2181 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2183     // it may be that this style's SPIPaint has not yet created its URIReference;
2184     // now that we have a document, we can create it here
2185     if (!paint->value.href && document) {
2186         paint->value.href = new SPPaintServerReference(document);
2187         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));
2188     }
2190     if (paint->value.href && paint->value.href->getObject())
2191         paint->value.href->detach();
2193     if (paint->value.href) {
2194         try {
2195             paint->value.href->attach(*uri);
2196         } catch (Inkscape::BadURIException &e) {
2197             g_warning("%s", e.what());
2198             paint->value.href->detach();
2199         }
2200     }
2203 static void
2204 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2206     try {
2207         const Inkscape::URI IURI(uri);
2208         sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2209     } catch (...) {
2210         g_warning("URI failed to parse: %s", uri);
2211     }
2214 void
2215 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2217     sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2220 /**
2221  *
2222  */
2223 static void
2224 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2226     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2227         bool isset = paint->set;
2228         paint->clear();
2229         paint->set = isset;
2230         paint->currentcolor = TRUE;
2231         paint->setColor(style->color.value.color);
2232         return;
2233     }
2235     paint->clear();
2236     if ( parent->isPaintserver() ) {
2237         if (parent->value.href) {
2238             sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2239         } else {
2240             g_warning("Expected paint server not found.");
2241         }
2242     } else if ( parent->isColor() ) {
2243         paint->setColor( parent->value.color );
2244     } else if ( parent->isNoneSet() ) {
2245         paint->noneSet = TRUE;
2246     } else if ( parent->isNone() ) {
2247         //
2248     } else {
2249         g_assert_not_reached();
2250     }
2254 /**
2255  * Merge filter style from parent.
2256  * Filter effects do not inherit by default
2257  */
2258 static void
2259 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2261     // FIXME:
2262     // instead of just copying over, we need to _really merge_ the two filters by combining their
2263     // filter primitives
2265     sp_style_filter_clear(style);
2266     style->filter.set = parent->set;
2267     style->filter.inherit = parent->inherit;
2269     if (style->filter.href && style->filter.href->getObject())
2270        style->filter.href->detach();
2272     // it may be that this style has not yet created its SPFilterReference
2273     if (!style->filter.href && style->object && SP_OBJECT_DOCUMENT(style->object)) {
2274             style->filter.href = new SPFilterReference(SP_OBJECT_DOCUMENT(style->object));
2275             style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2276     }
2278     if (style->filter.href && parent->href && parent->href->getObject()) {
2279         try {
2280             style->filter.href->attach(*parent->href->getURI());
2281         } catch (Inkscape::BadURIException &e) {
2282             g_warning("%s", e.what());
2283             style->filter.href->detach();
2284         }
2285     }
2288 /**
2289  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2290  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2291  * complete cascaded style to style_clipboard. When you need a CSS string
2292  * for an object in the document tree, you normally call
2293  * sp_style_write_difference instead to take into account the object's parent.
2294  *
2295  * \pre style != NULL.
2296  * \pre flags in {IFSET, ALWAYS}.
2297  * \post ret != NULL.
2298  */
2299 gchar *
2300 sp_style_write_string(SPStyle const *const style, guint const flags)
2302     /** \todo
2303      * Merge with write_difference, much duplicate code!
2304      */
2305     g_return_val_if_fail(style != NULL, NULL);
2306     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2307                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2308                          NULL);
2310     gchar c[BMAX];
2311     gchar *p = c;
2312     *p = '\0';
2314     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2315     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2316     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2317     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2318     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2320     /* Text */
2321     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2322     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2323     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2324     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2325     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2326     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2327     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2328     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2329     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2330     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2332     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2333     p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &style->baseline_shift, NULL, flags);
2336     /// \todo fixme: Per type methods need default flag too (lauris)
2338     if (style->opacity.value != SP_SCALE24_MAX) {
2339         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2340     }
2342     if (!style->color.noneSet) { // CSS does not permit "none" for color
2343         p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2344     }
2346     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2347     // if fill:none, skip writing fill properties
2348     if (!style->fill.noneSet) {
2349         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2350         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2351     }
2353     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2355     // stroke width affects markers, so write it if there's stroke OR any markers
2356     if (!style->stroke.noneSet ||
2357         style->marker[SP_MARKER_LOC].set ||
2358         style->marker[SP_MARKER_LOC_START].set ||
2359         style->marker[SP_MARKER_LOC_MID].set ||
2360         style->marker[SP_MARKER_LOC_END].set) {
2361         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2362     }
2364     // if stroke:none, skip writing stroke properties
2365     if (!style->stroke.noneSet) {
2366         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2367         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2368         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2369         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2371         /** \todo fixme: */
2372         if ((flags == SP_STYLE_FLAG_ALWAYS)
2373             || style->stroke_dasharray_set)
2374         {
2375             if (style->stroke_dasharray_inherit) {
2376                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2377             } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2378                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2379                 gint i;
2380                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2381                     Inkscape::CSSOStringStream os;
2382                     if (i) {
2383                         os << ", ";
2384                     }
2385                     os << style->stroke_dash.dash[i];
2386                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2387                 }
2388                 if (p < c + BMAX) {
2389                     *p++ = ';';
2390                 }
2391             } else {
2392                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2393             }
2394         }
2396         /** \todo fixme: */
2397         if (style->stroke_dashoffset_set) {
2398             if (style->stroke_dashoffset_inherit) {
2399                 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2400             } else {
2401                 Inkscape::CSSOStringStream os;
2402                 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2403                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2404             }
2405         } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2406             p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2407         }
2408     }
2410     bool marker_none = false;
2411     gchar *master = style->marker[SP_MARKER_LOC].value;
2412     if (style->marker[SP_MARKER_LOC].set) {
2413         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2414     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2415         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2416         marker_none = true;
2417     }
2418     if (style->marker[SP_MARKER_LOC_START].set
2419        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_START].value))) {
2420         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2421     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2422         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2423     }
2424     if (style->marker[SP_MARKER_LOC_MID].set
2425        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_MID].value))) {
2426         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2427     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2428         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2429     }
2430     if (style->marker[SP_MARKER_LOC_END].set
2431        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_END].value))) {
2432         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2433     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2434         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2435     }
2437     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2438     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2439     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2441     /* filter: */
2442     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2444     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2446     /* fixme: */
2447     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2449     /* Get rid of trailing `;'. */
2450     if (p != c) {
2451         --p;
2452         if (*p == ';') {
2453             *p = '\0';
2454         }
2455     }
2457     return g_strdup(c);
2461 #define STYLE_BUF_MAX
2464 /**
2465  * Dumps style to CSS string, see sp_style_write_string()
2466  *
2467  * \pre from != NULL.
2468  * \pre to != NULL.
2469  * \post ret != NULL.
2470  */
2471 gchar *
2472 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2474     g_return_val_if_fail(from != NULL, NULL);
2475     g_return_val_if_fail(to != NULL, NULL);
2477     gchar c[BMAX], *p = c;
2478     *p = '\0';
2480     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2481     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2482     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2483     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2484     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2486     /* Text */
2487     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2488     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2489     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2490     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2491     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2492     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2493     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2494     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2495     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2496     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2498     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2499     p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &from->baseline_shift, &to->baseline_shift, SP_STYLE_FLAG_IFDIFF);
2501     /// \todo fixme: Per type methods need default flag too
2502     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2503         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2504     }
2506     if (!from->color.noneSet) { // CSS does not permit "none" for color
2507         p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2508     }
2510     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2511     // if fill:none, skip writing fill properties
2512     if (!from->fill.noneSet) {
2513         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2514         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2515     }
2517     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2519     // stroke width affects markers, so write it if there's stroke OR any markers
2520     if (!from->stroke.noneSet ||
2521         from->marker[SP_MARKER_LOC].set ||
2522         from->marker[SP_MARKER_LOC_START].set ||
2523         from->marker[SP_MARKER_LOC_MID].set ||
2524         from->marker[SP_MARKER_LOC_END].set) {
2525         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2526     }
2528     // if stroke:none, skip writing stroke properties
2529     if (!from->stroke.noneSet) {
2530         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2531                                   &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2532         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2533                                   &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2534         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2535                                    &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2536         /** \todo fixme: */
2537         if (from->stroke_dasharray_set) {
2538             if (from->stroke_dasharray_inherit) {
2539                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2540             } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2541                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2542                 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2543                     Inkscape::CSSOStringStream os;
2544                     if (i) {
2545                         os << ", ";
2546                     }
2547                     os << from->stroke_dash.dash[i];
2548                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2549                 }
2550                 p += g_snprintf(p, c + BMAX - p, ";");
2551             }
2552         }
2553         /* fixme: */
2554         if (from->stroke_dashoffset_set) {
2555             if (from->stroke_dashoffset_inherit) {
2556                 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2557             } else {
2558                 Inkscape::CSSOStringStream os;
2559                 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2560                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2561             }
2562         }
2563         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2564     }
2566     /* markers */
2567     gchar *master = from->marker[SP_MARKER_LOC].value;
2568     if (master != NULL) {
2569         p += g_snprintf(p, c + BMAX - p, "marker:%s;", master);
2570     }
2571     if (from->marker[SP_MARKER_LOC_START].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_START].value))) {
2572         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2573     }
2574     if (from->marker[SP_MARKER_LOC_MID].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_MID].value))) {
2575         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2576     }
2577     if (from->marker[SP_MARKER_LOC_END].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_END].value))) {
2578         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2579     }
2581     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2582     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2583     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2585     /* filter: */
2586     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2588     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2590     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2592     /** \todo
2593      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2594      * flag is mainly only for attributes that don't handle explicit unset well.
2595      * We may need to revisit the behaviour of this routine.
2596      */
2598     /* Get rid of trailing `;'. */
2599     if (p != c) {
2600         --p;
2601         if (*p == ';') {
2602             *p = '\0';
2603         }
2604     }
2606     return g_strdup(c);
2611 /**
2612  * Reset all style properties.
2613  */
2614 static void
2615 sp_style_clear(SPStyle *style)
2617     g_return_if_fail(style != NULL);
2619     style->fill.clear();
2620     style->stroke.clear();
2621     sp_style_filter_clear(style);
2623     if (style->fill.value.href) {
2624         delete style->fill.value.href;
2625         style->fill.value.href = NULL;
2626     }
2627     if (style->stroke.value.href) {
2628         delete style->stroke.value.href;
2629         style->stroke.value.href = NULL;
2630     }
2631     if (style->filter.href) {
2632         delete style->filter.href;
2633         style->filter.href = NULL;
2634     }
2636     if (style->stroke_dash.dash) {
2637         g_free(style->stroke_dash.dash);
2638     }
2640     style->stroke_dasharray_inherit = FALSE;
2641     style->stroke_dashoffset_inherit = FALSE;
2643     /** \todo fixme: Do that text manipulation via parents */
2644     SPObject *object = style->object;
2645     SPDocument *document = style->document;
2646     gint const refcount = style->refcount;
2647     SPTextStyle *text = style->text;
2648     unsigned const text_private = style->text_private;
2650     memset(style, 0, sizeof(SPStyle));
2652     style->refcount = refcount;
2653     style->object = object;
2654     style->document = document;
2656     if (document) {
2657         style->filter.href = new SPFilterReference(document);
2658         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2660         style->fill.value.href = new SPPaintServerReference(document);
2661         style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2663         style->stroke.value.href = new SPPaintServerReference(document);
2664         style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2665     }
2667     style->text = text;
2668     style->text_private = text_private;
2670     style->text->font_specification.set = FALSE;
2671     style->text->font.set = FALSE;
2672     style->text->font_family.set = FALSE;
2674     style->font_size.set = FALSE;
2675     style->font_size.type = SP_FONT_SIZE_LITERAL;
2676     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2677     style->font_size.computed = 12.0;
2678     style->font_style.set = FALSE;
2679     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2680     style->font_variant.set = FALSE;
2681     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2682     style->font_weight.set = FALSE;
2683     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2684     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2685     style->font_stretch.set = FALSE;
2686     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2688     /* text */
2689     style->text_indent.set = FALSE;
2690     style->text_indent.unit = SP_CSS_UNIT_NONE;
2691     style->text_indent.computed = 0.0;
2693     style->text_align.set = FALSE;
2694     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2696     style->text_decoration.set = FALSE;
2697     style->text_decoration.underline = FALSE;
2698     style->text_decoration.overline = FALSE;
2699     style->text_decoration.line_through = FALSE;
2700     style->text_decoration.blink = FALSE;
2702     style->line_height.set = FALSE;
2703     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2704     style->line_height.normal = TRUE;
2705     style->line_height.value = style->line_height.computed = 1.0;
2707     style->letter_spacing.set = FALSE;
2708     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2709     style->letter_spacing.normal = TRUE;
2710     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2712     style->word_spacing.set = FALSE;
2713     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2714     style->word_spacing.normal = TRUE;
2715     style->word_spacing.value = style->word_spacing.computed = 0.0;
2717     style->baseline_shift.set = FALSE;
2718     style->baseline_shift.type = SP_BASELINE_SHIFT_LITERAL;
2719     style->baseline_shift.unit = SP_CSS_UNIT_NONE;
2720     style->baseline_shift.literal = SP_CSS_BASELINE_SHIFT_BASELINE; 
2721     style->baseline_shift.value = 0.0;
2722     style->baseline_shift.computed = 0.0;
2724     style->text_transform.set = FALSE;
2725     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2727     style->direction.set = FALSE;
2728     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2730     style->block_progression.set = FALSE;
2731     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2733     style->writing_mode.set = FALSE;
2734     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2736     style->text_anchor.set = FALSE;
2737     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2740     style->opacity.value = SP_SCALE24_MAX;
2741     style->visibility.set = FALSE;
2742     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2743     style->display.set = FALSE;
2744     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2745     style->overflow.set = FALSE;
2746     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2748     style->color.clear();
2749     style->color.setColor(0.0, 0.0, 0.0);
2751     style->fill.clear();
2752     style->fill.setColor(0.0, 0.0, 0.0);
2753     style->fill_opacity.value = SP_SCALE24_MAX;
2754     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2756     style->stroke.clear();
2757     style->stroke_opacity.value = SP_SCALE24_MAX;
2759     style->stroke_width.set = FALSE;
2760     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2761     style->stroke_width.computed = 1.0;
2763     style->stroke_linecap.set = FALSE;
2764     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2765     style->stroke_linejoin.set = FALSE;
2766     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2768     style->stroke_miterlimit.set = FALSE;
2769     style->stroke_miterlimit.value = 4.0;
2771     style->stroke_dash.n_dash = 0;
2772     style->stroke_dash.dash = NULL;
2773     style->stroke_dash.offset = 0.0;
2775     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2776         g_free(style->marker[i].value);
2777         style->marker[i].set = FALSE;
2778     }
2780     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2781     style->enable_background.set = false;
2782     style->enable_background.inherit = false;
2787 /**
2788  *
2789  */
2790 static void
2791 sp_style_read_dash(SPStyle *style, gchar const *str)
2793     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2794     style->stroke_dasharray_set = TRUE;
2796     if (strcmp(str, "inherit") == 0) {
2797         style->stroke_dasharray_inherit = true;
2798         return;
2799     }
2800     style->stroke_dasharray_inherit = false;
2802     NRVpathDash &dash = style->stroke_dash;
2803     g_free(dash.dash);
2804     dash.dash = NULL;
2806     if (strcmp(str, "none") == 0) {
2807         dash.n_dash = 0;
2808         return;
2809     }
2811     gint n_dash = 0;
2812     gdouble d[64];
2813     gchar *e = NULL;
2815     bool LineSolid=true;
2816     while (e != str && n_dash < 64) {
2817         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2818         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2819         if (d[n_dash] > 0.00000001)
2820             LineSolid = false;
2821         if (e != str) {
2822             n_dash += 1;
2823             str = e;
2824         }
2825         while (str && *str && !isalnum(*str)) str += 1;
2826     }
2828     if (LineSolid) {
2829         dash.n_dash = 0;
2830         return;
2831     }
2833     if (n_dash > 0) {
2834         dash.dash = g_new(gdouble, n_dash);
2835         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2836         dash.n_dash = n_dash;
2837     }
2841 /*#########################
2842 ## SPTextStyle operations
2843 #########################*/
2846 /**
2847  * Return new SPTextStyle object with default settings.
2848  */
2849 static SPTextStyle *
2850 sp_text_style_new()
2852     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2853     ts->refcount = 1;
2854     sp_text_style_clear(ts);
2856     ts->font_specification.value = g_strdup("Sans");
2857     ts->font.value = g_strdup("Sans");
2858     ts->font_family.value = g_strdup("Sans");
2860     return ts;
2864 /**
2865  * Clear text style settings.
2866  */
2867 static void
2868 sp_text_style_clear(SPTextStyle *ts)
2870     ts->font_specification.set = FALSE;
2871     ts->font.set = FALSE;
2872     ts->font_family.set = FALSE;
2877 /**
2878  * Reduce refcount of text style and possibly free it.
2879  */
2880 static SPTextStyle *
2881 sp_text_style_unref(SPTextStyle *st)
2883     st->refcount -= 1;
2885     if (st->refcount < 1) {
2886         g_free(st->font_specification.value);
2887         g_free(st->font.value);
2888         g_free(st->font_family.value);
2889         g_free(st);
2890     }
2892     return NULL;
2897 /**
2898  * Return duplicate of text style.
2899  */
2900 static SPTextStyle *
2901 sp_text_style_duplicate_unset(SPTextStyle *st)
2903     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2904     nt->refcount = 1;
2906     nt->font_specification.value = g_strdup(st->font_specification.value);
2907     nt->font.value = g_strdup(st->font.value);
2908     nt->font_family.value = g_strdup(st->font_family.value);
2910     return nt;
2915 /**
2916  * Write SPTextStyle object into string.
2917  */
2918 static guint
2919 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2921     gint d = 0;
2923     // We do not do diffing for text style
2924     if (flags == SP_STYLE_FLAG_IFDIFF)
2925         flags = SP_STYLE_FLAG_IFSET;
2927     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2928     d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2929     return d;
2934 /* The following sp_tyle_read_* functions ignore invalid values, as per
2935  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2936  *
2937  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2938  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2939  * pairs, in which case SVG's error-handling rules
2940  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2941  */
2944 /**
2945  * Set SPIFloat object from string.
2946  */
2947 static void
2948 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2950     if (!strcmp(str, "inherit")) {
2951         val->set = TRUE;
2952         val->inherit = TRUE;
2953     } else {
2954         gfloat value;
2955         if (sp_svg_number_read_f(str, &value)) {
2956             val->set = TRUE;
2957             val->inherit = FALSE;
2958             val->value = value;
2959         }
2960     }
2965 /**
2966  * Set SPIScale24 object from string.
2967  */
2968 static void
2969 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2971     if (!strcmp(str, "inherit")) {
2972         val->set = TRUE;
2973         val->inherit = TRUE;
2974     } else {
2975         gfloat value;
2976         if (sp_svg_number_read_f(str, &value)) {
2977             val->set = TRUE;
2978             val->inherit = FALSE;
2979             value = CLAMP(value, 0.0, 1.0);
2980             val->value = SP_SCALE24_FROM_FLOAT(value);
2981         }
2982     }
2985 /**
2986  * Reads a style value and performs lookup based on the given style value enumerations.
2987  */
2988 static void
2989 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2990                     bool const can_explicitly_inherit)
2992     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2993         val->set = TRUE;
2994         val->inherit = TRUE;
2995     } else {
2996         for (unsigned i = 0; dict[i].key; i++) {
2997             if (!strcmp(str, dict[i].key)) {
2998                 val->set = TRUE;
2999                 val->inherit = FALSE;
3000                 val->value = dict[i].value;
3001                 /* Save copying for values not needing it */
3002                 val->computed = val->value;
3003                 break;
3004             }
3005         }
3006     }
3011 /**
3012  * Set SPIString object from string.
3013  */
3014 static void
3015 sp_style_read_istring(SPIString *val, gchar const *str)
3017     g_free(val->value);
3019     if (!strcmp(str, "inherit")) {
3020         val->set = TRUE;
3021         val->inherit = TRUE;
3022         val->value = NULL;
3023     } else {
3024         val->set = TRUE;
3025         val->inherit = FALSE;
3026         val->value = g_strdup(str);
3027     }
3032 /**
3033  * Set SPILength object from string.
3034  */
3035 static void
3036 sp_style_read_ilength(SPILength *val, gchar const *str)
3038     if (!strcmp(str, "inherit")) {
3039         val->set = TRUE;
3040         val->inherit = TRUE;
3041     } else {
3042         gdouble value;
3043         gchar *e;
3044         /** \todo fixme: Move this to standard place (Lauris) */
3045         value = g_ascii_strtod(str, &e);
3046         if ((gchar const *) e != str) {
3047             /** \todo
3048              * Allow the number of px per inch to vary (document preferences,
3049              * X server or whatever).  E.g. don't fill in computed here, do
3050              * it at the same time as percentage units are done.
3051              */
3052             if (!*e) {
3053                 /* Userspace */
3054                 val->unit = SP_CSS_UNIT_NONE;
3055                 val->computed = value;
3056             } else if (!strcmp(e, "px")) {
3057                 /* Userspace */
3058                 val->unit = SP_CSS_UNIT_PX;
3059                 val->computed = value;
3060             } else if (!strcmp(e, "pt")) {
3061                 /* Userspace / DEVICESCALE */
3062                 val->unit = SP_CSS_UNIT_PT;
3063                 val->computed = value * PX_PER_PT;
3064             } else if (!strcmp(e, "pc")) {
3065                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
3066                 val->unit = SP_CSS_UNIT_PC;
3067                 val->computed = value * PX_PER_PT * 12;
3068             } else if (!strcmp(e, "mm")) {
3069                 val->unit = SP_CSS_UNIT_MM;
3070                 val->computed = value * PX_PER_MM;
3071             } else if (!strcmp(e, "cm")) {
3072                 val->unit = SP_CSS_UNIT_CM;
3073                 val->computed = value * PX_PER_CM;
3074             } else if (!strcmp(e, "in")) {
3075                 val->unit = SP_CSS_UNIT_IN;
3076                 val->computed = value * PX_PER_IN;
3077             } else if (!strcmp(e, "em")) {
3078                 /* EM square */
3079                 val->unit = SP_CSS_UNIT_EM;
3080                 val->value = value;
3081             } else if (!strcmp(e, "ex")) {
3082                 /* ex square */
3083                 val->unit = SP_CSS_UNIT_EX;
3084                 val->value = value;
3085             } else if (!strcmp(e, "%")) {
3086                 /* Percentage */
3087                 val->unit = SP_CSS_UNIT_PERCENT;
3088                 val->value = value * 0.01;
3089             } else {
3090                 /* Invalid */
3091                 return;
3092             }
3093             val->set = TRUE;
3094             val->inherit = FALSE;
3095         }
3096     }
3099 /**
3100  * Set SPILengthOrNormal object from string.
3101  */
3102 static void
3103 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
3105     if (!strcmp(str, "normal")) {
3106         val->set = TRUE;
3107         val->inherit = FALSE;
3108         val->normal = TRUE;
3109         val->unit = SP_CSS_UNIT_NONE;
3110         val->value = val->computed = 0.0;
3111     } else {
3112         SPILength length;
3113         sp_style_read_ilength(&length, str);
3114         val->set = length.set;
3115         val->inherit = length.inherit;
3116         val->normal = FALSE;
3117         val->unit = length.unit;
3118         val->value = length.value;
3119         val->computed = length.computed;
3120     }
3123 /**
3124  * Set SPITextDecoration object from string.
3125  */
3126 static void
3127 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
3129     if (!strcmp(str, "inherit")) {
3130         val->set = TRUE;
3131         val->inherit = TRUE;
3132     } else if (!strcmp(str, "none")) {
3133         val->set = TRUE;
3134         val->inherit = FALSE;
3135         val->underline = FALSE;
3136         val->overline = FALSE;
3137         val->line_through = FALSE;
3138         val->blink = FALSE;
3139     } else {
3140         bool found_underline = false;
3141         bool found_overline = false;
3142         bool found_line_through = false;
3143         bool found_blink = false;
3144         for ( ; *str ; str++ ) {
3145             if (*str == ' ') continue;
3146             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
3147                 found_underline = true;
3148                 str += 9;
3149             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
3150                 found_overline = true;
3151                 str += 8;
3152             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
3153                 found_line_through = true;
3154                 str += 12;
3155             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
3156                 found_blink = true;
3157                 str += 5;
3158             } else {
3159                 return;  // invalid value
3160             }
3161         }
3162         if (!(found_underline || found_overline || found_line_through || found_blink)) {
3163             return;  // invalid value: empty
3164         }
3165         val->set = TRUE;
3166         val->inherit = FALSE;
3167         val->underline = found_underline;
3168         val->overline = found_overline;
3169         val->line_through = found_line_through;
3170         val->blink = found_blink;
3171     }
3174 /**
3175  * Set SPIPaint object from string containing an integer value.
3176  * \param document Ignored
3177  */
3178 static void
3179 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3181     (void)style; // TODO
3182     (void)document; // TODO
3183     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
3184     if (!strcmp(str, "inherit")) {
3185         paint->set = TRUE;
3186         paint->inherit = TRUE;
3187     } else {
3188         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
3189         if (rgb0 != 0xff) {
3190             paint->setColor(rgb0);
3191             paint->set = TRUE;
3192             paint->inherit = FALSE;
3193         }
3194     }
3198 /**
3199  * Set SPIPaint object from string.
3200  *
3201  * \pre paint == \&style.fill || paint == \&style.stroke.
3202  */
3203 static void
3204 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3206     while (g_ascii_isspace(*str)) {
3207         ++str;
3208     }
3210     paint->clear();
3212     if (streq(str, "inherit")) {
3213         paint->set = TRUE;
3214         paint->inherit = TRUE;
3215     } else {
3216         if ( strneq(str, "url", 3) ) {
3217             gchar *uri = extract_uri( str, &str );
3218             while ( g_ascii_isspace(*str) ) {
3219                 ++str;
3220             }
3221             // TODO check on and comment the comparrison "paint != &style->color".
3222             if ( uri && *uri && (paint != &style->color) ) {
3223                 paint->set = TRUE;
3225                 // it may be that this style's SPIPaint has not yet created its URIReference;
3226                 // now that we have a document, we can create it here
3227                 if (!paint->value.href && document) {
3228                     paint->value.href = new SPPaintServerReference(document);
3229                     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));
3230                 }
3232                 // TODO check what this does in light of move away from union
3233                 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3234             }
3235             g_free( uri );
3236         }
3238         if (streq(str, "currentColor") && paint != &style->color) {
3239             paint->set = TRUE;
3240             paint->currentcolor = TRUE;
3241         } else if (streq(str, "none") && paint != &style->color) {
3242             paint->set = TRUE;
3243             paint->noneSet = TRUE;
3244         } else {
3245             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3246             if (rgb0 != 0xff) {
3247                 paint->setColor( rgb0 );
3248                 paint->set = TRUE;
3250                 while (g_ascii_isspace(*str)) {
3251                     ++str;
3252                 }
3253                 if (strneq(str, "icc-color(", 10)) {
3254                     SVGICCColor* tmp = new SVGICCColor();
3255                     if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3256                         delete tmp;
3257                         tmp = 0;
3258                     }
3259                     paint->value.color.icc = tmp;
3260                 }
3261             }
3262         }
3263     }
3268 /**
3269  * Set SPIFontSize object from string.
3270  */
3271 static void
3272 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3274     if (!strcmp(str, "inherit")) {
3275         val->set = TRUE;
3276         val->inherit = TRUE;
3277     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3278         // xx-small, x-small, etc.
3279         for (unsigned i = 0; enum_font_size[i].key; i++) {
3280             if (!strcmp(str, enum_font_size[i].key)) {
3281                 val->set = TRUE;
3282                 val->inherit = FALSE;
3283                 val->type = SP_FONT_SIZE_LITERAL;
3284                 val->value = enum_font_size[i].value;
3285                 return;
3286             }
3287         }
3288         /* Invalid */
3289         return;
3290     } else {
3291         gdouble value;
3292         gchar *e;
3293         /* fixme: Move this to standard place (Lauris) */
3294         value = g_ascii_strtod(str, &e);
3295         if ((gchar const *) e != str) {
3296             if (!*e) {
3297                 /* Userspace */
3298             } else if (!strcmp(e, "px")) {
3299                 /* Userspace */
3300             } else if (!strcmp(e, "pt")) {
3301                 /* Userspace * DEVICESCALE */
3302                 value *= PX_PER_PT;
3303             } else if (!strcmp(e, "pc")) {
3304                 /* 12pt */
3305                 value *= PX_PER_PT * 12.0;
3306             } else if (!strcmp(e, "mm")) {
3307                 value *= PX_PER_MM;
3308             } else if (!strcmp(e, "cm")) {
3309                 value *= PX_PER_CM;
3310             } else if (!strcmp(e, "in")) {
3311                 value *= PX_PER_IN;
3312             } else if (!strcmp(e, "%")) {
3313                 /* Percentage */
3314                 val->set = TRUE;
3315                 val->inherit = FALSE;
3316                 val->type = SP_FONT_SIZE_PERCENTAGE;
3317                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3318                 return;
3319             } else {
3320                 /* Invalid */
3321                 return;
3322             }
3323             /* Length */
3324             val->set = TRUE;
3325             val->inherit = FALSE;
3326             val->type = SP_FONT_SIZE_LENGTH;
3327             val->computed = value;
3328             return;
3329         }
3330     }
3334 /**
3335  * Set SPIBaselineShift object from string.
3336  */
3337 static void
3338 sp_style_read_ibaselineshift(SPIBaselineShift *val, gchar const *str)
3340     if (!strcmp(str, "inherit")) {
3341         val->set = TRUE;
3342         val->inherit = TRUE;
3343     } else if ((*str == 'b') || (*str == 's')) {
3344         // baseline or sub or super
3345         for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
3346             if (!strcmp(str, enum_baseline_shift[i].key)) {
3347                 val->set = TRUE;
3348                 val->inherit = FALSE;
3349                 val->type = SP_BASELINE_SHIFT_LITERAL;
3350                 val->literal = enum_baseline_shift[i].value;
3351                 return;
3352             }
3353         }
3354         /* Invalid */
3355         return;
3356     } else {
3357         SPILength length;
3358         sp_style_read_ilength(&length, str);
3359         val->set      = length.set;
3360         val->inherit  = length.inherit;
3361         val->unit     = length.unit;
3362         val->value    = length.value;
3363         val->computed = length.computed;
3364         if( val->unit == SP_CSS_UNIT_PERCENT ) {
3365             val->type = SP_BASELINE_SHIFT_PERCENTAGE;
3366         } else {
3367             val->type = SP_BASELINE_SHIFT_LENGTH;
3368         }
3369         return;
3370     }
3374 /**
3375  * Set SPIFilter object from string.
3376  */
3377 static void
3378 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3380     SPIFilter *f = &(style->filter);
3381     /* Try all possible values: inherit, none, uri */
3382     if (streq(str, "inherit")) {
3383         f->set = TRUE;
3384         f->inherit = TRUE;
3385         if (f->href && f->href->getObject())
3386             f->href->detach();
3387     } else if(streq(str, "none")) {
3388         f->set = TRUE;
3389         f->inherit = FALSE;
3390         if (f->href && f->href->getObject())
3391            f->href->detach();
3392     } else if (strneq(str, "url", 3)) {
3393         char *uri = extract_uri(str);
3394         if(uri == NULL || uri[0] == '\0') {
3395             g_warning("Specified filter url is empty");
3396             f->set = TRUE;
3397             f->inherit = FALSE;
3398             return;
3399         }
3400         f->set = TRUE;
3401         f->inherit = FALSE;
3402         if (f->href && f->href->getObject())
3403             f->href->detach();
3405         // it may be that this style has not yet created its SPFilterReference;
3406         // now that we have a document, we can create it here
3407         if (!f->href && document) {
3408             f->href = new SPFilterReference(document);
3409             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3410         }
3412         try {
3413             f->href->attach(Inkscape::URI(uri));
3414         } catch (Inkscape::BadURIException &e) {
3415             g_warning("%s", e.what());
3416             f->href->detach();
3417         }
3418         g_free (uri);
3420     } else {
3421         /* We shouldn't reach this if SVG input is well-formed */
3422         f->set = FALSE;
3423         f->inherit = FALSE;
3424         if (f->href && f->href->getObject())
3425             f->href->detach();
3426     }
3429 /**
3430  * Set SPIEnum object from repr attribute.
3431  */
3432 static void
3433 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3434                     gchar const *key, SPStyleEnum const *dict,
3435                     bool const can_explicitly_inherit)
3437     gchar const *str = repr->attribute(key);
3438     if (str) {
3439         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3440     }
3445 /**
3446  * Set SPILength object from repr attribute.
3447  */
3448 static void
3449 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3451     gchar const *str = repr->attribute(key);
3452     if (str) {
3453         sp_style_read_ilength(val, str);
3454     }
3457 /**
3458  * Set SPIFontSize object from repr attribute.
3459  */
3460 static void
3461 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3463     gchar const *str = repr->attribute(key);
3464     if (str) {
3465         sp_style_read_ifontsize(val, str);
3466     }
3470 /**
3471  * Set SPIBaselineShift object from repr attribute.
3472  */
3473 static void
3474 sp_style_read_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key)
3476     gchar const *str = repr->attribute(key);
3477     if (str) {
3478         sp_style_read_ibaselineshift(val, str);
3479     }
3483 /**
3484  * Set SPIFloat object from repr attribute.
3485  */
3486 static void
3487 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3489     gchar const *str = repr->attribute(key);
3490     if (str) {
3491         sp_style_read_ifloat(val, str);
3492     }
3496 /**
3497  * Write SPIFloat object into string.
3498  */
3499 static gint
3500 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3501                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3503     Inkscape::CSSOStringStream os;
3505     if ((flags & SP_STYLE_FLAG_ALWAYS)
3506         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3507         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3508             && (!base->set || (val->value != base->value))))
3509     {
3510         if (val->inherit) {
3511             return g_snprintf(p, len, "%s:inherit;", key);
3512         } else {
3513             os << key << ":" << val->value << ";";
3514             return g_strlcpy(p, os.str().c_str(), len);
3515         }
3516     }
3517     return 0;
3521 /**
3522  * Write SPIScale24 object into string.
3523  */
3524 static gint
3525 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3526                         SPIScale24 const *const val, SPIScale24 const *const base,
3527                         guint const flags)
3529     Inkscape::CSSOStringStream os;
3531     if ((flags & SP_STYLE_FLAG_ALWAYS)
3532         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3533         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3534             && (!base->set || (val->value != base->value))))
3535     {
3536         if (val->inherit) {
3537             return g_snprintf(p, len, "%s:inherit;", key);
3538         } else {
3539             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3540             return g_strlcpy(p, os.str().c_str(), len);
3541         }
3542     }
3543     return 0;
3547 /**
3548  * Write SPIEnum object into string.
3549  */
3550 static gint
3551 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3552                      SPStyleEnum const *const dict,
3553                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3555     if ((flags & SP_STYLE_FLAG_ALWAYS)
3556         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3557         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3558             && (!base->set || (val->computed != base->computed))))
3559     {
3560         if (val->inherit) {
3561             return g_snprintf(p, len, "%s:inherit;", key);
3562         }
3563         for (unsigned i = 0; dict[i].key; i++) {
3564             if (dict[i].value == static_cast< gint > (val->value) ) {
3565                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3566             }
3567         }
3568     }
3569     return 0;
3574 /**
3575  * Write SPIString object into string.
3576  */
3577 static gint
3578 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3579                        SPIString const *const val, SPIString const *const base, guint const flags)
3581     if ((flags & SP_STYLE_FLAG_ALWAYS)
3582         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3583         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3584             && (!base->set || strcmp(val->value, base->value))))
3585     {
3586         if (val->inherit) {
3587             return g_snprintf(p, len, "%s:inherit;", key);
3588         } else {
3589             gchar *val_quoted = css2_escape_quote(val->value);
3590             if (val_quoted) {
3591                 return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3592                 g_free (val_quoted);
3593             }
3594         }
3595     }
3596     return 0;
3600 /**
3601  *
3602  */
3603 static bool
3604 sp_length_differ(SPILength const *const a, SPILength const *const b)
3606     if (a->unit != b->unit) {
3607         if (a->unit == SP_CSS_UNIT_EM) return true;
3608         if (a->unit == SP_CSS_UNIT_EX) return true;
3609         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3610         if (b->unit == SP_CSS_UNIT_EM) return true;
3611         if (b->unit == SP_CSS_UNIT_EX) return true;
3612         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3613     }
3615     return (a->computed != b->computed);
3620 /**
3621  * Write SPILength object into string.
3622  */
3623 static gint
3624 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3625                        SPILength const *const val, SPILength const *const base, guint const flags)
3627     Inkscape::CSSOStringStream os;
3629     if ((flags & SP_STYLE_FLAG_ALWAYS)
3630         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3631         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3632             && (!base->set || sp_length_differ(val, base))))
3633     {
3634         if (val->inherit) {
3635             return g_snprintf(p, len, "%s:inherit;", key);
3636         } else {
3637             switch (val->unit) {
3638                 case SP_CSS_UNIT_NONE:
3639                     os << key << ":" << val->computed << ";";
3640                     return g_strlcpy(p, os.str().c_str(), len);
3641                     break;
3642                 case SP_CSS_UNIT_PX:
3643                     os << key << ":" << val->computed << "px;";
3644                     return g_strlcpy(p, os.str().c_str(), len);
3645                     break;
3646                 case SP_CSS_UNIT_PT:
3647                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3648                     return g_strlcpy(p, os.str().c_str(), len);
3649                     break;
3650                 case SP_CSS_UNIT_PC:
3651                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3652                     return g_strlcpy(p, os.str().c_str(), len);
3653                     break;
3654                 case SP_CSS_UNIT_MM:
3655                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3656                     return g_strlcpy(p, os.str().c_str(), len);
3657                     break;
3658                 case SP_CSS_UNIT_CM:
3659                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3660                     return g_strlcpy(p, os.str().c_str(), len);
3661                     break;
3662                 case SP_CSS_UNIT_IN:
3663                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3664                     return g_strlcpy(p, os.str().c_str(), len);
3665                     break;
3666                 case SP_CSS_UNIT_EM:
3667                     os << key << ":" << val->value << "em;";
3668                     return g_strlcpy(p, os.str().c_str(), len);
3669                     break;
3670                 case SP_CSS_UNIT_EX:
3671                     os << key << ":" << val->value << "ex;";
3672                     return g_strlcpy(p, os.str().c_str(), len);
3673                     break;
3674                 case SP_CSS_UNIT_PERCENT:
3675                     os << key << ":" << (val->value * 100.0) << "%;";
3676                     return g_strlcpy(p, os.str().c_str(), len);
3677                     break;
3678                 default:
3679                     /* Invalid */
3680                     break;
3681             }
3682         }
3683     }
3684     return 0;
3688 /**
3689  *
3690  */
3691 static bool
3692 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3694     if (a->normal != b->normal) return true;
3695     if (a->normal) return false;
3697     if (a->unit != b->unit) {
3698         if (a->unit == SP_CSS_UNIT_EM) return true;
3699         if (a->unit == SP_CSS_UNIT_EX) return true;
3700         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3701         if (b->unit == SP_CSS_UNIT_EM) return true;
3702         if (b->unit == SP_CSS_UNIT_EX) return true;
3703         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3704     }
3706     return (a->computed != b->computed);
3709 /**
3710  * Write SPILengthOrNormal object into string.
3711  */
3712 static gint
3713 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3714                                SPILengthOrNormal const *const val,
3715                                SPILengthOrNormal const *const base,
3716                                guint const flags)
3718     if ((flags & SP_STYLE_FLAG_ALWAYS)
3719         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3720         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3721             && (!base->set || sp_lengthornormal_differ(val, base))))
3722     {
3723         if (val->normal) {
3724             return g_snprintf(p, len, "%s:normal;", key);
3725         } else {
3726             SPILength length;
3727             length.set = val->set;
3728             length.inherit = val->inherit;
3729             length.unit = val->unit;
3730             length.value = val->value;
3731             length.computed = val->computed;
3732             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3733         }
3734     }
3735     return 0;
3738 /**
3739  *
3740  */
3741 static bool
3742 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3744     return    a->underline != b->underline
3745            || a->overline != b->overline
3746            || a->line_through != b->line_through
3747            || a->blink != b->blink;
3750 /**
3751  * Write SPITextDecoration object into string.
3752  */
3753 static gint
3754 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3755                                SPITextDecoration const *const val,
3756                                SPITextDecoration const *const base,
3757                                guint const flags)
3759     Inkscape::CSSOStringStream os;
3761     if ((flags & SP_STYLE_FLAG_ALWAYS)
3762         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3763         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3764             && (!base->set || sp_textdecoration_differ(val, base))))
3765     {
3766         if (val->inherit) {
3767             return g_snprintf(p, len, "%s:inherit;", key);
3768         } else {
3769             os << key << ":";
3770             if (val->underline || val->overline || val->line_through || val->blink) {
3771                 if (val->underline) os << " underline";
3772                 if (val->overline) os << " overline";
3773                 if (val->line_through) os << " line-through";
3774                 if (val->blink) os << " blink";
3775             } else
3776                 os << "none";
3777             os << ";";
3778             return g_strlcpy(p, os.str().c_str(), len);
3779         }
3780     }
3781     return 0;
3784 /**
3785  *
3786  */
3787 static bool
3788 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3790     if ( (a->isColor() != b->isColor())
3791          || (a->isPaintserver() != b->isPaintserver())
3792          || (a->set != b->set)
3793          || (a->currentcolor != b->currentcolor)
3794          || (a->inherit!= b->inherit) ) {
3795         return true;
3796     }
3798     // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3800     if ( a->isPaintserver() ) {
3801         return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3802     }
3804     if ( a->isColor() ) {
3805         return !( (a->value.color == b->value.color)
3806                  && ((a->value.color.icc == b->value.color.icc)
3807                      || (a->value.color.icc && b->value.color.icc
3808                          && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3809                          && (a->value.color.icc->colors == b->value.color.icc->colors))));
3810     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3811      * in the string representation. */
3812     }
3814     return false;
3819 /**
3820  * Write SPIPaint object into string.
3821  */
3822 static gint
3823 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3824                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3826     int retval = 0;
3828     if ((flags & SP_STYLE_FLAG_ALWAYS)
3829         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3830         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3831             && (!base->set || sp_paint_differ(paint, base))))
3832     {
3833         CSSOStringStream css;
3835         if (paint->inherit) {
3836             css << "inherit";
3837         } else {
3838             if ( paint->value.href && paint->value.href->getURI() ) {
3839                 const gchar* uri = paint->value.href->getURI()->toString();
3840                 css << "url(" << uri << ")";
3841             }
3843             if ( paint->noneSet ) {
3844                 if ( !css.str().empty() ) {
3845                     css << " ";
3846                 }
3847                 css << "none";
3848             }
3850             if ( paint->currentcolor ) {
3851                 if ( !css.str().empty() ) {
3852                     css << " ";
3853                 }
3854                 css << "currentColor";
3855             }
3857             if ( paint->colorSet && !paint->currentcolor ) {
3858                 if ( !css.str().empty() ) {
3859                     css << " ";
3860                 }
3861                 char color_buf[8];
3862                 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3863                 css << color_buf;
3864             }
3866             if (paint->value.color.icc && !paint->currentcolor) {
3867                 if ( !css.str().empty() ) {
3868                     css << " ";
3869                 }
3870                 css << "icc-color(" << paint->value.color.icc->colorProfile;
3871                 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3872                          iEnd(paint->value.color.icc->colors.end());
3873                      i != iEnd; ++i) {
3874                     css << ", " << *i;
3875                 }
3876                 css << ')';
3877             }
3878         }
3880         if ( !css.str().empty() ) {
3881             retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3882         }
3883     }
3885     return retval;
3889 /**
3890  *
3891  */
3892 static bool
3893 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3895     if (a->type != b->type)
3896         return true;
3897     if (a->type == SP_FONT_SIZE_LENGTH) {
3898         if (a->computed != b->computed)
3899             return true;
3900     } else {
3901         if (a->value != b->value)
3902             return true;
3903     }
3904     return false;
3908 /**
3909  * Write SPIFontSize object into string.
3910  */
3911 static gint
3912 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3913                          SPIFontSize const *const val, SPIFontSize const *const base,
3914                          guint const flags)
3916     if ((flags & SP_STYLE_FLAG_ALWAYS)
3917         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3918         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3919             && (!base->set || sp_fontsize_differ(val, base))))
3920     {
3921         if (val->inherit) {
3922             return g_snprintf(p, len, "%s:inherit;", key);
3923         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3924             for (unsigned i = 0; enum_font_size[i].key; i++) {
3925                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3926                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3927                 }
3928             }
3929         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3930             Inkscape::CSSOStringStream os;
3931             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3932             return g_strlcpy(p, os.str().c_str(), len);
3933         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3934             Inkscape::CSSOStringStream os;
3935             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3936             return g_strlcpy(p, os.str().c_str(), len);
3937         }
3938     }
3939     return 0;
3943 /*
3944  * baseline-shift is relative to parent. The only time it should
3945  * not be written out is if it is zero (or not set).
3946  */
3947 static bool
3948 sp_baseline_shift_notzero(SPIBaselineShift const *const a )
3950     if( a->type == SP_BASELINE_SHIFT_LITERAL ) {
3951         if( a->literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
3952             return false;
3953         }
3954     } else {
3955         if( a->value == 0.0 ) {
3956             return false;
3957         }
3958     }
3959     return true;
3962 /**
3963  * Write SPIBaselineShift object into string.
3964  */
3965 static gint
3966 sp_style_write_ibaselineshift(gchar *p, gint const len, gchar const *key,
3967                               SPIBaselineShift const *const val, SPIBaselineShift const *const base,
3968                               guint const flags)
3970     if ((flags & SP_STYLE_FLAG_ALWAYS)
3971         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3972         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3973             && (!base->set || sp_baseline_shift_notzero(val) )))
3974     {
3975         if (val->inherit) {
3976             return g_snprintf(p, len, "%s:inherit;", key);
3977         } else if (val->type == SP_BASELINE_SHIFT_LITERAL) {
3978             for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
3979                 if (enum_baseline_shift[i].value == static_cast< gint > (val->literal) ) {
3980                     return g_snprintf(p, len, "%s:%s;", key, enum_baseline_shift[i].key);
3981                 }
3982             }
3983         } else if (val->type == SP_BASELINE_SHIFT_LENGTH) {
3984             if( val->unit == SP_CSS_UNIT_EM || val->unit == SP_CSS_UNIT_EX ) {
3985                 Inkscape::CSSOStringStream os;
3986                 os << key << ":" << val->value << (val->unit == SP_CSS_UNIT_EM ? "em;" : "ex;");
3987                 return g_strlcpy(p, os.str().c_str(), len);
3988             } else {
3989                 Inkscape::CSSOStringStream os;
3990                 os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3991                 return g_strlcpy(p, os.str().c_str(), len);
3992             }
3993         } else if (val->type == SP_BASELINE_SHIFT_PERCENTAGE) {
3994             Inkscape::CSSOStringStream os;
3995             os << key << ":" << (val->value * 100.0) << "%;";
3996             return g_strlcpy(p, os.str().c_str(), len);
3997         }
3998     }
3999     return 0;
4004 /**
4005  * Write SPIFilter object into string.
4006  */
4007 static gint
4008 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
4009                          SPIFilter const *const val, SPIFilter const *const base,
4010                          guint const flags)
4012     (void)base; // TODO
4013     if ((flags & SP_STYLE_FLAG_ALWAYS)
4014         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
4015         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
4016     {
4017         if (val->inherit) {
4018             return g_snprintf(p, len, "%s:inherit;", key);
4019         } else if (val->href && val->href->getURI()) {
4020             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
4021         }
4022     }
4025     return 0;
4029 void SPIPaint::clear()
4031     set = false;
4032     inherit = false;
4033     currentcolor = false;
4034     colorSet = false;
4035     noneSet = false;
4036     value.color.set( 0 );
4037     if ( value.href && value.href->getObject() )
4038     {
4039         value.href->detach();
4040     }
4044 /**
4045  * Clear filter object, and disconnect style from paintserver (if present).
4046  */
4047 static void
4048 sp_style_filter_clear(SPStyle *style)
4050     if (style->filter.href && style->filter.href->getObject())
4051         style->filter.href->detach();
4055 // FIXME: Everything below this line belongs in a different file - css-chemistry?
4057 void
4058 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
4060     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
4062     if (repr == NULL) return;
4064     SPCSSAttr *css = sp_repr_css_attr_new();
4065     if (linked) {
4066         gchar *val = g_strdup_printf("url(#%s)", linked->getId());
4067         sp_repr_css_set_property(css, property, val);
4068         g_free(val);
4069     } else {
4070         sp_repr_css_unset_property(css, "filter");
4071     }
4073     if (recursive) {
4074         sp_repr_css_change_recursive(repr, css, "style");
4075     } else {
4076         sp_repr_css_change(repr, css, "style");
4077     }
4078     sp_repr_css_attr_unref(css);
4082 /**
4083  * Clear all style property attributes in object.
4084  */
4085 void
4086 sp_style_unset_property_attrs(SPObject *o)
4088     if (!o) return;
4090     SPStyle *style = SP_OBJECT_STYLE(o);
4091     if (!style) return;
4093     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
4094     if (!repr) return;
4096     if (style->opacity.set) {
4097         repr->setAttribute("opacity", NULL);
4098     }
4099     if (style->color.set) {
4100         repr->setAttribute("color", NULL);
4101     }
4102     if (style->fill.set) {
4103         repr->setAttribute("fill", NULL);
4104     }
4105     if (style->fill_opacity.set) {
4106         repr->setAttribute("fill-opacity", NULL);
4107     }
4108     if (style->fill_rule.set) {
4109         repr->setAttribute("fill-rule", NULL);
4110     }
4111     if (style->stroke.set) {
4112         repr->setAttribute("stroke", NULL);
4113     }
4114     if (style->stroke_width.set) {
4115         repr->setAttribute("stroke-width", NULL);
4116     }
4117     if (style->stroke_linecap.set) {
4118         repr->setAttribute("stroke-linecap", NULL);
4119     }
4120     if (style->stroke_linejoin.set) {
4121         repr->setAttribute("stroke-linejoin", NULL);
4122     }
4123     if (style->marker[SP_MARKER_LOC].set) {
4124         repr->setAttribute("marker", NULL);
4125     }
4126     if (style->marker[SP_MARKER_LOC_START].set) {
4127         repr->setAttribute("marker-start", NULL);
4128     }
4129     if (style->marker[SP_MARKER_LOC_MID].set) {
4130         repr->setAttribute("marker-mid", NULL);
4131     }
4132     if (style->marker[SP_MARKER_LOC_END].set) {
4133         repr->setAttribute("marker-end", NULL);
4134     }
4135     if (style->stroke_opacity.set) {
4136         repr->setAttribute("stroke-opacity", NULL);
4137     }
4138     if (style->stroke_dasharray_set) {
4139         repr->setAttribute("stroke-dasharray", NULL);
4140     }
4141     if (style->stroke_dashoffset_set) {
4142         repr->setAttribute("stroke-dashoffset", NULL);
4143     }
4144     if (style->text_private && style->text->font_specification.set) {
4145         repr->setAttribute("-inkscape-font-specification", NULL);
4146     }
4147     if (style->text_private && style->text->font_family.set) {
4148         repr->setAttribute("font-family", NULL);
4149     }
4150     if (style->text_anchor.set) {
4151         repr->setAttribute("text-anchor", NULL);
4152     }
4153     if (style->writing_mode.set) {
4154         repr->setAttribute("writing_mode", NULL);
4155     }
4156     if (style->filter.set) {
4157         repr->setAttribute("filter", NULL);
4158     }
4159     if (style->enable_background.set) {
4160         repr->setAttribute("enable-background", NULL);
4161     }
4164 /**
4165  * \pre style != NULL.
4166  * \pre flags in {IFSET, ALWAYS}.
4167  */
4168 SPCSSAttr *
4169 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
4171     g_return_val_if_fail(style != NULL, NULL);
4172     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4173                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
4174                          NULL);
4175     gchar *style_str = sp_style_write_string(style, flags);
4176     SPCSSAttr *css = sp_repr_css_attr_new();
4177     sp_repr_css_attr_add_from_string(css, style_str);
4178     g_free(style_str);
4179     return css;
4183 /**
4184  * \pre object != NULL
4185  * \pre flags in {IFSET, ALWAYS}.
4186  */
4187 SPCSSAttr *
4188 sp_css_attr_from_object(SPObject *object, guint const flags)
4190     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4191                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
4192                          NULL);
4193     SPStyle const *const style = SP_OBJECT_STYLE(object);
4194     if (style == NULL)
4195         return NULL;
4196     return sp_css_attr_from_style(style, flags);
4199 /**
4200  * Unset any text-related properties
4201  */
4202 SPCSSAttr *
4203 sp_css_attr_unset_text(SPCSSAttr *css)
4205     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
4206     sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
4207     sp_repr_css_set_property(css, "font-size", NULL);
4208     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
4209     sp_repr_css_set_property(css, "font-style", NULL);
4210     sp_repr_css_set_property(css, "font-variant", NULL);
4211     sp_repr_css_set_property(css, "font-weight", NULL);
4212     sp_repr_css_set_property(css, "font-stretch", NULL);
4213     sp_repr_css_set_property(css, "font-family", NULL);
4214     sp_repr_css_set_property(css, "text-indent", NULL);
4215     sp_repr_css_set_property(css, "text-align", NULL);
4216     sp_repr_css_set_property(css, "text-decoration", NULL);
4217     sp_repr_css_set_property(css, "line-height", NULL);
4218     sp_repr_css_set_property(css, "letter-spacing", NULL);
4219     sp_repr_css_set_property(css, "word-spacing", NULL);
4220     sp_repr_css_set_property(css, "text-transform", NULL);
4221     sp_repr_css_set_property(css, "direction", NULL);
4222     sp_repr_css_set_property(css, "block-progression", NULL);
4223     sp_repr_css_set_property(css, "writing-mode", NULL);
4224     sp_repr_css_set_property(css, "text-anchor", NULL);
4225     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
4226     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
4227     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
4228     sp_repr_css_set_property(css, "baseline-shift", NULL);
4230     return css;
4233 bool
4234 is_url(char const *p)
4236     if (p == NULL)
4237         return false;
4238 /** \todo
4239  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
4240  * in property values must start with 'url('.
4241  */
4242     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
4245 /**
4246  * Unset any properties that contain URI values.
4247  *
4248  * Used for storing style that will be reused across documents when carrying
4249  * the referenced defs is impractical.
4250  */
4251 SPCSSAttr *
4252 sp_css_attr_unset_uris(SPCSSAttr *css)
4254 // All properties that may hold <uri> or <paint> according to SVG 1.1
4255     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
4256     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
4257     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
4258     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
4259     if (is_url(sp_repr_css_property(css, "marker", NULL))) sp_repr_css_set_property(css, "marker", NULL);
4260     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
4261     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
4262     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
4263     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
4264     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
4265     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
4267     return css;
4270 /**
4271  * Scale a single-value property.
4272  */
4273 void
4274 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
4275                                   double ex, bool only_with_units = false)
4277     gchar const *w = sp_repr_css_property(css, property, NULL);
4278     if (w) {
4279         gchar *units = NULL;
4280         double wd = g_ascii_strtod(w, &units) * ex;
4281         if (w == units) {// nothing converted, non-numeric value
4282             return;
4283         }
4284         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
4285             // only_with_units, but no units found, so do nothing.
4286             return;
4287         }
4288         Inkscape::CSSOStringStream os;
4289         os << wd << units; // reattach units
4290         sp_repr_css_set_property(css, property, os.str().c_str());
4291     }
4294 /**
4295  * Scale a list-of-values property.
4296  */
4297 void
4298 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
4300     gchar const *string = sp_repr_css_property(css, property, NULL);
4301     if (string) {
4302         Inkscape::CSSOStringStream os;
4303         gchar **a = g_strsplit(string, ",", 10000);
4304         bool first = true;
4305         for (gchar **i = a; i != NULL; i++) {
4306             gchar *w = *i;
4307             if (w == NULL)
4308                 break;
4309             gchar *units = NULL;
4310             double wd = g_ascii_strtod(w, &units) * ex;
4311             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4312                 g_strfreev(a);
4313                 return;
4314             }
4315             if (!first) {
4316                 os << ",";
4317             }
4318             os << wd << units; // reattach units
4319             first = false;
4320         }
4321         sp_repr_css_set_property(css, property, os.str().c_str());
4322         g_strfreev(a);
4323     }
4326 /**
4327  * Scale any properties that may hold <length> by ex.
4328  */
4329 SPCSSAttr *
4330 sp_css_attr_scale(SPCSSAttr *css, double ex)
4332     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4333     sp_css_attr_scale_property_single(css, "stroke-width", ex);
4334     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
4335     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4336     sp_css_attr_scale_property_single(css, "font-size", ex);
4337     sp_css_attr_scale_property_single(css, "kerning", ex);
4338     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4339     sp_css_attr_scale_property_single(css, "word-spacing", ex);
4340     sp_css_attr_scale_property_single(css, "line-height", ex, true);
4342     return css;
4346 /**
4347  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4348  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4349  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4350  * it parsed for us. So this function is only used to remove those quotes and for
4351  * presentation attributes, without any unescaping. (XML unescaping
4352  * (&amp; etc) is done by XML parser.)
4353  */
4354 gchar *
4355 attribute_unquote(gchar const *val)
4357     if (val) {
4358         if (*val == '\'' || *val == '"') {
4359             int l = strlen(val);
4360             if (l >= 2) {
4361                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4362                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4363                     return (g_strndup (val+1, l-2));
4364                 }
4365             }
4366         }
4367     }
4369     return (val? g_strdup (val) : NULL);
4372 /**
4373  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4374  */
4375 gchar *
4376 css2_escape_quote(gchar const *val) {
4378     Glib::ustring t;
4379     bool quote = false;
4380     bool last_was_space = false;
4382     for (gchar const *i = val; *i; i++) {
4383         bool is_space = ( *i == ' ' );
4384         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4385             // ASCII alphanumeric, - and _ don't require quotes
4386             t.push_back(*i);
4387         } else if ( is_space && !last_was_space ) {
4388             // non-consecutive spaces don't require quotes
4389             t.push_back(*i);
4390         } else if (*i=='\'') {
4391             // single quotes require escaping and quotes
4392             t.push_back('\\');
4393             t.push_back(*i);
4394             quote = true;
4395         } else {
4396             // everything else requires quotes
4397             t.push_back(*i);
4398             quote = true;
4399         }
4400         if (i == val && !g_ascii_isalpha(*i)) {
4401             // a non-ASCII/non-alpha initial character requires quotes
4402             quote = true;
4403         }
4404         last_was_space = is_space;
4405     }
4407     if (last_was_space) {
4408         // a trailing space requires quotes
4409         quote = true;
4410     }
4412     if (quote) {
4413         // we use single quotes so the result can be stored in an XML
4414         // attribute without incurring un-aesthetic additional quoting
4415         // (our XML emitter always uses double quotes)
4416         t.insert(t.begin(), '\'');
4417         t.push_back('\'');
4418     }
4420     return (t.empty() ? NULL : g_strdup (t.c_str()));
4423 /*
4424   Local Variables:
4425   mode:c++
4426   c-file-style:"stroustrup"
4427   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4428   indent-tabs-mode:nil
4429   fill-column:99
4430   End:
4431 */
4432 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :