Code

initial implementation of XML::Subtree API for tracking changes on a
[inkscape.git] / src / style.cpp
1 #define __SP_STYLE_C__
3 /** \file
4  * SVG stylesheets implementation.
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 2001-2002 Lauris Kaplinski
12  * Copyright (C) 2001 Ximian, Inc.
13  * Copyright (C) 2005 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include "libcroco/cr-sel-eng.h"
23 #include "xml/croco-node-iface.h"
25 #include "svg/svg.h"
26 #include "svg/svg-color.h"
27 #include "svg/svg-icc-color.h"
29 #include "display/canvas-bpath.h"
30 #include "attributes.h"
31 #include "document.h"
32 #include "extract-uri.h"
33 #include "uri-references.h"
34 #include "uri.h"
35 #include "sp-paint-server.h"
36 #include "streq.h"
37 #include "strneq.h"
38 #include "style.h"
39 #include "svg/css-ostringstream.h"
40 #include "xml/repr.h"
41 #include "unit-constants.h"
42 #include "isnan.h"
43 #include "macros.h"
45 #include "sp-filter-reference.h"
47 #include <sigc++/functors/ptr_fun.h>
48 #include <sigc++/adaptors/bind.h>
50 using Inkscape::CSSOStringStream;
51 using std::vector;
53 namespace Inkscape {
55 /**
56  * Parses a CSS url() specification; temporary hack until
57  * style stuff is redone.
58  * \param string the CSS string to parse
59  * \return a newly-allocated URL string (or NULL); free with g_free()
60  */
61 gchar *parse_css_url(gchar const *string) {
62     if (!string)
63         return NULL;
65     gchar const *iter = string;
66     for ( ; g_ascii_isspace(*iter) ; iter = g_utf8_next_char(iter) );
67     if (strncmp(iter, "url(", 4))
68         return NULL;
69     iter += 4;
71     gchar const end_char = *iter;
72     if ( *iter == '"' || *iter == '\'' ) {
73         iter += 1;
74     }
76     GString *temp = g_string_new(NULL);
77     for ( ; *iter ; iter = g_utf8_next_char(iter) ) {
78         if ( *iter == '(' || *iter == ')'  ||
79              *iter == '"' || *iter == '\'' ||
80              g_ascii_isspace(*iter)        ||
81              g_ascii_iscntrl(*iter)           )
82         {
83             break;
84         }
85         if ( *iter == '\\' ) {
86             iter = g_utf8_next_char(iter);
87         }
88         if ( *iter & (gchar)0x80 ) {
89             break;
90         } else {
91             g_string_append_c(temp, *iter);
92         }
93     }
95     if ( *iter == end_char && end_char != ')' ) {
96         iter = g_utf8_next_char(iter);
97     }
98     gchar *result;
99     if ( *iter == ')' ) {
100         result = temp->str;
101         g_string_free(temp, FALSE);
102     } else {
103         result = NULL;
104         g_string_free(temp, TRUE);
105     }
107     return result;
112 #define BMAX 8192
114 class SPStyleEnum;
116 /*#########################
117 ## FORWARD DECLARATIONS
118 #########################*/
119 static void sp_style_clear(SPStyle *style);
121 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
123 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
124 static void sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent);
125 static void sp_style_read_dash(SPStyle *style, gchar const *str);
127 static SPTextStyle *sp_text_style_new(void);
128 static void sp_text_style_clear(SPTextStyle *ts);
129 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
130 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
131 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
132 static void sp_style_privatize_text(SPStyle *style);
134 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
135 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
136 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
137 static void sp_style_read_istring(SPIString *val, gchar const *str);
138 static void sp_style_read_ilength(SPILength *val, gchar const *str);
139 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
140 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
141 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
142 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
143 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
144 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
146 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
147 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
148 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
149 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
151 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
152 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
153 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
154 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
155 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
156 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
157 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
158 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);
159 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);
160 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
162 static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint);
164 static void sp_style_filter_clear(SPStyle *style);
166 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
167 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
169 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
170 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
172 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
174 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
175 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
177 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
179 struct SPStyleEnum {
180     gchar const *key;
181     gint value;
182 };
184 static SPStyleEnum const enum_fill_rule[] = {
185     {"nonzero", SP_WIND_RULE_NONZERO},
186     {"evenodd", SP_WIND_RULE_EVENODD},
187     {NULL, -1}
188 };
190 static SPStyleEnum const enum_stroke_linecap[] = {
191     {"butt", SP_STROKE_LINECAP_BUTT},
192     {"round", SP_STROKE_LINECAP_ROUND},
193     {"square", SP_STROKE_LINECAP_SQUARE},
194     {NULL, -1}
195 };
197 static SPStyleEnum const enum_stroke_linejoin[] = {
198     {"miter", SP_STROKE_LINEJOIN_MITER},
199     {"round", SP_STROKE_LINEJOIN_ROUND},
200     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
201     {NULL, -1}
202 };
204 static SPStyleEnum const enum_font_style[] = {
205     {"normal", SP_CSS_FONT_STYLE_NORMAL},
206     {"italic", SP_CSS_FONT_STYLE_ITALIC},
207     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
208     {NULL, -1}
209 };
211 static SPStyleEnum const enum_font_size[] = {
212     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
213     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
214     {"small", SP_CSS_FONT_SIZE_SMALL},
215     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
216     {"large", SP_CSS_FONT_SIZE_LARGE},
217     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
218     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
219     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
220     {"larger", SP_CSS_FONT_SIZE_LARGER},
221     {NULL, -1}
222 };
224 static SPStyleEnum const enum_font_variant[] = {
225     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
226     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
227     {NULL, -1}
228 };
230 static SPStyleEnum const enum_font_weight[] = {
231     {"100", SP_CSS_FONT_WEIGHT_100},
232     {"200", SP_CSS_FONT_WEIGHT_200},
233     {"300", SP_CSS_FONT_WEIGHT_300},
234     {"400", SP_CSS_FONT_WEIGHT_400},
235     {"500", SP_CSS_FONT_WEIGHT_500},
236     {"600", SP_CSS_FONT_WEIGHT_600},
237     {"700", SP_CSS_FONT_WEIGHT_700},
238     {"800", SP_CSS_FONT_WEIGHT_800},
239     {"900", SP_CSS_FONT_WEIGHT_900},
240     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
241     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
242     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
243     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
244     {NULL, -1}
245 };
247 static SPStyleEnum const enum_font_stretch[] = {
248     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
249     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
250     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
251     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
252     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
253     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
254     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
255     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
256     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
257     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
258     {"wider", SP_CSS_FONT_STRETCH_WIDER},
259     {NULL, -1}
260 };
262 static SPStyleEnum const enum_text_align[] = {
263     {"start", SP_CSS_TEXT_ALIGN_START},
264     {"end", SP_CSS_TEXT_ALIGN_END},
265     {"left", SP_CSS_TEXT_ALIGN_LEFT},
266     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
267     {"center", SP_CSS_TEXT_ALIGN_CENTER},
268     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
269     {NULL, -1}
270 };
272 static SPStyleEnum const enum_text_transform[] = {
273     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
274     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
275     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
276     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
277     {NULL, -1}
278 };
280 static SPStyleEnum const enum_text_anchor[] = {
281     {"start", SP_CSS_TEXT_ANCHOR_START},
282     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
283     {"end", SP_CSS_TEXT_ANCHOR_END},
284     {NULL, -1}
285 };
287 static SPStyleEnum const enum_direction[] = {
288     {"ltr", SP_CSS_DIRECTION_LTR},
289     {"rtl", SP_CSS_DIRECTION_RTL},
290     {NULL, -1}
291 };
293 static SPStyleEnum const enum_block_progression[] = {
294     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
295     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
296     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
297     {NULL, -1}
298 };
300 static SPStyleEnum const enum_writing_mode[] = {
301     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
302      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
303      * value is lr-tb rather than lr.
304      *
305      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
306      * sharing enumerators for different strings may be a bug (once we support ecma script).
307      */
308     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
309     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
310     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
311     {"lr", SP_CSS_WRITING_MODE_LR_TB},
312     {"rl", SP_CSS_WRITING_MODE_RL_TB},
313     {"tb", SP_CSS_WRITING_MODE_TB_RL},
314     {NULL, -1}
315 };
317 static SPStyleEnum const enum_visibility[] = {
318     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
319     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
320     {"visible", SP_CSS_VISIBILITY_VISIBLE},
321     {NULL, -1}
322 };
324 static SPStyleEnum const enum_overflow[] = {
325     {"visible", SP_CSS_OVERFLOW_VISIBLE},
326     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
327     {"scroll", SP_CSS_OVERFLOW_SCROLL},
328     {"auto", SP_CSS_OVERFLOW_AUTO},
329     {NULL, -1}
330 };
332 static SPStyleEnum const enum_display[] = {
333     {"none",      SP_CSS_DISPLAY_NONE},
334     {"inline",    SP_CSS_DISPLAY_INLINE},
335     {"block",     SP_CSS_DISPLAY_BLOCK},
336     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
337     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
338     {"compact",   SP_CSS_DISPLAY_COMPACT},
339     {"marker",    SP_CSS_DISPLAY_MARKER},
340     {"table",     SP_CSS_DISPLAY_TABLE},
341     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
342     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
343     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
344     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
345     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
346     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
347     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
348     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
349     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
350     {NULL, -1}
351 };
353 static SPStyleEnum const enum_shape_rendering[] = {
354     {"auto", 0},
355     {"optimizeSpeed", 0},
356     {"crispEdges", 0},
357     {"geometricPrecision", 0},
358     {NULL, -1}
359 };
361 static SPStyleEnum const enum_color_rendering[] = {
362     {"auto", 0},
363     {"optimizeSpeed", 0},
364     {"optimizeQuality", 0},
365     {NULL, -1}
366 };
368 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
370 static SPStyleEnum const enum_text_rendering[] = {
371     {"auto", 0},
372     {"optimizeSpeed", 0},
373     {"optimizeLegibility", 0},
374     {"geometricPrecision", 0},
375     {NULL, -1}
376 };
378 static SPStyleEnum const enum_enable_background[] = {
379     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
380     {"new", SP_CSS_BACKGROUND_NEW},
381     {NULL, -1}
382 };
384 /**
385  * Release callback.
386  */
387 static void
388 sp_style_object_release(SPObject *object, SPStyle *style)
390     style->object = NULL;
393 /**
394  * Emit style modified signal on style's object if the filter changed.
395  */
396 static void
397 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
399     SPFilter *filter=static_cast<SPFilter *>(obj);
400     if (style->getFilter() == filter)
401     {
402         if (style->object) {
403             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
404         }
405     }
408 /**
409  * Gets called when the filter is (re)attached to the style
410  */
411 static void
412 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
414     if (old_ref) {
415         sp_signal_disconnect_by_data(old_ref, style);
416     }
417     if ( SP_IS_FILTER(ref))
418     {
419         ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
420     }
422     sp_style_filter_ref_modified(ref, 0, style);
426 /**
427  * Returns a new SPStyle object with settings as per sp_style_clear().
428  */
429 SPStyle *
430 sp_style_new(SPDocument *document)
432     SPStyle *const style = g_new0(SPStyle, 1);
434     style->refcount = 1;
435     style->object = NULL;
436     style->text = sp_text_style_new();
437     style->text_private = TRUE;
439     if (document) {
440         style->filter.href = new SPFilterReference(document);
441         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
442     }
444     sp_style_clear(style);
446     style->cloned = false;
448     style->fill_hreffed = false;
449     style->stroke_hreffed = false;
451     new (&style->release_connection) sigc::connection();
453     new (&style->fill_release_connection) sigc::connection();
454     new (&style->fill_modified_connection) sigc::connection();
456     new (&style->stroke_release_connection) sigc::connection();
457     new (&style->stroke_modified_connection) sigc::connection();
459     return style;
463 /**
464  * Creates a new SPStyle object, and attaches it to the specified SPObject.
465  */
466 SPStyle *
467 sp_style_new_from_object(SPObject *object)
469     g_return_val_if_fail(object != NULL, NULL);
470     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
472     SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
473     style->object = object;
474     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
476     if (object && SP_OBJECT_IS_CLONED(object)) {
477         style->cloned = true;
478     }
480     return style;
484 /**
485  * Increase refcount of style.
486  */
487 SPStyle *
488 sp_style_ref(SPStyle *style)
490     g_return_val_if_fail(style != NULL, NULL);
491     g_return_val_if_fail(style->refcount > 0, NULL);
493     style->refcount += 1;
495     return style;
499 /**
500  * Decrease refcount of style with possible destruction.
501  */
502 SPStyle *
503 sp_style_unref(SPStyle *style)
505     g_return_val_if_fail(style != NULL, NULL);
506     g_return_val_if_fail(style->refcount > 0, NULL);
508     style->refcount -= 1;
510     if (style->refcount < 1) {
511         style->release_connection.disconnect();
512         style->release_connection.~connection();
513         if (style->text) sp_text_style_unref(style->text);
514         sp_style_paint_clear(style, &style->fill);
515         sp_style_paint_clear(style, &style->stroke);
516         sp_style_filter_clear(style);
517         style->fill_release_connection.disconnect();
518         style->fill_release_connection.~connection();
519         style->fill_modified_connection.disconnect();
520         style->fill_modified_connection.~connection();
521         style->stroke_release_connection.disconnect();
522         style->stroke_release_connection.~connection();
523         style->stroke_modified_connection.disconnect();
524         style->stroke_modified_connection.~connection();
525         g_free(style->stroke_dash.dash);
526         g_free(style);
527     }
529     return NULL;
532 /**
533  *  Reads the various style parameters for an object from repr.
534  */
535 static void
536 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
538     g_assert(style != NULL);
539     g_assert(repr != NULL);
540     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
542     sp_style_clear(style);
544     if (object && SP_OBJECT_IS_CLONED(object)) {
545         style->cloned = true;
546     }
548     /* 1. Style attribute */
549     gchar const *val = repr->attribute("style");
550     if (val != NULL) {
551         sp_style_merge_from_style_string(style, val);
552     }
554     if (object) {
555         sp_style_merge_from_object_stylesheet(style, object);
556     } else {
557         /** \todo No stylesheet information. Find out under what circumstances
558          * this occurs, and handle accordingly.  (If we really wanted to, we
559          * could probably get stylesheets by going through repr->doc.)
560          */
561     }
563     /* 2. Presentation attributes */
564     /* CSS2 */
565     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
566     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
567     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
568     /* Font */
569     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
570     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
571     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
572     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
573     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
574     /* Text (css2 chapter 16) */
575     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
576     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
577     if (!style->text_decoration.set) {
578         val = repr->attribute("text-decoration");
579         if (val) {
580             sp_style_read_itextdecoration(&style->text_decoration, val);
581         }
582     }
583     if (!style->line_height.set) {
584         val = repr->attribute("line-height");
585         if (val) {
586             sp_style_read_ilengthornormal(&style->line_height, val);
587         }
588     }
589     if (!style->letter_spacing.set) {
590         val = repr->attribute("letter-spacing");
591         if (val) {
592             sp_style_read_ilengthornormal(&style->letter_spacing, val);
593         }
594     }
595     if (!style->word_spacing.set) {
596         val = repr->attribute("word-spacing");
597         if (val) {
598             sp_style_read_ilengthornormal(&style->word_spacing, val);
599         }
600     }
601     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
602     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
603     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
605     /* SVG */
606     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
607                             enum_writing_mode, true);
608     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
609                             enum_text_anchor, true);
611     /* opacity */
612     if (!style->opacity.set) {
613         val = repr->attribute("opacity");
614         if (val) {
615             sp_style_read_iscale24(&style->opacity, val);
616         }
617     }
618     /* color */
619     if (!style->color.set) {
620         val = repr->attribute("color");
621         if (val) {
622             sp_style_read_icolor(&style->color, val, style, ( object
623                                                               ? SP_OBJECT_DOCUMENT(object)
624                                                               : NULL ));
625         }
626     }
627     /* fill */
628     if (!style->fill.set) {
629         val = repr->attribute("fill");
630         if (val) {
631             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
632         }
633     }
634     /* fill-opacity */
635     if (!style->fill_opacity.set) {
636         val = repr->attribute("fill-opacity");
637         if (val) {
638             sp_style_read_iscale24(&style->fill_opacity, val);
639         }
640     }
641     /* fill-rule */
642     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
643     /* stroke */
644     if (!style->stroke.set) {
645         val = repr->attribute("stroke");
646         if (val) {
647             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
648         }
649     }
650     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
651     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
652     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
653     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
655     /* markers */
656     if (!style->marker[SP_MARKER_LOC].set) {
657         val = repr->attribute("marker");
658         if (val) {
659             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
660         }
661     }
662     if (!style->marker[SP_MARKER_LOC_START].set) {
663         val = repr->attribute("marker-start");
664         if (val) {
665             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
666         }
667     }
668     if (!style->marker[SP_MARKER_LOC_MID].set) {
669         val = repr->attribute("marker-mid");
670         if (val) {
671             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
672         }
673     }
674     if (!style->marker[SP_MARKER_LOC_END].set) {
675         val = repr->attribute("marker-end");
676         if (val) {
677             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
678         }
679     }
681     /* stroke-opacity */
682     if (!style->stroke_opacity.set) {
683         val = repr->attribute("stroke-opacity");
684         if (val) {
685             sp_style_read_iscale24(&style->stroke_opacity, val);
686         }
687     }
688     if (!style->stroke_dasharray_set) {
689         val = repr->attribute("stroke-dasharray");
690         if (val) {
691             sp_style_read_dash(style, val);
692         }
693     }
694     if (!style->stroke_dashoffset_set) {
695         /* fixme */
696         val = repr->attribute("stroke-dashoffset");
697         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
698             style->stroke_dashoffset_set = TRUE;
699         } else {
700             style->stroke_dashoffset_set = FALSE;
701         }
702     }
704     /* font-family */
705     if (!style->text_private || !style->text->font_family.set) {
706         val = repr->attribute("font-family");
707         if (val) {
708             if (!style->text_private) sp_style_privatize_text(style);
709             gchar *val_unquoted = attribute_unquote(val);
710             sp_style_read_istring(&style->text->font_family, val_unquoted);
711             if (val_unquoted) g_free (val_unquoted);
712         }
713     }
715     /* filter effects */
716     if (!style->filter.set) {
717         val = repr->attribute("filter");
718         if (val) {
719                 sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
720         }
721     }
722     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
723                             "enable-background", enum_enable_background, true);
725     /* 3. Merge from parent */
726     if (object) {
727         if (object->parent) {
728             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
729         }
730     } else {
731         if (sp_repr_parent(repr)) {
732             /// \todo fixme: This is not the prettiest thing (Lauris)
733             SPStyle *parent = sp_style_new(NULL);
734             sp_style_read(parent, NULL, sp_repr_parent(repr));
735             sp_style_merge_from_parent(style, parent);
736             sp_style_unref(parent);
737         }
738     }
742 /**
743  * Read style properties from object's repr.
744  *
745  * 1. Reset existing object style
746  * 2. Load current effective object style
747  * 3. Load i attributes from immediate parent (which has to be up-to-date)
748  */
749 void
750 sp_style_read_from_object(SPStyle *style, SPObject *object)
752     g_return_if_fail(style != NULL);
753     g_return_if_fail(object != NULL);
754     g_return_if_fail(SP_IS_OBJECT(object));
756     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
757     g_return_if_fail(repr != NULL);
759     sp_style_read(style, object, repr);
763 /**
764  * Read style properties from repr only.
765  */
766 void
767 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
769     g_return_if_fail(style != NULL);
770     g_return_if_fail(repr != NULL);
772     sp_style_read(style, NULL, repr);
777 /**
778  *
779  */
780 static void
781 sp_style_privatize_text(SPStyle *style)
783     SPTextStyle *text = style->text;
784     style->text = sp_text_style_duplicate_unset(style->text);
785     sp_text_style_unref(text);
786     style->text_private = TRUE;
790 /**
791  * Merge property into style.
792  *
793  * Should be called in order of highest to lowest precedence.
794  * E.g. for a single style string, call from the last declaration to the first,
795  * as CSS says that later declarations override earlier ones.
796  *
797  * \pre val != NULL.
798  */
799 static void
800 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
802     g_return_if_fail(val != NULL);
804     switch (id) {
805         /* CSS2 */
806         /* Font */
807         case SP_PROP_FONT_FAMILY:
808             if (!style->text_private) sp_style_privatize_text(style);
809             if (!style->text->font_family.set) {
810                 gchar *val_unquoted = attribute_unquote(val);
811                 sp_style_read_istring(&style->text->font_family, val_unquoted);
812                 if (val_unquoted) g_free (val_unquoted);
813             }
814             break;
815         case SP_PROP_FONT_SIZE:
816             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
817             break;
818         case SP_PROP_FONT_SIZE_ADJUST:
819             if (strcmp(val, "none") != 0) {
820                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
821             }
822             break;
823         case SP_PROP_FONT_STYLE:
824             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
825             break;
826         case SP_PROP_FONT_VARIANT:
827             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
828             break;
829         case SP_PROP_FONT_WEIGHT:
830             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
831             break;
832         case SP_PROP_FONT_STRETCH:
833             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
834             break;
835         case SP_PROP_FONT:
836             if (!style->text_private) sp_style_privatize_text(style);
837             if (!style->text->font.set) {
838                 g_free(style->text->font.value);
839                 style->text->font.value = g_strdup(val);
840                 style->text->font.set = TRUE;
841                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
842             }
843             break;
844             /* Text */
845         case SP_PROP_TEXT_INDENT:
846             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
847             break;
848         case SP_PROP_TEXT_ALIGN:
849             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
850             break;
851         case SP_PROP_TEXT_DECORATION:
852             if (!style->text_decoration.set) {
853                 sp_style_read_itextdecoration(&style->text_decoration, val);
854             }
855             break;
856         case SP_PROP_LINE_HEIGHT:
857             if (!style->line_height.set) {
858                 sp_style_read_ilengthornormal(&style->line_height, val);
859             }
860             break;
861         case SP_PROP_LETTER_SPACING:
862             if (!style->letter_spacing.set) {
863                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
864             }
865             break;
866         case SP_PROP_WORD_SPACING:
867             if (!style->word_spacing.set) {
868                 sp_style_read_ilengthornormal(&style->word_spacing, val);
869             }
870             break;
871         case SP_PROP_TEXT_TRANSFORM:
872             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
873             break;
874             /* Text (css3) */
875         case SP_PROP_DIRECTION:
876             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
877             break;
878         case SP_PROP_BLOCK_PROGRESSION:
879             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
880             break;
881         case SP_PROP_WRITING_MODE:
882             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
883             break;
884         case SP_PROP_TEXT_ANCHOR:
885             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
886             break;
887             /* Text (unimplemented) */
888         case SP_PROP_TEXT_RENDERING: {
889             /* Ignore the hint. */
890             SPIEnum dummy;
891             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
892             break;
893         }
894         case SP_PROP_ALIGNMENT_BASELINE:
895             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
896             break;
897         case SP_PROP_BASELINE_SHIFT:
898             g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
899             break;
900         case SP_PROP_DOMINANT_BASELINE:
901             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
902             break;
903         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
904             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
905             break;
906         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
907             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
908             break;
909         case SP_PROP_KERNING:
910             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
911             break;
912             /* Misc */
913         case SP_PROP_CLIP:
914             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
915             break;
916         case SP_PROP_COLOR:
917             if (!style->color.set) {
918                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
919             }
920             break;
921         case SP_PROP_CURSOR:
922             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
923             break;
924         case SP_PROP_DISPLAY:
925             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
926             break;
927         case SP_PROP_OVERFLOW:
928             /** \todo
929              * FIXME: not supported properly yet, we just read and write it,
930              * but act as if it is always "display".
931              */
932             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
933             break;
934         case SP_PROP_VISIBILITY:
935             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
936             break;
937             /* SVG */
938             /* Clip/Mask */
939         case SP_PROP_CLIP_PATH:
940             g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
941             break;
942         case SP_PROP_CLIP_RULE:
943             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
944             break;
945         case SP_PROP_MASK:
946             g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
947             break;
948         case SP_PROP_OPACITY:
949             if (!style->opacity.set) {
950                 sp_style_read_iscale24(&style->opacity, val);
951             }
952             break;
953         case SP_PROP_ENABLE_BACKGROUND:
954             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
955                                     enum_enable_background, true);
956             break;
957             /* Filter */
958         case SP_PROP_FILTER:
959             if (!style->filter.set && !style->filter.inherit) {
960                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
961             }
962             break;
963         case SP_PROP_FLOOD_COLOR:
964             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
965             break;
966         case SP_PROP_FLOOD_OPACITY:
967             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
968             break;
969         case SP_PROP_LIGHTING_COLOR:
970             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
971             break;
972             /* Gradient */
973         case SP_PROP_STOP_COLOR:
974             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
975             break;
976         case SP_PROP_STOP_OPACITY:
977             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
978             break;
979             /* Interactivity */
980         case SP_PROP_POINTER_EVENTS:
981             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
982             break;
983             /* Paint */
984         case SP_PROP_COLOR_INTERPOLATION:
985             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
986             break;
987         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
988             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
989             break;
990         case SP_PROP_COLOR_PROFILE:
991             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
992             break;
993         case SP_PROP_COLOR_RENDERING: {
994             /* Ignore the hint. */
995             SPIEnum dummy;
996             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
997             break;
998         }
999         case SP_PROP_FILL:
1000             if (!style->fill.set) {
1001                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1002             }
1003             break;
1004         case SP_PROP_FILL_OPACITY:
1005             if (!style->fill_opacity.set) {
1006                 sp_style_read_iscale24(&style->fill_opacity, val);
1007             }
1008             break;
1009         case SP_PROP_FILL_RULE:
1010             if (!style->fill_rule.set) {
1011                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1012             }
1013             break;
1014         case SP_PROP_IMAGE_RENDERING: {
1015             /* Ignore the hint. */
1016             SPIEnum dummy;
1017             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1018             break;
1019         }
1020         case SP_PROP_MARKER:
1021             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1022             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1023             if (!style->marker[SP_MARKER_LOC].set) {
1024                 g_free(style->marker[SP_MARKER_LOC].value);
1025                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1026                 style->marker[SP_MARKER_LOC].set = TRUE;
1027                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1028             }
1029             break;
1031         case SP_PROP_MARKER_START:
1032             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1033             if (!style->marker[SP_MARKER_LOC_START].set) {
1034                 g_free(style->marker[SP_MARKER_LOC_START].value);
1035                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1036                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1037                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1038             }
1039             break;
1040         case SP_PROP_MARKER_MID:
1041             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1042             if (!style->marker[SP_MARKER_LOC_MID].set) {
1043                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1044                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1045                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1046                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1047             }
1048             break;
1049         case SP_PROP_MARKER_END:
1050             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1051             if (!style->marker[SP_MARKER_LOC_END].set) {
1052                 g_free(style->marker[SP_MARKER_LOC_END].value);
1053                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1054                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1055                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1056             }
1057             break;
1059         case SP_PROP_SHAPE_RENDERING: {
1060             /* Ignore the hint. */
1061             SPIEnum dummy;
1062             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1063             break;
1064         }
1066         case SP_PROP_STROKE:
1067             if (!style->stroke.set) {
1068                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1069             }
1070             break;
1071         case SP_PROP_STROKE_WIDTH:
1072             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1073             break;
1074         case SP_PROP_STROKE_DASHARRAY:
1075             if (!style->stroke_dasharray_set) {
1076                 sp_style_read_dash(style, val);
1077             }
1078             break;
1079         case SP_PROP_STROKE_DASHOFFSET:
1080             if (!style->stroke_dashoffset_set) {
1081                 /* fixme */
1082                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1083                     style->stroke_dashoffset_set = TRUE;
1084                 } else {
1085                     style->stroke_dashoffset_set = FALSE;
1086                 }
1087             }
1088             break;
1089         case SP_PROP_STROKE_LINECAP:
1090             if (!style->stroke_linecap.set) {
1091                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1092             }
1093             break;
1094         case SP_PROP_STROKE_LINEJOIN:
1095             if (!style->stroke_linejoin.set) {
1096                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1097             }
1098             break;
1099         case SP_PROP_STROKE_MITERLIMIT:
1100             if (!style->stroke_miterlimit.set) {
1101                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1102             }
1103             break;
1104         case SP_PROP_STROKE_OPACITY:
1105             if (!style->stroke_opacity.set) {
1106                 sp_style_read_iscale24(&style->stroke_opacity, val);
1107             }
1108             break;
1110         default:
1111             g_warning("Invalid style property id: %d value: %s", id, val);
1112             break;
1113     }
1116 static void
1117 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1119     /** \todo Ensure that property is lcased, as per
1120      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1121      * Should probably be done in libcroco.
1122      */
1123     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1124     if (prop_idx != SP_ATTR_INVALID) {
1125         /** \todo
1126          * effic: Test whether the property is already set before trying to
1127          * convert to string. Alternatively, set from CRTerm directly rather
1128          * than converting to string.
1129          */
1130         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1131         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1132         sp_style_merge_property(style, prop_idx, str_value);
1133         g_free(str_value);
1134     }
1137 static void
1138 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1140 #if 0 /* forwards */
1141     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1142         CRDeclaration *decl = NULL;
1143         cr_prop_list_get_decl(cur, &decl);
1144         sp_style_merge_style_from_decl(style, decl);
1145     }
1146 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1147     if (props) {
1148         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1149         CRDeclaration *decl = NULL;
1150         cr_prop_list_get_decl(props, &decl);
1151         sp_style_merge_style_from_decl(style, decl);
1152     }
1153 #endif
1156 /**
1157  * \pre decl_list != NULL
1158  */
1159 static void
1160 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1162     // read the decls from end to start, using head recursion, so that latter declarations override
1163     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1164     // because sp_style_merge_style_from_decl only sets properties that are unset
1165     if (decl_list->next) {
1166         sp_style_merge_from_decl_list(style, decl_list->next);
1167     }
1168     sp_style_merge_style_from_decl(style, decl_list);
1171 static CRSelEng *
1172 sp_repr_sel_eng()
1174     CRSelEng *const ret = cr_sel_eng_new();
1175     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1177     /** \todo
1178      * Check whether we need to register any pseudo-class handlers.
1179      * libcroco has its own default handlers for first-child and lang.
1180      *
1181      * We probably want handlers for link and arguably visited (though
1182      * inkscape can't visit links at the time of writing).  hover etc.
1183      * more useful in inkview than the editor inkscape.
1184      *
1185      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1186      * the following should be honoured, at least by inkview:
1187      * :hover, :active, :focus, :visited, :link.
1188      */
1190     g_assert(ret);
1191     return ret;
1194 static void
1195 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1197     static CRSelEng *sel_eng = NULL;
1198     if (!sel_eng) {
1199         sel_eng = sp_repr_sel_eng();
1200     }
1202     CRPropList *props = NULL;
1203     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1204                                                                      object->document->style_cascade,
1205                                                                      object->repr,
1206                                                                      &props);
1207     g_return_if_fail(status == CR_OK);
1208     /// \todo Check what errors can occur, and handle them properly.
1209     if (props) {
1210         sp_style_merge_from_props(style, props);
1211         cr_prop_list_destroy(props);
1212     }
1215 /**
1216  * Parses a style="..." string and merges it with an existing SPStyle.
1217  */
1218 void
1219 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1221     /*
1222      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1223      * ``When CSS styling is used, CSS inline style is specified by including
1224      * semicolon-separated property declarations of the form "name : value"
1225      * within the style attribute''.
1226      *
1227      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1228      * Why does it say "including", what else is allowed in the style
1229      * attribute value?
1230      */
1232     CRDeclaration *const decl_list
1233         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1234     if (decl_list) {
1235         sp_style_merge_from_decl_list(style, decl_list);
1236         cr_declaration_destroy(decl_list);
1237     }
1240 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1241 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1243 static void
1244 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1246     /* 'font-size' */
1247     if (!child.set || child.inherit) {
1248         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1249         child.computed = parent.computed;
1250     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1251         /** \todo
1252          * fixme: SVG and CSS do not specify clearly, whether we should use
1253          * user or screen coordinates (Lauris)
1254          */
1255         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1256             child.computed = font_size_table[child.value];
1257         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1258             child.computed = parent.computed / 1.2;
1259         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1260             child.computed = parent.computed * 1.2;
1261         } else {
1262             /* Illegal value */
1263         }
1264     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1265         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1266          * rather than viewport. */
1267         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1268     }
1271 /**
1272  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1273  * calculating from) corresponding computed values of \a parent.
1274  *
1275  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1276  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1277  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1278  * about what the computed value is (not obvious for lengths).
1279  *
1280  * \pre \a parent's computed values are already up-to-date.
1281  */
1282 void
1283 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1285     g_return_if_fail(style != NULL);
1287     /** \todo
1288      * fixme: Check for existing callers that might pass null parent.
1289      * This should probably be g_return_if_fail, or else we should make a
1290      * best attempt to set computed values correctly without having a parent
1291      * (i.e., by assuming parent has initial values).
1292      */
1293     if (!parent)
1294         return;
1296     /* CSS2 */
1297     /* Font */
1298     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1300     /* 'font-style' */
1301     if (!style->font_style.set || style->font_style.inherit) {
1302         style->font_style.computed = parent->font_style.computed;
1303     }
1305     /* 'font-variant' */
1306     if (!style->font_variant.set || style->font_variant.inherit) {
1307         style->font_variant.computed = parent->font_variant.computed;
1308     }
1310     /* 'font-weight' */
1311     if (!style->font_weight.set || style->font_weight.inherit) {
1312         style->font_weight.computed = parent->font_weight.computed;
1313     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1314         /** \todo
1315          * fixme: This is unconditional, i.e., happens even if parent not
1316          * present.
1317          */
1318         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1319     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1320         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1321     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1322         unsigned const parent_val = parent->font_weight.computed;
1323         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1324         // strictly, 'bolder' and 'lighter' should go to the next weight
1325         // expressible in the current font family, but that's difficult to
1326         // find out, so jumping by 3 seems an appropriate approximation
1327         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1328                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1329                                        : parent_val - 3);
1330         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1331     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1332         unsigned const parent_val = parent->font_weight.computed;
1333         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1334         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1335                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1336                                        : parent_val + 3);
1337         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1338     }
1340     /* 'font-stretch' */
1341     if (!style->font_stretch.set || style->font_stretch.inherit) {
1342         style->font_stretch.computed = parent->font_stretch.computed;
1343     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1344         unsigned const parent_val = parent->font_stretch.computed;
1345         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1346                                         ? parent_val
1347                                         : parent_val - 1);
1348         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1349     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1350         unsigned const parent_val = parent->font_stretch.computed;
1351         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1352         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1353                                         ? parent_val
1354                                         : parent_val + 1);
1355         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1356     }
1358     /* text (css2) */
1359     if (!style->text_indent.set || style->text_indent.inherit) {
1360         style->text_indent.computed = parent->text_indent.computed;
1361     }
1363     if (!style->text_align.set || style->text_align.inherit) {
1364         style->text_align.computed = parent->text_align.computed;
1365     }
1367     if (!style->text_decoration.set || style->text_decoration.inherit) {
1368         style->text_decoration.underline = parent->text_decoration.underline;
1369         style->text_decoration.overline = parent->text_decoration.overline;
1370         style->text_decoration.line_through = parent->text_decoration.line_through;
1371         style->text_decoration.blink = parent->text_decoration.blink;
1372     }
1374     if (!style->line_height.set || style->line_height.inherit) {
1375         style->line_height.computed = parent->line_height.computed;
1376         style->line_height.normal = parent->line_height.normal;
1377     }
1379     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1380         style->letter_spacing.computed = parent->letter_spacing.computed;
1381         style->letter_spacing.normal = parent->letter_spacing.normal;
1382     }
1384     if (!style->word_spacing.set || style->word_spacing.inherit) {
1385         style->word_spacing.computed = parent->word_spacing.computed;
1386         style->word_spacing.normal = parent->word_spacing.normal;
1387     }
1389     if (!style->text_transform.set || style->text_transform.inherit) {
1390         style->text_transform.computed = parent->text_transform.computed;
1391     }
1393     if (!style->direction.set || style->direction.inherit) {
1394         style->direction.computed = parent->direction.computed;
1395     }
1397     if (!style->block_progression.set || style->block_progression.inherit) {
1398         style->block_progression.computed = parent->block_progression.computed;
1399     }
1401     if (!style->writing_mode.set || style->writing_mode.inherit) {
1402         style->writing_mode.computed = parent->writing_mode.computed;
1403     }
1405     if (!style->text_anchor.set || style->text_anchor.inherit) {
1406         style->text_anchor.computed = parent->text_anchor.computed;
1407     }
1409     if (style->opacity.inherit) {
1410         style->opacity.value = parent->opacity.value;
1411     }
1413     /* Color */
1414     if (!style->color.set || style->color.inherit) {
1415         sp_style_merge_ipaint(style, &style->color, &parent->color);
1416     }
1418     /* Fill */
1419     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1420         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1421     }
1423     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1424         style->fill_opacity.value = parent->fill_opacity.value;
1425     }
1427     if (!style->fill_rule.set || style->fill_rule.inherit) {
1428         style->fill_rule.computed = parent->fill_rule.computed;
1429     }
1431     /* Stroke */
1432     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1433         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1434     }
1436     if (!style->stroke_width.set || style->stroke_width.inherit) {
1437         style->stroke_width.computed = parent->stroke_width.computed;
1438     } else {
1439         /* Update computed value for any change in font inherited from parent. */
1440         double const em = style->font_size.computed;
1441         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1442             style->stroke_width.computed = style->stroke_width.value * em;
1443         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1444             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1445             style->stroke_width.computed = style->stroke_width.value * ex;
1446         }
1447     }
1449     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1450         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1451     }
1453     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1454         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1455     }
1457     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1458         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1459     }
1461     if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1462         /** \todo
1463          * This code looks wrong.  Why does the logic differ from the
1464          * above properties? Similarly dashoffset below.
1465          */
1466         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1467         if (style->stroke_dash.n_dash > 0) {
1468             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1469             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1470         }
1471     }
1473     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1474         style->stroke_dash.offset = parent->stroke_dash.offset;
1475     }
1477     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1478         style->stroke_opacity.value = parent->stroke_opacity.value;
1479     }
1481     if (style->text && parent->text) {
1482         if (!style->text->font_family.set || style->text->font_family.inherit) {
1483             g_free(style->text->font_family.value);
1484             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1485         }
1486     }
1488     /* Markers - Free the old value and make copy of the new */
1489     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1490         if (!style->marker[i].set || style->marker[i].inherit) {
1491             g_free(style->marker[i].value);
1492             style->marker[i].value = g_strdup(parent->marker[i].value);
1493         }
1494     }
1496     /* Filter effects */
1497     if (style->filter.inherit) {
1498         sp_style_merge_ifilter(style, &parent->filter);
1499     }
1501     if(style->enable_background.inherit) {
1502         style->enable_background.value = parent->enable_background.value;
1503     }
1506 template <typename T>
1507 static void
1508 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1510     if ( ( !(child.set) || child.inherit )
1511          && parent.set && !(parent.inherit) )
1512     {
1513         child = parent;
1514     }
1517 /**
1518  * Copy SPIString from parent to child.
1519  */
1520 static void
1521 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1523     if ( ( !(child.set) || child.inherit )
1524          && parent.set && !(parent.inherit) )
1525     {
1526         g_free(child.value);
1527         child.value = g_strdup(parent.value);
1528         child.set = parent.set;
1529         child.inherit = parent.inherit;
1530     }
1533 static void
1534 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1535                                             SPIPaint &child, SPIPaint const &parent)
1537     /** \todo
1538      * I haven't given this much attention.  See comments below about
1539      * currentColor, colorProfile, and relative URIs.
1540      */
1541     if (!child.set || child.inherit || child.currentcolor) {
1542         sp_style_merge_ipaint(style, &child, &parent);
1543         child.set = parent.set;
1544         child.inherit = parent.inherit;
1545     }
1548 static void
1549 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1550                                                unsigned const max_computed_val,
1551                                                unsigned const smaller_val)
1553     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1554        then zero or more other absolute values, then smaller_val then larger_val. */
1555     unsigned const min_computed_val = 0;
1556     unsigned const larger_val = smaller_val + 1;
1557     g_return_if_fail(min_computed_val < max_computed_val);
1558     g_return_if_fail(max_computed_val < smaller_val);
1560     if (parent.set && !parent.inherit) {
1561         if (!child.set || child.inherit) {
1562             child.value = parent.value;
1563             child.set = parent.set;  // i.e. true
1564             child.inherit = parent.inherit;  // i.e. false
1565         } else if (child.value < smaller_val) {
1566             /* Child has absolute value: leave as is. */
1567         } else if ( ( child.value == smaller_val
1568                       && parent.value == larger_val )
1569                     || ( parent.value == smaller_val
1570                          && child.value == larger_val ) )
1571         {
1572             child.set = false;
1573             /*
1574              * Note that this can result in a change in computed value in the
1575              * rare case that the parent's setting was a no-op (i.e. if the
1576              * parent's parent's computed value was already ultra-condensed or
1577              * ultra-expanded).  However, I'd guess that the change is for the
1578              * better: I'd guess that if the properties were specified
1579              * relatively, then the intent is to counteract parent's effect.
1580              * I.e. I believe this is the best code even in that case.
1581              */
1582         } else if (child.value == parent.value) {
1583             /* Leave as is. */
1584             /** \todo
1585              * It's unclear what to do if style and parent specify the same
1586              * relative directive (narrower or wider).  We can either convert
1587              * to absolute specification or coalesce to a single relative
1588              * request (of half the strength of the original pair).
1589              *
1590              * Converting to a single level of relative specification is a
1591              * better choice if the newly-unlinked clone is itself cloned to
1592              * other contexts (inheriting different font stretchiness): it
1593              * would preserve the effect that it will be narrower than
1594              * the inherited context in each case.  The disadvantage is that
1595              * it will ~certainly affect the computed value of the
1596              * newly-unlinked clone.
1597              */
1598         } else {
1599             unsigned const parent_val = parent.computed;
1600             child.value = ( child.value == smaller_val
1601                             ? ( parent_val == min_computed_val
1602                                 ? parent_val
1603                                 : parent_val - 1 )
1604                             : ( parent_val == max_computed_val
1605                                 ? parent_val
1606                                 : parent_val + 1 ) );
1607             g_assert(child.value <= max_computed_val);
1608             child.inherit = false;
1609             g_assert(child.set);
1610         }
1611     }
1614 template <typename LengthT>
1615 static void
1616 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1617                                              double const parent_child_em_ratio)
1619     if ( ( !(child.set) || child.inherit )
1620          && parent.set && !(parent.inherit) )
1621     {
1622         child = parent;
1623         switch (parent.unit) {
1624             case SP_CSS_UNIT_EM:
1625             case SP_CSS_UNIT_EX:
1626                 child.value *= parent_child_em_ratio;
1627                 /** \todo
1628                  * fixme: Have separate ex ratio parameter.
1629                  * Get x height from libnrtype or pango.
1630                  */
1631                 if (!isFinite(child.value)) {
1632                     child.value = child.computed;
1633                     child.unit = SP_CSS_UNIT_NONE;
1634                 }
1635                 break;
1637             default:
1638                 break;
1639         }
1640     }
1643 static double
1644 get_relative_font_size_frac(SPIFontSize const &font_size)
1646     switch (font_size.type) {
1647         case SP_FONT_SIZE_LITERAL: {
1648             switch (font_size.value) {
1649                 case SP_CSS_FONT_SIZE_SMALLER:
1650                     return 5.0 / 6.0;
1652                 case SP_CSS_FONT_SIZE_LARGER:
1653                     return 6.0 / 5.0;
1655                 default:
1656                     g_assert_not_reached();
1657             }
1658         }
1660         case SP_FONT_SIZE_PERCENTAGE:
1661             return SP_F8_16_TO_FLOAT(font_size.value);
1663         case SP_FONT_SIZE_LENGTH:
1664             g_assert_not_reached();
1665     }
1666     g_assert_not_reached();
1669 /**
1670  * Combine \a style and \a parent style specifications into a single style specification that
1671  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1672  *
1673  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1674  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1675  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1676  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1677  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1678  *
1679  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1680  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1681  * style was previously up-to-date wrt \a parent.
1682  *
1683  * \pre \a parent's computed values are already up-to-date.
1684  *   (\a style's computed values needn't be up-to-date.)
1685  */
1686 void
1687 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1689     /** \note
1690      * The general rule for each property is as follows:
1691      *
1692      *   If style is set to an absolute value, then leave it as is.
1693      *
1694      *   Otherwise (i.e. if style has a relative value):
1695      *
1696      *     If parent is set to an absolute value, then set style to the computed value.
1697      *
1698      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1699      */
1701     /* We do font-size first, to ensure that em size is up-to-date. */
1702     /** \todo
1703      * fixme: We'll need to have more font-related things up the top once
1704      * we're getting x-height from pango or libnrtype.
1705      */
1707     /* Some things that allow relative specifications. */
1708     {
1709         /* font-size.  Note that we update the computed font-size of style,
1710            to assist in em calculations later in this function. */
1711         if (parent->font_size.set && !parent->font_size.inherit) {
1712             if (!style->font_size.set || style->font_size.inherit) {
1713                 /* font_size inherits the computed value, so we can use the parent value
1714                  * verbatim. */
1715                 style->font_size = parent->font_size;
1716             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1717                 /* Child already has absolute size (stored in computed value), so do nothing. */
1718             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1719                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1720                 /* Child already has absolute size, but we ensure that the computed value
1721                    is up-to-date. */
1722                 unsigned const ix = style->font_size.value;
1723                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1724                 style->font_size.computed = font_size_table[ix];
1725             } else {
1726                 /* Child has relative size. */
1727                 double const child_frac(get_relative_font_size_frac(style->font_size));
1728                 style->font_size.set = true;
1729                 style->font_size.inherit = false;
1730                 style->font_size.computed = parent->font_size.computed * child_frac;
1732                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1733                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1734                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1735                 {
1736                     /* Absolute value. */
1737                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1738                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1739                 } else {
1740                     /* Relative value. */
1741                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1742                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1743                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1744                 }
1745             }
1746         }
1748         /* 'font-stretch' */
1749         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1750                                                        parent->font_stretch,
1751                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1752                                                        SP_CSS_FONT_STRETCH_NARROWER);
1754         /* font-weight */
1755         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1756                                                        parent->font_weight,
1757                                                        SP_CSS_FONT_WEIGHT_900,
1758                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1759     }
1762     /* Enum values that don't have any relative settings (other than `inherit'). */
1763     {
1764         SPIEnum SPStyle::*const fields[] = {
1765             //nyi: SPStyle::clip_rule,
1766             //nyi: SPStyle::color_interpolation,
1767             //nyi: SPStyle::color_interpolation_filters,
1768             //nyi: SPStyle::color_rendering,
1769             &SPStyle::direction,
1770             &SPStyle::fill_rule,
1771             &SPStyle::font_style,
1772             &SPStyle::font_variant,
1773             //nyi: SPStyle::image_rendering,
1774             //nyi: SPStyle::pointer_events,
1775             //nyi: SPStyle::shape_rendering,
1776             &SPStyle::stroke_linecap,
1777             &SPStyle::stroke_linejoin,
1778             &SPStyle::text_anchor,
1779             //nyi: &SPStyle::text_rendering,
1780             &SPStyle::visibility,
1781             &SPStyle::writing_mode
1782         };
1784         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1785             SPIEnum SPStyle::*const fld = fields[i];
1786             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1787         }
1788     }
1790     /* A few other simple inheritance properties. */
1791     {
1792         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1793         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1794         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1796         /** \todo
1797          * We currently treat text-decoration as if it were a simple inherited
1798          * property (fixme). This code may need changing once we do the
1799          * special fill/stroke inheritance mentioned by the spec.
1800          */
1801         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1802                                                                  parent->text_decoration);
1804         //nyi: font-size-adjust,  // <number> | none | inherit
1805         //nyi: glyph-orientation-horizontal,
1806         //nyi: glyph-orientation-vertical,
1807     }
1809     /* Properties that involve length but are easy in other respects. */
1810     {
1811         /* The difficulty with lengths is that font-relative units need adjusting if the font
1812          * varies between parent & child.
1813          *
1814          * Lengths specified in the existing child can stay as they are: its computed font
1815          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1816          * size.
1817          *
1818          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1819          * ratio of em or ex size between parent & child.
1820          */
1821         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1823         SPILength SPStyle::*const lfields[] = {
1824             &SPStyle::stroke_width,
1825             &SPStyle::text_indent
1826         };
1827         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1828             SPILength SPStyle::*fld = lfields[i];
1829             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1830                                                                     parent->*fld,
1831                                                                     parent_child_em_ratio);
1832         }
1834         SPILengthOrNormal SPStyle::*const nfields[] = {
1835             &SPStyle::letter_spacing,
1836             &SPStyle::line_height,
1837             &SPStyle::word_spacing
1838         };
1839         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1840             SPILengthOrNormal SPStyle::*fld = nfields[i];
1841             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1842                                                                             parent->*fld,
1843                                                                             parent_child_em_ratio);
1844         }
1846         //nyi: &SPStyle::kerning: length or `auto'
1848         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1849            can accept units. */
1850     }
1852     /* Properties that involve a URI but are easy in other respects. */
1853     {
1854         /** \todo
1855          * Could cause problems if original object was in another document
1856          * and it used a relative URL.  (At the time of writing, we don't
1857          * allow hrefs to other documents, so this isn't a problem yet.)
1858          * Paint properties also allow URIs.
1859          */
1860         //nyi: cursor,   // may involve change in effect, but we can't do much better
1861         //nyi: color-profile,
1863         // Markers (marker-start, marker-mid, marker-end).
1864         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1865             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1866         }
1867     }
1869     /* CSS2 */
1870     /* Font */
1872     if (style->text && parent->text) {
1873         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1874                                                      parent->text->font_family);
1875     }
1877     /* Properties that don't inherit by default.  Most of these need special handling. */
1878     {
1879         /*
1880          * opacity's effect is cumulative; we set the new value to the combined effect.  The
1881          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
1882          * fill-opacity are quite different from opacity, and don't need any special handling.)
1883          *
1884          * Cases:
1885          * - parent & child were each previously unset, in which case the effective
1886          *   opacity value is 1.0, and style should remain unset.
1887          * - parent was previously unset (so computed opacity value of 1.0)
1888          *   and child was set to inherit.  The merged child should
1889          *   get a value of 1.0, and shouldn't inherit (lest the new parent
1890          *   has a different opacity value).  Given that opacity's default
1891          *   value is 1.0 (rather than inherit), we might as well have the
1892          *   merged child's opacity be unset.
1893          * - parent was previously unset (so opacity 1.0), and child was set to a number.
1894          *   The merged child should retain its existing settings (though it doesn't matter
1895          *   if we make it unset if that number was 1.0).
1896          * - parent was inherit and child was unset.  Merged child should be set to inherit.
1897          * - parent was inherit and child was inherit.  (We can't in general reproduce this
1898          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
1899          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1900          *   child's value should be set to the product of the two, i.e. the square of the
1901          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
1902          *   is more important to retain the effective opacity than to retain the inheriting
1903          *   effect, and assumes that the inheriting effect either isn't important enough to create
1904          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
1905          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1906          *   closer to maintaining the effect.
1907          * - parent was inherit and child was set to a numerical value.  If the child's value
1908          *   was 1.0, then the merged child should have the same settings as the parent.
1909          *   If the child's value was 0, then the merged child should also be set to 0.
1910          *   If the child's value was anything else, then we do the same as for the inherit/inherit
1911          *   case above: have the merged child set to the product of the two opacities and not
1912          *   marked as inherit, for the same reasons as for that case.
1913          * - parent was set to a value, and child was unset.  The merged child should have
1914          *   parent's settings.
1915          * - parent was set to a value, and child was inherit.  The merged child should
1916          *   be set to the product, i.e. the square of the parent's value.
1917          * - parent & child are each set to a value.  The merged child should be set to the
1918          *   product.
1919          */
1920         if ( !style->opacity.set
1921              || ( !style->opacity.inherit
1922                   && style->opacity.value == SP_SCALE24_MAX ) )
1923         {
1924             style->opacity = parent->opacity;
1925         } else {
1926             /* Ensure that style's computed value is up-to-date. */
1927             if (style->opacity.inherit) {
1928                 style->opacity.value = parent->opacity.value;
1929             }
1931             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1932             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1933                                                   parent->opacity.value);
1935             style->opacity.inherit = (parent->opacity.inherit
1936                                       && style->opacity.inherit
1937                                       && (parent->opacity.value == 0 ||
1938                                           parent->opacity.value == SP_SCALE24_MAX));
1939             style->opacity.set = ( style->opacity.inherit
1940                                    || style->opacity.value < SP_SCALE24_MAX );
1941         }
1943         /* display is in principle similar to opacity, but implementation is easier. */
1944         if ( parent->display.set && !parent->display.inherit
1945              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1946             style->display.value = SP_CSS_DISPLAY_NONE;
1947             style->display.set = true;
1948             style->display.inherit = false;
1949         } else if (style->display.inherit) {
1950             style->display.value = parent->display.value;
1951             style->display.set = parent->display.set;
1952             style->display.inherit = parent->display.inherit;
1953         } else {
1954             /* Leave as is.  (display doesn't inherit by default.) */
1955         }
1957         /* enable-background - this is rather complicated, because
1958          * it is valid only when applied to container elements.
1959          * Let's check a simple case anyhow. */
1960         if (parent->enable_background.set
1961             && !parent->enable_background.inherit
1962             && style->enable_background.inherit)
1963         {
1964             style->enable_background.set = true;
1965             style->enable_background.inherit = false;
1966             style->enable_background.value = parent->enable_background.value;
1967         }
1969         if (!style->filter.set || style->filter.inherit)
1970         {
1971             // FIXME:
1972             // instead of just copying over, we need to _really merge_ the two filters by combining their
1973             // filter primitives
1974             sp_style_merge_ifilter(style, &parent->filter);
1975         }
1977         /** \todo
1978          * fixme: Check that we correctly handle all properties that don't
1979          * inherit by default (as shown in
1980          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1981          */
1982     }
1984     /* SPIPaint properties (including color). */
1985     {
1986         /** \todo
1987          * Think about the issues involved if specified as currentColor or
1988          * if specified relative to colorProfile, and if the currentColor or
1989          * colorProfile differs between parent \& child.  See also comments
1990          * elsewhere in this function about URIs.
1991          */
1992         SPIPaint SPStyle::*const fields[] = {
1993             &SPStyle::color,
1994             &SPStyle::fill,
1995             &SPStyle::stroke
1996         };
1997         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1998             SPIPaint SPStyle::*const fld = fields[i];
1999             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2000         }
2001     }
2003     /* Things from SVG 1.2 or CSS3. */
2004     {
2005         /* Note: If we ever support setting string values for text-align then we'd need strdup
2006          * handling here. */
2007         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2009         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2010         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2011     }
2013     /* Note: this will need length handling once dasharray supports units. */
2014     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2015          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2016     {
2017         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2018         if (style->stroke_dash.n_dash > 0) {
2019             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2020             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2021         }
2022         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2023         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2024     }
2026     /* Note: this will need length handling once dasharray_offset supports units. */
2027     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
2028         style->stroke_dash.offset = parent->stroke_dash.offset;
2029         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2030         /* fixme: we don't currently allow stroke-dashoffset to be `inherit'.  TODO: Try to
2031          * represent it as a normal SPILength; though will need to do something about existing
2032          * users of stroke_dash.offset and stroke_dashoffset_set. */
2033     }
2038 /**
2039  * Disconnects from possible fill and stroke paint servers.
2040  */
2041 static void
2042 sp_style_paint_server_release(SPObject *obj, SPStyle *style)
2044     SPPaintServer *server=static_cast<SPPaintServer *>(obj);
2045     if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2046         && (server == style->fill.value.paint.server))
2047     {
2048         sp_style_paint_clear(style, &style->fill);
2049     }
2051     if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2052         && (server == style->stroke.value.paint.server))
2053     {
2054         sp_style_paint_clear(style, &style->stroke);
2055     }
2061 /**
2062  * Emit style modified signal on style's object if server is style's fill
2063  * or stroke paint server.
2064  */
2065 static void
2066 sp_style_paint_server_modified(SPObject *obj, guint flags, SPStyle *style)
2068     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
2069     if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2070         && (server == style->fill.value.paint.server))
2071     {
2072         if (style->object) {
2073             /** \todo
2074              * fixme: I do not know, whether it is optimal - we are
2075              * forcing reread of everything (Lauris)
2076              */
2077             /** \todo
2078              * fixme: We have to use object_modified flag, because parent
2079              * flag is only available downstreams.
2080              */
2081             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2082         }
2083     } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2084                && (server == style->stroke.value.paint.server))
2085     {
2086         if (style->object) {
2087             /// \todo fixme:
2088             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2089         }
2090     } else {
2091         g_assert_not_reached();
2092     }
2095 /**
2096  *
2097  */
2098 static void
2099 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2101     sp_style_paint_clear(style, paint);
2103     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2104         paint->currentcolor = TRUE;
2105         paint->type = SP_PAINT_TYPE_COLOR;
2106         sp_color_copy(&paint->value.color, &style->color.value.color);
2107         return;
2108     }
2110     paint->type = parent->type;
2111     switch (paint->type) {
2112         case SP_PAINT_TYPE_COLOR:
2113             sp_color_copy(&paint->value.color, &parent->value.color);
2114             break;
2115         case SP_PAINT_TYPE_PAINTSERVER:
2116             paint->value.paint.server = parent->value.paint.server;
2117             paint->value.paint.uri = parent->value.paint.uri;
2118             if (paint->value.paint.server) {
2119                 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
2120                     sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2121                     if (paint == &style->fill) {
2122                         style->fill_hreffed = true;
2123                     } else {
2124                         assert(paint == &style->stroke);
2125                         style->stroke_hreffed = true;
2126                     }
2127                 }
2128                 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
2129                     SPObject *server = paint->value.paint.server;
2130                     sigc::connection release_connection
2131                       = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
2132                     sigc::connection modified_connection
2133                       = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
2134                     if (paint == &style->fill) {
2135                         style->fill_release_connection = release_connection;
2136                         style->fill_modified_connection = modified_connection;
2137                     } else {
2138                         assert(paint == &style->stroke);
2139                         style->stroke_release_connection = release_connection;
2140                         style->stroke_modified_connection = modified_connection;
2141                     }
2142                 }
2143             }
2144             break;
2145         case SP_PAINT_TYPE_NONE:
2146             break;
2147         default:
2148             g_assert_not_reached();
2149             break;
2150     }
2154 /**
2155  * Merge filter style from parent.
2156  * Filter effects do not inherit by default
2157  */
2158 static void
2159 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2161     sp_style_filter_clear(style);
2162     style->filter.set = parent->set;
2163     style->filter.inherit = parent->inherit;
2165     if (style->filter.href && style->filter.href->getObject())
2166        style->filter.href->detach();
2168     if (style->filter.href && parent->href) {
2169         try {
2170             style->filter.href->attach(*parent->href->getURI());
2171         } catch (Inkscape::BadURIException &e) {
2172             g_warning("%s", e.what());
2173             style->filter.href->detach();
2174         }
2175     }
2178 /**
2179  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2180  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2181  * complete cascaded style to style_clipboard. When you need a CSS string
2182  * for an object in the document tree, you normally call
2183  * sp_style_write_difference instead to take into account the object's parent.
2184  *
2185  * \pre style != NULL.
2186  * \pre flags in {IFSET, ALWAYS}.
2187  * \post ret != NULL.
2188  */
2189 gchar *
2190 sp_style_write_string(SPStyle const *const style, guint const flags)
2192     /** \todo
2193      * Merge with write_difference, much duplicate code!
2194      */
2195     g_return_val_if_fail(style != NULL, NULL);
2196     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2197                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2198                          NULL);
2200     gchar c[BMAX];
2201     gchar *p = c;
2202     *p = '\0';
2204     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2205     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2206     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2207     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2208     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2210     /* Text */
2211     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2212     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2213     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2214     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2215     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2216     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2217     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2218     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2219     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2220     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2222     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2224     /// \todo fixme: Per type methods need default flag too (lauris)
2225     p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2226     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2227     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2228     p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2229     p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2230     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2231     p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2232     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2233     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2235     if (style->marker[SP_MARKER_LOC].set) {
2236         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2237     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2238         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2239     }
2240     if (style->marker[SP_MARKER_LOC_START].set) {
2241         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2242     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2243         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2244     }
2245     if (style->marker[SP_MARKER_LOC_MID].set) {
2246         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2247     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2248         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2249     }
2250     if (style->marker[SP_MARKER_LOC_END].set) {
2251         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2252     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2253         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2254     }
2256     p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2258     /** \todo fixme: */
2259     if ((flags == SP_STYLE_FLAG_ALWAYS)
2260         || style->stroke_dasharray_set)
2261     {
2262         if (style->stroke_dasharray_inherit) {
2263             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2264         } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2265             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2266             gint i;
2267             for (i = 0; i < style->stroke_dash.n_dash; i++) {
2268                 Inkscape::CSSOStringStream os;
2269                 if (i) {
2270                     os << ", ";
2271                 }
2272                 os << style->stroke_dash.dash[i];
2273                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2274             }
2275             if (p < c + BMAX) {
2276                 *p++ = ';';
2277             }
2278         } else {
2279             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2280         }
2281     }
2283     /** \todo fixme: */
2284     if (style->stroke_dashoffset_set) {
2285         Inkscape::CSSOStringStream os;
2286         os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2287         p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2288     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2289         p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2290     }
2292     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2294     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2295     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2296     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2298     /* filter: */
2299     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2301     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2303     /* fixme: */
2304     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2306     /* Get rid of trailing `;'. */
2307     if (p != c) {
2308         --p;
2309         if (*p == ';') {
2310             *p = '\0';
2311         }
2312     }
2314     return g_strdup(c);
2318 #define STYLE_BUF_MAX
2321 /**
2322  * Dumps style to CSS string, see sp_style_write_string()
2323  *
2324  * \pre from != NULL.
2325  * \pre to != NULL.
2326  * \post ret != NULL.
2327  */
2328 gchar *
2329 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2331     g_return_val_if_fail(from != NULL, NULL);
2332     g_return_val_if_fail(to != NULL, NULL);
2334     gchar c[BMAX], *p = c;
2335     *p = '\0';
2337     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2338     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2339     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2340     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2341     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2343     /* Text */
2344     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2345     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2346     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2347     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2348     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2349     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2350     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2351     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2352     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2353     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2355     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2357     /// \todo fixme: Per type methods need default flag too
2358     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2359         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2360     }
2361     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2362     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2363     p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2364     p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2365     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2366     p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2367     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2368                               &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2369     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2370                               &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2371     p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2372                                &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2373     /** \todo fixme: */
2374     if (from->stroke_dasharray_set) {
2375         if (from->stroke_dasharray_inherit) {
2376             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2377         } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2378             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2379             for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2380                 Inkscape::CSSOStringStream os;
2381                 if (i) {
2382                     os << ", ";
2383                 }
2384                 os << from->stroke_dash.dash[i];
2385                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2386             }
2387             p += g_snprintf(p, c + BMAX - p, ";");
2388         }
2389     }
2390     /* fixme: */
2391     if (from->stroke_dashoffset_set) {
2392         Inkscape::CSSOStringStream os;
2393         os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2394         p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2395     }
2396     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2398     /* markers */
2399     if (from->marker[SP_MARKER_LOC].value != NULL) {
2400         p += g_snprintf(p, c + BMAX - p, "marker:%s;",       from->marker[SP_MARKER_LOC].value);
2401     }
2402     if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2403         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2404     }
2405     if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2406         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2407     }
2408     if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2409         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2410     }
2412     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2413     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2414     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2416     /* filter: */
2417     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2419     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2421     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2423     /** \todo
2424      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2425      * flag is mainly only for attributes that don't handle explicit unset well.
2426      * We may need to revisit the behaviour of this routine.
2427      */
2429     /* Get rid of trailing `;'. */
2430     if (p != c) {
2431         --p;
2432         if (*p == ';') {
2433             *p = '\0';
2434         }
2435     }
2437     return g_strdup(c);
2442 /**
2443  * Reset all style properties.
2444  */
2445 static void
2446 sp_style_clear(SPStyle *style)
2448     g_return_if_fail(style != NULL);
2450     sp_style_paint_clear(style, &style->fill);
2451     sp_style_paint_clear(style, &style->stroke);
2452     sp_style_filter_clear(style);
2453     if (style->stroke_dash.dash) {
2454         g_free(style->stroke_dash.dash);
2455     }
2457     /** \todo fixme: Do that text manipulation via parents */
2458     SPObject *object = style->object;
2459     gint const refcount = style->refcount;
2460     SPTextStyle *text = style->text;
2461     unsigned const text_private = style->text_private;
2462     memset(style, 0, sizeof(SPStyle));
2463     style->refcount = refcount;
2464     style->object = object;
2465     style->text = text;
2466     style->text_private = text_private;
2467     /* fixme: */
2468     style->text->font.set = FALSE;
2469     style->text->font_family.set = FALSE;
2471     style->font_size.set = FALSE;
2472     style->font_size.type = SP_FONT_SIZE_LITERAL;
2473     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2474     style->font_size.computed = 12.0;
2475     style->font_style.set = FALSE;
2476     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2477     style->font_variant.set = FALSE;
2478     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2479     style->font_weight.set = FALSE;
2480     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2481     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2482     style->font_stretch.set = FALSE;
2483     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2485     /* text */
2486     style->text_indent.set = FALSE;
2487     style->text_indent.unit = SP_CSS_UNIT_NONE;
2488     style->text_indent.computed = 0.0;
2490     style->text_align.set = FALSE;
2491     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2493     style->text_decoration.set = FALSE;
2494     style->text_decoration.underline = FALSE;
2495     style->text_decoration.overline = FALSE;
2496     style->text_decoration.line_through = FALSE;
2497     style->text_decoration.blink = FALSE;
2499     style->line_height.set = FALSE;
2500     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2501     style->line_height.normal = TRUE;
2502     style->line_height.value = style->line_height.computed = 1.0;
2504     style->letter_spacing.set = FALSE;
2505     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2506     style->letter_spacing.normal = TRUE;
2507     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2509     style->word_spacing.set = FALSE;
2510     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2511     style->word_spacing.normal = TRUE;
2512     style->word_spacing.value = style->word_spacing.computed = 0.0;
2514     style->text_transform.set = FALSE;
2515     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2517     style->direction.set = FALSE;
2518     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2520     style->block_progression.set = FALSE;
2521     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2523     style->writing_mode.set = FALSE;
2524     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2527     style->text_anchor.set = FALSE;
2528     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2531     style->opacity.value = SP_SCALE24_MAX;
2532     style->visibility.set = FALSE;
2533     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2534     style->display.set = FALSE;
2535     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2536     style->overflow.set = FALSE;
2537     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2539     style->color.type = SP_PAINT_TYPE_COLOR;
2540     sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
2542     style->fill.type = SP_PAINT_TYPE_COLOR;
2543     sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
2544     style->fill_opacity.value = SP_SCALE24_MAX;
2545     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2547     style->stroke.type = SP_PAINT_TYPE_NONE;
2548     style->stroke.set = FALSE;
2549     sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
2550     style->stroke_opacity.value = SP_SCALE24_MAX;
2552     style->stroke_width.set = FALSE;
2553     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2554     style->stroke_width.computed = 1.0;
2556     style->stroke_linecap.set = FALSE;
2557     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2558     style->stroke_linejoin.set = FALSE;
2559     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2561     style->stroke_miterlimit.set = FALSE;
2562     style->stroke_miterlimit.value = 4.0;
2564     style->stroke_dash.n_dash = 0;
2565     style->stroke_dash.dash = NULL;
2566     style->stroke_dash.offset = 0.0;
2568     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2569         g_free(style->marker[i].value);
2570         style->marker[i].set = FALSE;
2571     }
2573     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2574     style->enable_background.set = false;
2575     style->enable_background.inherit = false;
2580 /**
2581  *
2582  */
2583 static void
2584 sp_style_read_dash(SPStyle *style, gchar const *str)
2586     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2587     style->stroke_dasharray_set = TRUE;
2589     if (strcmp(str, "inherit") == 0) {
2590         style->stroke_dasharray_inherit = true;
2591         return;
2592     }
2593     style->stroke_dasharray_inherit = false;
2595     NRVpathDash &dash = style->stroke_dash;
2596     g_free(dash.dash);
2597     dash.dash = NULL;
2599     if (strcmp(str, "none") == 0) {
2600         dash.n_dash = 0;
2601         return;
2602     }
2604     gint n_dash = 0;
2605     gdouble d[64];
2606     gchar *e = NULL;
2608     bool LineSolid=true;
2609     while (e != str && n_dash < 64) {
2610         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2611         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2612         if (d[n_dash] > 0.00000001)
2613             LineSolid = false;
2614         if (e != str) {
2615             n_dash += 1;
2616             str = e;
2617         }
2618         while (str && *str && !isalnum(*str)) str += 1;
2619     }
2621     if (LineSolid) {
2622         dash.n_dash = 0;
2623         return;
2624     }
2626     if (n_dash > 0) {
2627         dash.dash = g_new(gdouble, n_dash);
2628         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2629         dash.n_dash = n_dash;
2630     }
2634 /*#########################
2635 ## SPTextStyle operations
2636 #########################*/
2639 /**
2640  * Return new SPTextStyle object with default settings.
2641  */
2642 static SPTextStyle *
2643 sp_text_style_new()
2645     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2646     ts->refcount = 1;
2647     sp_text_style_clear(ts);
2649     ts->font.value = g_strdup("Bitstream Vera Sans");
2650     ts->font_family.value = g_strdup("Bitstream Vera Sans");
2652     return ts;
2656 /**
2657  * Clear text style settings.
2658  */
2659 static void
2660 sp_text_style_clear(SPTextStyle *ts)
2662     ts->font.set = FALSE;
2663     ts->font_family.set = FALSE;
2668 /**
2669  * Reduce refcount of text style and possibly free it.
2670  */
2671 static SPTextStyle *
2672 sp_text_style_unref(SPTextStyle *st)
2674     st->refcount -= 1;
2676     if (st->refcount < 1) {
2677         g_free(st->font.value);
2678         g_free(st->font_family.value);
2679         g_free(st);
2680     }
2682     return NULL;
2687 /**
2688  * Return duplicate of text style.
2689  */
2690 static SPTextStyle *
2691 sp_text_style_duplicate_unset(SPTextStyle *st)
2693     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2694     nt->refcount = 1;
2696     nt->font.value = g_strdup(st->font.value);
2697     nt->font_family.value = g_strdup(st->font_family.value);
2699     return nt;
2704 /**
2705  * Write SPTextStyle object into string.
2706  */
2707 static guint
2708 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2710     gint d = 0;
2712     // We do not do diffing for text style
2713     if (flags == SP_STYLE_FLAG_IFDIFF)
2714         flags = SP_STYLE_FLAG_IFSET;
2716     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2717     return d;
2722 /* The following sp_tyle_read_* functions ignore invalid values, as per
2723  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2724  *
2725  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2726  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2727  * pairs, in which case SVG's error-handling rules
2728  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2729  */
2732 /**
2733  * Set SPIFloat object from string.
2734  */
2735 static void
2736 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2738     if (!strcmp(str, "inherit")) {
2739         val->set = TRUE;
2740         val->inherit = TRUE;
2741     } else {
2742         gfloat value;
2743         if (sp_svg_number_read_f(str, &value)) {
2744             val->set = TRUE;
2745             val->inherit = FALSE;
2746             val->value = value;
2747         }
2748     }
2753 /**
2754  * Set SPIScale24 object from string.
2755  */
2756 static void
2757 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2759     if (!strcmp(str, "inherit")) {
2760         val->set = TRUE;
2761         val->inherit = TRUE;
2762     } else {
2763         gfloat value;
2764         if (sp_svg_number_read_f(str, &value)) {
2765             val->set = TRUE;
2766             val->inherit = FALSE;
2767             value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
2768             val->value = SP_SCALE24_FROM_FLOAT(value);
2769         }
2770     }
2773 /**
2774  * Reads a style value and performs lookup based on the given style value enumerations.
2775  */
2776 static void
2777 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2778                     bool const can_explicitly_inherit)
2780     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2781         val->set = TRUE;
2782         val->inherit = TRUE;
2783     } else {
2784         for (unsigned i = 0; dict[i].key; i++) {
2785             if (!strcmp(str, dict[i].key)) {
2786                 val->set = TRUE;
2787                 val->inherit = FALSE;
2788                 val->value = dict[i].value;
2789                 /* Save copying for values not needing it */
2790                 val->computed = val->value;
2791                 break;
2792             }
2793         }
2794     }
2799 /**
2800  * Set SPIString object from string.
2801  */
2802 static void
2803 sp_style_read_istring(SPIString *val, gchar const *str)
2805     g_free(val->value);
2807     if (!strcmp(str, "inherit")) {
2808         val->set = TRUE;
2809         val->inherit = TRUE;
2810         val->value = NULL;
2811     } else {
2812         val->set = TRUE;
2813         val->inherit = FALSE;
2814         val->value = g_strdup(str);
2815     }
2820 /**
2821  * Set SPILength object from string.
2822  */
2823 static void
2824 sp_style_read_ilength(SPILength *val, gchar const *str)
2826     if (!strcmp(str, "inherit")) {
2827         val->set = TRUE;
2828         val->inherit = TRUE;
2829     } else {
2830         gdouble value;
2831         gchar *e;
2832         /** \todo fixme: Move this to standard place (Lauris) */
2833         value = g_ascii_strtod(str, &e);
2834         if ((gchar const *) e != str) {
2835             /** \todo
2836              * Allow the number of px per inch to vary (document preferences,
2837              * X server or whatever).  E.g. don't fill in computed here, do
2838              * it at the same time as percentage units are done.
2839              */
2840             if (!*e) {
2841                 /* Userspace */
2842                 val->unit = SP_CSS_UNIT_NONE;
2843                 val->computed = value;
2844             } else if (!strcmp(e, "px")) {
2845                 /* Userspace */
2846                 val->unit = SP_CSS_UNIT_PX;
2847                 val->computed = value;
2848             } else if (!strcmp(e, "pt")) {
2849                 /* Userspace / DEVICESCALE */
2850                 val->unit = SP_CSS_UNIT_PT;
2851                 val->computed = value * PX_PER_PT;
2852             } else if (!strcmp(e, "pc")) {
2853                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2854                 val->unit = SP_CSS_UNIT_PC;
2855                 val->computed = value * PX_PER_PT * 12;
2856             } else if (!strcmp(e, "mm")) {
2857                 val->unit = SP_CSS_UNIT_MM;
2858                 val->computed = value * PX_PER_MM;
2859             } else if (!strcmp(e, "cm")) {
2860                 val->unit = SP_CSS_UNIT_CM;
2861                 val->computed = value * PX_PER_CM;
2862             } else if (!strcmp(e, "in")) {
2863                 val->unit = SP_CSS_UNIT_IN;
2864                 val->computed = value * PX_PER_IN;
2865             } else if (!strcmp(e, "em")) {
2866                 /* EM square */
2867                 val->unit = SP_CSS_UNIT_EM;
2868                 val->value = value;
2869             } else if (!strcmp(e, "ex")) {
2870                 /* ex square */
2871                 val->unit = SP_CSS_UNIT_EX;
2872                 val->value = value;
2873             } else if (!strcmp(e, "%")) {
2874                 /* Percentage */
2875                 val->unit = SP_CSS_UNIT_PERCENT;
2876                 val->value = value * 0.01;
2877             } else {
2878                 /* Invalid */
2879                 return;
2880             }
2881             val->set = TRUE;
2882             val->inherit = FALSE;
2883         }
2884     }
2887 /**
2888  * Set SPILengthOrNormal object from string.
2889  */
2890 static void
2891 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2893     if (!strcmp(str, "normal")) {
2894         val->set = TRUE;
2895         val->inherit = FALSE;
2896         val->normal = TRUE;
2897         val->unit = SP_CSS_UNIT_NONE;
2898         val->value = val->computed = 0.0;
2899     } else {
2900         SPILength length;
2901         sp_style_read_ilength(&length, str);
2902         val->set = length.set;
2903         val->inherit = length.inherit;
2904         val->normal = FALSE;
2905         val->unit = length.unit;
2906         val->value = length.value;
2907         val->computed = length.computed;
2908     }
2911 /**
2912  * Set SPITextDecoration object from string.
2913  */
2914 static void
2915 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2917     if (!strcmp(str, "inherit")) {
2918         val->set = TRUE;
2919         val->inherit = TRUE;
2920     } else if (!strcmp(str, "none")) {
2921         val->set = TRUE;
2922         val->inherit = FALSE;
2923         val->underline = FALSE;
2924         val->overline = FALSE;
2925         val->line_through = FALSE;
2926         val->blink = FALSE;
2927     } else {
2928         bool found_underline = false;
2929         bool found_overline = false;
2930         bool found_line_through = false;
2931         bool found_blink = false;
2932         for ( ; *str ; str++ ) {
2933             if (*str == ' ') continue;
2934             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2935                 found_underline = true;
2936                 str += 9;
2937             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2938                 found_overline = true;
2939                 str += 8;
2940             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2941                 found_line_through = true;
2942                 str += 12;
2943             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2944                 found_blink = true;
2945                 str += 5;
2946             } else {
2947                 return;  // invalid value
2948             }
2949         }
2950         if (!(found_underline || found_overline || found_line_through || found_blink)) {
2951             return;  // invalid value: empty
2952         }
2953         val->set = TRUE;
2954         val->inherit = FALSE;
2955         val->underline = found_underline;
2956         val->overline = found_overline;
2957         val->line_through = found_line_through;
2958         val->blink = found_blink;
2959     }
2962 /**
2963  * Set SPIPaint object from string containing an integer value.
2964  * \param document Ignored
2965  */
2966 static void
2967 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2969     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
2970     if (!strcmp(str, "inherit")) {
2971         paint->set = TRUE;
2972         paint->inherit = TRUE;
2973     } else {
2974         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2975         if (rgb0 != 0xff) {
2976             paint->type = SP_PAINT_TYPE_COLOR;
2977             sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2978             paint->set = TRUE;
2979             paint->inherit = FALSE;
2980         }
2981     }
2985 /**
2986  * Set SPIPaint object from string.
2987  *
2988  * \pre paint == \&style.fill || paint == \&style.stroke.
2989  */
2990 static void
2991 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2993     while (isspace(*str)) {
2994         ++str;
2995     }
2997     if (streq(str, "inherit")) {
2998         paint->set = TRUE;
2999         paint->inherit = TRUE;
3000         paint->currentcolor = FALSE;
3001     } else if (streq(str, "currentColor") && paint != &style->color) {
3002         paint->set = TRUE;
3003         paint->inherit = FALSE;
3004         paint->currentcolor = TRUE;
3005     } else if (streq(str, "none") && paint != &style->color) {
3006         paint->type = SP_PAINT_TYPE_NONE;
3007         paint->set = TRUE;
3008         paint->inherit = FALSE;
3009         paint->currentcolor = FALSE;
3010     } else if (strneq(str, "url", 3) && paint != &style->color) {
3011         // this is alloc'd uri, but seems to be shared with a parent
3012         // potentially, so it's not any easy g_free case...
3013         paint->value.paint.uri = extract_uri(str);
3014         if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
3015             paint->type = SP_PAINT_TYPE_NONE;
3016             return;
3017         }
3018         paint->type = SP_PAINT_TYPE_PAINTSERVER;
3019         paint->set = TRUE;
3020         paint->inherit = FALSE;
3021         paint->currentcolor = FALSE;
3022         if (document) {
3023             SPObject *ps = sp_uri_reference_resolve(document, str);
3024             if (ps && SP_IS_PAINT_SERVER(ps)) {
3025                 paint->value.paint.server = SP_PAINT_SERVER(ps);
3026                 if (style->object && !style->cloned) {
3027                     sp_object_href(SP_OBJECT(paint->value.paint.server), style);
3028                     if (paint == &style->fill) {
3029                         style->fill_hreffed = true;
3030                     } else {
3031                         assert(paint == &style->stroke);
3032                         style->stroke_hreffed = true;
3033                     }
3034                 }
3035                 if (style->object || style->cloned) {
3036                     SPObject *server = paint->value.paint.server;
3037                     sigc::connection release_connection
3038                       = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
3039                     sigc::connection modified_connection
3040                       = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
3041                     if (paint == &style->fill) {
3042                         style->fill_release_connection = release_connection;
3043                         style->fill_modified_connection = modified_connection;
3044                     } else {
3045                         assert(paint == &style->stroke);
3046                         style->stroke_release_connection = release_connection;
3047                         style->stroke_modified_connection = modified_connection;
3048                     }
3049                 }
3050             } else {
3051                 paint->value.paint.server = NULL;
3052             }
3053         }
3054     } else {
3055         guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3056         if (rgb0 != 0xff) {
3057             paint->type = SP_PAINT_TYPE_COLOR;
3058             sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
3059             paint->set = TRUE;
3060             paint->inherit = FALSE;
3061             paint->currentcolor = FALSE;
3063             while (g_ascii_isspace(*str)) {
3064                 ++str;
3065             }
3066             if (strneq(str, "icc-color(", 10)) {
3067                 SVGICCColor* tmp = new SVGICCColor();
3068                 if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3069                     delete tmp;
3070                     tmp = 0;
3071                 }
3072                 paint->value.iccColor = tmp;
3073             }
3074         }
3075     }
3080 /**
3081  * Set SPIFontSize object from string.
3082  */
3083 static void
3084 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3086     if (!strcmp(str, "inherit")) {
3087         val->set = TRUE;
3088         val->inherit = TRUE;
3089     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3090         for (unsigned i = 0; enum_font_size[i].key; i++) {
3091             if (!strcmp(str, enum_font_size[i].key)) {
3092                 val->set = TRUE;
3093                 val->inherit = FALSE;
3094                 val->type = SP_FONT_SIZE_LITERAL;
3095                 val->value = enum_font_size[i].value;
3096                 return;
3097             }
3098         }
3099         /* Invalid */
3100         return;
3101     } else {
3102         gdouble value;
3103         gchar *e;
3104         /* fixme: Move this to standard place (Lauris) */
3105         value = g_ascii_strtod(str, &e);
3106         if ((gchar const *) e != str) {
3107             if (!*e) {
3108                 /* Userspace */
3109             } else if (!strcmp(e, "px")) {
3110                 /* Userspace */
3111             } else if (!strcmp(e, "pt")) {
3112                 /* Userspace * DEVICESCALE */
3113                 value *= PX_PER_PT;
3114             } else if (!strcmp(e, "pc")) {
3115                 /* 12pt */
3116                 value *= PX_PER_PT * 12.0;
3117             } else if (!strcmp(e, "mm")) {
3118                 value *= PX_PER_MM;
3119             } else if (!strcmp(e, "cm")) {
3120                 value *= PX_PER_CM;
3121             } else if (!strcmp(e, "in")) {
3122                 value *= PX_PER_IN;
3123             } else if (!strcmp(e, "%")) {
3124                 /* Percentage */
3125                 val->set = TRUE;
3126                 val->inherit = FALSE;
3127                 val->type = SP_FONT_SIZE_PERCENTAGE;
3128                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3129                 return;
3130             } else {
3131                 /* Invalid */
3132                 return;
3133             }
3134             /* Length */
3135             val->set = TRUE;
3136             val->inherit = FALSE;
3137             val->type = SP_FONT_SIZE_LENGTH;
3138             val->computed = value;
3139             return;
3140         }
3141     }
3146 /**
3147  * Set SPIFilter object from string.
3148  */
3149 static void
3150 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3152     SPIFilter *f = &(style->filter);
3153     /* Try all possible values: inherit, none, uri */
3154     if (streq(str, "inherit")) {
3155         f->set = TRUE;
3156         f->inherit = TRUE;
3157         if (f->href && f->href->getObject())
3158             f->href->detach(); //f->filter = NULL;
3159     } else if(streq(str, "none")) {
3160         f->set = TRUE;
3161         f->inherit = FALSE;
3162         if (f->href && f->href->getObject())
3163            f->href->detach(); //f->filter = NULL;
3164     } else if (strneq(str, "url", 3)) {
3165         char *uri = extract_uri(str);
3166         if(uri == NULL || uri[0] == '\0') {
3167             g_warning("Specified filter url is empty");
3168             f->set = TRUE;
3169             f->inherit = FALSE;
3170             return;
3171         }
3172         f->set = TRUE;
3173         f->inherit = FALSE;
3174         if (f->href && f->href->getObject())
3175             f->href->detach();
3177         // it may be that this style has not yet created its SPFilterReference;
3178         // now that we have a document, we can create it here
3179         if (!f->href) {
3180             f->href = new SPFilterReference(document);
3181             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3182         }
3184         try {
3185             f->href->attach(Inkscape::URI(uri));
3186         } catch (Inkscape::BadURIException &e) {
3187             g_warning("%s", e.what());
3188             f->href->detach();
3189         }
3191     } else {
3192         /* We shouldn't reach this if SVG input is well-formed */
3193         f->set = FALSE;
3194         f->inherit = FALSE;
3195         if (f->href && f->href->getObject())
3196             f->href->detach(); 
3197     }
3200 /**
3201  * Set SPIEnum object from repr attribute.
3202  */
3203 static void
3204 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3205                     gchar const *key, SPStyleEnum const *dict,
3206                     bool const can_explicitly_inherit)
3208     gchar const *str = repr->attribute(key);
3209     if (str) {
3210         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3211     }
3216 /**
3217  * Set SPILength object from repr attribute.
3218  */
3219 static void
3220 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3222     gchar const *str = repr->attribute(key);
3223     if (str) {
3224         sp_style_read_ilength(val, str);
3225     }
3228 /**
3229  * Set SPIFontSize object from repr attribute.
3230  */
3231 static void
3232 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3234     gchar const *str = repr->attribute(key);
3235     if (str) {
3236         sp_style_read_ifontsize(val, str);
3237     }
3241 /**
3242  * Set SPIFloat object from repr attribute.
3243  */
3244 static void
3245 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3247     gchar const *str = repr->attribute(key);
3248     if (str) {
3249         sp_style_read_ifloat(val, str);
3250     }
3254 /**
3255  * Write SPIFloat object into string.
3256  */
3257 static gint
3258 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3259                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3261     Inkscape::CSSOStringStream os;
3263     if ((flags & SP_STYLE_FLAG_ALWAYS)
3264         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3265         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3266             && (!base->set || (val->value != base->value))))
3267     {
3268         if (val->inherit) {
3269             return g_snprintf(p, len, "%s:inherit;", key);
3270         } else {
3271             os << key << ":" << val->value << ";";
3272             return g_strlcpy(p, os.str().c_str(), len);
3273         }
3274     }
3275     return 0;
3279 /**
3280  * Write SPIScale24 object into string.
3281  */
3282 static gint
3283 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3284                         SPIScale24 const *const val, SPIScale24 const *const base,
3285                         guint const flags)
3287     Inkscape::CSSOStringStream os;
3289     if ((flags & SP_STYLE_FLAG_ALWAYS)
3290         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3291         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3292             && (!base->set || (val->value != base->value))))
3293     {
3294         if (val->inherit) {
3295             return g_snprintf(p, len, "%s:inherit;", key);
3296         } else {
3297             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3298             return g_strlcpy(p, os.str().c_str(), len);
3299         }
3300     }
3301     return 0;
3305 /**
3306  * Write SPIEnum object into string.
3307  */
3308 static gint
3309 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3310                      SPStyleEnum const *const dict,
3311                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3313     if ((flags & SP_STYLE_FLAG_ALWAYS)
3314         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3315         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3316             && (!base->set || (val->computed != base->computed))))
3317     {
3318         if (val->inherit) {
3319             return g_snprintf(p, len, "%s:inherit;", key);
3320         }
3321         for (unsigned i = 0; dict[i].key; i++) {
3322             if (dict[i].value == static_cast< gint > (val->value) ) {
3323                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3324             }
3325         }
3326     }
3327     return 0;
3332 /**
3333  * Write SPIString object into string.
3334  */
3335 static gint
3336 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3337                        SPIString const *const val, SPIString const *const base, guint const flags)
3339     if ((flags & SP_STYLE_FLAG_ALWAYS)
3340         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3341         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3342             && (!base->set || strcmp(val->value, base->value))))
3343     {
3344         if (val->inherit) {
3345             return g_snprintf(p, len, "%s:inherit;", key);
3346         } else {
3347             gchar *val_quoted = css2_escape_quote(val->value);
3348             return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3349             g_free (val_quoted);
3350         }
3351     }
3352     return 0;
3356 /**
3357  *
3358  */
3359 static bool
3360 sp_length_differ(SPILength const *const a, SPILength const *const b)
3362     if (a->unit != b->unit) {
3363         if (a->unit == SP_CSS_UNIT_EM) return true;
3364         if (a->unit == SP_CSS_UNIT_EX) return true;
3365         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3366         if (b->unit == SP_CSS_UNIT_EM) return true;
3367         if (b->unit == SP_CSS_UNIT_EX) return true;
3368         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3369     }
3371     return (a->computed != b->computed);
3376 /**
3377  * Write SPILength object into string.
3378  */
3379 static gint
3380 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3381                        SPILength const *const val, SPILength const *const base, guint const flags)
3383     Inkscape::CSSOStringStream os;
3385     if ((flags & SP_STYLE_FLAG_ALWAYS)
3386         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3387         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3388             && (!base->set || sp_length_differ(val, base))))
3389     {
3390         if (val->inherit) {
3391             return g_snprintf(p, len, "%s:inherit;", key);
3392         } else {
3393             switch (val->unit) {
3394                 case SP_CSS_UNIT_NONE:
3395                     os << key << ":" << val->computed << ";";
3396                     return g_strlcpy(p, os.str().c_str(), len);
3397                     break;
3398                 case SP_CSS_UNIT_PX:
3399                     os << key << ":" << val->computed << "px;";
3400                     return g_strlcpy(p, os.str().c_str(), len);
3401                     break;
3402                 case SP_CSS_UNIT_PT:
3403                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3404                     return g_strlcpy(p, os.str().c_str(), len);
3405                     break;
3406                 case SP_CSS_UNIT_PC:
3407                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3408                     return g_strlcpy(p, os.str().c_str(), len);
3409                     break;
3410                 case SP_CSS_UNIT_MM:
3411                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3412                     return g_strlcpy(p, os.str().c_str(), len);
3413                     break;
3414                 case SP_CSS_UNIT_CM:
3415                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3416                     return g_strlcpy(p, os.str().c_str(), len);
3417                     break;
3418                 case SP_CSS_UNIT_IN:
3419                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3420                     return g_strlcpy(p, os.str().c_str(), len);
3421                     break;
3422                 case SP_CSS_UNIT_EM:
3423                     os << key << ":" << val->value << "em;";
3424                     return g_strlcpy(p, os.str().c_str(), len);
3425                     break;
3426                 case SP_CSS_UNIT_EX:
3427                     os << key << ":" << val->value << "ex;";
3428                     return g_strlcpy(p, os.str().c_str(), len);
3429                     break;
3430                 case SP_CSS_UNIT_PERCENT:
3431                     os << key << ":" << (val->value * 100.0) << "%;";
3432                     return g_strlcpy(p, os.str().c_str(), len);
3433                     break;
3434                 default:
3435                     /* Invalid */
3436                     break;
3437             }
3438         }
3439     }
3440     return 0;
3444 /**
3445  *
3446  */
3447 static bool
3448 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3450     if (a->normal != b->normal) return true;
3451     if (a->normal) return false;
3453     if (a->unit != b->unit) {
3454         if (a->unit == SP_CSS_UNIT_EM) return true;
3455         if (a->unit == SP_CSS_UNIT_EX) return true;
3456         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3457         if (b->unit == SP_CSS_UNIT_EM) return true;
3458         if (b->unit == SP_CSS_UNIT_EX) return true;
3459         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3460     }
3462     return (a->computed != b->computed);
3465 /**
3466  * Write SPILengthOrNormal object into string.
3467  */
3468 static gint
3469 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3470                                SPILengthOrNormal const *const val,
3471                                SPILengthOrNormal const *const base,
3472                                guint const flags)
3474     if ((flags & SP_STYLE_FLAG_ALWAYS)
3475         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3476         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3477             && (!base->set || sp_lengthornormal_differ(val, base))))
3478     {
3479         if (val->normal) {
3480             return g_snprintf(p, len, "%s:normal;", key);
3481         } else {
3482             SPILength length;
3483             length.set = val->set;
3484             length.inherit = val->inherit;
3485             length.unit = val->unit;
3486             length.value = val->value;
3487             length.computed = val->computed;
3488             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3489         }
3490     }
3491     return 0;
3494 /**
3495  *
3496  */
3497 static bool
3498 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3500     return    a->underline != b->underline
3501            || a->overline != b->overline
3502            || a->line_through != b->line_through
3503            || a->blink != b->blink;
3506 /**
3507  * Write SPITextDecoration object into string.
3508  */
3509 static gint
3510 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3511                                SPITextDecoration const *const val,
3512                                SPITextDecoration const *const base,
3513                                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_textdecoration_differ(val, base))))
3521     {
3522         if (val->inherit) {
3523             return g_snprintf(p, len, "%s:inherit;", key);
3524         } else {
3525             os << key << ":";
3526             if (val->underline || val->overline || val->line_through || val->blink) {
3527                 if (val->underline) os << " underline";
3528                 if (val->overline) os << " overline";
3529                 if (val->line_through) os << " line-through";
3530                 if (val->blink) os << " blink";
3531             } else
3532                 os << "none";
3533             os << ";";
3534             return g_strlcpy(p, os.str().c_str(), len);
3535         }
3536     }
3537     return 0;
3540 /**
3541  *
3542  */
3543 static bool
3544 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3546     if (a->type != b->type)
3547         return true;
3548     if (a->type == SP_PAINT_TYPE_COLOR)
3549         return !(sp_color_is_equal(&a->value.color, &b->value.color)
3550                  && ((a->value.iccColor == b->value.iccColor)
3551                      || (a->value.iccColor && b->value.iccColor
3552                          && (a->value.iccColor->colorProfile == b->value.iccColor->colorProfile)
3553                          && (a->value.iccColor->colors == b->value.iccColor->colors))));
3554     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3555      * in the string representation. */
3556     if (a->type == SP_PAINT_TYPE_PAINTSERVER)
3557         return (a->value.paint.server != b->value.paint.server);
3558     return false;
3563 /**
3564  * Write SPIPaint object into string.
3565  */
3566 static gint
3567 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3568                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3570     if ((flags & SP_STYLE_FLAG_ALWAYS)
3571         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3572         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3573             && (!base->set || sp_paint_differ(paint, base))))
3574     {
3575         if (paint->inherit) {
3576             return g_snprintf(b, len, "%s:inherit;", key);
3577         } else if (paint->currentcolor) {
3578             return g_snprintf(b, len, "%s:currentColor;", key);
3579         } else {
3580             switch (paint->type) {
3581                 case SP_PAINT_TYPE_COLOR: {
3582                     char color_buf[8];
3583                     sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
3584                     if (paint->value.iccColor) {
3585                         CSSOStringStream css;
3586                         css << color_buf << " icc-color(" << paint->value.iccColor->colorProfile;
3587                         for (vector<double>::const_iterator i(paint->value.iccColor->colors.begin()),
3588                                  iEnd(paint->value.iccColor->colors.end());
3589                              i != iEnd; ++i) {
3590                             css << ", " << *i;
3591                         }
3592                         css << ')';
3593                         return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
3594                     } else {
3595                         return g_snprintf(b, len, "%s:%s;", key, color_buf);
3596                     }
3597                 }
3598                 case SP_PAINT_TYPE_PAINTSERVER:
3599                     return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
3600                 default:
3601                     break;
3602             }
3603             return g_snprintf(b, len, "%s:none;", key);
3604         }
3605     }
3606     return 0;
3610 /**
3611  *
3612  */
3613 static bool
3614 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3616     if (a->type != b->type)
3617         return true;
3618     if (a->type == SP_FONT_SIZE_LENGTH) {
3619         if (a->computed != b->computed)
3620             return true;
3621     } else {
3622         if (a->value != b->value)
3623             return true;
3624     }
3625     return false;
3629 /**
3630  * Write SPIFontSize object into string.
3631  */
3632 static gint
3633 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3634                          SPIFontSize const *const val, SPIFontSize const *const base,
3635                          guint const flags)
3637     if ((flags & SP_STYLE_FLAG_ALWAYS)
3638         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3639         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3640             && (!base->set || sp_fontsize_differ(val, base))))
3641     {
3642         if (val->inherit) {
3643             return g_snprintf(p, len, "%s:inherit;", key);
3644         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3645             for (unsigned i = 0; enum_font_size[i].key; i++) {
3646                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3647                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3648                 }
3649             }
3650         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3651             Inkscape::CSSOStringStream os;
3652             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3653             return g_strlcpy(p, os.str().c_str(), len);
3654         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3655             Inkscape::CSSOStringStream os;
3656             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3657             return g_strlcpy(p, os.str().c_str(), len);
3658         }
3659     }
3660     return 0;
3664 /**
3665  * Write SPIFilter object into string.
3666  */
3667 static gint
3668 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3669                          SPIFilter const *const val, SPIFilter const *const base,
3670                          guint const flags)
3672     if ((flags & SP_STYLE_FLAG_ALWAYS)
3673         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3674         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3675     {
3676         if (val->inherit) {
3677             return g_snprintf(p, len, "%s:inherit;", key);
3678         } else if (val->href && val->href->getURI()) {
3679             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
3680         }
3681     }
3684     return 0;
3688 /**
3689  * Clear paint object, and disconnect style from paintserver (if present).
3690  */
3691 static void
3692 sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
3694     if ((paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
3695         if (paint == &style->fill) {
3696             if (style->fill_hreffed) {
3697                 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3698                 style->fill_hreffed = false;
3699             }
3700             style->fill_release_connection.disconnect();
3701             style->fill_modified_connection.disconnect();
3702         } else {
3703             assert(paint == &style->stroke);  // Only fill & stroke can have a paint server.
3704             if (style->stroke_hreffed) {
3705                 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3706                 style->stroke_hreffed = false;
3707             }
3708             style->stroke_release_connection.disconnect();
3709             style->stroke_modified_connection.disconnect();
3710         }
3712         paint->value.paint.server = NULL;
3713     }
3714     paint->value.paint.uri = NULL;
3715     paint->type = SP_PAINT_TYPE_NONE;
3716     delete paint->value.iccColor;
3717     paint->value.iccColor = NULL;
3721 /**
3722  * Clear filter object, and disconnect style from paintserver (if present).
3723  */
3724 static void
3725 sp_style_filter_clear(SPStyle *style)
3727     if (style->filter.href && style->filter.href->getObject())
3728         style->filter.href->detach(); 
3732 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3734 void
3735 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3737     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3739     if (repr == NULL) return;
3741     g_return_if_fail(linked != NULL);
3743     gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3745     SPCSSAttr *css = sp_repr_css_attr_new();
3746     sp_repr_css_set_property(css, property, val);
3747     g_free(val);
3748     if (recursive) {
3749         sp_repr_css_change_recursive(repr, css, "style");
3750     } else {
3751         sp_repr_css_change(repr, css, "style");
3752     }
3753     sp_repr_css_attr_unref(css);
3757 /**
3758  * Clear all style property attributes in object.
3759  */
3760 void
3761 sp_style_unset_property_attrs(SPObject *o)
3763     if (!o) return;
3765     SPStyle *style = SP_OBJECT_STYLE(o);
3766     if (!style) return;
3768     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3769     if (!repr) return;
3771     if (style->opacity.set) {
3772         repr->setAttribute("opacity", NULL);
3773     }
3774     if (style->color.set) {
3775         repr->setAttribute("color", NULL);
3776     }
3777     if (style->fill.set) {
3778         repr->setAttribute("fill", NULL);
3779     }
3780     if (style->fill_opacity.set) {
3781         repr->setAttribute("fill-opacity", NULL);
3782     }
3783     if (style->fill_rule.set) {
3784         repr->setAttribute("fill-rule", NULL);
3785     }
3786     if (style->stroke.set) {
3787         repr->setAttribute("stroke", NULL);
3788     }
3789     if (style->stroke_width.set) {
3790         repr->setAttribute("stroke-width", NULL);
3791     }
3792     if (style->stroke_linecap.set) {
3793         repr->setAttribute("stroke-linecap", NULL);
3794     }
3795     if (style->stroke_linejoin.set) {
3796         repr->setAttribute("stroke-linejoin", NULL);
3797     }
3798     if (style->marker[SP_MARKER_LOC].set) {
3799         repr->setAttribute("marker", NULL);
3800     }
3801     if (style->marker[SP_MARKER_LOC_START].set) {
3802         repr->setAttribute("marker-start", NULL);
3803     }
3804     if (style->marker[SP_MARKER_LOC_MID].set) {
3805         repr->setAttribute("marker-mid", NULL);
3806     }
3807     if (style->marker[SP_MARKER_LOC_END].set) {
3808         repr->setAttribute("marker-end", NULL);
3809     }
3810     if (style->stroke_opacity.set) {
3811         repr->setAttribute("stroke-opacity", NULL);
3812     }
3813     if (style->stroke_dasharray_set) {
3814         repr->setAttribute("stroke-dasharray", NULL);
3815     }
3816     if (style->stroke_dashoffset_set) {
3817         repr->setAttribute("stroke-dashoffset", NULL);
3818     }
3819     if (style->text_private && style->text->font_family.set) {
3820         repr->setAttribute("font-family", NULL);
3821     }
3822     if (style->text_anchor.set) {
3823         repr->setAttribute("text-anchor", NULL);
3824     }
3825     if (style->writing_mode.set) {
3826         repr->setAttribute("writing_mode", NULL);
3827     }
3828     if (style->filter.set) {
3829         repr->setAttribute("filter", NULL);
3830     }
3831     if (style->enable_background.set) {
3832         repr->setAttribute("enable-background", NULL);
3833     }
3836 /**
3837  * \pre style != NULL.
3838  * \pre flags in {IFSET, ALWAYS}.
3839  */
3840 SPCSSAttr *
3841 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3843     g_return_val_if_fail(style != NULL, NULL);
3844     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3845                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3846                          NULL);
3847     gchar *style_str = sp_style_write_string(style, flags);
3848     SPCSSAttr *css = sp_repr_css_attr_new();
3849     sp_repr_css_attr_add_from_string(css, style_str);
3850     g_free(style_str);
3851     return css;
3855 /**
3856  * \pre object != NULL
3857  * \pre flags in {IFSET, ALWAYS}.
3858  */
3859 SPCSSAttr *
3860 sp_css_attr_from_object(SPObject *object, guint const flags)
3862     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3863                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3864                          NULL);
3865     SPStyle const *const style = SP_OBJECT_STYLE(object);
3866     if (style == NULL)
3867         return NULL;
3868     return sp_css_attr_from_style(style, flags);
3871 /**
3872  * Unset any text-related properties
3873  */
3874 SPCSSAttr *
3875 sp_css_attr_unset_text(SPCSSAttr *css)
3877     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3878     sp_repr_css_set_property(css, "font-size", NULL);
3879     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3880     sp_repr_css_set_property(css, "font-style", NULL);
3881     sp_repr_css_set_property(css, "font-variant", NULL);
3882     sp_repr_css_set_property(css, "font-weight", NULL);
3883     sp_repr_css_set_property(css, "font-stretch", NULL);
3884     sp_repr_css_set_property(css, "font-family", NULL);
3885     sp_repr_css_set_property(css, "text-indent", NULL);
3886     sp_repr_css_set_property(css, "text-align", NULL);
3887     sp_repr_css_set_property(css, "text-decoration", NULL);
3888     sp_repr_css_set_property(css, "line-height", NULL);
3889     sp_repr_css_set_property(css, "letter-spacing", NULL);
3890     sp_repr_css_set_property(css, "word-spacing", NULL);
3891     sp_repr_css_set_property(css, "text-transform", NULL);
3892     sp_repr_css_set_property(css, "direction", NULL);
3893     sp_repr_css_set_property(css, "block-progression", NULL);
3894     sp_repr_css_set_property(css, "writing-mode", NULL);
3895     sp_repr_css_set_property(css, "text-anchor", NULL);
3896     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3897     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3898     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3899     sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3901     return css;
3904 bool
3905 is_url(char const *p)
3907     if (p == NULL)
3908         return false;
3909 /** \todo
3910  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3911  * in property values must start with 'url('.
3912  */
3913     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3916 /**
3917  * Unset any properties that contain URI values.
3918  *
3919  * Used for storing style that will be reused across documents when carrying
3920  * the referenced defs is impractical.
3921  */
3922 SPCSSAttr *
3923 sp_css_attr_unset_uris(SPCSSAttr *css)
3925 // All properties that may hold <uri> or <paint> according to SVG 1.1
3926     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3927     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3928     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3929     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3930     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3931     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3932     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3933     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3934     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3935     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3937     return css;
3940 /**
3941  * Scale a single-value property.
3942  */
3943 void
3944 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3945                                   double ex, bool only_with_units = false)
3947     gchar const *w = sp_repr_css_property(css, property, NULL);
3948     if (w) {
3949         gchar *units = NULL;
3950         double wd = g_ascii_strtod(w, &units) * ex;
3951         if (w == units) {// nothing converted, non-numeric value
3952             return;
3953         }
3954         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3955             // only_with_units, but no units found, so do nothing.
3956             return;
3957         }
3958         Inkscape::CSSOStringStream os;
3959         os << wd << units; // reattach units
3960         sp_repr_css_set_property(css, property, os.str().c_str());
3961     }
3964 /**
3965  * Scale a list-of-values property.
3966  */
3967 void
3968 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3970     gchar const *string = sp_repr_css_property(css, property, NULL);
3971     if (string) {
3972         Inkscape::CSSOStringStream os;
3973         gchar **a = g_strsplit(string, ",", 10000);
3974         bool first = true;
3975         for (gchar **i = a; i != NULL; i++) {
3976             gchar *w = *i;
3977             if (w == NULL)
3978                 break;
3979             gchar *units = NULL;
3980             double wd = g_ascii_strtod(w, &units) * ex;
3981             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3982                 g_strfreev(a);
3983                 return;
3984             }
3985             if (!first) {
3986                 os << ",";
3987             }
3988             os << wd << units; // reattach units
3989             first = false;
3990         }
3991         sp_repr_css_set_property(css, property, os.str().c_str());
3992         g_strfreev(a);
3993     }
3996 /**
3997  * Scale any properties that may hold <length> by ex.
3998  */
3999 SPCSSAttr *
4000 sp_css_attr_scale(SPCSSAttr *css, double ex)
4002     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4003     sp_css_attr_scale_property_single(css, "stroke-width", ex);
4004     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
4005     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4006     sp_css_attr_scale_property_single(css, "font-size", ex);
4007     sp_css_attr_scale_property_single(css, "kerning", ex);
4008     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4009     sp_css_attr_scale_property_single(css, "word-spacing", ex);
4010     sp_css_attr_scale_property_single(css, "line-height", ex, true);
4012     return css;
4016 /**
4017  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4018  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4019  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4020  * it parsed for us. So this function is only used to remove those quotes and for
4021  * presentation attributes, without any unescaping. (XML unescaping
4022  * (&amp; etc) is done by XML parser.)
4023  */
4024 gchar *
4025 attribute_unquote(gchar const *val)
4027     if (val) {
4028         if (*val == '\'' || *val == '"') {
4029             int l = strlen(val);
4030             if (l >= 2) {
4031                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4032                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4033                     return (g_strndup (val+1, l-2));
4034                 }
4035             }
4036         }
4037     }
4039     return (val? g_strdup (val) : NULL);
4042 /**
4043  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4044  */
4045 gchar *
4046 css2_escape_quote(gchar const *val) {
4048     Glib::ustring t;
4049     bool quote = false;
4050     bool last_was_space = false;
4052     for (gchar const *i = val; *i; i++) {
4053         bool is_space = ( *i == ' ' );
4054         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4055             // ASCII alphanumeric, - and _ don't require quotes
4056             t.push_back(*i);
4057         } else if ( is_space && !last_was_space ) {
4058             // non-consecutive spaces don't require quotes
4059             t.push_back(*i);
4060         } else if (*i=='\'') {
4061             // single quotes require escaping and quotes
4062             t.push_back('\\');
4063             t.push_back(*i);
4064             quote = true;
4065         } else {
4066             // everything else requires quotes
4067             t.push_back(*i);
4068             quote = true;
4069         }
4070         if (i == val && !g_ascii_isalpha(*i)) {
4071             // a non-ASCII/non-alpha initial character requires quotes
4072             quote = true;
4073         }
4074         last_was_space = is_space;
4075     }
4077     if (last_was_space) {
4078         // a trailing space requires quotes
4079         quote = true;
4080     }
4082     if (quote) {
4083         // we use single quotes so the result can be stored in an XML
4084         // attribute without incurring un-aesthetic additional quoting
4085         // (our XML emitter always uses double quotes)
4086         t.insert(t.begin(), '\'');
4087         t.push_back('\'');
4088     }
4090     return (t.empty() ? NULL : g_strdup (t.c_str()));
4093 /*
4094   Local Variables:
4095   mode:c++
4096   c-file-style:"stroustrup"
4097   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4098   indent-tabs-mode:nil
4099   fill-column:99
4100   End:
4101 */
4102 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :