Code

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