Code

Purged fill type enum
[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 #define BMAX 8192
55 class SPStyleEnum;
57 /*#########################
58 ## FORWARD DECLARATIONS
59 #########################*/
60 static void sp_style_clear(SPStyle *style);
62 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
64 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
65 static void sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent);
66 static void sp_style_read_dash(SPStyle *style, gchar const *str);
68 static SPTextStyle *sp_text_style_new(void);
69 static void sp_text_style_clear(SPTextStyle *ts);
70 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
71 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
72 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
73 static void sp_style_privatize_text(SPStyle *style);
75 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
76 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
77 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
78 static void sp_style_read_istring(SPIString *val, gchar const *str);
79 static void sp_style_read_ilength(SPILength *val, gchar const *str);
80 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
81 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
82 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
83 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
84 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
85 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
87 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
88 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
89 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
90 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
92 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
93 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
94 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
95 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
96 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
97 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
98 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
99 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);
100 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);
101 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
103 static void sp_style_filter_clear(SPStyle *style);
105 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
106 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
108 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
109 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
111 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
113 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
114 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
116 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
118 struct SPStyleEnum {
119     gchar const *key;
120     gint value;
121 };
123 static SPStyleEnum const enum_fill_rule[] = {
124     {"nonzero", SP_WIND_RULE_NONZERO},
125     {"evenodd", SP_WIND_RULE_EVENODD},
126     {NULL, -1}
127 };
129 static SPStyleEnum const enum_stroke_linecap[] = {
130     {"butt", SP_STROKE_LINECAP_BUTT},
131     {"round", SP_STROKE_LINECAP_ROUND},
132     {"square", SP_STROKE_LINECAP_SQUARE},
133     {NULL, -1}
134 };
136 static SPStyleEnum const enum_stroke_linejoin[] = {
137     {"miter", SP_STROKE_LINEJOIN_MITER},
138     {"round", SP_STROKE_LINEJOIN_ROUND},
139     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
140     {NULL, -1}
141 };
143 static SPStyleEnum const enum_font_style[] = {
144     {"normal", SP_CSS_FONT_STYLE_NORMAL},
145     {"italic", SP_CSS_FONT_STYLE_ITALIC},
146     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
147     {NULL, -1}
148 };
150 static SPStyleEnum const enum_font_size[] = {
151     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
152     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
153     {"small", SP_CSS_FONT_SIZE_SMALL},
154     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
155     {"large", SP_CSS_FONT_SIZE_LARGE},
156     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
157     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
158     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
159     {"larger", SP_CSS_FONT_SIZE_LARGER},
160     {NULL, -1}
161 };
163 static SPStyleEnum const enum_font_variant[] = {
164     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
165     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
166     {NULL, -1}
167 };
169 static SPStyleEnum const enum_font_weight[] = {
170     {"100", SP_CSS_FONT_WEIGHT_100},
171     {"200", SP_CSS_FONT_WEIGHT_200},
172     {"300", SP_CSS_FONT_WEIGHT_300},
173     {"400", SP_CSS_FONT_WEIGHT_400},
174     {"500", SP_CSS_FONT_WEIGHT_500},
175     {"600", SP_CSS_FONT_WEIGHT_600},
176     {"700", SP_CSS_FONT_WEIGHT_700},
177     {"800", SP_CSS_FONT_WEIGHT_800},
178     {"900", SP_CSS_FONT_WEIGHT_900},
179     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
180     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
181     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
182     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
183     {NULL, -1}
184 };
186 static SPStyleEnum const enum_font_stretch[] = {
187     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
188     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
189     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
190     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
191     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
192     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
193     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
194     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
195     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
196     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
197     {"wider", SP_CSS_FONT_STRETCH_WIDER},
198     {NULL, -1}
199 };
201 static SPStyleEnum const enum_text_align[] = {
202     {"start", SP_CSS_TEXT_ALIGN_START},
203     {"end", SP_CSS_TEXT_ALIGN_END},
204     {"left", SP_CSS_TEXT_ALIGN_LEFT},
205     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
206     {"center", SP_CSS_TEXT_ALIGN_CENTER},
207     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
208     {NULL, -1}
209 };
211 static SPStyleEnum const enum_text_transform[] = {
212     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
213     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
214     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
215     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
216     {NULL, -1}
217 };
219 static SPStyleEnum const enum_text_anchor[] = {
220     {"start", SP_CSS_TEXT_ANCHOR_START},
221     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
222     {"end", SP_CSS_TEXT_ANCHOR_END},
223     {NULL, -1}
224 };
226 static SPStyleEnum const enum_direction[] = {
227     {"ltr", SP_CSS_DIRECTION_LTR},
228     {"rtl", SP_CSS_DIRECTION_RTL},
229     {NULL, -1}
230 };
232 static SPStyleEnum const enum_block_progression[] = {
233     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
234     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
235     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
236     {NULL, -1}
237 };
239 static SPStyleEnum const enum_writing_mode[] = {
240     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
241      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
242      * value is lr-tb rather than lr.
243      *
244      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
245      * sharing enumerators for different strings may be a bug (once we support ecma script).
246      */
247     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
248     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
249     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
250     {"lr", SP_CSS_WRITING_MODE_LR_TB},
251     {"rl", SP_CSS_WRITING_MODE_RL_TB},
252     {"tb", SP_CSS_WRITING_MODE_TB_RL},
253     {NULL, -1}
254 };
256 static SPStyleEnum const enum_visibility[] = {
257     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
258     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
259     {"visible", SP_CSS_VISIBILITY_VISIBLE},
260     {NULL, -1}
261 };
263 static SPStyleEnum const enum_overflow[] = {
264     {"visible", SP_CSS_OVERFLOW_VISIBLE},
265     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
266     {"scroll", SP_CSS_OVERFLOW_SCROLL},
267     {"auto", SP_CSS_OVERFLOW_AUTO},
268     {NULL, -1}
269 };
271 static SPStyleEnum const enum_display[] = {
272     {"none",      SP_CSS_DISPLAY_NONE},
273     {"inline",    SP_CSS_DISPLAY_INLINE},
274     {"block",     SP_CSS_DISPLAY_BLOCK},
275     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
276     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
277     {"compact",   SP_CSS_DISPLAY_COMPACT},
278     {"marker",    SP_CSS_DISPLAY_MARKER},
279     {"table",     SP_CSS_DISPLAY_TABLE},
280     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
281     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
282     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
283     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
284     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
285     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
286     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
287     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
288     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
289     {NULL, -1}
290 };
292 static SPStyleEnum const enum_shape_rendering[] = {
293     {"auto", 0},
294     {"optimizeSpeed", 0},
295     {"crispEdges", 0},
296     {"geometricPrecision", 0},
297     {NULL, -1}
298 };
300 static SPStyleEnum const enum_color_rendering[] = {
301     {"auto", 0},
302     {"optimizeSpeed", 0},
303     {"optimizeQuality", 0},
304     {NULL, -1}
305 };
307 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
309 static SPStyleEnum const enum_text_rendering[] = {
310     {"auto", 0},
311     {"optimizeSpeed", 0},
312     {"optimizeLegibility", 0},
313     {"geometricPrecision", 0},
314     {NULL, -1}
315 };
317 static SPStyleEnum const enum_enable_background[] = {
318     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
319     {"new", SP_CSS_BACKGROUND_NEW},
320     {NULL, -1}
321 };
323 /**
324  * Release callback.
325  */
326 static void
327 sp_style_object_release(SPObject *object, SPStyle *style)
329     (void)object; // TODO
330     style->object = NULL;
333 /**
334  * Emit style modified signal on style's object if the filter changed.
335  */
336 static void
337 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
339     (void)flags; // TODO
340     SPFilter *filter=static_cast<SPFilter *>(obj);
341     if (style->getFilter() == filter)
342     {
343         if (style->object) {
344             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
345         }
346     }
349 /**
350  * Gets called when the filter is (re)attached to the style
351  */
352 static void
353 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
355     if (old_ref) {
356         style->filter_modified_connection.disconnect();
357     }
358     if ( SP_IS_FILTER(ref))
359     {
360         style->filter_modified_connection = 
361            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
362     }
364     sp_style_filter_ref_modified(ref, 0, style);
367 /**
368  * Emit style modified signal on style's object if server is style's fill
369  * or stroke paint server.
370  */
371 static void
372 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
374     (void)flags; // TODO
375     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
377     if ((style->fill.isPaintserver())
378         && style->getFillPaintServer() == server)
379     {
380         if (style->object) {
381             /** \todo
382              * fixme: I do not know, whether it is optimal - we are
383              * forcing reread of everything (Lauris)
384              */
385             /** \todo
386              * fixme: We have to use object_modified flag, because parent
387              * flag is only available downstreams.
388              */
389             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
390         }
391     } else if ((style->stroke.isPaintserver())
392         && style->getStrokePaintServer() == server)
393     {
394         if (style->object) {
395             /// \todo fixme:
396             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
397         }
398     } else if (server) {
399         g_assert_not_reached();
400     }
403 /**
404  * Gets called when the paintserver is (re)attached to the style
405  */
406 static void
407 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
409     if (old_ref) {
410         style->fill_ps_modified_connection.disconnect();
411     }
412     if (SP_IS_PAINT_SERVER(ref)) {
413         style->fill_ps_modified_connection = 
414            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
415     }
417     sp_style_paint_server_ref_modified(ref, 0, style);
420 /**
421  * Gets called when the paintserver is (re)attached to the style
422  */
423 static void
424 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
426     if (old_ref) {
427         style->stroke_ps_modified_connection.disconnect();
428     }
429     if (SP_IS_PAINT_SERVER(ref)) {
430         style->stroke_ps_modified_connection = 
431           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
432     }
434     sp_style_paint_server_ref_modified(ref, 0, style);
437 /**
438  * Returns a new SPStyle object with settings as per sp_style_clear().
439  */
440 SPStyle *
441 sp_style_new(SPDocument *document)
443     SPStyle *const style = g_new0(SPStyle, 1);
445     style->refcount = 1;
446     style->object = NULL;
447     style->document = document;
448     style->text = sp_text_style_new();
449     style->text_private = TRUE;
451     sp_style_clear(style);
453     style->cloned = false;
455     new (&style->release_connection) sigc::connection();
456     new (&style->filter_modified_connection) sigc::connection();
457     new (&style->fill_ps_modified_connection) sigc::connection();
458     new (&style->stroke_ps_modified_connection) sigc::connection();
460     return style;
464 /**
465  * Creates a new SPStyle object, and attaches it to the specified SPObject.
466  */
467 SPStyle *
468 sp_style_new_from_object(SPObject *object)
470     g_return_val_if_fail(object != NULL, NULL);
471     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
473     SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
474     style->object = object;
475     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
477     if (object && SP_OBJECT_IS_CLONED(object)) {
478         style->cloned = true;
479     }
481     return style;
485 /**
486  * Increase refcount of style.
487  */
488 SPStyle *
489 sp_style_ref(SPStyle *style)
491     g_return_val_if_fail(style != NULL, NULL);
492     g_return_val_if_fail(style->refcount > 0, NULL);
494     style->refcount += 1;
496     return style;
500 /**
501  * Decrease refcount of style with possible destruction.
502  */
503 SPStyle *
504 sp_style_unref(SPStyle *style)
506     g_return_val_if_fail(style != NULL, NULL);
507     g_return_val_if_fail(style->refcount > 0, NULL);
509     style->refcount -= 1;
511     if (style->refcount < 1) {
512         style->release_connection.disconnect();
513         style->release_connection.~connection();
514         if (style->text) sp_text_style_unref(style->text);
516          if (style->fill.value.href) {
517              style->fill_ps_modified_connection.disconnect();
518              delete style->fill.value.href;
519              style->fill.value.href = NULL;
520          }
521          if (style->stroke.value.href) {
522              style->stroke_ps_modified_connection.disconnect();
523              delete style->stroke.value.href;
524              style->stroke.value.href = NULL;
525          }
526          if (style->filter.href) {
527              style->filter_modified_connection.disconnect();
528              delete style->filter.href;
529              style->filter.href = NULL;
530          }
532         style->filter_modified_connection.~connection();
533         style->fill_ps_modified_connection.~connection();
534         style->stroke_ps_modified_connection.~connection();
536         style->fill.clear();
537         style->stroke.clear();
538         sp_style_filter_clear(style);
540         g_free(style->stroke_dash.dash);
541         g_free(style);
542     }
544     return NULL;
547 /**
548  *  Reads the various style parameters for an object from repr.
549  */
550 static void
551 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
553     g_assert(style != NULL);
554     g_assert(repr != NULL);
555     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
557     sp_style_clear(style);
559     if (object && SP_OBJECT_IS_CLONED(object)) {
560         style->cloned = true;
561     }
563     /* 1. Style attribute */
564     gchar const *val = repr->attribute("style");
565     if (val != NULL) {
566         sp_style_merge_from_style_string(style, val);
567     }
569     if (object) {
570         sp_style_merge_from_object_stylesheet(style, object);
571     } else {
572         /** \todo No stylesheet information. Find out under what circumstances
573          * this occurs, and handle accordingly.  (If we really wanted to, we
574          * could probably get stylesheets by going through repr->doc.)
575          */
576     }
578     /* 2. Presentation attributes */
579     /* CSS2 */
580     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
581     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
582     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
583     /* Font */
584     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
585     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
586     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
587     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
588     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
589     /* Text (css2 chapter 16) */
590     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
591     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
592     if (!style->text_decoration.set) {
593         val = repr->attribute("text-decoration");
594         if (val) {
595             sp_style_read_itextdecoration(&style->text_decoration, val);
596         }
597     }
598     if (!style->line_height.set) {
599         val = repr->attribute("line-height");
600         if (val) {
601             sp_style_read_ilengthornormal(&style->line_height, val);
602         }
603     }
604     if (!style->letter_spacing.set) {
605         val = repr->attribute("letter-spacing");
606         if (val) {
607             sp_style_read_ilengthornormal(&style->letter_spacing, val);
608         }
609     }
610     if (!style->word_spacing.set) {
611         val = repr->attribute("word-spacing");
612         if (val) {
613             sp_style_read_ilengthornormal(&style->word_spacing, val);
614         }
615     }
616     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
617     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
618     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
620     /* SVG */
621     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
622                             enum_writing_mode, true);
623     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
624                             enum_text_anchor, true);
626     /* opacity */
627     if (!style->opacity.set) {
628         val = repr->attribute("opacity");
629         if (val) {
630             sp_style_read_iscale24(&style->opacity, val);
631         }
632     }
633     /* color */
634     if (!style->color.set) {
635         val = repr->attribute("color");
636         if (val) {
637             sp_style_read_icolor(&style->color, val, style, ( object
638                                                               ? SP_OBJECT_DOCUMENT(object)
639                                                               : NULL ));
640         }
641     }
642     /* fill */
643     if (!style->fill.set) {
644         val = repr->attribute("fill");
645         if (val) {
646             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
647         }
648     }
649     /* fill-opacity */
650     if (!style->fill_opacity.set) {
651         val = repr->attribute("fill-opacity");
652         if (val) {
653             sp_style_read_iscale24(&style->fill_opacity, val);
654         }
655     }
656     /* fill-rule */
657     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
658     /* stroke */
659     if (!style->stroke.set) {
660         val = repr->attribute("stroke");
661         if (val) {
662             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
663         }
664     }
665     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
666     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
667     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
668     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
670     /* markers */
671     if (!style->marker[SP_MARKER_LOC].set) {
672         val = repr->attribute("marker");
673         if (val) {
674             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
675         }
676     }
677     if (!style->marker[SP_MARKER_LOC_START].set) {
678         val = repr->attribute("marker-start");
679         if (val) {
680             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
681         }
682     }
683     if (!style->marker[SP_MARKER_LOC_MID].set) {
684         val = repr->attribute("marker-mid");
685         if (val) {
686             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
687         }
688     }
689     if (!style->marker[SP_MARKER_LOC_END].set) {
690         val = repr->attribute("marker-end");
691         if (val) {
692             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
693         }
694     }
696     /* stroke-opacity */
697     if (!style->stroke_opacity.set) {
698         val = repr->attribute("stroke-opacity");
699         if (val) {
700             sp_style_read_iscale24(&style->stroke_opacity, val);
701         }
702     }
703     if (!style->stroke_dasharray_set) {
704         val = repr->attribute("stroke-dasharray");
705         if (val) {
706             sp_style_read_dash(style, val);
707         }
708     }
709     if (!style->stroke_dashoffset_set) {
710         /* fixme */
711         val = repr->attribute("stroke-dashoffset");
712         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
713             style->stroke_dashoffset_set = TRUE;
714         } else {
715             style->stroke_dashoffset_set = FALSE;
716         }
717     }
719     /* font-family */
720     if (!style->text_private || !style->text->font_family.set) {
721         val = repr->attribute("font-family");
722         if (val) {
723             if (!style->text_private) sp_style_privatize_text(style);
724             gchar *val_unquoted = attribute_unquote(val);
725             sp_style_read_istring(&style->text->font_family, val_unquoted);
726             if (val_unquoted) g_free (val_unquoted);
727         }
728     }
730     /* filter effects */
731     if (!style->filter.set) {
732         val = repr->attribute("filter");
733         if (val) {
734                       sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
735         }
736     }
737     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
738                             "enable-background", enum_enable_background, true);
740     /* 3. Merge from parent */
741     if (object) {
742         if (object->parent) {
743             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
744         }
745     } else {
746         if (sp_repr_parent(repr)) {
747             /// \todo fixme: This is not the prettiest thing (Lauris)
748             SPStyle *parent = sp_style_new(NULL);
749             sp_style_read(parent, NULL, sp_repr_parent(repr));
750             sp_style_merge_from_parent(style, parent);
751             sp_style_unref(parent);
752         }
753     }
757 /**
758  * Read style properties from object's repr.
759  *
760  * 1. Reset existing object style
761  * 2. Load current effective object style
762  * 3. Load i attributes from immediate parent (which has to be up-to-date)
763  */
764 void
765 sp_style_read_from_object(SPStyle *style, SPObject *object)
767     g_return_if_fail(style != NULL);
768     g_return_if_fail(object != NULL);
769     g_return_if_fail(SP_IS_OBJECT(object));
771     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
772     g_return_if_fail(repr != NULL);
774     sp_style_read(style, object, repr);
778 /**
779  * Read style properties from repr only.
780  */
781 void
782 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
784     g_return_if_fail(style != NULL);
785     g_return_if_fail(repr != NULL);
787     sp_style_read(style, NULL, repr);
792 /**
793  *
794  */
795 static void
796 sp_style_privatize_text(SPStyle *style)
798     SPTextStyle *text = style->text;
799     style->text = sp_text_style_duplicate_unset(style->text);
800     sp_text_style_unref(text);
801     style->text_private = TRUE;
805 /**
806  * Merge property into style.
807  *
808  * Should be called in order of highest to lowest precedence.
809  * E.g. for a single style string, call from the last declaration to the first,
810  * as CSS says that later declarations override earlier ones.
811  *
812  * \pre val != NULL.
813  */
814 static void
815 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
817     g_return_if_fail(val != NULL);
819     switch (id) {
820         /* CSS2 */
821         /* Font */
822         case SP_PROP_FONT_FAMILY:
823             if (!style->text_private) sp_style_privatize_text(style);
824             if (!style->text->font_family.set) {
825                 gchar *val_unquoted = attribute_unquote(val);
826                 sp_style_read_istring(&style->text->font_family, val_unquoted);
827                 if (val_unquoted) g_free (val_unquoted);
828             }
829             break;
830         case SP_PROP_FONT_SIZE:
831             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
832             break;
833         case SP_PROP_FONT_SIZE_ADJUST:
834             if (strcmp(val, "none") != 0) {
835                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
836             }
837             break;
838         case SP_PROP_FONT_STYLE:
839             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
840             break;
841         case SP_PROP_FONT_VARIANT:
842             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
843             break;
844         case SP_PROP_FONT_WEIGHT:
845             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
846             break;
847         case SP_PROP_FONT_STRETCH:
848             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
849             break;
850         case SP_PROP_FONT:
851             if (!style->text_private) sp_style_privatize_text(style);
852             if (!style->text->font.set) {
853                 g_free(style->text->font.value);
854                 style->text->font.value = g_strdup(val);
855                 style->text->font.set = TRUE;
856                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
857             }
858             break;
859             /* Text */
860         case SP_PROP_TEXT_INDENT:
861             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
862             break;
863         case SP_PROP_TEXT_ALIGN:
864             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
865             break;
866         case SP_PROP_TEXT_DECORATION:
867             if (!style->text_decoration.set) {
868                 sp_style_read_itextdecoration(&style->text_decoration, val);
869             }
870             break;
871         case SP_PROP_LINE_HEIGHT:
872             if (!style->line_height.set) {
873                 sp_style_read_ilengthornormal(&style->line_height, val);
874             }
875             break;
876         case SP_PROP_LETTER_SPACING:
877             if (!style->letter_spacing.set) {
878                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
879             }
880             break;
881         case SP_PROP_WORD_SPACING:
882             if (!style->word_spacing.set) {
883                 sp_style_read_ilengthornormal(&style->word_spacing, val);
884             }
885             break;
886         case SP_PROP_TEXT_TRANSFORM:
887             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
888             break;
889             /* Text (css3) */
890         case SP_PROP_DIRECTION:
891             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
892             break;
893         case SP_PROP_BLOCK_PROGRESSION:
894             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
895             break;
896         case SP_PROP_WRITING_MODE:
897             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
898             break;
899         case SP_PROP_TEXT_ANCHOR:
900             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
901             break;
902             /* Text (unimplemented) */
903         case SP_PROP_TEXT_RENDERING: {
904             /* Ignore the hint. */
905             SPIEnum dummy;
906             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
907             break;
908         }
909         case SP_PROP_ALIGNMENT_BASELINE:
910             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
911             break;
912         case SP_PROP_BASELINE_SHIFT:
913             g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
914             break;
915         case SP_PROP_DOMINANT_BASELINE:
916             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
917             break;
918         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
919             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
920             break;
921         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
922             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
923             break;
924         case SP_PROP_KERNING:
925             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
926             break;
927             /* Misc */
928         case SP_PROP_CLIP:
929             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
930             break;
931         case SP_PROP_COLOR:
932             if (!style->color.set) {
933                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
934             }
935             break;
936         case SP_PROP_CURSOR:
937             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
938             break;
939         case SP_PROP_DISPLAY:
940             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
941             break;
942         case SP_PROP_OVERFLOW:
943             /** \todo
944              * FIXME: not supported properly yet, we just read and write it,
945              * but act as if it is always "display".
946              */
947             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
948             break;
949         case SP_PROP_VISIBILITY:
950             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
951             break;
952             /* SVG */
953             /* Clip/Mask */
954         case SP_PROP_CLIP_PATH:
955             g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
956             break;
957         case SP_PROP_CLIP_RULE:
958             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
959             break;
960         case SP_PROP_MASK:
961             g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
962             break;
963         case SP_PROP_OPACITY:
964             if (!style->opacity.set) {
965                 sp_style_read_iscale24(&style->opacity, val);
966             }
967             break;
968         case SP_PROP_ENABLE_BACKGROUND:
969             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
970                                     enum_enable_background, true);
971             break;
972             /* Filter */
973         case SP_PROP_FILTER:
974             if (!style->filter.set && !style->filter.inherit) {
975                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
976             }
977             break;
978         case SP_PROP_FLOOD_COLOR:
979             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
980             break;
981         case SP_PROP_FLOOD_OPACITY:
982             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
983             break;
984         case SP_PROP_LIGHTING_COLOR:
985             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
986             break;
987             /* Gradient */
988         case SP_PROP_STOP_COLOR:
989             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
990             break;
991         case SP_PROP_STOP_OPACITY:
992             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
993             break;
994             /* Interactivity */
995         case SP_PROP_POINTER_EVENTS:
996             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
997             break;
998             /* Paint */
999         case SP_PROP_COLOR_INTERPOLATION:
1000             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1001             break;
1002         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1003             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1004             break;
1005         case SP_PROP_COLOR_PROFILE:
1006             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1007             break;
1008         case SP_PROP_COLOR_RENDERING: {
1009             /* Ignore the hint. */
1010             SPIEnum dummy;
1011             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1012             break;
1013         }
1014         case SP_PROP_FILL:
1015             if (!style->fill.set) {
1016                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1017             }
1018             break;
1019         case SP_PROP_FILL_OPACITY:
1020             if (!style->fill_opacity.set) {
1021                 sp_style_read_iscale24(&style->fill_opacity, val);
1022             }
1023             break;
1024         case SP_PROP_FILL_RULE:
1025             if (!style->fill_rule.set) {
1026                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1027             }
1028             break;
1029         case SP_PROP_IMAGE_RENDERING: {
1030             /* Ignore the hint. */
1031             SPIEnum dummy;
1032             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1033             break;
1034         }
1035         case SP_PROP_MARKER:
1036             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1037             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1038             if (!style->marker[SP_MARKER_LOC].set) {
1039                 g_free(style->marker[SP_MARKER_LOC].value);
1040                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1041                 style->marker[SP_MARKER_LOC].set = TRUE;
1042                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1043             }
1044             break;
1046         case SP_PROP_MARKER_START:
1047             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1048             if (!style->marker[SP_MARKER_LOC_START].set) {
1049                 g_free(style->marker[SP_MARKER_LOC_START].value);
1050                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1051                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1052                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1053             }
1054             break;
1055         case SP_PROP_MARKER_MID:
1056             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1057             if (!style->marker[SP_MARKER_LOC_MID].set) {
1058                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1059                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1060                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1061                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1062             }
1063             break;
1064         case SP_PROP_MARKER_END:
1065             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1066             if (!style->marker[SP_MARKER_LOC_END].set) {
1067                 g_free(style->marker[SP_MARKER_LOC_END].value);
1068                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1069                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1070                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1071             }
1072             break;
1074         case SP_PROP_SHAPE_RENDERING: {
1075             /* Ignore the hint. */
1076             SPIEnum dummy;
1077             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1078             break;
1079         }
1081         case SP_PROP_STROKE:
1082             if (!style->stroke.set) {
1083                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1084             }
1085             break;
1086         case SP_PROP_STROKE_WIDTH:
1087             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1088             break;
1089         case SP_PROP_STROKE_DASHARRAY:
1090             if (!style->stroke_dasharray_set) {
1091                 sp_style_read_dash(style, val);
1092             }
1093             break;
1094         case SP_PROP_STROKE_DASHOFFSET:
1095             if (!style->stroke_dashoffset_set) {
1096                 /* fixme */
1097                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1098                     style->stroke_dashoffset_set = TRUE;
1099                 } else {
1100                     style->stroke_dashoffset_set = FALSE;
1101                 }
1102             }
1103             break;
1104         case SP_PROP_STROKE_LINECAP:
1105             if (!style->stroke_linecap.set) {
1106                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1107             }
1108             break;
1109         case SP_PROP_STROKE_LINEJOIN:
1110             if (!style->stroke_linejoin.set) {
1111                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1112             }
1113             break;
1114         case SP_PROP_STROKE_MITERLIMIT:
1115             if (!style->stroke_miterlimit.set) {
1116                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1117             }
1118             break;
1119         case SP_PROP_STROKE_OPACITY:
1120             if (!style->stroke_opacity.set) {
1121                 sp_style_read_iscale24(&style->stroke_opacity, val);
1122             }
1123             break;
1125         default:
1126             g_warning("Invalid style property id: %d value: %s", id, val);
1127             break;
1128     }
1131 static void
1132 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1134     /** \todo Ensure that property is lcased, as per
1135      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1136      * Should probably be done in libcroco.
1137      */
1138     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1139     if (prop_idx != SP_ATTR_INVALID) {
1140         /** \todo
1141          * effic: Test whether the property is already set before trying to
1142          * convert to string. Alternatively, set from CRTerm directly rather
1143          * than converting to string.
1144          */
1145         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1146         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1147         sp_style_merge_property(style, prop_idx, str_value);
1148         g_free(str_value);
1149     }
1152 static void
1153 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1155 #if 0 /* forwards */
1156     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1157         CRDeclaration *decl = NULL;
1158         cr_prop_list_get_decl(cur, &decl);
1159         sp_style_merge_style_from_decl(style, decl);
1160     }
1161 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1162     if (props) {
1163         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1164         CRDeclaration *decl = NULL;
1165         cr_prop_list_get_decl(props, &decl);
1166         sp_style_merge_style_from_decl(style, decl);
1167     }
1168 #endif
1171 /**
1172  * \pre decl_list != NULL
1173  */
1174 static void
1175 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1177     // read the decls from end to start, using head recursion, so that latter declarations override
1178     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1179     // because sp_style_merge_style_from_decl only sets properties that are unset
1180     if (decl_list->next) {
1181         sp_style_merge_from_decl_list(style, decl_list->next);
1182     }
1183     sp_style_merge_style_from_decl(style, decl_list);
1186 static CRSelEng *
1187 sp_repr_sel_eng()
1189     CRSelEng *const ret = cr_sel_eng_new();
1190     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1192     /** \todo
1193      * Check whether we need to register any pseudo-class handlers.
1194      * libcroco has its own default handlers for first-child and lang.
1195      *
1196      * We probably want handlers for link and arguably visited (though
1197      * inkscape can't visit links at the time of writing).  hover etc.
1198      * more useful in inkview than the editor inkscape.
1199      *
1200      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1201      * the following should be honoured, at least by inkview:
1202      * :hover, :active, :focus, :visited, :link.
1203      */
1205     g_assert(ret);
1206     return ret;
1209 static void
1210 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1212     static CRSelEng *sel_eng = NULL;
1213     if (!sel_eng) {
1214         sel_eng = sp_repr_sel_eng();
1215     }
1217     CRPropList *props = NULL;
1218     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1219                                                                      object->document->style_cascade,
1220                                                                      object->repr,
1221                                                                      &props);
1222     g_return_if_fail(status == CR_OK);
1223     /// \todo Check what errors can occur, and handle them properly.
1224     if (props) {
1225         sp_style_merge_from_props(style, props);
1226         cr_prop_list_destroy(props);
1227     }
1230 /**
1231  * Parses a style="..." string and merges it with an existing SPStyle.
1232  */
1233 void
1234 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1236     /*
1237      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1238      * ``When CSS styling is used, CSS inline style is specified by including
1239      * semicolon-separated property declarations of the form "name : value"
1240      * within the style attribute''.
1241      *
1242      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1243      * Why does it say "including", what else is allowed in the style
1244      * attribute value?
1245      */
1247     CRDeclaration *const decl_list
1248         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1249     if (decl_list) {
1250         sp_style_merge_from_decl_list(style, decl_list);
1251         cr_declaration_destroy(decl_list);
1252     }
1255 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1256 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1258 static void
1259 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1261     /* 'font-size' */
1262     if (!child.set || child.inherit) {
1263         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1264         child.computed = parent.computed;
1265     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1266         /** \todo
1267          * fixme: SVG and CSS do not specify clearly, whether we should use
1268          * user or screen coordinates (Lauris)
1269          */
1270         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1271             child.computed = font_size_table[child.value];
1272         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1273             child.computed = parent.computed / 1.2;
1274         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1275             child.computed = parent.computed * 1.2;
1276         } else {
1277             /* Illegal value */
1278         }
1279     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1280         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1281          * rather than viewport. */
1282         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1283     }
1286 /**
1287  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1288  * calculating from) corresponding computed values of \a parent.
1289  *
1290  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1291  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1292  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1293  * about what the computed value is (not obvious for lengths).
1294  *
1295  * \pre \a parent's computed values are already up-to-date.
1296  */
1297 void
1298 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1300     g_return_if_fail(style != NULL);
1302     /** \todo
1303      * fixme: Check for existing callers that might pass null parent.
1304      * This should probably be g_return_if_fail, or else we should make a
1305      * best attempt to set computed values correctly without having a parent
1306      * (i.e., by assuming parent has initial values).
1307      */
1308     if (!parent)
1309         return;
1311     /* CSS2 */
1312     /* Font */
1313     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1315     /* 'font-style' */
1316     if (!style->font_style.set || style->font_style.inherit) {
1317         style->font_style.computed = parent->font_style.computed;
1318     }
1320     /* 'font-variant' */
1321     if (!style->font_variant.set || style->font_variant.inherit) {
1322         style->font_variant.computed = parent->font_variant.computed;
1323     }
1325     /* 'font-weight' */
1326     if (!style->font_weight.set || style->font_weight.inherit) {
1327         style->font_weight.computed = parent->font_weight.computed;
1328     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1329         /** \todo
1330          * fixme: This is unconditional, i.e., happens even if parent not
1331          * present.
1332          */
1333         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1334     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1335         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1336     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1337         unsigned const parent_val = parent->font_weight.computed;
1338         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1339         // strictly, 'bolder' and 'lighter' should go to the next weight
1340         // expressible in the current font family, but that's difficult to
1341         // find out, so jumping by 3 seems an appropriate approximation
1342         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1343                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1344                                        : parent_val - 3);
1345         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1346     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1347         unsigned const parent_val = parent->font_weight.computed;
1348         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1349         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1350                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1351                                        : parent_val + 3);
1352         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1353     }
1355     /* 'font-stretch' */
1356     if (!style->font_stretch.set || style->font_stretch.inherit) {
1357         style->font_stretch.computed = parent->font_stretch.computed;
1358     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1359         unsigned const parent_val = parent->font_stretch.computed;
1360         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1361                                         ? parent_val
1362                                         : parent_val - 1);
1363         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1364     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1365         unsigned const parent_val = parent->font_stretch.computed;
1366         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1367         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1368                                         ? parent_val
1369                                         : parent_val + 1);
1370         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1371     }
1373     /* text (css2) */
1374     if (!style->text_indent.set || style->text_indent.inherit) {
1375         style->text_indent.computed = parent->text_indent.computed;
1376     }
1378     if (!style->text_align.set || style->text_align.inherit) {
1379         style->text_align.computed = parent->text_align.computed;
1380     }
1382     if (!style->text_decoration.set || style->text_decoration.inherit) {
1383         style->text_decoration.underline = parent->text_decoration.underline;
1384         style->text_decoration.overline = parent->text_decoration.overline;
1385         style->text_decoration.line_through = parent->text_decoration.line_through;
1386         style->text_decoration.blink = parent->text_decoration.blink;
1387     }
1389     if (!style->line_height.set || style->line_height.inherit) {
1390         style->line_height.computed = parent->line_height.computed;
1391         style->line_height.normal = parent->line_height.normal;
1392     }
1394     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1395         style->letter_spacing.computed = parent->letter_spacing.computed;
1396         style->letter_spacing.normal = parent->letter_spacing.normal;
1397     }
1399     if (!style->word_spacing.set || style->word_spacing.inherit) {
1400         style->word_spacing.computed = parent->word_spacing.computed;
1401         style->word_spacing.normal = parent->word_spacing.normal;
1402     }
1404     if (!style->text_transform.set || style->text_transform.inherit) {
1405         style->text_transform.computed = parent->text_transform.computed;
1406     }
1408     if (!style->direction.set || style->direction.inherit) {
1409         style->direction.computed = parent->direction.computed;
1410     }
1412     if (!style->block_progression.set || style->block_progression.inherit) {
1413         style->block_progression.computed = parent->block_progression.computed;
1414     }
1416     if (!style->writing_mode.set || style->writing_mode.inherit) {
1417         style->writing_mode.computed = parent->writing_mode.computed;
1418     }
1420     if (!style->text_anchor.set || style->text_anchor.inherit) {
1421         style->text_anchor.computed = parent->text_anchor.computed;
1422     }
1424     if (style->opacity.inherit) {
1425         style->opacity.value = parent->opacity.value;
1426     }
1428     /* Color */
1429     if (!style->color.set || style->color.inherit) {
1430         sp_style_merge_ipaint(style, &style->color, &parent->color);
1431     }
1433     /* Fill */
1434     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1435         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1436     }
1438     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1439         style->fill_opacity.value = parent->fill_opacity.value;
1440     }
1442     if (!style->fill_rule.set || style->fill_rule.inherit) {
1443         style->fill_rule.computed = parent->fill_rule.computed;
1444     }
1446     /* Stroke */
1447     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1448         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1449     }
1451     if (!style->stroke_width.set || style->stroke_width.inherit) {
1452         style->stroke_width.computed = parent->stroke_width.computed;
1453     } else {
1454         /* Update computed value for any change in font inherited from parent. */
1455         double const em = style->font_size.computed;
1456         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1457             style->stroke_width.computed = style->stroke_width.value * em;
1458         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1459             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1460             style->stroke_width.computed = style->stroke_width.value * ex;
1461         }
1462     }
1464     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1465         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1466     }
1468     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1469         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1470     }
1472     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1473         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1474     }
1476     if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1477         /** \todo
1478          * This code looks wrong.  Why does the logic differ from the
1479          * above properties? Similarly dashoffset below.
1480          */
1481         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1482         if (style->stroke_dash.n_dash > 0) {
1483             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1484             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1485         }
1486     }
1488     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1489         style->stroke_dash.offset = parent->stroke_dash.offset;
1490     }
1492     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1493         style->stroke_opacity.value = parent->stroke_opacity.value;
1494     }
1496     if (style->text && parent->text) {
1497         if (!style->text->font_family.set || style->text->font_family.inherit) {
1498             g_free(style->text->font_family.value);
1499             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1500         }
1501     }
1503     /* Markers - Free the old value and make copy of the new */
1504     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1505         if (!style->marker[i].set || style->marker[i].inherit) {
1506             g_free(style->marker[i].value);
1507             style->marker[i].value = g_strdup(parent->marker[i].value);
1508         }
1509     }
1511     /* Filter effects */
1512     if (style->filter.inherit) {
1513         sp_style_merge_ifilter(style, &parent->filter);
1514     }
1516     if(style->enable_background.inherit) {
1517         style->enable_background.value = parent->enable_background.value;
1518     }
1521 template <typename T>
1522 static void
1523 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1525     if ( ( !(child.set) || child.inherit )
1526          && parent.set && !(parent.inherit) )
1527     {
1528         child = parent;
1529     }
1532 /**
1533  * Copy SPIString from parent to child.
1534  */
1535 static void
1536 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1538     if ( ( !(child.set) || child.inherit )
1539          && parent.set && !(parent.inherit) )
1540     {
1541         g_free(child.value);
1542         child.value = g_strdup(parent.value);
1543         child.set = parent.set;
1544         child.inherit = parent.inherit;
1545     }
1548 static void
1549 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1550                                             SPIPaint &child, SPIPaint const &parent)
1552     /** \todo
1553      * I haven't given this much attention.  See comments below about
1554      * currentColor, colorProfile, and relative URIs.
1555      */
1556     if (!child.set || child.inherit || child.currentcolor) {
1557         sp_style_merge_ipaint(style, &child, &parent);
1558         child.set = parent.set;
1559         child.inherit = parent.inherit;
1560     }
1563 static void
1564 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1565                                                unsigned const max_computed_val,
1566                                                unsigned const smaller_val)
1568     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1569        then zero or more other absolute values, then smaller_val then larger_val. */
1570     unsigned const min_computed_val = 0;
1571     unsigned const larger_val = smaller_val + 1;
1572     g_return_if_fail(min_computed_val < max_computed_val);
1573     g_return_if_fail(max_computed_val < smaller_val);
1575     if (parent.set && !parent.inherit) {
1576         if (!child.set || child.inherit) {
1577             child.value = parent.value;
1578             child.set = parent.set;  // i.e. true
1579             child.inherit = parent.inherit;  // i.e. false
1580         } else if (child.value < smaller_val) {
1581             /* Child has absolute value: leave as is. */
1582         } else if ( ( child.value == smaller_val
1583                       && parent.value == larger_val )
1584                     || ( parent.value == smaller_val
1585                          && child.value == larger_val ) )
1586         {
1587             child.set = false;
1588             /*
1589              * Note that this can result in a change in computed value in the
1590              * rare case that the parent's setting was a no-op (i.e. if the
1591              * parent's parent's computed value was already ultra-condensed or
1592              * ultra-expanded).  However, I'd guess that the change is for the
1593              * better: I'd guess that if the properties were specified
1594              * relatively, then the intent is to counteract parent's effect.
1595              * I.e. I believe this is the best code even in that case.
1596              */
1597         } else if (child.value == parent.value) {
1598             /* Leave as is. */
1599             /** \todo
1600              * It's unclear what to do if style and parent specify the same
1601              * relative directive (narrower or wider).  We can either convert
1602              * to absolute specification or coalesce to a single relative
1603              * request (of half the strength of the original pair).
1604              *
1605              * Converting to a single level of relative specification is a
1606              * better choice if the newly-unlinked clone is itself cloned to
1607              * other contexts (inheriting different font stretchiness): it
1608              * would preserve the effect that it will be narrower than
1609              * the inherited context in each case.  The disadvantage is that
1610              * it will ~certainly affect the computed value of the
1611              * newly-unlinked clone.
1612              */
1613         } else {
1614             unsigned const parent_val = parent.computed;
1615             child.value = ( child.value == smaller_val
1616                             ? ( parent_val == min_computed_val
1617                                 ? parent_val
1618                                 : parent_val - 1 )
1619                             : ( parent_val == max_computed_val
1620                                 ? parent_val
1621                                 : parent_val + 1 ) );
1622             g_assert(child.value <= max_computed_val);
1623             child.inherit = false;
1624             g_assert(child.set);
1625         }
1626     }
1629 template <typename LengthT>
1630 static void
1631 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1632                                              double const parent_child_em_ratio)
1634     if ( ( !(child.set) || child.inherit )
1635          && parent.set && !(parent.inherit) )
1636     {
1637         child = parent;
1638         switch (parent.unit) {
1639             case SP_CSS_UNIT_EM:
1640             case SP_CSS_UNIT_EX:
1641                 child.value *= parent_child_em_ratio;
1642                 /** \todo
1643                  * fixme: Have separate ex ratio parameter.
1644                  * Get x height from libnrtype or pango.
1645                  */
1646                 if (!isFinite(child.value)) {
1647                     child.value = child.computed;
1648                     child.unit = SP_CSS_UNIT_NONE;
1649                 }
1650                 break;
1652             default:
1653                 break;
1654         }
1655     }
1658 static double
1659 get_relative_font_size_frac(SPIFontSize const &font_size)
1661     switch (font_size.type) {
1662         case SP_FONT_SIZE_LITERAL: {
1663             switch (font_size.value) {
1664                 case SP_CSS_FONT_SIZE_SMALLER:
1665                     return 5.0 / 6.0;
1667                 case SP_CSS_FONT_SIZE_LARGER:
1668                     return 6.0 / 5.0;
1670                 default:
1671                     g_assert_not_reached();
1672             }
1673         }
1675         case SP_FONT_SIZE_PERCENTAGE:
1676             return SP_F8_16_TO_FLOAT(font_size.value);
1678         case SP_FONT_SIZE_LENGTH:
1679             g_assert_not_reached();
1680     }
1681     g_assert_not_reached();
1684 /**
1685  * Combine \a style and \a parent style specifications into a single style specification that
1686  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1687  *
1688  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1689  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1690  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1691  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1692  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1693  *
1694  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1695  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1696  * style was previously up-to-date wrt \a parent.
1697  *
1698  * \pre \a parent's computed values are already up-to-date.
1699  *   (\a style's computed values needn't be up-to-date.)
1700  */
1701 void
1702 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1704     /** \note
1705      * The general rule for each property is as follows:
1706      *
1707      *   If style is set to an absolute value, then leave it as is.
1708      *
1709      *   Otherwise (i.e. if style has a relative value):
1710      *
1711      *     If parent is set to an absolute value, then set style to the computed value.
1712      *
1713      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1714      */
1716     /* We do font-size first, to ensure that em size is up-to-date. */
1717     /** \todo
1718      * fixme: We'll need to have more font-related things up the top once
1719      * we're getting x-height from pango or libnrtype.
1720      */
1722     /* Some things that allow relative specifications. */
1723     {
1724         /* font-size.  Note that we update the computed font-size of style,
1725            to assist in em calculations later in this function. */
1726         if (parent->font_size.set && !parent->font_size.inherit) {
1727             if (!style->font_size.set || style->font_size.inherit) {
1728                 /* font_size inherits the computed value, so we can use the parent value
1729                  * verbatim. */
1730                 style->font_size = parent->font_size;
1731             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1732                 /* Child already has absolute size (stored in computed value), so do nothing. */
1733             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1734                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1735                 /* Child already has absolute size, but we ensure that the computed value
1736                    is up-to-date. */
1737                 unsigned const ix = style->font_size.value;
1738                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1739                 style->font_size.computed = font_size_table[ix];
1740             } else {
1741                 /* Child has relative size. */
1742                 double const child_frac(get_relative_font_size_frac(style->font_size));
1743                 style->font_size.set = true;
1744                 style->font_size.inherit = false;
1745                 style->font_size.computed = parent->font_size.computed * child_frac;
1747                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1748                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1749                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1750                 {
1751                     /* Absolute value. */
1752                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1753                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1754                 } else {
1755                     /* Relative value. */
1756                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1757                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1758                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1759                 }
1760             }
1761         }
1763         /* 'font-stretch' */
1764         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1765                                                        parent->font_stretch,
1766                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1767                                                        SP_CSS_FONT_STRETCH_NARROWER);
1769         /* font-weight */
1770         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1771                                                        parent->font_weight,
1772                                                        SP_CSS_FONT_WEIGHT_900,
1773                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1774     }
1777     /* Enum values that don't have any relative settings (other than `inherit'). */
1778     {
1779         SPIEnum SPStyle::*const fields[] = {
1780             //nyi: SPStyle::clip_rule,
1781             //nyi: SPStyle::color_interpolation,
1782             //nyi: SPStyle::color_interpolation_filters,
1783             //nyi: SPStyle::color_rendering,
1784             &SPStyle::direction,
1785             &SPStyle::fill_rule,
1786             &SPStyle::font_style,
1787             &SPStyle::font_variant,
1788             //nyi: SPStyle::image_rendering,
1789             //nyi: SPStyle::pointer_events,
1790             //nyi: SPStyle::shape_rendering,
1791             &SPStyle::stroke_linecap,
1792             &SPStyle::stroke_linejoin,
1793             &SPStyle::text_anchor,
1794             //nyi: &SPStyle::text_rendering,
1795             &SPStyle::visibility,
1796             &SPStyle::writing_mode
1797         };
1799         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1800             SPIEnum SPStyle::*const fld = fields[i];
1801             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1802         }
1803     }
1805     /* A few other simple inheritance properties. */
1806     {
1807         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1808         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1809         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1811         /** \todo
1812          * We currently treat text-decoration as if it were a simple inherited
1813          * property (fixme). This code may need changing once we do the
1814          * special fill/stroke inheritance mentioned by the spec.
1815          */
1816         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1817                                                                  parent->text_decoration);
1819         //nyi: font-size-adjust,  // <number> | none | inherit
1820         //nyi: glyph-orientation-horizontal,
1821         //nyi: glyph-orientation-vertical,
1822     }
1824     /* Properties that involve length but are easy in other respects. */
1825     {
1826         /* The difficulty with lengths is that font-relative units need adjusting if the font
1827          * varies between parent & child.
1828          *
1829          * Lengths specified in the existing child can stay as they are: its computed font
1830          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1831          * size.
1832          *
1833          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1834          * ratio of em or ex size between parent & child.
1835          */
1836         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1838         SPILength SPStyle::*const lfields[] = {
1839             &SPStyle::stroke_width,
1840             &SPStyle::text_indent
1841         };
1842         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1843             SPILength SPStyle::*fld = lfields[i];
1844             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1845                                                                     parent->*fld,
1846                                                                     parent_child_em_ratio);
1847         }
1849         SPILengthOrNormal SPStyle::*const nfields[] = {
1850             &SPStyle::letter_spacing,
1851             &SPStyle::line_height,
1852             &SPStyle::word_spacing
1853         };
1854         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1855             SPILengthOrNormal SPStyle::*fld = nfields[i];
1856             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1857                                                                             parent->*fld,
1858                                                                             parent_child_em_ratio);
1859         }
1861         //nyi: &SPStyle::kerning: length or `auto'
1863         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1864            can accept units. */
1865     }
1867     /* Properties that involve a URI but are easy in other respects. */
1868     {
1869         /** \todo
1870          * Could cause problems if original object was in another document
1871          * and it used a relative URL.  (At the time of writing, we don't
1872          * allow hrefs to other documents, so this isn't a problem yet.)
1873          * Paint properties also allow URIs.
1874          */
1875         //nyi: cursor,   // may involve change in effect, but we can't do much better
1876         //nyi: color-profile,
1878         // Markers (marker-start, marker-mid, marker-end).
1879         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1880             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1881         }
1882     }
1884     /* CSS2 */
1885     /* Font */
1887     if (style->text && parent->text) {
1888         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1889                                                      parent->text->font_family);
1890     }
1892     /* Properties that don't inherit by default.  Most of these need special handling. */
1893     {
1894         /*
1895          * opacity's effect is cumulative; we set the new value to the combined effect.  The
1896          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
1897          * fill-opacity are quite different from opacity, and don't need any special handling.)
1898          *
1899          * Cases:
1900          * - parent & child were each previously unset, in which case the effective
1901          *   opacity value is 1.0, and style should remain unset.
1902          * - parent was previously unset (so computed opacity value of 1.0)
1903          *   and child was set to inherit.  The merged child should
1904          *   get a value of 1.0, and shouldn't inherit (lest the new parent
1905          *   has a different opacity value).  Given that opacity's default
1906          *   value is 1.0 (rather than inherit), we might as well have the
1907          *   merged child's opacity be unset.
1908          * - parent was previously unset (so opacity 1.0), and child was set to a number.
1909          *   The merged child should retain its existing settings (though it doesn't matter
1910          *   if we make it unset if that number was 1.0).
1911          * - parent was inherit and child was unset.  Merged child should be set to inherit.
1912          * - parent was inherit and child was inherit.  (We can't in general reproduce this
1913          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
1914          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1915          *   child's value should be set to the product of the two, i.e. the square of the
1916          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
1917          *   is more important to retain the effective opacity than to retain the inheriting
1918          *   effect, and assumes that the inheriting effect either isn't important enough to create
1919          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
1920          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1921          *   closer to maintaining the effect.
1922          * - parent was inherit and child was set to a numerical value.  If the child's value
1923          *   was 1.0, then the merged child should have the same settings as the parent.
1924          *   If the child's value was 0, then the merged child should also be set to 0.
1925          *   If the child's value was anything else, then we do the same as for the inherit/inherit
1926          *   case above: have the merged child set to the product of the two opacities and not
1927          *   marked as inherit, for the same reasons as for that case.
1928          * - parent was set to a value, and child was unset.  The merged child should have
1929          *   parent's settings.
1930          * - parent was set to a value, and child was inherit.  The merged child should
1931          *   be set to the product, i.e. the square of the parent's value.
1932          * - parent & child are each set to a value.  The merged child should be set to the
1933          *   product.
1934          */
1935         if ( !style->opacity.set
1936              || ( !style->opacity.inherit
1937                   && style->opacity.value == SP_SCALE24_MAX ) )
1938         {
1939             style->opacity = parent->opacity;
1940         } else {
1941             /* Ensure that style's computed value is up-to-date. */
1942             if (style->opacity.inherit) {
1943                 style->opacity.value = parent->opacity.value;
1944             }
1946             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1947             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1948                                                   parent->opacity.value);
1950             style->opacity.inherit = (parent->opacity.inherit
1951                                       && style->opacity.inherit
1952                                       && (parent->opacity.value == 0 ||
1953                                           parent->opacity.value == SP_SCALE24_MAX));
1954             style->opacity.set = ( style->opacity.inherit
1955                                    || style->opacity.value < SP_SCALE24_MAX );
1956         }
1958         /* display is in principle similar to opacity, but implementation is easier. */
1959         if ( parent->display.set && !parent->display.inherit
1960              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1961             style->display.value = SP_CSS_DISPLAY_NONE;
1962             style->display.set = true;
1963             style->display.inherit = false;
1964         } else if (style->display.inherit) {
1965             style->display.value = parent->display.value;
1966             style->display.set = parent->display.set;
1967             style->display.inherit = parent->display.inherit;
1968         } else {
1969             /* Leave as is.  (display doesn't inherit by default.) */
1970         }
1972         /* enable-background - this is rather complicated, because
1973          * it is valid only when applied to container elements.
1974          * Let's check a simple case anyhow. */
1975         if (parent->enable_background.set
1976             && !parent->enable_background.inherit
1977             && style->enable_background.inherit)
1978         {
1979             style->enable_background.set = true;
1980             style->enable_background.inherit = false;
1981             style->enable_background.value = parent->enable_background.value;
1982         }
1984         if (!style->filter.set || style->filter.inherit)
1985         {
1986             // FIXME:
1987             // instead of just copying over, we need to _really merge_ the two filters by combining their
1988             // filter primitives
1989             sp_style_merge_ifilter(style, &parent->filter);
1990         }
1992         /** \todo
1993          * fixme: Check that we correctly handle all properties that don't
1994          * inherit by default (as shown in
1995          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1996          */
1997     }
1999     /* SPIPaint properties (including color). */
2000     {
2001         /** \todo
2002          * Think about the issues involved if specified as currentColor or
2003          * if specified relative to colorProfile, and if the currentColor or
2004          * colorProfile differs between parent \& child.  See also comments
2005          * elsewhere in this function about URIs.
2006          */
2007         SPIPaint SPStyle::*const fields[] = {
2008             &SPStyle::color,
2009             &SPStyle::fill,
2010             &SPStyle::stroke
2011         };
2012         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2013             SPIPaint SPStyle::*const fld = fields[i];
2014             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2015         }
2016     }
2018     /* Things from SVG 1.2 or CSS3. */
2019     {
2020         /* Note: If we ever support setting string values for text-align then we'd need strdup
2021          * handling here. */
2022         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2024         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2025         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2026     }
2028     /* Note: this will need length handling once dasharray supports units. */
2029     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2030          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2031     {
2032         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2033         if (style->stroke_dash.n_dash > 0) {
2034             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2035             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2036         }
2037         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2038         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2039     }
2041     /* Note: this will need length handling once dasharray_offset supports units. */
2042     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
2043         style->stroke_dash.offset = parent->stroke_dash.offset;
2044         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2045         /* fixme: we don't currently allow stroke-dashoffset to be `inherit'.  TODO: Try to
2046          * represent it as a normal SPILength; though will need to do something about existing
2047          * users of stroke_dash.offset and stroke_dashoffset_set. */
2048     }
2052 static void
2053 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2055     // it may be that this style's SPIPaint has not yet created its URIReference;
2056     // now that we have a document, we can create it here
2057     if (!paint->value.href && document) {
2058         paint->value.href = new SPPaintServerReference(document);
2059         paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
2060     }
2062     if (paint->value.href && paint->value.href->getObject())
2063         paint->value.href->detach();
2065     if (paint->value.href) {
2066         try {
2067             paint->value.href->attach(*uri);
2068         } catch (Inkscape::BadURIException &e) {
2069             g_warning("%s", e.what());
2070             paint->value.href->detach();
2071         }
2072     }
2075 static void
2076 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2078     const Inkscape::URI IURI(uri);
2079     sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2082 void
2083 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2085     sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2088 /**
2089  *
2090  */
2091 static void
2092 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2094     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2095         paint->clear();
2096         paint->currentcolor = TRUE;
2097         paint->setColor(style->color.value.color);
2098         return;
2099     }
2101     paint->clear();
2102     if ( parent->isPaintserver() ) {
2103         if (parent->value.href) {
2104             sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2105         } else {
2106             g_warning("Expected paint server not found.");
2107         }
2108     } else if ( parent->isColor() ) {
2109         paint->setColor( parent->value.color );
2110     } else if ( parent->isNone() ) {
2111         //
2112     } else {
2113         g_assert_not_reached();
2114     }
2118 /**
2119  * Merge filter style from parent.
2120  * Filter effects do not inherit by default
2121  */
2122 static void
2123 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2125     sp_style_filter_clear(style);
2126     style->filter.set = parent->set;
2127     style->filter.inherit = parent->inherit;
2129     if (style->filter.href && style->filter.href->getObject())
2130        style->filter.href->detach();
2132     if (style->filter.href && parent->href) {
2133         try {
2134             style->filter.href->attach(*parent->href->getURI());
2135         } catch (Inkscape::BadURIException &e) {
2136             g_warning("%s", e.what());
2137             style->filter.href->detach();
2138         }
2139     }
2142 /**
2143  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2144  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2145  * complete cascaded style to style_clipboard. When you need a CSS string
2146  * for an object in the document tree, you normally call
2147  * sp_style_write_difference instead to take into account the object's parent.
2148  *
2149  * \pre style != NULL.
2150  * \pre flags in {IFSET, ALWAYS}.
2151  * \post ret != NULL.
2152  */
2153 gchar *
2154 sp_style_write_string(SPStyle const *const style, guint const flags)
2156     /** \todo
2157      * Merge with write_difference, much duplicate code!
2158      */
2159     g_return_val_if_fail(style != NULL, NULL);
2160     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2161                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2162                          NULL);
2164     gchar c[BMAX];
2165     gchar *p = c;
2166     *p = '\0';
2168     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2169     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2170     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2171     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2172     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2174     /* Text */
2175     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2176     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2177     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2178     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2179     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2180     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2181     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2182     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2183     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2184     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2186     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2188     /// \todo fixme: Per type methods need default flag too (lauris)
2189     p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2190     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2191     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2192     p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2193     p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2194     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2195     p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2196     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2197     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2199     if (style->marker[SP_MARKER_LOC].set) {
2200         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2201     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2202         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2203     }
2204     if (style->marker[SP_MARKER_LOC_START].set) {
2205         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2206     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2207         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2208     }
2209     if (style->marker[SP_MARKER_LOC_MID].set) {
2210         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2211     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2212         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2213     }
2214     if (style->marker[SP_MARKER_LOC_END].set) {
2215         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2216     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2217         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2218     }
2220     p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2222     /** \todo fixme: */
2223     if ((flags == SP_STYLE_FLAG_ALWAYS)
2224         || style->stroke_dasharray_set)
2225     {
2226         if (style->stroke_dasharray_inherit) {
2227             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2228         } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2229             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2230             gint i;
2231             for (i = 0; i < style->stroke_dash.n_dash; i++) {
2232                 Inkscape::CSSOStringStream os;
2233                 if (i) {
2234                     os << ", ";
2235                 }
2236                 os << style->stroke_dash.dash[i];
2237                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2238             }
2239             if (p < c + BMAX) {
2240                 *p++ = ';';
2241             }
2242         } else {
2243             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2244         }
2245     }
2247     /** \todo fixme: */
2248     if (style->stroke_dashoffset_set) {
2249         Inkscape::CSSOStringStream os;
2250         os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2251         p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2252     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2253         p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2254     }
2256     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2258     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2259     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2260     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2262     /* filter: */
2263     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2265     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2267     /* fixme: */
2268     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2270     /* Get rid of trailing `;'. */
2271     if (p != c) {
2272         --p;
2273         if (*p == ';') {
2274             *p = '\0';
2275         }
2276     }
2278     return g_strdup(c);
2282 #define STYLE_BUF_MAX
2285 /**
2286  * Dumps style to CSS string, see sp_style_write_string()
2287  *
2288  * \pre from != NULL.
2289  * \pre to != NULL.
2290  * \post ret != NULL.
2291  */
2292 gchar *
2293 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2295     g_return_val_if_fail(from != NULL, NULL);
2296     g_return_val_if_fail(to != NULL, NULL);
2298     gchar c[BMAX], *p = c;
2299     *p = '\0';
2301     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2302     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2303     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2304     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2305     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2307     /* Text */
2308     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2309     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2310     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2311     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2312     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2313     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2314     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2315     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2316     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2317     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2319     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2321     /// \todo fixme: Per type methods need default flag too
2322     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2323         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2324     }
2325     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2326     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2327     p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2328     p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2329     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2330     p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2331     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2332                               &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2333     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2334                               &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2335     p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2336                                &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2337     /** \todo fixme: */
2338     if (from->stroke_dasharray_set) {
2339         if (from->stroke_dasharray_inherit) {
2340             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2341         } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2342             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2343             for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2344                 Inkscape::CSSOStringStream os;
2345                 if (i) {
2346                     os << ", ";
2347                 }
2348                 os << from->stroke_dash.dash[i];
2349                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2350             }
2351             p += g_snprintf(p, c + BMAX - p, ";");
2352         }
2353     }
2354     /* fixme: */
2355     if (from->stroke_dashoffset_set) {
2356         Inkscape::CSSOStringStream os;
2357         os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2358         p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2359     }
2360     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2362     /* markers */
2363     if (from->marker[SP_MARKER_LOC].value != NULL) {
2364         p += g_snprintf(p, c + BMAX - p, "marker:%s;",       from->marker[SP_MARKER_LOC].value);
2365     }
2366     if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2367         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2368     }
2369     if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2370         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2371     }
2372     if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2373         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2374     }
2376     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2377     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2378     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2380     /* filter: */
2381     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2383     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2385     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2387     /** \todo
2388      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2389      * flag is mainly only for attributes that don't handle explicit unset well.
2390      * We may need to revisit the behaviour of this routine.
2391      */
2393     /* Get rid of trailing `;'. */
2394     if (p != c) {
2395         --p;
2396         if (*p == ';') {
2397             *p = '\0';
2398         }
2399     }
2401     return g_strdup(c);
2406 /**
2407  * Reset all style properties.
2408  */
2409 static void
2410 sp_style_clear(SPStyle *style)
2412     g_return_if_fail(style != NULL);
2414     style->fill.clear();
2415     style->stroke.clear();
2416     sp_style_filter_clear(style);
2418     if (style->fill.value.href)
2419         delete style->fill.value.href;
2420     if (style->stroke.value.href)
2421         delete style->stroke.value.href;
2422     if (style->filter.href)
2423         delete style->filter.href;
2425     if (style->stroke_dash.dash) {
2426         g_free(style->stroke_dash.dash);
2427     }
2429     /** \todo fixme: Do that text manipulation via parents */
2430     SPObject *object = style->object;
2431     SPDocument *document = style->document;
2432     gint const refcount = style->refcount;
2433     SPTextStyle *text = style->text;
2434     unsigned const text_private = style->text_private;
2436     memset(style, 0, sizeof(SPStyle));
2438     style->refcount = refcount;
2439     style->object = object;
2440     style->document = document;
2442     if (document) {
2443         style->filter.href = new SPFilterReference(document);
2444         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2446         style->fill.value.href = new SPPaintServerReference(document);
2447         style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2449         style->stroke.value.href = new SPPaintServerReference(document);
2450         style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2451     }
2453     style->text = text;
2454     style->text_private = text_private;
2456     /* fixme: */
2457     style->text->font.set = FALSE;
2458     style->text->font_family.set = FALSE;
2460     style->font_size.set = FALSE;
2461     style->font_size.type = SP_FONT_SIZE_LITERAL;
2462     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2463     style->font_size.computed = 12.0;
2464     style->font_style.set = FALSE;
2465     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2466     style->font_variant.set = FALSE;
2467     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2468     style->font_weight.set = FALSE;
2469     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2470     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2471     style->font_stretch.set = FALSE;
2472     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2474     /* text */
2475     style->text_indent.set = FALSE;
2476     style->text_indent.unit = SP_CSS_UNIT_NONE;
2477     style->text_indent.computed = 0.0;
2479     style->text_align.set = FALSE;
2480     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2482     style->text_decoration.set = FALSE;
2483     style->text_decoration.underline = FALSE;
2484     style->text_decoration.overline = FALSE;
2485     style->text_decoration.line_through = FALSE;
2486     style->text_decoration.blink = FALSE;
2488     style->line_height.set = FALSE;
2489     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2490     style->line_height.normal = TRUE;
2491     style->line_height.value = style->line_height.computed = 1.0;
2493     style->letter_spacing.set = FALSE;
2494     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2495     style->letter_spacing.normal = TRUE;
2496     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2498     style->word_spacing.set = FALSE;
2499     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2500     style->word_spacing.normal = TRUE;
2501     style->word_spacing.value = style->word_spacing.computed = 0.0;
2503     style->text_transform.set = FALSE;
2504     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2506     style->direction.set = FALSE;
2507     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2509     style->block_progression.set = FALSE;
2510     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2512     style->writing_mode.set = FALSE;
2513     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2516     style->text_anchor.set = FALSE;
2517     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2520     style->opacity.value = SP_SCALE24_MAX;
2521     style->visibility.set = FALSE;
2522     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2523     style->display.set = FALSE;
2524     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2525     style->overflow.set = FALSE;
2526     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2528     style->color.clear();
2529     style->color.setColor(0.0, 0.0, 0.0);
2531     style->fill.clear();
2532     style->fill.setColor(0.0, 0.0, 0.0);
2533     style->fill_opacity.value = SP_SCALE24_MAX;
2534     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2536     style->stroke.clear();
2537     style->stroke_opacity.value = SP_SCALE24_MAX;
2539     style->stroke_width.set = FALSE;
2540     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2541     style->stroke_width.computed = 1.0;
2543     style->stroke_linecap.set = FALSE;
2544     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2545     style->stroke_linejoin.set = FALSE;
2546     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2548     style->stroke_miterlimit.set = FALSE;
2549     style->stroke_miterlimit.value = 4.0;
2551     style->stroke_dash.n_dash = 0;
2552     style->stroke_dash.dash = NULL;
2553     style->stroke_dash.offset = 0.0;
2555     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2556         g_free(style->marker[i].value);
2557         style->marker[i].set = FALSE;
2558     }
2560     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2561     style->enable_background.set = false;
2562     style->enable_background.inherit = false;
2567 /**
2568  *
2569  */
2570 static void
2571 sp_style_read_dash(SPStyle *style, gchar const *str)
2573     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2574     style->stroke_dasharray_set = TRUE;
2576     if (strcmp(str, "inherit") == 0) {
2577         style->stroke_dasharray_inherit = true;
2578         return;
2579     }
2580     style->stroke_dasharray_inherit = false;
2582     NRVpathDash &dash = style->stroke_dash;
2583     g_free(dash.dash);
2584     dash.dash = NULL;
2586     if (strcmp(str, "none") == 0) {
2587         dash.n_dash = 0;
2588         return;
2589     }
2591     gint n_dash = 0;
2592     gdouble d[64];
2593     gchar *e = NULL;
2595     bool LineSolid=true;
2596     while (e != str && n_dash < 64) {
2597         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2598         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2599         if (d[n_dash] > 0.00000001)
2600             LineSolid = false;
2601         if (e != str) {
2602             n_dash += 1;
2603             str = e;
2604         }
2605         while (str && *str && !isalnum(*str)) str += 1;
2606     }
2608     if (LineSolid) {
2609         dash.n_dash = 0;
2610         return;
2611     }
2613     if (n_dash > 0) {
2614         dash.dash = g_new(gdouble, n_dash);
2615         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2616         dash.n_dash = n_dash;
2617     }
2621 /*#########################
2622 ## SPTextStyle operations
2623 #########################*/
2626 /**
2627  * Return new SPTextStyle object with default settings.
2628  */
2629 static SPTextStyle *
2630 sp_text_style_new()
2632     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2633     ts->refcount = 1;
2634     sp_text_style_clear(ts);
2636     ts->font.value = g_strdup("Bitstream Vera Sans");
2637     ts->font_family.value = g_strdup("Bitstream Vera Sans");
2639     return ts;
2643 /**
2644  * Clear text style settings.
2645  */
2646 static void
2647 sp_text_style_clear(SPTextStyle *ts)
2649     ts->font.set = FALSE;
2650     ts->font_family.set = FALSE;
2655 /**
2656  * Reduce refcount of text style and possibly free it.
2657  */
2658 static SPTextStyle *
2659 sp_text_style_unref(SPTextStyle *st)
2661     st->refcount -= 1;
2663     if (st->refcount < 1) {
2664         g_free(st->font.value);
2665         g_free(st->font_family.value);
2666         g_free(st);
2667     }
2669     return NULL;
2674 /**
2675  * Return duplicate of text style.
2676  */
2677 static SPTextStyle *
2678 sp_text_style_duplicate_unset(SPTextStyle *st)
2680     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2681     nt->refcount = 1;
2683     nt->font.value = g_strdup(st->font.value);
2684     nt->font_family.value = g_strdup(st->font_family.value);
2686     return nt;
2691 /**
2692  * Write SPTextStyle object into string.
2693  */
2694 static guint
2695 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2697     gint d = 0;
2699     // We do not do diffing for text style
2700     if (flags == SP_STYLE_FLAG_IFDIFF)
2701         flags = SP_STYLE_FLAG_IFSET;
2703     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2704     return d;
2709 /* The following sp_tyle_read_* functions ignore invalid values, as per
2710  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2711  *
2712  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2713  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2714  * pairs, in which case SVG's error-handling rules
2715  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2716  */
2719 /**
2720  * Set SPIFloat object from string.
2721  */
2722 static void
2723 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2725     if (!strcmp(str, "inherit")) {
2726         val->set = TRUE;
2727         val->inherit = TRUE;
2728     } else {
2729         gfloat value;
2730         if (sp_svg_number_read_f(str, &value)) {
2731             val->set = TRUE;
2732             val->inherit = FALSE;
2733             val->value = value;
2734         }
2735     }
2740 /**
2741  * Set SPIScale24 object from string.
2742  */
2743 static void
2744 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2746     if (!strcmp(str, "inherit")) {
2747         val->set = TRUE;
2748         val->inherit = TRUE;
2749     } else {
2750         gfloat value;
2751         if (sp_svg_number_read_f(str, &value)) {
2752             val->set = TRUE;
2753             val->inherit = FALSE;
2754             value = CLAMP(value, 0.0, 1.0);
2755             val->value = SP_SCALE24_FROM_FLOAT(value);
2756         }
2757     }
2760 /**
2761  * Reads a style value and performs lookup based on the given style value enumerations.
2762  */
2763 static void
2764 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2765                     bool const can_explicitly_inherit)
2767     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2768         val->set = TRUE;
2769         val->inherit = TRUE;
2770     } else {
2771         for (unsigned i = 0; dict[i].key; i++) {
2772             if (!strcmp(str, dict[i].key)) {
2773                 val->set = TRUE;
2774                 val->inherit = FALSE;
2775                 val->value = dict[i].value;
2776                 /* Save copying for values not needing it */
2777                 val->computed = val->value;
2778                 break;
2779             }
2780         }
2781     }
2786 /**
2787  * Set SPIString object from string.
2788  */
2789 static void
2790 sp_style_read_istring(SPIString *val, gchar const *str)
2792     g_free(val->value);
2794     if (!strcmp(str, "inherit")) {
2795         val->set = TRUE;
2796         val->inherit = TRUE;
2797         val->value = NULL;
2798     } else {
2799         val->set = TRUE;
2800         val->inherit = FALSE;
2801         val->value = g_strdup(str);
2802     }
2807 /**
2808  * Set SPILength object from string.
2809  */
2810 static void
2811 sp_style_read_ilength(SPILength *val, gchar const *str)
2813     if (!strcmp(str, "inherit")) {
2814         val->set = TRUE;
2815         val->inherit = TRUE;
2816     } else {
2817         gdouble value;
2818         gchar *e;
2819         /** \todo fixme: Move this to standard place (Lauris) */
2820         value = g_ascii_strtod(str, &e);
2821         if ((gchar const *) e != str) {
2822             /** \todo
2823              * Allow the number of px per inch to vary (document preferences,
2824              * X server or whatever).  E.g. don't fill in computed here, do
2825              * it at the same time as percentage units are done.
2826              */
2827             if (!*e) {
2828                 /* Userspace */
2829                 val->unit = SP_CSS_UNIT_NONE;
2830                 val->computed = value;
2831             } else if (!strcmp(e, "px")) {
2832                 /* Userspace */
2833                 val->unit = SP_CSS_UNIT_PX;
2834                 val->computed = value;
2835             } else if (!strcmp(e, "pt")) {
2836                 /* Userspace / DEVICESCALE */
2837                 val->unit = SP_CSS_UNIT_PT;
2838                 val->computed = value * PX_PER_PT;
2839             } else if (!strcmp(e, "pc")) {
2840                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2841                 val->unit = SP_CSS_UNIT_PC;
2842                 val->computed = value * PX_PER_PT * 12;
2843             } else if (!strcmp(e, "mm")) {
2844                 val->unit = SP_CSS_UNIT_MM;
2845                 val->computed = value * PX_PER_MM;
2846             } else if (!strcmp(e, "cm")) {
2847                 val->unit = SP_CSS_UNIT_CM;
2848                 val->computed = value * PX_PER_CM;
2849             } else if (!strcmp(e, "in")) {
2850                 val->unit = SP_CSS_UNIT_IN;
2851                 val->computed = value * PX_PER_IN;
2852             } else if (!strcmp(e, "em")) {
2853                 /* EM square */
2854                 val->unit = SP_CSS_UNIT_EM;
2855                 val->value = value;
2856             } else if (!strcmp(e, "ex")) {
2857                 /* ex square */
2858                 val->unit = SP_CSS_UNIT_EX;
2859                 val->value = value;
2860             } else if (!strcmp(e, "%")) {
2861                 /* Percentage */
2862                 val->unit = SP_CSS_UNIT_PERCENT;
2863                 val->value = value * 0.01;
2864             } else {
2865                 /* Invalid */
2866                 return;
2867             }
2868             val->set = TRUE;
2869             val->inherit = FALSE;
2870         }
2871     }
2874 /**
2875  * Set SPILengthOrNormal object from string.
2876  */
2877 static void
2878 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2880     if (!strcmp(str, "normal")) {
2881         val->set = TRUE;
2882         val->inherit = FALSE;
2883         val->normal = TRUE;
2884         val->unit = SP_CSS_UNIT_NONE;
2885         val->value = val->computed = 0.0;
2886     } else {
2887         SPILength length;
2888         sp_style_read_ilength(&length, str);
2889         val->set = length.set;
2890         val->inherit = length.inherit;
2891         val->normal = FALSE;
2892         val->unit = length.unit;
2893         val->value = length.value;
2894         val->computed = length.computed;
2895     }
2898 /**
2899  * Set SPITextDecoration object from string.
2900  */
2901 static void
2902 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2904     if (!strcmp(str, "inherit")) {
2905         val->set = TRUE;
2906         val->inherit = TRUE;
2907     } else if (!strcmp(str, "none")) {
2908         val->set = TRUE;
2909         val->inherit = FALSE;
2910         val->underline = FALSE;
2911         val->overline = FALSE;
2912         val->line_through = FALSE;
2913         val->blink = FALSE;
2914     } else {
2915         bool found_underline = false;
2916         bool found_overline = false;
2917         bool found_line_through = false;
2918         bool found_blink = false;
2919         for ( ; *str ; str++ ) {
2920             if (*str == ' ') continue;
2921             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2922                 found_underline = true;
2923                 str += 9;
2924             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2925                 found_overline = true;
2926                 str += 8;
2927             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2928                 found_line_through = true;
2929                 str += 12;
2930             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2931                 found_blink = true;
2932                 str += 5;
2933             } else {
2934                 return;  // invalid value
2935             }
2936         }
2937         if (!(found_underline || found_overline || found_line_through || found_blink)) {
2938             return;  // invalid value: empty
2939         }
2940         val->set = TRUE;
2941         val->inherit = FALSE;
2942         val->underline = found_underline;
2943         val->overline = found_overline;
2944         val->line_through = found_line_through;
2945         val->blink = found_blink;
2946     }
2949 /**
2950  * Set SPIPaint object from string containing an integer value.
2951  * \param document Ignored
2952  */
2953 static void
2954 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2956     (void)style; // TODO
2957     (void)document; // TODO
2958     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
2959     if (!strcmp(str, "inherit")) {
2960         paint->set = TRUE;
2961         paint->inherit = TRUE;
2962     } else {
2963         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2964         if (rgb0 != 0xff) {
2965             paint->setColor(rgb0);
2966             paint->inherit = FALSE;
2967         }
2968     }
2972 /**
2973  * Set SPIPaint object from string.
2974  *
2975  * \pre paint == \&style.fill || paint == \&style.stroke.
2976  */
2977 static void
2978 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2980     while (g_ascii_isspace(*str)) {
2981         ++str;
2982     }
2984     paint->clear();
2986     if (streq(str, "inherit")) {
2987         paint->set = TRUE;
2988         paint->inherit = TRUE;
2989     } else {
2990         if ( strneq(str, "url", 3) ) {
2991             gchar *uri = extract_uri( str, &str );
2992             while ( g_ascii_isspace(*str) ) {
2993                 ++str;
2994             }
2995             // TODO check on and comment the comparrison "paint != &style->color".
2996             if ( uri && *uri && (paint != &style->color) ) {
2997                 paint->set = TRUE;
2999                 // it may be that this style's SPIPaint has not yet created its URIReference;
3000                 // now that we have a document, we can create it here
3001                 if (!paint->value.href && document) {
3002                     paint->value.href = new SPPaintServerReference(document);
3003                     paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
3004                 }
3006                 // TODO check what this does in light of move away from union
3007                 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3008             }
3009             g_free( uri );
3010         }
3012         if (streq(str, "currentColor") && paint != &style->color) {
3013             paint->set = TRUE;
3014             paint->currentcolor = TRUE;
3015         } else if (streq(str, "none") && paint != &style->color) {
3016             paint->set = TRUE;
3017             paint->noneSet = TRUE;
3018         } else {
3019             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3020             if (rgb0 != 0xff) {
3021                 paint->setColor( rgb0 );
3022                 paint->set = TRUE;
3024                 while (g_ascii_isspace(*str)) {
3025                     ++str;
3026                 }
3027                 if (strneq(str, "icc-color(", 10)) {
3028                     SVGICCColor* tmp = new SVGICCColor();
3029                     if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3030                         delete tmp;
3031                         tmp = 0;
3032                     }
3033                     paint->value.iccColor = tmp;
3034                 }
3035             }
3036         }
3037     }
3042 /**
3043  * Set SPIFontSize object from string.
3044  */
3045 static void
3046 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3048     if (!strcmp(str, "inherit")) {
3049         val->set = TRUE;
3050         val->inherit = TRUE;
3051     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3052         for (unsigned i = 0; enum_font_size[i].key; i++) {
3053             if (!strcmp(str, enum_font_size[i].key)) {
3054                 val->set = TRUE;
3055                 val->inherit = FALSE;
3056                 val->type = SP_FONT_SIZE_LITERAL;
3057                 val->value = enum_font_size[i].value;
3058                 return;
3059             }
3060         }
3061         /* Invalid */
3062         return;
3063     } else {
3064         gdouble value;
3065         gchar *e;
3066         /* fixme: Move this to standard place (Lauris) */
3067         value = g_ascii_strtod(str, &e);
3068         if ((gchar const *) e != str) {
3069             if (!*e) {
3070                 /* Userspace */
3071             } else if (!strcmp(e, "px")) {
3072                 /* Userspace */
3073             } else if (!strcmp(e, "pt")) {
3074                 /* Userspace * DEVICESCALE */
3075                 value *= PX_PER_PT;
3076             } else if (!strcmp(e, "pc")) {
3077                 /* 12pt */
3078                 value *= PX_PER_PT * 12.0;
3079             } else if (!strcmp(e, "mm")) {
3080                 value *= PX_PER_MM;
3081             } else if (!strcmp(e, "cm")) {
3082                 value *= PX_PER_CM;
3083             } else if (!strcmp(e, "in")) {
3084                 value *= PX_PER_IN;
3085             } else if (!strcmp(e, "%")) {
3086                 /* Percentage */
3087                 val->set = TRUE;
3088                 val->inherit = FALSE;
3089                 val->type = SP_FONT_SIZE_PERCENTAGE;
3090                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3091                 return;
3092             } else {
3093                 /* Invalid */
3094                 return;
3095             }
3096             /* Length */
3097             val->set = TRUE;
3098             val->inherit = FALSE;
3099             val->type = SP_FONT_SIZE_LENGTH;
3100             val->computed = value;
3101             return;
3102         }
3103     }
3108 /**
3109  * Set SPIFilter object from string.
3110  */
3111 static void
3112 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3114     SPIFilter *f = &(style->filter);
3115     /* Try all possible values: inherit, none, uri */
3116     if (streq(str, "inherit")) {
3117         f->set = TRUE;
3118         f->inherit = TRUE;
3119         if (f->href && f->href->getObject())
3120             f->href->detach(); 
3121     } else if(streq(str, "none")) {
3122         f->set = TRUE;
3123         f->inherit = FALSE;
3124         if (f->href && f->href->getObject())
3125            f->href->detach(); 
3126     } else if (strneq(str, "url", 3)) {
3127         char *uri = extract_uri(str);
3128         if(uri == NULL || uri[0] == '\0') {
3129             g_warning("Specified filter url is empty");
3130             f->set = TRUE;
3131             f->inherit = FALSE;
3132             return;
3133         }
3134         f->set = TRUE;
3135         f->inherit = FALSE;
3136         if (f->href && f->href->getObject())
3137             f->href->detach();
3139         // it may be that this style has not yet created its SPFilterReference;
3140         // now that we have a document, we can create it here
3141         if (!f->href && document) {
3142             f->href = new SPFilterReference(document);
3143             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3144         }
3146         try {
3147             f->href->attach(Inkscape::URI(uri));
3148         } catch (Inkscape::BadURIException &e) {
3149             g_warning("%s", e.what());
3150             f->href->detach();
3151         }
3152         g_free (uri);
3154     } else {
3155         /* We shouldn't reach this if SVG input is well-formed */
3156         f->set = FALSE;
3157         f->inherit = FALSE;
3158         if (f->href && f->href->getObject())
3159             f->href->detach(); 
3160     }
3163 /**
3164  * Set SPIEnum object from repr attribute.
3165  */
3166 static void
3167 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3168                     gchar const *key, SPStyleEnum const *dict,
3169                     bool const can_explicitly_inherit)
3171     gchar const *str = repr->attribute(key);
3172     if (str) {
3173         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3174     }
3179 /**
3180  * Set SPILength object from repr attribute.
3181  */
3182 static void
3183 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3185     gchar const *str = repr->attribute(key);
3186     if (str) {
3187         sp_style_read_ilength(val, str);
3188     }
3191 /**
3192  * Set SPIFontSize object from repr attribute.
3193  */
3194 static void
3195 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3197     gchar const *str = repr->attribute(key);
3198     if (str) {
3199         sp_style_read_ifontsize(val, str);
3200     }
3204 /**
3205  * Set SPIFloat object from repr attribute.
3206  */
3207 static void
3208 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3210     gchar const *str = repr->attribute(key);
3211     if (str) {
3212         sp_style_read_ifloat(val, str);
3213     }
3217 /**
3218  * Write SPIFloat object into string.
3219  */
3220 static gint
3221 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3222                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3224     Inkscape::CSSOStringStream os;
3226     if ((flags & SP_STYLE_FLAG_ALWAYS)
3227         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3228         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3229             && (!base->set || (val->value != base->value))))
3230     {
3231         if (val->inherit) {
3232             return g_snprintf(p, len, "%s:inherit;", key);
3233         } else {
3234             os << key << ":" << val->value << ";";
3235             return g_strlcpy(p, os.str().c_str(), len);
3236         }
3237     }
3238     return 0;
3242 /**
3243  * Write SPIScale24 object into string.
3244  */
3245 static gint
3246 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3247                         SPIScale24 const *const val, SPIScale24 const *const base,
3248                         guint const flags)
3250     Inkscape::CSSOStringStream os;
3252     if ((flags & SP_STYLE_FLAG_ALWAYS)
3253         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3254         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3255             && (!base->set || (val->value != base->value))))
3256     {
3257         if (val->inherit) {
3258             return g_snprintf(p, len, "%s:inherit;", key);
3259         } else {
3260             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3261             return g_strlcpy(p, os.str().c_str(), len);
3262         }
3263     }
3264     return 0;
3268 /**
3269  * Write SPIEnum object into string.
3270  */
3271 static gint
3272 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3273                      SPStyleEnum const *const dict,
3274                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3276     if ((flags & SP_STYLE_FLAG_ALWAYS)
3277         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3278         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3279             && (!base->set || (val->computed != base->computed))))
3280     {
3281         if (val->inherit) {
3282             return g_snprintf(p, len, "%s:inherit;", key);
3283         }
3284         for (unsigned i = 0; dict[i].key; i++) {
3285             if (dict[i].value == static_cast< gint > (val->value) ) {
3286                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3287             }
3288         }
3289     }
3290     return 0;
3295 /**
3296  * Write SPIString object into string.
3297  */
3298 static gint
3299 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3300                        SPIString const *const val, SPIString const *const base, guint const flags)
3302     if ((flags & SP_STYLE_FLAG_ALWAYS)
3303         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3304         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3305             && (!base->set || strcmp(val->value, base->value))))
3306     {
3307         if (val->inherit) {
3308             return g_snprintf(p, len, "%s:inherit;", key);
3309         } else {
3310             gchar *val_quoted = css2_escape_quote(val->value);
3311             return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3312             g_free (val_quoted);
3313         }
3314     }
3315     return 0;
3319 /**
3320  *
3321  */
3322 static bool
3323 sp_length_differ(SPILength const *const a, SPILength const *const b)
3325     if (a->unit != b->unit) {
3326         if (a->unit == SP_CSS_UNIT_EM) return true;
3327         if (a->unit == SP_CSS_UNIT_EX) return true;
3328         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3329         if (b->unit == SP_CSS_UNIT_EM) return true;
3330         if (b->unit == SP_CSS_UNIT_EX) return true;
3331         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3332     }
3334     return (a->computed != b->computed);
3339 /**
3340  * Write SPILength object into string.
3341  */
3342 static gint
3343 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3344                        SPILength const *const val, SPILength const *const base, guint const flags)
3346     Inkscape::CSSOStringStream os;
3348     if ((flags & SP_STYLE_FLAG_ALWAYS)
3349         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3350         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3351             && (!base->set || sp_length_differ(val, base))))
3352     {
3353         if (val->inherit) {
3354             return g_snprintf(p, len, "%s:inherit;", key);
3355         } else {
3356             switch (val->unit) {
3357                 case SP_CSS_UNIT_NONE:
3358                     os << key << ":" << val->computed << ";";
3359                     return g_strlcpy(p, os.str().c_str(), len);
3360                     break;
3361                 case SP_CSS_UNIT_PX:
3362                     os << key << ":" << val->computed << "px;";
3363                     return g_strlcpy(p, os.str().c_str(), len);
3364                     break;
3365                 case SP_CSS_UNIT_PT:
3366                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3367                     return g_strlcpy(p, os.str().c_str(), len);
3368                     break;
3369                 case SP_CSS_UNIT_PC:
3370                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3371                     return g_strlcpy(p, os.str().c_str(), len);
3372                     break;
3373                 case SP_CSS_UNIT_MM:
3374                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3375                     return g_strlcpy(p, os.str().c_str(), len);
3376                     break;
3377                 case SP_CSS_UNIT_CM:
3378                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3379                     return g_strlcpy(p, os.str().c_str(), len);
3380                     break;
3381                 case SP_CSS_UNIT_IN:
3382                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3383                     return g_strlcpy(p, os.str().c_str(), len);
3384                     break;
3385                 case SP_CSS_UNIT_EM:
3386                     os << key << ":" << val->value << "em;";
3387                     return g_strlcpy(p, os.str().c_str(), len);
3388                     break;
3389                 case SP_CSS_UNIT_EX:
3390                     os << key << ":" << val->value << "ex;";
3391                     return g_strlcpy(p, os.str().c_str(), len);
3392                     break;
3393                 case SP_CSS_UNIT_PERCENT:
3394                     os << key << ":" << (val->value * 100.0) << "%;";
3395                     return g_strlcpy(p, os.str().c_str(), len);
3396                     break;
3397                 default:
3398                     /* Invalid */
3399                     break;
3400             }
3401         }
3402     }
3403     return 0;
3407 /**
3408  *
3409  */
3410 static bool
3411 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3413     if (a->normal != b->normal) return true;
3414     if (a->normal) return false;
3416     if (a->unit != b->unit) {
3417         if (a->unit == SP_CSS_UNIT_EM) return true;
3418         if (a->unit == SP_CSS_UNIT_EX) return true;
3419         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3420         if (b->unit == SP_CSS_UNIT_EM) return true;
3421         if (b->unit == SP_CSS_UNIT_EX) return true;
3422         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3423     }
3425     return (a->computed != b->computed);
3428 /**
3429  * Write SPILengthOrNormal object into string.
3430  */
3431 static gint
3432 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3433                                SPILengthOrNormal const *const val,
3434                                SPILengthOrNormal const *const base,
3435                                guint const flags)
3437     if ((flags & SP_STYLE_FLAG_ALWAYS)
3438         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3439         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3440             && (!base->set || sp_lengthornormal_differ(val, base))))
3441     {
3442         if (val->normal) {
3443             return g_snprintf(p, len, "%s:normal;", key);
3444         } else {
3445             SPILength length;
3446             length.set = val->set;
3447             length.inherit = val->inherit;
3448             length.unit = val->unit;
3449             length.value = val->value;
3450             length.computed = val->computed;
3451             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3452         }
3453     }
3454     return 0;
3457 /**
3458  *
3459  */
3460 static bool
3461 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3463     return    a->underline != b->underline
3464            || a->overline != b->overline
3465            || a->line_through != b->line_through
3466            || a->blink != b->blink;
3469 /**
3470  * Write SPITextDecoration object into string.
3471  */
3472 static gint
3473 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3474                                SPITextDecoration const *const val,
3475                                SPITextDecoration const *const base,
3476                                guint const flags)
3478     Inkscape::CSSOStringStream os;
3480     if ((flags & SP_STYLE_FLAG_ALWAYS)
3481         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3482         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3483             && (!base->set || sp_textdecoration_differ(val, base))))
3484     {
3485         if (val->inherit) {
3486             return g_snprintf(p, len, "%s:inherit;", key);
3487         } else {
3488             os << key << ":";
3489             if (val->underline || val->overline || val->line_through || val->blink) {
3490                 if (val->underline) os << " underline";
3491                 if (val->overline) os << " overline";
3492                 if (val->line_through) os << " line-through";
3493                 if (val->blink) os << " blink";
3494             } else
3495                 os << "none";
3496             os << ";";
3497             return g_strlcpy(p, os.str().c_str(), len);
3498         }
3499     }
3500     return 0;
3503 /**
3504  *
3505  */
3506 static bool
3507 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3509     if ( (a->isColor() != b->isColor())
3510          || (a->isPaintserver() != b->isPaintserver())
3511          || (a->set != b->set)
3512          || (a->currentcolor != b->currentcolor)
3513          || (a->inherit!= b->inherit) ) {
3514         return true;
3515     }
3517     // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3519     if ( a->isPaintserver() ) {
3520         return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3521     }
3523     if ( a->isColor() ) {
3524         return !(sp_color_is_equal(&a->value.color, &b->value.color)
3525                  && ((a->value.iccColor == b->value.iccColor)
3526                      || (a->value.iccColor && b->value.iccColor
3527                          && (a->value.iccColor->colorProfile == b->value.iccColor->colorProfile)
3528                          && (a->value.iccColor->colors == b->value.iccColor->colors))));
3529     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3530      * in the string representation. */
3531     }
3533     return false;
3538 /**
3539  * Write SPIPaint object into string.
3540  */
3541 static gint
3542 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3543                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3545     int retval = 0;
3547     if ((flags & SP_STYLE_FLAG_ALWAYS)
3548         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3549         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3550             && (!base->set || sp_paint_differ(paint, base))))
3551     {
3552         CSSOStringStream css;
3554         if (paint->inherit) {
3555             css << "inherit";
3556         } else {
3557             if ( paint->value.href && paint->value.href->getURI() ) {
3558                 const gchar* uri = paint->value.href->getURI()->toString();
3559                 css << "url(" << uri << ")";
3560             }
3562             if ( paint->noneSet ) {
3563                 if ( !css.str().empty() ) {
3564                     css << " ";
3565                 }
3566                 css << "none";
3567             }
3569             if ( paint->currentcolor ) {
3570                 if ( !css.str().empty() ) {
3571                     css << " ";
3572                 }
3573                 css << "currentColor";
3574             }
3576             if ( paint->colorSet ) {
3577                 if ( !css.str().empty() ) {
3578                     css << " ";
3579                 }
3580                 char color_buf[8];
3581                 sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
3582                 css << color_buf;
3583             }
3585             if (paint->value.iccColor) {
3586                 if ( !css.str().empty() ) {
3587                     css << " ";
3588                 }
3589                 css << "icc-color(" << paint->value.iccColor->colorProfile;
3590                 for (vector<double>::const_iterator i(paint->value.iccColor->colors.begin()),
3591                          iEnd(paint->value.iccColor->colors.end());
3592                      i != iEnd; ++i) {
3593                     css << ", " << *i;
3594                 }
3595                 css << ')';
3596             }
3597         }
3599         if ( !css.str().empty() ) {
3600             retval = g_snprintf( b, len, "%s:%s;", key, css.gcharp() );
3601         }
3602     }
3604     return retval;
3608 /**
3609  *
3610  */
3611 static bool
3612 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3614     if (a->type != b->type)
3615         return true;
3616     if (a->type == SP_FONT_SIZE_LENGTH) {
3617         if (a->computed != b->computed)
3618             return true;
3619     } else {
3620         if (a->value != b->value)
3621             return true;
3622     }
3623     return false;
3627 /**
3628  * Write SPIFontSize object into string.
3629  */
3630 static gint
3631 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3632                          SPIFontSize const *const val, SPIFontSize const *const base,
3633                          guint const flags)
3635     if ((flags & SP_STYLE_FLAG_ALWAYS)
3636         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3637         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3638             && (!base->set || sp_fontsize_differ(val, base))))
3639     {
3640         if (val->inherit) {
3641             return g_snprintf(p, len, "%s:inherit;", key);
3642         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3643             for (unsigned i = 0; enum_font_size[i].key; i++) {
3644                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3645                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3646                 }
3647             }
3648         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3649             Inkscape::CSSOStringStream os;
3650             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3651             return g_strlcpy(p, os.str().c_str(), len);
3652         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3653             Inkscape::CSSOStringStream os;
3654             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3655             return g_strlcpy(p, os.str().c_str(), len);
3656         }
3657     }
3658     return 0;
3662 /**
3663  * Write SPIFilter object into string.
3664  */
3665 static gint
3666 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3667                          SPIFilter const *const val, SPIFilter const *const base,
3668                          guint const flags)
3670     (void)base; // TODO
3671     if ((flags & SP_STYLE_FLAG_ALWAYS)
3672         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3673         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3674     {
3675         if (val->inherit) {
3676             return g_snprintf(p, len, "%s:inherit;", key);
3677         } else if (val->href && val->href->getURI()) {
3678             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
3679         }
3680     }
3683     return 0;
3687 void SPIPaint::clear()
3689     set = false;
3690     inherit = false;
3691     currentcolor = false;
3692     colorSet = false;
3693     noneSet = false;
3694     sp_color_set_rgb_rgba32( &value.color, 0 );
3695     if ( value.href && value.href->getObject() )
3696     {
3697         value.href->detach();
3698     }
3700     delete value.iccColor;
3701     value.iccColor = 0;
3705 /**
3706  * Clear filter object, and disconnect style from paintserver (if present).
3707  */
3708 static void
3709 sp_style_filter_clear(SPStyle *style)
3711     if (style->filter.href && style->filter.href->getObject())
3712         style->filter.href->detach();
3716 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3718 void
3719 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3721     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3723     if (repr == NULL) return;
3725     g_return_if_fail(linked != NULL);
3727     gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3729     SPCSSAttr *css = sp_repr_css_attr_new();
3730     sp_repr_css_set_property(css, property, val);
3731     g_free(val);
3732     if (recursive) {
3733         sp_repr_css_change_recursive(repr, css, "style");
3734     } else {
3735         sp_repr_css_change(repr, css, "style");
3736     }
3737     sp_repr_css_attr_unref(css);
3741 /**
3742  * Clear all style property attributes in object.
3743  */
3744 void
3745 sp_style_unset_property_attrs(SPObject *o)
3747     if (!o) return;
3749     SPStyle *style = SP_OBJECT_STYLE(o);
3750     if (!style) return;
3752     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3753     if (!repr) return;
3755     if (style->opacity.set) {
3756         repr->setAttribute("opacity", NULL);
3757     }
3758     if (style->color.set) {
3759         repr->setAttribute("color", NULL);
3760     }
3761     if (style->fill.set) {
3762         repr->setAttribute("fill", NULL);
3763     }
3764     if (style->fill_opacity.set) {
3765         repr->setAttribute("fill-opacity", NULL);
3766     }
3767     if (style->fill_rule.set) {
3768         repr->setAttribute("fill-rule", NULL);
3769     }
3770     if (style->stroke.set) {
3771         repr->setAttribute("stroke", NULL);
3772     }
3773     if (style->stroke_width.set) {
3774         repr->setAttribute("stroke-width", NULL);
3775     }
3776     if (style->stroke_linecap.set) {
3777         repr->setAttribute("stroke-linecap", NULL);
3778     }
3779     if (style->stroke_linejoin.set) {
3780         repr->setAttribute("stroke-linejoin", NULL);
3781     }
3782     if (style->marker[SP_MARKER_LOC].set) {
3783         repr->setAttribute("marker", NULL);
3784     }
3785     if (style->marker[SP_MARKER_LOC_START].set) {
3786         repr->setAttribute("marker-start", NULL);
3787     }
3788     if (style->marker[SP_MARKER_LOC_MID].set) {
3789         repr->setAttribute("marker-mid", NULL);
3790     }
3791     if (style->marker[SP_MARKER_LOC_END].set) {
3792         repr->setAttribute("marker-end", NULL);
3793     }
3794     if (style->stroke_opacity.set) {
3795         repr->setAttribute("stroke-opacity", NULL);
3796     }
3797     if (style->stroke_dasharray_set) {
3798         repr->setAttribute("stroke-dasharray", NULL);
3799     }
3800     if (style->stroke_dashoffset_set) {
3801         repr->setAttribute("stroke-dashoffset", NULL);
3802     }
3803     if (style->text_private && style->text->font_family.set) {
3804         repr->setAttribute("font-family", NULL);
3805     }
3806     if (style->text_anchor.set) {
3807         repr->setAttribute("text-anchor", NULL);
3808     }
3809     if (style->writing_mode.set) {
3810         repr->setAttribute("writing_mode", NULL);
3811     }
3812     if (style->filter.set) {
3813         repr->setAttribute("filter", NULL);
3814     }
3815     if (style->enable_background.set) {
3816         repr->setAttribute("enable-background", NULL);
3817     }
3820 /**
3821  * \pre style != NULL.
3822  * \pre flags in {IFSET, ALWAYS}.
3823  */
3824 SPCSSAttr *
3825 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3827     g_return_val_if_fail(style != NULL, NULL);
3828     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3829                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3830                          NULL);
3831     gchar *style_str = sp_style_write_string(style, flags);
3832     SPCSSAttr *css = sp_repr_css_attr_new();
3833     sp_repr_css_attr_add_from_string(css, style_str);
3834     g_free(style_str);
3835     return css;
3839 /**
3840  * \pre object != NULL
3841  * \pre flags in {IFSET, ALWAYS}.
3842  */
3843 SPCSSAttr *
3844 sp_css_attr_from_object(SPObject *object, guint const flags)
3846     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3847                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3848                          NULL);
3849     SPStyle const *const style = SP_OBJECT_STYLE(object);
3850     if (style == NULL)
3851         return NULL;
3852     return sp_css_attr_from_style(style, flags);
3855 /**
3856  * Unset any text-related properties
3857  */
3858 SPCSSAttr *
3859 sp_css_attr_unset_text(SPCSSAttr *css)
3861     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3862     sp_repr_css_set_property(css, "font-size", NULL);
3863     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3864     sp_repr_css_set_property(css, "font-style", NULL);
3865     sp_repr_css_set_property(css, "font-variant", NULL);
3866     sp_repr_css_set_property(css, "font-weight", NULL);
3867     sp_repr_css_set_property(css, "font-stretch", NULL);
3868     sp_repr_css_set_property(css, "font-family", NULL);
3869     sp_repr_css_set_property(css, "text-indent", NULL);
3870     sp_repr_css_set_property(css, "text-align", NULL);
3871     sp_repr_css_set_property(css, "text-decoration", NULL);
3872     sp_repr_css_set_property(css, "line-height", NULL);
3873     sp_repr_css_set_property(css, "letter-spacing", NULL);
3874     sp_repr_css_set_property(css, "word-spacing", NULL);
3875     sp_repr_css_set_property(css, "text-transform", NULL);
3876     sp_repr_css_set_property(css, "direction", NULL);
3877     sp_repr_css_set_property(css, "block-progression", NULL);
3878     sp_repr_css_set_property(css, "writing-mode", NULL);
3879     sp_repr_css_set_property(css, "text-anchor", NULL);
3880     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3881     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3882     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3883     sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3885     return css;
3888 bool
3889 is_url(char const *p)
3891     if (p == NULL)
3892         return false;
3893 /** \todo
3894  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3895  * in property values must start with 'url('.
3896  */
3897     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3900 /**
3901  * Unset any properties that contain URI values.
3902  *
3903  * Used for storing style that will be reused across documents when carrying
3904  * the referenced defs is impractical.
3905  */
3906 SPCSSAttr *
3907 sp_css_attr_unset_uris(SPCSSAttr *css)
3909 // All properties that may hold <uri> or <paint> according to SVG 1.1
3910     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3911     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3912     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3913     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3914     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3915     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3916     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3917     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3918     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3919     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3921     return css;
3924 /**
3925  * Scale a single-value property.
3926  */
3927 void
3928 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3929                                   double ex, bool only_with_units = false)
3931     gchar const *w = sp_repr_css_property(css, property, NULL);
3932     if (w) {
3933         gchar *units = NULL;
3934         double wd = g_ascii_strtod(w, &units) * ex;
3935         if (w == units) {// nothing converted, non-numeric value
3936             return;
3937         }
3938         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3939             // only_with_units, but no units found, so do nothing.
3940             return;
3941         }
3942         Inkscape::CSSOStringStream os;
3943         os << wd << units; // reattach units
3944         sp_repr_css_set_property(css, property, os.str().c_str());
3945     }
3948 /**
3949  * Scale a list-of-values property.
3950  */
3951 void
3952 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3954     gchar const *string = sp_repr_css_property(css, property, NULL);
3955     if (string) {
3956         Inkscape::CSSOStringStream os;
3957         gchar **a = g_strsplit(string, ",", 10000);
3958         bool first = true;
3959         for (gchar **i = a; i != NULL; i++) {
3960             gchar *w = *i;
3961             if (w == NULL)
3962                 break;
3963             gchar *units = NULL;
3964             double wd = g_ascii_strtod(w, &units) * ex;
3965             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3966                 g_strfreev(a);
3967                 return;
3968             }
3969             if (!first) {
3970                 os << ",";
3971             }
3972             os << wd << units; // reattach units
3973             first = false;
3974         }
3975         sp_repr_css_set_property(css, property, os.str().c_str());
3976         g_strfreev(a);
3977     }
3980 /**
3981  * Scale any properties that may hold <length> by ex.
3982  */
3983 SPCSSAttr *
3984 sp_css_attr_scale(SPCSSAttr *css, double ex)
3986     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
3987     sp_css_attr_scale_property_single(css, "stroke-width", ex);
3988     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
3989     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
3990     sp_css_attr_scale_property_single(css, "font-size", ex);
3991     sp_css_attr_scale_property_single(css, "kerning", ex);
3992     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
3993     sp_css_attr_scale_property_single(css, "word-spacing", ex);
3994     sp_css_attr_scale_property_single(css, "line-height", ex, true);
3996     return css;
4000 /**
4001  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4002  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4003  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4004  * it parsed for us. So this function is only used to remove those quotes and for
4005  * presentation attributes, without any unescaping. (XML unescaping
4006  * (&amp; etc) is done by XML parser.)
4007  */
4008 gchar *
4009 attribute_unquote(gchar const *val)
4011     if (val) {
4012         if (*val == '\'' || *val == '"') {
4013             int l = strlen(val);
4014             if (l >= 2) {
4015                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4016                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4017                     return (g_strndup (val+1, l-2));
4018                 }
4019             }
4020         }
4021     }
4023     return (val? g_strdup (val) : NULL);
4026 /**
4027  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4028  */
4029 gchar *
4030 css2_escape_quote(gchar const *val) {
4032     Glib::ustring t;
4033     bool quote = false;
4034     bool last_was_space = false;
4036     for (gchar const *i = val; *i; i++) {
4037         bool is_space = ( *i == ' ' );
4038         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4039             // ASCII alphanumeric, - and _ don't require quotes
4040             t.push_back(*i);
4041         } else if ( is_space && !last_was_space ) {
4042             // non-consecutive spaces don't require quotes
4043             t.push_back(*i);
4044         } else if (*i=='\'') {
4045             // single quotes require escaping and quotes
4046             t.push_back('\\');
4047             t.push_back(*i);
4048             quote = true;
4049         } else {
4050             // everything else requires quotes
4051             t.push_back(*i);
4052             quote = true;
4053         }
4054         if (i == val && !g_ascii_isalpha(*i)) {
4055             // a non-ASCII/non-alpha initial character requires quotes
4056             quote = true;
4057         }
4058         last_was_space = is_space;
4059     }
4061     if (last_was_space) {
4062         // a trailing space requires quotes
4063         quote = true;
4064     }
4066     if (quote) {
4067         // we use single quotes so the result can be stored in an XML
4068         // attribute without incurring un-aesthetic additional quoting
4069         // (our XML emitter always uses double quotes)
4070         t.insert(t.begin(), '\'');
4071         t.push_back('\'');
4072     }
4074     return (t.empty() ? NULL : g_strdup (t.c_str()));
4077 /*
4078   Local Variables:
4079   mode:c++
4080   c-file-style:"stroustrup"
4081   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4082   indent-tabs-mode:nil
4083   fill-column:99
4084   End:
4085 */
4086 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :