Code

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