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