Code

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