Code

Pot and Dutch translation update
[inkscape.git] / src / style.cpp
1 /** @file
2  * @brief SVG stylesheets implementation.
3  */
4 /* Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
7  *   bulia byak <buliabyak@users.sf.net>
8  *
9  * Copyright (C) 2001-2002 Lauris Kaplinski
10  * Copyright (C) 2001 Ximian, Inc.
11  * Copyright (C) 2005 Monash University
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include <cstring>
21 #include <string>
23 #include "libcroco/cr-sel-eng.h"
24 #include "xml/croco-node-iface.h"
26 #include "svg/svg.h"
27 #include "svg/svg-color.h"
28 #include "svg/svg-icc-color.h"
30 #include "display/canvas-bpath.h"
31 #include "attributes.h"
32 #include "document.h"
33 #include "extract-uri.h"
34 #include "uri-references.h"
35 #include "uri.h"
36 #include "sp-paint-server.h"
37 #include "streq.h"
38 #include "strneq.h"
39 #include "style.h"
40 #include "svg/css-ostringstream.h"
41 #include "xml/repr.h"
42 #include "xml/simple-document.h"
43 #include "unit-constants.h"
44 #include "2geom/isnan.h"
45 #include "macros.h"
46 #include "preferences.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_ifontsize(SPIFontSize *val, gchar const *str);
87 static void sp_style_read_ibaselineshift(SPIBaselineShift *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_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key);
94 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
96 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
97 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
98 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
99 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
100 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
101 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
102 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
103 static gint sp_style_write_ibaselineshift(gchar *p, gint len, gchar const *key, SPIBaselineShift const *val, SPIBaselineShift const *base, guint flags);
104 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);
105 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);
106 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
108 static void sp_style_filter_clear(SPStyle *style);
110 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
111 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
113 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
114 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
116 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
118 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
119 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
121 #define SPS_READ_IBASELINE_SHIFT_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ibaselineshift((v), (s));}
122 #define SPS_READ_PBASELINE_SHIFT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pbaselineshift((v), (r), (k));}
124 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
126 struct SPStyleEnum {
127     gchar const *key;
128     gint value;
129 };
131 static SPStyleEnum const enum_fill_rule[] = {
132     {"nonzero", SP_WIND_RULE_NONZERO},
133     {"evenodd", SP_WIND_RULE_EVENODD},
134     {NULL, -1}
135 };
137 static SPStyleEnum const enum_stroke_linecap[] = {
138     {"butt", SP_STROKE_LINECAP_BUTT},
139     {"round", SP_STROKE_LINECAP_ROUND},
140     {"square", SP_STROKE_LINECAP_SQUARE},
141     {NULL, -1}
142 };
144 static SPStyleEnum const enum_stroke_linejoin[] = {
145     {"miter", SP_STROKE_LINEJOIN_MITER},
146     {"round", SP_STROKE_LINEJOIN_ROUND},
147     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
148     {NULL, -1}
149 };
151 static SPStyleEnum const enum_font_style[] = {
152     {"normal", SP_CSS_FONT_STYLE_NORMAL},
153     {"italic", SP_CSS_FONT_STYLE_ITALIC},
154     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
155     {NULL, -1}
156 };
158 static SPStyleEnum const enum_font_size[] = {
159     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
160     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
161     {"small", SP_CSS_FONT_SIZE_SMALL},
162     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
163     {"large", SP_CSS_FONT_SIZE_LARGE},
164     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
165     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
166     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
167     {"larger", SP_CSS_FONT_SIZE_LARGER},
168     {NULL, -1}
169 };
171 static SPStyleEnum const enum_font_variant[] = {
172     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
173     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
174     {NULL, -1}
175 };
177 static SPStyleEnum const enum_font_weight[] = {
178     {"100", SP_CSS_FONT_WEIGHT_100},
179     {"200", SP_CSS_FONT_WEIGHT_200},
180     {"300", SP_CSS_FONT_WEIGHT_300},
181     {"400", SP_CSS_FONT_WEIGHT_400},
182     {"500", SP_CSS_FONT_WEIGHT_500},
183     {"600", SP_CSS_FONT_WEIGHT_600},
184     {"700", SP_CSS_FONT_WEIGHT_700},
185     {"800", SP_CSS_FONT_WEIGHT_800},
186     {"900", SP_CSS_FONT_WEIGHT_900},
187     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
188     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
189     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
190     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
191     {NULL, -1}
192 };
194 static SPStyleEnum const enum_font_stretch[] = {
195     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
196     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
197     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
198     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
199     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
200     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
201     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
202     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
203     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
204     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
205     {"wider", SP_CSS_FONT_STRETCH_WIDER},
206     {NULL, -1}
207 };
209 static SPStyleEnum const enum_text_align[] = {
210     {"start", SP_CSS_TEXT_ALIGN_START},
211     {"end", SP_CSS_TEXT_ALIGN_END},
212     {"left", SP_CSS_TEXT_ALIGN_LEFT},
213     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
214     {"center", SP_CSS_TEXT_ALIGN_CENTER},
215     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
216     {NULL, -1}
217 };
219 static SPStyleEnum const enum_text_transform[] = {
220     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
221     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
222     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
223     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
224     {NULL, -1}
225 };
227 static SPStyleEnum const enum_text_anchor[] = {
228     {"start", SP_CSS_TEXT_ANCHOR_START},
229     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
230     {"end", SP_CSS_TEXT_ANCHOR_END},
231     {NULL, -1}
232 };
234 static SPStyleEnum const enum_direction[] = {
235     {"ltr", SP_CSS_DIRECTION_LTR},
236     {"rtl", SP_CSS_DIRECTION_RTL},
237     {NULL, -1}
238 };
240 static SPStyleEnum const enum_block_progression[] = {
241     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
242     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
243     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
244     {NULL, -1}
245 };
247 static SPStyleEnum const enum_writing_mode[] = {
248     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
249      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
250      * value is lr-tb rather than lr.
251      *
252      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
253      * sharing enumerators for different strings may be a bug (once we support ecma script).
254      */
255     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
256     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
257     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
258     {"lr", SP_CSS_WRITING_MODE_LR_TB},
259     {"rl", SP_CSS_WRITING_MODE_RL_TB},
260     {"tb", SP_CSS_WRITING_MODE_TB_RL},
261     {NULL, -1}
262 };
264 static SPStyleEnum const enum_baseline_shift[] = {
265     {"baseline", SP_CSS_BASELINE_SHIFT_BASELINE},
266     {"sub",      SP_CSS_BASELINE_SHIFT_SUB},
267     {"super",    SP_CSS_BASELINE_SHIFT_SUPER},
268     {NULL, -1}
269 };
271 static SPStyleEnum const enum_visibility[] = {
272     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
273     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
274     {"visible", SP_CSS_VISIBILITY_VISIBLE},
275     {NULL, -1}
276 };
278 static SPStyleEnum const enum_overflow[] = {
279     {"visible", SP_CSS_OVERFLOW_VISIBLE},
280     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
281     {"scroll", SP_CSS_OVERFLOW_SCROLL},
282     {"auto", SP_CSS_OVERFLOW_AUTO},
283     {NULL, -1}
284 };
286 static SPStyleEnum const enum_display[] = {
287     {"none",      SP_CSS_DISPLAY_NONE},
288     {"inline",    SP_CSS_DISPLAY_INLINE},
289     {"block",     SP_CSS_DISPLAY_BLOCK},
290     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
291     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
292     {"compact",   SP_CSS_DISPLAY_COMPACT},
293     {"marker",    SP_CSS_DISPLAY_MARKER},
294     {"table",     SP_CSS_DISPLAY_TABLE},
295     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
296     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
297     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
298     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
299     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
300     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
301     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
302     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
303     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
304     {NULL, -1}
305 };
307 static SPStyleEnum const enum_shape_rendering[] = {
308     {"auto", 0},
309     {"optimizeSpeed", 0},
310     {"crispEdges", 0},
311     {"geometricPrecision", 0},
312     {NULL, -1}
313 };
315 static SPStyleEnum const enum_color_rendering[] = {
316     {"auto", 0},
317     {"optimizeSpeed", 0},
318     {"optimizeQuality", 0},
319     {NULL, -1}
320 };
322 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
324 static SPStyleEnum const enum_text_rendering[] = {
325     {"auto", 0},
326     {"optimizeSpeed", 0},
327     {"optimizeLegibility", 0},
328     {"geometricPrecision", 0},
329     {NULL, -1}
330 };
332 static SPStyleEnum const enum_enable_background[] = {
333     {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
334     {"new", SP_CSS_BACKGROUND_NEW},
335     {NULL, -1}
336 };
338 /**
339  * Release callback.
340  */
341 static void
342 sp_style_object_release(SPObject *object, SPStyle *style)
344     (void)object; // TODO
345     style->object = NULL;
348 /**
349  * Emit style modified signal on style's object if the filter changed.
350  */
351 static void
352 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
354     (void)flags; // TODO
355     SPFilter *filter=static_cast<SPFilter *>(obj);
356     if (style->getFilter() == filter)
357     {
358         if (style->object) {
359             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
360         }
361     }
364 /**
365  * Gets called when the filter is (re)attached to the style
366  */
367 static void
368 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
370     if (old_ref) {
371         style->filter_modified_connection.disconnect();
372     }
373     if ( SP_IS_FILTER(ref))
374     {
375         style->filter_modified_connection =
376            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
377     }
379     sp_style_filter_ref_modified(ref, 0, style);
382 /**
383  * Emit style modified signal on style's object if server is style's fill
384  * or stroke paint server.
385  */
386 static void
387 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
389     (void)flags; // TODO
390     SPPaintServer *server = static_cast<SPPaintServer *>(obj);
392     if ((style->fill.isPaintserver())
393         && style->getFillPaintServer() == server)
394     {
395         if (style->object) {
396             /** \todo
397              * fixme: I do not know, whether it is optimal - we are
398              * forcing reread of everything (Lauris)
399              */
400             /** \todo
401              * fixme: We have to use object_modified flag, because parent
402              * flag is only available downstreams.
403              */
404             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
405         }
406     } else if ((style->stroke.isPaintserver())
407         && style->getStrokePaintServer() == server)
408     {
409         if (style->object) {
410             /// \todo fixme:
411             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
412         }
413     } else if (server) {
414         g_assert_not_reached();
415     }
418 /**
419  * Gets called when the paintserver is (re)attached to the style
420  */
421 static void
422 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
424     if (old_ref) {
425         style->fill_ps_modified_connection.disconnect();
426     }
427     if (SP_IS_PAINT_SERVER(ref)) {
428         style->fill_ps_modified_connection =
429            ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
430     }
432     sp_style_paint_server_ref_modified(ref, 0, style);
435 /**
436  * Gets called when the paintserver is (re)attached to the style
437  */
438 static void
439 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
441     if (old_ref) {
442         style->stroke_ps_modified_connection.disconnect();
443     }
444     if (SP_IS_PAINT_SERVER(ref)) {
445         style->stroke_ps_modified_connection =
446           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
447     }
449     sp_style_paint_server_ref_modified(ref, 0, style);
452 /**
453  * Returns a new SPStyle object with settings as per sp_style_clear().
454  */
455 SPStyle *
456 sp_style_new(SPDocument *document)
458     SPStyle *const style = g_new0(SPStyle, 1);
460     style->refcount = 1;
461     style->object = NULL;
462     style->document = document;
463     style->text = sp_text_style_new();
464     style->text_private = TRUE;
466     sp_style_clear(style);
468     style->cloned = false;
470     new (&style->release_connection) sigc::connection();
471     new (&style->filter_modified_connection) sigc::connection();
472     new (&style->fill_ps_modified_connection) sigc::connection();
473     new (&style->stroke_ps_modified_connection) sigc::connection();
475     return style;
479 /**
480  * Creates a new SPStyle object, and attaches it to the specified SPObject.
481  */
482 SPStyle *
483 sp_style_new_from_object(SPObject *object)
485     g_return_val_if_fail(object != NULL, NULL);
486     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
488     SPStyle *style = sp_style_new( object->document );
489     style->object = object;
490     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
492     if (object && object->cloned) {
493         style->cloned = true;
494     }
496     return style;
500 /**
501  * Increase refcount of style.
502  */
503 SPStyle *
504 sp_style_ref(SPStyle *style)
506     g_return_val_if_fail(style != NULL, NULL);
507     g_return_val_if_fail(style->refcount > 0, NULL);
509     style->refcount += 1;
511     return style;
515 /**
516  * Decrease refcount of style with possible destruction.
517  */
518 SPStyle *
519 sp_style_unref(SPStyle *style)
521     g_return_val_if_fail(style != NULL, NULL);
522     g_return_val_if_fail(style->refcount > 0, NULL);
524     style->refcount -= 1;
526     if (style->refcount < 1) {
527         style->release_connection.disconnect();
528         style->release_connection.~connection();
529         if (style->text) sp_text_style_unref(style->text);
531          if (style->fill.value.href) {
532              style->fill_ps_modified_connection.disconnect();
533              delete style->fill.value.href;
534              style->fill.value.href = NULL;
535          }
536          if (style->stroke.value.href) {
537              style->stroke_ps_modified_connection.disconnect();
538              delete style->stroke.value.href;
539              style->stroke.value.href = NULL;
540          }
541          if (style->filter.href) {
542              style->filter_modified_connection.disconnect();
543              delete style->filter.href;
544              style->filter.href = NULL;
545          }
547         style->filter_modified_connection.~connection();
548         style->fill_ps_modified_connection.~connection();
549         style->stroke_ps_modified_connection.~connection();
551         style->fill.clear();
552         style->stroke.clear();
553         sp_style_filter_clear(style);
555         g_free(style->stroke_dash.dash);
556         g_free(style);
557     }
559     return NULL;
562 /**
563  *  Reads the various style parameters for an object from repr.
564  */
565 static void
566 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
568     g_assert(style != NULL);
569     g_assert(repr != NULL);
570     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
572     sp_style_clear(style);
574     if (object && object->cloned) {
575         style->cloned = true;
576     }
578     /* 1. Style attribute */
579     gchar const *val = repr->attribute("style");
580     if (val != NULL) {
581         sp_style_merge_from_style_string(style, val);
582     }
584     if (object) {
585         sp_style_merge_from_object_stylesheet(style, object);
586     } else {
587         /** \todo No stylesheet information. Find out under what circumstances
588          * this occurs, and handle accordingly.  (If we really wanted to, we
589          * could probably get stylesheets by going through repr->doc.)
590          */
591     }
593     /* 2. Presentation attributes */
594     /* CSS2 */
595     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
596     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
597     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
598     /* Font */
599     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
600     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
601     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
602     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
603     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
604     /* Text (css2 chapter 16) */
605     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
606     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
607     if (!style->text_decoration.set) {
608         val = repr->attribute("text-decoration");
609         if (val) {
610             sp_style_read_itextdecoration(&style->text_decoration, val);
611         }
612     }
613     if (!style->line_height.set) {
614         val = repr->attribute("line-height");
615         if (val) {
616             sp_style_read_ilengthornormal(&style->line_height, val);
617         }
618     }
619     if (!style->letter_spacing.set) {
620         val = repr->attribute("letter-spacing");
621         if (val) {
622             sp_style_read_ilengthornormal(&style->letter_spacing, val);
623         }
624     }
625     if (!style->word_spacing.set) {
626         val = repr->attribute("word-spacing");
627         if (val) {
628             sp_style_read_ilengthornormal(&style->word_spacing, val);
629         }
630     }
631     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
632     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
633     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
635     /* SVG */
636     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
637                             enum_writing_mode, true);
638     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
639                             enum_text_anchor, true);
640     SPS_READ_PBASELINE_SHIFT_IF_UNSET(&style->baseline_shift, repr, "baseline-shift");
642     /* opacity */
643     if (!style->opacity.set) {
644         val = repr->attribute("opacity");
645         if (val) {
646             sp_style_read_iscale24(&style->opacity, val);
647         }
648     }
649     /* color */
650     if (!style->color.set) {
651         val = repr->attribute("color");
652         if (val) {
653             sp_style_read_icolor(&style->color, val, style, ( object
654                                                               ? SP_OBJECT_DOCUMENT(object)
655                                                               : NULL ));
656         }
657     }
658     /* fill */
659     if (!style->fill.set) {
660         val = repr->attribute("fill");
661         if (val) {
662             style->fill.read( val, *style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL );
663         }
664     }
665     /* fill-opacity */
666     if (!style->fill_opacity.set) {
667         val = repr->attribute("fill-opacity");
668         if (val) {
669             sp_style_read_iscale24(&style->fill_opacity, val);
670         }
671     }
672     /* fill-rule */
673     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
674     /* stroke */
675     if (!style->stroke.set) {
676         val = repr->attribute("stroke");
677         if (val) {
678             style->stroke.read( val, *style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL );
679         }
680     }
681     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
682     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
683     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
684     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
686     /* markers */
687     if (!style->marker[SP_MARKER_LOC].set) {
688         val = repr->attribute("marker");
689         if (val) {
690             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
691         }
692     }
693     if (!style->marker[SP_MARKER_LOC_START].set) {
694         val = repr->attribute("marker-start");
695         if (val) {
696             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
697         }
698     }
699     if (!style->marker[SP_MARKER_LOC_MID].set) {
700         val = repr->attribute("marker-mid");
701         if (val) {
702             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
703         }
704     }
705     if (!style->marker[SP_MARKER_LOC_END].set) {
706         val = repr->attribute("marker-end");
707         if (val) {
708             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
709         }
710     }
712     /* stroke-opacity */
713     if (!style->stroke_opacity.set) {
714         val = repr->attribute("stroke-opacity");
715         if (val) {
716             sp_style_read_iscale24(&style->stroke_opacity, val);
717         }
718     }
719     if (!style->stroke_dasharray_set) {
720         val = repr->attribute("stroke-dasharray");
721         if (val) {
722             sp_style_read_dash(style, val);
723         }
724     }
726     if (!style->stroke_dashoffset_set) {
727         val = repr->attribute("stroke-dashoffset");
728         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
729             style->stroke_dashoffset_set = TRUE;
730         } else if (val && !strcmp(val, "inherit")) {
731             style->stroke_dashoffset_set = TRUE;
732             style->stroke_dashoffset_inherit = TRUE;
733         } else {
734             style->stroke_dashoffset_set = FALSE;
735         }
736     }
738     /* -inkscape-font-specification */
739     if (!style->text_private || !style->text->font_specification.set) {
740         val = repr->attribute("-inkscape-font-specification");
741         if (val) {
742             if (!style->text_private) sp_style_privatize_text(style);
743             gchar *val_unquoted = attribute_unquote(val);
744             sp_style_read_istring(&style->text->font_specification, val_unquoted);
745             if (val_unquoted) g_free (val_unquoted);
746         }
747     }
749     /* font-family */
750     if (!style->text_private || !style->text->font_family.set) {
751         val = repr->attribute("font-family");
752         if (val) {
753             if (!style->text_private) sp_style_privatize_text(style);
754             gchar *val_unquoted = attribute_unquote(val);
755             sp_style_read_istring(&style->text->font_family, val_unquoted);
756             if (val_unquoted) g_free (val_unquoted);
757         }
758     }
760     /* filter effects */
761     if (!style->filter.set) {
762         val = repr->attribute("filter");
763         if (val) {
764             sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
765         }
766     }
767     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
768                             "enable-background", enum_enable_background, true);
770     /* 3. Merge from parent */
771     if (object) {
772         if (object->parent) {
773             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
774         }
775     } else {
776         if (sp_repr_parent(repr)) {
777             /// \todo fixme: This is not the prettiest thing (Lauris)
778             SPStyle *parent = sp_style_new(NULL);
779             sp_style_read(parent, NULL, sp_repr_parent(repr));
780             sp_style_merge_from_parent(style, parent);
781             sp_style_unref(parent);
782         }
783     }
787 /**
788  * Read style properties from object's repr.
789  *
790  * 1. Reset existing object style
791  * 2. Load current effective object style
792  * 3. Load i attributes from immediate parent (which has to be up-to-date)
793  */
794 void
795 sp_style_read_from_object(SPStyle *style, SPObject *object)
797     g_return_if_fail(style != NULL);
798     g_return_if_fail(object != NULL);
799     g_return_if_fail(SP_IS_OBJECT(object));
801     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
802     g_return_if_fail(repr != NULL);
804     sp_style_read(style, object, repr);
808 /**
809  * Read style properties from preferences.
810  * @param style The style to write to
811  * @param path Preferences directory from which the style should be read
812  */
813 void
814 sp_style_read_from_prefs(SPStyle *style, Glib::ustring const &path)
816     g_return_if_fail(style != NULL);
817     g_return_if_fail(path != "");
819     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
821     // not optimal: we reconstruct the node based on the prefs, then pass it to
822     // sp_style_read for actual processing.
823     Inkscape::XML::SimpleDocument *tempdoc = new Inkscape::XML::SimpleDocument;
824     Inkscape::XML::Node *tempnode = tempdoc->createElement("temp");
826     std::vector<Inkscape::Preferences::Entry> attrs = prefs->getAllEntries(path);
827     for (std::vector<Inkscape::Preferences::Entry>::iterator i = attrs.begin(); i != attrs.end(); ++i) {
828         tempnode->setAttribute(i->getEntryName().data(), i->getString().data());
829     }
831     sp_style_read(style, NULL, tempnode);
833     Inkscape::GC::release(tempnode);
834     Inkscape::GC::release(tempdoc);
835     delete tempdoc;
840 /**
841  *
842  */
843 static void
844 sp_style_privatize_text(SPStyle *style)
846     SPTextStyle *text = style->text;
847     style->text = sp_text_style_duplicate_unset(style->text);
848     sp_text_style_unref(text);
849     style->text_private = TRUE;
853 /**
854  * Merge property into style.
855  *
856  * Should be called in order of highest to lowest precedence.
857  * E.g. for a single style string, call from the last declaration to the first,
858  * as CSS says that later declarations override earlier ones.
859  *
860  * \pre val != NULL.
861  */
862 static void
863 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
865     g_return_if_fail(val != NULL);
867     switch (id) {
868         case SP_PROP_INKSCAPE_FONT_SPEC:
869             if (!style->text_private) sp_style_privatize_text(style);
870             if (!style->text->font_specification.set) {
871                 gchar *val_unquoted = attribute_unquote(val);
872                 sp_style_read_istring(&style->text->font_specification, val_unquoted);
873                 if (val_unquoted) g_free (val_unquoted);
874             }
875             break;
876         /* CSS2 */
877         /* Font */
878         case SP_PROP_FONT_FAMILY:
879             if (!style->text_private) sp_style_privatize_text(style);
880             if (!style->text->font_family.set) {
881                 gchar *val_unquoted = attribute_unquote(val);
882                 sp_style_read_istring(&style->text->font_family, val_unquoted);
883                 if (val_unquoted) g_free (val_unquoted);
884             }
885             break;
886         case SP_PROP_FONT_SIZE:
887             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
888             break;
889         case SP_PROP_FONT_SIZE_ADJUST:
890             if (strcmp(val, "none") != 0) {
891                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
892             }
893             break;
894         case SP_PROP_FONT_STYLE:
895             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
896             break;
897         case SP_PROP_FONT_VARIANT:
898             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
899             break;
900         case SP_PROP_FONT_WEIGHT:
901             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
902             break;
903         case SP_PROP_FONT_STRETCH:
904             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
905             break;
906         case SP_PROP_FONT:
907             if (!style->text_private) sp_style_privatize_text(style);
908             if (!style->text->font.set) {
909                 g_free(style->text->font.value);
910                 style->text->font.value = g_strdup(val);
911                 style->text->font.set = TRUE;
912                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
913             }
914             break;
915             /* Text */
916         case SP_PROP_TEXT_INDENT:
917             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
918             break;
919         case SP_PROP_TEXT_ALIGN:
920             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
921             break;
922         case SP_PROP_TEXT_DECORATION:
923             if (!style->text_decoration.set) {
924                 sp_style_read_itextdecoration(&style->text_decoration, val);
925             }
926             break;
927         case SP_PROP_LINE_HEIGHT:
928             if (!style->line_height.set) {
929                 sp_style_read_ilengthornormal(&style->line_height, val);
930             }
931             break;
932         case SP_PROP_LETTER_SPACING:
933             if (!style->letter_spacing.set) {
934                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
935             }
936             break;
937         case SP_PROP_WORD_SPACING:
938             if (!style->word_spacing.set) {
939                 sp_style_read_ilengthornormal(&style->word_spacing, val);
940             }
941             break;
942         case SP_PROP_TEXT_TRANSFORM:
943             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
944             break;
945             /* Text (css3) */
946         case SP_PROP_DIRECTION:
947             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
948             break;
949         case SP_PROP_BLOCK_PROGRESSION:
950             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
951             break;
952         case SP_PROP_WRITING_MODE:
953             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
954             break;
955         case SP_PROP_TEXT_ANCHOR:
956             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
957             break;
958         case SP_PROP_BASELINE_SHIFT:
959             SPS_READ_IBASELINE_SHIFT_IF_UNSET(&style->baseline_shift, val);
960             break;
961             /* Text (unimplemented) */
962         case SP_PROP_TEXT_RENDERING: {
963             /* Ignore the hint. */
964             SPIEnum dummy;
965             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
966             break;
967         }
968         case SP_PROP_ALIGNMENT_BASELINE:
969             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
970             break;
971         case SP_PROP_DOMINANT_BASELINE:
972             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
973             break;
974         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
975             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
976             break;
977         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
978             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
979             break;
980         case SP_PROP_KERNING:
981             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
982             break;
983             /* Misc */
984         case SP_PROP_CLIP:
985             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
986             break;
987         case SP_PROP_COLOR:
988             if (!style->color.set) {
989                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
990             }
991             break;
992         case SP_PROP_CURSOR:
993             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
994             break;
995         case SP_PROP_DISPLAY:
996             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
997             break;
998         case SP_PROP_OVERFLOW:
999             /** \todo
1000              * FIXME: not supported properly yet, we just read and write it,
1001              * but act as if it is always "display".
1002              */
1003             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
1004             break;
1005         case SP_PROP_VISIBILITY:
1006             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
1007             break;
1008             /* SVG */
1009             /* Clip/Mask */
1010         case SP_PROP_CLIP_PATH:
1011             /** \todo
1012              * This is a workaround. Inkscape only supports 'clip-path' as SVG attribute, not as
1013              * style property. By having both CSS and SVG attribute set, editing of clip-path
1014              * will fail, since CSS always overwrites SVG attributes.
1015              * Fixes Bug #324849
1016              */
1017             g_warning("attribute 'clip-path' given as CSS");
1018             style->object->repr->setAttribute("clip-path", val);
1019             break;
1020         case SP_PROP_CLIP_RULE:
1021             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
1022             break;
1023         case SP_PROP_MASK:
1024             /** \todo
1025              * See comment for SP_PROP_CLIP_PATH
1026              */
1027             g_warning("attribute 'mask' given as CSS");
1028             style->object->repr->setAttribute("mask", val);
1029             break;
1030         case SP_PROP_OPACITY:
1031             if (!style->opacity.set) {
1032                 sp_style_read_iscale24(&style->opacity, val);
1033             }
1034             break;
1035         case SP_PROP_ENABLE_BACKGROUND:
1036             SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
1037                                     enum_enable_background, true);
1038             break;
1039             /* Filter */
1040         case SP_PROP_FILTER:
1041             if (!style->filter.set && !style->filter.inherit) {
1042                 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1043             }
1044             break;
1045         case SP_PROP_FLOOD_COLOR:
1046             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
1047             break;
1048         case SP_PROP_FLOOD_OPACITY:
1049             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
1050             break;
1051         case SP_PROP_LIGHTING_COLOR:
1052             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
1053             break;
1054             /* Gradient */
1055         case SP_PROP_STOP_COLOR:
1056             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
1057             break;
1058         case SP_PROP_STOP_OPACITY:
1059             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
1060             break;
1061             /* Interactivity */
1062         case SP_PROP_POINTER_EVENTS:
1063             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
1064             break;
1065             /* Paint */
1066         case SP_PROP_COLOR_INTERPOLATION:
1067             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1068             break;
1069         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1070             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1071             break;
1072         case SP_PROP_COLOR_PROFILE:
1073             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1074             break;
1075         case SP_PROP_COLOR_RENDERING: {
1076             /* Ignore the hint. */
1077             SPIEnum dummy;
1078             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1079             break;
1080         }
1081         case SP_PROP_FILL:
1082             if (!style->fill.set) {
1083                 style->fill.read( val, *style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL );
1084             }
1085             break;
1086         case SP_PROP_FILL_OPACITY:
1087             if (!style->fill_opacity.set) {
1088                 sp_style_read_iscale24(&style->fill_opacity, val);
1089             }
1090             break;
1091         case SP_PROP_FILL_RULE:
1092             if (!style->fill_rule.set) {
1093                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1094             }
1095             break;
1096         case SP_PROP_IMAGE_RENDERING: {
1097             /* Ignore the hint. */
1098             SPIEnum dummy;
1099             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1100             break;
1101         }
1102         case SP_PROP_MARKER:
1103             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1104             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1105             if (!style->marker[SP_MARKER_LOC].set) {
1106                 g_free(style->marker[SP_MARKER_LOC].value);
1107                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1108                 style->marker[SP_MARKER_LOC].set = TRUE;
1109                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1110             }
1111             break;
1113         case SP_PROP_MARKER_START:
1114             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1115             if (!style->marker[SP_MARKER_LOC_START].set) {
1116                 g_free(style->marker[SP_MARKER_LOC_START].value);
1117                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1118                 style->marker[SP_MARKER_LOC_START].set = TRUE;
1119                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1120             }
1121             break;
1122         case SP_PROP_MARKER_MID:
1123             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1124             if (!style->marker[SP_MARKER_LOC_MID].set) {
1125                 g_free(style->marker[SP_MARKER_LOC_MID].value);
1126                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1127                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1128                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1129             }
1130             break;
1131         case SP_PROP_MARKER_END:
1132             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1133             if (!style->marker[SP_MARKER_LOC_END].set) {
1134                 g_free(style->marker[SP_MARKER_LOC_END].value);
1135                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1136                 style->marker[SP_MARKER_LOC_END].set = TRUE;
1137                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1138             }
1139             break;
1141         case SP_PROP_SHAPE_RENDERING: {
1142             /* Ignore the hint. */
1143             SPIEnum dummy;
1144             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1145             break;
1146         }
1148         case SP_PROP_STROKE:
1149             if (!style->stroke.set) {
1150                 style->stroke.read( val, *style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL );
1151             }
1152             break;
1153         case SP_PROP_STROKE_WIDTH:
1154             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1155             break;
1156         case SP_PROP_STROKE_DASHARRAY:
1157             if (!style->stroke_dasharray_set) {
1158                 sp_style_read_dash(style, val);
1159             }
1160             break;
1161         case SP_PROP_STROKE_DASHOFFSET:
1162             if (!style->stroke_dashoffset_set) {
1163                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1164                     style->stroke_dashoffset_set = TRUE;
1165                 } else if (val && !strcmp(val, "inherit")) {
1166                     style->stroke_dashoffset_set = TRUE;
1167                     style->stroke_dashoffset_inherit = TRUE;
1168                 } else {
1169                     style->stroke_dashoffset_set = FALSE;
1170                 }
1171             }
1172             break;
1173         case SP_PROP_STROKE_LINECAP:
1174             if (!style->stroke_linecap.set) {
1175                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1176             }
1177             break;
1178         case SP_PROP_STROKE_LINEJOIN:
1179             if (!style->stroke_linejoin.set) {
1180                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1181             }
1182             break;
1183         case SP_PROP_STROKE_MITERLIMIT:
1184             if (!style->stroke_miterlimit.set) {
1185                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1186             }
1187             break;
1188         case SP_PROP_STROKE_OPACITY:
1189             if (!style->stroke_opacity.set) {
1190                 sp_style_read_iscale24(&style->stroke_opacity, val);
1191             }
1192             break;
1194         default:
1195             g_warning("Invalid style property id: %d value: %s", id, val);
1196             break;
1197     }
1200 static void
1201 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1203     /** \todo Ensure that property is lcased, as per
1204      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1205      * Should probably be done in libcroco.
1206      */
1207     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1208     if (prop_idx != SP_ATTR_INVALID) {
1209         /** \todo
1210          * effic: Test whether the property is already set before trying to
1211          * convert to string. Alternatively, set from CRTerm directly rather
1212          * than converting to string.
1213          */
1214         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1215         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1216         sp_style_merge_property(style, prop_idx, str_value);
1217         g_free(str_value);
1218     }
1221 static void
1222 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1224 #if 0 /* forwards */
1225     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1226         CRDeclaration *decl = NULL;
1227         cr_prop_list_get_decl(cur, &decl);
1228         sp_style_merge_style_from_decl(style, decl);
1229     }
1230 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1231     if (props) {
1232         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1233         CRDeclaration *decl = NULL;
1234         cr_prop_list_get_decl(props, &decl);
1235         sp_style_merge_style_from_decl(style, decl);
1236     }
1237 #endif
1240 /**
1241  * \pre decl_list != NULL
1242  */
1243 static void
1244 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1246     // read the decls from end to start, using head recursion, so that latter declarations override
1247     // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1248     // because sp_style_merge_style_from_decl only sets properties that are unset
1249     if (decl_list->next) {
1250         sp_style_merge_from_decl_list(style, decl_list->next);
1251     }
1252     sp_style_merge_style_from_decl(style, decl_list);
1255 static CRSelEng *
1256 sp_repr_sel_eng()
1258     CRSelEng *const ret = cr_sel_eng_new();
1259     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1261     /** \todo
1262      * Check whether we need to register any pseudo-class handlers.
1263      * libcroco has its own default handlers for first-child and lang.
1264      *
1265      * We probably want handlers for link and arguably visited (though
1266      * inkscape can't visit links at the time of writing).  hover etc.
1267      * more useful in inkview than the editor inkscape.
1268      *
1269      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1270      * the following should be honoured, at least by inkview:
1271      * :hover, :active, :focus, :visited, :link.
1272      */
1274     g_assert(ret);
1275     return ret;
1278 static void
1279 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1281     static CRSelEng *sel_eng = NULL;
1282     if (!sel_eng) {
1283         sel_eng = sp_repr_sel_eng();
1284     }
1286     CRPropList *props = NULL;
1287     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1288                                                                      object->document->style_cascade,
1289                                                                      object->repr,
1290                                                                      &props);
1291     g_return_if_fail(status == CR_OK);
1292     /// \todo Check what errors can occur, and handle them properly.
1293     if (props) {
1294         sp_style_merge_from_props(style, props);
1295         cr_prop_list_destroy(props);
1296     }
1299 /**
1300  * Parses a style="..." string and merges it with an existing SPStyle.
1301  */
1302 void
1303 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1305     /*
1306      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1307      * ``When CSS styling is used, CSS inline style is specified by including
1308      * semicolon-separated property declarations of the form "name : value"
1309      * within the style attribute''.
1310      *
1311      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1312      * Why does it say "including", what else is allowed in the style
1313      * attribute value?
1314      */
1316     CRDeclaration *const decl_list
1317         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1318     if (decl_list) {
1319         sp_style_merge_from_decl_list(style, decl_list);
1320         cr_declaration_destroy(decl_list);
1321     }
1324 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1325 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1327 static void
1328 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1330     /* 'font-size' */
1331     if (!child.set || child.inherit) {
1332         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1333         child.computed = parent.computed;
1334     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1335         /** \todo
1336          * fixme: SVG and CSS do not specify clearly, whether we should use
1337          * user or screen coordinates (Lauris)
1338          */
1339         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1340             child.computed = font_size_table[child.value];
1341         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1342             child.computed = parent.computed / 1.2;
1343         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1344             child.computed = parent.computed * 1.2;
1345         } else {
1346             /* Illegal value */
1347         }
1348     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1349         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1350          * rather than viewport. */
1351         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1352     }
1355 // Some shifts are defined relative to parent.
1356 static void
1357 sp_style_merge_baseline_shift_from_parent(SPIBaselineShift &child, SPIBaselineShift const &parent,
1358                                           SPIFontSize const &pfont_size)
1360     /* 'baseline-shift' */
1361     if (!child.set || child.inherit) {
1362         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1363         child.computed = parent.computed;  // Does this make sense (applying a shift a second time)?
1364     } else if (child.type == SP_BASELINE_SHIFT_LITERAL) {
1365         if( child.literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
1366             child.computed = 0; // No change
1367         } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUB ) {
1368             // Should use subscript position from font relative to alphabetic baseline
1369             // OpenOffice, Adobe: -0.33, Word -0.14, LaTex about -0.2.
1370             child.computed = -0.2 * pfont_size.computed; 
1371         } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUPER ) {
1372             // Should use superscript position from font relative to alphabetic baseline
1373             // OpenOffice, Adobe: 0.33, Word 0.35, LaTex about 0.45.
1374             child.computed =  0.4 * pfont_size.computed; 
1375         } else {
1376             /* Illegal value */
1377         }
1378     } else if (child.type == SP_BASELINE_SHIFT_PERCENTAGE) {
1379         // Percentage for baseline shift is relative to computed "line-height"
1380         // which is just font-size (see SVG1.1 'font').
1381         child.computed = pfont_size.computed * child.value;
1382     } else if (child.type == SP_BASELINE_SHIFT_LENGTH) {
1383         switch (child.unit) {
1384             case SP_CSS_UNIT_EM:
1385                 child.computed = child.value * pfont_size.computed; 
1386                 break;
1387             case SP_CSS_UNIT_EX:
1388                 child.computed = child.value * 0.5 * pfont_size.computed; 
1389                 break;
1390             default:
1391                 /* No change */
1392                 break;
1393         }
1394     }
1395     // baseline-shifts are relative to parent baseline
1396     child.computed += parent.computed;
1399 /**
1400  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1401  * calculating from) corresponding computed values of \a parent.
1402  *
1403  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1404  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1405  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1406  * about what the computed value is (not obvious for lengths).
1407  *
1408  * \pre \a parent's computed values are already up-to-date.
1409  */
1410 void
1411 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1413     g_return_if_fail(style != NULL);
1415     /** \todo
1416      * fixme: Check for existing callers that might pass null parent.
1417      * This should probably be g_return_if_fail, or else we should make a
1418      * best attempt to set computed values correctly without having a parent
1419      * (i.e., by assuming parent has initial values).
1420      */
1421     if (!parent)
1422         return;
1424     /* CSS2 */
1425     /* Font */
1426     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1428     /* 'font-style' */
1429     if (!style->font_style.set || style->font_style.inherit) {
1430         style->font_style.computed = parent->font_style.computed;
1431     }
1433     /* 'font-variant' */
1434     if (!style->font_variant.set || style->font_variant.inherit) {
1435         style->font_variant.computed = parent->font_variant.computed;
1436     }
1438     /* 'font-weight' */
1439     if (!style->font_weight.set || style->font_weight.inherit) {
1440         style->font_weight.computed = parent->font_weight.computed;
1441     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1442         /** \todo
1443          * fixme: This is unconditional, i.e., happens even if parent not
1444          * present.
1445          */
1446         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1447     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1448         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1449     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1450         unsigned const parent_val = parent->font_weight.computed;
1451         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1452         // strictly, 'bolder' and 'lighter' should go to the next weight
1453         // expressible in the current font family, but that's difficult to
1454         // find out, so jumping by 3 seems an appropriate approximation
1455         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1456                                        ? (unsigned)SP_CSS_FONT_WEIGHT_100
1457                                        : parent_val - 3);
1458         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1459     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1460         unsigned const parent_val = parent->font_weight.computed;
1461         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1462         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1463                                        ? (unsigned)SP_CSS_FONT_WEIGHT_900
1464                                        : parent_val + 3);
1465         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1466     }
1468     /* 'font-stretch' */
1469     if (!style->font_stretch.set || style->font_stretch.inherit) {
1470         style->font_stretch.computed = parent->font_stretch.computed;
1471     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1472         unsigned const parent_val = parent->font_stretch.computed;
1473         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1474                                         ? parent_val
1475                                         : parent_val - 1);
1476         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1477     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1478         unsigned const parent_val = parent->font_stretch.computed;
1479         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1480         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1481                                         ? parent_val
1482                                         : parent_val + 1);
1483         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1484     }
1486     /* text (css2) */
1487     if (!style->text_indent.set || style->text_indent.inherit) {
1488         style->text_indent.computed = parent->text_indent.computed;
1489     }
1491     if (!style->text_align.set || style->text_align.inherit) {
1492         style->text_align.computed = parent->text_align.computed;
1493     }
1495     if (!style->text_decoration.set || style->text_decoration.inherit) {
1496         style->text_decoration.underline = parent->text_decoration.underline;
1497         style->text_decoration.overline = parent->text_decoration.overline;
1498         style->text_decoration.line_through = parent->text_decoration.line_through;
1499         style->text_decoration.blink = parent->text_decoration.blink;
1500     }
1502     if (!style->line_height.set || style->line_height.inherit) {
1503         style->line_height.value = parent->line_height.value;
1504         style->line_height.computed = parent->line_height.computed;
1505         style->line_height.normal = parent->line_height.normal;
1506     }
1508     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1509         style->letter_spacing.value = parent->letter_spacing.value;
1510         style->letter_spacing.computed = parent->letter_spacing.computed;
1511         style->letter_spacing.normal = parent->letter_spacing.normal;
1512     }
1514     if (!style->word_spacing.set || style->word_spacing.inherit) {
1515         style->word_spacing.value = parent->word_spacing.value;
1516         style->word_spacing.computed = parent->word_spacing.computed;
1517         style->word_spacing.normal = parent->word_spacing.normal;
1518     }
1520     if (!style->text_transform.set || style->text_transform.inherit) {
1521         style->text_transform.computed = parent->text_transform.computed;
1522     }
1524     if (!style->direction.set || style->direction.inherit) {
1525         style->direction.computed = parent->direction.computed;
1526     }
1528     if (!style->block_progression.set || style->block_progression.inherit) {
1529         style->block_progression.computed = parent->block_progression.computed;
1530     }
1532     if (!style->writing_mode.set || style->writing_mode.inherit) {
1533         style->writing_mode.computed = parent->writing_mode.computed;
1534     }
1536     if (!style->text_anchor.set || style->text_anchor.inherit) {
1537         style->text_anchor.computed = parent->text_anchor.computed;
1538     }
1540     /* Baseline Shift... Some shifts are relative to parent. */
1541     sp_style_merge_baseline_shift_from_parent(style->baseline_shift, parent->baseline_shift,
1542                                               parent->font_size);
1544     if (style->opacity.inherit) {
1545         style->opacity.value = parent->opacity.value;
1546     }
1548     /* Color */
1549     if (!style->color.set || style->color.inherit) {
1550         sp_style_merge_ipaint(style, &style->color, &parent->color);
1551     }
1553     /* Fill */
1554     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1555         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1556     }
1558     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1559         style->fill_opacity.value = parent->fill_opacity.value;
1560     }
1562     if (!style->fill_rule.set || style->fill_rule.inherit) {
1563         style->fill_rule.computed = parent->fill_rule.computed;
1564     }
1566     /* Stroke */
1567     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1568         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1569     }
1571     if (!style->stroke_width.set || style->stroke_width.inherit) {
1572         style->stroke_width.computed = parent->stroke_width.computed;
1573     } else {
1574         /* Update computed value for any change in font inherited from parent. */
1575         double const em = style->font_size.computed;
1576         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1577             style->stroke_width.computed = style->stroke_width.value * em;
1578         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1579             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1580             style->stroke_width.computed = style->stroke_width.value * ex;
1581         }
1582     }
1584     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1585         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1586     }
1588     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1589         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1590     }
1592     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1593         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1594     }
1596     if (!style->stroke_dasharray_set || style->stroke_dasharray_inherit) {
1597         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1598         if (style->stroke_dash.n_dash > 0) {
1599             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1600             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1601         }
1602     }
1604     if (!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) {
1605         style->stroke_dash.offset = parent->stroke_dash.offset;
1606     }
1608     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1609         style->stroke_opacity.value = parent->stroke_opacity.value;
1610     }
1612     if (style->text && parent->text) {
1613         if (!style->text->font_family.set || style->text->font_family.inherit) {
1614             g_free(style->text->font_family.value);
1615             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1616         }
1617     }
1619     if (style->text && parent->text) {
1620         if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1621             g_free(style->text->font_specification.value);
1622             style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1623         }
1624     }
1626     /* Markers - Free the old value and make copy of the new */
1627     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1628         if (!style->marker[i].set || style->marker[i].inherit) {
1629             g_free(style->marker[i].value);
1630             style->marker[i].value = g_strdup(parent->marker[i].value);
1631         }
1632     }
1634     /* Filter effects */
1635     if (style->filter.inherit) {
1636         sp_style_merge_ifilter(style, &parent->filter);
1637     }
1639     if(style->enable_background.inherit) {
1640         style->enable_background.value = parent->enable_background.value;
1641     }
1644 template <typename T>
1645 static void
1646 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1648     if ( ( !(child.set) || child.inherit )
1649          && parent.set && !(parent.inherit) )
1650     {
1651         child = parent;
1652     }
1655 /**
1656  * Copy SPIString from parent to child.
1657  */
1658 static void
1659 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1661     if ( ( !(child.set) || child.inherit )
1662          && parent.set && !(parent.inherit) )
1663     {
1664         g_free(child.value);
1665         child.value = g_strdup(parent.value);
1666         child.set = parent.set;
1667         child.inherit = parent.inherit;
1668     }
1671 static void
1672 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1673                                             SPIPaint &child, SPIPaint const &parent)
1675     /** \todo
1676      * I haven't given this much attention.  See comments below about
1677      * currentColor, colorProfile, and relative URIs.
1678      */
1679     if (!child.set || child.inherit) {
1680         sp_style_merge_ipaint(style, &child, &parent);
1681         child.set = parent.set;
1682         child.inherit = parent.inherit;
1683     }
1686 static void
1687 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1688                                                unsigned const max_computed_val,
1689                                                unsigned const smaller_val)
1691     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1692        then zero or more other absolute values, then smaller_val then larger_val. */
1693     unsigned const min_computed_val = 0;
1694     unsigned const larger_val = smaller_val + 1;
1695     g_return_if_fail(min_computed_val < max_computed_val);
1696     g_return_if_fail(max_computed_val < smaller_val);
1698     if (parent.set && !parent.inherit) {
1699         if (!child.set || child.inherit) {
1700             child.value = parent.value;
1701             child.set = parent.set;  // i.e. true
1702             child.inherit = parent.inherit;  // i.e. false
1703         } else if (child.value < smaller_val) {
1704             /* Child has absolute value: leave as is. */
1705         } else if ( ( child.value == smaller_val
1706                       && parent.value == larger_val )
1707                     || ( parent.value == smaller_val
1708                          && child.value == larger_val ) )
1709         {
1710             child.set = false;
1711             /*
1712              * Note that this can result in a change in computed value in the
1713              * rare case that the parent's setting was a no-op (i.e. if the
1714              * parent's parent's computed value was already ultra-condensed or
1715              * ultra-expanded).  However, I'd guess that the change is for the
1716              * better: I'd guess that if the properties were specified
1717              * relatively, then the intent is to counteract parent's effect.
1718              * I.e. I believe this is the best code even in that case.
1719              */
1720         } else if (child.value == parent.value) {
1721             /* Leave as is. */
1722             /** \todo
1723              * It's unclear what to do if style and parent specify the same
1724              * relative directive (narrower or wider).  We can either convert
1725              * to absolute specification or coalesce to a single relative
1726              * request (of half the strength of the original pair).
1727              *
1728              * Converting to a single level of relative specification is a
1729              * better choice if the newly-unlinked clone is itself cloned to
1730              * other contexts (inheriting different font stretchiness): it
1731              * would preserve the effect that it will be narrower than
1732              * the inherited context in each case.  The disadvantage is that
1733              * it will ~certainly affect the computed value of the
1734              * newly-unlinked clone.
1735              */
1736         } else {
1737             unsigned const parent_val = parent.computed;
1738             child.value = ( child.value == smaller_val
1739                             ? ( parent_val == min_computed_val
1740                                 ? parent_val
1741                                 : parent_val - 1 )
1742                             : ( parent_val == max_computed_val
1743                                 ? parent_val
1744                                 : parent_val + 1 ) );
1745             g_assert(child.value <= max_computed_val);
1746             child.inherit = false;
1747             g_assert(child.set);
1748         }
1749     }
1752 template <typename LengthT>
1753 static void
1754 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1755                                              double const parent_child_em_ratio)
1757     if ( ( !(child.set) || child.inherit )
1758          && parent.set && !(parent.inherit) )
1759     {
1760         child = parent;
1761         switch (parent.unit) {
1762             case SP_CSS_UNIT_EM:
1763             case SP_CSS_UNIT_EX:
1764                 child.value *= parent_child_em_ratio;
1765                 /** \todo
1766                  * fixme: Have separate ex ratio parameter.
1767                  * Get x height from libnrtype or pango.
1768                  */
1769                 if (!IS_FINITE(child.value)) {
1770                     child.value = child.computed;
1771                     child.unit = SP_CSS_UNIT_NONE;
1772                 }
1773                 break;
1775             default:
1776                 break;
1777         }
1778     }
1781 static double
1782 get_relative_font_size_frac(SPIFontSize const &font_size)
1784     switch (font_size.type) {
1785         case SP_FONT_SIZE_LITERAL: {
1786             switch (font_size.value) {
1787                 case SP_CSS_FONT_SIZE_SMALLER:
1788                     return 5.0 / 6.0;
1790                 case SP_CSS_FONT_SIZE_LARGER:
1791                     return 6.0 / 5.0;
1793                 default:
1794                     g_assert_not_reached();
1795             }
1796         }
1798         case SP_FONT_SIZE_PERCENTAGE:
1799             return SP_F8_16_TO_FLOAT(font_size.value);
1801         case SP_FONT_SIZE_LENGTH:
1802             g_assert_not_reached();
1803     }
1804     g_assert_not_reached();
1807 /**
1808  * Combine \a style and \a parent style specifications into a single style specification that
1809  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1810  *
1811  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1812  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1813  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1814  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1815  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1816  *
1817  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1818  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1819  * style was previously up-to-date wrt \a parent.
1820  *
1821  * \pre \a parent's computed values are already up-to-date.
1822  *   (\a style's computed values needn't be up-to-date.)
1823  */
1824 void
1825 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1827     /** \note
1828      * The general rule for each property is as follows:
1829      *
1830      *   If style is set to an absolute value, then leave it as is.
1831      *
1832      *   Otherwise (i.e. if style has a relative value):
1833      *
1834      *     If parent is set to an absolute value, then set style to the computed value.
1835      *
1836      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1837      */
1839     /* We do font-size first, to ensure that em size is up-to-date. */
1840     /** \todo
1841      * fixme: We'll need to have more font-related things up the top once
1842      * we're getting x-height from pango or libnrtype.
1843      */
1845     /* Some things that allow relative specifications. */
1846     {
1847         /* font-size.  Note that we update the computed font-size of style,
1848            to assist in em calculations later in this function. */
1849         if (parent->font_size.set && !parent->font_size.inherit) {
1850             if (!style->font_size.set || style->font_size.inherit) {
1851                 /* font_size inherits the computed value, so we can use the parent value
1852                  * verbatim. */
1853                 style->font_size = parent->font_size;
1854             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1855                 /* Child already has absolute size (stored in computed value), so do nothing. */
1856             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1857                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1858                 /* Child already has absolute size, but we ensure that the computed value
1859                    is up-to-date. */
1860                 unsigned const ix = style->font_size.value;
1861                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1862                 style->font_size.computed = font_size_table[ix];
1863             } else {
1864                 /* Child has relative size. */
1865                 double const child_frac(get_relative_font_size_frac(style->font_size));
1866                 style->font_size.set = true;
1867                 style->font_size.inherit = false;
1868                 style->font_size.computed = parent->font_size.computed * child_frac;
1870                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1871                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1872                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1873                 {
1874                     /* Absolute value. */
1875                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1876                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1877                 } else {
1878                     /* Relative value. */
1879                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1880                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1881                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1882                 }
1883             }
1884         }
1886         /* 'font-stretch' */
1887         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1888                                                        parent->font_stretch,
1889                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1890                                                        SP_CSS_FONT_STRETCH_NARROWER);
1892         /* font-weight */
1893         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1894                                                        parent->font_weight,
1895                                                        SP_CSS_FONT_WEIGHT_900,
1896                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1897     }
1900     /* Enum values that don't have any relative settings (other than `inherit'). */
1901     {
1902         SPIEnum SPStyle::*const fields[] = {
1903             //nyi: SPStyle::clip_rule,
1904             //nyi: SPStyle::color_interpolation,
1905             //nyi: SPStyle::color_interpolation_filters,
1906             //nyi: SPStyle::color_rendering,
1907             &SPStyle::direction,
1908             &SPStyle::fill_rule,
1909             &SPStyle::font_style,
1910             &SPStyle::font_variant,
1911             //nyi: SPStyle::image_rendering,
1912             //nyi: SPStyle::pointer_events,
1913             //nyi: SPStyle::shape_rendering,
1914             &SPStyle::stroke_linecap,
1915             &SPStyle::stroke_linejoin,
1916             &SPStyle::text_anchor,
1917             //nyi: &SPStyle::text_rendering,
1918             &SPStyle::visibility,
1919             &SPStyle::writing_mode
1920         };
1922         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1923             SPIEnum SPStyle::*const fld = fields[i];
1924             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1925         }
1926     }
1928     /* A few other simple inheritance properties. */
1929     {
1930         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1931         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1932         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1934         /** \todo
1935          * We currently treat text-decoration as if it were a simple inherited
1936          * property (fixme). This code may need changing once we do the
1937          * special fill/stroke inheritance mentioned by the spec.
1938          */
1939         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1940                                                                  parent->text_decoration);
1942         //nyi: font-size-adjust,  // <number> | none | inherit
1943         //nyi: glyph-orientation-horizontal,
1944         //nyi: glyph-orientation-vertical,
1945     }
1947     /* Properties that involve length but are easy in other respects. */
1948     {
1949         /* The difficulty with lengths is that font-relative units need adjusting if the font
1950          * varies between parent & child.
1951          *
1952          * Lengths specified in the existing child can stay as they are: its computed font
1953          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1954          * size.
1955          *
1956          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1957          * ratio of em or ex size between parent & child.
1958          */
1959         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1961         SPILength SPStyle::*const lfields[] = {
1962             &SPStyle::stroke_width,
1963             &SPStyle::text_indent
1964         };
1965         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1966             SPILength SPStyle::*fld = lfields[i];
1967             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1968                                                                     parent->*fld,
1969                                                                     parent_child_em_ratio);
1970         }
1972         SPILengthOrNormal SPStyle::*const nfields[] = {
1973             &SPStyle::letter_spacing,
1974             &SPStyle::line_height,
1975             &SPStyle::word_spacing
1976         };
1977         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1978             SPILengthOrNormal SPStyle::*fld = nfields[i];
1979             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1980                                                                             parent->*fld,
1981                                                                             parent_child_em_ratio);
1982         }
1984         //nyi: &SPStyle::kerning: length or `auto'
1986         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1987            can accept units. */
1988     }
1990     /* Properties that involve a URI but are easy in other respects. */
1991     {
1992         /** \todo
1993          * Could cause problems if original object was in another document
1994          * and it used a relative URL.  (At the time of writing, we don't
1995          * allow hrefs to other documents, so this isn't a problem yet.)
1996          * Paint properties also allow URIs.
1997          */
1998         //nyi: cursor,   // may involve change in effect, but we can't do much better
1999         //nyi: color-profile,
2001         // Markers (marker-start, marker-mid, marker-end).
2002         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2003             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
2004         }
2005     }
2007     /* CSS2 */
2008     /* Font */
2010     if (style->text && parent->text) {
2011         sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
2012                                                      parent->text->font_specification);
2014         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
2015                                                      parent->text->font_family);
2016     }
2019     /* Properties that don't inherit by default.  Most of these need special handling. */
2020     {
2021         /*
2022          * opacity's effect is cumulative; we set the new value to the combined effect.  The
2023          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
2024          * fill-opacity are quite different from opacity, and don't need any special handling.)
2025          *
2026          * Cases:
2027          * - parent & child were each previously unset, in which case the effective
2028          *   opacity value is 1.0, and style should remain unset.
2029          * - parent was previously unset (so computed opacity value of 1.0)
2030          *   and child was set to inherit.  The merged child should
2031          *   get a value of 1.0, and shouldn't inherit (lest the new parent
2032          *   has a different opacity value).  Given that opacity's default
2033          *   value is 1.0 (rather than inherit), we might as well have the
2034          *   merged child's opacity be unset.
2035          * - parent was previously unset (so opacity 1.0), and child was set to a number.
2036          *   The merged child should retain its existing settings (though it doesn't matter
2037          *   if we make it unset if that number was 1.0).
2038          * - parent was inherit and child was unset.  Merged child should be set to inherit.
2039          * - parent was inherit and child was inherit.  (We can't in general reproduce this
2040          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
2041          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
2042          *   child's value should be set to the product of the two, i.e. the square of the
2043          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
2044          *   is more important to retain the effective opacity than to retain the inheriting
2045          *   effect, and assumes that the inheriting effect either isn't important enough to create
2046          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
2047          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
2048          *   closer to maintaining the effect.
2049          * - parent was inherit and child was set to a numerical value.  If the child's value
2050          *   was 1.0, then the merged child should have the same settings as the parent.
2051          *   If the child's value was 0, then the merged child should also be set to 0.
2052          *   If the child's value was anything else, then we do the same as for the inherit/inherit
2053          *   case above: have the merged child set to the product of the two opacities and not
2054          *   marked as inherit, for the same reasons as for that case.
2055          * - parent was set to a value, and child was unset.  The merged child should have
2056          *   parent's settings.
2057          * - parent was set to a value, and child was inherit.  The merged child should
2058          *   be set to the product, i.e. the square of the parent's value.
2059          * - parent & child are each set to a value.  The merged child should be set to the
2060          *   product.
2061          */
2062         if ( !style->opacity.set
2063              || ( !style->opacity.inherit
2064                   && style->opacity.value == SP_SCALE24_MAX ) )
2065         {
2066             style->opacity = parent->opacity;
2067         } else {
2068             /* Ensure that style's computed value is up-to-date. */
2069             if (style->opacity.inherit) {
2070                 style->opacity.value = parent->opacity.value;
2071             }
2073             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
2074             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
2075                                                   parent->opacity.value);
2077             style->opacity.inherit = (parent->opacity.inherit
2078                                       && style->opacity.inherit
2079                                       && (parent->opacity.value == 0 ||
2080                                           parent->opacity.value == SP_SCALE24_MAX));
2081             style->opacity.set = ( style->opacity.inherit
2082                                    || style->opacity.value < SP_SCALE24_MAX );
2083         }
2085         /* display is in principle similar to opacity, but implementation is easier. */
2086         if ( parent->display.set && !parent->display.inherit
2087              && parent->display.value == SP_CSS_DISPLAY_NONE ) {
2088             style->display.value = SP_CSS_DISPLAY_NONE;
2089             style->display.set = true;
2090             style->display.inherit = false;
2091         } else if (style->display.inherit) {
2092             style->display.value = parent->display.value;
2093             style->display.set = parent->display.set;
2094             style->display.inherit = parent->display.inherit;
2095         } else {
2096             /* Leave as is.  (display doesn't inherit by default.) */
2097         }
2099         /* enable-background - this is rather complicated, because
2100          * it is valid only when applied to container elements.
2101          * Let's check a simple case anyhow. */
2102         if (parent->enable_background.set
2103             && !parent->enable_background.inherit
2104             && style->enable_background.inherit)
2105         {
2106             style->enable_background.set = true;
2107             style->enable_background.inherit = false;
2108             style->enable_background.value = parent->enable_background.value;
2109         }
2111         if (!style->filter.set || style->filter.inherit)
2112         {
2113             sp_style_merge_ifilter(style, &parent->filter);
2114         }
2116         /** \todo
2117          * fixme: Check that we correctly handle all properties that don't
2118          * inherit by default (as shown in
2119          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2120          */
2121     }
2123     /* SPIPaint properties (including color). */
2124     {
2125         /** \todo
2126          * Think about the issues involved if specified as currentColor or
2127          * if specified relative to colorProfile, and if the currentColor or
2128          * colorProfile differs between parent \& child.  See also comments
2129          * elsewhere in this function about URIs.
2130          */
2131         SPIPaint SPStyle::*const fields[] = {
2132             &SPStyle::color,
2133             &SPStyle::fill,
2134             &SPStyle::stroke
2135         };
2136         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2137             SPIPaint SPStyle::*const fld = fields[i];
2138             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2139         }
2140     }
2142     /* Things from SVG 1.2 or CSS3. */
2143     {
2144         /* Note: If we ever support setting string values for text-align then we'd need strdup
2145          * handling here. */
2146         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2148         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2149         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2150     }
2152     /* Note: this will need length handling once dasharray supports units. */
2153     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2154          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2155     {
2156         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2157         if (style->stroke_dash.n_dash > 0) {
2158             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2159             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2160         }
2161         style->stroke_dasharray_set = parent->stroke_dasharray_set;
2162         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2163     }
2165     /* Note: this will need length handling once dasharray_offset supports units. */
2166     if ((!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) && parent->stroke_dashoffset_set && !parent->stroke_dashoffset_inherit) {
2167         style->stroke_dash.offset = parent->stroke_dash.offset;
2168         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2169         style->stroke_dashoffset_inherit = parent->stroke_dashoffset_inherit;
2170         /* TODO: Try to
2171          * represent it as a normal SPILength; though will need to do something about existing
2172          * users of stroke_dash.offset and stroke_dashoffset_set. */
2173     }
2177 static void
2178 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2180     // it may be that this style's SPIPaint has not yet created its URIReference;
2181     // now that we have a document, we can create it here
2182     if (!paint->value.href && document) {
2183         paint->value.href = new SPPaintServerReference(document);
2184         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));
2185     }
2187     if (paint->value.href && paint->value.href->getObject())
2188         paint->value.href->detach();
2190     if (paint->value.href) {
2191         try {
2192             paint->value.href->attach(*uri);
2193         } catch (Inkscape::BadURIException &e) {
2194             g_warning("%s", e.what());
2195             paint->value.href->detach();
2196         }
2197     }
2200 static void
2201 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2203     try {
2204         const Inkscape::URI IURI(uri);
2205         sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2206     } catch (...) {
2207         g_warning("URI failed to parse: %s", uri);
2208     }
2211 void
2212 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2214     sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2217 /**
2218  *
2219  */
2220 static void
2221 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2223     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2224         bool isset = paint->set;
2225         paint->clear();
2226         paint->set = isset;
2227         paint->currentcolor = TRUE;
2228         paint->setColor(style->color.value.color);
2229         return;
2230     }
2232     paint->clear();
2233     if ( parent->isPaintserver() ) {
2234         if (parent->value.href) {
2235             sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2236         } else {
2237             g_warning("Expected paint server not found.");
2238         }
2239     } else if ( parent->isColor() ) {
2240         paint->setColor( parent->value.color );
2241     } else if ( parent->isNoneSet() ) {
2242         paint->noneSet = TRUE;
2243     } else if ( parent->isNone() ) {
2244         //
2245     } else {
2246         g_assert_not_reached();
2247     }
2251 /**
2252  * Merge filter style from parent.
2253  * Filter effects do not inherit by default
2254  */
2255 static void
2256 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2258     // FIXME:
2259     // instead of just copying over, we need to _really merge_ the two filters by combining their
2260     // filter primitives
2262     sp_style_filter_clear(style);
2263     style->filter.set = parent->set;
2264     style->filter.inherit = parent->inherit;
2266     if (style->filter.href && style->filter.href->getObject())
2267        style->filter.href->detach();
2269     // it may be that this style has not yet created its SPFilterReference
2270     if (!style->filter.href && style->object && SP_OBJECT_DOCUMENT(style->object)) {
2271             style->filter.href = new SPFilterReference(SP_OBJECT_DOCUMENT(style->object));
2272             style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2273     }
2275     if (style->filter.href && parent->href && parent->href->getObject()) {
2276         try {
2277             style->filter.href->attach(*parent->href->getURI());
2278         } catch (Inkscape::BadURIException &e) {
2279             g_warning("%s", e.what());
2280             style->filter.href->detach();
2281         }
2282     }
2285 /**
2286  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2287  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2288  * complete cascaded style to style_clipboard. When you need a CSS string
2289  * for an object in the document tree, you normally call
2290  * sp_style_write_difference instead to take into account the object's parent.
2291  *
2292  * \pre style != NULL.
2293  * \pre flags in {IFSET, ALWAYS}.
2294  * \post ret != NULL.
2295  */
2296 gchar *
2297 sp_style_write_string(SPStyle const *const style, guint const flags)
2299     /** \todo
2300      * Merge with write_difference, much duplicate code!
2301      */
2302     g_return_val_if_fail(style != NULL, NULL);
2303     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2304                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2305                          NULL);
2307     gchar c[BMAX];
2308     gchar *p = c;
2309     *p = '\0';
2311     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2312     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2313     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2314     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2315     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2317     /* Text */
2318     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2319     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2320     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2321     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2322     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2323     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2324     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2325     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2326     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2327     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2329     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2330     p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &style->baseline_shift, NULL, flags);
2333     /// \todo fixme: Per type methods need default flag too (lauris)
2335     if (style->opacity.value != SP_SCALE24_MAX) {
2336         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2337     }
2339     if (!style->color.noneSet) { // CSS does not permit "none" for color
2340         p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2341     }
2343     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2344     // if fill:none, skip writing fill properties
2345     if (!style->fill.noneSet) {
2346         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2347         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2348     }
2350     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2352     // stroke width affects markers, so write it if there's stroke OR any markers
2353     if (!style->stroke.noneSet ||
2354         style->marker[SP_MARKER_LOC].set ||
2355         style->marker[SP_MARKER_LOC_START].set ||
2356         style->marker[SP_MARKER_LOC_MID].set ||
2357         style->marker[SP_MARKER_LOC_END].set) {
2358         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2359     }
2361     // if stroke:none, skip writing stroke properties
2362     if (!style->stroke.noneSet) {
2363         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2364         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2365         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2366         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2368         /** \todo fixme: */
2369         if ((flags == SP_STYLE_FLAG_ALWAYS)
2370             || style->stroke_dasharray_set)
2371         {
2372             if (style->stroke_dasharray_inherit) {
2373                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2374             } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2375                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2376                 gint i;
2377                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2378                     Inkscape::CSSOStringStream os;
2379                     if (i) {
2380                         os << ", ";
2381                     }
2382                     os << style->stroke_dash.dash[i];
2383                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2384                 }
2385                 if (p < c + BMAX) {
2386                     *p++ = ';';
2387                 }
2388             } else {
2389                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2390             }
2391         }
2393         /** \todo fixme: */
2394         if (style->stroke_dashoffset_set) {
2395             if (style->stroke_dashoffset_inherit) {
2396                 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2397             } else {
2398                 Inkscape::CSSOStringStream os;
2399                 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2400                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2401             }
2402         } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2403             p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2404         }
2405     }
2407     bool marker_none = false;
2408     gchar *master = style->marker[SP_MARKER_LOC].value;
2409     if (style->marker[SP_MARKER_LOC].set) {
2410         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2411     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2412         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2413         marker_none = true;
2414     }
2415     if (style->marker[SP_MARKER_LOC_START].set
2416        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_START].value))) {
2417         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2418     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2419         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2420     }
2421     if (style->marker[SP_MARKER_LOC_MID].set
2422        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_MID].value))) {
2423         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2424     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2425         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2426     }
2427     if (style->marker[SP_MARKER_LOC_END].set
2428        && (!master || strcmp(master, style->marker[SP_MARKER_LOC_END].value))) {
2429         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2430     } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2431         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2432     }
2434     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2435     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2436     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2438     /* filter: */
2439     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2441     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2443     /* fixme: */
2444     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2446     /* Get rid of trailing `;'. */
2447     if (p != c) {
2448         --p;
2449         if (*p == ';') {
2450             *p = '\0';
2451         }
2452     }
2454     return g_strdup(c);
2458 #define STYLE_BUF_MAX
2461 /**
2462  * Dumps style to CSS string, see sp_style_write_string()
2463  *
2464  * \pre from != NULL.
2465  * \pre to != NULL.
2466  * \post ret != NULL.
2467  */
2468 gchar *
2469 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2471     g_return_val_if_fail(from != NULL, NULL);
2472     g_return_val_if_fail(to != NULL, NULL);
2474     gchar c[BMAX], *p = c;
2475     *p = '\0';
2477     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2478     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2479     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2480     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2481     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2483     /* Text */
2484     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2485     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2486     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2487     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2488     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2489     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2490     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2491     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2492     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2493     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2495     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2496     p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &from->baseline_shift, &to->baseline_shift, SP_STYLE_FLAG_IFDIFF);
2498     /// \todo fixme: Per type methods need default flag too
2499     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2500         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2501     }
2503     if (!from->color.noneSet) { // CSS does not permit "none" for color
2504         p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2505     }
2507     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2508     // if fill:none, skip writing fill properties
2509     if (!from->fill.noneSet) {
2510         p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2511         p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2512     }
2514     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2516     // stroke width affects markers, so write it if there's stroke OR any markers
2517     if (!from->stroke.noneSet ||
2518         from->marker[SP_MARKER_LOC].set ||
2519         from->marker[SP_MARKER_LOC_START].set ||
2520         from->marker[SP_MARKER_LOC_MID].set ||
2521         from->marker[SP_MARKER_LOC_END].set) {
2522         p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2523     }
2525     // if stroke:none, skip writing stroke properties
2526     if (!from->stroke.noneSet) {
2527         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2528                                   &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2529         p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2530                                   &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2531         p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2532                                    &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2533         /** \todo fixme: */
2534         if (from->stroke_dasharray_set) {
2535             if (from->stroke_dasharray_inherit) {
2536                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2537             } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2538                 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2539                 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2540                     Inkscape::CSSOStringStream os;
2541                     if (i) {
2542                         os << ", ";
2543                     }
2544                     os << from->stroke_dash.dash[i];
2545                     p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2546                 }
2547                 p += g_snprintf(p, c + BMAX - p, ";");
2548             }
2549         }
2550         /* fixme: */
2551         if (from->stroke_dashoffset_set) {
2552             if (from->stroke_dashoffset_inherit) {
2553                 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2554             } else {
2555                 Inkscape::CSSOStringStream os;
2556                 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2557                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2558             }
2559         }
2560         p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2561     }
2563     /* markers */
2564     gchar *master = from->marker[SP_MARKER_LOC].value;
2565     if (master != NULL) {
2566         p += g_snprintf(p, c + BMAX - p, "marker:%s;", master);
2567     }
2568     if (from->marker[SP_MARKER_LOC_START].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_START].value))) {
2569         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2570     }
2571     if (from->marker[SP_MARKER_LOC_MID].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_MID].value))) {
2572         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2573     }
2574     if (from->marker[SP_MARKER_LOC_END].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_END].value))) {
2575         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2576     }
2578     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2579     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2580     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2582     /* filter: */
2583     p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2585     p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2587     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2589     /** \todo
2590      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2591      * flag is mainly only for attributes that don't handle explicit unset well.
2592      * We may need to revisit the behaviour of this routine.
2593      */
2595     /* Get rid of trailing `;'. */
2596     if (p != c) {
2597         --p;
2598         if (*p == ';') {
2599             *p = '\0';
2600         }
2601     }
2603     return g_strdup(c);
2608 /**
2609  * Reset all style properties.
2610  */
2611 static void
2612 sp_style_clear(SPStyle *style)
2614     g_return_if_fail(style != NULL);
2616     style->fill.clear();
2617     style->stroke.clear();
2618     sp_style_filter_clear(style);
2620     if (style->fill.value.href) {
2621         delete style->fill.value.href;
2622         style->fill.value.href = NULL;
2623     }
2624     if (style->stroke.value.href) {
2625         delete style->stroke.value.href;
2626         style->stroke.value.href = NULL;
2627     }
2628     if (style->filter.href) {
2629         delete style->filter.href;
2630         style->filter.href = NULL;
2631     }
2633     if (style->stroke_dash.dash) {
2634         g_free(style->stroke_dash.dash);
2635     }
2637     style->stroke_dasharray_inherit = FALSE;
2638     style->stroke_dashoffset_inherit = FALSE;
2640     /** \todo fixme: Do that text manipulation via parents */
2641     SPObject *object = style->object;
2642     SPDocument *document = style->document;
2643     gint const refcount = style->refcount;
2644     SPTextStyle *text = style->text;
2645     unsigned const text_private = style->text_private;
2647     memset(style, 0, sizeof(SPStyle));
2649     style->refcount = refcount;
2650     style->object = object;
2651     style->document = document;
2653     if (document) {
2654         style->filter.href = new SPFilterReference(document);
2655         style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2657         style->fill.value.href = new SPPaintServerReference(document);
2658         style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2660         style->stroke.value.href = new SPPaintServerReference(document);
2661         style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2662     }
2664     style->text = text;
2665     style->text_private = text_private;
2667     style->text->font_specification.set = FALSE;
2668     style->text->font.set = FALSE;
2669     style->text->font_family.set = FALSE;
2671     style->font_size.set = FALSE;
2672     style->font_size.type = SP_FONT_SIZE_LITERAL;
2673     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2674     style->font_size.computed = 12.0;
2675     style->font_style.set = FALSE;
2676     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2677     style->font_variant.set = FALSE;
2678     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2679     style->font_weight.set = FALSE;
2680     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2681     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2682     style->font_stretch.set = FALSE;
2683     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2685     /* text */
2686     style->text_indent.set = FALSE;
2687     style->text_indent.unit = SP_CSS_UNIT_NONE;
2688     style->text_indent.computed = 0.0;
2690     style->text_align.set = FALSE;
2691     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2693     style->text_decoration.set = FALSE;
2694     style->text_decoration.underline = FALSE;
2695     style->text_decoration.overline = FALSE;
2696     style->text_decoration.line_through = FALSE;
2697     style->text_decoration.blink = FALSE;
2699     style->line_height.set = FALSE;
2700     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2701     style->line_height.normal = TRUE;
2702     style->line_height.value = style->line_height.computed = 1.0;
2704     style->letter_spacing.set = FALSE;
2705     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2706     style->letter_spacing.normal = TRUE;
2707     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2709     style->word_spacing.set = FALSE;
2710     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2711     style->word_spacing.normal = TRUE;
2712     style->word_spacing.value = style->word_spacing.computed = 0.0;
2714     style->baseline_shift.set = FALSE;
2715     style->baseline_shift.type = SP_BASELINE_SHIFT_LITERAL;
2716     style->baseline_shift.unit = SP_CSS_UNIT_NONE;
2717     style->baseline_shift.literal = SP_CSS_BASELINE_SHIFT_BASELINE; 
2718     style->baseline_shift.value = 0.0;
2719     style->baseline_shift.computed = 0.0;
2721     style->text_transform.set = FALSE;
2722     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2724     style->direction.set = FALSE;
2725     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2727     style->block_progression.set = FALSE;
2728     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2730     style->writing_mode.set = FALSE;
2731     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2733     style->text_anchor.set = FALSE;
2734     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2737     style->opacity.value = SP_SCALE24_MAX;
2738     style->visibility.set = FALSE;
2739     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2740     style->display.set = FALSE;
2741     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2742     style->overflow.set = FALSE;
2743     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2745     style->color.clear();
2746     style->color.setColor(0.0, 0.0, 0.0);
2748     style->fill.clear();
2749     style->fill.setColor(0.0, 0.0, 0.0);
2750     style->fill_opacity.value = SP_SCALE24_MAX;
2751     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2753     style->stroke.clear();
2754     style->stroke_opacity.value = SP_SCALE24_MAX;
2756     style->stroke_width.set = FALSE;
2757     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2758     style->stroke_width.computed = 1.0;
2760     style->stroke_linecap.set = FALSE;
2761     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2762     style->stroke_linejoin.set = FALSE;
2763     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2765     style->stroke_miterlimit.set = FALSE;
2766     style->stroke_miterlimit.value = 4.0;
2768     style->stroke_dash.n_dash = 0;
2769     style->stroke_dash.dash = NULL;
2770     style->stroke_dash.offset = 0.0;
2772     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2773         g_free(style->marker[i].value);
2774         style->marker[i].set = FALSE;
2775     }
2777     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2778     style->enable_background.set = false;
2779     style->enable_background.inherit = false;
2784 /**
2785  *
2786  */
2787 static void
2788 sp_style_read_dash(SPStyle *style, gchar const *str)
2790     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2791     style->stroke_dasharray_set = TRUE;
2793     if (strcmp(str, "inherit") == 0) {
2794         style->stroke_dasharray_inherit = true;
2795         return;
2796     }
2797     style->stroke_dasharray_inherit = false;
2799     NRVpathDash &dash = style->stroke_dash;
2800     g_free(dash.dash);
2801     dash.dash = NULL;
2803     if (strcmp(str, "none") == 0) {
2804         dash.n_dash = 0;
2805         return;
2806     }
2808     gint n_dash = 0;
2809     gdouble d[64];
2810     gchar *e = NULL;
2812     bool LineSolid=true;
2813     while (e != str && n_dash < 64) {
2814         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2815         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2816         if (d[n_dash] > 0.00000001)
2817             LineSolid = false;
2818         if (e != str) {
2819             n_dash += 1;
2820             str = e;
2821         }
2822         while (str && *str && !isalnum(*str)) str += 1;
2823     }
2825     if (LineSolid) {
2826         dash.n_dash = 0;
2827         return;
2828     }
2830     if (n_dash > 0) {
2831         dash.dash = g_new(gdouble, n_dash);
2832         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2833         dash.n_dash = n_dash;
2834     }
2838 /*#########################
2839 ## SPTextStyle operations
2840 #########################*/
2843 /**
2844  * Return new SPTextStyle object with default settings.
2845  */
2846 static SPTextStyle *
2847 sp_text_style_new()
2849     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2850     ts->refcount = 1;
2851     sp_text_style_clear(ts);
2853     ts->font_specification.value = g_strdup("Sans");
2854     ts->font.value = g_strdup("Sans");
2855     ts->font_family.value = g_strdup("Sans");
2857     return ts;
2861 /**
2862  * Clear text style settings.
2863  */
2864 static void
2865 sp_text_style_clear(SPTextStyle *ts)
2867     ts->font_specification.set = FALSE;
2868     ts->font.set = FALSE;
2869     ts->font_family.set = FALSE;
2874 /**
2875  * Reduce refcount of text style and possibly free it.
2876  */
2877 static SPTextStyle *
2878 sp_text_style_unref(SPTextStyle *st)
2880     st->refcount -= 1;
2882     if (st->refcount < 1) {
2883         g_free(st->font_specification.value);
2884         g_free(st->font.value);
2885         g_free(st->font_family.value);
2886         g_free(st);
2887     }
2889     return NULL;
2894 /**
2895  * Return duplicate of text style.
2896  */
2897 static SPTextStyle *
2898 sp_text_style_duplicate_unset(SPTextStyle *st)
2900     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2901     nt->refcount = 1;
2903     nt->font_specification.value = g_strdup(st->font_specification.value);
2904     nt->font.value = g_strdup(st->font.value);
2905     nt->font_family.value = g_strdup(st->font_family.value);
2907     return nt;
2912 /**
2913  * Write SPTextStyle object into string.
2914  */
2915 static guint
2916 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2918     gint d = 0;
2920     // We do not do diffing for text style
2921     if (flags == SP_STYLE_FLAG_IFDIFF)
2922         flags = SP_STYLE_FLAG_IFSET;
2924     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2925     d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2926     return d;
2931 /* The following sp_tyle_read_* functions ignore invalid values, as per
2932  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2933  *
2934  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2935  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2936  * pairs, in which case SVG's error-handling rules
2937  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2938  */
2941 /**
2942  * Set SPIFloat object from string.
2943  */
2944 static void
2945 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2947     if (!strcmp(str, "inherit")) {
2948         val->set = TRUE;
2949         val->inherit = TRUE;
2950     } else {
2951         gfloat value;
2952         if (sp_svg_number_read_f(str, &value)) {
2953             val->set = TRUE;
2954             val->inherit = FALSE;
2955             val->value = value;
2956         }
2957     }
2962 /**
2963  * Set SPIScale24 object from string.
2964  */
2965 static void
2966 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2968     if (!strcmp(str, "inherit")) {
2969         val->set = TRUE;
2970         val->inherit = TRUE;
2971     } else {
2972         gfloat value;
2973         if (sp_svg_number_read_f(str, &value)) {
2974             val->set = TRUE;
2975             val->inherit = FALSE;
2976             value = CLAMP(value, 0.0, 1.0);
2977             val->value = SP_SCALE24_FROM_FLOAT(value);
2978         }
2979     }
2982 /**
2983  * Reads a style value and performs lookup based on the given style value enumerations.
2984  */
2985 static void
2986 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2987                     bool const can_explicitly_inherit)
2989     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2990         val->set = TRUE;
2991         val->inherit = TRUE;
2992     } else {
2993         for (unsigned i = 0; dict[i].key; i++) {
2994             if (!strcmp(str, dict[i].key)) {
2995                 val->set = TRUE;
2996                 val->inherit = FALSE;
2997                 val->value = dict[i].value;
2998                 /* Save copying for values not needing it */
2999                 val->computed = val->value;
3000                 break;
3001             }
3002         }
3003     }
3008 /**
3009  * Set SPIString object from string.
3010  */
3011 static void
3012 sp_style_read_istring(SPIString *val, gchar const *str)
3014     g_free(val->value);
3016     if (!strcmp(str, "inherit")) {
3017         val->set = TRUE;
3018         val->inherit = TRUE;
3019         val->value = NULL;
3020     } else {
3021         val->set = TRUE;
3022         val->inherit = FALSE;
3023         val->value = g_strdup(str);
3024     }
3029 /**
3030  * Set SPILength object from string.
3031  */
3032 static void
3033 sp_style_read_ilength(SPILength *val, gchar const *str)
3035     if (!strcmp(str, "inherit")) {
3036         val->set = TRUE;
3037         val->inherit = TRUE;
3038     } else {
3039         gdouble value;
3040         gchar *e;
3041         /** \todo fixme: Move this to standard place (Lauris) */
3042         value = g_ascii_strtod(str, &e);
3043         if ((gchar const *) e != str) {
3044             /** \todo
3045              * Allow the number of px per inch to vary (document preferences,
3046              * X server or whatever).  E.g. don't fill in computed here, do
3047              * it at the same time as percentage units are done.
3048              */
3049             if (!*e) {
3050                 /* Userspace */
3051                 val->unit = SP_CSS_UNIT_NONE;
3052                 val->computed = value;
3053             } else if (!strcmp(e, "px")) {
3054                 /* Userspace */
3055                 val->unit = SP_CSS_UNIT_PX;
3056                 val->computed = value;
3057             } else if (!strcmp(e, "pt")) {
3058                 /* Userspace / DEVICESCALE */
3059                 val->unit = SP_CSS_UNIT_PT;
3060                 val->computed = value * PX_PER_PT;
3061             } else if (!strcmp(e, "pc")) {
3062                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
3063                 val->unit = SP_CSS_UNIT_PC;
3064                 val->computed = value * PX_PER_PT * 12;
3065             } else if (!strcmp(e, "mm")) {
3066                 val->unit = SP_CSS_UNIT_MM;
3067                 val->computed = value * PX_PER_MM;
3068             } else if (!strcmp(e, "cm")) {
3069                 val->unit = SP_CSS_UNIT_CM;
3070                 val->computed = value * PX_PER_CM;
3071             } else if (!strcmp(e, "in")) {
3072                 val->unit = SP_CSS_UNIT_IN;
3073                 val->computed = value * PX_PER_IN;
3074             } else if (!strcmp(e, "em")) {
3075                 /* EM square */
3076                 val->unit = SP_CSS_UNIT_EM;
3077                 val->value = value;
3078             } else if (!strcmp(e, "ex")) {
3079                 /* ex square */
3080                 val->unit = SP_CSS_UNIT_EX;
3081                 val->value = value;
3082             } else if (!strcmp(e, "%")) {
3083                 /* Percentage */
3084                 val->unit = SP_CSS_UNIT_PERCENT;
3085                 val->value = value * 0.01;
3086             } else {
3087                 /* Invalid */
3088                 return;
3089             }
3090             val->set = TRUE;
3091             val->inherit = FALSE;
3092         }
3093     }
3096 /**
3097  * Set SPILengthOrNormal object from string.
3098  */
3099 static void
3100 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
3102     if (!strcmp(str, "normal")) {
3103         val->set = TRUE;
3104         val->inherit = FALSE;
3105         val->normal = TRUE;
3106         val->unit = SP_CSS_UNIT_NONE;
3107         val->value = val->computed = 0.0;
3108     } else {
3109         SPILength length;
3110         sp_style_read_ilength(&length, str);
3111         val->set = length.set;
3112         val->inherit = length.inherit;
3113         val->normal = FALSE;
3114         val->unit = length.unit;
3115         val->value = length.value;
3116         val->computed = length.computed;
3117     }
3120 /**
3121  * Set SPITextDecoration object from string.
3122  */
3123 static void
3124 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
3126     if (!strcmp(str, "inherit")) {
3127         val->set = TRUE;
3128         val->inherit = TRUE;
3129     } else if (!strcmp(str, "none")) {
3130         val->set = TRUE;
3131         val->inherit = FALSE;
3132         val->underline = FALSE;
3133         val->overline = FALSE;
3134         val->line_through = FALSE;
3135         val->blink = FALSE;
3136     } else {
3137         bool found_underline = false;
3138         bool found_overline = false;
3139         bool found_line_through = false;
3140         bool found_blink = false;
3141         for ( ; *str ; str++ ) {
3142             if (*str == ' ') continue;
3143             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
3144                 found_underline = true;
3145                 str += 9;
3146             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
3147                 found_overline = true;
3148                 str += 8;
3149             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
3150                 found_line_through = true;
3151                 str += 12;
3152             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
3153                 found_blink = true;
3154                 str += 5;
3155             } else {
3156                 return;  // invalid value
3157             }
3158         }
3159         if (!(found_underline || found_overline || found_line_through || found_blink)) {
3160             return;  // invalid value: empty
3161         }
3162         val->set = TRUE;
3163         val->inherit = FALSE;
3164         val->underline = found_underline;
3165         val->overline = found_overline;
3166         val->line_through = found_line_through;
3167         val->blink = found_blink;
3168     }
3171 /**
3172  * Set SPIPaint object from string containing an integer value.
3173  * \param document Ignored
3174  */
3175 static void
3176 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3178     (void)style; // TODO
3179     (void)document; // TODO
3180     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
3181     if (!strcmp(str, "inherit")) {
3182         paint->set = TRUE;
3183         paint->inherit = TRUE;
3184     } else {
3185         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
3186         if (rgb0 != 0xff) {
3187             paint->setColor(rgb0);
3188             paint->set = TRUE;
3189             paint->inherit = FALSE;
3190         }
3191     }
3195 /**
3196  * Set SPIPaint object from string.
3197  *
3198  * \pre paint == \&style.fill || paint == \&style.stroke.
3199  */
3200 void SPIPaint::read( gchar const *str, SPStyle &style, SPDocument *document )
3202     while (g_ascii_isspace(*str)) {
3203         ++str;
3204     }
3206     clear();
3208     if (streq(str, "inherit")) {
3209         set = TRUE;
3210         inherit = TRUE;
3211     } else {
3212         if ( strneq(str, "url", 3) ) {
3213             gchar *uri = extract_uri( str, &str );
3214             while ( g_ascii_isspace(*str) ) {
3215                 ++str;
3216             }
3217             // TODO check on and comment the comparrison "paint != &style->color".
3218             if ( uri && *uri && (this != &style.color) ) {
3219                 set = TRUE;
3221                 // it may be that this style's SPIPaint has not yet created its URIReference;
3222                 // now that we have a document, we can create it here
3223                 if (!value.href && document) {
3224                     value.href = new SPPaintServerReference(document);
3225                     value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((this == &style.fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), &style));
3226                 }
3228                 // TODO check what this does in light of move away from union
3229                 sp_style_set_ipaint_to_uri_string (&style, this, uri);
3230             }
3231             g_free( uri );
3232         }
3234         if (streq(str, "currentColor") && (this != &style.color)) {
3235             set = TRUE;
3236             currentcolor = TRUE;
3237         } else if (streq(str, "none") && (this != &style.color)) {
3238             set = TRUE;
3239             noneSet = TRUE;
3240         } else {
3241             guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3242             if (rgb0 != 0xff) {
3243                 setColor( rgb0 );
3244                 set = TRUE;
3246                 while (g_ascii_isspace(*str)) {
3247                     ++str;
3248                 }
3249                 if (strneq(str, "icc-color(", 10)) {
3250                     SVGICCColor* tmp = new SVGICCColor();
3251                     if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3252                         delete tmp;
3253                         tmp = 0;
3254                     }
3255                     value.color.icc = tmp;
3256                 }
3257             }
3258         }
3259     }
3264 /**
3265  * Set SPIFontSize object from string.
3266  */
3267 static void
3268 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3270     if (!strcmp(str, "inherit")) {
3271         val->set = TRUE;
3272         val->inherit = TRUE;
3273     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3274         // xx-small, x-small, etc.
3275         for (unsigned i = 0; enum_font_size[i].key; i++) {
3276             if (!strcmp(str, enum_font_size[i].key)) {
3277                 val->set = TRUE;
3278                 val->inherit = FALSE;
3279                 val->type = SP_FONT_SIZE_LITERAL;
3280                 val->value = enum_font_size[i].value;
3281                 return;
3282             }
3283         }
3284         /* Invalid */
3285         return;
3286     } else {
3287         gdouble value;
3288         gchar *e;
3289         /* fixme: Move this to standard place (Lauris) */
3290         value = g_ascii_strtod(str, &e);
3291         if ((gchar const *) e != str) {
3292             if (!*e) {
3293                 /* Userspace */
3294             } else if (!strcmp(e, "px")) {
3295                 /* Userspace */
3296             } else if (!strcmp(e, "pt")) {
3297                 /* Userspace * DEVICESCALE */
3298                 value *= PX_PER_PT;
3299             } else if (!strcmp(e, "pc")) {
3300                 /* 12pt */
3301                 value *= PX_PER_PT * 12.0;
3302             } else if (!strcmp(e, "mm")) {
3303                 value *= PX_PER_MM;
3304             } else if (!strcmp(e, "cm")) {
3305                 value *= PX_PER_CM;
3306             } else if (!strcmp(e, "in")) {
3307                 value *= PX_PER_IN;
3308             } else if (!strcmp(e, "%")) {
3309                 /* Percentage */
3310                 val->set = TRUE;
3311                 val->inherit = FALSE;
3312                 val->type = SP_FONT_SIZE_PERCENTAGE;
3313                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3314                 return;
3315             } else {
3316                 /* Invalid */
3317                 return;
3318             }
3319             /* Length */
3320             val->set = TRUE;
3321             val->inherit = FALSE;
3322             val->type = SP_FONT_SIZE_LENGTH;
3323             val->computed = value;
3324             return;
3325         }
3326     }
3330 /**
3331  * Set SPIBaselineShift object from string.
3332  */
3333 static void
3334 sp_style_read_ibaselineshift(SPIBaselineShift *val, gchar const *str)
3336     if (!strcmp(str, "inherit")) {
3337         val->set = TRUE;
3338         val->inherit = TRUE;
3339     } else if ((*str == 'b') || (*str == 's')) {
3340         // baseline or sub or super
3341         for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
3342             if (!strcmp(str, enum_baseline_shift[i].key)) {
3343                 val->set = TRUE;
3344                 val->inherit = FALSE;
3345                 val->type = SP_BASELINE_SHIFT_LITERAL;
3346                 val->literal = enum_baseline_shift[i].value;
3347                 return;
3348             }
3349         }
3350         /* Invalid */
3351         return;
3352     } else {
3353         SPILength length;
3354         sp_style_read_ilength(&length, str);
3355         val->set      = length.set;
3356         val->inherit  = length.inherit;
3357         val->unit     = length.unit;
3358         val->value    = length.value;
3359         val->computed = length.computed;
3360         if( val->unit == SP_CSS_UNIT_PERCENT ) {
3361             val->type = SP_BASELINE_SHIFT_PERCENTAGE;
3362         } else {
3363             val->type = SP_BASELINE_SHIFT_LENGTH;
3364         }
3365         return;
3366     }
3370 /**
3371  * Set SPIFilter object from string.
3372  */
3373 static void
3374 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3376     SPIFilter *f = &(style->filter);
3377     /* Try all possible values: inherit, none, uri */
3378     if (streq(str, "inherit")) {
3379         f->set = TRUE;
3380         f->inherit = TRUE;
3381         if (f->href && f->href->getObject())
3382             f->href->detach();
3383     } else if(streq(str, "none")) {
3384         f->set = TRUE;
3385         f->inherit = FALSE;
3386         if (f->href && f->href->getObject())
3387            f->href->detach();
3388     } else if (strneq(str, "url", 3)) {
3389         char *uri = extract_uri(str);
3390         if(uri == NULL || uri[0] == '\0') {
3391             g_warning("Specified filter url is empty");
3392             f->set = TRUE;
3393             f->inherit = FALSE;
3394             return;
3395         }
3396         f->set = TRUE;
3397         f->inherit = FALSE;
3398         if (f->href && f->href->getObject())
3399             f->href->detach();
3401         // it may be that this style has not yet created its SPFilterReference;
3402         // now that we have a document, we can create it here
3403         if (!f->href && document) {
3404             f->href = new SPFilterReference(document);
3405             f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3406         }
3408         try {
3409             f->href->attach(Inkscape::URI(uri));
3410         } catch (Inkscape::BadURIException &e) {
3411             g_warning("%s", e.what());
3412             f->href->detach();
3413         }
3414         g_free (uri);
3416     } else {
3417         /* We shouldn't reach this if SVG input is well-formed */
3418         f->set = FALSE;
3419         f->inherit = FALSE;
3420         if (f->href && f->href->getObject())
3421             f->href->detach();
3422     }
3425 /**
3426  * Set SPIEnum object from repr attribute.
3427  */
3428 static void
3429 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3430                     gchar const *key, SPStyleEnum const *dict,
3431                     bool const can_explicitly_inherit)
3433     gchar const *str = repr->attribute(key);
3434     if (str) {
3435         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3436     }
3441 /**
3442  * Set SPILength object from repr attribute.
3443  */
3444 static void
3445 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3447     gchar const *str = repr->attribute(key);
3448     if (str) {
3449         sp_style_read_ilength(val, str);
3450     }
3453 /**
3454  * Set SPIFontSize object from repr attribute.
3455  */
3456 static void
3457 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3459     gchar const *str = repr->attribute(key);
3460     if (str) {
3461         sp_style_read_ifontsize(val, str);
3462     }
3466 /**
3467  * Set SPIBaselineShift object from repr attribute.
3468  */
3469 static void
3470 sp_style_read_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key)
3472     gchar const *str = repr->attribute(key);
3473     if (str) {
3474         sp_style_read_ibaselineshift(val, str);
3475     }
3479 /**
3480  * Set SPIFloat object from repr attribute.
3481  */
3482 static void
3483 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3485     gchar const *str = repr->attribute(key);
3486     if (str) {
3487         sp_style_read_ifloat(val, str);
3488     }
3492 /**
3493  * Write SPIFloat object into string.
3494  */
3495 static gint
3496 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3497                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3499     Inkscape::CSSOStringStream os;
3501     if ((flags & SP_STYLE_FLAG_ALWAYS)
3502         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3503         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3504             && (!base->set || (val->value != base->value))))
3505     {
3506         if (val->inherit) {
3507             return g_snprintf(p, len, "%s:inherit;", key);
3508         } else {
3509             os << key << ":" << val->value << ";";
3510             return g_strlcpy(p, os.str().c_str(), len);
3511         }
3512     }
3513     return 0;
3517 /**
3518  * Write SPIScale24 object into string.
3519  */
3520 static gint
3521 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3522                         SPIScale24 const *const val, SPIScale24 const *const base,
3523                         guint const flags)
3525     Inkscape::CSSOStringStream os;
3527     if ((flags & SP_STYLE_FLAG_ALWAYS)
3528         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3529         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3530             && (!base->set || (val->value != base->value))))
3531     {
3532         if (val->inherit) {
3533             return g_snprintf(p, len, "%s:inherit;", key);
3534         } else {
3535             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3536             return g_strlcpy(p, os.str().c_str(), len);
3537         }
3538     }
3539     return 0;
3543 /**
3544  * Write SPIEnum object into string.
3545  */
3546 static gint
3547 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3548                      SPStyleEnum const *const dict,
3549                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3551     if ((flags & SP_STYLE_FLAG_ALWAYS)
3552         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3553         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3554             && (!base->set || (val->computed != base->computed))))
3555     {
3556         if (val->inherit) {
3557             return g_snprintf(p, len, "%s:inherit;", key);
3558         }
3559         for (unsigned i = 0; dict[i].key; i++) {
3560             if (dict[i].value == static_cast< gint > (val->value) ) {
3561                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3562             }
3563         }
3564     }
3565     return 0;
3570 /**
3571  * Write SPIString object into string.
3572  */
3573 static gint
3574 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3575                        SPIString const *const val, SPIString const *const base, guint const flags)
3577     if ((flags & SP_STYLE_FLAG_ALWAYS)
3578         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3579         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3580             && (!base->set || strcmp(val->value, base->value))))
3581     {
3582         if (val->inherit) {
3583             return g_snprintf(p, len, "%s:inherit;", key);
3584         } else {
3585             gchar *val_quoted = css2_escape_quote(val->value);
3586             if (val_quoted) {
3587                 return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3588                 g_free (val_quoted);
3589             }
3590         }
3591     }
3592     return 0;
3596 /**
3597  *
3598  */
3599 static bool
3600 sp_length_differ(SPILength const *const a, SPILength const *const b)
3602     if (a->unit != b->unit) {
3603         if (a->unit == SP_CSS_UNIT_EM) return true;
3604         if (a->unit == SP_CSS_UNIT_EX) return true;
3605         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3606         if (b->unit == SP_CSS_UNIT_EM) return true;
3607         if (b->unit == SP_CSS_UNIT_EX) return true;
3608         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3609     }
3611     return (a->computed != b->computed);
3616 /**
3617  * Write SPILength object into string.
3618  */
3619 static gint
3620 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3621                        SPILength const *const val, SPILength const *const base, guint const flags)
3623     Inkscape::CSSOStringStream os;
3625     if ((flags & SP_STYLE_FLAG_ALWAYS)
3626         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3627         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3628             && (!base->set || sp_length_differ(val, base))))
3629     {
3630         if (val->inherit) {
3631             return g_snprintf(p, len, "%s:inherit;", key);
3632         } else {
3633             switch (val->unit) {
3634                 case SP_CSS_UNIT_NONE:
3635                     os << key << ":" << val->computed << ";";
3636                     return g_strlcpy(p, os.str().c_str(), len);
3637                     break;
3638                 case SP_CSS_UNIT_PX:
3639                     os << key << ":" << val->computed << "px;";
3640                     return g_strlcpy(p, os.str().c_str(), len);
3641                     break;
3642                 case SP_CSS_UNIT_PT:
3643                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3644                     return g_strlcpy(p, os.str().c_str(), len);
3645                     break;
3646                 case SP_CSS_UNIT_PC:
3647                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3648                     return g_strlcpy(p, os.str().c_str(), len);
3649                     break;
3650                 case SP_CSS_UNIT_MM:
3651                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3652                     return g_strlcpy(p, os.str().c_str(), len);
3653                     break;
3654                 case SP_CSS_UNIT_CM:
3655                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3656                     return g_strlcpy(p, os.str().c_str(), len);
3657                     break;
3658                 case SP_CSS_UNIT_IN:
3659                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3660                     return g_strlcpy(p, os.str().c_str(), len);
3661                     break;
3662                 case SP_CSS_UNIT_EM:
3663                     os << key << ":" << val->value << "em;";
3664                     return g_strlcpy(p, os.str().c_str(), len);
3665                     break;
3666                 case SP_CSS_UNIT_EX:
3667                     os << key << ":" << val->value << "ex;";
3668                     return g_strlcpy(p, os.str().c_str(), len);
3669                     break;
3670                 case SP_CSS_UNIT_PERCENT:
3671                     os << key << ":" << (val->value * 100.0) << "%;";
3672                     return g_strlcpy(p, os.str().c_str(), len);
3673                     break;
3674                 default:
3675                     /* Invalid */
3676                     break;
3677             }
3678         }
3679     }
3680     return 0;
3684 /**
3685  *
3686  */
3687 static bool
3688 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3690     if (a->normal != b->normal) return true;
3691     if (a->normal) return false;
3693     if (a->unit != b->unit) {
3694         if (a->unit == SP_CSS_UNIT_EM) return true;
3695         if (a->unit == SP_CSS_UNIT_EX) return true;
3696         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3697         if (b->unit == SP_CSS_UNIT_EM) return true;
3698         if (b->unit == SP_CSS_UNIT_EX) return true;
3699         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3700     }
3702     return (a->computed != b->computed);
3705 /**
3706  * Write SPILengthOrNormal object into string.
3707  */
3708 static gint
3709 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3710                                SPILengthOrNormal const *const val,
3711                                SPILengthOrNormal const *const base,
3712                                guint const flags)
3714     if ((flags & SP_STYLE_FLAG_ALWAYS)
3715         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3716         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3717             && (!base->set || sp_lengthornormal_differ(val, base))))
3718     {
3719         if (val->normal) {
3720             return g_snprintf(p, len, "%s:normal;", key);
3721         } else {
3722             SPILength length;
3723             length.set = val->set;
3724             length.inherit = val->inherit;
3725             length.unit = val->unit;
3726             length.value = val->value;
3727             length.computed = val->computed;
3728             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3729         }
3730     }
3731     return 0;
3734 /**
3735  *
3736  */
3737 static bool
3738 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3740     return    a->underline != b->underline
3741            || a->overline != b->overline
3742            || a->line_through != b->line_through
3743            || a->blink != b->blink;
3746 /**
3747  * Write SPITextDecoration object into string.
3748  */
3749 static gint
3750 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3751                                SPITextDecoration const *const val,
3752                                SPITextDecoration const *const base,
3753                                guint const flags)
3755     Inkscape::CSSOStringStream os;
3757     if ((flags & SP_STYLE_FLAG_ALWAYS)
3758         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3759         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3760             && (!base->set || sp_textdecoration_differ(val, base))))
3761     {
3762         if (val->inherit) {
3763             return g_snprintf(p, len, "%s:inherit;", key);
3764         } else {
3765             os << key << ":";
3766             if (val->underline || val->overline || val->line_through || val->blink) {
3767                 if (val->underline) os << " underline";
3768                 if (val->overline) os << " overline";
3769                 if (val->line_through) os << " line-through";
3770                 if (val->blink) os << " blink";
3771             } else
3772                 os << "none";
3773             os << ";";
3774             return g_strlcpy(p, os.str().c_str(), len);
3775         }
3776     }
3777     return 0;
3780 /**
3781  *
3782  */
3783 static bool
3784 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3786     if ( (a->isColor() != b->isColor())
3787          || (a->isPaintserver() != b->isPaintserver())
3788          || (a->set != b->set)
3789          || (a->currentcolor != b->currentcolor)
3790          || (a->inherit!= b->inherit) ) {
3791         return true;
3792     }
3794     // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3796     if ( a->isPaintserver() ) {
3797         return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3798     }
3800     if ( a->isColor() ) {
3801         return !( (a->value.color == b->value.color)
3802                  && ((a->value.color.icc == b->value.color.icc)
3803                      || (a->value.color.icc && b->value.color.icc
3804                          && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3805                          && (a->value.color.icc->colors == b->value.color.icc->colors))));
3806     /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3807      * in the string representation. */
3808     }
3810     return false;
3815 /**
3816  * Write SPIPaint object into string.
3817  */
3818 static gint
3819 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3820                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3822     int retval = 0;
3824     if ((flags & SP_STYLE_FLAG_ALWAYS)
3825         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3826         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3827             && (!base->set || sp_paint_differ(paint, base))))
3828     {
3829         CSSOStringStream css;
3831         if (paint->inherit) {
3832             css << "inherit";
3833         } else {
3834             if ( paint->value.href && paint->value.href->getURI() ) {
3835                 const gchar* uri = paint->value.href->getURI()->toString();
3836                 css << "url(" << uri << ")";
3837             }
3839             if ( paint->noneSet ) {
3840                 if ( !css.str().empty() ) {
3841                     css << " ";
3842                 }
3843                 css << "none";
3844             }
3846             if ( paint->currentcolor ) {
3847                 if ( !css.str().empty() ) {
3848                     css << " ";
3849                 }
3850                 css << "currentColor";
3851             }
3853             if ( paint->colorSet && !paint->currentcolor ) {
3854                 if ( !css.str().empty() ) {
3855                     css << " ";
3856                 }
3857                 char color_buf[8];
3858                 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3859                 css << color_buf;
3860             }
3862             if (paint->value.color.icc && !paint->currentcolor) {
3863                 if ( !css.str().empty() ) {
3864                     css << " ";
3865                 }
3866                 css << "icc-color(" << paint->value.color.icc->colorProfile;
3867                 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3868                          iEnd(paint->value.color.icc->colors.end());
3869                      i != iEnd; ++i) {
3870                     css << ", " << *i;
3871                 }
3872                 css << ')';
3873             }
3874         }
3876         if ( !css.str().empty() ) {
3877             retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3878         }
3879     }
3881     return retval;
3885 /**
3886  *
3887  */
3888 static bool
3889 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3891     if (a->type != b->type)
3892         return true;
3893     if (a->type == SP_FONT_SIZE_LENGTH) {
3894         if (a->computed != b->computed)
3895             return true;
3896     } else {
3897         if (a->value != b->value)
3898             return true;
3899     }
3900     return false;
3904 /**
3905  * Write SPIFontSize object into string.
3906  */
3907 static gint
3908 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3909                          SPIFontSize const *const val, SPIFontSize const *const base,
3910                          guint const flags)
3912     if ((flags & SP_STYLE_FLAG_ALWAYS)
3913         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3914         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3915             && (!base->set || sp_fontsize_differ(val, base))))
3916     {
3917         if (val->inherit) {
3918             return g_snprintf(p, len, "%s:inherit;", key);
3919         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3920             for (unsigned i = 0; enum_font_size[i].key; i++) {
3921                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3922                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3923                 }
3924             }
3925         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3926             Inkscape::CSSOStringStream os;
3927             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3928             return g_strlcpy(p, os.str().c_str(), len);
3929         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3930             Inkscape::CSSOStringStream os;
3931             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3932             return g_strlcpy(p, os.str().c_str(), len);
3933         }
3934     }
3935     return 0;
3939 /*
3940  * baseline-shift is relative to parent. The only time it should
3941  * not be written out is if it is zero (or not set).
3942  */
3943 static bool
3944 sp_baseline_shift_notzero(SPIBaselineShift const *const a )
3946     if( a->type == SP_BASELINE_SHIFT_LITERAL ) {
3947         if( a->literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
3948             return false;
3949         }
3950     } else {
3951         if( a->value == 0.0 ) {
3952             return false;
3953         }
3954     }
3955     return true;
3958 /**
3959  * Write SPIBaselineShift object into string.
3960  */
3961 static gint
3962 sp_style_write_ibaselineshift(gchar *p, gint const len, gchar const *key,
3963                               SPIBaselineShift const *const val, SPIBaselineShift const *const base,
3964                               guint const flags)
3966     if ((flags & SP_STYLE_FLAG_ALWAYS)
3967         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3968         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3969             && (!base->set || sp_baseline_shift_notzero(val) )))
3970     {
3971         if (val->inherit) {
3972             return g_snprintf(p, len, "%s:inherit;", key);
3973         } else if (val->type == SP_BASELINE_SHIFT_LITERAL) {
3974             for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
3975                 if (enum_baseline_shift[i].value == static_cast< gint > (val->literal) ) {
3976                     return g_snprintf(p, len, "%s:%s;", key, enum_baseline_shift[i].key);
3977                 }
3978             }
3979         } else if (val->type == SP_BASELINE_SHIFT_LENGTH) {
3980             if( val->unit == SP_CSS_UNIT_EM || val->unit == SP_CSS_UNIT_EX ) {
3981                 Inkscape::CSSOStringStream os;
3982                 os << key << ":" << val->value << (val->unit == SP_CSS_UNIT_EM ? "em;" : "ex;");
3983                 return g_strlcpy(p, os.str().c_str(), len);
3984             } else {
3985                 Inkscape::CSSOStringStream os;
3986                 os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3987                 return g_strlcpy(p, os.str().c_str(), len);
3988             }
3989         } else if (val->type == SP_BASELINE_SHIFT_PERCENTAGE) {
3990             Inkscape::CSSOStringStream os;
3991             os << key << ":" << (val->value * 100.0) << "%;";
3992             return g_strlcpy(p, os.str().c_str(), len);
3993         }
3994     }
3995     return 0;
4000 /**
4001  * Write SPIFilter object into string.
4002  */
4003 static gint
4004 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
4005                          SPIFilter const *const val, SPIFilter const *const base,
4006                          guint const flags)
4008     (void)base; // TODO
4009     if ((flags & SP_STYLE_FLAG_ALWAYS)
4010         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
4011         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
4012     {
4013         if (val->inherit) {
4014             return g_snprintf(p, len, "%s:inherit;", key);
4015         } else if (val->href && val->href->getURI()) {
4016             return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
4017         }
4018     }
4021     return 0;
4024 SPIPaint::SPIPaint() :
4025     set(0),
4026     inherit(0),
4027     currentcolor(0),
4028     colorSet(0),
4029     noneSet(0),
4030     value()
4032     value.color.set( 0 );
4033     value.href = 0;
4036 void SPIPaint::clear()
4038     set = false;
4039     inherit = false;
4040     currentcolor = false;
4041     colorSet = false;
4042     noneSet = false;
4043     value.color.set( 0 );
4044     if ( value.href && value.href->getObject() )
4045     {
4046         value.href->detach();
4047     }
4051 /**
4052  * Clear filter object, and disconnect style from paintserver (if present).
4053  */
4054 static void
4055 sp_style_filter_clear(SPStyle *style)
4057     if (style->filter.href && style->filter.href->getObject())
4058         style->filter.href->detach();
4062 // FIXME: Everything below this line belongs in a different file - css-chemistry?
4064 void
4065 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
4067     Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
4069     if (repr == NULL) return;
4071     SPCSSAttr *css = sp_repr_css_attr_new();
4072     if (linked) {
4073         gchar *val = g_strdup_printf("url(#%s)", linked->getId());
4074         sp_repr_css_set_property(css, property, val);
4075         g_free(val);
4076     } else {
4077         sp_repr_css_unset_property(css, "filter");
4078     }
4080     if (recursive) {
4081         sp_repr_css_change_recursive(repr, css, "style");
4082     } else {
4083         sp_repr_css_change(repr, css, "style");
4084     }
4085     sp_repr_css_attr_unref(css);
4089 /**
4090  * Clear all style property attributes in object.
4091  */
4092 void
4093 sp_style_unset_property_attrs(SPObject *o)
4095     if (!o) return;
4097     SPStyle *style = SP_OBJECT_STYLE(o);
4098     if (!style) return;
4100     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
4101     if (!repr) return;
4103     if (style->opacity.set) {
4104         repr->setAttribute("opacity", NULL);
4105     }
4106     if (style->color.set) {
4107         repr->setAttribute("color", NULL);
4108     }
4109     if (style->fill.set) {
4110         repr->setAttribute("fill", NULL);
4111     }
4112     if (style->fill_opacity.set) {
4113         repr->setAttribute("fill-opacity", NULL);
4114     }
4115     if (style->fill_rule.set) {
4116         repr->setAttribute("fill-rule", NULL);
4117     }
4118     if (style->stroke.set) {
4119         repr->setAttribute("stroke", NULL);
4120     }
4121     if (style->stroke_width.set) {
4122         repr->setAttribute("stroke-width", NULL);
4123     }
4124     if (style->stroke_linecap.set) {
4125         repr->setAttribute("stroke-linecap", NULL);
4126     }
4127     if (style->stroke_linejoin.set) {
4128         repr->setAttribute("stroke-linejoin", NULL);
4129     }
4130     if (style->marker[SP_MARKER_LOC].set) {
4131         repr->setAttribute("marker", NULL);
4132     }
4133     if (style->marker[SP_MARKER_LOC_START].set) {
4134         repr->setAttribute("marker-start", NULL);
4135     }
4136     if (style->marker[SP_MARKER_LOC_MID].set) {
4137         repr->setAttribute("marker-mid", NULL);
4138     }
4139     if (style->marker[SP_MARKER_LOC_END].set) {
4140         repr->setAttribute("marker-end", NULL);
4141     }
4142     if (style->stroke_opacity.set) {
4143         repr->setAttribute("stroke-opacity", NULL);
4144     }
4145     if (style->stroke_dasharray_set) {
4146         repr->setAttribute("stroke-dasharray", NULL);
4147     }
4148     if (style->stroke_dashoffset_set) {
4149         repr->setAttribute("stroke-dashoffset", NULL);
4150     }
4151     if (style->text_private && style->text->font_specification.set) {
4152         repr->setAttribute("-inkscape-font-specification", NULL);
4153     }
4154     if (style->text_private && style->text->font_family.set) {
4155         repr->setAttribute("font-family", NULL);
4156     }
4157     if (style->text_anchor.set) {
4158         repr->setAttribute("text-anchor", NULL);
4159     }
4160     if (style->writing_mode.set) {
4161         repr->setAttribute("writing_mode", NULL);
4162     }
4163     if (style->filter.set) {
4164         repr->setAttribute("filter", NULL);
4165     }
4166     if (style->enable_background.set) {
4167         repr->setAttribute("enable-background", NULL);
4168     }
4171 /**
4172  * \pre style != NULL.
4173  * \pre flags in {IFSET, ALWAYS}.
4174  */
4175 SPCSSAttr *
4176 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
4178     g_return_val_if_fail(style != NULL, NULL);
4179     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4180                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
4181                          NULL);
4182     gchar *style_str = sp_style_write_string(style, flags);
4183     SPCSSAttr *css = sp_repr_css_attr_new();
4184     sp_repr_css_attr_add_from_string(css, style_str);
4185     g_free(style_str);
4186     return css;
4190 /**
4191  * \pre object != NULL
4192  * \pre flags in {IFSET, ALWAYS}.
4193  */
4194 SPCSSAttr *
4195 sp_css_attr_from_object(SPObject *object, guint const flags)
4197     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4198                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
4199                          NULL);
4200     SPStyle const *const style = SP_OBJECT_STYLE(object);
4201     if (style == NULL)
4202         return NULL;
4203     return sp_css_attr_from_style(style, flags);
4206 /**
4207  * Unset any text-related properties
4208  */
4209 SPCSSAttr *
4210 sp_css_attr_unset_text(SPCSSAttr *css)
4212     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
4213     sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
4214     sp_repr_css_set_property(css, "font-size", NULL);
4215     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
4216     sp_repr_css_set_property(css, "font-style", NULL);
4217     sp_repr_css_set_property(css, "font-variant", NULL);
4218     sp_repr_css_set_property(css, "font-weight", NULL);
4219     sp_repr_css_set_property(css, "font-stretch", NULL);
4220     sp_repr_css_set_property(css, "font-family", NULL);
4221     sp_repr_css_set_property(css, "text-indent", NULL);
4222     sp_repr_css_set_property(css, "text-align", NULL);
4223     sp_repr_css_set_property(css, "text-decoration", NULL);
4224     sp_repr_css_set_property(css, "line-height", NULL);
4225     sp_repr_css_set_property(css, "letter-spacing", NULL);
4226     sp_repr_css_set_property(css, "word-spacing", NULL);
4227     sp_repr_css_set_property(css, "text-transform", NULL);
4228     sp_repr_css_set_property(css, "direction", NULL);
4229     sp_repr_css_set_property(css, "block-progression", NULL);
4230     sp_repr_css_set_property(css, "writing-mode", NULL);
4231     sp_repr_css_set_property(css, "text-anchor", NULL);
4232     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
4233     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
4234     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
4235     sp_repr_css_set_property(css, "baseline-shift", NULL);
4237     return css;
4240 bool
4241 is_url(char const *p)
4243     if (p == NULL)
4244         return false;
4245 /** \todo
4246  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
4247  * in property values must start with 'url('.
4248  */
4249     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
4252 /**
4253  * Unset any properties that contain URI values.
4254  *
4255  * Used for storing style that will be reused across documents when carrying
4256  * the referenced defs is impractical.
4257  */
4258 SPCSSAttr *
4259 sp_css_attr_unset_uris(SPCSSAttr *css)
4261 // All properties that may hold <uri> or <paint> according to SVG 1.1
4262     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
4263     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
4264     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
4265     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
4266     if (is_url(sp_repr_css_property(css, "marker", NULL))) sp_repr_css_set_property(css, "marker", NULL);
4267     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
4268     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
4269     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
4270     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
4271     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
4272     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
4274     return css;
4277 /**
4278  * Scale a single-value property.
4279  */
4280 void
4281 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
4282                                   double ex, bool only_with_units = false)
4284     gchar const *w = sp_repr_css_property(css, property, NULL);
4285     if (w) {
4286         gchar *units = NULL;
4287         double wd = g_ascii_strtod(w, &units) * ex;
4288         if (w == units) {// nothing converted, non-numeric value
4289             return;
4290         }
4291         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
4292             // only_with_units, but no units found, so do nothing.
4293             return;
4294         }
4295         Inkscape::CSSOStringStream os;
4296         os << wd << units; // reattach units
4297         sp_repr_css_set_property(css, property, os.str().c_str());
4298     }
4301 /**
4302  * Scale a list-of-values property.
4303  */
4304 void
4305 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
4307     gchar const *string = sp_repr_css_property(css, property, NULL);
4308     if (string) {
4309         Inkscape::CSSOStringStream os;
4310         gchar **a = g_strsplit(string, ",", 10000);
4311         bool first = true;
4312         for (gchar **i = a; i != NULL; i++) {
4313             gchar *w = *i;
4314             if (w == NULL)
4315                 break;
4316             gchar *units = NULL;
4317             double wd = g_ascii_strtod(w, &units) * ex;
4318             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4319                 g_strfreev(a);
4320                 return;
4321             }
4322             if (!first) {
4323                 os << ",";
4324             }
4325             os << wd << units; // reattach units
4326             first = false;
4327         }
4328         sp_repr_css_set_property(css, property, os.str().c_str());
4329         g_strfreev(a);
4330     }
4333 /**
4334  * Scale any properties that may hold <length> by ex.
4335  */
4336 SPCSSAttr *
4337 sp_css_attr_scale(SPCSSAttr *css, double ex)
4339     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4340     sp_css_attr_scale_property_single(css, "stroke-width", ex);
4341     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
4342     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4343     sp_css_attr_scale_property_single(css, "font-size", ex);
4344     sp_css_attr_scale_property_single(css, "kerning", ex);
4345     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4346     sp_css_attr_scale_property_single(css, "word-spacing", ex);
4347     sp_css_attr_scale_property_single(css, "line-height", ex, true);
4349     return css;
4353 /**
4354  * Remove quotes and escapes from a string. Returned value must be g_free'd.
4355  * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4356  * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4357  * it parsed for us. So this function is only used to remove those quotes and for
4358  * presentation attributes, without any unescaping. (XML unescaping
4359  * (&amp; etc) is done by XML parser.)
4360  */
4361 gchar *
4362 attribute_unquote(gchar const *val)
4364     if (val) {
4365         if (*val == '\'' || *val == '"') {
4366             int l = strlen(val);
4367             if (l >= 2) {
4368                 if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
4369                      ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
4370                     return (g_strndup (val+1, l-2));
4371                 }
4372             }
4373         }
4374     }
4376     return (val? g_strdup (val) : NULL);
4379 /**
4380  * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4381  */
4382 gchar *
4383 css2_escape_quote(gchar const *val) {
4385     Glib::ustring t;
4386     bool quote = false;
4387     bool last_was_space = false;
4389     for (gchar const *i = val; *i; i++) {
4390         bool is_space = ( *i == ' ' );
4391         if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4392             // ASCII alphanumeric, - and _ don't require quotes
4393             t.push_back(*i);
4394         } else if ( is_space && !last_was_space ) {
4395             // non-consecutive spaces don't require quotes
4396             t.push_back(*i);
4397         } else if (*i=='\'') {
4398             // single quotes require escaping and quotes
4399             t.push_back('\\');
4400             t.push_back(*i);
4401             quote = true;
4402         } else {
4403             // everything else requires quotes
4404             t.push_back(*i);
4405             quote = true;
4406         }
4407         if (i == val && !g_ascii_isalpha(*i)) {
4408             // a non-ASCII/non-alpha initial character requires quotes
4409             quote = true;
4410         }
4411         last_was_space = is_space;
4412     }
4414     if (last_was_space) {
4415         // a trailing space requires quotes
4416         quote = true;
4417     }
4419     if (quote) {
4420         // we use single quotes so the result can be stored in an XML
4421         // attribute without incurring un-aesthetic additional quoting
4422         // (our XML emitter always uses double quotes)
4423         t.insert(t.begin(), '\'');
4424         t.push_back('\'');
4425     }
4427     return (t.empty() ? NULL : g_strdup (t.c_str()));
4430 /*
4431   Local Variables:
4432   mode:c++
4433   c-file-style:"stroustrup"
4434   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4435   indent-tabs-mode:nil
4436   fill-column:99
4437   End:
4438 */
4439 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :