Code

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