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