Code

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