Code

#include svg/svg-color.h instead of or as well as (as appropriate) svg/svg.h.
[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"
28 #include "display/canvas-bpath.h"
29 #include "attributes.h"
30 #include "document.h"
31 #include "extract-uri.h"
32 #include "marker-status.h"
33 #include "uri-references.h"
34 #include "sp-paint-server.h"
35 #include "streq.h"
36 #include "strneq.h"
37 #include "style.h"
38 #include "svg/css-ostringstream.h"
39 #include "xml/repr.h"
40 #include "unit-constants.h"
41 #include "isnan.h"
43 namespace Inkscape {
45 /**
46  * Parses a CSS url() specification; temporary hack until
47  * style stuff is redone.
48  * \param string the CSS string to parse
49  * \return a newly-allocated URL string (or NULL); free with g_free()
50  */
51 gchar *parse_css_url(gchar const *string) {
52     if (!string)
53         return NULL;
55     gchar const *iter = string;
56     for ( ; g_ascii_isspace(*iter) ; iter = g_utf8_next_char(iter) );
57     if (strncmp(iter, "url(", 4))
58         return NULL;
59     iter += 4;
61     gchar const end_char = *iter;
62     if ( *iter == '"' || *iter == '\'' ) {
63         iter += 1;
64     }
66     GString *temp = g_string_new(NULL);
67     for ( ; *iter ; iter = g_utf8_next_char(iter) ) {
68         if ( *iter == '(' || *iter == ')'  ||
69              *iter == '"' || *iter == '\'' ||
70              g_ascii_isspace(*iter)        ||
71              g_ascii_iscntrl(*iter)           )
72         {
73             break;
74         }
75         if ( *iter == '\\' ) {
76             iter = g_utf8_next_char(iter);
77         }
78         if ( *iter & (gchar)0x80 ) {
79             break;
80         } else {
81             g_string_append_c(temp, *iter);
82         }
83     }
85     if ( *iter == end_char && end_char != ')' ) {
86         iter = g_utf8_next_char(iter);
87     }
88     gchar *result;
89     if ( *iter == ')' ) {
90         result = temp->str;
91         g_string_free(temp, FALSE);
92     } else {
93         result = NULL;
94         g_string_free(temp, TRUE);
95     }
97     return result;
98 }
102 #define BMAX 8192
104 class SPStyleEnum;
106 /*#########################
107 ## FORWARD DECLARATIONS
108 #########################*/
109 static void sp_style_clear(SPStyle *style);
111 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
113 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
114 static void sp_style_read_dash(SPStyle *style, gchar const *str);
116 static SPTextStyle *sp_text_style_new(void);
117 static void sp_text_style_clear(SPTextStyle *ts);
118 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
119 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
120 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
121 static void sp_style_privatize_text(SPStyle *style);
123 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
124 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
125 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
126 static void sp_style_read_istring(SPIString *val, gchar const *str);
127 static void sp_style_read_ilength(SPILength *val, gchar const *str);
128 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
129 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
130 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
131 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
132 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
134 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
135 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
136 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
137 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
139 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
140 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
141 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
142 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
143 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
144 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
145 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
146 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);
147 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);
149 void css2_unescape_unquote (SPIString *val);
151 static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint, unsigned hunref, unsigned unset);
153 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
154 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
156 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
157 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
159 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
161 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
162 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
164 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
166 struct SPStyleEnum {
167     gchar const *key;
168     gint value;
169 };
171 static SPStyleEnum const enum_fill_rule[] = {
172     {"nonzero", SP_WIND_RULE_NONZERO},
173     {"evenodd", SP_WIND_RULE_EVENODD},
174     {NULL, -1}
175 };
177 static SPStyleEnum const enum_stroke_linecap[] = {
178     {"butt", SP_STROKE_LINECAP_BUTT},
179     {"round", SP_STROKE_LINECAP_ROUND},
180     {"square", SP_STROKE_LINECAP_SQUARE},
181     {NULL, -1}
182 };
184 static SPStyleEnum const enum_stroke_linejoin[] = {
185     {"miter", SP_STROKE_LINEJOIN_MITER},
186     {"round", SP_STROKE_LINEJOIN_ROUND},
187     {"bevel", SP_STROKE_LINEJOIN_BEVEL},
188     {NULL, -1}
189 };
191 static SPStyleEnum const enum_font_style[] = {
192     {"normal", SP_CSS_FONT_STYLE_NORMAL},
193     {"italic", SP_CSS_FONT_STYLE_ITALIC},
194     {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
195     {NULL, -1}
196 };
198 static SPStyleEnum const enum_font_size[] = {
199     {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
200     {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
201     {"small", SP_CSS_FONT_SIZE_SMALL},
202     {"medium", SP_CSS_FONT_SIZE_MEDIUM},
203     {"large", SP_CSS_FONT_SIZE_LARGE},
204     {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
205     {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
206     {"smaller", SP_CSS_FONT_SIZE_SMALLER},
207     {"larger", SP_CSS_FONT_SIZE_LARGER},
208     {NULL, -1}
209 };
211 static SPStyleEnum const enum_font_variant[] = {
212     {"normal", SP_CSS_FONT_VARIANT_NORMAL},
213     {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
214     {NULL, -1}
215 };
217 static SPStyleEnum const enum_font_weight[] = {
218     {"100", SP_CSS_FONT_WEIGHT_100},
219     {"200", SP_CSS_FONT_WEIGHT_200},
220     {"300", SP_CSS_FONT_WEIGHT_300},
221     {"400", SP_CSS_FONT_WEIGHT_400},
222     {"500", SP_CSS_FONT_WEIGHT_500},
223     {"600", SP_CSS_FONT_WEIGHT_600},
224     {"700", SP_CSS_FONT_WEIGHT_700},
225     {"800", SP_CSS_FONT_WEIGHT_800},
226     {"900", SP_CSS_FONT_WEIGHT_900},
227     {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
228     {"bold", SP_CSS_FONT_WEIGHT_BOLD},
229     {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
230     {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
231     {NULL, -1}
232 };
234 static SPStyleEnum const enum_font_stretch[] = {
235     {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
236     {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
237     {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
238     {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
239     {"normal", SP_CSS_FONT_STRETCH_NORMAL},
240     {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
241     {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
242     {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
243     {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
244     {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
245     {"wider", SP_CSS_FONT_STRETCH_WIDER},
246     {NULL, -1}
247 };
249 static SPStyleEnum const enum_text_align[] = {
250     {"start", SP_CSS_TEXT_ALIGN_START},
251     {"end", SP_CSS_TEXT_ALIGN_END},
252     {"left", SP_CSS_TEXT_ALIGN_LEFT},
253     {"right", SP_CSS_TEXT_ALIGN_RIGHT},
254     {"center", SP_CSS_TEXT_ALIGN_CENTER},
255     {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
256     {NULL, -1}
257 };
259 static SPStyleEnum const enum_text_transform[] = {
260     {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
261     {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
262     {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
263     {"none", SP_CSS_TEXT_TRANSFORM_NONE},
264     {NULL, -1}
265 };
267 static SPStyleEnum const enum_text_anchor[] = {
268     {"start", SP_CSS_TEXT_ANCHOR_START},
269     {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
270     {"end", SP_CSS_TEXT_ANCHOR_END},
271     {NULL, -1}
272 };
274 static SPStyleEnum const enum_direction[] = {
275     {"ltr", SP_CSS_DIRECTION_LTR},
276     {"rtl", SP_CSS_DIRECTION_RTL},
277     {NULL, -1}
278 };
280 static SPStyleEnum const enum_block_progression[] = {
281     {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
282     {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
283     {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
284     {NULL, -1}
285 };
287 static SPStyleEnum const enum_writing_mode[] = {
288     /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
289      * input file said lr.  We prefer writing lr-tb on the grounds that the spec says the initial
290      * value is lr-tb rather than lr.
291      *
292      * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
293      * sharing enumerators for different strings may be a bug (once we support ecma script).
294      */
295     {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
296     {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
297     {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
298     {"lr", SP_CSS_WRITING_MODE_LR_TB},
299     {"rl", SP_CSS_WRITING_MODE_RL_TB},
300     {"tb", SP_CSS_WRITING_MODE_TB_RL},
301     {NULL, -1}
302 };
304 static SPStyleEnum const enum_visibility[] = {
305     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
306     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
307     {"visible", SP_CSS_VISIBILITY_VISIBLE},
308     {NULL, -1}
309 };
311 static SPStyleEnum const enum_overflow[] = {
312     {"visible", SP_CSS_OVERFLOW_VISIBLE},
313     {"hidden", SP_CSS_OVERFLOW_HIDDEN},
314     {"scroll", SP_CSS_OVERFLOW_SCROLL},
315     {"auto", SP_CSS_OVERFLOW_AUTO},
316     {NULL, -1}
317 };
319 static SPStyleEnum const enum_display[] = {
320     {"none",      SP_CSS_DISPLAY_NONE},
321     {"inline",    SP_CSS_DISPLAY_INLINE},
322     {"block",     SP_CSS_DISPLAY_BLOCK},
323     {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
324     {"run-in",    SP_CSS_DISPLAY_RUN_IN},
325     {"compact",   SP_CSS_DISPLAY_COMPACT},
326     {"marker",    SP_CSS_DISPLAY_MARKER},
327     {"table",     SP_CSS_DISPLAY_TABLE},
328     {"inline-table",  SP_CSS_DISPLAY_INLINE_TABLE},
329     {"table-row-group",    SP_CSS_DISPLAY_TABLE_ROW_GROUP},
330     {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
331     {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
332     {"table-row",     SP_CSS_DISPLAY_TABLE_ROW},
333     {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
334     {"table-column",  SP_CSS_DISPLAY_TABLE_COLUMN},
335     {"table-cell",    SP_CSS_DISPLAY_TABLE_CELL},
336     {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
337     {NULL, -1}
338 };
340 static SPStyleEnum const enum_shape_rendering[] = {
341     {"auto", 0},
342     {"optimizeSpeed", 0},
343     {"crispEdges", 0},
344     {"geometricPrecision", 0},
345     {NULL, -1}
346 };
348 static SPStyleEnum const enum_color_rendering[] = {
349     {"auto", 0},
350     {"optimizeSpeed", 0},
351     {"optimizeQuality", 0},
352     {NULL, -1}
353 };
355 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
357 static SPStyleEnum const enum_text_rendering[] = {
358     {"auto", 0},
359     {"optimizeSpeed", 0},
360     {"optimizeLegibility", 0},
361     {"geometricPrecision", 0},
362     {NULL, -1}
363 };
365 /**
366  * Release callback.
367  */
368 static void
369 sp_style_object_release(SPObject *object, SPStyle *style)
371     style->object = NULL;
377 /**
378  * Returns a new SPStyle object with settings as per sp_style_clear().
379  */
380 SPStyle *
381 sp_style_new()
383     SPStyle *const style = g_new0(SPStyle, 1);
385     style->refcount = 1;
386     style->object = NULL;
387     style->text = sp_text_style_new();
388     style->text_private = TRUE;
390     sp_style_clear(style);
392     style->cloned = false;
393     style->hreffed = false;
394     style->listening = false;
396     return style;
400 /**
401  * Creates a new SPStyle object, and attaches it to the specified SPObject.
402  */
403 SPStyle *
404 sp_style_new_from_object(SPObject *object)
406     g_return_val_if_fail(object != NULL, NULL);
407     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
409     SPStyle *style = sp_style_new();
410     style->object = object;
411     g_signal_connect(G_OBJECT(object), "release", G_CALLBACK(sp_style_object_release), style);
413     if (object && SP_OBJECT_IS_CLONED(object)) {
414         style->cloned = true;
415     }
417     return style;
421 /**
422  * Increase refcount of style.
423  */
424 SPStyle *
425 sp_style_ref(SPStyle *style)
427     g_return_val_if_fail(style != NULL, NULL);
428     g_return_val_if_fail(style->refcount > 0, NULL);
430     style->refcount += 1;
432     return style;
436 /**
437  * Decrease refcount of style with possible destruction.
438  */
439 SPStyle *
440 sp_style_unref(SPStyle *style)
442     g_return_val_if_fail(style != NULL, NULL);
443     g_return_val_if_fail(style->refcount > 0, NULL);
445     style->refcount -= 1;
447     if (style->refcount < 1) {
448         if (style->object)
449             g_signal_handlers_disconnect_matched(G_OBJECT(style->object),
450                                                  G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
451         if (style->text) sp_text_style_unref(style->text);
452         sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
453         sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
454         g_free(style->stroke_dash.dash);
455         g_free(style);
456     }
458     return NULL;
461 /**
462  *  Reads the various style parameters for an object from repr.
463  */
464 static void
465 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
467     g_assert(style != NULL);
468     g_assert(repr != NULL);
469     g_assert(!object || (SP_OBJECT_REPR(object) == repr));
471     sp_style_clear(style);
473     if (object && SP_OBJECT_IS_CLONED(object)) {
474         style->cloned = true;
475     }
477     /* 1. Style itself */
478     gchar const *val = repr->attribute("style");
479     if (val != NULL) {
480         sp_style_merge_from_style_string(style, val);
481     }
483     if (object) {
484         sp_style_merge_from_object_stylesheet(style, object);
485     } else {
486         /** \todo No stylesheet information. Find out under what circumstances
487          * this occurs, and handle accordingly.  (If we really wanted to, we
488          * could probably get stylesheets by going through repr->doc.)
489          */
490     }
492     /* 2. Presentation attributes */
493     /* CSS2 */
494     SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
495     SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
496     SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
497     /* Font */
498     SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
499     SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
500     SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
501     SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
502     SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
503     /* Text (css2 chapter 16) */
504     SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
505     SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
506     if (!style->text_decoration.set) {
507         val = repr->attribute("text-decoration");
508         if (val) {
509             sp_style_read_itextdecoration(&style->text_decoration, val);
510         }
511     }
512     if (!style->line_height.set) {
513         val = repr->attribute("line-height");
514         if (val) {
515             sp_style_read_ilengthornormal(&style->line_height, val);
516         }
517     }
518     if (!style->letter_spacing.set) {
519         val = repr->attribute("letter-spacing");
520         if (val) {
521             sp_style_read_ilengthornormal(&style->letter_spacing, val);
522         }
523     }
524     if (!style->word_spacing.set) {
525         val = repr->attribute("word-spacing");
526         if (val) {
527             sp_style_read_ilengthornormal(&style->word_spacing, val);
528         }
529     }
530     SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
531     SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
532     SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
534     /* SVG */
535     SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
536                             enum_writing_mode, true);
537     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
538                             enum_text_anchor, true);
540     /* opacity */
541     if (!style->opacity.set) {
542         val = repr->attribute("opacity");
543         if (val) {
544             sp_style_read_iscale24(&style->opacity, val);
545         }
546     }
547     /* color */
548     if (!style->color.set) {
549         val = repr->attribute("color");
550         if (val) {
551             sp_style_read_icolor(&style->color, val, style, ( object
552                                                               ? SP_OBJECT_DOCUMENT(object)
553                                                               : NULL ));
554         }
555     }
556     /* fill */
557     if (!style->fill.set) {
558         val = repr->attribute("fill");
559         if (val) {
560             sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
561         }
562     }
563     /* fill-opacity */
564     if (!style->fill_opacity.set) {
565         val = repr->attribute("fill-opacity");
566         if (val) {
567             sp_style_read_iscale24(&style->fill_opacity, val);
568         }
569     }
570     /* fill-rule */
571     SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
572     /* stroke */
573     if (!style->stroke.set) {
574         val = repr->attribute("stroke");
575         if (val) {
576             sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
577         }
578     }
579     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
580     SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
581     SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
582     SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
584     /* markers */
585     if (!style->marker[SP_MARKER_LOC].set) {
586         val = repr->attribute("marker");
587         if (val) {
588             sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
589         }
590     }
591     if (!style->marker[SP_MARKER_LOC_START].set) {
592         val = repr->attribute("marker-start");
593         if (val) {
594             sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
595         }
596     }
597     if (!style->marker[SP_MARKER_LOC_MID].set) {
598         val = repr->attribute("marker-mid");
599         if (val) {
600             sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
601         }
602     }
603     if (!style->marker[SP_MARKER_LOC_END].set) {
604         val = repr->attribute("marker-end");
605         if (val) {
606             sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
607         }
608     }
610     /* stroke-opacity */
611     if (!style->stroke_opacity.set) {
612         val = repr->attribute("stroke-opacity");
613         if (val) {
614             sp_style_read_iscale24(&style->stroke_opacity, val);
615         }
616     }
617     if (!style->stroke_dasharray_set) {
618         val = repr->attribute("stroke-dasharray");
619         if (val) {
620             sp_style_read_dash(style, val);
621         }
622     }
623     if (!style->stroke_dashoffset_set) {
624         /* fixme */
625         val = repr->attribute("stroke-dashoffset");
626         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
627             style->stroke_dashoffset_set = TRUE;
628         } else {
629             style->stroke_dashoffset_set = FALSE;
630         }
631     }
633     /* font-family */
634     if (!style->text_private || !style->text->font_family.set) {
635         val = repr->attribute("font-family");
636         if (val) {
637             if (!style->text_private) sp_style_privatize_text(style);
638             sp_style_read_istring(&style->text->font_family, val);
639             css2_unescape_unquote(&style->text->font_family);
640         }
641     }
643     /* 3. Merge from parent */
644     if (object) {
645         if (object->parent) {
646             sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
647         }
648     } else {
649         if (sp_repr_parent(repr)) {
650             /// \todo fixme: This is not the prettiest thing (Lauris)
651             SPStyle *parent = sp_style_new();
652             sp_style_read(parent, NULL, sp_repr_parent(repr));
653             sp_style_merge_from_parent(style, parent);
654             sp_style_unref(parent);
655         }
656     }
660 /**
661  * Read style properties from object's repr.
662  *
663  * 1. Reset existing object style
664  * 2. Load current effective object style
665  * 3. Load i attributes from immediate parent (which has to be up-to-date)
666  */
667 void
668 sp_style_read_from_object(SPStyle *style, SPObject *object)
670     g_return_if_fail(style != NULL);
671     g_return_if_fail(object != NULL);
672     g_return_if_fail(SP_IS_OBJECT(object));
674     Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
675     g_return_if_fail(repr != NULL);
677     sp_style_read(style, object, repr);
681 /**
682  * Read style properties from repr only.
683  */
684 void
685 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
687     g_return_if_fail(style != NULL);
688     g_return_if_fail(repr != NULL);
690     sp_style_read(style, NULL, repr);
695 /**
696  *
697  */
698 static void
699 sp_style_privatize_text(SPStyle *style)
701     SPTextStyle *text = style->text;
702     style->text = sp_text_style_duplicate_unset(style->text);
703     sp_text_style_unref(text);
704     style->text_private = TRUE;
708 /**
709  * Merge property into style.
710  *
711  * Should be called in order of highest to lowest precedence.
712  * E.g. for a single style string, call from the last declaration to the first,
713  * as CSS says that later declarations override earlier ones.
714  *
715  * \pre val != NULL.
716  */
717 static void
718 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
720     g_return_if_fail(val != NULL);
722     switch (id) {
723         /* CSS2 */
724         /* Font */
725         case SP_PROP_FONT_FAMILY:
726             if (!style->text_private) sp_style_privatize_text(style);
727             if (!style->text->font_family.set) {
728                 sp_style_read_istring(&style->text->font_family, val);
729                 css2_unescape_unquote (&style->text->font_family);
730             }
731             break;
732         case SP_PROP_FONT_SIZE:
733             SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
734             break;
735         case SP_PROP_FONT_SIZE_ADJUST:
736             if (strcmp(val, "none") != 0) {
737                 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
738             }
739             break;
740         case SP_PROP_FONT_STYLE:
741             SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
742             break;
743         case SP_PROP_FONT_VARIANT:
744             SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
745             break;
746         case SP_PROP_FONT_WEIGHT:
747             SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
748             break;
749         case SP_PROP_FONT_STRETCH:
750             SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
751             break;
752         case SP_PROP_FONT:
753             if (!style->text_private) sp_style_privatize_text(style);
754             if (!style->text->font.set) {
755                 g_free(style->text->font.value);
756                 style->text->font.value = g_strdup(val);
757                 style->text->font.set = TRUE;
758                 style->text->font.inherit = (val && !strcmp(val, "inherit"));
759             }
760             break;
761             /* Text */
762         case SP_PROP_TEXT_INDENT:
763             SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
764             break;
765         case SP_PROP_TEXT_ALIGN:
766             SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
767             break;
768         case SP_PROP_TEXT_DECORATION:
769             if (!style->text_decoration.set) {
770                 sp_style_read_itextdecoration(&style->text_decoration, val);
771             }
772             break;
773         case SP_PROP_LINE_HEIGHT:
774             if (!style->line_height.set) {
775                 sp_style_read_ilengthornormal(&style->line_height, val);
776             }
777             break;
778         case SP_PROP_LETTER_SPACING:
779             if (!style->letter_spacing.set) {
780                 sp_style_read_ilengthornormal(&style->letter_spacing, val);
781             }
782             break;
783         case SP_PROP_WORD_SPACING:
784             if (!style->word_spacing.set) {
785                 sp_style_read_ilengthornormal(&style->word_spacing, val);
786             }
787             break;
788         case SP_PROP_TEXT_TRANSFORM:
789             SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
790             break;
791             /* Text (css3) */
792         case SP_PROP_DIRECTION:
793             SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
794             break;
795         case SP_PROP_BLOCK_PROGRESSION:
796             SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
797             break;
798         case SP_PROP_WRITING_MODE:
799             SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
800             break;
801         case SP_PROP_TEXT_ANCHOR:
802             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
803             break;
804             /* Text (unimplemented) */
805         case SP_PROP_TEXT_RENDERING: {
806             /* Ignore the hint. */
807             SPIEnum dummy;
808             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
809             break;
810         }
811         case SP_PROP_ALIGNMENT_BASELINE:
812             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
813             break;
814         case SP_PROP_BASELINE_SHIFT:
815             g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
816             break;
817         case SP_PROP_DOMINANT_BASELINE:
818             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
819             break;
820         case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
821             g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
822             break;
823         case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
824             g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
825             break;
826         case SP_PROP_KERNING:
827             g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
828             break;
829             /* Misc */
830         case SP_PROP_CLIP:
831             g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
832             break;
833         case SP_PROP_COLOR:
834             if (!style->color.set) {
835                 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
836             }
837             break;
838         case SP_PROP_CURSOR:
839             g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
840             break;
841         case SP_PROP_DISPLAY:
842             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
843             break;
844         case SP_PROP_OVERFLOW:
845             /** \todo
846              * FIXME: not supported properly yet, we just read and write it,
847              * but act as if it is always "display".
848              */
849             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
850             break;
851         case SP_PROP_VISIBILITY:
852             SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
853             break;
854             /* SVG */
855             /* Clip/Mask */
856         case SP_PROP_CLIP_PATH:
857             g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
858             break;
859         case SP_PROP_CLIP_RULE:
860             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
861             break;
862         case SP_PROP_MASK:
863             g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
864             break;
865         case SP_PROP_OPACITY:
866             if (!style->opacity.set) {
867                 sp_style_read_iscale24(&style->opacity, val);
868             }
869             break;
870             /* Filter */
871         case SP_PROP_ENABLE_BACKGROUND:
872             g_warning("Unimplemented style property SP_PROP_ENABLE_BACKGROUND: value: %s", val);
873             break;
874         case SP_PROP_FILTER:
875             g_warning("Unimplemented style property SP_PROP_FILTER: value: %s", val);
876             break;
877         case SP_PROP_FLOOD_COLOR:
878             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
879             break;
880         case SP_PROP_FLOOD_OPACITY:
881             g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
882             break;
883         case SP_PROP_LIGHTING_COLOR:
884             g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
885             break;
886             /* Gradient */
887         case SP_PROP_STOP_COLOR:
888             g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
889             break;
890         case SP_PROP_STOP_OPACITY:
891             g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
892             break;
893             /* Interactivity */
894         case SP_PROP_POINTER_EVENTS:
895             g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
896             break;
897             /* Paint */
898         case SP_PROP_COLOR_INTERPOLATION:
899             g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
900             break;
901         case SP_PROP_COLOR_INTERPOLATION_FILTERS:
902             g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
903             break;
904         case SP_PROP_COLOR_PROFILE:
905             g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
906             break;
907         case SP_PROP_COLOR_RENDERING: {
908             /* Ignore the hint. */
909             SPIEnum dummy;
910             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
911             break;
912         }
913         case SP_PROP_FILL:
914             if (!style->fill.set) {
915                 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
916             }
917             break;
918         case SP_PROP_FILL_OPACITY:
919             if (!style->fill_opacity.set) {
920                 sp_style_read_iscale24(&style->fill_opacity, val);
921             }
922             break;
923         case SP_PROP_FILL_RULE:
924             if (!style->fill_rule.set) {
925                 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
926             }
927             break;
928         case SP_PROP_IMAGE_RENDERING: {
929             /* Ignore the hint. */
930             SPIEnum dummy;
931             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
932             break;
933         }
934         case SP_PROP_MARKER:
935             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
936             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
937             marker_status("Setting SP_PROP_MARKER");
938             if (!style->marker[SP_MARKER_LOC].set) {
939                 g_free(style->marker[SP_MARKER_LOC].value);
940                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
941                 style->marker[SP_MARKER_LOC].set = TRUE;
942                 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
943             }
944             break;
946         case SP_PROP_MARKER_START:
947             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
948             marker_status("Setting SP_PROP_MARKER_START");
949             if (!style->marker[SP_MARKER_LOC_START].set) {
950                 g_free(style->marker[SP_MARKER_LOC_START].value);
951                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
952                 style->marker[SP_MARKER_LOC_START].set = TRUE;
953                 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
954             }
955             break;
956         case SP_PROP_MARKER_MID:
957             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
958             marker_status("Setting SP_PROP_MARKER_MID");
959             if (!style->marker[SP_MARKER_LOC_MID].set) {
960                 g_free(style->marker[SP_MARKER_LOC_MID].value);
961                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
962                 style->marker[SP_MARKER_LOC_MID].set = TRUE;
963                 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
964             }
965             break;
966         case SP_PROP_MARKER_END:
967             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
968             marker_status("Setting SP_PROP_MARKER_END");
969             if (!style->marker[SP_MARKER_LOC_END].set) {
970                 g_free(style->marker[SP_MARKER_LOC_END].value);
971                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
972                 style->marker[SP_MARKER_LOC_END].set = TRUE;
973                 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
974             }
975             break;
977         case SP_PROP_SHAPE_RENDERING: {
978             /* Ignore the hint. */
979             SPIEnum dummy;
980             SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
981             break;
982         }
984         case SP_PROP_STROKE:
985             if (!style->stroke.set) {
986                 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
987             }
988             break;
989         case SP_PROP_STROKE_WIDTH:
990             SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
991             break;
992         case SP_PROP_STROKE_DASHARRAY:
993             if (!style->stroke_dasharray_set) {
994                 sp_style_read_dash(style, val);
995             }
996             break;
997         case SP_PROP_STROKE_DASHOFFSET:
998             if (!style->stroke_dashoffset_set) {
999                 /* fixme */
1000                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1001                     style->stroke_dashoffset_set = TRUE;
1002                 } else {
1003                     style->stroke_dashoffset_set = FALSE;
1004                 }
1005             }
1006             break;
1007         case SP_PROP_STROKE_LINECAP:
1008             if (!style->stroke_linecap.set) {
1009                 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1010             }
1011             break;
1012         case SP_PROP_STROKE_LINEJOIN:
1013             if (!style->stroke_linejoin.set) {
1014                 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1015             }
1016             break;
1017         case SP_PROP_STROKE_MITERLIMIT:
1018             if (!style->stroke_miterlimit.set) {
1019                 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1020             }
1021             break;
1022         case SP_PROP_STROKE_OPACITY:
1023             if (!style->stroke_opacity.set) {
1024                 sp_style_read_iscale24(&style->stroke_opacity, val);
1025             }
1026             break;
1028         default:
1029             g_warning("Invalid style property id: %d value: %s", id, val);
1030             break;
1031     }
1034 static void
1035 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1037     /** \todo Ensure that property is lcased, as per
1038      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1039      * Should probably be done in libcroco.
1040      */
1041     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1042     if (prop_idx != SP_ATTR_INVALID) {
1043         /** \todo
1044          * effic: Test whether the property is already set before trying to
1045          * convert to string. Alternatively, set from CRTerm directly rather
1046          * than converting to string.
1047          */
1048         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1049         gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1050         sp_style_merge_property(style, prop_idx, str_value);
1051         g_free(str_value);
1052     }
1055 static void
1056 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1058 #if 0 /* forwards */
1059     for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1060         CRDeclaration *decl = NULL;
1061         cr_prop_list_get_decl(cur, &decl);
1062         sp_style_merge_style_from_decl(style, decl);
1063     }
1064 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1065     if (props) {
1066         sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1067         CRDeclaration *decl = NULL;
1068         cr_prop_list_get_decl(props, &decl);
1069         sp_style_merge_style_from_decl(style, decl);
1070     }
1071 #endif
1074 /**
1075  * \pre decl_list != NULL
1076  */
1077 static void
1078 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1080     if (decl_list->next) {
1081         sp_style_merge_from_decl_list(style, decl_list->next);
1082     }
1083     sp_style_merge_style_from_decl(style, decl_list);
1086 static CRSelEng *
1087 sp_repr_sel_eng()
1089     CRSelEng *const ret = cr_sel_eng_new();
1090     cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1092     /** \todo
1093      * Check whether we need to register any pseudo-class handlers.
1094      * libcroco has its own default handlers for first-child and lang.
1095      *
1096      * We probably want handlers for link and arguably visited (though
1097      * inkscape can't visit links at the time of writing).  hover etc.
1098      * more useful in inkview than the editor inkscape.
1099      *
1100      * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1101      * the following should be honoured, at least by inkview:
1102      * :hover, :active, :focus, :visited, :link.
1103      */
1105     g_assert(ret);
1106     return ret;
1109 static void
1110 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1112     static CRSelEng *sel_eng = NULL;
1113     if (!sel_eng) {
1114         sel_eng = sp_repr_sel_eng();
1115     }
1117     CRPropList *props = NULL;
1118     CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1119                                                                      object->document->style_cascade,
1120                                                                      object->repr,
1121                                                                      &props);
1122     g_return_if_fail(status == CR_OK);
1123     /// \todo Check what errors can occur, and handle them properly.
1124     if (props) {
1125         sp_style_merge_from_props(style, props);
1126         cr_prop_list_destroy(props);
1127     }
1130 /**
1131  * Parses a style="..." string and merges it with an existing SPStyle.
1132  */
1133 void
1134 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1136     /*
1137      * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1138      * ``When CSS styling is used, CSS inline style is specified by including
1139      * semicolon-separated property declarations of the form "name : value"
1140      * within the style attribute''.
1141      *
1142      * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
1143      * Why does it say "including", what else is allowed in the style
1144      * attribute value?
1145      */
1147     CRDeclaration *const decl_list
1148         = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1149     if (decl_list) {
1150         sp_style_merge_from_decl_list(style, decl_list);
1151         cr_declaration_destroy(decl_list);
1152     }
1155 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1156 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1158 static void
1159 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1161     /* 'font-size' */
1162     if (!child.set || child.inherit) {
1163         /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1164         child.computed = parent.computed;
1165     } else if (child.type == SP_FONT_SIZE_LITERAL) {
1166         /** \todo
1167          * fixme: SVG and CSS do not specify clearly, whether we should use
1168          * user or screen coordinates (Lauris)
1169          */
1170         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1171             child.computed = font_size_table[child.value];
1172         } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1173             child.computed = parent.computed / 1.2;
1174         } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1175             child.computed = parent.computed * 1.2;
1176         } else {
1177             /* Illegal value */
1178         }
1179     } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1180         /* Unlike most other lengths, percentage for font size is relative to parent computed value
1181          * rather than viewport. */
1182         child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1183     }
1186 /**
1187  * Sets computed values in \a style, which may involve inheriting from (or in some other way
1188  * calculating from) corresponding computed values of \a parent.
1189  *
1190  * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1191  * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1192  * inherit a value.  http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1193  * about what the computed value is (not obvious for lengths).
1194  *
1195  * \pre \a parent's computed values are already up-to-date.
1196  */
1197 void
1198 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1200     g_return_if_fail(style != NULL);
1202     /** \todo
1203      * fixme: Check for existing callers that might pass null parent.
1204      * This should probably be g_return_if_fail, or else we should make a
1205      * best attempt to set computed values correctly without having a parent
1206      * (i.e., by assuming parent has initial values).
1207      */
1208     if (!parent)
1209         return;
1211     /* CSS2 */
1212     /* Font */
1213     sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1215     /* 'font-style' */
1216     if (!style->font_style.set || style->font_style.inherit) {
1217         style->font_style.computed = parent->font_style.computed;
1218     }
1220     /* 'font-variant' */
1221     if (!style->font_variant.set || style->font_variant.inherit) {
1222         style->font_variant.computed = parent->font_variant.computed;
1223     }
1225     /* 'font-weight' */
1226     if (!style->font_weight.set || style->font_weight.inherit) {
1227         style->font_weight.computed = parent->font_weight.computed;
1228     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1229         /** \todo
1230          * fixme: This is unconditional, i.e., happens even if parent not
1231          * present.
1232          */
1233         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1234     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1235         style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1236     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1237         unsigned const parent_val = parent->font_weight.computed;
1238         g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1239         // strictly, 'bolder' and 'lighter' should go to the next weight
1240         // expressible in the current font family, but that's difficult to
1241         // find out, so jumping by 3 seems an appropriate approximation
1242         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1243                            ? (unsigned)SP_CSS_FONT_WEIGHT_100
1244                            : parent_val - 3);
1245         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1246     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1247         unsigned const parent_val = parent->font_weight.computed;
1248         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1249         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1250                            ? (unsigned)SP_CSS_FONT_WEIGHT_900
1251                            : parent_val + 3);
1252         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1253     }
1255     /* 'font-stretch' */
1256     if (!style->font_stretch.set || style->font_stretch.inherit) {
1257         style->font_stretch.computed = parent->font_stretch.computed;
1258     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1259         unsigned const parent_val = parent->font_stretch.computed;
1260         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1261                         ? parent_val
1262                         : parent_val - 1);
1263         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1264     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1265         unsigned const parent_val = parent->font_stretch.computed;
1266         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1267         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1268                         ? parent_val
1269                         : parent_val + 1);
1270         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1271     }
1273     /* text (css2) */
1274     if (!style->text_indent.set || style->text_indent.inherit) {
1275         style->text_indent.computed = parent->text_indent.computed;
1276     }
1278     if (!style->text_align.set || style->text_align.inherit) {
1279         style->text_align.computed = parent->text_align.computed;
1280     }
1282     if (!style->text_decoration.set || style->text_decoration.inherit) {
1283         style->text_decoration.underline = parent->text_decoration.underline;
1284         style->text_decoration.overline = parent->text_decoration.overline;
1285         style->text_decoration.line_through = parent->text_decoration.line_through;
1286         style->text_decoration.blink = parent->text_decoration.blink;
1287     }
1289     if (!style->line_height.set || style->line_height.inherit) {
1290         style->line_height.computed = parent->line_height.computed;
1291         style->line_height.normal = parent->line_height.normal;
1292     }
1294     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1295         style->letter_spacing.computed = parent->letter_spacing.computed;
1296         style->letter_spacing.normal = parent->letter_spacing.normal;
1297     }
1299     if (!style->word_spacing.set || style->word_spacing.inherit) {
1300         style->word_spacing.computed = parent->word_spacing.computed;
1301         style->word_spacing.normal = parent->word_spacing.normal;
1302     }
1304     if (!style->text_transform.set || style->text_transform.inherit) {
1305         style->text_transform.computed = parent->text_transform.computed;
1306     }
1308     if (!style->direction.set || style->direction.inherit) {
1309         style->direction.computed = parent->direction.computed;
1310     }
1312     if (!style->block_progression.set || style->block_progression.inherit) {
1313         style->block_progression.computed = parent->block_progression.computed;
1314     }
1316     if (!style->writing_mode.set || style->writing_mode.inherit) {
1317         style->writing_mode.computed = parent->writing_mode.computed;
1318     }
1320     if (!style->text_anchor.set || style->text_anchor.inherit) {
1321         style->text_anchor.computed = parent->text_anchor.computed;
1322     }
1324     if (style->opacity.inherit) {
1325         style->opacity.value = parent->opacity.value;
1326     }
1328     /* Color */
1329     if (!style->color.set || style->color.inherit) {
1330         sp_style_merge_ipaint(style, &style->color, &parent->color);
1331     }
1333     /* Fill */
1334     if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1335         sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1336     }
1338     if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1339         style->fill_opacity.value = parent->fill_opacity.value;
1340     }
1342     if (!style->fill_rule.set || style->fill_rule.inherit) {
1343         style->fill_rule.computed = parent->fill_rule.computed;
1344     }
1346     /* Stroke */
1347     if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1348         sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1349     }
1351     if (!style->stroke_width.set || style->stroke_width.inherit) {
1352         style->stroke_width.computed = parent->stroke_width.computed;
1353     } else {
1354         /* Update computed value for any change in font inherited from parent. */
1355         double const em = style->font_size.computed;
1356         if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1357             style->stroke_width.computed = style->stroke_width.value * em;
1358         } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1359             double const ex = em * 0.5;  // fixme: Get x height from libnrtype or pango.
1360             style->stroke_width.computed = style->stroke_width.value * ex;
1361         }
1362     }
1364     if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1365         style->stroke_linecap.computed = parent->stroke_linecap.computed;
1366     }
1368     if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1369         style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1370     }
1372     if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1373         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1374     }
1376     if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1377         /** \todo
1378          * This code looks wrong.  Why does the logic differ from the
1379          * above properties? Similarly dashoffset below.
1380          */
1381         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1382         if (style->stroke_dash.n_dash > 0) {
1383             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1384             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1385         }
1386     }
1388     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1389         style->stroke_dash.offset = parent->stroke_dash.offset;
1390     }
1392     if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1393         style->stroke_opacity.value = parent->stroke_opacity.value;
1394     }
1396     if (style->text && parent->text) {
1397         if (!style->text->font_family.set || style->text->font_family.inherit) {
1398             g_free(style->text->font_family.value);
1399             style->text->font_family.value = g_strdup(parent->text->font_family.value);
1400         }
1401     }
1403     /* Markers - Free the old value and make copy of the new */
1404     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1405         if (!style->marker[i].set || style->marker[i].inherit) {
1406             g_free(style->marker[i].value);
1407             style->marker[i].value = g_strdup(parent->marker[i].value);
1408         }
1409     }
1412 template <typename T>
1413 static void
1414 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1416     if ( ( !(child.set) || child.inherit )
1417          && parent.set && !(parent.inherit) )
1418     {
1419         child = parent;
1420     }
1423 /**
1424  * Copy SPIString from parent to child.
1425  */
1426 static void
1427 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1429     if ( ( !(child.set) || child.inherit )
1430          && parent.set && !(parent.inherit) )
1431     {
1432         g_free(child.value);
1433         child.value = g_strdup(parent.value);
1434         child.set = parent.set;
1435         child.inherit = parent.inherit;
1436     }
1439 static void
1440 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1441                                             SPIPaint &child, SPIPaint const &parent)
1443     /** \todo
1444      * I haven't given this much attention.  See comments below about
1445      * currentColor, colorProfile, and relative URIs.
1446      */
1447     if (!child.set || child.inherit || child.currentcolor) {
1448         sp_style_merge_ipaint(style, &child, &parent);
1449         child.set = parent.set;
1450         child.inherit = parent.inherit;
1451     }
1454 static void
1455 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1456                                                unsigned const max_computed_val,
1457                                                unsigned const smaller_val)
1459     /* We assume that min computed val is 0, contiguous up to max_computed_val,
1460        then zero or more other absolute values, then smaller_val then larger_val. */
1461     unsigned const min_computed_val = 0;
1462     unsigned const larger_val = smaller_val + 1;
1463     g_return_if_fail(min_computed_val < max_computed_val);
1464     g_return_if_fail(max_computed_val < smaller_val);
1466     if (parent.set && !parent.inherit) {
1467         if (!child.set || child.inherit) {
1468             child.value = parent.value;
1469             child.set = parent.set;  // i.e. true
1470             child.inherit = parent.inherit;  // i.e. false
1471         } else if (child.value < smaller_val) {
1472             /* Child has absolute value: leave as is. */
1473         } else if ( ( child.value == smaller_val
1474                       && parent.value == larger_val )
1475                     || ( parent.value == smaller_val
1476                          && child.value == larger_val ) )
1477         {
1478             child.set = false;
1479             /*
1480              * Note that this can result in a change in computed value in the
1481              * rare case that the parent's setting was a no-op (i.e. if the
1482              * parent's parent's computed value was already ultra-condensed or
1483              * ultra-expanded).  However, I'd guess that the change is for the
1484              * better: I'd guess that if the properties were specified
1485              * relatively, then the intent is to counteract parent's effect.
1486              * I.e. I believe this is the best code even in that case.
1487              */
1488         } else if (child.value == parent.value) {
1489             /* Leave as is. */
1490             /** \todo
1491              * It's unclear what to do if style and parent specify the same
1492              * relative directive (narrower or wider).  We can either convert
1493              * to absolute specification or coalesce to a single relative
1494              * request (of half the strength of the original pair).
1495              *
1496              * Converting to a single level of relative specification is a
1497              * better choice if the newly-unlinked clone is itself cloned to
1498              * other contexts (inheriting different font stretchiness): it
1499              * would preserve the effect that it will be narrower than
1500              * the inherited context in each case.  The disadvantage is that
1501              * it will ~certainly affect the computed value of the
1502              * newly-unlinked clone.
1503              */
1504         } else {
1505             unsigned const parent_val = parent.computed;
1506             child.value = ( child.value == smaller_val
1507                             ? ( parent_val == min_computed_val
1508                                 ? parent_val
1509                                 : parent_val - 1 )
1510                             : ( parent_val == max_computed_val
1511                                 ? parent_val
1512                                 : parent_val + 1 ) );
1513             g_assert(child.value <= max_computed_val);
1514             child.inherit = false;
1515             g_assert(child.set);
1516         }
1517     }
1520 template <typename LengthT>
1521 static void
1522 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1523                                              double const parent_child_em_ratio)
1525     if ( ( !(child.set) || child.inherit )
1526          && parent.set && !(parent.inherit) )
1527     {
1528         child = parent;
1529         switch (parent.unit) {
1530             case SP_CSS_UNIT_EM:
1531             case SP_CSS_UNIT_EX:
1532                 child.value *= parent_child_em_ratio;
1533                 /** \todo
1534                  * fixme: Have separate ex ratio parameter.
1535                  * Get x height from libnrtype or pango.
1536                  */
1537                 if (!isFinite(child.value)) {
1538                     child.value = child.computed;
1539                     child.unit = SP_CSS_UNIT_NONE;
1540                 }
1541                 break;
1543             default:
1544                 break;
1545         }
1546     }
1549 static double
1550 get_relative_font_size_frac(SPIFontSize const &font_size)
1552     switch (font_size.type) {
1553         case SP_FONT_SIZE_LITERAL: {
1554             switch (font_size.value) {
1555                 case SP_CSS_FONT_SIZE_SMALLER:
1556                     return 5.0 / 6.0;
1558                 case SP_CSS_FONT_SIZE_LARGER:
1559                     return 6.0 / 5.0;
1561                 default:
1562                     g_assert_not_reached();
1563             }
1564         }
1566         case SP_FONT_SIZE_PERCENTAGE:
1567             return SP_F8_16_TO_FLOAT(font_size.value);
1569         case SP_FONT_SIZE_LENGTH:
1570             g_assert_not_reached();
1571     }
1572     g_assert_not_reached();
1575 /**
1576  * Combine \a style and \a parent style specifications into a single style specification that
1577  * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1578  *
1579  * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1580  * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1581  * trying to retain the same visual appearance once the parent is removed.  Interesting cases are
1582  * when there is unusual interaction with the parent's value (opacity, display) or when the value
1583  * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1584  *
1585  * Doesn't update computed values of \a style.  For correctness, you should subsequently call
1586  * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1587  * style was previously up-to-date wrt \a parent.
1588  *
1589  * \pre \a parent's computed values are already up-to-date.
1590  *   (\a style's computed values needn't be up-to-date.)
1591  */
1592 void
1593 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1595     /** \note
1596      * The general rule for each property is as follows:
1597      *
1598      *   If style is set to an absolute value, then leave it as is.
1599      *
1600      *   Otherwise (i.e. if style has a relative value):
1601      *
1602      *     If parent is set to an absolute value, then set style to the computed value.
1603      *
1604      *     Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1605      */
1607     /* We do font-size first, to ensure that em size is up-to-date. */
1608     /** \todo
1609      * fixme: We'll need to have more font-related things up the top once
1610      * we're getting x-height from pango or libnrtype.
1611      */
1613     /* Some things that allow relative specifications. */
1614     {
1615         /* font-size.  Note that we update the computed font-size of style,
1616            to assist in em calculations later in this function. */
1617         if (parent->font_size.set && !parent->font_size.inherit) {
1618             if (!style->font_size.set || style->font_size.inherit) {
1619                 /* font_size inherits the computed value, so we can use the parent value
1620                  * verbatim. */
1621                 style->font_size = parent->font_size;
1622             } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1623                 /* Child already has absolute size (stored in computed value), so do nothing. */
1624             } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1625                         && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1626                 /* Child already has absolute size, but we ensure that the computed value
1627                    is up-to-date. */
1628                 unsigned const ix = style->font_size.value;
1629                 g_assert(ix < G_N_ELEMENTS(font_size_table));
1630                 style->font_size.computed = font_size_table[ix];
1631             } else {
1632                 /* Child has relative size. */
1633                 double const child_frac(get_relative_font_size_frac(style->font_size));
1634                 style->font_size.set = true;
1635                 style->font_size.inherit = false;
1636                 style->font_size.computed = parent->font_size.computed * child_frac;
1638                 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1639                        && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1640                      || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1641                 {
1642                     /* Absolute value. */
1643                     style->font_size.type = SP_FONT_SIZE_LENGTH;
1644                     /* .value is unused for SP_FONT_SIZE_LENGTH. */
1645                 } else {
1646                     /* Relative value. */
1647                     double const parent_frac(get_relative_font_size_frac(parent->font_size));
1648                     style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1649                     style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1650                 }
1651             }
1652         }
1654         /* 'font-stretch' */
1655         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1656                                                        parent->font_stretch,
1657                                                        SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1658                                                        SP_CSS_FONT_STRETCH_NARROWER);
1660         /* font-weight */
1661         sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1662                                                        parent->font_weight,
1663                                                        SP_CSS_FONT_WEIGHT_900,
1664                                                        SP_CSS_FONT_WEIGHT_LIGHTER);
1665     }
1668     /* Enum values that don't have any relative settings (other than `inherit'). */
1669     {
1670         SPIEnum SPStyle::*const fields[] = {
1671             //nyi: SPStyle::clip_rule,
1672             //nyi: SPStyle::color_interpolation,
1673             //nyi: SPStyle::color_interpolation_filters,
1674             //nyi: SPStyle::color_rendering,
1675             &SPStyle::direction,
1676             &SPStyle::fill_rule,
1677             &SPStyle::font_style,
1678             &SPStyle::font_variant,
1679             //nyi: SPStyle::image_rendering,
1680             //nyi: SPStyle::pointer_events,
1681             //nyi: SPStyle::shape_rendering,
1682             &SPStyle::stroke_linecap,
1683             &SPStyle::stroke_linejoin,
1684             &SPStyle::text_anchor,
1685             //nyi: &SPStyle::text_rendering,
1686             &SPStyle::visibility,
1687             &SPStyle::writing_mode
1688         };
1690         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1691             SPIEnum SPStyle::*const fld = fields[i];
1692             sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1693         }
1694     }
1696     /* A few other simple inheritance properties. */
1697     {
1698         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1699         sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1700         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1702         /** \todo
1703          * We currently treat text-decoration as if it were a simple inherited
1704          * property (fixme). This code may need changing once we do the
1705          * special fill/stroke inheritance mentioned by the spec.
1706          */
1707         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1708                                                                  parent->text_decoration);
1710         //nyi: font-size-adjust,  // <number> | none | inherit
1711         //nyi: glyph-orientation-horizontal,
1712         //nyi: glyph-orientation-vertical,
1713     }
1715     /* Properties that involve length but are easy in other respects. */
1716     {
1717         /* The difficulty with lengths is that font-relative units need adjusting if the font
1718          * varies between parent & child.
1719          *
1720          * Lengths specified in the existing child can stay as they are: its computed font
1721          * specification should stay unchanged, so em & ex lengths should continue to mean the same
1722          * size.
1723          *
1724          * Lengths specified in the dying parent in em or ex need to be scaled according to the
1725          * ratio of em or ex size between parent & child.
1726          */
1727         double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1729         SPILength SPStyle::*const lfields[] = {
1730             &SPStyle::stroke_width,
1731             &SPStyle::text_indent
1732         };
1733         for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1734             SPILength SPStyle::*fld = lfields[i];
1735             sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1736                                                                     parent->*fld,
1737                                                                     parent_child_em_ratio);
1738         }
1740         SPILengthOrNormal SPStyle::*const nfields[] = {
1741             &SPStyle::letter_spacing,
1742             &SPStyle::line_height,
1743             &SPStyle::word_spacing
1744         };
1745         for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1746             SPILengthOrNormal SPStyle::*fld = nfields[i];
1747             sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1748                                                                             parent->*fld,
1749                                                                             parent_child_em_ratio);
1750         }
1752         //nyi: &SPStyle::kerning: length or `auto'
1754         /* fixme: Move stroke-dash and stroke-dash-offset here once they
1755            can accept units. */
1756     }
1758     /* Properties that involve a URI but are easy in other respects. */
1759     {
1760         /** \todo
1761          * Could cause problems if original object was in another document
1762          * and it used a relative URL.  (At the time of writing, we don't
1763          * allow hrefs to other documents, so this isn't a problem yet.)
1764          * Paint properties also allow URIs.
1765          */
1766         //nyi: cursor,   // may involve change in effect, but we can't do much better
1767         //nyi: color-profile,
1769         // Markers (marker-start, marker-mid, marker-end).
1770         for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1771             sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1772         }
1773     }
1775     /* CSS2 */
1776     /* Font */
1778     if (style->text && parent->text) {
1779         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1780                                                      parent->text->font_family);
1781     }
1783     /* Properties that don't inherit by default.  Most of these need special handling. */
1784     {
1785         /*
1786          * opacity's effect is cumulative; we set the new value to the combined effect.  The
1787          * default value for opacity is 1.0, not inherit.  (Note that stroke-opacity and
1788          * fill-opacity are quite different from opacity, and don't need any special handling.)
1789          *
1790          * Cases:
1791          * - parent & child were each previously unset, in which case the effective
1792          *   opacity value is 1.0, and style should remain unset.
1793          * - parent was previously unset (so computed opacity value of 1.0)
1794          *   and child was set to inherit.  The merged child should
1795          *   get a value of 1.0, and shouldn't inherit (lest the new parent
1796          *   has a different opacity value).  Given that opacity's default
1797          *   value is 1.0 (rather than inherit), we might as well have the
1798          *   merged child's opacity be unset.
1799          * - parent was previously unset (so opacity 1.0), and child was set to a number.
1800          *   The merged child should retain its existing settings (though it doesn't matter
1801          *   if we make it unset if that number was 1.0).
1802          * - parent was inherit and child was unset.  Merged child should be set to inherit.
1803          * - parent was inherit and child was inherit.  (We can't in general reproduce this
1804          *   effect (short of introducing a new group), but setting opacity to inherit is rare.)
1805          *   If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1806          *   child's value should be set to the product of the two, i.e. the square of the
1807          *   inherited value, and should not be marked as inherit.  (This decision assumes that it
1808          *   is more important to retain the effective opacity than to retain the inheriting
1809          *   effect, and assumes that the inheriting effect either isn't important enough to create
1810          *   a group or isn't common enough to bother maintaining the code to create a group.)  If
1811          *   the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1812          *   closer to maintaining the effect.
1813          * - parent was inherit and child was set to a numerical value.  If the child's value
1814          *   was 1.0, then the merged child should have the same settings as the parent.
1815          *   If the child's value was 0, then the merged child should also be set to 0.
1816          *   If the child's value was anything else, then we do the same as for the inherit/inherit
1817          *   case above: have the merged child set to the product of the two opacities and not
1818          *   marked as inherit, for the same reasons as for that case.
1819          * - parent was set to a value, and child was unset.  The merged child should have
1820          *   parent's settings.
1821          * - parent was set to a value, and child was inherit.  The merged child should
1822          *   be set to the product, i.e. the square of the parent's value.
1823          * - parent & child are each set to a value.  The merged child should be set to the
1824          *   product.
1825          */
1826         if ( !style->opacity.set
1827              || ( !style->opacity.inherit
1828                   && style->opacity.value == SP_SCALE24_MAX ) )
1829         {
1830             style->opacity = parent->opacity;
1831         } else {
1832             /* Ensure that style's computed value is up-to-date. */
1833             if (style->opacity.inherit) {
1834                 style->opacity.value = parent->opacity.value;
1835             }
1837             /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1838             style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1839                                                   parent->opacity.value);
1841             style->opacity.inherit = (parent->opacity.inherit
1842                                       && style->opacity.inherit
1843                                       && (parent->opacity.value == 0 ||
1844                                           parent->opacity.value == SP_SCALE24_MAX));
1845             style->opacity.set = ( style->opacity.inherit
1846                                    || style->opacity.value < SP_SCALE24_MAX );
1847         }
1849         /* display is in principle similar to opacity, but implementation is easier. */
1850         if ( parent->display.set && !parent->display.inherit
1851                     && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1852             style->display.value = SP_CSS_DISPLAY_NONE;
1853             style->display.set = true;
1854             style->display.inherit = false;
1855         } else if (style->display.inherit) {
1856             style->display.value = parent->display.value;
1857             style->display.set = parent->display.set;
1858             style->display.inherit = parent->display.inherit;
1859         } else {
1860             /* Leave as is.  (display doesn't inherit by default.) */
1861         }
1863         /** \todo
1864          * fixme: Check that we correctly handle all properties that don't
1865          * inherit by default (as shown in
1866          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1867          */
1868     }
1870     /* SPIPaint properties (including color). */
1871     {
1872         /** \todo
1873          * Think about the issues involved if specified as currentColor or
1874          * if specified relative to colorProfile, and if the currentColor or
1875          * colorProfile differs between parent \& child.  See also comments
1876          * elsewhere in this function about URIs.
1877          */
1878         SPIPaint SPStyle::*const fields[] = {
1879             &SPStyle::color,
1880             &SPStyle::fill,
1881             &SPStyle::stroke
1882         };
1883         for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1884             SPIPaint SPStyle::*const fld = fields[i];
1885             sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
1886         }
1887     }
1889     /* Things from SVG 1.2 or CSS3. */
1890     {
1891         /* Note: If we ever support setting string values for text-align then we'd need strdup
1892          * handling here. */
1893         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
1895         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
1896         sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
1897     }
1899     /* Note: this will need length handling once dasharray supports units. */
1900     if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
1901          && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
1902     {
1903         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1904         if (style->stroke_dash.n_dash > 0) {
1905             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1906             memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1907         }
1908         style->stroke_dasharray_set = parent->stroke_dasharray_set;
1909         style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
1910     }
1912     /* Note: this will need length handling once dasharray_offset supports units. */
1913     if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1914         style->stroke_dash.offset = parent->stroke_dash.offset;
1915         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
1916         /* fixme: we don't currently allow stroke-dashoffset to be `inherit'.  TODO: Try to
1917          * represent it as a normal SPILength; though will need to do something about existing
1918          * users of stroke_dash.offset and stroke_dashoffset_set. */
1919     }
1924 /**
1925  * Disconnects from possible fill and stroke paint servers.
1926  */
1927 static void
1928 sp_style_paint_server_release(SPPaintServer *server, SPStyle *style)
1930     if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1931         && (server == style->fill.value.paint.server))
1932     {
1933         sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
1934     }
1936     if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
1937         && (server == style->stroke.value.paint.server))
1938     {
1939         sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
1940     }
1946 /**
1947  * Emit style modified signal on style's object if server is style's fill
1948  * or stroke paint server.
1949  */
1950 static void
1951 sp_style_paint_server_modified(SPPaintServer *server, guint flags, SPStyle *style)
1953     if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1954         && (server == style->fill.value.paint.server))
1955     {
1956         if (style->object) {
1957             /** \todo
1958              * fixme: I do not know, whether it is optimal - we are
1959              * forcing reread of everything (Lauris)
1960              */
1961             /** \todo
1962              * fixme: We have to use object_modified flag, because parent
1963              * flag is only available downstreams.
1964              */
1965             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1966         }
1967     } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
1968                && (server == style->stroke.value.paint.server))
1969     {
1970         if (style->object) {
1971             /// \todo fixme:
1972             style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1973         }
1974     } else {
1975         g_assert_not_reached();
1976     }
1981 /**
1982  *
1983  */
1984 static void
1985 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
1987     sp_style_paint_clear(style, paint, TRUE, FALSE);
1989     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
1990         paint->currentcolor = TRUE;
1991         paint->type = SP_PAINT_TYPE_COLOR;
1992         sp_color_copy(&paint->value.color, &style->color.value.color);
1993         return;
1994     }
1996     paint->type = parent->type;
1997     switch (paint->type) {
1998         case SP_PAINT_TYPE_COLOR:
1999             sp_color_copy(&paint->value.color, &parent->value.color);
2000             break;
2001         case SP_PAINT_TYPE_PAINTSERVER:
2002             paint->value.paint.server = parent->value.paint.server;
2003             paint->value.paint.uri = parent->value.paint.uri;
2004             if (paint->value.paint.server) {
2005                 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
2006                     style->hreffed = true;
2007                     sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2008                 }
2009                 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
2010                     g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
2011                                      G_CALLBACK(sp_style_paint_server_release), style);
2012                     g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
2013                                      G_CALLBACK(sp_style_paint_server_modified), style);
2014                     style->listening = true;
2015                 }
2016             }
2017             break;
2018         case SP_PAINT_TYPE_NONE:
2019             break;
2020         default:
2021             g_assert_not_reached();
2022             break;
2023     }
2027 /**
2028  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2029  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2030  * complete cascaded style to style_clipboard. When you need a CSS string
2031  * for an object in the document tree, you normally call
2032  * sp_style_write_difference instead to take into account the object's parent.
2033  *
2034  * \pre style != NULL.
2035  * \pre flags in {IFSET, ALWAYS}.
2036  * \post ret != NULL.
2037  */
2038 gchar *
2039 sp_style_write_string(SPStyle const *const style, guint const flags)
2041     /** \todo
2042      * Merge with write_difference, much duplicate code!
2043      */
2044     g_return_val_if_fail(style != NULL, NULL);
2045     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2046                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
2047                          NULL);
2049     gchar c[BMAX];
2050     gchar *p = c;
2051     *p = '\0';
2053     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2054     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2055     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2056     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2057     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2059     /* Text */
2060     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2061     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2062     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2063     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2064     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2065     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2066     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2067     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2068     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2069     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2071     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2073     /// \todo fixme: Per type methods need default flag too (lauris)
2074     p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2075     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2076     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2077     p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2078     p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2079     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2080     p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2081     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2082     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2084     marker_status("sp_style_write_string:  Writing markers");
2085     if (style->marker[SP_MARKER_LOC].set) {
2086         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2087     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2088         p += g_snprintf(p, c + BMAX - p, "marker:none;");
2089     }
2090     if (style->marker[SP_MARKER_LOC_START].set) {
2091         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2092     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2093         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2094     }
2095     if (style->marker[SP_MARKER_LOC_MID].set) {
2096         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2097     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2098         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2099     }
2100     if (style->marker[SP_MARKER_LOC_END].set) {
2101         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2102     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2103         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2104     }
2106     p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2108     /** \todo fixme: */
2109     if ((flags == SP_STYLE_FLAG_ALWAYS)
2110         || style->stroke_dasharray_set)
2111     {
2112         if (style->stroke_dasharray_inherit) {
2113             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2114         } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2115             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2116             gint i;
2117             for (i = 0; i < style->stroke_dash.n_dash; i++) {
2118                 Inkscape::CSSOStringStream os;
2119                 if (i) {
2120                     os << ", ";
2121                 }
2122                 os << style->stroke_dash.dash[i];
2123                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2124             }
2125             if (p < c + BMAX) {
2126                 *p++ = ';';
2127             }
2128         } else {
2129             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2130         }
2131     }
2133     /** \todo fixme: */
2134     if (style->stroke_dashoffset_set) {
2135         Inkscape::CSSOStringStream os;
2136         os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2137         p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2138     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2139         p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2140     }
2142     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2144     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2145     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2146     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2148     /* fixme: */
2149     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2151     /* Get rid of trailing `;'. */
2152     if (p != c) {
2153         --p;
2154         if (*p == ';') {
2155             *p = '\0';
2156         }
2157     }
2159     return g_strdup(c);
2163 #define STYLE_BUF_MAX
2166 /**
2167  * Dumps style to CSS string, see sp_style_write_string()
2168  *
2169  * \pre from != NULL.
2170  * \pre to != NULL.
2171  * \post ret != NULL.
2172  */
2173 gchar *
2174 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2176     g_return_val_if_fail(from != NULL, NULL);
2177     g_return_val_if_fail(to != NULL, NULL);
2179     gchar c[BMAX], *p = c;
2180     *p = '\0';
2182     p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2183     p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2184     p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2185     p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2186     p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2188     /* Text */
2189     p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2190     p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2191     p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2192     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2193     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2194     p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2195     p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2196     p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2197     p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2198     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2200     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2202     /// \todo fixme: Per type methods need default flag too
2203     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2204         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2205     }
2206     p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2207     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2208     p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2209     p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2210     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2211     p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2212     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2213                               &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2214     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2215                               &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2216     p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2217                                &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2218     /** \todo fixme: */
2219     if (from->stroke_dasharray_set) {
2220         if (from->stroke_dasharray_inherit) {
2221             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2222         } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2223             p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2224             for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2225                 Inkscape::CSSOStringStream os;
2226                 if (i) {
2227                     os << ", ";
2228                 }
2229                 os << from->stroke_dash.dash[i];
2230                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2231             }
2232             p += g_snprintf(p, c + BMAX - p, ";");
2233         }
2234     }
2235     /* fixme: */
2236     if (from->stroke_dashoffset_set) {
2237         Inkscape::CSSOStringStream os;
2238         os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2239         p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2240     }
2241     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2243     /* markers */
2244     marker_status("sp_style_write_difference:  Writing markers");
2245     if (from->marker[SP_MARKER_LOC].value != NULL) {
2246         p += g_snprintf(p, c + BMAX - p, "marker:%s;",       from->marker[SP_MARKER_LOC].value);
2247     }
2248     if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2249         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2250     }
2251     if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2252         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
2253     }
2254     if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2255         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
2256     }
2258     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2259     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2260     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2262     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2264     /** \todo
2265      * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2266      * flag is mainly only for attributes that don't handle explicit unset well.
2267      * We may need to revisit the behaviour of this routine.
2268      */
2270     /* Get rid of trailing `;'. */
2271     if (p != c) {
2272         --p;
2273         if (*p == ';') {
2274             *p = '\0';
2275         }
2276     }
2278     return g_strdup(c);
2283 /**
2284  * Reset all style properties.
2285  */
2286 static void
2287 sp_style_clear(SPStyle *style)
2289     g_return_if_fail(style != NULL);
2291     sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
2292     sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
2293     if (style->stroke_dash.dash) {
2294         g_free(style->stroke_dash.dash);
2295     }
2297     /** \todo fixme: Do that text manipulation via parents */
2298     SPObject *object = style->object;
2299     gint const refcount = style->refcount;
2300     SPTextStyle *text = style->text;
2301     unsigned const text_private = style->text_private;
2302     memset(style, 0, sizeof(SPStyle));
2303     style->refcount = refcount;
2304     style->object = object;
2305     style->text = text;
2306     style->text_private = text_private;
2307     /* fixme: */
2308     style->text->font.set = FALSE;
2309     style->text->font_family.set = FALSE;
2311     style->font_size.set = FALSE;
2312     style->font_size.type = SP_FONT_SIZE_LITERAL;
2313     style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2314     style->font_size.computed = 12.0;
2315     style->font_style.set = FALSE;
2316     style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2317     style->font_variant.set = FALSE;
2318     style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2319     style->font_weight.set = FALSE;
2320     style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2321     style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2322     style->font_stretch.set = FALSE;
2323     style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2325     /* text */
2326     style->text_indent.set = FALSE;
2327     style->text_indent.unit = SP_CSS_UNIT_NONE;
2328     style->text_indent.computed = 0.0;
2330     style->text_align.set = FALSE;
2331     style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2333     style->text_decoration.set = FALSE;
2334     style->text_decoration.underline = FALSE;
2335     style->text_decoration.overline = FALSE;
2336     style->text_decoration.line_through = FALSE;
2337     style->text_decoration.blink = FALSE;
2339     style->line_height.set = FALSE;
2340     style->line_height.unit = SP_CSS_UNIT_PERCENT;
2341     style->line_height.normal = TRUE;
2342     style->line_height.value = style->line_height.computed = 1.0;
2344     style->letter_spacing.set = FALSE;
2345     style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2346     style->letter_spacing.normal = TRUE;
2347     style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2349     style->word_spacing.set = FALSE;
2350     style->word_spacing.unit = SP_CSS_UNIT_NONE;
2351     style->word_spacing.normal = TRUE;
2352     style->word_spacing.value = style->word_spacing.computed = 0.0;
2354     style->text_transform.set = FALSE;
2355     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2357     style->direction.set = FALSE;
2358     style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2360     style->block_progression.set = FALSE;
2361     style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2363     style->writing_mode.set = FALSE;
2364     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2367     style->text_anchor.set = FALSE;
2368     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2371     style->opacity.value = SP_SCALE24_MAX;
2372     style->visibility.set = FALSE;
2373     style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2374     style->display.set = FALSE;
2375     style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2376     style->overflow.set = FALSE;
2377     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2379     style->color.type = SP_PAINT_TYPE_COLOR;
2380     sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
2382     style->fill.type = SP_PAINT_TYPE_COLOR;
2383     sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
2384     style->fill_opacity.value = SP_SCALE24_MAX;
2385     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2387     style->stroke.type = SP_PAINT_TYPE_NONE;
2388     style->stroke.set = FALSE;
2389     sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
2390     style->stroke_opacity.value = SP_SCALE24_MAX;
2392     style->stroke_width.set = FALSE;
2393     style->stroke_width.unit = SP_CSS_UNIT_NONE;
2394     style->stroke_width.computed = 1.0;
2396     style->stroke_linecap.set = FALSE;
2397     style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2398     style->stroke_linejoin.set = FALSE;
2399     style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2401     style->stroke_miterlimit.set = FALSE;
2402     style->stroke_miterlimit.value = 4.0;
2404     style->stroke_dash.n_dash = 0;
2405     style->stroke_dash.dash = NULL;
2406     style->stroke_dash.offset = 0.0;
2408     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2409         g_free(style->marker[i].value);
2410         style->marker[i].set = FALSE;
2411     }
2416 /**
2417  *
2418  */
2419 static void
2420 sp_style_read_dash(SPStyle *style, gchar const *str)
2422     /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2423     style->stroke_dasharray_set = TRUE;
2425     if (strcmp(str, "inherit") == 0) {
2426         style->stroke_dasharray_inherit = true;
2427         return;
2428     }
2429     style->stroke_dasharray_inherit = false;
2431     NRVpathDash &dash = style->stroke_dash;
2432     g_free(dash.dash);
2433     dash.dash = NULL;
2435     if (strcmp(str, "none") == 0) {
2436         dash.n_dash = 0;
2437         return;
2438     }
2440     gint n_dash = 0;
2441     gdouble d[64];
2442     gchar *e = NULL;
2444     bool LineSolid=true;
2445     while (e != str && n_dash < 64) {
2446         /* TODO: Should allow <length> rather than just a unitless (px) number. */
2447         d[n_dash] = g_ascii_strtod(str, (char **) &e);
2448         if (d[n_dash] > 0.00000001)
2449             LineSolid = false;
2450         if (e != str) {
2451             n_dash += 1;
2452             str = e;
2453         }
2454         while (str && *str && !isalnum(*str)) str += 1;
2455     }
2457     if (LineSolid) {
2458         dash.n_dash = 0;
2459         return;
2460     }
2462     if (n_dash > 0) {
2463         dash.dash = g_new(gdouble, n_dash);
2464         memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2465         dash.n_dash = n_dash;
2466     }
2470 /*#########################
2471 ## SPTextStyle operations
2472 #########################*/
2475 /**
2476  * Return new SPTextStyle object with default settings.
2477  */
2478 static SPTextStyle *
2479 sp_text_style_new()
2481     SPTextStyle *ts = g_new0(SPTextStyle, 1);
2482     ts->refcount = 1;
2483     sp_text_style_clear(ts);
2485     ts->font.value = g_strdup("Bitstream Vera Sans");
2486     ts->font_family.value = g_strdup("Bitstream Vera Sans");
2488     return ts;
2492 /**
2493  * Clear text style settings.
2494  */
2495 static void
2496 sp_text_style_clear(SPTextStyle *ts)
2498     ts->font.set = FALSE;
2499     ts->font_family.set = FALSE;
2504 /**
2505  * Reduce refcount of text style and possibly free it.
2506  */
2507 static SPTextStyle *
2508 sp_text_style_unref(SPTextStyle *st)
2510     st->refcount -= 1;
2512     if (st->refcount < 1) {
2513         g_free(st->font.value);
2514         g_free(st->font_family.value);
2515         g_free(st);
2516     }
2518     return NULL;
2523 /**
2524  * Return duplicate of text style.
2525  */
2526 static SPTextStyle *
2527 sp_text_style_duplicate_unset(SPTextStyle *st)
2529     SPTextStyle *nt = g_new0(SPTextStyle, 1);
2530     nt->refcount = 1;
2532     nt->font.value = g_strdup(st->font.value);
2533     nt->font_family.value = g_strdup(st->font_family.value);
2535     return nt;
2540 /**
2541  * Write SPTextStyle object into string.
2542  */
2543 static guint
2544 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2546     gint d = 0;
2548     // We do not do diffing for text style
2549     if (flags == SP_STYLE_FLAG_IFDIFF)
2550         flags = SP_STYLE_FLAG_IFSET;
2552     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2553     return d;
2558 /* The following sp_tyle_read_* functions ignore invalid values, as per
2559  * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2560  *
2561  * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2562  * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2563  * pairs, in which case SVG's error-handling rules
2564  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2565  */
2568 /**
2569  * Set SPIFloat object from string.
2570  */
2571 static void
2572 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2574     if (!strcmp(str, "inherit")) {
2575         val->set = TRUE;
2576         val->inherit = TRUE;
2577     } else {
2578         gfloat value;
2579         if (sp_svg_number_read_f(str, &value)) {
2580             val->set = TRUE;
2581             val->inherit = FALSE;
2582             val->value = value;
2583         }
2584     }
2589 /**
2590  * Set SPIScale24 object from string.
2591  */
2592 static void
2593 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2595     if (!strcmp(str, "inherit")) {
2596         val->set = TRUE;
2597         val->inherit = TRUE;
2598     } else {
2599         gfloat value;
2600         if (sp_svg_number_read_f(str, &value)) {
2601             val->set = TRUE;
2602             val->inherit = FALSE;
2603             value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
2604             val->value = SP_SCALE24_FROM_FLOAT(value);
2605         }
2606     }
2609 /**
2610  * Reads a style value and performs lookup based on the given style value enumerations.
2611  */
2612 static void
2613 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2614                     bool const can_explicitly_inherit)
2616     if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2617         val->set = TRUE;
2618         val->inherit = TRUE;
2619     } else {
2620         for (unsigned i = 0; dict[i].key; i++) {
2621             if (!strcmp(str, dict[i].key)) {
2622                 val->set = TRUE;
2623                 val->inherit = FALSE;
2624                 val->value = dict[i].value;
2625                 /* Save copying for values not needing it */
2626                 val->computed = val->value;
2627                 break;
2628             }
2629         }
2630     }
2635 /**
2636  * Set SPIString object from string.
2637  */
2638 static void
2639 sp_style_read_istring(SPIString *val, gchar const *str)
2641     g_free(val->value);
2643     if (!strcmp(str, "inherit")) {
2644         val->set = TRUE;
2645         val->inherit = TRUE;
2646         val->value = NULL;
2647     } else {
2648         val->set = TRUE;
2649         val->inherit = FALSE;
2650         val->value = g_strdup(str);
2651     }
2656 /**
2657  * Set SPILength object from string.
2658  */
2659 static void
2660 sp_style_read_ilength(SPILength *val, gchar const *str)
2662     if (!strcmp(str, "inherit")) {
2663         val->set = TRUE;
2664         val->inherit = TRUE;
2665     } else {
2666         gdouble value;
2667         gchar *e;
2668         /** \todo fixme: Move this to standard place (Lauris) */
2669         value = g_ascii_strtod(str, &e);
2670         if ((gchar const *) e != str) {
2671             /** \todo
2672              * Allow the number of px per inch to vary (document preferences,
2673              * X server or whatever).  E.g. don't fill in computed here, do
2674              * it at the same time as percentage units are done.
2675              */
2676             if (!*e) {
2677                 /* Userspace */
2678                 val->unit = SP_CSS_UNIT_NONE;
2679                 val->computed = value;
2680             } else if (!strcmp(e, "px")) {
2681                 /* Userspace */
2682                 val->unit = SP_CSS_UNIT_PX;
2683                 val->computed = value;
2684             } else if (!strcmp(e, "pt")) {
2685                 /* Userspace / DEVICESCALE */
2686                 val->unit = SP_CSS_UNIT_PT;
2687                 val->computed = value * PX_PER_PT;
2688             } else if (!strcmp(e, "pc")) {
2689                 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2690                 val->unit = SP_CSS_UNIT_PC;
2691                 val->computed = value * PX_PER_PT * 12;
2692             } else if (!strcmp(e, "mm")) {
2693                 val->unit = SP_CSS_UNIT_MM;
2694                 val->computed = value * PX_PER_MM;
2695             } else if (!strcmp(e, "cm")) {
2696                 val->unit = SP_CSS_UNIT_CM;
2697                 val->computed = value * PX_PER_CM;
2698             } else if (!strcmp(e, "in")) {
2699                 val->unit = SP_CSS_UNIT_IN;
2700                 val->computed = value * PX_PER_IN;
2701             } else if (!strcmp(e, "em")) {
2702                 /* EM square */
2703                 val->unit = SP_CSS_UNIT_EM;
2704                 val->value = value;
2705             } else if (!strcmp(e, "ex")) {
2706                 /* ex square */
2707                 val->unit = SP_CSS_UNIT_EX;
2708                 val->value = value;
2709             } else if (!strcmp(e, "%")) {
2710                 /* Percentage */
2711                 val->unit = SP_CSS_UNIT_PERCENT;
2712                 val->value = value * 0.01;
2713             } else {
2714                 /* Invalid */
2715                 return;
2716             }
2717             val->set = TRUE;
2718             val->inherit = FALSE;
2719         }
2720     }
2723 /**
2724  * Set SPILengthOrNormal object from string.
2725  */
2726 static void
2727 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2729     if (!strcmp(str, "normal")) {
2730         val->set = TRUE;
2731         val->inherit = FALSE;
2732         val->normal = TRUE;
2733         val->unit = SP_CSS_UNIT_NONE;
2734         val->value = val->computed = 0.0;
2735     } else {
2736         SPILength length;
2737         sp_style_read_ilength(&length, str);
2738         val->set = length.set;
2739         val->inherit = length.inherit;
2740         val->normal = FALSE;
2741         val->unit = length.unit;
2742         val->value = length.value;
2743         val->computed = length.computed;
2744     }
2747 /**
2748  * Set SPITextDecoration object from string.
2749  */
2750 static void
2751 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2753     if (!strcmp(str, "inherit")) {
2754         val->set = TRUE;
2755         val->inherit = TRUE;
2756     } else if (!strcmp(str, "none")) {
2757         val->set = TRUE;
2758         val->inherit = FALSE;
2759         val->underline = FALSE;
2760         val->overline = FALSE;
2761         val->line_through = FALSE;
2762         val->blink = FALSE;
2763     } else {
2764         bool found_underline = false;
2765         bool found_overline = false;
2766         bool found_line_through = false;
2767         bool found_blink = false;
2768         for ( ; *str ; str++ ) {
2769             if (*str == ' ') continue;
2770             if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2771                 found_underline = true;
2772                 str += 9;
2773             } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2774                 found_overline = true;
2775                 str += 8;
2776             } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2777                 found_line_through = true;
2778                 str += 12;
2779             } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2780                 found_blink = true;
2781                 str += 5;
2782             } else {
2783                 return;  // invalid value
2784             }
2785         }
2786         if (!(found_underline || found_overline || found_line_through || found_blink)) {
2787             return;  // invalid value: empty
2788         }
2789         val->set = TRUE;
2790         val->inherit = FALSE;
2791         val->underline = found_underline;
2792         val->overline = found_overline;
2793         val->line_through = found_line_through;
2794         val->blink = found_blink;
2795     }
2798 /**
2799  * Set SPIPaint object from string containing an integer value.
2800  * \param document Ignored
2801  */
2802 static void
2803 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2805     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
2806     if (!strcmp(str, "inherit")) {
2807         paint->set = TRUE;
2808         paint->inherit = TRUE;
2809     } else {
2810         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2811         if (rgb0 != 0xff) {
2812             paint->type = SP_PAINT_TYPE_COLOR;
2813             sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2814             paint->set = TRUE;
2815             paint->inherit = FALSE;
2816         }
2817     }
2821 /**
2822  * Set SPIPaint object from string.
2823  */
2824 static void
2825 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2827     while (isspace(*str)) {
2828         ++str;
2829     }
2831     if (streq(str, "inherit")) {
2832         paint->set = TRUE;
2833         paint->inherit = TRUE;
2834         paint->currentcolor = FALSE;
2835     } else if (streq(str, "currentColor")) {
2836         paint->set = TRUE;
2837         paint->inherit = FALSE;
2838         paint->currentcolor = TRUE;
2839     } else if (streq(str, "none")) {
2840         paint->type = SP_PAINT_TYPE_NONE;
2841         paint->set = TRUE;
2842         paint->inherit = FALSE;
2843         paint->currentcolor = FALSE;
2844     } else if (strneq(str, "url", 3)) {
2845         // this is alloc'd uri, but seems to be shared with a parent
2846         // potentially, so it's not any easy g_free case...
2847         paint->value.paint.uri = extract_uri(str);
2848         if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
2849             paint->type = SP_PAINT_TYPE_NONE;
2850             return;
2851         }
2852         paint->type = SP_PAINT_TYPE_PAINTSERVER;
2853         paint->set = TRUE;
2854         paint->inherit = FALSE;
2855         paint->currentcolor = FALSE;
2856         if (document) {
2857             SPObject *ps = sp_uri_reference_resolve(document, str);
2858             if (ps && SP_IS_PAINT_SERVER(ps)) {
2859                 paint->value.paint.server = SP_PAINT_SERVER(ps);
2860                 if (style->object && !style->cloned) {
2861                     sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2862                     style->hreffed = true;
2863                 }
2864                 if (style->object || style->cloned) {
2865                     g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
2866                                      G_CALLBACK(sp_style_paint_server_release), style);
2867                     g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
2868                                      G_CALLBACK(sp_style_paint_server_modified), style);
2869                     style->listening = true;
2870                 }
2871             } else {
2872                 paint->value.paint.server = NULL;
2873             }
2874         }
2875     } else {
2876         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2877         if (rgb0 != 0xff) {
2878             paint->type = SP_PAINT_TYPE_COLOR;
2879             sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2880             paint->set = TRUE;
2881             paint->inherit = FALSE;
2882             paint->currentcolor = FALSE;
2883         }
2884     }
2889 /**
2890  * Set SPIFontSize object from string.
2891  */
2892 static void
2893 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
2895     if (!strcmp(str, "inherit")) {
2896         val->set = TRUE;
2897         val->inherit = TRUE;
2898     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
2899         for (unsigned i = 0; enum_font_size[i].key; i++) {
2900             if (!strcmp(str, enum_font_size[i].key)) {
2901                 val->set = TRUE;
2902                 val->inherit = FALSE;
2903                 val->type = SP_FONT_SIZE_LITERAL;
2904                 val->value = enum_font_size[i].value;
2905                 return;
2906             }
2907         }
2908         /* Invalid */
2909         return;
2910     } else {
2911         gdouble value;
2912         gchar *e;
2913         /* fixme: Move this to standard place (Lauris) */
2914         value = g_ascii_strtod(str, &e);
2915         if ((gchar const *) e != str) {
2916             if (!*e) {
2917                 /* Userspace */
2918             } else if (!strcmp(e, "px")) {
2919                 /* Userspace */
2920             } else if (!strcmp(e, "pt")) {
2921                 /* Userspace * DEVICESCALE */
2922                 value *= PX_PER_PT;
2923             } else if (!strcmp(e, "pc")) {
2924                 /* 12pt */
2925                 value *= PX_PER_PT * 12.0;
2926             } else if (!strcmp(e, "mm")) {
2927                 value *= PX_PER_MM;
2928             } else if (!strcmp(e, "cm")) {
2929                 value *= PX_PER_CM;
2930             } else if (!strcmp(e, "in")) {
2931                 value *= PX_PER_IN;
2932             } else if (!strcmp(e, "%")) {
2933                 /* Percentage */
2934                 val->set = TRUE;
2935                 val->inherit = FALSE;
2936                 val->type = SP_FONT_SIZE_PERCENTAGE;
2937                 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
2938                 return;
2939             } else {
2940                 /* Invalid */
2941                 return;
2942             }
2943             /* Length */
2944             val->set = TRUE;
2945             val->inherit = FALSE;
2946             val->type = SP_FONT_SIZE_LENGTH;
2947             val->computed = value;
2948             return;
2949         }
2950     }
2955 /**
2956  * Set SPIEnum object from repr attribute.
2957  */
2958 static void
2959 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
2960                     gchar const *key, SPStyleEnum const *dict,
2961                     bool const can_explicitly_inherit)
2963     gchar const *str = repr->attribute(key);
2964     if (str) {
2965         sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
2966     }
2971 /**
2972  * Set SPILength object from repr attribute.
2973  */
2974 static void
2975 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
2977     gchar const *str = repr->attribute(key);
2978     if (str) {
2979         sp_style_read_ilength(val, str);
2980     }
2983 /**
2984  * Set SPIFontSize object from repr attribute.
2985  */
2986 static void
2987 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
2989     gchar const *str = repr->attribute(key);
2990     if (str) {
2991         sp_style_read_ifontsize(val, str);
2992     }
2996 /**
2997  * Set SPIFloat object from repr attribute.
2998  */
2999 static void
3000 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3002     gchar const *str = repr->attribute(key);
3003     if (str) {
3004         sp_style_read_ifloat(val, str);
3005     }
3009 /**
3010  * Write SPIFloat object into string.
3011  */
3012 static gint
3013 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3014                       SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3016     Inkscape::CSSOStringStream os;
3018     if ((flags & SP_STYLE_FLAG_ALWAYS)
3019         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3020         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3021             && (!base->set || (val->value != base->value))))
3022     {
3023         if (val->inherit) {
3024             return g_snprintf(p, len, "%s:inherit;", key);
3025         } else {
3026             os << key << ":" << val->value << ";";
3027             return g_strlcpy(p, os.str().c_str(), len);
3028         }
3029     }
3030     return 0;
3034 /**
3035  * Write SPIScale24 object into string.
3036  */
3037 static gint
3038 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3039                         SPIScale24 const *const val, SPIScale24 const *const base,
3040                         guint const flags)
3042     Inkscape::CSSOStringStream os;
3044     if ((flags & SP_STYLE_FLAG_ALWAYS)
3045         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3046         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3047             && (!base->set || (val->value != base->value))))
3048     {
3049         if (val->inherit) {
3050             return g_snprintf(p, len, "%s:inherit;", key);
3051         } else {
3052             os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3053             return g_strlcpy(p, os.str().c_str(), len);
3054         }
3055     }
3056     return 0;
3060 /**
3061  * Write SPIEnum object into string.
3062  */
3063 static gint
3064 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3065                      SPStyleEnum const *const dict,
3066                      SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3068     if ((flags & SP_STYLE_FLAG_ALWAYS)
3069         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3070         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3071             && (!base->set || (val->computed != base->computed))))
3072     {
3073         if (val->inherit) {
3074             return g_snprintf(p, len, "%s:inherit;", key);
3075         }
3076         for (unsigned i = 0; dict[i].key; i++) {
3077             if (dict[i].value == static_cast< gint > (val->value) ) {
3078                 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3079             }
3080         }
3081     }
3082     return 0;
3087 /**
3088  * Write SPIString object into string.
3089  */
3090 static gint
3091 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3092                        SPIString const *const val, SPIString const *const base, guint const flags)
3094     if ((flags & SP_STYLE_FLAG_ALWAYS)
3095         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3096         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3097             && (!base->set || strcmp(val->value, base->value))))
3098     {
3099         if (val->inherit) {
3100             return g_snprintf(p, len, "%s:inherit;", key);
3101         } else {
3102             return g_snprintf(p, len, "%s:%s;", key, val->value);
3103         }
3104     }
3105     return 0;
3109 /**
3110  *
3111  */
3112 static bool
3113 sp_length_differ(SPILength const *const a, SPILength const *const b)
3115     if (a->unit != b->unit) {
3116         if (a->unit == SP_CSS_UNIT_EM) return true;
3117         if (a->unit == SP_CSS_UNIT_EX) return true;
3118         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3119         if (b->unit == SP_CSS_UNIT_EM) return true;
3120         if (b->unit == SP_CSS_UNIT_EX) return true;
3121         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3122     }
3124     return (a->computed != b->computed);
3129 /**
3130  * Write SPILength object into string.
3131  */
3132 static gint
3133 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3134                        SPILength const *const val, SPILength const *const base, guint const flags)
3136     Inkscape::CSSOStringStream os;
3138     if ((flags & SP_STYLE_FLAG_ALWAYS)
3139         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3140         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3141             && (!base->set || sp_length_differ(val, base))))
3142     {
3143         if (val->inherit) {
3144             return g_snprintf(p, len, "%s:inherit;", key);
3145         } else {
3146             switch (val->unit) {
3147                 case SP_CSS_UNIT_NONE:
3148                     os << key << ":" << val->computed << ";";
3149                     return g_strlcpy(p, os.str().c_str(), len);
3150                     break;
3151                 case SP_CSS_UNIT_PX:
3152                     os << key << ":" << val->computed << "px;";
3153                     return g_strlcpy(p, os.str().c_str(), len);
3154                     break;
3155                 case SP_CSS_UNIT_PT:
3156                     os << key << ":" << val->computed * PT_PER_PX << "pt;";
3157                     return g_strlcpy(p, os.str().c_str(), len);
3158                     break;
3159                 case SP_CSS_UNIT_PC:
3160                     os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3161                     return g_strlcpy(p, os.str().c_str(), len);
3162                     break;
3163                 case SP_CSS_UNIT_MM:
3164                     os << key << ":" << val->computed * MM_PER_PX << "mm;";
3165                     return g_strlcpy(p, os.str().c_str(), len);
3166                     break;
3167                 case SP_CSS_UNIT_CM:
3168                     os << key << ":" << val->computed * CM_PER_PX << "cm;";
3169                     return g_strlcpy(p, os.str().c_str(), len);
3170                     break;
3171                 case SP_CSS_UNIT_IN:
3172                     os << key << ":" << val->computed * IN_PER_PX << "in;";
3173                     return g_strlcpy(p, os.str().c_str(), len);
3174                     break;
3175                 case SP_CSS_UNIT_EM:
3176                     os << key << ":" << val->value << "em;";
3177                     return g_strlcpy(p, os.str().c_str(), len);
3178                     break;
3179                 case SP_CSS_UNIT_EX:
3180                     os << key << ":" << val->value << "ex;";
3181                     return g_strlcpy(p, os.str().c_str(), len);
3182                     break;
3183                 case SP_CSS_UNIT_PERCENT:
3184                     os << key << ":" << (val->value * 100.0) << "%;";
3185                     return g_strlcpy(p, os.str().c_str(), len);
3186                     break;
3187                 default:
3188                     /* Invalid */
3189                     break;
3190             }
3191         }
3192     }
3193     return 0;
3197 /**
3198  *
3199  */
3200 static bool
3201 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3203     if (a->normal != b->normal) return true;
3204     if (a->normal) return false;
3206     if (a->unit != b->unit) {
3207         if (a->unit == SP_CSS_UNIT_EM) return true;
3208         if (a->unit == SP_CSS_UNIT_EX) return true;
3209         if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3210         if (b->unit == SP_CSS_UNIT_EM) return true;
3211         if (b->unit == SP_CSS_UNIT_EX) return true;
3212         if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3213     }
3215     return (a->computed != b->computed);
3218 /**
3219  * Write SPILengthOrNormal object into string.
3220  */
3221 static gint
3222 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3223                                SPILengthOrNormal const *const val,
3224                                SPILengthOrNormal const *const base,
3225                                guint const flags)
3227     if ((flags & SP_STYLE_FLAG_ALWAYS)
3228         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3229         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3230             && (!base->set || sp_lengthornormal_differ(val, base))))
3231     {
3232         if (val->normal) {
3233             return g_snprintf(p, len, "%s:normal;", key);
3234         } else {
3235             SPILength length;
3236             length.set = val->set;
3237             length.inherit = val->inherit;
3238             length.unit = val->unit;
3239             length.value = val->value;
3240             length.computed = val->computed;
3241             return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3242         }
3243     }
3244     return 0;
3247 /**
3248  *
3249  */
3250 static bool
3251 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3253     return    a->underline != b->underline
3254            || a->overline != b->overline
3255            || a->line_through != b->line_through
3256            || a->blink != b->blink;
3259 /**
3260  * Write SPITextDecoration object into string.
3261  */
3262 static gint
3263 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3264                                SPITextDecoration const *const val,
3265                                SPITextDecoration const *const base,
3266                                guint const flags)
3268     Inkscape::CSSOStringStream os;
3270     if ((flags & SP_STYLE_FLAG_ALWAYS)
3271         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3272         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3273             && (!base->set || sp_textdecoration_differ(val, base))))
3274     {
3275         if (val->inherit) {
3276             return g_snprintf(p, len, "%s:inherit;", key);
3277         } else {
3278             os << key << ":";
3279             if (val->underline || val->overline || val->line_through || val->blink) {
3280                 if (val->underline) os << " underline";
3281                 if (val->overline) os << " overline";
3282                 if (val->line_through) os << " line-through";
3283                 if (val->blink) os << " blink";
3284             } else
3285                 os << "none";
3286             os << ";";
3287             return g_strlcpy(p, os.str().c_str(), len);
3288         }
3289     }
3290     return 0;
3293 /**
3294  *
3295  */
3296 static bool
3297 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3299     if (a->type != b->type)
3300         return true;
3301     if (a->type == SP_PAINT_TYPE_COLOR)
3302         return !sp_color_is_equal(&a->value.color, &b->value.color);
3303     if (a->type == SP_PAINT_TYPE_PAINTSERVER)
3304         return (a->value.paint.server != b->value.paint.server);
3305     return false;
3310 /**
3311  * Write SPIPaint object into string.
3312  */
3313 static gint
3314 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3315                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3317     if ((flags & SP_STYLE_FLAG_ALWAYS)
3318         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3319         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3320             && (!base->set || sp_paint_differ(paint, base))))
3321     {
3322         if (paint->inherit) {
3323             return g_snprintf(b, len, "%s:inherit;", key);
3324         } else if (paint->currentcolor) {
3325             return g_snprintf(b, len, "%s:currentColor;", key);
3326         } else {
3327             switch (paint->type) {
3328             case SP_PAINT_TYPE_COLOR:
3329                 return g_snprintf(b, len, "%s:#%06x;", key, sp_color_get_rgba32_falpha(&paint->value.color, 0.0) >> 8);
3330                 break;
3331             case SP_PAINT_TYPE_PAINTSERVER:
3332                     return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
3333                 break;
3334             default:
3335                 break;
3336             }
3337             return g_snprintf(b, len, "%s:none;", key);
3338         }
3339     }
3340     return 0;
3344 /**
3345  *
3346  */
3347 static bool
3348 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3350     if (a->type != b->type)
3351         return true;
3352     if (a->type == SP_FONT_SIZE_LENGTH) {
3353         if (a->computed != b->computed)
3354             return true;
3355     } else {
3356         if (a->value != b->value)
3357             return true;
3358     }
3359     return false;
3363 /**
3364  * Write SPIFontSize object into string.
3365  */
3366 static gint
3367 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3368                          SPIFontSize const *const val, SPIFontSize const *const base,
3369                          guint const flags)
3371     if ((flags & SP_STYLE_FLAG_ALWAYS)
3372         || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3373         || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3374             && (!base->set || sp_fontsize_differ(val, base))))
3375     {
3376         if (val->inherit) {
3377             return g_snprintf(p, len, "%s:inherit;", key);
3378         } else if (val->type == SP_FONT_SIZE_LITERAL) {
3379             for (unsigned i = 0; enum_font_size[i].key; i++) {
3380                 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3381                     return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3382                 }
3383             }
3384         } else if (val->type == SP_FONT_SIZE_LENGTH) {
3385             Inkscape::CSSOStringStream os;
3386             os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
3387             return g_strlcpy(p, os.str().c_str(), len);
3388         } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3389             Inkscape::CSSOStringStream os;
3390             os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3391             return g_strlcpy(p, os.str().c_str(), len);
3392         }
3393     }
3394     return 0;
3398 /**
3399  * Clear paint object; conditionally disconnect style from paintserver.
3400  */
3401 static void
3402 sp_style_paint_clear(SPStyle *style, SPIPaint *paint,
3403                      unsigned hunref, unsigned unset)
3405     if (hunref && (paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
3406         if (style->hreffed) {
3407             sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3408             style->hreffed = false;
3409         }
3410         if (style->listening) {
3411             g_signal_handlers_disconnect_matched(G_OBJECT(paint->value.paint.server),
3412                                                  G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
3413             style->listening = false;
3414         }
3415         paint->value.paint.server = NULL;
3416         paint->value.paint.uri = NULL;
3417         paint->type = SP_PAINT_TYPE_NONE;
3418     }
3420     if (unset) {
3421         paint->set = FALSE;
3422         paint->inherit = FALSE;
3423     }
3426 /**
3427  * Clear all style property attributes in object.
3428  */
3429 void
3430 sp_style_unset_property_attrs(SPObject *o)
3432     if (!o) return;
3434     SPStyle *style = SP_OBJECT_STYLE(o);
3435     if (!style) return;
3437     Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3438     if (!repr) return;
3440     if (style->opacity.set) {
3441         repr->setAttribute("opacity", NULL);
3442     }
3443     if (style->color.set) {
3444         repr->setAttribute("color", NULL);
3445     }
3446     if (style->fill.set) {
3447         repr->setAttribute("fill", NULL);
3448     }
3449     if (style->fill_opacity.set) {
3450         repr->setAttribute("fill-opacity", NULL);
3451     }
3452     if (style->fill_rule.set) {
3453         repr->setAttribute("fill-rule", NULL);
3454     }
3455     if (style->stroke.set) {
3456         repr->setAttribute("stroke", NULL);
3457     }
3458     if (style->stroke_width.set) {
3459         repr->setAttribute("stroke-width", NULL);
3460     }
3461     if (style->stroke_linecap.set) {
3462         repr->setAttribute("stroke-linecap", NULL);
3463     }
3464     if (style->stroke_linejoin.set) {
3465         repr->setAttribute("stroke-linejoin", NULL);
3466     }
3467     if (style->marker[SP_MARKER_LOC].set) {
3468         repr->setAttribute("marker", NULL);
3469     }
3470     if (style->marker[SP_MARKER_LOC_START].set) {
3471         repr->setAttribute("marker-start", NULL);
3472     }
3473     if (style->marker[SP_MARKER_LOC_MID].set) {
3474         repr->setAttribute("marker-mid", NULL);
3475     }
3476     if (style->marker[SP_MARKER_LOC_END].set) {
3477         repr->setAttribute("marker-end", NULL);
3478     }
3479     if (style->stroke_opacity.set) {
3480         repr->setAttribute("stroke-opacity", NULL);
3481     }
3482     if (style->stroke_dasharray_set) {
3483         repr->setAttribute("stroke-dasharray", NULL);
3484     }
3485     if (style->stroke_dashoffset_set) {
3486         repr->setAttribute("stroke-dashoffset", NULL);
3487     }
3488     if (style->text_private && style->text->font_family.set) {
3489         repr->setAttribute("font-family", NULL);
3490     }
3491     if (style->text_anchor.set) {
3492         repr->setAttribute("text-anchor", NULL);
3493     }
3494     if (style->writing_mode.set) {
3495         repr->setAttribute("writing_mode", NULL);
3496     }
3499 /**
3500  * \pre style != NULL.
3501  * \pre flags in {IFSET, ALWAYS}.
3502  */
3503 SPCSSAttr *
3504 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3506     g_return_val_if_fail(style != NULL, NULL);
3507     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3508                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3509                          NULL);
3510     gchar *style_str = sp_style_write_string(style, flags);
3511     SPCSSAttr *css = sp_repr_css_attr_new();
3512     sp_repr_css_attr_add_from_string(css, style_str);
3513     g_free(style_str);
3514     return css;
3518 /**
3519  * \pre object != NULL
3520  * \pre flags in {IFSET, ALWAYS}.
3521  */
3522 SPCSSAttr *
3523 sp_css_attr_from_object(SPObject *object, guint const flags)
3525     g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3526                           (flags == SP_STYLE_FLAG_ALWAYS)  ),
3527                          NULL);
3528     SPStyle const *const style = SP_OBJECT_STYLE(object);
3529     if (style == NULL)
3530         return NULL;
3531     return sp_css_attr_from_style (style, flags);
3534 /**
3535  * Unset any text-related properties
3536  */
3537 SPCSSAttr *
3538 sp_css_attr_unset_text(SPCSSAttr *css)
3540     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3541     sp_repr_css_set_property(css, "font-size", NULL);
3542     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3543     sp_repr_css_set_property(css, "font-style", NULL);
3544     sp_repr_css_set_property(css, "font-variant", NULL);
3545     sp_repr_css_set_property(css, "font-weight", NULL);
3546     sp_repr_css_set_property(css, "font-stretch", NULL);
3547     sp_repr_css_set_property(css, "font-family", NULL);
3548     sp_repr_css_set_property(css, "text-indent", NULL);
3549     sp_repr_css_set_property(css, "text-align", NULL);
3550     sp_repr_css_set_property(css, "text-decoration", NULL);
3551     sp_repr_css_set_property(css, "line-height", NULL);
3552     sp_repr_css_set_property(css, "letter-spacing", NULL);
3553     sp_repr_css_set_property(css, "word-spacing", NULL);
3554     sp_repr_css_set_property(css, "text-transform", NULL);
3555     sp_repr_css_set_property(css, "direction", NULL);
3556     sp_repr_css_set_property(css, "block-progression", NULL);
3557     sp_repr_css_set_property(css, "writing-mode", NULL);
3558     sp_repr_css_set_property(css, "text-anchor", NULL);
3559     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3560     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3561     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3562     sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3564     return css;
3567 bool
3568 is_url(char const *p)
3570     if (p == NULL)
3571         return false;
3572 /** \todo
3573  * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3574  * in property values must start with 'url('.
3575  */
3576     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3579 /**
3580  * Unset any properties that contain URI values.
3581  *
3582  * Used for storing style that will be reused across documents when carrying
3583  * the referenced defs is impractical.
3584  */
3585 SPCSSAttr *
3586 sp_css_attr_unset_uris(SPCSSAttr *css)
3588 // All properties that may hold <uri> or <paint> according to SVG 1.1
3589     if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3590     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3591     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3592     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3593     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3594     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3595     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3596     if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3597     if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3598     if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3600     return css;
3603 /**
3604  * Scale a single-value property.
3605  */
3606 void
3607 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3608                                   double ex, bool only_with_units = false)
3610     gchar const *w = sp_repr_css_property(css, property, NULL);
3611     if (w) {
3612         gchar *units = NULL;
3613         double wd = g_ascii_strtod(w, &units) * ex;
3614         if (w == units) {// nothing converted, non-numeric value
3615             return;
3616         }
3617         if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3618             // only_with_units, but no units found, so do nothing.
3619             return;
3620         }
3621         Inkscape::CSSOStringStream os;
3622         os << wd << units; // reattach units
3623         sp_repr_css_set_property(css, property, os.str().c_str());
3624     }
3627 /**
3628  * Scale a list-of-values property.
3629  */
3630 void
3631 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3633     gchar const *string = sp_repr_css_property(css, property, NULL);
3634     if (string) {
3635         Inkscape::CSSOStringStream os;
3636         gchar **a = g_strsplit(string, ",", 10000);
3637         bool first = true;
3638         for (gchar **i = a; i != NULL; i++) {
3639             gchar *w = *i;
3640             if (w == NULL)
3641                 break;
3642             gchar *units = NULL;
3643             double wd = g_ascii_strtod(w, &units) * ex;
3644             if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3645                 g_strfreev(a);
3646                 return;
3647             }
3648             if (!first) {
3649                 os << ",";
3650             }
3651             os << wd << units; // reattach units
3652             first = false;
3653         }
3654         sp_repr_css_set_property(css, property, os.str().c_str());
3655         g_strfreev(a);
3656     }
3659 /**
3660  * Scale any properties that may hold <length> by ex.
3661  */
3662 SPCSSAttr *
3663 sp_css_attr_scale(SPCSSAttr *css, double ex)
3665     sp_css_attr_scale_property_single(css, "baseline-shift", ex);
3666     sp_css_attr_scale_property_single(css, "stroke-width", ex);
3667     sp_css_attr_scale_property_list   (css, "stroke-dasharray", ex);
3668     sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
3669     sp_css_attr_scale_property_single(css, "font-size", ex);
3670     sp_css_attr_scale_property_single(css, "kerning", ex);
3671     sp_css_attr_scale_property_single(css, "letter-spacing", ex);
3672     sp_css_attr_scale_property_single(css, "word-spacing", ex);
3673     sp_css_attr_scale_property_single(css, "line-height", ex, true);
3675     return css;
3679 /**
3680  * Remove quotes from SPIString object value.
3681  *
3682  * \todo FIXME: now used for font family, but perhaps this should apply to
3683  * ALL strings (check CSS spec), in which case this should be part of
3684  * read_istring.
3685  */
3686 void
3687 css2_unescape_unquote (SPIString *val)
3689     if (val->set && val->value && strlen(val->value) >= 2) {
3691        /// \todo unescape all \-escaped chars
3693         int l = strlen(val->value);
3694         if ((val->value[0] == '"' && val->value[l - 1] == '"') ||
3695             (val->value[0] == '\'' && val->value[l - 1] == '\'')) {
3696                 memcpy (val->value, val->value+1, l - 2);
3697                 val->value[l - 2] = '\0';
3698         }
3699     }
3703 /*
3704   Local Variables:
3705   mode:c++
3706   c-file-style:"stroustrup"
3707   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3708   indent-tabs-mode:nil
3709   fill-column:99
3710   End:
3711 */
3712 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :