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