Code

fix 246944
[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 <cstring>
23 #include <string>
25 #include "libcroco/cr-sel-eng.h"
26 #include "xml/croco-node-iface.h"
28 #include "svg/svg.h"
29 #include "svg/svg-color.h"
30 #include "svg/svg-icc-color.h"
32 #include "display/canvas-bpath.h"
33 #include "attributes.h"
34 #include "document.h"
35 #include "extract-uri.h"
36 #include "uri-references.h"
37 #include "uri.h"
38 #include "sp-paint-server.h"
39 #include "streq.h"
40 #include "strneq.h"
41 #include "style.h"
42 #include "svg/css-ostringstream.h"
43 #include "xml/repr.h"
44 #include "unit-constants.h"
45 #include "2geom/isnan.h"
46 #include "macros.h"
48 #include "sp-filter-reference.h"
50 #include <sigc++/functors/ptr_fun.h>
51 #include <sigc++/adaptors/bind.h>
53 using Inkscape::CSSOStringStream;
54 using std::vector;
56 #define BMAX 8192
58 class SPStyleEnum;
60 /*#########################
61 ## FORWARD DECLARATIONS
62 #########################*/
63 static void sp_style_clear(SPStyle *style);
65 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
67 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
68 static void sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent);
69 static void sp_style_read_dash(SPStyle *style, gchar const *str);
71 static SPTextStyle *sp_text_style_new(void);
72 static void sp_text_style_clear(SPTextStyle *ts);
73 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
74 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
75 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
76 static void sp_style_privatize_text(SPStyle *style);
78 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
79 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
80 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
81 static void sp_style_read_istring(SPIString *val, gchar const *str);
82 static void sp_style_read_ilength(SPILength *val, gchar const *str);
83 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
84 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
85 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
86 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
87 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
88 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
90 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
91 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
92 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
93 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
95 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
96 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
97 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
98 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
99 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
100 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
101 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
102 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);
103 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);
104 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
106 static void sp_style_filter_clear(SPStyle *style);
108 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
109 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
111 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
112 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
114 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
116 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
117 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
119 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
121 struct SPStyleEnum {
122     gchar const *key;
123     gint value;
124 };
126 static SPStyleEnum const enum_fill_rule[] = {
127     {"nonzero", SP_WIND_RULE_NONZERO},
128     {"evenodd", SP_WIND_RULE_EVENODD},
129     {NULL, -1}
130 };
132 static SPStyleEnum const enum_stroke_linecap[] = {
133     {"butt", SP_STROKE_LINECAP_BUTT},
134     {"round", SP_STROKE_LINECAP_ROUND},
135     {"square", SP_STROKE_LINECAP_SQUARE},
136     {NULL, -1}
137 };
139 static SPStyleEnum const enum_stroke_linejoin[] = {
140     {"miter", SP_STROKE_LINEJOIN_MITER},
141     {"round", SP_STROKE_LINEJOIN_ROUND},
142     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
143     {NULL, -1}
144 };
146 static SPStyleEnum const enum_font_style[] = {
147     {"normal", SP_CSS_FONT_STYLE_NORMAL},
148     {"italic", SP_CSS_FONT_STYLE_ITALIC},
149     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
150     {NULL, -1}
151 };
153 static SPStyleEnum const enum_font_size[] = {
154     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
155     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
156     {"small", SP_CSS_FONT_SIZE_SMALL},
157     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
158     {"large", SP_CSS_FONT_SIZE_LARGE},
159     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
160     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
161     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
162     {"larger", SP_CSS_FONT_SIZE_LARGER},
163     {NULL, -1}
164 };
166 static SPStyleEnum const enum_font_variant[] = {
167     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
168     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
169     {NULL, -1}
170 };
172 static SPStyleEnum const enum_font_weight[] = {
173     {"100", SP_CSS_FONT_WEIGHT_100},
174     {"200", SP_CSS_FONT_WEIGHT_200},
175     {"300", SP_CSS_FONT_WEIGHT_300},
176     {"400", SP_CSS_FONT_WEIGHT_400},
177     {"500", SP_CSS_FONT_WEIGHT_500},
178     {"600", SP_CSS_FONT_WEIGHT_600},
179     {"700", SP_CSS_FONT_WEIGHT_700},
180     {"800", SP_CSS_FONT_WEIGHT_800},
181     {"900", SP_CSS_FONT_WEIGHT_900},
182     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
183     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
184     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
185     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
186     {NULL, -1}
187 };
189 static SPStyleEnum const enum_font_stretch[] = {
190     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
191     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
192     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
193     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
194     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
195     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
196     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
197     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
198     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
199     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
200     {"wider", SP_CSS_FONT_STRETCH_WIDER},
201     {NULL, -1}
202 };
204 static SPStyleEnum const enum_text_align[] = {
205     {"start", SP_CSS_TEXT_ALIGN_START},
206     {"end", SP_CSS_TEXT_ALIGN_END},
207     {"left", SP_CSS_TEXT_ALIGN_LEFT},
208     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
209     {"center", SP_CSS_TEXT_ALIGN_CENTER},
210     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
211     {NULL, -1}
212 };
214 static SPStyleEnum const enum_text_transform[] = {
215     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
216     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
217     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
218     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
219     {NULL, -1}
220 };
222 static SPStyleEnum const enum_text_anchor[] = {
223     {"start", SP_CSS_TEXT_ANCHOR_START},
224     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
225     {"end", SP_CSS_TEXT_ANCHOR_END},
226     {NULL, -1}
227 };
229 static SPStyleEnum const enum_direction[] = {
230     {"ltr", SP_CSS_DIRECTION_LTR},
231     {"rtl", SP_CSS_DIRECTION_RTL},
232     {NULL, -1}
233 };
235 static SPStyleEnum const enum_block_progression[] = {
236     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
237     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
238     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
239     {NULL, -1}
240 };
242 static SPStyleEnum const enum_writing_mode[] = {
243     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
244      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
245      * value is lr-tb rather than lr.
246      *
247      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
248      * sharing enumerators for different strings may be a bug (once we support ecma script).
249      */
250     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
251     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
252     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
253     {"lr", SP_CSS_WRITING_MODE_LR_TB},
254     {"rl", SP_CSS_WRITING_MODE_RL_TB},
255     {"tb", SP_CSS_WRITING_MODE_TB_RL},
256     {NULL, -1}
257 };
259 static SPStyleEnum const enum_visibility[] = {
260     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
261     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
262     {"visible", SP_CSS_VISIBILITY_VISIBLE},
263     {NULL, -1}
264 };
266 static SPStyleEnum const enum_overflow[] = {
267     {"visible", SP_CSS_OVERFLOW_VISIBLE},
268     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
269     {"scroll", SP_CSS_OVERFLOW_SCROLL},
270     {"auto", SP_CSS_OVERFLOW_AUTO},
271     {NULL, -1}
272 };
274 static SPStyleEnum const enum_display[] = {
275     {"none",      SP_CSS_DISPLAY_NONE},
276     {"inline",    SP_CSS_DISPLAY_INLINE},
277     {"block",     SP_CSS_DISPLAY_BLOCK},
278     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
279     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
280     {"compact",   SP_CSS_DISPLAY_COMPACT},
281     {"marker",    SP_CSS_DISPLAY_MARKER},
282     {"table",     SP_CSS_DISPLAY_TABLE},
283     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
284     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
285     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
286     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
287     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
288     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
289     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
290     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
291     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
292     {NULL, -1}
293 };
295 static SPStyleEnum const enum_shape_rendering[] = {
296     {"auto", 0},
297     {"optimizeSpeed", 0},
298     {"crispEdges", 0},
299     {"geometricPrecision", 0},
300     {NULL, -1}
301 };
303 static SPStyleEnum const enum_color_rendering[] = {
304     {"auto", 0},
305     {"optimizeSpeed", 0},
306     {"optimizeQuality", 0},
307     {NULL, -1}
308 };
310 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
312 static SPStyleEnum const enum_text_rendering[] = {
313     {"auto", 0},
314     {"optimizeSpeed", 0},
315     {"optimizeLegibility", 0},
316     {"geometricPrecision", 0},
317     {NULL, -1}
318 };
320 static SPStyleEnum const enum_enable_background[] = {
321     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
322     {"new", SP_CSS_BACKGROUND_NEW},
323     {NULL, -1}
324 };
326 /**
327  * Release callback.
328  */
329 static void
330 sp_style_object_release(SPObject *object, SPStyle *style)
332     (void)object; // TODO
333     style->object = NULL;
336 /**
337  * Emit style modified signal on style's object if the filter changed.
338  */
339 static void
340 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
342     (void)flags; // TODO
343     SPFilter *filter=static_cast<SPFilter *>(obj);
344     if (style->getFilter() == filter)
345     {
346         if (style->object) {
347             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
348         }
349     }
352 /**
353  * Gets called when the filter is (re)attached to the style
354  */
355 static void
356 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
358     if (old_ref) {
359         style->filter_modified_connection.disconnect();
360     }
361     if ( SP_IS_FILTER(ref))
362     {
363         style->filter_modified_connection = 
364            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
365     }
367     sp_style_filter_ref_modified(ref, 0, style);
370 /**
371  * Emit style modified signal on style's object if server is style's fill
372  * or stroke paint server.
373  */
374 static void
375 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
377     (void)flags; // TODO
378     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
380     if ((style->fill.isPaintserver())
381         && style->getFillPaintServer() == server)
382     {
383         if (style->object) {
384             /** \todo
385              * fixme: I do not know, whether it is optimal - we are
386              * forcing reread of everything (Lauris)
387              */
388             /** \todo
389              * fixme: We have to use object_modified flag, because parent
390              * flag is only available downstreams.
391              */
392             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
393         }
394     } else if ((style->stroke.isPaintserver())
395         && style->getStrokePaintServer() == server)
396     {
397         if (style->object) {
398             /// \todo fixme:
399             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
400         }
401     } else if (server) {
402         g_assert_not_reached();
403     }
406 /**
407  * Gets called when the paintserver is (re)attached to the style
408  */
409 static void
410 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
412     if (old_ref) {
413         style->fill_ps_modified_connection.disconnect();
414     }
415     if (SP_IS_PAINT_SERVER(ref)) {
416         style->fill_ps_modified_connection = 
417            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
418     }
420     sp_style_paint_server_ref_modified(ref, 0, style);
423 /**
424  * Gets called when the paintserver is (re)attached to the style
425  */
426 static void
427 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
429     if (old_ref) {
430         style->stroke_ps_modified_connection.disconnect();
431     }
432     if (SP_IS_PAINT_SERVER(ref)) {
433         style->stroke_ps_modified_connection = 
434           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
435     }
437     sp_style_paint_server_ref_modified(ref, 0, style);
440 /**
441  * Returns a new SPStyle object with settings as per sp_style_clear().
442  */
443 SPStyle *
444 sp_style_new(SPDocument *document)
446     SPStyle *const style = g_new0(SPStyle, 1);
448     style->refcount = 1;
449     style->object = NULL;
450     style->document = document;
451     style->text = sp_text_style_new();
452     style->text_private = TRUE;
454     sp_style_clear(style);
456     style->cloned = false;
458     new (&style->release_connection) sigc::connection();
459     new (&style->filter_modified_connection) sigc::connection();
460     new (&style->fill_ps_modified_connection) sigc::connection();
461     new (&style->stroke_ps_modified_connection) sigc::connection();
463     return style;
467 /**
468  * Creates a new SPStyle object, and attaches it to the specified SPObject.
469  */
470 SPStyle *
471 sp_style_new_from_object(SPObject *object)
473     g_return_val_if_fail(object != NULL, NULL);
474     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
476     SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
477     style->object = object;
478     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
480     if (object && SP_OBJECT_IS_CLONED(object)) {
481         style->cloned = true;
482     }
484     return style;
488 /**
489  * Increase refcount of style.
490  */
491 SPStyle *
492 sp_style_ref(SPStyle *style)
494     g_return_val_if_fail(style != NULL, NULL);
495     g_return_val_if_fail(style->refcount > 0, NULL);
497     style->refcount += 1;
499     return style;
503 /**
504  * Decrease refcount of style with possible destruction.
505  */
506 SPStyle *
507 sp_style_unref(SPStyle *style)
509     g_return_val_if_fail(style != NULL, NULL);
510     g_return_val_if_fail(style->refcount > 0, NULL);
512     style->refcount -= 1;
514     if (style->refcount < 1) {
515         style->release_connection.disconnect();
516         style->release_connection.~connection();
517         if (style->text) sp_text_style_unref(style->text);
519          if (style->fill.value.href) {
520              style->fill_ps_modified_connection.disconnect();
521              delete style->fill.value.href;
522              style->fill.value.href = NULL;
523          }
524          if (style->stroke.value.href) {
525              style->stroke_ps_modified_connection.disconnect();
526              delete style->stroke.value.href;
527              style->stroke.value.href = NULL;
528          }
529          if (style->filter.href) {
530              style->filter_modified_connection.disconnect();
531              delete style->filter.href;
532              style->filter.href = NULL;
533          }
535         style->filter_modified_connection.~connection();
536         style->fill_ps_modified_connection.~connection();
537         style->stroke_ps_modified_connection.~connection();
539         style->fill.clear();
540         style->stroke.clear();
541         sp_style_filter_clear(style);
543         g_free(style->stroke_dash.dash);
544         g_free(style);
545     }
547     return NULL;
550 /**
551  *  Reads the various style parameters for an object from repr.
552  */
553 static void
554 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
556     g_assert(style != NULL);
557     g_assert(repr != NULL);
558     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
560     sp_style_clear(style);
562     if (object && SP_OBJECT_IS_CLONED(object)) {
563         style->cloned = true;
564     }
566     /* 1. Style attribute */
567     gchar const *val = repr->attribute("style");
568     if (val != NULL) {
569         sp_style_merge_from_style_string(style, val);
570     }
572     if (object) {
573         sp_style_merge_from_object_stylesheet(style, object);
574     } else {
575         /** \todo No stylesheet information. Find out under what circumstances
576          * this occurs, and handle accordingly.  (If we really wanted to, we
577          * could probably get stylesheets by going through repr->doc.)
578          */
579     }
581     /* 2. Presentation attributes */
582     /* CSS2 */
583     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
584     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
585     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
586     /* Font */
587     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
588     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
589     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
590     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
591     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
592     /* Text (css2 chapter 16) */
593     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
594     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
595     if (!style->text_decoration.set) {
596         val = repr->attribute("text-decoration");
597         if (val) {
598             sp_style_read_itextdecoration(&style->text_decoration, val);
599         }
600     }
601     if (!style->line_height.set) {
602         val = repr->attribute("line-height");
603         if (val) {
604             sp_style_read_ilengthornormal(&style->line_height, val);
605         }
606     }
607     if (!style->letter_spacing.set) {
608         val = repr->attribute("letter-spacing");
609         if (val) {
610             sp_style_read_ilengthornormal(&style->letter_spacing, val);
611         }
612     }
613     if (!style->word_spacing.set) {
614         val = repr->attribute("word-spacing");
615         if (val) {
616             sp_style_read_ilengthornormal(&style->word_spacing, val);
617         }
618     }
619     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
620     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
621     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
623     /* SVG */
624     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
625                             enum_writing_mode, true);
626     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
627                             enum_text_anchor, true);
629     /* opacity */
630     if (!style->opacity.set) {
631         val = repr->attribute("opacity");
632         if (val) {
633             sp_style_read_iscale24(&style->opacity, val);
634         }
635     }
636     /* color */
637     if (!style->color.set) {
638         val = repr->attribute("color");
639         if (val) {
640             sp_style_read_icolor(&style->color, val, style, ( object
641                                                               ? SP_OBJECT_DOCUMENT(object)
642                                                               : NULL ));
643         }
644     }
645     /* fill */
646     if (!style->fill.set) {
647         val = repr->attribute("fill");
648         if (val) {
649             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
650         }
651     }
652     /* fill-opacity */
653     if (!style->fill_opacity.set) {
654         val = repr->attribute("fill-opacity");
655         if (val) {
656             sp_style_read_iscale24(&style->fill_opacity, val);
657         }
658     }
659     /* fill-rule */
660     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
661     /* stroke */
662     if (!style->stroke.set) {
663         val = repr->attribute("stroke");
664         if (val) {
665             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
666         }
667     }
668     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
669     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
670     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
671     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
673     /* markers */
674     if (!style->marker[SP_MARKER_LOC].set) {
675         val = repr->attribute("marker");
676         if (val) {
677             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
678         }
679     }
680     if (!style->marker[SP_MARKER_LOC_START].set) {
681         val = repr->attribute("marker-start");
682         if (val) {
683             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
684         }
685     }
686     if (!style->marker[SP_MARKER_LOC_MID].set) {
687         val = repr->attribute("marker-mid");
688         if (val) {
689             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
690         }
691     }
692     if (!style->marker[SP_MARKER_LOC_END].set) {
693         val = repr->attribute("marker-end");
694         if (val) {
695             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
696         }
697     }
699     /* stroke-opacity */
700     if (!style->stroke_opacity.set) {
701         val = repr->attribute("stroke-opacity");
702         if (val) {
703             sp_style_read_iscale24(&style->stroke_opacity, val);
704         }
705     }
706     if (!style->stroke_dasharray_set) {
707         val = repr->attribute("stroke-dasharray");
708         if (val) {
709             sp_style_read_dash(style, val);
710         }
711     }
712     if (!style->stroke_dashoffset_set) {
713         /* fixme */
714         val = repr->attribute("stroke-dashoffset");
715         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
716             style->stroke_dashoffset_set = TRUE;
717         } else {
718             style->stroke_dashoffset_set = FALSE;
719         }
720     }
721     
722     /* -inkscape-font-specification */
723     if (!style->text_private || !style->text->font_specification.set) {
724         val = repr->attribute("-inkscape-font-specification");
725         if (val) {
726             if (!style->text_private) sp_style_privatize_text(style);
727             gchar *val_unquoted = attribute_unquote(val);
728             sp_style_read_istring(&style->text->font_specification, val_unquoted);
729             if (val_unquoted) g_free (val_unquoted);
730         }
731     }
733     /* font-family */
734     if (!style->text_private || !style->text->font_family.set) {
735         val = repr->attribute("font-family");
736         if (val) {
737             if (!style->text_private) sp_style_privatize_text(style);
738             gchar *val_unquoted = attribute_unquote(val);
739             sp_style_read_istring(&style->text->font_family, val_unquoted);
740             if (val_unquoted) g_free (val_unquoted);
741         }
742     }
744     /* filter effects */
745     if (!style->filter.set) {
746         val = repr->attribute("filter");
747         if (val) {
748                       sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
749         }
750     }
751     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
752                             "enable-background", enum_enable_background, true);
754     /* 3. Merge from parent */
755     if (object) {
756         if (object->parent) {
757             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
758         }
759     } else {
760         if (sp_repr_parent(repr)) {
761             /// \todo fixme: This is not the prettiest thing (Lauris)
762             SPStyle *parent = sp_style_new(NULL);
763             sp_style_read(parent, NULL, sp_repr_parent(repr));
764             sp_style_merge_from_parent(style, parent);
765             sp_style_unref(parent);
766         }
767     }
771 /**
772  * Read style properties from object's repr.
773  *
774  * 1. Reset existing object style
775  * 2. Load current effective object style
776  * 3. Load i attributes from immediate parent (which has to be up-to-date)
777  */
778 void
779 sp_style_read_from_object(SPStyle *style, SPObject *object)
781     g_return_if_fail(style != NULL);
782     g_return_if_fail(object != NULL);
783     g_return_if_fail(SP_IS_OBJECT(object));
785     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
786     g_return_if_fail(repr != NULL);
788     sp_style_read(style, object, repr);
792 /**
793  * Read style properties from repr only.
794  */
795 void
796 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
798     g_return_if_fail(style != NULL);
799     g_return_if_fail(repr != NULL);
801     sp_style_read(style, NULL, repr);
806 /**
807  *
808  */
809 static void
810 sp_style_privatize_text(SPStyle *style)
812     SPTextStyle *text = style->text;
813     style->text = sp_text_style_duplicate_unset(style->text);
814     sp_text_style_unref(text);
815     style->text_private = TRUE;
819 /**
820  * Merge property into style.
821  *
822  * Should be called in order of highest to lowest precedence.
823  * E.g. for a single style string, call from the last declaration to the first,
824  * as CSS says that later declarations override earlier ones.
825  *
826  * \pre val != NULL.
827  */
828 static void
829 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
831     g_return_if_fail(val != NULL);
833     switch (id) {
834         case SP_PROP_INKSCAPE_FONT_SPEC:
835             if (!style->text_private) sp_style_privatize_text(style);
836             if (!style->text->font_specification.set) {
837                 gchar *val_unquoted = attribute_unquote(val);
838                 sp_style_read_istring(&style->text->font_specification, val_unquoted);
839                 if (val_unquoted) g_free (val_unquoted);
840             }
841             break;
842         /* CSS2 */
843         /* Font */
844         case SP_PROP_FONT_FAMILY:
845             if (!style->text_private) sp_style_privatize_text(style);
846             if (!style->text->font_family.set) {
847                 gchar *val_unquoted = attribute_unquote(val);
848                 sp_style_read_istring(&style->text->font_family, val_unquoted);
849                 if (val_unquoted) g_free (val_unquoted);
850             }
851             break;
852         case SP_PROP_FONT_SIZE:
853             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
854             break;
855         case SP_PROP_FONT_SIZE_ADJUST:
856             if (strcmp(val, "none") != 0) {
857                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
858             }
859             break;
860         case SP_PROP_FONT_STYLE:
861             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
862             break;
863         case SP_PROP_FONT_VARIANT:
864             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
865             break;
866         case SP_PROP_FONT_WEIGHT:
867             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
868             break;
869         case SP_PROP_FONT_STRETCH:
870             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
871             break;
872         case SP_PROP_FONT:
873             if (!style->text_private) sp_style_privatize_text(style);
874             if (!style->text->font.set) {
875                 g_free(style->text->font.value);
876                 style->text->font.value = g_strdup(val);
877                 style->text->font.set = TRUE;
878                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
879             }
880             break;
881             /* Text */
882         case SP_PROP_TEXT_INDENT:
883             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
884             break;
885         case SP_PROP_TEXT_ALIGN:
886             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
887             break;
888         case SP_PROP_TEXT_DECORATION:
889             if (!style->text_decoration.set) {
890                 sp_style_read_itextdecoration(&style->text_decoration, val);
891             }
892             break;
893         case SP_PROP_LINE_HEIGHT:
894             if (!style->line_height.set) {
895                 sp_style_read_ilengthornormal(&style->line_height, val);
896             }
897             break;
898         case SP_PROP_LETTER_SPACING:
899             if (!style->letter_spacing.set) {
900                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
901             }
902             break;
903         case SP_PROP_WORD_SPACING:
904             if (!style->word_spacing.set) {
905                 sp_style_read_ilengthornormal(&style->word_spacing, val);
906             }
907             break;
908         case SP_PROP_TEXT_TRANSFORM:
909             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
910             break;
911             /* Text (css3) */
912         case SP_PROP_DIRECTION:
913             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
914             break;
915         case SP_PROP_BLOCK_PROGRESSION:
916             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
917             break;
918         case SP_PROP_WRITING_MODE:
919             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
920             break;
921         case SP_PROP_TEXT_ANCHOR:
922             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
923             break;
924             /* Text (unimplemented) */
925         case SP_PROP_TEXT_RENDERING: {
926             /* Ignore the hint. */
927             SPIEnum dummy;
928             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
929             break;
930         }
931         case SP_PROP_ALIGNMENT_BASELINE:
932             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
933             break;
934         case SP_PROP_BASELINE_SHIFT:
935             g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
936             break;
937         case SP_PROP_DOMINANT_BASELINE:
938             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
939             break;
940         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
941             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
942             break;
943         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
944             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
945             break;
946         case SP_PROP_KERNING:
947             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
948             break;
949             /* Misc */
950         case SP_PROP_CLIP:
951             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
952             break;
953         case SP_PROP_COLOR:
954             if (!style->color.set) {
955                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
956             }
957             break;
958         case SP_PROP_CURSOR:
959             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
960             break;
961         case SP_PROP_DISPLAY:
962             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
963             break;
964         case SP_PROP_OVERFLOW:
965             /** \todo
966              * FIXME: not supported properly yet, we just read and write it,
967              * but act as if it is always "display".
968              */
969             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
970             break;
971         case SP_PROP_VISIBILITY:
972             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
973             break;
974             /* SVG */
975             /* Clip/Mask */
976         case SP_PROP_CLIP_PATH:
977             g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
978             break;
979         case SP_PROP_CLIP_RULE:
980             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
981             break;
982         case SP_PROP_MASK:
983             g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
984             break;
985         case SP_PROP_OPACITY:
986             if (!style->opacity.set) {
987                 sp_style_read_iscale24(&style->opacity, val);
988             }
989             break;
990         case SP_PROP_ENABLE_BACKGROUND:
991             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
992                                     enum_enable_background, true);
993             break;
994             /* Filter */
995         case SP_PROP_FILTER:
996             if (!style->filter.set && !style->filter.inherit) {
997                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
998             }
999             break;
1000         case SP_PROP_FLOOD_COLOR:
1001             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
1002             break;
1003         case SP_PROP_FLOOD_OPACITY:
1004             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
1005             break;
1006         case SP_PROP_LIGHTING_COLOR:
1007             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
1008             break;
1009             /* Gradient */
1010         case SP_PROP_STOP_COLOR:
1011             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
1012             break;
1013         case SP_PROP_STOP_OPACITY:
1014             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
1015             break;
1016             /* Interactivity */
1017         case SP_PROP_POINTER_EVENTS:
1018             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
1019             break;
1020             /* Paint */
1021         case SP_PROP_COLOR_INTERPOLATION:
1022             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1023             break;
1024         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1025             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1026             break;
1027         case SP_PROP_COLOR_PROFILE:
1028             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1029             break;
1030         case SP_PROP_COLOR_RENDERING: {
1031             /* Ignore the hint. */
1032             SPIEnum dummy;
1033             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1034             break;
1035         }
1036         case SP_PROP_FILL:
1037             if (!style->fill.set) {
1038                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1039             }
1040             break;
1041         case SP_PROP_FILL_OPACITY:
1042             if (!style->fill_opacity.set) {
1043                 sp_style_read_iscale24(&style->fill_opacity, val);
1044             }
1045             break;
1046         case SP_PROP_FILL_RULE:
1047             if (!style->fill_rule.set) {
1048                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1049             }
1050             break;
1051         case SP_PROP_IMAGE_RENDERING: {
1052             /* Ignore the hint. */
1053             SPIEnum dummy;
1054             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1055             break;
1056         }
1057         case SP_PROP_MARKER:
1058             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1059             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1060             if (!style->marker[SP_MARKER_LOC].set) {
1061                 g_free(style->marker[SP_MARKER_LOC].value);
1062                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1063                 style->marker[SP_MARKER_LOC].set = TRUE;
1064                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1065             }
1066             break;
1068         case SP_PROP_MARKER_START:
1069             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1070             if (!style->marker[SP_MARKER_LOC_START].set) {
1071                 g_free(style->marker[SP_MARKER_LOC_START].value);
1072                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1073                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1074                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1075             }
1076             break;
1077         case SP_PROP_MARKER_MID:
1078             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1079             if (!style->marker[SP_MARKER_LOC_MID].set) {
1080                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1081                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1082                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1083                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1084             }
1085             break;
1086         case SP_PROP_MARKER_END:
1087             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1088             if (!style->marker[SP_MARKER_LOC_END].set) {
1089                 g_free(style->marker[SP_MARKER_LOC_END].value);
1090                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1091                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1092                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1093             }
1094             break;
1096         case SP_PROP_SHAPE_RENDERING: {
1097             /* Ignore the hint. */
1098             SPIEnum dummy;
1099             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1100             break;
1101         }
1103         case SP_PROP_STROKE:
1104             if (!style->stroke.set) {
1105                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1106             }
1107             break;
1108         case SP_PROP_STROKE_WIDTH:
1109             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1110             break;
1111         case SP_PROP_STROKE_DASHARRAY:
1112             if (!style->stroke_dasharray_set) {
1113                 sp_style_read_dash(style, val);
1114             }
1115             break;
1116         case SP_PROP_STROKE_DASHOFFSET:
1117             if (!style->stroke_dashoffset_set) {
1118                 /* fixme */
1119                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1120                     style->stroke_dashoffset_set = TRUE;
1121                 } else {
1122                     style->stroke_dashoffset_set = FALSE;
1123                 }
1124             }
1125             break;
1126         case SP_PROP_STROKE_LINECAP:
1127             if (!style->stroke_linecap.set) {
1128                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1129             }
1130             break;
1131         case SP_PROP_STROKE_LINEJOIN:
1132             if (!style->stroke_linejoin.set) {
1133                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1134             }
1135             break;
1136         case SP_PROP_STROKE_MITERLIMIT:
1137             if (!style->stroke_miterlimit.set) {
1138                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1139             }
1140             break;
1141         case SP_PROP_STROKE_OPACITY:
1142             if (!style->stroke_opacity.set) {
1143                 sp_style_read_iscale24(&style->stroke_opacity, val);
1144             }
1145             break;
1147         default:
1148             g_warning("Invalid style property id: %d value: %s", id, val);
1149             break;
1150     }
1153 static void
1154 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1156     /** \todo Ensure that property is lcased, as per
1157      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1158      * Should probably be done in libcroco.
1159      */
1160     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1161     if (prop_idx != SP_ATTR_INVALID) {
1162         /** \todo
1163          * effic: Test whether the property is already set before trying to
1164          * convert to string. Alternatively, set from CRTerm directly rather
1165          * than converting to string.
1166          */
1167         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1168         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1169         sp_style_merge_property(style, prop_idx, str_value);
1170         g_free(str_value);
1171     }
1174 static void
1175 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1177 #if 0 /* forwards */
1178     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1179         CRDeclaration *decl = NULL;
1180         cr_prop_list_get_decl(cur, &decl);
1181         sp_style_merge_style_from_decl(style, decl);
1182     }
1183 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1184     if (props) {
1185         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1186         CRDeclaration *decl = NULL;
1187         cr_prop_list_get_decl(props, &decl);
1188         sp_style_merge_style_from_decl(style, decl);
1189     }
1190 #endif
1193 /**
1194  * \pre decl_list != NULL
1195  */
1196 static void
1197 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1199     // read the decls from end to start, using head recursion, so that latter declarations override
1200     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1201     // because sp_style_merge_style_from_decl only sets properties that are unset
1202     if (decl_list->next) {
1203         sp_style_merge_from_decl_list(style, decl_list->next);
1204     }
1205     sp_style_merge_style_from_decl(style, decl_list);
1208 static CRSelEng *
1209 sp_repr_sel_eng()
1211     CRSelEng *const ret = cr_sel_eng_new();
1212     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1214     /** \todo
1215      * Check whether we need to register any pseudo-class handlers.
1216      * libcroco has its own default handlers for first-child and lang.
1217      *
1218      * We probably want handlers for link and arguably visited (though
1219      * inkscape can't visit links at the time of writing).  hover etc.
1220      * more useful in inkview than the editor inkscape.
1221      *
1222      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1223      * the following should be honoured, at least by inkview:
1224      * :hover, :active, :focus, :visited, :link.
1225      */
1227     g_assert(ret);
1228     return ret;
1231 static void
1232 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1234     static CRSelEng *sel_eng = NULL;
1235     if (!sel_eng) {
1236         sel_eng = sp_repr_sel_eng();
1237     }
1239     CRPropList *props = NULL;
1240     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1241                                                                      object->document->style_cascade,
1242                                                                      object->repr,
1243                                                                      &props);
1244     g_return_if_fail(status == CR_OK);
1245     /// \todo Check what errors can occur, and handle them properly.
1246     if (props) {
1247         sp_style_merge_from_props(style, props);
1248         cr_prop_list_destroy(props);
1249     }
1252 /**
1253  * Parses a style="..." string and merges it with an existing SPStyle.
1254  */
1255 void
1256 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1258     /*
1259      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1260      * ``When CSS styling is used, CSS inline style is specified by including
1261      * semicolon-separated property declarations of the form "name : value"
1262      * within the style attribute''.
1263      *
1264      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1265      * Why does it say "including", what else is allowed in the style
1266      * attribute value?
1267      */
1269     CRDeclaration *const decl_list
1270         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1271     if (decl_list) {
1272         sp_style_merge_from_decl_list(style, decl_list);
1273         cr_declaration_destroy(decl_list);
1274     }
1277 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1278 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1280 static void
1281 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1283     /* 'font-size' */
1284     if (!child.set || child.inherit) {
1285         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1286         child.computed = parent.computed;
1287     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1288         /** \todo
1289          * fixme: SVG and CSS do not specify clearly, whether we should use
1290          * user or screen coordinates (Lauris)
1291          */
1292         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1293             child.computed = font_size_table[child.value];
1294         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1295             child.computed = parent.computed / 1.2;
1296         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1297             child.computed = parent.computed * 1.2;
1298         } else {
1299             /* Illegal value */
1300         }
1301     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1302         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1303          * rather than viewport. */
1304         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1305     }
1308 /**
1309  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1310  * calculating from) corresponding computed values of \a parent.
1311  *
1312  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1313  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1314  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1315  * about what the computed value is (not obvious for lengths).
1316  *
1317  * \pre \a parent's computed values are already up-to-date.
1318  */
1319 void
1320 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1322     g_return_if_fail(style != NULL);
1324     /** \todo
1325      * fixme: Check for existing callers that might pass null parent.
1326      * This should probably be g_return_if_fail, or else we should make a
1327      * best attempt to set computed values correctly without having a parent
1328      * (i.e., by assuming parent has initial values).
1329      */
1330     if (!parent)
1331         return;
1333     /* CSS2 */
1334     /* Font */
1335     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1337     /* 'font-style' */
1338     if (!style->font_style.set || style->font_style.inherit) {
1339         style->font_style.computed = parent->font_style.computed;
1340     }
1342     /* 'font-variant' */
1343     if (!style->font_variant.set || style->font_variant.inherit) {
1344         style->font_variant.computed = parent->font_variant.computed;
1345     }
1347     /* 'font-weight' */
1348     if (!style->font_weight.set || style->font_weight.inherit) {
1349         style->font_weight.computed = parent->font_weight.computed;
1350     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1351         /** \todo
1352          * fixme: This is unconditional, i.e., happens even if parent not
1353          * present.
1354          */
1355         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1356     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1357         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1358     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1359         unsigned const parent_val = parent->font_weight.computed;
1360         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1361         // strictly, 'bolder' and 'lighter' should go to the next weight
1362         // expressible in the current font family, but that's difficult to
1363         // find out, so jumping by 3 seems an appropriate approximation
1364         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1365                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1366                                        : parent_val - 3);
1367         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1368     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1369         unsigned const parent_val = parent->font_weight.computed;
1370         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1371         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1372                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1373                                        : parent_val + 3);
1374         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1375     }
1377     /* 'font-stretch' */
1378     if (!style->font_stretch.set || style->font_stretch.inherit) {
1379         style->font_stretch.computed = parent->font_stretch.computed;
1380     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1381         unsigned const parent_val = parent->font_stretch.computed;
1382         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1383                                         ? parent_val
1384                                         : parent_val - 1);
1385         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1386     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1387         unsigned const parent_val = parent->font_stretch.computed;
1388         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1389         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1390                                         ? parent_val
1391                                         : parent_val + 1);
1392         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1393     }
1395     /* text (css2) */
1396     if (!style->text_indent.set || style->text_indent.inherit) {
1397         style->text_indent.computed = parent->text_indent.computed;
1398     }
1400     if (!style->text_align.set || style->text_align.inherit) {
1401         style->text_align.computed = parent->text_align.computed;
1402     }
1404     if (!style->text_decoration.set || style->text_decoration.inherit) {
1405         style->text_decoration.underline = parent->text_decoration.underline;
1406         style->text_decoration.overline = parent->text_decoration.overline;
1407         style->text_decoration.line_through = parent->text_decoration.line_through;
1408         style->text_decoration.blink = parent->text_decoration.blink;
1409     }
1411     if (!style->line_height.set || style->line_height.inherit) {
1412         style->line_height.computed = parent->line_height.computed;
1413         style->line_height.normal = parent->line_height.normal;
1414     }
1416     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1417         style->letter_spacing.computed = parent->letter_spacing.computed;
1418         style->letter_spacing.normal = parent->letter_spacing.normal;
1419     }
1421     if (!style->word_spacing.set || style->word_spacing.inherit) {
1422         style->word_spacing.computed = parent->word_spacing.computed;
1423         style->word_spacing.normal = parent->word_spacing.normal;
1424     }
1426     if (!style->text_transform.set || style->text_transform.inherit) {
1427         style->text_transform.computed = parent->text_transform.computed;
1428     }
1430     if (!style->direction.set || style->direction.inherit) {
1431         style->direction.computed = parent->direction.computed;
1432     }
1434     if (!style->block_progression.set || style->block_progression.inherit) {
1435         style->block_progression.computed = parent->block_progression.computed;
1436     }
1438     if (!style->writing_mode.set || style->writing_mode.inherit) {
1439         style->writing_mode.computed = parent->writing_mode.computed;
1440     }
1442     if (!style->text_anchor.set || style->text_anchor.inherit) {
1443         style->text_anchor.computed = parent->text_anchor.computed;
1444     }
1446     if (style->opacity.inherit) {
1447         style->opacity.value = parent->opacity.value;
1448     }
1450     /* Color */
1451     if (!style->color.set || style->color.inherit) {
1452         sp_style_merge_ipaint(style, &style->color, &parent->color);
1453     }
1455     /* Fill */
1456     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1457         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1458     }
1460     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1461         style->fill_opacity.value = parent->fill_opacity.value;
1462     }
1464     if (!style->fill_rule.set || style->fill_rule.inherit) {
1465         style->fill_rule.computed = parent->fill_rule.computed;
1466     }
1468     /* Stroke */
1469     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1470         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1471     }
1473     if (!style->stroke_width.set || style->stroke_width.inherit) {
1474         style->stroke_width.computed = parent->stroke_width.computed;
1475     } else {
1476         /* Update computed value for any change in font inherited from parent. */
1477         double const em = style->font_size.computed;
1478         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1479             style->stroke_width.computed = style->stroke_width.value * em;
1480         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1481             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1482             style->stroke_width.computed = style->stroke_width.value * ex;
1483         }
1484     }
1486     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1487         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1488     }
1490     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1491         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1492     }
1494     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1495         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1496     }
1498     if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1499         /** \todo
1500          * This code looks wrong.  Why does the logic differ from the
1501          * above properties? Similarly dashoffset below.
1502          */
1503         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1504         if (style->stroke_dash.n_dash > 0) {
1505             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1506             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1507         }
1508     }
1510     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1511         style->stroke_dash.offset = parent->stroke_dash.offset;
1512     }
1514     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1515         style->stroke_opacity.value = parent->stroke_opacity.value;
1516     }
1518     if (style->text && parent->text) {
1519         if (!style->text->font_family.set || style->text->font_family.inherit) {
1520             g_free(style->text->font_family.value);
1521             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1522         }
1523     }
1525     if (style->text && parent->text) {
1526         if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1527             g_free(style->text->font_specification.value);
1528             style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1529         }
1530     }
1532     /* Markers - Free the old value and make copy of the new */
1533     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1534         if (!style->marker[i].set || style->marker[i].inherit) {
1535             g_free(style->marker[i].value);
1536             style->marker[i].value = g_strdup(parent->marker[i].value);
1537         }
1538     }
1540     /* Filter effects */
1541     if (style->filter.inherit) {
1542         sp_style_merge_ifilter(style, &parent->filter);
1543     }
1545     if(style->enable_background.inherit) {
1546         style->enable_background.value = parent->enable_background.value;
1547     }
1550 template <typename T>
1551 static void
1552 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1554     if ( ( !(child.set) || child.inherit )
1555          && parent.set && !(parent.inherit) )
1556     {
1557         child = parent;
1558     }
1561 /**
1562  * Copy SPIString from parent to child.
1563  */
1564 static void
1565 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1567     if ( ( !(child.set) || child.inherit )
1568          && parent.set && !(parent.inherit) )
1569     {
1570         g_free(child.value);
1571         child.value = g_strdup(parent.value);
1572         child.set = parent.set;
1573         child.inherit = parent.inherit;
1574     }
1577 static void
1578 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1579                                             SPIPaint &child, SPIPaint const &parent)
1581     /** \todo
1582      * I haven't given this much attention.  See comments below about
1583      * currentColor, colorProfile, and relative URIs.
1584      */
1585     if (!child.set || child.inherit) {
1586         sp_style_merge_ipaint(style, &child, &parent);
1587         child.set = parent.set;
1588         child.inherit = parent.inherit;
1589     }
1592 static void
1593 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1594                                                unsigned const max_computed_val,
1595                                                unsigned const smaller_val)
1597     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1598        then zero or more other absolute values, then smaller_val then larger_val. */
1599     unsigned const min_computed_val = 0;
1600     unsigned const larger_val = smaller_val + 1;
1601     g_return_if_fail(min_computed_val < max_computed_val);
1602     g_return_if_fail(max_computed_val < smaller_val);
1604     if (parent.set && !parent.inherit) {
1605         if (!child.set || child.inherit) {
1606             child.value = parent.value;
1607             child.set = parent.set;  // i.e. true
1608             child.inherit = parent.inherit;  // i.e. false
1609         } else if (child.value < smaller_val) {
1610             /* Child has absolute value: leave as is. */
1611         } else if ( ( child.value == smaller_val
1612                       && parent.value == larger_val )
1613                     || ( parent.value == smaller_val
1614                          && child.value == larger_val ) )
1615         {
1616             child.set = false;
1617             /*
1618              * Note that this can result in a change in computed value in the
1619              * rare case that the parent's setting was a no-op (i.e. if the
1620              * parent's parent's computed value was already ultra-condensed or
1621              * ultra-expanded).  However, I'd guess that the change is for the
1622              * better: I'd guess that if the properties were specified
1623              * relatively, then the intent is to counteract parent's effect.
1624              * I.e. I believe this is the best code even in that case.
1625              */
1626         } else if (child.value == parent.value) {
1627             /* Leave as is. */
1628             /** \todo
1629              * It's unclear what to do if style and parent specify the same
1630              * relative directive (narrower or wider).  We can either convert
1631              * to absolute specification or coalesce to a single relative
1632              * request (of half the strength of the original pair).
1633              *
1634              * Converting to a single level of relative specification is a
1635              * better choice if the newly-unlinked clone is itself cloned to
1636              * other contexts (inheriting different font stretchiness): it
1637              * would preserve the effect that it will be narrower than
1638              * the inherited context in each case.  The disadvantage is that
1639              * it will ~certainly affect the computed value of the
1640              * newly-unlinked clone.
1641              */
1642         } else {
1643             unsigned const parent_val = parent.computed;
1644             child.value = ( child.value == smaller_val
1645                             ? ( parent_val == min_computed_val
1646                                 ? parent_val
1647                                 : parent_val - 1 )
1648                             : ( parent_val == max_computed_val
1649                                 ? parent_val
1650                                 : parent_val + 1 ) );
1651             g_assert(child.value <= max_computed_val);
1652             child.inherit = false;
1653             g_assert(child.set);
1654         }
1655     }
1658 template <typename LengthT>
1659 static void
1660 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1661                                              double const parent_child_em_ratio)
1663     if ( ( !(child.set) || child.inherit )
1664          && parent.set && !(parent.inherit) )
1665     {
1666         child = parent;
1667         switch (parent.unit) {
1668             case SP_CSS_UNIT_EM:
1669             case SP_CSS_UNIT_EX:
1670                 child.value *= parent_child_em_ratio;
1671                 /** \todo
1672                  * fixme: Have separate ex ratio parameter.
1673                  * Get x height from libnrtype or pango.
1674                  */
1675                 if (!IS_FINITE(child.value)) {
1676                     child.value = child.computed;
1677                     child.unit = SP_CSS_UNIT_NONE;
1678                 }
1679                 break;
1681             default:
1682                 break;
1683         }
1684     }
1687 static double
1688 get_relative_font_size_frac(SPIFontSize const &font_size)
1690     switch (font_size.type) {
1691         case SP_FONT_SIZE_LITERAL: {
1692             switch (font_size.value) {
1693                 case SP_CSS_FONT_SIZE_SMALLER:
1694                     return 5.0 / 6.0;
1696                 case SP_CSS_FONT_SIZE_LARGER:
1697                     return 6.0 / 5.0;
1699                 default:
1700                     g_assert_not_reached();
1701             }
1702         }
1704         case SP_FONT_SIZE_PERCENTAGE:
1705             return SP_F8_16_TO_FLOAT(font_size.value);
1707         case SP_FONT_SIZE_LENGTH:
1708             g_assert_not_reached();
1709     }
1710     g_assert_not_reached();
1713 /**
1714  * Combine \a style and \a parent style specifications into a single style specification that
1715  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1716  *
1717  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1718  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1719  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1720  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1721  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1722  *
1723  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1724  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1725  * style was previously up-to-date wrt \a parent.
1726  *
1727  * \pre \a parent's computed values are already up-to-date.
1728  *   (\a style's computed values needn't be up-to-date.)
1729  */
1730 void
1731 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1733     /** \note
1734      * The general rule for each property is as follows:
1735      *
1736      *   If style is set to an absolute value, then leave it as is.
1737      *
1738      *   Otherwise (i.e. if style has a relative value):
1739      *
1740      *     If parent is set to an absolute value, then set style to the computed value.
1741      *
1742      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1743      */
1745     /* We do font-size first, to ensure that em size is up-to-date. */
1746     /** \todo
1747      * fixme: We'll need to have more font-related things up the top once
1748      * we're getting x-height from pango or libnrtype.
1749      */
1751     /* Some things that allow relative specifications. */
1752     {
1753         /* font-size.  Note that we update the computed font-size of style,
1754            to assist in em calculations later in this function. */
1755         if (parent->font_size.set && !parent->font_size.inherit) {
1756             if (!style->font_size.set || style->font_size.inherit) {
1757                 /* font_size inherits the computed value, so we can use the parent value
1758                  * verbatim. */
1759                 style->font_size = parent->font_size;
1760             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1761                 /* Child already has absolute size (stored in computed value), so do nothing. */
1762             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1763                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1764                 /* Child already has absolute size, but we ensure that the computed value
1765                    is up-to-date. */
1766                 unsigned const ix = style->font_size.value;
1767                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1768                 style->font_size.computed = font_size_table[ix];
1769             } else {
1770                 /* Child has relative size. */
1771                 double const child_frac(get_relative_font_size_frac(style->font_size));
1772                 style->font_size.set = true;
1773                 style->font_size.inherit = false;
1774                 style->font_size.computed = parent->font_size.computed * child_frac;
1776                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1777                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1778                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1779                 {
1780                     /* Absolute value. */
1781                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1782                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1783                 } else {
1784                     /* Relative value. */
1785                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1786                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1787                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1788                 }
1789             }
1790         }
1792         /* 'font-stretch' */
1793         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1794                                                        parent->font_stretch,
1795                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1796                                                        SP_CSS_FONT_STRETCH_NARROWER);
1798         /* font-weight */
1799         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1800                                                        parent->font_weight,
1801                                                        SP_CSS_FONT_WEIGHT_900,
1802                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1803     }
1806     /* Enum values that don't have any relative settings (other than `inherit'). */
1807     {
1808         SPIEnum SPStyle::*const fields[] = {
1809             //nyi: SPStyle::clip_rule,
1810             //nyi: SPStyle::color_interpolation,
1811             //nyi: SPStyle::color_interpolation_filters,
1812             //nyi: SPStyle::color_rendering,
1813             &SPStyle::direction,
1814             &SPStyle::fill_rule,
1815             &SPStyle::font_style,
1816             &SPStyle::font_variant,
1817             //nyi: SPStyle::image_rendering,
1818             //nyi: SPStyle::pointer_events,
1819             //nyi: SPStyle::shape_rendering,
1820             &SPStyle::stroke_linecap,
1821             &SPStyle::stroke_linejoin,
1822             &SPStyle::text_anchor,
1823             //nyi: &SPStyle::text_rendering,
1824             &SPStyle::visibility,
1825             &SPStyle::writing_mode
1826         };
1828         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1829             SPIEnum SPStyle::*const fld = fields[i];
1830             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1831         }
1832     }
1834     /* A few other simple inheritance properties. */
1835     {
1836         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1837         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1838         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1840         /** \todo
1841          * We currently treat text-decoration as if it were a simple inherited
1842          * property (fixme). This code may need changing once we do the
1843          * special fill/stroke inheritance mentioned by the spec.
1844          */
1845         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1846                                                                  parent->text_decoration);
1848         //nyi: font-size-adjust,  // <number> | none | inherit
1849         //nyi: glyph-orientation-horizontal,
1850         //nyi: glyph-orientation-vertical,
1851     }
1853     /* Properties that involve length but are easy in other respects. */
1854     {
1855         /* The difficulty with lengths is that font-relative units need adjusting if the font
1856          * varies between parent & child.
1857          *
1858          * Lengths specified in the existing child can stay as they are: its computed font
1859          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1860          * size.
1861          *
1862          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1863          * ratio of em or ex size between parent & child.
1864          */
1865         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1867         SPILength SPStyle::*const lfields[] = {
1868             &SPStyle::stroke_width,
1869             &SPStyle::text_indent
1870         };
1871         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1872             SPILength SPStyle::*fld = lfields[i];
1873             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1874                                                                     parent->*fld,
1875                                                                     parent_child_em_ratio);
1876         }
1878         SPILengthOrNormal SPStyle::*const nfields[] = {
1879             &SPStyle::letter_spacing,
1880             &SPStyle::line_height,
1881             &SPStyle::word_spacing
1882         };
1883         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1884             SPILengthOrNormal SPStyle::*fld = nfields[i];
1885             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1886                                                                             parent->*fld,
1887                                                                             parent_child_em_ratio);
1888         }
1890         //nyi: &SPStyle::kerning: length or `auto'
1892         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1893            can accept units. */
1894     }
1896     /* Properties that involve a URI but are easy in other respects. */
1897     {
1898         /** \todo
1899          * Could cause problems if original object was in another document
1900          * and it used a relative URL.  (At the time of writing, we don't
1901          * allow hrefs to other documents, so this isn't a problem yet.)
1902          * Paint properties also allow URIs.
1903          */
1904         //nyi: cursor,   // may involve change in effect, but we can't do much better
1905         //nyi: color-profile,
1907         // Markers (marker-start, marker-mid, marker-end).
1908         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1909             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1910         }
1911     }
1913     /* CSS2 */
1914     /* Font */
1916     if (style->text && parent->text) {
1917         sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
1918                                                      parent->text->font_specification);
1919         
1920         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1921                                                      parent->text->font_family);
1922     }
1925     /* Properties that don't inherit by default.  Most of these need special handling. */
1926     {
1927         /*
1928          * opacity's effect is cumulative; we set the new value to the combined effect.  The
1929          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
1930          * fill-opacity are quite different from opacity, and don't need any special handling.)
1931          *
1932          * Cases:
1933          * - parent & child were each previously unset, in which case the effective
1934          *   opacity value is 1.0, and style should remain unset.
1935          * - parent was previously unset (so computed opacity value of 1.0)
1936          *   and child was set to inherit.  The merged child should
1937          *   get a value of 1.0, and shouldn't inherit (lest the new parent
1938          *   has a different opacity value).  Given that opacity's default
1939          *   value is 1.0 (rather than inherit), we might as well have the
1940          *   merged child's opacity be unset.
1941          * - parent was previously unset (so opacity 1.0), and child was set to a number.
1942          *   The merged child should retain its existing settings (though it doesn't matter
1943          *   if we make it unset if that number was 1.0).
1944          * - parent was inherit and child was unset.  Merged child should be set to inherit.
1945          * - parent was inherit and child was inherit.  (We can't in general reproduce this
1946          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
1947          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1948          *   child's value should be set to the product of the two, i.e. the square of the
1949          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
1950          *   is more important to retain the effective opacity than to retain the inheriting
1951          *   effect, and assumes that the inheriting effect either isn't important enough to create
1952          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
1953          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1954          *   closer to maintaining the effect.
1955          * - parent was inherit and child was set to a numerical value.  If the child's value
1956          *   was 1.0, then the merged child should have the same settings as the parent.
1957          *   If the child's value was 0, then the merged child should also be set to 0.
1958          *   If the child's value was anything else, then we do the same as for the inherit/inherit
1959          *   case above: have the merged child set to the product of the two opacities and not
1960          *   marked as inherit, for the same reasons as for that case.
1961          * - parent was set to a value, and child was unset.  The merged child should have
1962          *   parent's settings.
1963          * - parent was set to a value, and child was inherit.  The merged child should
1964          *   be set to the product, i.e. the square of the parent's value.
1965          * - parent & child are each set to a value.  The merged child should be set to the
1966          *   product.
1967          */
1968         if ( !style->opacity.set
1969              || ( !style->opacity.inherit
1970                   && style->opacity.value == SP_SCALE24_MAX ) )
1971         {
1972             style->opacity = parent->opacity;
1973         } else {
1974             /* Ensure that style's computed value is up-to-date. */
1975             if (style->opacity.inherit) {
1976                 style->opacity.value = parent->opacity.value;
1977             }
1979             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1980             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1981                                                   parent->opacity.value);
1983             style->opacity.inherit = (parent->opacity.inherit
1984                                       && style->opacity.inherit
1985                                       && (parent->opacity.value == 0 ||
1986                                           parent->opacity.value == SP_SCALE24_MAX));
1987             style->opacity.set = ( style->opacity.inherit
1988                                    || style->opacity.value < SP_SCALE24_MAX );
1989         }
1991         /* display is in principle similar to opacity, but implementation is easier. */
1992         if ( parent->display.set && !parent->display.inherit
1993              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1994             style->display.value = SP_CSS_DISPLAY_NONE;
1995             style->display.set = true;
1996             style->display.inherit = false;
1997         } else if (style->display.inherit) {
1998             style->display.value = parent->display.value;
1999             style->display.set = parent->display.set;
2000             style->display.inherit = parent->display.inherit;
2001         } else {
2002             /* Leave as is.  (display doesn't inherit by default.) */
2003         }
2005         /* enable-background - this is rather complicated, because
2006          * it is valid only when applied to container elements.
2007          * Let's check a simple case anyhow. */
2008         if (parent->enable_background.set
2009             && !parent->enable_background.inherit
2010             && style->enable_background.inherit)
2011         {
2012             style->enable_background.set = true;
2013             style->enable_background.inherit = false;
2014             style->enable_background.value = parent->enable_background.value;
2015         }
2017         if (!style->filter.set || style->filter.inherit)
2018         {
2019             sp_style_merge_ifilter(style, &parent->filter);
2020         }
2022         /** \todo
2023          * fixme: Check that we correctly handle all properties that don't
2024          * inherit by default (as shown in
2025          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2026          */
2027     }
2029     /* SPIPaint properties (including color). */
2030     {
2031         /** \todo
2032          * Think about the issues involved if specified as currentColor or
2033          * if specified relative to colorProfile, and if the currentColor or
2034          * colorProfile differs between parent \& child.  See also comments
2035          * elsewhere in this function about URIs.
2036          */
2037         SPIPaint SPStyle::*const fields[] = {
2038             &SPStyle::color,
2039             &SPStyle::fill,
2040             &SPStyle::stroke
2041         };
2042         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2043             SPIPaint SPStyle::*const fld = fields[i];
2044             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2045         }
2046     }
2048     /* Things from SVG 1.2 or CSS3. */
2049     {
2050         /* Note: If we ever support setting string values for text-align then we'd need strdup
2051          * handling here. */
2052         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2054         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2055         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2056     }
2058     /* Note: this will need length handling once dasharray supports units. */
2059     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2060          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2061     {
2062         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2063         if (style->stroke_dash.n_dash > 0) {
2064             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2065             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2066         }
2067         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2068         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2069     }
2071     /* Note: this will need length handling once dasharray_offset supports units. */
2072     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
2073         style->stroke_dash.offset = parent->stroke_dash.offset;
2074         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2075         /* fixme: we don't currently allow stroke-dashoffset to be `inherit'.  TODO: Try to
2076          * represent it as a normal SPILength; though will need to do something about existing
2077          * users of stroke_dash.offset and stroke_dashoffset_set. */
2078     }
2082 static void
2083 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2085     // it may be that this style's SPIPaint has not yet created its URIReference;
2086     // now that we have a document, we can create it here
2087     if (!paint->value.href && document) {
2088         paint->value.href = new SPPaintServerReference(document);
2089         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));
2090     }
2092     if (paint->value.href && paint->value.href->getObject())
2093         paint->value.href->detach();
2095     if (paint->value.href) {
2096         try {
2097             paint->value.href->attach(*uri);
2098         } catch (Inkscape::BadURIException &e) {
2099             g_warning("%s", e.what());
2100             paint->value.href->detach();
2101         }
2102     }
2105 static void
2106 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2108     try {
2109         const Inkscape::URI IURI(uri);
2110         sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2111     } catch (...) {
2112         g_warning("URI failed to parse: %s", uri);
2113     }
2116 void
2117 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2119     sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2122 /**
2123  *
2124  */
2125 static void
2126 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2128     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2129         bool isset = paint->set;
2130         paint->clear();
2131         paint->set = isset;
2132         paint->currentcolor = TRUE;
2133         paint->setColor(style->color.value.color);
2134         return;
2135     }
2137     paint->clear();
2138     if ( parent->isPaintserver() ) {
2139         if (parent->value.href) {
2140             sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2141         } else {
2142             g_warning("Expected paint server not found.");
2143         }
2144     } else if ( parent->isColor() ) {
2145         paint->setColor( parent->value.color );
2146     } else if ( parent->isNoneSet() ) {
2147         paint->noneSet = TRUE;
2148     } else if ( parent->isNone() ) {
2149         //
2150     } else {
2151         g_assert_not_reached();
2152     }
2156 /**
2157  * Merge filter style from parent.
2158  * Filter effects do not inherit by default
2159  */
2160 static void
2161 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2163     // FIXME:
2164     // instead of just copying over, we need to _really merge_ the two filters by combining their
2165     // filter primitives
2167     sp_style_filter_clear(style);
2168     style->filter.set = parent->set;
2169     style->filter.inherit = parent->inherit;
2171     if (style->filter.href && style->filter.href->getObject())
2172        style->filter.href->detach();
2174     // it may be that this style has not yet created its SPFilterReference
2175     if (!style->filter.href && style->object && SP_OBJECT_DOCUMENT(style->object)) {
2176             style->filter.href = new SPFilterReference(SP_OBJECT_DOCUMENT(style->object));
2177             style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2178     }
2180     if (style->filter.href && parent->href) {
2181         try {
2182             style->filter.href->attach(*parent->href->getURI());
2183         } catch (Inkscape::BadURIException &e) {
2184             g_warning("%s", e.what());
2185             style->filter.href->detach();
2186         }
2187     }
2190 /**
2191  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2192  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2193  * complete cascaded style to style_clipboard. When you need a CSS string
2194  * for an object in the document tree, you normally call
2195  * sp_style_write_difference instead to take into account the object's parent.
2196  *
2197  * \pre style != NULL.
2198  * \pre flags in {IFSET, ALWAYS}.
2199  * \post ret != NULL.
2200  */
2201 gchar *
2202 sp_style_write_string(SPStyle const *const style, guint const flags)
2204     /** \todo
2205      * Merge with write_difference, much duplicate code!
2206      */
2207     g_return_val_if_fail(style != NULL, NULL);
2208     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2209                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2210                          NULL);
2212     gchar c[BMAX];
2213     gchar *p = c;
2214     *p = '\0';
2216     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2217     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2218     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2219     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2220     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2222     /* Text */
2223     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2224     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2225     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2226     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2227     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2228     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2229     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2230     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2231     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2232     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2234     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2236     /// \todo fixme: Per type methods need default flag too (lauris)
2237     p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2238     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2240     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2241     // if fill:none, skip writing fill properties
2242     if (!style->fill.noneSet) {
2243         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2244         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2245     }
2247     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2249     // stroke width affects markers, so write it if there's stroke OR any markers
2250     if (!style->stroke.noneSet || 
2251         style->marker[SP_MARKER_LOC].set ||
2252         style->marker[SP_MARKER_LOC_START].set ||
2253         style->marker[SP_MARKER_LOC_MID].set || 
2254         style->marker[SP_MARKER_LOC_END].set) {
2255         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2256     }
2258     // if stroke:none, skip writing stroke properties
2259     if (!style->stroke.noneSet) {
2260         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2261         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2262         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2263         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2265         /** \todo fixme: */
2266         if ((flags == SP_STYLE_FLAG_ALWAYS)
2267             || style->stroke_dasharray_set)
2268         {
2269             if (style->stroke_dasharray_inherit) {
2270                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2271             } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2272                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2273                 gint i;
2274                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2275                     Inkscape::CSSOStringStream os;
2276                     if (i) {
2277                         os << ", ";
2278                     }
2279                     os << style->stroke_dash.dash[i];
2280                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2281                 }
2282                 if (p < c + BMAX) {
2283                     *p++ = ';';
2284                 }
2285             } else {
2286                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2287             }
2288         }
2290         /** \todo fixme: */
2291         if (style->stroke_dashoffset_set) {
2292             Inkscape::CSSOStringStream os;
2293             os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2294             p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2295         } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2296             p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2297         }
2298     }
2300     if (style->marker[SP_MARKER_LOC].set) {
2301         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2302     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2303         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2304     }
2305     if (style->marker[SP_MARKER_LOC_START].set) {
2306         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2307     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2308         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2309     }
2310     if (style->marker[SP_MARKER_LOC_MID].set) {
2311         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2312     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2313         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2314     }
2315     if (style->marker[SP_MARKER_LOC_END].set) {
2316         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2317     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2318         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2319     }
2321     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2322     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2323     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2325     /* filter: */
2326     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2328     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2330     /* fixme: */
2331     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2333     /* Get rid of trailing `;'. */
2334     if (p != c) {
2335         --p;
2336         if (*p == ';') {
2337             *p = '\0';
2338         }
2339     }
2341     return g_strdup(c);
2345 #define STYLE_BUF_MAX
2348 /**
2349  * Dumps style to CSS string, see sp_style_write_string()
2350  *
2351  * \pre from != NULL.
2352  * \pre to != NULL.
2353  * \post ret != NULL.
2354  */
2355 gchar *
2356 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2358     g_return_val_if_fail(from != NULL, NULL);
2359     g_return_val_if_fail(to != NULL, NULL);
2361     gchar c[BMAX], *p = c;
2362     *p = '\0';
2364     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2365     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2366     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2367     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2368     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2370     /* Text */
2371     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2372     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2373     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2374     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2375     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2376     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2377     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2378     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2379     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2380     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2382     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2384     /// \todo fixme: Per type methods need default flag too
2385     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2386         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2387     }
2388     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2390     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2391     // if fill:none, skip writing fill properties
2392     if (!from->fill.noneSet) {
2393         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2394         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2395     }
2397     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2399     // stroke width affects markers, so write it if there's stroke OR any markers
2400     if (!from->stroke.noneSet || 
2401         from->marker[SP_MARKER_LOC].set ||
2402         from->marker[SP_MARKER_LOC_START].set ||
2403         from->marker[SP_MARKER_LOC_MID].set || 
2404         from->marker[SP_MARKER_LOC_END].set) {
2405         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2406     }
2408     // if stroke:none, skip writing stroke properties
2409     if (!from->stroke.noneSet) {
2410         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2411                                   &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2412         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2413                                   &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2414         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2415                                    &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2416         /** \todo fixme: */
2417         if (from->stroke_dasharray_set) {
2418             if (from->stroke_dasharray_inherit) {
2419                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2420             } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2421                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2422                 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2423                     Inkscape::CSSOStringStream os;
2424                     if (i) {
2425                         os << ", ";
2426                     }
2427                     os << from->stroke_dash.dash[i];
2428                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2429                 }
2430                 p += g_snprintf(p, c + BMAX - p, ";");
2431             }
2432         }
2433         /* fixme: */
2434         if (from->stroke_dashoffset_set) {
2435             Inkscape::CSSOStringStream os;
2436             os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2437             p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2438         }
2439         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2440     }
2442     /* markers */
2443     if (from->marker[SP_MARKER_LOC].value != NULL) {
2444         p += g_snprintf(p, c + BMAX - p, "marker:%s;",       from->marker[SP_MARKER_LOC].value);
2445     }
2446     if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2447         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2448     }
2449     if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2450         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2451     }
2452     if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2453         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2454     }
2456     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2457     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2458     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2460     /* filter: */
2461     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2463     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2465     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2467     /** \todo
2468      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2469      * flag is mainly only for attributes that don't handle explicit unset well.
2470      * We may need to revisit the behaviour of this routine.
2471      */
2473     /* Get rid of trailing `;'. */
2474     if (p != c) {
2475         --p;
2476         if (*p == ';') {
2477             *p = '\0';
2478         }
2479     }
2481     return g_strdup(c);
2486 /**
2487  * Reset all style properties.
2488  */
2489 static void
2490 sp_style_clear(SPStyle *style)
2492     g_return_if_fail(style != NULL);
2494     style->fill.clear();
2495     style->stroke.clear();
2496     sp_style_filter_clear(style);
2498     if (style->fill.value.href) {
2499         delete style->fill.value.href;
2500         style->fill.value.href = NULL;
2501     }
2502     if (style->stroke.value.href) {
2503         delete style->stroke.value.href;
2504         style->stroke.value.href = NULL;
2505     }
2506     if (style->filter.href) {
2507         delete style->filter.href;
2508         style->filter.href = NULL;
2509     }
2511     if (style->stroke_dash.dash) {
2512         g_free(style->stroke_dash.dash);
2513     }
2515     /** \todo fixme: Do that text manipulation via parents */
2516     SPObject *object = style->object;
2517     SPDocument *document = style->document;
2518     gint const refcount = style->refcount;
2519     SPTextStyle *text = style->text;
2520     unsigned const text_private = style->text_private;
2522     memset(style, 0, sizeof(SPStyle));
2524     style->refcount = refcount;
2525     style->object = object;
2526     style->document = document;
2528     if (document) {
2529         style->filter.href = new SPFilterReference(document);
2530         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2532         style->fill.value.href = new SPPaintServerReference(document);
2533         style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2535         style->stroke.value.href = new SPPaintServerReference(document);
2536         style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2537     }
2539     style->text = text;
2540     style->text_private = text_private;
2542     style->text->font_specification.set = FALSE;
2543     style->text->font.set = FALSE;
2544     style->text->font_family.set = FALSE;
2546     style->font_size.set = FALSE;
2547     style->font_size.type = SP_FONT_SIZE_LITERAL;
2548     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2549     style->font_size.computed = 12.0;
2550     style->font_style.set = FALSE;
2551     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2552     style->font_variant.set = FALSE;
2553     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2554     style->font_weight.set = FALSE;
2555     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2556     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2557     style->font_stretch.set = FALSE;
2558     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2560     /* text */
2561     style->text_indent.set = FALSE;
2562     style->text_indent.unit = SP_CSS_UNIT_NONE;
2563     style->text_indent.computed = 0.0;
2565     style->text_align.set = FALSE;
2566     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2568     style->text_decoration.set = FALSE;
2569     style->text_decoration.underline = FALSE;
2570     style->text_decoration.overline = FALSE;
2571     style->text_decoration.line_through = FALSE;
2572     style->text_decoration.blink = FALSE;
2574     style->line_height.set = FALSE;
2575     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2576     style->line_height.normal = TRUE;
2577     style->line_height.value = style->line_height.computed = 1.0;
2579     style->letter_spacing.set = FALSE;
2580     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2581     style->letter_spacing.normal = TRUE;
2582     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2584     style->word_spacing.set = FALSE;
2585     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2586     style->word_spacing.normal = TRUE;
2587     style->word_spacing.value = style->word_spacing.computed = 0.0;
2589     style->text_transform.set = FALSE;
2590     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2592     style->direction.set = FALSE;
2593     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2595     style->block_progression.set = FALSE;
2596     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2598     style->writing_mode.set = FALSE;
2599     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2602     style->text_anchor.set = FALSE;
2603     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2606     style->opacity.value = SP_SCALE24_MAX;
2607     style->visibility.set = FALSE;
2608     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2609     style->display.set = FALSE;
2610     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2611     style->overflow.set = FALSE;
2612     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2614     style->color.clear();
2615     style->color.setColor(0.0, 0.0, 0.0);
2617     style->fill.clear();
2618     style->fill.setColor(0.0, 0.0, 0.0);
2619     style->fill_opacity.value = SP_SCALE24_MAX;
2620     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2622     style->stroke.clear();
2623     style->stroke_opacity.value = SP_SCALE24_MAX;
2625     style->stroke_width.set = FALSE;
2626     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2627     style->stroke_width.computed = 1.0;
2629     style->stroke_linecap.set = FALSE;
2630     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2631     style->stroke_linejoin.set = FALSE;
2632     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2634     style->stroke_miterlimit.set = FALSE;
2635     style->stroke_miterlimit.value = 4.0;
2637     style->stroke_dash.n_dash = 0;
2638     style->stroke_dash.dash = NULL;
2639     style->stroke_dash.offset = 0.0;
2641     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2642         g_free(style->marker[i].value);
2643         style->marker[i].set = FALSE;
2644     }
2646     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2647     style->enable_background.set = false;
2648     style->enable_background.inherit = false;
2653 /**
2654  *
2655  */
2656 static void
2657 sp_style_read_dash(SPStyle *style, gchar const *str)
2659     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2660     style->stroke_dasharray_set = TRUE;
2662     if (strcmp(str, "inherit") == 0) {
2663         style->stroke_dasharray_inherit = true;
2664         return;
2665     }
2666     style->stroke_dasharray_inherit = false;
2668     NRVpathDash &dash = style->stroke_dash;
2669     g_free(dash.dash);
2670     dash.dash = NULL;
2672     if (strcmp(str, "none") == 0) {
2673         dash.n_dash = 0;
2674         return;
2675     }
2677     gint n_dash = 0;
2678     gdouble d[64];
2679     gchar *e = NULL;
2681     bool LineSolid=true;
2682     while (e != str && n_dash < 64) {
2683         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2684         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2685         if (d[n_dash] > 0.00000001)
2686             LineSolid = false;
2687         if (e != str) {
2688             n_dash += 1;
2689             str = e;
2690         }
2691         while (str && *str && !isalnum(*str)) str += 1;
2692     }
2694     if (LineSolid) {
2695         dash.n_dash = 0;
2696         return;
2697     }
2699     if (n_dash > 0) {
2700         dash.dash = g_new(gdouble, n_dash);
2701         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2702         dash.n_dash = n_dash;
2703     }
2707 /*#########################
2708 ## SPTextStyle operations
2709 #########################*/
2712 /**
2713  * Return new SPTextStyle object with default settings.
2714  */
2715 static SPTextStyle *
2716 sp_text_style_new()
2718     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2719     ts->refcount = 1;
2720     sp_text_style_clear(ts);
2722     ts->font_specification.value = g_strdup("Bitstream Vera Sans");
2723     ts->font.value = g_strdup("Bitstream Vera Sans");
2724     ts->font_family.value = g_strdup("Bitstream Vera Sans");
2726     return ts;
2730 /**
2731  * Clear text style settings.
2732  */
2733 static void
2734 sp_text_style_clear(SPTextStyle *ts)
2736     ts->font_specification.set = FALSE;
2737     ts->font.set = FALSE;
2738     ts->font_family.set = FALSE;
2743 /**
2744  * Reduce refcount of text style and possibly free it.
2745  */
2746 static SPTextStyle *
2747 sp_text_style_unref(SPTextStyle *st)
2749     st->refcount -= 1;
2751     if (st->refcount < 1) {
2752         g_free(st->font_specification.value);
2753         g_free(st->font.value);
2754         g_free(st->font_family.value);
2755         g_free(st);
2756     }
2758     return NULL;
2763 /**
2764  * Return duplicate of text style.
2765  */
2766 static SPTextStyle *
2767 sp_text_style_duplicate_unset(SPTextStyle *st)
2769     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2770     nt->refcount = 1;
2772     nt->font_specification.value = g_strdup(st->font_specification.value);
2773     nt->font.value = g_strdup(st->font.value);
2774     nt->font_family.value = g_strdup(st->font_family.value);
2776     return nt;
2781 /**
2782  * Write SPTextStyle object into string.
2783  */
2784 static guint
2785 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2787     gint d = 0;
2789     // We do not do diffing for text style
2790     if (flags == SP_STYLE_FLAG_IFDIFF)
2791         flags = SP_STYLE_FLAG_IFSET;
2793     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2794     d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2795     return d;
2800 /* The following sp_tyle_read_* functions ignore invalid values, as per
2801  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2802  *
2803  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2804  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2805  * pairs, in which case SVG's error-handling rules
2806  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2807  */
2810 /**
2811  * Set SPIFloat object from string.
2812  */
2813 static void
2814 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2816     if (!strcmp(str, "inherit")) {
2817         val->set = TRUE;
2818         val->inherit = TRUE;
2819     } else {
2820         gfloat value;
2821         if (sp_svg_number_read_f(str, &value)) {
2822             val->set = TRUE;
2823             val->inherit = FALSE;
2824             val->value = value;
2825         }
2826     }
2831 /**
2832  * Set SPIScale24 object from string.
2833  */
2834 static void
2835 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2837     if (!strcmp(str, "inherit")) {
2838         val->set = TRUE;
2839         val->inherit = TRUE;
2840     } else {
2841         gfloat value;
2842         if (sp_svg_number_read_f(str, &value)) {
2843             val->set = TRUE;
2844             val->inherit = FALSE;
2845             value = CLAMP(value, 0.0, 1.0);
2846             val->value = SP_SCALE24_FROM_FLOAT(value);
2847         }
2848     }
2851 /**
2852  * Reads a style value and performs lookup based on the given style value enumerations.
2853  */
2854 static void
2855 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2856                     bool const can_explicitly_inherit)
2858     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2859         val->set = TRUE;
2860         val->inherit = TRUE;
2861     } else {
2862         for (unsigned i = 0; dict[i].key; i++) {
2863             if (!strcmp(str, dict[i].key)) {
2864                 val->set = TRUE;
2865                 val->inherit = FALSE;
2866                 val->value = dict[i].value;
2867                 /* Save copying for values not needing it */
2868                 val->computed = val->value;
2869                 break;
2870             }
2871         }
2872     }
2877 /**
2878  * Set SPIString object from string.
2879  */
2880 static void
2881 sp_style_read_istring(SPIString *val, gchar const *str)
2883     g_free(val->value);
2885     if (!strcmp(str, "inherit")) {
2886         val->set = TRUE;
2887         val->inherit = TRUE;
2888         val->value = NULL;
2889     } else {
2890         val->set = TRUE;
2891         val->inherit = FALSE;
2892         val->value = g_strdup(str);
2893     }
2898 /**
2899  * Set SPILength object from string.
2900  */
2901 static void
2902 sp_style_read_ilength(SPILength *val, gchar const *str)
2904     if (!strcmp(str, "inherit")) {
2905         val->set = TRUE;
2906         val->inherit = TRUE;
2907     } else {
2908         gdouble value;
2909         gchar *e;
2910         /** \todo fixme: Move this to standard place (Lauris) */
2911         value = g_ascii_strtod(str, &e);
2912         if ((gchar const *) e != str) {
2913             /** \todo
2914              * Allow the number of px per inch to vary (document preferences,
2915              * X server or whatever).  E.g. don't fill in computed here, do
2916              * it at the same time as percentage units are done.
2917              */
2918             if (!*e) {
2919                 /* Userspace */
2920                 val->unit = SP_CSS_UNIT_NONE;
2921                 val->computed = value;
2922             } else if (!strcmp(e, "px")) {
2923                 /* Userspace */
2924                 val->unit = SP_CSS_UNIT_PX;
2925                 val->computed = value;
2926             } else if (!strcmp(e, "pt")) {
2927                 /* Userspace / DEVICESCALE */
2928                 val->unit = SP_CSS_UNIT_PT;
2929                 val->computed = value * PX_PER_PT;
2930             } else if (!strcmp(e, "pc")) {
2931                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2932                 val->unit = SP_CSS_UNIT_PC;
2933                 val->computed = value * PX_PER_PT * 12;
2934             } else if (!strcmp(e, "mm")) {
2935                 val->unit = SP_CSS_UNIT_MM;
2936                 val->computed = value * PX_PER_MM;
2937             } else if (!strcmp(e, "cm")) {
2938                 val->unit = SP_CSS_UNIT_CM;
2939                 val->computed = value * PX_PER_CM;
2940             } else if (!strcmp(e, "in")) {
2941                 val->unit = SP_CSS_UNIT_IN;
2942                 val->computed = value * PX_PER_IN;
2943             } else if (!strcmp(e, "em")) {
2944                 /* EM square */
2945                 val->unit = SP_CSS_UNIT_EM;
2946                 val->value = value;
2947             } else if (!strcmp(e, "ex")) {
2948                 /* ex square */
2949                 val->unit = SP_CSS_UNIT_EX;
2950                 val->value = value;
2951             } else if (!strcmp(e, "%")) {
2952                 /* Percentage */
2953                 val->unit = SP_CSS_UNIT_PERCENT;
2954                 val->value = value * 0.01;
2955             } else {
2956                 /* Invalid */
2957                 return;
2958             }
2959             val->set = TRUE;
2960             val->inherit = FALSE;
2961         }
2962     }
2965 /**
2966  * Set SPILengthOrNormal object from string.
2967  */
2968 static void
2969 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2971     if (!strcmp(str, "normal")) {
2972         val->set = TRUE;
2973         val->inherit = FALSE;
2974         val->normal = TRUE;
2975         val->unit = SP_CSS_UNIT_NONE;
2976         val->value = val->computed = 0.0;
2977     } else {
2978         SPILength length;
2979         sp_style_read_ilength(&length, str);
2980         val->set = length.set;
2981         val->inherit = length.inherit;
2982         val->normal = FALSE;
2983         val->unit = length.unit;
2984         val->value = length.value;
2985         val->computed = length.computed;
2986     }
2989 /**
2990  * Set SPITextDecoration object from string.
2991  */
2992 static void
2993 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2995     if (!strcmp(str, "inherit")) {
2996         val->set = TRUE;
2997         val->inherit = TRUE;
2998     } else if (!strcmp(str, "none")) {
2999         val->set = TRUE;
3000         val->inherit = FALSE;
3001         val->underline = FALSE;
3002         val->overline = FALSE;
3003         val->line_through = FALSE;
3004         val->blink = FALSE;
3005     } else {
3006         bool found_underline = false;
3007         bool found_overline = false;
3008         bool found_line_through = false;
3009         bool found_blink = false;
3010         for ( ; *str ; str++ ) {
3011             if (*str == ' ') continue;
3012             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
3013                 found_underline = true;
3014                 str += 9;
3015             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
3016                 found_overline = true;
3017                 str += 8;
3018             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
3019                 found_line_through = true;
3020                 str += 12;
3021             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
3022                 found_blink = true;
3023                 str += 5;
3024             } else {
3025                 return;  // invalid value
3026             }
3027         }
3028         if (!(found_underline || found_overline || found_line_through || found_blink)) {
3029             return;  // invalid value: empty
3030         }
3031         val->set = TRUE;
3032         val->inherit = FALSE;
3033         val->underline = found_underline;
3034         val->overline = found_overline;
3035         val->line_through = found_line_through;
3036         val->blink = found_blink;
3037     }
3040 /**
3041  * Set SPIPaint object from string containing an integer value.
3042  * \param document Ignored
3043  */
3044 static void
3045 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3047     (void)style; // TODO
3048     (void)document; // TODO
3049     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
3050     if (!strcmp(str, "inherit")) {
3051         paint->set = TRUE;
3052         paint->inherit = TRUE;
3053     } else {
3054         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
3055         if (rgb0 != 0xff) {
3056             paint->setColor(rgb0);
3057             paint->set = TRUE;
3058             paint->inherit = FALSE;
3059         }
3060     }
3064 /**
3065  * Set SPIPaint object from string.
3066  *
3067  * \pre paint == \&style.fill || paint == \&style.stroke.
3068  */
3069 static void
3070 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3072     while (g_ascii_isspace(*str)) {
3073         ++str;
3074     }
3076     paint->clear();
3078     if (streq(str, "inherit")) {
3079         paint->set = TRUE;
3080         paint->inherit = TRUE;
3081     } else {
3082         if ( strneq(str, "url", 3) ) {
3083             gchar *uri = extract_uri( str, &str );
3084             while ( g_ascii_isspace(*str) ) {
3085                 ++str;
3086             }
3087             // TODO check on and comment the comparrison "paint != &style->color".
3088             if ( uri && *uri && (paint != &style->color) ) {
3089                 paint->set = TRUE;
3091                 // it may be that this style's SPIPaint has not yet created its URIReference;
3092                 // now that we have a document, we can create it here
3093                 if (!paint->value.href && document) {
3094                     paint->value.href = new SPPaintServerReference(document);
3095                     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));
3096                 }
3098                 // TODO check what this does in light of move away from union
3099                 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3100             }
3101             g_free( uri );
3102         }
3104         if (streq(str, "currentColor") && paint != &style->color) {
3105             paint->set = TRUE;
3106             paint->currentcolor = TRUE;
3107         } else if (streq(str, "none") && paint != &style->color) {
3108             paint->set = TRUE;
3109             paint->noneSet = TRUE;
3110         } else {
3111             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3112             if (rgb0 != 0xff) {
3113                 paint->setColor( rgb0 );
3114                 paint->set = TRUE;
3116                 while (g_ascii_isspace(*str)) {
3117                     ++str;
3118                 }
3119                 if (strneq(str, "icc-color(", 10)) {
3120                     SVGICCColor* tmp = new SVGICCColor();
3121                     if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3122                         delete tmp;
3123                         tmp = 0;
3124                     }
3125                     paint->value.color.icc = tmp;
3126                 }
3127             }
3128         }
3129     }
3134 /**
3135  * Set SPIFontSize object from string.
3136  */
3137 static void
3138 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3140     if (!strcmp(str, "inherit")) {
3141         val->set = TRUE;
3142         val->inherit = TRUE;
3143     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3144         for (unsigned i = 0; enum_font_size[i].key; i++) {
3145             if (!strcmp(str, enum_font_size[i].key)) {
3146                 val->set = TRUE;
3147                 val->inherit = FALSE;
3148                 val->type = SP_FONT_SIZE_LITERAL;
3149                 val->value = enum_font_size[i].value;
3150                 return;
3151             }
3152         }
3153         /* Invalid */
3154         return;
3155     } else {
3156         gdouble value;
3157         gchar *e;
3158         /* fixme: Move this to standard place (Lauris) */
3159         value = g_ascii_strtod(str, &e);
3160         if ((gchar const *) e != str) {
3161             if (!*e) {
3162                 /* Userspace */
3163             } else if (!strcmp(e, "px")) {
3164                 /* Userspace */
3165             } else if (!strcmp(e, "pt")) {
3166                 /* Userspace * DEVICESCALE */
3167                 value *= PX_PER_PT;
3168             } else if (!strcmp(e, "pc")) {
3169                 /* 12pt */
3170                 value *= PX_PER_PT * 12.0;
3171             } else if (!strcmp(e, "mm")) {
3172                 value *= PX_PER_MM;
3173             } else if (!strcmp(e, "cm")) {
3174                 value *= PX_PER_CM;
3175             } else if (!strcmp(e, "in")) {
3176                 value *= PX_PER_IN;
3177             } else if (!strcmp(e, "%")) {
3178                 /* Percentage */
3179                 val->set = TRUE;
3180                 val->inherit = FALSE;
3181                 val->type = SP_FONT_SIZE_PERCENTAGE;
3182                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3183                 return;
3184             } else {
3185                 /* Invalid */
3186                 return;
3187             }
3188             /* Length */
3189             val->set = TRUE;
3190             val->inherit = FALSE;
3191             val->type = SP_FONT_SIZE_LENGTH;
3192             val->computed = value;
3193             return;
3194         }
3195     }
3200 /**
3201  * Set SPIFilter object from string.
3202  */
3203 static void
3204 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3206     SPIFilter *f = &(style->filter);
3207     /* Try all possible values: inherit, none, uri */
3208     if (streq(str, "inherit")) {
3209         f->set = TRUE;
3210         f->inherit = TRUE;
3211         if (f->href && f->href->getObject())
3212             f->href->detach(); 
3213     } else if(streq(str, "none")) {
3214         f->set = TRUE;
3215         f->inherit = FALSE;
3216         if (f->href && f->href->getObject())
3217            f->href->detach(); 
3218     } else if (strneq(str, "url", 3)) {
3219         char *uri = extract_uri(str);
3220         if(uri == NULL || uri[0] == '\0') {
3221             g_warning("Specified filter url is empty");
3222             f->set = TRUE;
3223             f->inherit = FALSE;
3224             return;
3225         }
3226         f->set = TRUE;
3227         f->inherit = FALSE;
3228         if (f->href && f->href->getObject())
3229             f->href->detach();
3231         // it may be that this style has not yet created its SPFilterReference;
3232         // now that we have a document, we can create it here
3233         if (!f->href && document) {
3234             f->href = new SPFilterReference(document);
3235             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3236         }
3238         try {
3239             f->href->attach(Inkscape::URI(uri));
3240         } catch (Inkscape::BadURIException &e) {
3241             g_warning("%s", e.what());
3242             f->href->detach();
3243         }
3244         g_free (uri);
3246     } else {
3247         /* We shouldn't reach this if SVG input is well-formed */
3248         f->set = FALSE;
3249         f->inherit = FALSE;
3250         if (f->href && f->href->getObject())
3251             f->href->detach(); 
3252     }
3255 /**
3256  * Set SPIEnum object from repr attribute.
3257  */
3258 static void
3259 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3260                     gchar const *key, SPStyleEnum const *dict,
3261                     bool const can_explicitly_inherit)
3263     gchar const *str = repr->attribute(key);
3264     if (str) {
3265         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3266     }
3271 /**
3272  * Set SPILength object from repr attribute.
3273  */
3274 static void
3275 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3277     gchar const *str = repr->attribute(key);
3278     if (str) {
3279         sp_style_read_ilength(val, str);
3280     }
3283 /**
3284  * Set SPIFontSize object from repr attribute.
3285  */
3286 static void
3287 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3289     gchar const *str = repr->attribute(key);
3290     if (str) {
3291         sp_style_read_ifontsize(val, str);
3292     }
3296 /**
3297  * Set SPIFloat object from repr attribute.
3298  */
3299 static void
3300 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3302     gchar const *str = repr->attribute(key);
3303     if (str) {
3304         sp_style_read_ifloat(val, str);
3305     }
3309 /**
3310  * Write SPIFloat object into string.
3311  */
3312 static gint
3313 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3314                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3316     Inkscape::CSSOStringStream os;
3318     if ((flags & SP_STYLE_FLAG_ALWAYS)
3319         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3320         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3321             && (!base->set || (val->value != base->value))))
3322     {
3323         if (val->inherit) {
3324             return g_snprintf(p, len, "%s:inherit;", key);
3325         } else {
3326             os << key << ":" << val->value << ";";
3327             return g_strlcpy(p, os.str().c_str(), len);
3328         }
3329     }
3330     return 0;
3334 /**
3335  * Write SPIScale24 object into string.
3336  */
3337 static gint
3338 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3339                         SPIScale24 const *const val, SPIScale24 const *const base,
3340                         guint const flags)
3342     Inkscape::CSSOStringStream os;
3344     if ((flags & SP_STYLE_FLAG_ALWAYS)
3345         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3346         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3347             && (!base->set || (val->value != base->value))))
3348     {
3349         if (val->inherit) {
3350             return g_snprintf(p, len, "%s:inherit;", key);
3351         } else {
3352             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3353             return g_strlcpy(p, os.str().c_str(), len);
3354         }
3355     }
3356     return 0;
3360 /**
3361  * Write SPIEnum object into string.
3362  */
3363 static gint
3364 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3365                      SPStyleEnum const *const dict,
3366                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3368     if ((flags & SP_STYLE_FLAG_ALWAYS)
3369         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3370         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3371             && (!base->set || (val->computed != base->computed))))
3372     {
3373         if (val->inherit) {
3374             return g_snprintf(p, len, "%s:inherit;", key);
3375         }
3376         for (unsigned i = 0; dict[i].key; i++) {
3377             if (dict[i].value == static_cast< gint > (val->value) ) {
3378                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3379             }
3380         }
3381     }
3382     return 0;
3387 /**
3388  * Write SPIString object into string.
3389  */
3390 static gint
3391 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3392                        SPIString const *const val, SPIString const *const base, guint const flags)
3394     if ((flags & SP_STYLE_FLAG_ALWAYS)
3395         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3396         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3397             && (!base->set || strcmp(val->value, base->value))))
3398     {
3399         if (val->inherit) {
3400             return g_snprintf(p, len, "%s:inherit;", key);
3401         } else {
3402             gchar *val_quoted = css2_escape_quote(val->value);
3403             if (val_quoted) {
3404                 return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3405                 g_free (val_quoted);
3406             }
3407         }
3408     }
3409     return 0;
3413 /**
3414  *
3415  */
3416 static bool
3417 sp_length_differ(SPILength const *const a, SPILength const *const b)
3419     if (a->unit != b->unit) {
3420         if (a->unit == SP_CSS_UNIT_EM) return true;
3421         if (a->unit == SP_CSS_UNIT_EX) return true;
3422         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3423         if (b->unit == SP_CSS_UNIT_EM) return true;
3424         if (b->unit == SP_CSS_UNIT_EX) return true;
3425         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3426     }
3428     return (a->computed != b->computed);
3433 /**
3434  * Write SPILength object into string.
3435  */
3436 static gint
3437 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3438                        SPILength const *const val, SPILength const *const base, guint const flags)
3440     Inkscape::CSSOStringStream os;
3442     if ((flags & SP_STYLE_FLAG_ALWAYS)
3443         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3444         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3445             && (!base->set || sp_length_differ(val, base))))
3446     {
3447         if (val->inherit) {
3448             return g_snprintf(p, len, "%s:inherit;", key);
3449         } else {
3450             switch (val->unit) {
3451                 case SP_CSS_UNIT_NONE:
3452                     os << key << ":" << val->computed << ";";
3453                     return g_strlcpy(p, os.str().c_str(), len);
3454                     break;
3455                 case SP_CSS_UNIT_PX:
3456                     os << key << ":" << val->computed << "px;";
3457                     return g_strlcpy(p, os.str().c_str(), len);
3458                     break;
3459                 case SP_CSS_UNIT_PT:
3460                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3461                     return g_strlcpy(p, os.str().c_str(), len);
3462                     break;
3463                 case SP_CSS_UNIT_PC:
3464                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3465                     return g_strlcpy(p, os.str().c_str(), len);
3466                     break;
3467                 case SP_CSS_UNIT_MM:
3468                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3469                     return g_strlcpy(p, os.str().c_str(), len);
3470                     break;
3471                 case SP_CSS_UNIT_CM:
3472                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3473                     return g_strlcpy(p, os.str().c_str(), len);
3474                     break;
3475                 case SP_CSS_UNIT_IN:
3476                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3477                     return g_strlcpy(p, os.str().c_str(), len);
3478                     break;
3479                 case SP_CSS_UNIT_EM:
3480                     os << key << ":" << val->value << "em;";
3481                     return g_strlcpy(p, os.str().c_str(), len);
3482                     break;
3483                 case SP_CSS_UNIT_EX:
3484                     os << key << ":" << val->value << "ex;";
3485                     return g_strlcpy(p, os.str().c_str(), len);
3486                     break;
3487                 case SP_CSS_UNIT_PERCENT:
3488                     os << key << ":" << (val->value * 100.0) << "%;";
3489                     return g_strlcpy(p, os.str().c_str(), len);
3490                     break;
3491                 default:
3492                     /* Invalid */
3493                     break;
3494             }
3495         }
3496     }
3497     return 0;
3501 /**
3502  *
3503  */
3504 static bool
3505 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3507     if (a->normal != b->normal) return true;
3508     if (a->normal) return false;
3510     if (a->unit != b->unit) {
3511         if (a->unit == SP_CSS_UNIT_EM) return true;
3512         if (a->unit == SP_CSS_UNIT_EX) return true;
3513         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3514         if (b->unit == SP_CSS_UNIT_EM) return true;
3515         if (b->unit == SP_CSS_UNIT_EX) return true;
3516         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3517     }
3519     return (a->computed != b->computed);
3522 /**
3523  * Write SPILengthOrNormal object into string.
3524  */
3525 static gint
3526 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3527                                SPILengthOrNormal const *const val,
3528                                SPILengthOrNormal const *const base,
3529                                guint const flags)
3531     if ((flags & SP_STYLE_FLAG_ALWAYS)
3532         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3533         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3534             && (!base->set || sp_lengthornormal_differ(val, base))))
3535     {
3536         if (val->normal) {
3537             return g_snprintf(p, len, "%s:normal;", key);
3538         } else {
3539             SPILength length;
3540             length.set = val->set;
3541             length.inherit = val->inherit;
3542             length.unit = val->unit;
3543             length.value = val->value;
3544             length.computed = val->computed;
3545             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3546         }
3547     }
3548     return 0;
3551 /**
3552  *
3553  */
3554 static bool
3555 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3557     return    a->underline != b->underline
3558            || a->overline != b->overline
3559            || a->line_through != b->line_through
3560            || a->blink != b->blink;
3563 /**
3564  * Write SPITextDecoration object into string.
3565  */
3566 static gint
3567 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3568                                SPITextDecoration const *const val,
3569                                SPITextDecoration const *const base,
3570                                guint const flags)
3572     Inkscape::CSSOStringStream os;
3574     if ((flags & SP_STYLE_FLAG_ALWAYS)
3575         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3576         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3577             && (!base->set || sp_textdecoration_differ(val, base))))
3578     {
3579         if (val->inherit) {
3580             return g_snprintf(p, len, "%s:inherit;", key);
3581         } else {
3582             os << key << ":";
3583             if (val->underline || val->overline || val->line_through || val->blink) {
3584                 if (val->underline) os << " underline";
3585                 if (val->overline) os << " overline";
3586                 if (val->line_through) os << " line-through";
3587                 if (val->blink) os << " blink";
3588             } else
3589                 os << "none";
3590             os << ";";
3591             return g_strlcpy(p, os.str().c_str(), len);
3592         }
3593     }
3594     return 0;
3597 /**
3598  *
3599  */
3600 static bool
3601 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3603     if ( (a->isColor() != b->isColor())
3604          || (a->isPaintserver() != b->isPaintserver())
3605          || (a->set != b->set)
3606          || (a->currentcolor != b->currentcolor)
3607          || (a->inherit!= b->inherit) ) {
3608         return true;
3609     }
3611     // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3613     if ( a->isPaintserver() ) {
3614         return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3615     }
3617     if ( a->isColor() ) {
3618         return !( (a->value.color == b->value.color)
3619                  && ((a->value.color.icc == b->value.color.icc)
3620                      || (a->value.color.icc && b->value.color.icc
3621                          && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3622                          && (a->value.color.icc->colors == b->value.color.icc->colors))));
3623     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3624      * in the string representation. */
3625     }
3627     return false;
3632 /**
3633  * Write SPIPaint object into string.
3634  */
3635 static gint
3636 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3637                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3639     int retval = 0;
3641     if ((flags & SP_STYLE_FLAG_ALWAYS)
3642         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3643         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3644             && (!base->set || sp_paint_differ(paint, base))))
3645     {
3646         CSSOStringStream css;
3648         if (paint->inherit) {
3649             css << "inherit";
3650         } else {
3651             if ( paint->value.href && paint->value.href->getURI() ) {
3652                 const gchar* uri = paint->value.href->getURI()->toString();
3653                 css << "url(" << uri << ")";
3654             }
3656             if ( paint->noneSet ) {
3657                 if ( !css.str().empty() ) {
3658                     css << " ";
3659                 }
3660                 css << "none";
3661             }
3663             if ( paint->currentcolor ) {
3664                 if ( !css.str().empty() ) {
3665                     css << " ";
3666                 }
3667                 css << "currentColor";
3668             }
3670             if ( paint->colorSet && !paint->currentcolor ) {
3671                 if ( !css.str().empty() ) {
3672                     css << " ";
3673                 }
3674                 char color_buf[8];
3675                 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3676                 css << color_buf;
3677             }
3679             if (paint->value.color.icc && !paint->currentcolor) {
3680                 if ( !css.str().empty() ) {
3681                     css << " ";
3682                 }
3683                 css << "icc-color(" << paint->value.color.icc->colorProfile;
3684                 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3685                          iEnd(paint->value.color.icc->colors.end());
3686                      i != iEnd; ++i) {
3687                     css << ", " << *i;
3688                 }
3689                 css << ')';
3690             }
3691         }
3693         if ( !css.str().empty() ) {
3694             retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3695         }
3696     }
3698     return retval;
3702 /**
3703  *
3704  */
3705 static bool
3706 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3708     if (a->type != b->type)
3709         return true;
3710     if (a->type == SP_FONT_SIZE_LENGTH) {
3711         if (a->computed != b->computed)
3712             return true;
3713     } else {
3714         if (a->value != b->value)
3715             return true;
3716     }
3717     return false;
3721 /**
3722  * Write SPIFontSize object into string.
3723  */
3724 static gint
3725 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3726                          SPIFontSize const *const val, SPIFontSize const *const base,
3727                          guint const flags)
3729     if ((flags & SP_STYLE_FLAG_ALWAYS)
3730         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3731         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3732             && (!base->set || sp_fontsize_differ(val, base))))
3733     {
3734         if (val->inherit) {
3735             return g_snprintf(p, len, "%s:inherit;", key);
3736         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3737             for (unsigned i = 0; enum_font_size[i].key; i++) {
3738                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3739                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3740                 }
3741             }
3742         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3743             Inkscape::CSSOStringStream os;
3744             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3745             return g_strlcpy(p, os.str().c_str(), len);
3746         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3747             Inkscape::CSSOStringStream os;
3748             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3749             return g_strlcpy(p, os.str().c_str(), len);
3750         }
3751     }
3752     return 0;
3756 /**
3757  * Write SPIFilter object into string.
3758  */
3759 static gint
3760 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3761                          SPIFilter const *const val, SPIFilter const *const base,
3762                          guint const flags)
3764     (void)base; // TODO
3765     if ((flags & SP_STYLE_FLAG_ALWAYS)
3766         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3767         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3768     {
3769         if (val->inherit) {
3770             return g_snprintf(p, len, "%s:inherit;", key);
3771         } else if (val->href && val->href->getURI()) {
3772             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
3773         }
3774     }
3777     return 0;
3781 void SPIPaint::clear()
3783     set = false;
3784     inherit = false;
3785     currentcolor = false;
3786     colorSet = false;
3787     noneSet = false;
3788     value.color.set( 0 );
3789     if ( value.href && value.href->getObject() )
3790     {
3791         value.href->detach();
3792     }
3796 /**
3797  * Clear filter object, and disconnect style from paintserver (if present).
3798  */
3799 static void
3800 sp_style_filter_clear(SPStyle *style)
3802     if (style->filter.href && style->filter.href->getObject())
3803         style->filter.href->detach();
3807 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3809 void
3810 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3812     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3814     if (repr == NULL) return;
3816     g_return_if_fail(linked != NULL);
3818     gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3820     SPCSSAttr *css = sp_repr_css_attr_new();
3821     sp_repr_css_set_property(css, property, val);
3822     g_free(val);
3823     if (recursive) {
3824         sp_repr_css_change_recursive(repr, css, "style");
3825     } else {
3826         sp_repr_css_change(repr, css, "style");
3827     }
3828     sp_repr_css_attr_unref(css);
3832 /**
3833  * Clear all style property attributes in object.
3834  */
3835 void
3836 sp_style_unset_property_attrs(SPObject *o)
3838     if (!o) return;
3840     SPStyle *style = SP_OBJECT_STYLE(o);
3841     if (!style) return;
3843     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3844     if (!repr) return;
3846     if (style->opacity.set) {
3847         repr->setAttribute("opacity", NULL);
3848     }
3849     if (style->color.set) {
3850         repr->setAttribute("color", NULL);
3851     }
3852     if (style->fill.set) {
3853         repr->setAttribute("fill", NULL);
3854     }
3855     if (style->fill_opacity.set) {
3856         repr->setAttribute("fill-opacity", NULL);
3857     }
3858     if (style->fill_rule.set) {
3859         repr->setAttribute("fill-rule", NULL);
3860     }
3861     if (style->stroke.set) {
3862         repr->setAttribute("stroke", NULL);
3863     }
3864     if (style->stroke_width.set) {
3865         repr->setAttribute("stroke-width", NULL);
3866     }
3867     if (style->stroke_linecap.set) {
3868         repr->setAttribute("stroke-linecap", NULL);
3869     }
3870     if (style->stroke_linejoin.set) {
3871         repr->setAttribute("stroke-linejoin", NULL);
3872     }
3873     if (style->marker[SP_MARKER_LOC].set) {
3874         repr->setAttribute("marker", NULL);
3875     }
3876     if (style->marker[SP_MARKER_LOC_START].set) {
3877         repr->setAttribute("marker-start", NULL);
3878     }
3879     if (style->marker[SP_MARKER_LOC_MID].set) {
3880         repr->setAttribute("marker-mid", NULL);
3881     }
3882     if (style->marker[SP_MARKER_LOC_END].set) {
3883         repr->setAttribute("marker-end", NULL);
3884     }
3885     if (style->stroke_opacity.set) {
3886         repr->setAttribute("stroke-opacity", NULL);
3887     }
3888     if (style->stroke_dasharray_set) {
3889         repr->setAttribute("stroke-dasharray", NULL);
3890     }
3891     if (style->stroke_dashoffset_set) {
3892         repr->setAttribute("stroke-dashoffset", NULL);
3893     }
3894     if (style->text_private && style->text->font_specification.set) {
3895         repr->setAttribute("-inkscape-font-specification", NULL);
3896     }
3897     if (style->text_private && style->text->font_family.set) {
3898         repr->setAttribute("font-family", NULL);
3899     }
3900     if (style->text_anchor.set) {
3901         repr->setAttribute("text-anchor", NULL);
3902     }
3903     if (style->writing_mode.set) {
3904         repr->setAttribute("writing_mode", NULL);
3905     }
3906     if (style->filter.set) {
3907         repr->setAttribute("filter", NULL);
3908     }
3909     if (style->enable_background.set) {
3910         repr->setAttribute("enable-background", NULL);
3911     }
3914 /**
3915  * \pre style != NULL.
3916  * \pre flags in {IFSET, ALWAYS}.
3917  */
3918 SPCSSAttr *
3919 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3921     g_return_val_if_fail(style != NULL, NULL);
3922     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3923                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3924                          NULL);
3925     gchar *style_str = sp_style_write_string(style, flags);
3926     SPCSSAttr *css = sp_repr_css_attr_new();
3927     sp_repr_css_attr_add_from_string(css, style_str);
3928     g_free(style_str);
3929     return css;
3933 /**
3934  * \pre object != NULL
3935  * \pre flags in {IFSET, ALWAYS}.
3936  */
3937 SPCSSAttr *
3938 sp_css_attr_from_object(SPObject *object, guint const flags)
3940     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3941                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3942                          NULL);
3943     SPStyle const *const style = SP_OBJECT_STYLE(object);
3944     if (style == NULL)
3945         return NULL;
3946     return sp_css_attr_from_style(style, flags);
3949 /**
3950  * Unset any text-related properties
3951  */
3952 SPCSSAttr *
3953 sp_css_attr_unset_text(SPCSSAttr *css)
3955     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3956     sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
3957     sp_repr_css_set_property(css, "font-size", NULL);
3958     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3959     sp_repr_css_set_property(css, "font-style", NULL);
3960     sp_repr_css_set_property(css, "font-variant", NULL);
3961     sp_repr_css_set_property(css, "font-weight", NULL);
3962     sp_repr_css_set_property(css, "font-stretch", NULL);
3963     sp_repr_css_set_property(css, "font-family", NULL);
3964     sp_repr_css_set_property(css, "text-indent", NULL);
3965     sp_repr_css_set_property(css, "text-align", NULL);
3966     sp_repr_css_set_property(css, "text-decoration", NULL);
3967     sp_repr_css_set_property(css, "line-height", NULL);
3968     sp_repr_css_set_property(css, "letter-spacing", NULL);
3969     sp_repr_css_set_property(css, "word-spacing", NULL);
3970     sp_repr_css_set_property(css, "text-transform", NULL);
3971     sp_repr_css_set_property(css, "direction", NULL);
3972     sp_repr_css_set_property(css, "block-progression", NULL);
3973     sp_repr_css_set_property(css, "writing-mode", NULL);
3974     sp_repr_css_set_property(css, "text-anchor", NULL);
3975     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3976     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3977     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3978     sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3980     return css;
3983 bool
3984 is_url(char const *p)
3986     if (p == NULL)
3987         return false;
3988 /** \todo
3989  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3990  * in property values must start with 'url('.
3991  */
3992     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3995 /**
3996  * Unset any properties that contain URI values.
3997  *
3998  * Used for storing style that will be reused across documents when carrying
3999  * the referenced defs is impractical.
4000  */
4001 SPCSSAttr *
4002 sp_css_attr_unset_uris(SPCSSAttr *css)
4004 // All properties that may hold <uri> or <paint> according to SVG 1.1
4005     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
4006     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
4007     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
4008     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
4009     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
4010     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
4011     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
4012     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
4013     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
4014     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
4016     return css;
4019 /**
4020  * Scale a single-value property.
4021  */
4022 void
4023 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
4024                                   double ex, bool only_with_units = false)
4026     gchar const *w = sp_repr_css_property(css, property, NULL);
4027     if (w) {
4028         gchar *units = NULL;
4029         double wd = g_ascii_strtod(w, &units) * ex;
4030         if (w == units) {// nothing converted, non-numeric value
4031             return;
4032         }
4033         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
4034             // only_with_units, but no units found, so do nothing.
4035             return;
4036         }
4037         Inkscape::CSSOStringStream os;
4038         os << wd << units; // reattach units
4039         sp_repr_css_set_property(css, property, os.str().c_str());
4040     }
4043 /**
4044  * Scale a list-of-values property.
4045  */
4046 void
4047 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
4049     gchar const *string = sp_repr_css_property(css, property, NULL);
4050     if (string) {
4051         Inkscape::CSSOStringStream os;
4052         gchar **a = g_strsplit(string, ",", 10000);
4053         bool first = true;
4054         for (gchar **i = a; i != NULL; i++) {
4055             gchar *w = *i;
4056             if (w == NULL)
4057                 break;
4058             gchar *units = NULL;
4059             double wd = g_ascii_strtod(w, &units) * ex;
4060             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4061                 g_strfreev(a);
4062                 return;
4063             }
4064             if (!first) {
4065                 os << ",";
4066             }
4067             os << wd << units; // reattach units
4068             first = false;
4069         }
4070         sp_repr_css_set_property(css, property, os.str().c_str());
4071         g_strfreev(a);
4072     }
4075 /**
4076  * Scale any properties that may hold <length> by ex.
4077  */
4078 SPCSSAttr *
4079 sp_css_attr_scale(SPCSSAttr *css, double ex)
4081     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4082     sp_css_attr_scale_property_single(css, "stroke-width", ex);
4083     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
4084     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4085     sp_css_attr_scale_property_single(css, "font-size", ex);
4086     sp_css_attr_scale_property_single(css, "kerning", ex);
4087     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4088     sp_css_attr_scale_property_single(css, "word-spacing", ex);
4089     sp_css_attr_scale_property_single(css, "line-height", ex, true);
4091     return css;
4095 /**
4096  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4097  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4098  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4099  * it parsed for us. So this function is only used to remove those quotes and for
4100  * presentation attributes, without any unescaping. (XML unescaping
4101  * (&amp; etc) is done by XML parser.)
4102  */
4103 gchar *
4104 attribute_unquote(gchar const *val)
4106     if (val) {
4107         if (*val == '\'' || *val == '"') {
4108             int l = strlen(val);
4109             if (l >= 2) {
4110                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4111                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4112                     return (g_strndup (val+1, l-2));
4113                 }
4114             }
4115         }
4116     }
4118     return (val? g_strdup (val) : NULL);
4121 /**
4122  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4123  */
4124 gchar *
4125 css2_escape_quote(gchar const *val) {
4127     Glib::ustring t;
4128     bool quote = false;
4129     bool last_was_space = false;
4131     for (gchar const *i = val; *i; i++) {
4132         bool is_space = ( *i == ' ' );
4133         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4134             // ASCII alphanumeric, - and _ don't require quotes
4135             t.push_back(*i);
4136         } else if ( is_space && !last_was_space ) {
4137             // non-consecutive spaces don't require quotes
4138             t.push_back(*i);
4139         } else if (*i=='\'') {
4140             // single quotes require escaping and quotes
4141             t.push_back('\\');
4142             t.push_back(*i);
4143             quote = true;
4144         } else {
4145             // everything else requires quotes
4146             t.push_back(*i);
4147             quote = true;
4148         }
4149         if (i == val && !g_ascii_isalpha(*i)) {
4150             // a non-ASCII/non-alpha initial character requires quotes
4151             quote = true;
4152         }
4153         last_was_space = is_space;
4154     }
4156     if (last_was_space) {
4157         // a trailing space requires quotes
4158         quote = true;
4159     }
4161     if (quote) {
4162         // we use single quotes so the result can be stored in an XML
4163         // attribute without incurring un-aesthetic additional quoting
4164         // (our XML emitter always uses double quotes)
4165         t.insert(t.begin(), '\'');
4166         t.push_back('\'');
4167     }
4169     return (t.empty() ? NULL : g_strdup (t.c_str()));
4172 /*
4173   Local Variables:
4174   mode:c++
4175   c-file-style:"stroustrup"
4176   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4177   indent-tabs-mode:nil
4178   fill-column:99
4179   End:
4180 */
4181 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :