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