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 *child, 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(SPIFilter *f, 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, SPIFilter *filter);
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 return style;
425 }
428 /**
429 * Creates a new SPStyle object, and attaches it to the specified SPObject.
430 */
431 SPStyle *
432 sp_style_new_from_object(SPObject *object)
433 {
434 g_return_val_if_fail(object != NULL, NULL);
435 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
437 SPStyle *style = sp_style_new();
438 style->object = object;
439 style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
441 if (object && SP_OBJECT_IS_CLONED(object)) {
442 style->cloned = true;
443 }
445 return style;
446 }
449 /**
450 * Increase refcount of style.
451 */
452 SPStyle *
453 sp_style_ref(SPStyle *style)
454 {
455 g_return_val_if_fail(style != NULL, NULL);
456 g_return_val_if_fail(style->refcount > 0, NULL);
458 style->refcount += 1;
460 return style;
461 }
464 /**
465 * Decrease refcount of style with possible destruction.
466 */
467 SPStyle *
468 sp_style_unref(SPStyle *style)
469 {
470 g_return_val_if_fail(style != NULL, NULL);
471 g_return_val_if_fail(style->refcount > 0, NULL);
473 style->refcount -= 1;
475 if (style->refcount < 1) {
476 style->release_connection.disconnect();
477 style->release_connection.~connection();
478 if (style->text) sp_text_style_unref(style->text);
479 sp_style_paint_clear(style, &style->fill);
480 sp_style_paint_clear(style, &style->stroke);
481 sp_style_filter_clear(style, &style->filter);
482 style->fill_release_connection.disconnect();
483 style->fill_release_connection.~connection();
484 style->fill_modified_connection.disconnect();
485 style->fill_modified_connection.~connection();
486 style->stroke_release_connection.disconnect();
487 style->stroke_release_connection.~connection();
488 style->stroke_modified_connection.disconnect();
489 style->stroke_modified_connection.~connection();
490 g_free(style->stroke_dash.dash);
491 g_free(style);
492 }
494 return NULL;
495 }
497 /**
498 * Reads the various style parameters for an object from repr.
499 */
500 static void
501 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
502 {
503 g_assert(style != NULL);
504 g_assert(repr != NULL);
505 g_assert(!object || (SP_OBJECT_REPR(object) == repr));
507 sp_style_clear(style);
509 if (object && SP_OBJECT_IS_CLONED(object)) {
510 style->cloned = true;
511 }
513 /* 1. Style itself */
514 gchar const *val = repr->attribute("style");
515 if (val != NULL) {
516 sp_style_merge_from_style_string(style, val);
517 }
519 if (object) {
520 sp_style_merge_from_object_stylesheet(style, object);
521 } else {
522 /** \todo No stylesheet information. Find out under what circumstances
523 * this occurs, and handle accordingly. (If we really wanted to, we
524 * could probably get stylesheets by going through repr->doc.)
525 */
526 }
528 /* 2. Presentation attributes */
529 /* CSS2 */
530 SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
531 SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
532 SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
533 /* Font */
534 SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
535 SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
536 SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
537 SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
538 SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
539 /* Text (css2 chapter 16) */
540 SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
541 SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
542 if (!style->text_decoration.set) {
543 val = repr->attribute("text-decoration");
544 if (val) {
545 sp_style_read_itextdecoration(&style->text_decoration, val);
546 }
547 }
548 if (!style->line_height.set) {
549 val = repr->attribute("line-height");
550 if (val) {
551 sp_style_read_ilengthornormal(&style->line_height, val);
552 }
553 }
554 if (!style->letter_spacing.set) {
555 val = repr->attribute("letter-spacing");
556 if (val) {
557 sp_style_read_ilengthornormal(&style->letter_spacing, val);
558 }
559 }
560 if (!style->word_spacing.set) {
561 val = repr->attribute("word-spacing");
562 if (val) {
563 sp_style_read_ilengthornormal(&style->word_spacing, val);
564 }
565 }
566 SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
567 SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
568 SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
570 /* SVG */
571 SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
572 enum_writing_mode, true);
573 SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
574 enum_text_anchor, true);
576 /* opacity */
577 if (!style->opacity.set) {
578 val = repr->attribute("opacity");
579 if (val) {
580 sp_style_read_iscale24(&style->opacity, val);
581 }
582 }
583 /* color */
584 if (!style->color.set) {
585 val = repr->attribute("color");
586 if (val) {
587 sp_style_read_icolor(&style->color, val, style, ( object
588 ? SP_OBJECT_DOCUMENT(object)
589 : NULL ));
590 }
591 }
592 /* fill */
593 if (!style->fill.set) {
594 val = repr->attribute("fill");
595 if (val) {
596 sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
597 }
598 }
599 /* fill-opacity */
600 if (!style->fill_opacity.set) {
601 val = repr->attribute("fill-opacity");
602 if (val) {
603 sp_style_read_iscale24(&style->fill_opacity, val);
604 }
605 }
606 /* fill-rule */
607 SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
608 /* stroke */
609 if (!style->stroke.set) {
610 val = repr->attribute("stroke");
611 if (val) {
612 sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
613 }
614 }
615 SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
616 SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
617 SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
618 SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
620 /* markers */
621 if (!style->marker[SP_MARKER_LOC].set) {
622 val = repr->attribute("marker");
623 if (val) {
624 sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
625 }
626 }
627 if (!style->marker[SP_MARKER_LOC_START].set) {
628 val = repr->attribute("marker-start");
629 if (val) {
630 sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
631 }
632 }
633 if (!style->marker[SP_MARKER_LOC_MID].set) {
634 val = repr->attribute("marker-mid");
635 if (val) {
636 sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
637 }
638 }
639 if (!style->marker[SP_MARKER_LOC_END].set) {
640 val = repr->attribute("marker-end");
641 if (val) {
642 sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
643 }
644 }
646 /* stroke-opacity */
647 if (!style->stroke_opacity.set) {
648 val = repr->attribute("stroke-opacity");
649 if (val) {
650 sp_style_read_iscale24(&style->stroke_opacity, val);
651 }
652 }
653 if (!style->stroke_dasharray_set) {
654 val = repr->attribute("stroke-dasharray");
655 if (val) {
656 sp_style_read_dash(style, val);
657 }
658 }
659 if (!style->stroke_dashoffset_set) {
660 /* fixme */
661 val = repr->attribute("stroke-dashoffset");
662 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
663 style->stroke_dashoffset_set = TRUE;
664 } else {
665 style->stroke_dashoffset_set = FALSE;
666 }
667 }
669 /* font-family */
670 if (!style->text_private || !style->text->font_family.set) {
671 val = repr->attribute("font-family");
672 if (val) {
673 if (!style->text_private) sp_style_privatize_text(style);
674 sp_style_read_istring(&style->text->font_family, val);
675 css2_unescape_unquote(&style->text->font_family);
676 }
677 }
679 /* filter effects */
680 if (!style->filter.set) {
681 val = repr->attribute("filter");
682 if (val) {
683 sp_style_read_ifilter(&style->filter, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
684 }
685 }
686 SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
687 "enable-background", enum_enable_background, true);
689 /* 3. Merge from parent */
690 if (object) {
691 if (object->parent) {
692 sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
693 }
694 } else {
695 if (sp_repr_parent(repr)) {
696 /// \todo fixme: This is not the prettiest thing (Lauris)
697 SPStyle *parent = sp_style_new();
698 sp_style_read(parent, NULL, sp_repr_parent(repr));
699 sp_style_merge_from_parent(style, parent);
700 sp_style_unref(parent);
701 }
702 }
703 }
706 /**
707 * Read style properties from object's repr.
708 *
709 * 1. Reset existing object style
710 * 2. Load current effective object style
711 * 3. Load i attributes from immediate parent (which has to be up-to-date)
712 */
713 void
714 sp_style_read_from_object(SPStyle *style, SPObject *object)
715 {
716 g_return_if_fail(style != NULL);
717 g_return_if_fail(object != NULL);
718 g_return_if_fail(SP_IS_OBJECT(object));
720 Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
721 g_return_if_fail(repr != NULL);
723 sp_style_read(style, object, repr);
724 }
727 /**
728 * Read style properties from repr only.
729 */
730 void
731 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
732 {
733 g_return_if_fail(style != NULL);
734 g_return_if_fail(repr != NULL);
736 sp_style_read(style, NULL, repr);
737 }
741 /**
742 *
743 */
744 static void
745 sp_style_privatize_text(SPStyle *style)
746 {
747 SPTextStyle *text = style->text;
748 style->text = sp_text_style_duplicate_unset(style->text);
749 sp_text_style_unref(text);
750 style->text_private = TRUE;
751 }
754 /**
755 * Merge property into style.
756 *
757 * Should be called in order of highest to lowest precedence.
758 * E.g. for a single style string, call from the last declaration to the first,
759 * as CSS says that later declarations override earlier ones.
760 *
761 * \pre val != NULL.
762 */
763 static void
764 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
765 {
766 g_return_if_fail(val != NULL);
768 switch (id) {
769 /* CSS2 */
770 /* Font */
771 case SP_PROP_FONT_FAMILY:
772 if (!style->text_private) sp_style_privatize_text(style);
773 if (!style->text->font_family.set) {
774 sp_style_read_istring(&style->text->font_family, val);
775 css2_unescape_unquote(&style->text->font_family);
776 }
777 break;
778 case SP_PROP_FONT_SIZE:
779 SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
780 break;
781 case SP_PROP_FONT_SIZE_ADJUST:
782 if (strcmp(val, "none") != 0) {
783 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
784 }
785 break;
786 case SP_PROP_FONT_STYLE:
787 SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
788 break;
789 case SP_PROP_FONT_VARIANT:
790 SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
791 break;
792 case SP_PROP_FONT_WEIGHT:
793 SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
794 break;
795 case SP_PROP_FONT_STRETCH:
796 SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
797 break;
798 case SP_PROP_FONT:
799 if (!style->text_private) sp_style_privatize_text(style);
800 if (!style->text->font.set) {
801 g_free(style->text->font.value);
802 style->text->font.value = g_strdup(val);
803 style->text->font.set = TRUE;
804 style->text->font.inherit = (val && !strcmp(val, "inherit"));
805 }
806 break;
807 /* Text */
808 case SP_PROP_TEXT_INDENT:
809 SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
810 break;
811 case SP_PROP_TEXT_ALIGN:
812 SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
813 break;
814 case SP_PROP_TEXT_DECORATION:
815 if (!style->text_decoration.set) {
816 sp_style_read_itextdecoration(&style->text_decoration, val);
817 }
818 break;
819 case SP_PROP_LINE_HEIGHT:
820 if (!style->line_height.set) {
821 sp_style_read_ilengthornormal(&style->line_height, val);
822 }
823 break;
824 case SP_PROP_LETTER_SPACING:
825 if (!style->letter_spacing.set) {
826 sp_style_read_ilengthornormal(&style->letter_spacing, val);
827 }
828 break;
829 case SP_PROP_WORD_SPACING:
830 if (!style->word_spacing.set) {
831 sp_style_read_ilengthornormal(&style->word_spacing, val);
832 }
833 break;
834 case SP_PROP_TEXT_TRANSFORM:
835 SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
836 break;
837 /* Text (css3) */
838 case SP_PROP_DIRECTION:
839 SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
840 break;
841 case SP_PROP_BLOCK_PROGRESSION:
842 SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
843 break;
844 case SP_PROP_WRITING_MODE:
845 SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
846 break;
847 case SP_PROP_TEXT_ANCHOR:
848 SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
849 break;
850 /* Text (unimplemented) */
851 case SP_PROP_TEXT_RENDERING: {
852 /* Ignore the hint. */
853 SPIEnum dummy;
854 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
855 break;
856 }
857 case SP_PROP_ALIGNMENT_BASELINE:
858 g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
859 break;
860 case SP_PROP_BASELINE_SHIFT:
861 g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
862 break;
863 case SP_PROP_DOMINANT_BASELINE:
864 g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
865 break;
866 case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
867 g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
868 break;
869 case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
870 g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
871 break;
872 case SP_PROP_KERNING:
873 g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
874 break;
875 /* Misc */
876 case SP_PROP_CLIP:
877 g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
878 break;
879 case SP_PROP_COLOR:
880 if (!style->color.set) {
881 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
882 }
883 break;
884 case SP_PROP_CURSOR:
885 g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
886 break;
887 case SP_PROP_DISPLAY:
888 SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
889 break;
890 case SP_PROP_OVERFLOW:
891 /** \todo
892 * FIXME: not supported properly yet, we just read and write it,
893 * but act as if it is always "display".
894 */
895 SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
896 break;
897 case SP_PROP_VISIBILITY:
898 SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
899 break;
900 /* SVG */
901 /* Clip/Mask */
902 case SP_PROP_CLIP_PATH:
903 g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
904 break;
905 case SP_PROP_CLIP_RULE:
906 g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
907 break;
908 case SP_PROP_MASK:
909 g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
910 break;
911 case SP_PROP_OPACITY:
912 if (!style->opacity.set) {
913 sp_style_read_iscale24(&style->opacity, val);
914 }
915 break;
916 case SP_PROP_ENABLE_BACKGROUND:
917 SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
918 enum_enable_background, true);
919 break;
920 /* Filter */
921 case SP_PROP_FILTER:
922 if (!style->filter.set && !style->filter.inherit) {
923 sp_style_read_ifilter(&style->filter, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
924 }
925 break;
926 case SP_PROP_FLOOD_COLOR:
927 g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
928 break;
929 case SP_PROP_FLOOD_OPACITY:
930 g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
931 break;
932 case SP_PROP_LIGHTING_COLOR:
933 g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
934 break;
935 /* Gradient */
936 case SP_PROP_STOP_COLOR:
937 g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
938 break;
939 case SP_PROP_STOP_OPACITY:
940 g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
941 break;
942 /* Interactivity */
943 case SP_PROP_POINTER_EVENTS:
944 g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
945 break;
946 /* Paint */
947 case SP_PROP_COLOR_INTERPOLATION:
948 g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
949 break;
950 case SP_PROP_COLOR_INTERPOLATION_FILTERS:
951 g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
952 break;
953 case SP_PROP_COLOR_PROFILE:
954 g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
955 break;
956 case SP_PROP_COLOR_RENDERING: {
957 /* Ignore the hint. */
958 SPIEnum dummy;
959 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
960 break;
961 }
962 case SP_PROP_FILL:
963 if (!style->fill.set) {
964 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
965 }
966 break;
967 case SP_PROP_FILL_OPACITY:
968 if (!style->fill_opacity.set) {
969 sp_style_read_iscale24(&style->fill_opacity, val);
970 }
971 break;
972 case SP_PROP_FILL_RULE:
973 if (!style->fill_rule.set) {
974 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
975 }
976 break;
977 case SP_PROP_IMAGE_RENDERING: {
978 /* Ignore the hint. */
979 SPIEnum dummy;
980 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
981 break;
982 }
983 case SP_PROP_MARKER:
984 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
985 /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
986 marker_status("Setting SP_PROP_MARKER");
987 if (!style->marker[SP_MARKER_LOC].set) {
988 g_free(style->marker[SP_MARKER_LOC].value);
989 style->marker[SP_MARKER_LOC].value = g_strdup(val);
990 style->marker[SP_MARKER_LOC].set = TRUE;
991 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
992 }
993 break;
995 case SP_PROP_MARKER_START:
996 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
997 marker_status("Setting SP_PROP_MARKER_START");
998 if (!style->marker[SP_MARKER_LOC_START].set) {
999 g_free(style->marker[SP_MARKER_LOC_START].value);
1000 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1001 style->marker[SP_MARKER_LOC_START].set = TRUE;
1002 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1003 }
1004 break;
1005 case SP_PROP_MARKER_MID:
1006 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1007 marker_status("Setting SP_PROP_MARKER_MID");
1008 if (!style->marker[SP_MARKER_LOC_MID].set) {
1009 g_free(style->marker[SP_MARKER_LOC_MID].value);
1010 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1011 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1012 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1013 }
1014 break;
1015 case SP_PROP_MARKER_END:
1016 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1017 marker_status("Setting SP_PROP_MARKER_END");
1018 if (!style->marker[SP_MARKER_LOC_END].set) {
1019 g_free(style->marker[SP_MARKER_LOC_END].value);
1020 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1021 style->marker[SP_MARKER_LOC_END].set = TRUE;
1022 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1023 }
1024 break;
1026 case SP_PROP_SHAPE_RENDERING: {
1027 /* Ignore the hint. */
1028 SPIEnum dummy;
1029 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1030 break;
1031 }
1033 case SP_PROP_STROKE:
1034 if (!style->stroke.set) {
1035 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1036 }
1037 break;
1038 case SP_PROP_STROKE_WIDTH:
1039 SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1040 break;
1041 case SP_PROP_STROKE_DASHARRAY:
1042 if (!style->stroke_dasharray_set) {
1043 sp_style_read_dash(style, val);
1044 }
1045 break;
1046 case SP_PROP_STROKE_DASHOFFSET:
1047 if (!style->stroke_dashoffset_set) {
1048 /* fixme */
1049 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1050 style->stroke_dashoffset_set = TRUE;
1051 } else {
1052 style->stroke_dashoffset_set = FALSE;
1053 }
1054 }
1055 break;
1056 case SP_PROP_STROKE_LINECAP:
1057 if (!style->stroke_linecap.set) {
1058 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1059 }
1060 break;
1061 case SP_PROP_STROKE_LINEJOIN:
1062 if (!style->stroke_linejoin.set) {
1063 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1064 }
1065 break;
1066 case SP_PROP_STROKE_MITERLIMIT:
1067 if (!style->stroke_miterlimit.set) {
1068 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1069 }
1070 break;
1071 case SP_PROP_STROKE_OPACITY:
1072 if (!style->stroke_opacity.set) {
1073 sp_style_read_iscale24(&style->stroke_opacity, val);
1074 }
1075 break;
1077 default:
1078 g_warning("Invalid style property id: %d value: %s", id, val);
1079 break;
1080 }
1081 }
1083 static void
1084 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1085 {
1086 /** \todo Ensure that property is lcased, as per
1087 * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1088 * Should probably be done in libcroco.
1089 */
1090 unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1091 if (prop_idx != SP_ATTR_INVALID) {
1092 /** \todo
1093 * effic: Test whether the property is already set before trying to
1094 * convert to string. Alternatively, set from CRTerm directly rather
1095 * than converting to string.
1096 */
1097 guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1098 gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1099 sp_style_merge_property(style, prop_idx, str_value);
1100 g_free(str_value);
1101 }
1102 }
1104 static void
1105 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1106 {
1107 #if 0 /* forwards */
1108 for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1109 CRDeclaration *decl = NULL;
1110 cr_prop_list_get_decl(cur, &decl);
1111 sp_style_merge_style_from_decl(style, decl);
1112 }
1113 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1114 if (props) {
1115 sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1116 CRDeclaration *decl = NULL;
1117 cr_prop_list_get_decl(props, &decl);
1118 sp_style_merge_style_from_decl(style, decl);
1119 }
1120 #endif
1121 }
1123 /**
1124 * \pre decl_list != NULL
1125 */
1126 static void
1127 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1128 {
1129 if (decl_list->next) {
1130 sp_style_merge_from_decl_list(style, decl_list->next);
1131 }
1132 sp_style_merge_style_from_decl(style, decl_list);
1133 }
1135 static CRSelEng *
1136 sp_repr_sel_eng()
1137 {
1138 CRSelEng *const ret = cr_sel_eng_new();
1139 cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1141 /** \todo
1142 * Check whether we need to register any pseudo-class handlers.
1143 * libcroco has its own default handlers for first-child and lang.
1144 *
1145 * We probably want handlers for link and arguably visited (though
1146 * inkscape can't visit links at the time of writing). hover etc.
1147 * more useful in inkview than the editor inkscape.
1148 *
1149 * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1150 * the following should be honoured, at least by inkview:
1151 * :hover, :active, :focus, :visited, :link.
1152 */
1154 g_assert(ret);
1155 return ret;
1156 }
1158 static void
1159 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1160 {
1161 static CRSelEng *sel_eng = NULL;
1162 if (!sel_eng) {
1163 sel_eng = sp_repr_sel_eng();
1164 }
1166 CRPropList *props = NULL;
1167 CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1168 object->document->style_cascade,
1169 object->repr,
1170 &props);
1171 g_return_if_fail(status == CR_OK);
1172 /// \todo Check what errors can occur, and handle them properly.
1173 if (props) {
1174 sp_style_merge_from_props(style, props);
1175 cr_prop_list_destroy(props);
1176 }
1177 }
1179 /**
1180 * Parses a style="..." string and merges it with an existing SPStyle.
1181 */
1182 void
1183 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1184 {
1185 /*
1186 * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1187 * ``When CSS styling is used, CSS inline style is specified by including
1188 * semicolon-separated property declarations of the form "name : value"
1189 * within the style attribute''.
1190 *
1191 * That's fairly ambiguous. Is a `value' allowed to contain semicolons?
1192 * Why does it say "including", what else is allowed in the style
1193 * attribute value?
1194 */
1196 CRDeclaration *const decl_list
1197 = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1198 if (decl_list) {
1199 sp_style_merge_from_decl_list(style, decl_list);
1200 cr_declaration_destroy(decl_list);
1201 }
1202 }
1204 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1205 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1207 static void
1208 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1209 {
1210 /* 'font-size' */
1211 if (!child.set || child.inherit) {
1212 /* Inherit the computed value. Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1213 child.computed = parent.computed;
1214 } else if (child.type == SP_FONT_SIZE_LITERAL) {
1215 /** \todo
1216 * fixme: SVG and CSS do not specify clearly, whether we should use
1217 * user or screen coordinates (Lauris)
1218 */
1219 if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1220 child.computed = font_size_table[child.value];
1221 } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1222 child.computed = parent.computed / 1.2;
1223 } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1224 child.computed = parent.computed * 1.2;
1225 } else {
1226 /* Illegal value */
1227 }
1228 } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1229 /* Unlike most other lengths, percentage for font size is relative to parent computed value
1230 * rather than viewport. */
1231 child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1232 }
1233 }
1235 /**
1236 * Sets computed values in \a style, which may involve inheriting from (or in some other way
1237 * calculating from) corresponding computed values of \a parent.
1238 *
1239 * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1240 * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1241 * inherit a value. http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1242 * about what the computed value is (not obvious for lengths).
1243 *
1244 * \pre \a parent's computed values are already up-to-date.
1245 */
1246 void
1247 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1248 {
1249 g_return_if_fail(style != NULL);
1251 /** \todo
1252 * fixme: Check for existing callers that might pass null parent.
1253 * This should probably be g_return_if_fail, or else we should make a
1254 * best attempt to set computed values correctly without having a parent
1255 * (i.e., by assuming parent has initial values).
1256 */
1257 if (!parent)
1258 return;
1260 /* CSS2 */
1261 /* Font */
1262 sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1264 /* 'font-style' */
1265 if (!style->font_style.set || style->font_style.inherit) {
1266 style->font_style.computed = parent->font_style.computed;
1267 }
1269 /* 'font-variant' */
1270 if (!style->font_variant.set || style->font_variant.inherit) {
1271 style->font_variant.computed = parent->font_variant.computed;
1272 }
1274 /* 'font-weight' */
1275 if (!style->font_weight.set || style->font_weight.inherit) {
1276 style->font_weight.computed = parent->font_weight.computed;
1277 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1278 /** \todo
1279 * fixme: This is unconditional, i.e., happens even if parent not
1280 * present.
1281 */
1282 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1283 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1284 style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1285 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1286 unsigned const parent_val = parent->font_weight.computed;
1287 g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1288 // strictly, 'bolder' and 'lighter' should go to the next weight
1289 // expressible in the current font family, but that's difficult to
1290 // find out, so jumping by 3 seems an appropriate approximation
1291 style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1292 ? (unsigned)SP_CSS_FONT_WEIGHT_100
1293 : parent_val - 3);
1294 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1295 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1296 unsigned const parent_val = parent->font_weight.computed;
1297 g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1298 style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1299 ? (unsigned)SP_CSS_FONT_WEIGHT_900
1300 : parent_val + 3);
1301 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1302 }
1304 /* 'font-stretch' */
1305 if (!style->font_stretch.set || style->font_stretch.inherit) {
1306 style->font_stretch.computed = parent->font_stretch.computed;
1307 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1308 unsigned const parent_val = parent->font_stretch.computed;
1309 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1310 ? parent_val
1311 : parent_val - 1);
1312 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1313 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1314 unsigned const parent_val = parent->font_stretch.computed;
1315 g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1316 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1317 ? parent_val
1318 : parent_val + 1);
1319 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1320 }
1322 /* text (css2) */
1323 if (!style->text_indent.set || style->text_indent.inherit) {
1324 style->text_indent.computed = parent->text_indent.computed;
1325 }
1327 if (!style->text_align.set || style->text_align.inherit) {
1328 style->text_align.computed = parent->text_align.computed;
1329 }
1331 if (!style->text_decoration.set || style->text_decoration.inherit) {
1332 style->text_decoration.underline = parent->text_decoration.underline;
1333 style->text_decoration.overline = parent->text_decoration.overline;
1334 style->text_decoration.line_through = parent->text_decoration.line_through;
1335 style->text_decoration.blink = parent->text_decoration.blink;
1336 }
1338 if (!style->line_height.set || style->line_height.inherit) {
1339 style->line_height.computed = parent->line_height.computed;
1340 style->line_height.normal = parent->line_height.normal;
1341 }
1343 if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1344 style->letter_spacing.computed = parent->letter_spacing.computed;
1345 style->letter_spacing.normal = parent->letter_spacing.normal;
1346 }
1348 if (!style->word_spacing.set || style->word_spacing.inherit) {
1349 style->word_spacing.computed = parent->word_spacing.computed;
1350 style->word_spacing.normal = parent->word_spacing.normal;
1351 }
1353 if (!style->text_transform.set || style->text_transform.inherit) {
1354 style->text_transform.computed = parent->text_transform.computed;
1355 }
1357 if (!style->direction.set || style->direction.inherit) {
1358 style->direction.computed = parent->direction.computed;
1359 }
1361 if (!style->block_progression.set || style->block_progression.inherit) {
1362 style->block_progression.computed = parent->block_progression.computed;
1363 }
1365 if (!style->writing_mode.set || style->writing_mode.inherit) {
1366 style->writing_mode.computed = parent->writing_mode.computed;
1367 }
1369 if (!style->text_anchor.set || style->text_anchor.inherit) {
1370 style->text_anchor.computed = parent->text_anchor.computed;
1371 }
1373 if (style->opacity.inherit) {
1374 style->opacity.value = parent->opacity.value;
1375 }
1377 /* Color */
1378 if (!style->color.set || style->color.inherit) {
1379 sp_style_merge_ipaint(style, &style->color, &parent->color);
1380 }
1382 /* Fill */
1383 if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1384 sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1385 }
1387 if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1388 style->fill_opacity.value = parent->fill_opacity.value;
1389 }
1391 if (!style->fill_rule.set || style->fill_rule.inherit) {
1392 style->fill_rule.computed = parent->fill_rule.computed;
1393 }
1395 /* Stroke */
1396 if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1397 sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1398 }
1400 if (!style->stroke_width.set || style->stroke_width.inherit) {
1401 style->stroke_width.computed = parent->stroke_width.computed;
1402 } else {
1403 /* Update computed value for any change in font inherited from parent. */
1404 double const em = style->font_size.computed;
1405 if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1406 style->stroke_width.computed = style->stroke_width.value * em;
1407 } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1408 double const ex = em * 0.5; // fixme: Get x height from libnrtype or pango.
1409 style->stroke_width.computed = style->stroke_width.value * ex;
1410 }
1411 }
1413 if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1414 style->stroke_linecap.computed = parent->stroke_linecap.computed;
1415 }
1417 if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1418 style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1419 }
1421 if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1422 style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1423 }
1425 if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1426 /** \todo
1427 * This code looks wrong. Why does the logic differ from the
1428 * above properties? Similarly dashoffset below.
1429 */
1430 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1431 if (style->stroke_dash.n_dash > 0) {
1432 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1433 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1434 }
1435 }
1437 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1438 style->stroke_dash.offset = parent->stroke_dash.offset;
1439 }
1441 if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1442 style->stroke_opacity.value = parent->stroke_opacity.value;
1443 }
1445 if (style->text && parent->text) {
1446 if (!style->text->font_family.set || style->text->font_family.inherit) {
1447 g_free(style->text->font_family.value);
1448 style->text->font_family.value = g_strdup(parent->text->font_family.value);
1449 }
1450 }
1452 /* Markers - Free the old value and make copy of the new */
1453 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1454 if (!style->marker[i].set || style->marker[i].inherit) {
1455 g_free(style->marker[i].value);
1456 style->marker[i].value = g_strdup(parent->marker[i].value);
1457 }
1458 }
1460 /* Filter effects */
1461 if(style->filter.set && style->filter.inherit) {
1462 sp_style_merge_ifilter(style, &style->filter, &parent->filter);
1463 }
1465 if(style->enable_background.inherit) {
1466 style->enable_background.value = parent->enable_background.value;
1467 }
1468 }
1470 template <typename T>
1471 static void
1472 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1473 {
1474 if ( ( !(child.set) || child.inherit )
1475 && parent.set && !(parent.inherit) )
1476 {
1477 child = parent;
1478 }
1479 }
1481 /**
1482 * Copy SPIString from parent to child.
1483 */
1484 static void
1485 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1486 {
1487 if ( ( !(child.set) || child.inherit )
1488 && parent.set && !(parent.inherit) )
1489 {
1490 g_free(child.value);
1491 child.value = g_strdup(parent.value);
1492 child.set = parent.set;
1493 child.inherit = parent.inherit;
1494 }
1495 }
1497 static void
1498 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1499 SPIPaint &child, SPIPaint const &parent)
1500 {
1501 /** \todo
1502 * I haven't given this much attention. See comments below about
1503 * currentColor, colorProfile, and relative URIs.
1504 */
1505 if (!child.set || child.inherit || child.currentcolor) {
1506 sp_style_merge_ipaint(style, &child, &parent);
1507 child.set = parent.set;
1508 child.inherit = parent.inherit;
1509 }
1510 }
1512 static void
1513 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1514 unsigned const max_computed_val,
1515 unsigned const smaller_val)
1516 {
1517 /* We assume that min computed val is 0, contiguous up to max_computed_val,
1518 then zero or more other absolute values, then smaller_val then larger_val. */
1519 unsigned const min_computed_val = 0;
1520 unsigned const larger_val = smaller_val + 1;
1521 g_return_if_fail(min_computed_val < max_computed_val);
1522 g_return_if_fail(max_computed_val < smaller_val);
1524 if (parent.set && !parent.inherit) {
1525 if (!child.set || child.inherit) {
1526 child.value = parent.value;
1527 child.set = parent.set; // i.e. true
1528 child.inherit = parent.inherit; // i.e. false
1529 } else if (child.value < smaller_val) {
1530 /* Child has absolute value: leave as is. */
1531 } else if ( ( child.value == smaller_val
1532 && parent.value == larger_val )
1533 || ( parent.value == smaller_val
1534 && child.value == larger_val ) )
1535 {
1536 child.set = false;
1537 /*
1538 * Note that this can result in a change in computed value in the
1539 * rare case that the parent's setting was a no-op (i.e. if the
1540 * parent's parent's computed value was already ultra-condensed or
1541 * ultra-expanded). However, I'd guess that the change is for the
1542 * better: I'd guess that if the properties were specified
1543 * relatively, then the intent is to counteract parent's effect.
1544 * I.e. I believe this is the best code even in that case.
1545 */
1546 } else if (child.value == parent.value) {
1547 /* Leave as is. */
1548 /** \todo
1549 * It's unclear what to do if style and parent specify the same
1550 * relative directive (narrower or wider). We can either convert
1551 * to absolute specification or coalesce to a single relative
1552 * request (of half the strength of the original pair).
1553 *
1554 * Converting to a single level of relative specification is a
1555 * better choice if the newly-unlinked clone is itself cloned to
1556 * other contexts (inheriting different font stretchiness): it
1557 * would preserve the effect that it will be narrower than
1558 * the inherited context in each case. The disadvantage is that
1559 * it will ~certainly affect the computed value of the
1560 * newly-unlinked clone.
1561 */
1562 } else {
1563 unsigned const parent_val = parent.computed;
1564 child.value = ( child.value == smaller_val
1565 ? ( parent_val == min_computed_val
1566 ? parent_val
1567 : parent_val - 1 )
1568 : ( parent_val == max_computed_val
1569 ? parent_val
1570 : parent_val + 1 ) );
1571 g_assert(child.value <= max_computed_val);
1572 child.inherit = false;
1573 g_assert(child.set);
1574 }
1575 }
1576 }
1578 template <typename LengthT>
1579 static void
1580 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1581 double const parent_child_em_ratio)
1582 {
1583 if ( ( !(child.set) || child.inherit )
1584 && parent.set && !(parent.inherit) )
1585 {
1586 child = parent;
1587 switch (parent.unit) {
1588 case SP_CSS_UNIT_EM:
1589 case SP_CSS_UNIT_EX:
1590 child.value *= parent_child_em_ratio;
1591 /** \todo
1592 * fixme: Have separate ex ratio parameter.
1593 * Get x height from libnrtype or pango.
1594 */
1595 if (!isFinite(child.value)) {
1596 child.value = child.computed;
1597 child.unit = SP_CSS_UNIT_NONE;
1598 }
1599 break;
1601 default:
1602 break;
1603 }
1604 }
1605 }
1607 static double
1608 get_relative_font_size_frac(SPIFontSize const &font_size)
1609 {
1610 switch (font_size.type) {
1611 case SP_FONT_SIZE_LITERAL: {
1612 switch (font_size.value) {
1613 case SP_CSS_FONT_SIZE_SMALLER:
1614 return 5.0 / 6.0;
1616 case SP_CSS_FONT_SIZE_LARGER:
1617 return 6.0 / 5.0;
1619 default:
1620 g_assert_not_reached();
1621 }
1622 }
1624 case SP_FONT_SIZE_PERCENTAGE:
1625 return SP_F8_16_TO_FLOAT(font_size.value);
1627 case SP_FONT_SIZE_LENGTH:
1628 g_assert_not_reached();
1629 }
1630 g_assert_not_reached();
1631 }
1633 /**
1634 * Combine \a style and \a parent style specifications into a single style specification that
1635 * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1636 *
1637 * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1638 * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1639 * trying to retain the same visual appearance once the parent is removed. Interesting cases are
1640 * when there is unusual interaction with the parent's value (opacity, display) or when the value
1641 * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1642 *
1643 * Doesn't update computed values of \a style. For correctness, you should subsequently call
1644 * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1645 * style was previously up-to-date wrt \a parent.
1646 *
1647 * \pre \a parent's computed values are already up-to-date.
1648 * (\a style's computed values needn't be up-to-date.)
1649 */
1650 void
1651 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1652 {
1653 /** \note
1654 * The general rule for each property is as follows:
1655 *
1656 * If style is set to an absolute value, then leave it as is.
1657 *
1658 * Otherwise (i.e. if style has a relative value):
1659 *
1660 * If parent is set to an absolute value, then set style to the computed value.
1661 *
1662 * Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1663 */
1665 /* We do font-size first, to ensure that em size is up-to-date. */
1666 /** \todo
1667 * fixme: We'll need to have more font-related things up the top once
1668 * we're getting x-height from pango or libnrtype.
1669 */
1671 /* Some things that allow relative specifications. */
1672 {
1673 /* font-size. Note that we update the computed font-size of style,
1674 to assist in em calculations later in this function. */
1675 if (parent->font_size.set && !parent->font_size.inherit) {
1676 if (!style->font_size.set || style->font_size.inherit) {
1677 /* font_size inherits the computed value, so we can use the parent value
1678 * verbatim. */
1679 style->font_size = parent->font_size;
1680 } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1681 /* Child already has absolute size (stored in computed value), so do nothing. */
1682 } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1683 && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1684 /* Child already has absolute size, but we ensure that the computed value
1685 is up-to-date. */
1686 unsigned const ix = style->font_size.value;
1687 g_assert(ix < G_N_ELEMENTS(font_size_table));
1688 style->font_size.computed = font_size_table[ix];
1689 } else {
1690 /* Child has relative size. */
1691 double const child_frac(get_relative_font_size_frac(style->font_size));
1692 style->font_size.set = true;
1693 style->font_size.inherit = false;
1694 style->font_size.computed = parent->font_size.computed * child_frac;
1696 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1697 && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1698 || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1699 {
1700 /* Absolute value. */
1701 style->font_size.type = SP_FONT_SIZE_LENGTH;
1702 /* .value is unused for SP_FONT_SIZE_LENGTH. */
1703 } else {
1704 /* Relative value. */
1705 double const parent_frac(get_relative_font_size_frac(parent->font_size));
1706 style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1707 style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1708 }
1709 }
1710 }
1712 /* 'font-stretch' */
1713 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1714 parent->font_stretch,
1715 SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1716 SP_CSS_FONT_STRETCH_NARROWER);
1718 /* font-weight */
1719 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1720 parent->font_weight,
1721 SP_CSS_FONT_WEIGHT_900,
1722 SP_CSS_FONT_WEIGHT_LIGHTER);
1723 }
1726 /* Enum values that don't have any relative settings (other than `inherit'). */
1727 {
1728 SPIEnum SPStyle::*const fields[] = {
1729 //nyi: SPStyle::clip_rule,
1730 //nyi: SPStyle::color_interpolation,
1731 //nyi: SPStyle::color_interpolation_filters,
1732 //nyi: SPStyle::color_rendering,
1733 &SPStyle::direction,
1734 &SPStyle::fill_rule,
1735 &SPStyle::font_style,
1736 &SPStyle::font_variant,
1737 //nyi: SPStyle::image_rendering,
1738 //nyi: SPStyle::pointer_events,
1739 //nyi: SPStyle::shape_rendering,
1740 &SPStyle::stroke_linecap,
1741 &SPStyle::stroke_linejoin,
1742 &SPStyle::text_anchor,
1743 //nyi: &SPStyle::text_rendering,
1744 &SPStyle::visibility,
1745 &SPStyle::writing_mode
1746 };
1748 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1749 SPIEnum SPStyle::*const fld = fields[i];
1750 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1751 }
1752 }
1754 /* A few other simple inheritance properties. */
1755 {
1756 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1757 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1758 sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1760 /** \todo
1761 * We currently treat text-decoration as if it were a simple inherited
1762 * property (fixme). This code may need changing once we do the
1763 * special fill/stroke inheritance mentioned by the spec.
1764 */
1765 sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1766 parent->text_decoration);
1768 //nyi: font-size-adjust, // <number> | none | inherit
1769 //nyi: glyph-orientation-horizontal,
1770 //nyi: glyph-orientation-vertical,
1771 }
1773 /* Properties that involve length but are easy in other respects. */
1774 {
1775 /* The difficulty with lengths is that font-relative units need adjusting if the font
1776 * varies between parent & child.
1777 *
1778 * Lengths specified in the existing child can stay as they are: its computed font
1779 * specification should stay unchanged, so em & ex lengths should continue to mean the same
1780 * size.
1781 *
1782 * Lengths specified in the dying parent in em or ex need to be scaled according to the
1783 * ratio of em or ex size between parent & child.
1784 */
1785 double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1787 SPILength SPStyle::*const lfields[] = {
1788 &SPStyle::stroke_width,
1789 &SPStyle::text_indent
1790 };
1791 for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1792 SPILength SPStyle::*fld = lfields[i];
1793 sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1794 parent->*fld,
1795 parent_child_em_ratio);
1796 }
1798 SPILengthOrNormal SPStyle::*const nfields[] = {
1799 &SPStyle::letter_spacing,
1800 &SPStyle::line_height,
1801 &SPStyle::word_spacing
1802 };
1803 for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1804 SPILengthOrNormal SPStyle::*fld = nfields[i];
1805 sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1806 parent->*fld,
1807 parent_child_em_ratio);
1808 }
1810 //nyi: &SPStyle::kerning: length or `auto'
1812 /* fixme: Move stroke-dash and stroke-dash-offset here once they
1813 can accept units. */
1814 }
1816 /* Properties that involve a URI but are easy in other respects. */
1817 {
1818 /** \todo
1819 * Could cause problems if original object was in another document
1820 * and it used a relative URL. (At the time of writing, we don't
1821 * allow hrefs to other documents, so this isn't a problem yet.)
1822 * Paint properties also allow URIs.
1823 */
1824 //nyi: cursor, // may involve change in effect, but we can't do much better
1825 //nyi: color-profile,
1827 // Markers (marker-start, marker-mid, marker-end).
1828 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1829 sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1830 }
1831 }
1833 /* CSS2 */
1834 /* Font */
1836 if (style->text && parent->text) {
1837 sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1838 parent->text->font_family);
1839 }
1841 /* Properties that don't inherit by default. Most of these need special handling. */
1842 {
1843 /*
1844 * opacity's effect is cumulative; we set the new value to the combined effect. The
1845 * default value for opacity is 1.0, not inherit. (Note that stroke-opacity and
1846 * fill-opacity are quite different from opacity, and don't need any special handling.)
1847 *
1848 * Cases:
1849 * - parent & child were each previously unset, in which case the effective
1850 * opacity value is 1.0, and style should remain unset.
1851 * - parent was previously unset (so computed opacity value of 1.0)
1852 * and child was set to inherit. The merged child should
1853 * get a value of 1.0, and shouldn't inherit (lest the new parent
1854 * has a different opacity value). Given that opacity's default
1855 * value is 1.0 (rather than inherit), we might as well have the
1856 * merged child's opacity be unset.
1857 * - parent was previously unset (so opacity 1.0), and child was set to a number.
1858 * The merged child should retain its existing settings (though it doesn't matter
1859 * if we make it unset if that number was 1.0).
1860 * - parent was inherit and child was unset. Merged child should be set to inherit.
1861 * - parent was inherit and child was inherit. (We can't in general reproduce this
1862 * effect (short of introducing a new group), but setting opacity to inherit is rare.)
1863 * If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1864 * child's value should be set to the product of the two, i.e. the square of the
1865 * inherited value, and should not be marked as inherit. (This decision assumes that it
1866 * is more important to retain the effective opacity than to retain the inheriting
1867 * effect, and assumes that the inheriting effect either isn't important enough to create
1868 * a group or isn't common enough to bother maintaining the code to create a group.) If
1869 * the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1870 * closer to maintaining the effect.
1871 * - parent was inherit and child was set to a numerical value. If the child's value
1872 * was 1.0, then the merged child should have the same settings as the parent.
1873 * If the child's value was 0, then the merged child should also be set to 0.
1874 * If the child's value was anything else, then we do the same as for the inherit/inherit
1875 * case above: have the merged child set to the product of the two opacities and not
1876 * marked as inherit, for the same reasons as for that case.
1877 * - parent was set to a value, and child was unset. The merged child should have
1878 * parent's settings.
1879 * - parent was set to a value, and child was inherit. The merged child should
1880 * be set to the product, i.e. the square of the parent's value.
1881 * - parent & child are each set to a value. The merged child should be set to the
1882 * product.
1883 */
1884 if ( !style->opacity.set
1885 || ( !style->opacity.inherit
1886 && style->opacity.value == SP_SCALE24_MAX ) )
1887 {
1888 style->opacity = parent->opacity;
1889 } else {
1890 /* Ensure that style's computed value is up-to-date. */
1891 if (style->opacity.inherit) {
1892 style->opacity.value = parent->opacity.value;
1893 }
1895 /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1896 style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1897 parent->opacity.value);
1899 style->opacity.inherit = (parent->opacity.inherit
1900 && style->opacity.inherit
1901 && (parent->opacity.value == 0 ||
1902 parent->opacity.value == SP_SCALE24_MAX));
1903 style->opacity.set = ( style->opacity.inherit
1904 || style->opacity.value < SP_SCALE24_MAX );
1905 }
1907 /* display is in principle similar to opacity, but implementation is easier. */
1908 if ( parent->display.set && !parent->display.inherit
1909 && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1910 style->display.value = SP_CSS_DISPLAY_NONE;
1911 style->display.set = true;
1912 style->display.inherit = false;
1913 } else if (style->display.inherit) {
1914 style->display.value = parent->display.value;
1915 style->display.set = parent->display.set;
1916 style->display.inherit = parent->display.inherit;
1917 } else {
1918 /* Leave as is. (display doesn't inherit by default.) */
1919 }
1921 /* enable-background - this is rather complicated, because
1922 * it is valid only when applied to container elements.
1923 * Let's check a simple case anyhow. */
1924 if (parent->enable_background.set
1925 && !parent->enable_background.inherit
1926 && style->enable_background.inherit)
1927 {
1928 style->enable_background.set = true;
1929 style->enable_background.inherit = false;
1930 style->enable_background.value = parent->enable_background.value;
1931 }
1933 /** \todo
1934 * fixme: Check that we correctly handle all properties that don't
1935 * inherit by default (as shown in
1936 * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1937 */
1938 }
1940 /* SPIPaint properties (including color). */
1941 {
1942 /** \todo
1943 * Think about the issues involved if specified as currentColor or
1944 * if specified relative to colorProfile, and if the currentColor or
1945 * colorProfile differs between parent \& child. See also comments
1946 * elsewhere in this function about URIs.
1947 */
1948 SPIPaint SPStyle::*const fields[] = {
1949 &SPStyle::color,
1950 &SPStyle::fill,
1951 &SPStyle::stroke
1952 };
1953 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1954 SPIPaint SPStyle::*const fld = fields[i];
1955 sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
1956 }
1957 }
1959 /* Things from SVG 1.2 or CSS3. */
1960 {
1961 /* Note: If we ever support setting string values for text-align then we'd need strdup
1962 * handling here. */
1963 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
1965 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
1966 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
1967 }
1969 /* Note: this will need length handling once dasharray supports units. */
1970 if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
1971 && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
1972 {
1973 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1974 if (style->stroke_dash.n_dash > 0) {
1975 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1976 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1977 }
1978 style->stroke_dasharray_set = parent->stroke_dasharray_set;
1979 style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
1980 }
1982 /* Note: this will need length handling once dasharray_offset supports units. */
1983 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1984 style->stroke_dash.offset = parent->stroke_dash.offset;
1985 style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
1986 /* fixme: we don't currently allow stroke-dashoffset to be `inherit'. TODO: Try to
1987 * represent it as a normal SPILength; though will need to do something about existing
1988 * users of stroke_dash.offset and stroke_dashoffset_set. */
1989 }
1991 /* TODO: deal with filters */
1992 }
1996 /**
1997 * Disconnects from possible fill and stroke paint servers.
1998 */
1999 static void
2000 sp_style_paint_server_release(SPObject *obj, SPStyle *style)
2001 {
2002 SPPaintServer *server=static_cast<SPPaintServer *>(obj);
2003 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2004 && (server == style->fill.value.paint.server))
2005 {
2006 sp_style_paint_clear(style, &style->fill);
2007 }
2009 if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2010 && (server == style->stroke.value.paint.server))
2011 {
2012 sp_style_paint_clear(style, &style->stroke);
2013 }
2014 }
2019 /**
2020 * Emit style modified signal on style's object if server is style's fill
2021 * or stroke paint server.
2022 */
2023 static void
2024 sp_style_paint_server_modified(SPObject *obj, guint flags, SPStyle *style)
2025 {
2026 SPPaintServer *server = static_cast<SPPaintServer *>(obj);
2027 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2028 && (server == style->fill.value.paint.server))
2029 {
2030 if (style->object) {
2031 /** \todo
2032 * fixme: I do not know, whether it is optimal - we are
2033 * forcing reread of everything (Lauris)
2034 */
2035 /** \todo
2036 * fixme: We have to use object_modified flag, because parent
2037 * flag is only available downstreams.
2038 */
2039 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2040 }
2041 } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2042 && (server == style->stroke.value.paint.server))
2043 {
2044 if (style->object) {
2045 /// \todo fixme:
2046 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2047 }
2048 } else {
2049 g_assert_not_reached();
2050 }
2051 }
2055 /**
2056 *
2057 */
2058 static void
2059 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2060 {
2061 sp_style_paint_clear(style, paint);
2063 if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2064 paint->currentcolor = TRUE;
2065 paint->type = SP_PAINT_TYPE_COLOR;
2066 sp_color_copy(&paint->value.color, &style->color.value.color);
2067 return;
2068 }
2070 paint->type = parent->type;
2071 switch (paint->type) {
2072 case SP_PAINT_TYPE_COLOR:
2073 sp_color_copy(&paint->value.color, &parent->value.color);
2074 break;
2075 case SP_PAINT_TYPE_PAINTSERVER:
2076 paint->value.paint.server = parent->value.paint.server;
2077 paint->value.paint.uri = parent->value.paint.uri;
2078 if (paint->value.paint.server) {
2079 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
2080 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2081 if (paint == &style->fill) {
2082 style->fill_hreffed = true;
2083 } else {
2084 assert(paint == &style->stroke);
2085 style->stroke_hreffed = true;
2086 }
2087 }
2088 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
2089 SPObject *server = paint->value.paint.server;
2090 sigc::connection release_connection
2091 = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
2092 sigc::connection modified_connection
2093 = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
2094 if (paint == &style->fill) {
2095 style->fill_release_connection = release_connection;
2096 style->fill_modified_connection = modified_connection;
2097 } else {
2098 assert(paint == &style->stroke);
2099 style->stroke_release_connection = release_connection;
2100 style->stroke_modified_connection = modified_connection;
2101 }
2102 }
2103 }
2104 break;
2105 case SP_PAINT_TYPE_NONE:
2106 break;
2107 default:
2108 g_assert_not_reached();
2109 break;
2110 }
2111 }
2114 /**
2115 * Merge filter style from parent.
2116 * Filter effects do not inherit by default
2117 */
2118 static void
2119 sp_style_merge_ifilter(SPStyle *style, SPIFilter *child, SPIFilter const *parent)
2120 {
2121 sp_style_filter_clear(style, child);
2122 child->set = parent->set;
2123 child->inherit = parent->inherit;
2124 child->filter = parent->filter;
2125 child->uri = parent->uri;
2126 sp_object_href(SP_OBJECT(child), style);
2127 style->filter_hreffed = true;
2128 }
2130 /**
2131 * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2132 * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2133 * complete cascaded style to style_clipboard. When you need a CSS string
2134 * for an object in the document tree, you normally call
2135 * sp_style_write_difference instead to take into account the object's parent.
2136 *
2137 * \pre style != NULL.
2138 * \pre flags in {IFSET, ALWAYS}.
2139 * \post ret != NULL.
2140 */
2141 gchar *
2142 sp_style_write_string(SPStyle const *const style, guint const flags)
2143 {
2144 /** \todo
2145 * Merge with write_difference, much duplicate code!
2146 */
2147 g_return_val_if_fail(style != NULL, NULL);
2148 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2149 (flags == SP_STYLE_FLAG_ALWAYS) ),
2150 NULL);
2152 gchar c[BMAX];
2153 gchar *p = c;
2154 *p = '\0';
2156 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2157 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2158 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2159 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2160 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2162 /* Text */
2163 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2164 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2165 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2166 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2167 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2168 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2169 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2170 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2171 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2172 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2174 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2176 /// \todo fixme: Per type methods need default flag too (lauris)
2177 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2178 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2179 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2180 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2181 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2182 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2183 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2184 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2185 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2187 marker_status("sp_style_write_string: Writing markers");
2188 if (style->marker[SP_MARKER_LOC].set) {
2189 p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2190 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2191 p += g_snprintf(p, c + BMAX - p, "marker:none;");
2192 }
2193 if (style->marker[SP_MARKER_LOC_START].set) {
2194 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2195 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2196 p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2197 }
2198 if (style->marker[SP_MARKER_LOC_MID].set) {
2199 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2200 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2201 p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2202 }
2203 if (style->marker[SP_MARKER_LOC_END].set) {
2204 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2205 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2206 p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2207 }
2209 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2211 /** \todo fixme: */
2212 if ((flags == SP_STYLE_FLAG_ALWAYS)
2213 || style->stroke_dasharray_set)
2214 {
2215 if (style->stroke_dasharray_inherit) {
2216 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2217 } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2218 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2219 gint i;
2220 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2221 Inkscape::CSSOStringStream os;
2222 if (i) {
2223 os << ", ";
2224 }
2225 os << style->stroke_dash.dash[i];
2226 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2227 }
2228 if (p < c + BMAX) {
2229 *p++ = ';';
2230 }
2231 } else {
2232 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2233 }
2234 }
2236 /** \todo fixme: */
2237 if (style->stroke_dashoffset_set) {
2238 Inkscape::CSSOStringStream os;
2239 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2240 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2241 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2242 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2243 }
2245 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2247 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2248 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2249 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2251 /* filter: */
2252 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2254 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2256 /* fixme: */
2257 p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2259 /* Get rid of trailing `;'. */
2260 if (p != c) {
2261 --p;
2262 if (*p == ';') {
2263 *p = '\0';
2264 }
2265 }
2267 return g_strdup(c);
2268 }
2271 #define STYLE_BUF_MAX
2274 /**
2275 * Dumps style to CSS string, see sp_style_write_string()
2276 *
2277 * \pre from != NULL.
2278 * \pre to != NULL.
2279 * \post ret != NULL.
2280 */
2281 gchar *
2282 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2283 {
2284 g_return_val_if_fail(from != NULL, NULL);
2285 g_return_val_if_fail(to != NULL, NULL);
2287 gchar c[BMAX], *p = c;
2288 *p = '\0';
2290 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2291 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2292 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2293 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2294 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2296 /* Text */
2297 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2298 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2299 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2300 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2301 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2302 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2303 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2304 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2305 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2306 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2308 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2310 /// \todo fixme: Per type methods need default flag too
2311 if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2312 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2313 }
2314 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2315 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2316 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2317 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2318 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2319 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2320 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2321 &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2322 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2323 &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2324 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2325 &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2326 /** \todo fixme: */
2327 if (from->stroke_dasharray_set) {
2328 if (from->stroke_dasharray_inherit) {
2329 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2330 } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2331 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2332 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2333 Inkscape::CSSOStringStream os;
2334 if (i) {
2335 os << ", ";
2336 }
2337 os << from->stroke_dash.dash[i];
2338 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2339 }
2340 p += g_snprintf(p, c + BMAX - p, ";");
2341 }
2342 }
2343 /* fixme: */
2344 if (from->stroke_dashoffset_set) {
2345 Inkscape::CSSOStringStream os;
2346 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2347 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2348 }
2349 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2351 /* markers */
2352 marker_status("sp_style_write_difference: Writing markers");
2353 if (from->marker[SP_MARKER_LOC].value != NULL) {
2354 p += g_snprintf(p, c + BMAX - p, "marker:%s;", from->marker[SP_MARKER_LOC].value);
2355 }
2356 if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2357 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2358 }
2359 if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2360 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", from->marker[SP_MARKER_LOC_MID].value);
2361 }
2362 if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2363 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", from->marker[SP_MARKER_LOC_END].value);
2364 }
2366 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2367 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2368 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2370 /* filter: */
2371 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2373 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2375 p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2377 /** \todo
2378 * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2379 * flag is mainly only for attributes that don't handle explicit unset well.
2380 * We may need to revisit the behaviour of this routine.
2381 */
2383 /* Get rid of trailing `;'. */
2384 if (p != c) {
2385 --p;
2386 if (*p == ';') {
2387 *p = '\0';
2388 }
2389 }
2391 return g_strdup(c);
2392 }
2396 /**
2397 * Reset all style properties.
2398 */
2399 static void
2400 sp_style_clear(SPStyle *style)
2401 {
2402 g_return_if_fail(style != NULL);
2404 sp_style_paint_clear(style, &style->fill);
2405 sp_style_paint_clear(style, &style->stroke);
2406 sp_style_filter_clear(style, &style->filter);
2407 if (style->stroke_dash.dash) {
2408 g_free(style->stroke_dash.dash);
2409 }
2411 /** \todo fixme: Do that text manipulation via parents */
2412 SPObject *object = style->object;
2413 gint const refcount = style->refcount;
2414 SPTextStyle *text = style->text;
2415 unsigned const text_private = style->text_private;
2416 memset(style, 0, sizeof(SPStyle));
2417 style->refcount = refcount;
2418 style->object = object;
2419 style->text = text;
2420 style->text_private = text_private;
2421 /* fixme: */
2422 style->text->font.set = FALSE;
2423 style->text->font_family.set = FALSE;
2425 style->font_size.set = FALSE;
2426 style->font_size.type = SP_FONT_SIZE_LITERAL;
2427 style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2428 style->font_size.computed = 12.0;
2429 style->font_style.set = FALSE;
2430 style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2431 style->font_variant.set = FALSE;
2432 style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2433 style->font_weight.set = FALSE;
2434 style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2435 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2436 style->font_stretch.set = FALSE;
2437 style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2439 /* text */
2440 style->text_indent.set = FALSE;
2441 style->text_indent.unit = SP_CSS_UNIT_NONE;
2442 style->text_indent.computed = 0.0;
2444 style->text_align.set = FALSE;
2445 style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2447 style->text_decoration.set = FALSE;
2448 style->text_decoration.underline = FALSE;
2449 style->text_decoration.overline = FALSE;
2450 style->text_decoration.line_through = FALSE;
2451 style->text_decoration.blink = FALSE;
2453 style->line_height.set = FALSE;
2454 style->line_height.unit = SP_CSS_UNIT_PERCENT;
2455 style->line_height.normal = TRUE;
2456 style->line_height.value = style->line_height.computed = 1.0;
2458 style->letter_spacing.set = FALSE;
2459 style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2460 style->letter_spacing.normal = TRUE;
2461 style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2463 style->word_spacing.set = FALSE;
2464 style->word_spacing.unit = SP_CSS_UNIT_NONE;
2465 style->word_spacing.normal = TRUE;
2466 style->word_spacing.value = style->word_spacing.computed = 0.0;
2468 style->text_transform.set = FALSE;
2469 style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2471 style->direction.set = FALSE;
2472 style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2474 style->block_progression.set = FALSE;
2475 style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2477 style->writing_mode.set = FALSE;
2478 style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2481 style->text_anchor.set = FALSE;
2482 style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2485 style->opacity.value = SP_SCALE24_MAX;
2486 style->visibility.set = FALSE;
2487 style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2488 style->display.set = FALSE;
2489 style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2490 style->overflow.set = FALSE;
2491 style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2493 style->color.type = SP_PAINT_TYPE_COLOR;
2494 sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
2496 style->fill.type = SP_PAINT_TYPE_COLOR;
2497 sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
2498 style->fill_opacity.value = SP_SCALE24_MAX;
2499 style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2501 style->stroke.type = SP_PAINT_TYPE_NONE;
2502 style->stroke.set = FALSE;
2503 sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
2504 style->stroke_opacity.value = SP_SCALE24_MAX;
2506 style->stroke_width.set = FALSE;
2507 style->stroke_width.unit = SP_CSS_UNIT_NONE;
2508 style->stroke_width.computed = 1.0;
2510 style->stroke_linecap.set = FALSE;
2511 style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2512 style->stroke_linejoin.set = FALSE;
2513 style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2515 style->stroke_miterlimit.set = FALSE;
2516 style->stroke_miterlimit.value = 4.0;
2518 style->stroke_dash.n_dash = 0;
2519 style->stroke_dash.dash = NULL;
2520 style->stroke_dash.offset = 0.0;
2522 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2523 g_free(style->marker[i].value);
2524 style->marker[i].set = FALSE;
2525 }
2527 style->filter.set = FALSE;
2528 //Are these really needed?
2529 style->filter.inherit = FALSE;
2530 style->filter.uri = NULL;
2531 style->filter.filter = FALSE;
2533 style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2534 style->enable_background.set = false;
2535 style->enable_background.inherit = false;
2536 }
2540 /**
2541 *
2542 */
2543 static void
2544 sp_style_read_dash(SPStyle *style, gchar const *str)
2545 {
2546 /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2547 style->stroke_dasharray_set = TRUE;
2549 if (strcmp(str, "inherit") == 0) {
2550 style->stroke_dasharray_inherit = true;
2551 return;
2552 }
2553 style->stroke_dasharray_inherit = false;
2555 NRVpathDash &dash = style->stroke_dash;
2556 g_free(dash.dash);
2557 dash.dash = NULL;
2559 if (strcmp(str, "none") == 0) {
2560 dash.n_dash = 0;
2561 return;
2562 }
2564 gint n_dash = 0;
2565 gdouble d[64];
2566 gchar *e = NULL;
2568 bool LineSolid=true;
2569 while (e != str && n_dash < 64) {
2570 /* TODO: Should allow <length> rather than just a unitless (px) number. */
2571 d[n_dash] = g_ascii_strtod(str, (char **) &e);
2572 if (d[n_dash] > 0.00000001)
2573 LineSolid = false;
2574 if (e != str) {
2575 n_dash += 1;
2576 str = e;
2577 }
2578 while (str && *str && !isalnum(*str)) str += 1;
2579 }
2581 if (LineSolid) {
2582 dash.n_dash = 0;
2583 return;
2584 }
2586 if (n_dash > 0) {
2587 dash.dash = g_new(gdouble, n_dash);
2588 memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2589 dash.n_dash = n_dash;
2590 }
2591 }
2594 /*#########################
2595 ## SPTextStyle operations
2596 #########################*/
2599 /**
2600 * Return new SPTextStyle object with default settings.
2601 */
2602 static SPTextStyle *
2603 sp_text_style_new()
2604 {
2605 SPTextStyle *ts = g_new0(SPTextStyle, 1);
2606 ts->refcount = 1;
2607 sp_text_style_clear(ts);
2609 ts->font.value = g_strdup("Bitstream Vera Sans");
2610 ts->font_family.value = g_strdup("Bitstream Vera Sans");
2612 return ts;
2613 }
2616 /**
2617 * Clear text style settings.
2618 */
2619 static void
2620 sp_text_style_clear(SPTextStyle *ts)
2621 {
2622 ts->font.set = FALSE;
2623 ts->font_family.set = FALSE;
2624 }
2628 /**
2629 * Reduce refcount of text style and possibly free it.
2630 */
2631 static SPTextStyle *
2632 sp_text_style_unref(SPTextStyle *st)
2633 {
2634 st->refcount -= 1;
2636 if (st->refcount < 1) {
2637 g_free(st->font.value);
2638 g_free(st->font_family.value);
2639 g_free(st);
2640 }
2642 return NULL;
2643 }
2647 /**
2648 * Return duplicate of text style.
2649 */
2650 static SPTextStyle *
2651 sp_text_style_duplicate_unset(SPTextStyle *st)
2652 {
2653 SPTextStyle *nt = g_new0(SPTextStyle, 1);
2654 nt->refcount = 1;
2656 nt->font.value = g_strdup(st->font.value);
2657 nt->font_family.value = g_strdup(st->font_family.value);
2659 return nt;
2660 }
2664 /**
2665 * Write SPTextStyle object into string.
2666 */
2667 static guint
2668 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2669 {
2670 gint d = 0;
2672 // We do not do diffing for text style
2673 if (flags == SP_STYLE_FLAG_IFDIFF)
2674 flags = SP_STYLE_FLAG_IFSET;
2676 d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2677 return d;
2678 }
2682 /* The following sp_tyle_read_* functions ignore invalid values, as per
2683 * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2684 *
2685 * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2686 * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2687 * pairs, in which case SVG's error-handling rules
2688 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2689 */
2692 /**
2693 * Set SPIFloat object from string.
2694 */
2695 static void
2696 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2697 {
2698 if (!strcmp(str, "inherit")) {
2699 val->set = TRUE;
2700 val->inherit = TRUE;
2701 } else {
2702 gfloat value;
2703 if (sp_svg_number_read_f(str, &value)) {
2704 val->set = TRUE;
2705 val->inherit = FALSE;
2706 val->value = value;
2707 }
2708 }
2709 }
2713 /**
2714 * Set SPIScale24 object from string.
2715 */
2716 static void
2717 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2718 {
2719 if (!strcmp(str, "inherit")) {
2720 val->set = TRUE;
2721 val->inherit = TRUE;
2722 } else {
2723 gfloat value;
2724 if (sp_svg_number_read_f(str, &value)) {
2725 val->set = TRUE;
2726 val->inherit = FALSE;
2727 value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
2728 val->value = SP_SCALE24_FROM_FLOAT(value);
2729 }
2730 }
2731 }
2733 /**
2734 * Reads a style value and performs lookup based on the given style value enumerations.
2735 */
2736 static void
2737 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2738 bool const can_explicitly_inherit)
2739 {
2740 if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2741 val->set = TRUE;
2742 val->inherit = TRUE;
2743 } else {
2744 for (unsigned i = 0; dict[i].key; i++) {
2745 if (!strcmp(str, dict[i].key)) {
2746 val->set = TRUE;
2747 val->inherit = FALSE;
2748 val->value = dict[i].value;
2749 /* Save copying for values not needing it */
2750 val->computed = val->value;
2751 break;
2752 }
2753 }
2754 }
2755 }
2759 /**
2760 * Set SPIString object from string.
2761 */
2762 static void
2763 sp_style_read_istring(SPIString *val, gchar const *str)
2764 {
2765 g_free(val->value);
2767 if (!strcmp(str, "inherit")) {
2768 val->set = TRUE;
2769 val->inherit = TRUE;
2770 val->value = NULL;
2771 } else {
2772 val->set = TRUE;
2773 val->inherit = FALSE;
2774 val->value = g_strdup(str);
2775 }
2776 }
2780 /**
2781 * Set SPILength object from string.
2782 */
2783 static void
2784 sp_style_read_ilength(SPILength *val, gchar const *str)
2785 {
2786 if (!strcmp(str, "inherit")) {
2787 val->set = TRUE;
2788 val->inherit = TRUE;
2789 } else {
2790 gdouble value;
2791 gchar *e;
2792 /** \todo fixme: Move this to standard place (Lauris) */
2793 value = g_ascii_strtod(str, &e);
2794 if ((gchar const *) e != str) {
2795 /** \todo
2796 * Allow the number of px per inch to vary (document preferences,
2797 * X server or whatever). E.g. don't fill in computed here, do
2798 * it at the same time as percentage units are done.
2799 */
2800 if (!*e) {
2801 /* Userspace */
2802 val->unit = SP_CSS_UNIT_NONE;
2803 val->computed = value;
2804 } else if (!strcmp(e, "px")) {
2805 /* Userspace */
2806 val->unit = SP_CSS_UNIT_PX;
2807 val->computed = value;
2808 } else if (!strcmp(e, "pt")) {
2809 /* Userspace / DEVICESCALE */
2810 val->unit = SP_CSS_UNIT_PT;
2811 val->computed = value * PX_PER_PT;
2812 } else if (!strcmp(e, "pc")) {
2813 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2814 val->unit = SP_CSS_UNIT_PC;
2815 val->computed = value * PX_PER_PT * 12;
2816 } else if (!strcmp(e, "mm")) {
2817 val->unit = SP_CSS_UNIT_MM;
2818 val->computed = value * PX_PER_MM;
2819 } else if (!strcmp(e, "cm")) {
2820 val->unit = SP_CSS_UNIT_CM;
2821 val->computed = value * PX_PER_CM;
2822 } else if (!strcmp(e, "in")) {
2823 val->unit = SP_CSS_UNIT_IN;
2824 val->computed = value * PX_PER_IN;
2825 } else if (!strcmp(e, "em")) {
2826 /* EM square */
2827 val->unit = SP_CSS_UNIT_EM;
2828 val->value = value;
2829 } else if (!strcmp(e, "ex")) {
2830 /* ex square */
2831 val->unit = SP_CSS_UNIT_EX;
2832 val->value = value;
2833 } else if (!strcmp(e, "%")) {
2834 /* Percentage */
2835 val->unit = SP_CSS_UNIT_PERCENT;
2836 val->value = value * 0.01;
2837 } else {
2838 /* Invalid */
2839 return;
2840 }
2841 val->set = TRUE;
2842 val->inherit = FALSE;
2843 }
2844 }
2845 }
2847 /**
2848 * Set SPILengthOrNormal object from string.
2849 */
2850 static void
2851 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2852 {
2853 if (!strcmp(str, "normal")) {
2854 val->set = TRUE;
2855 val->inherit = FALSE;
2856 val->normal = TRUE;
2857 val->unit = SP_CSS_UNIT_NONE;
2858 val->value = val->computed = 0.0;
2859 } else {
2860 SPILength length;
2861 sp_style_read_ilength(&length, str);
2862 val->set = length.set;
2863 val->inherit = length.inherit;
2864 val->normal = FALSE;
2865 val->unit = length.unit;
2866 val->value = length.value;
2867 val->computed = length.computed;
2868 }
2869 }
2871 /**
2872 * Set SPITextDecoration object from string.
2873 */
2874 static void
2875 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2876 {
2877 if (!strcmp(str, "inherit")) {
2878 val->set = TRUE;
2879 val->inherit = TRUE;
2880 } else if (!strcmp(str, "none")) {
2881 val->set = TRUE;
2882 val->inherit = FALSE;
2883 val->underline = FALSE;
2884 val->overline = FALSE;
2885 val->line_through = FALSE;
2886 val->blink = FALSE;
2887 } else {
2888 bool found_underline = false;
2889 bool found_overline = false;
2890 bool found_line_through = false;
2891 bool found_blink = false;
2892 for ( ; *str ; str++ ) {
2893 if (*str == ' ') continue;
2894 if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2895 found_underline = true;
2896 str += 9;
2897 } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2898 found_overline = true;
2899 str += 8;
2900 } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2901 found_line_through = true;
2902 str += 12;
2903 } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2904 found_blink = true;
2905 str += 5;
2906 } else {
2907 return; // invalid value
2908 }
2909 }
2910 if (!(found_underline || found_overline || found_line_through || found_blink)) {
2911 return; // invalid value: empty
2912 }
2913 val->set = TRUE;
2914 val->inherit = FALSE;
2915 val->underline = found_underline;
2916 val->overline = found_overline;
2917 val->line_through = found_line_through;
2918 val->blink = found_blink;
2919 }
2920 }
2922 /**
2923 * Set SPIPaint object from string containing an integer value.
2924 * \param document Ignored
2925 */
2926 static void
2927 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2928 {
2929 paint->currentcolor = FALSE; /* currentColor not a valid <color>. */
2930 if (!strcmp(str, "inherit")) {
2931 paint->set = TRUE;
2932 paint->inherit = TRUE;
2933 } else {
2934 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2935 if (rgb0 != 0xff) {
2936 paint->type = SP_PAINT_TYPE_COLOR;
2937 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2938 paint->set = TRUE;
2939 paint->inherit = FALSE;
2940 }
2941 }
2942 }
2945 /**
2946 * Set SPIPaint object from string.
2947 *
2948 * \pre paint == \&style.fill || paint == \&style.stroke.
2949 */
2950 static void
2951 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2952 {
2953 while (isspace(*str)) {
2954 ++str;
2955 }
2957 if (streq(str, "inherit")) {
2958 paint->set = TRUE;
2959 paint->inherit = TRUE;
2960 paint->currentcolor = FALSE;
2961 } else if (streq(str, "currentColor") && paint != &style->color) {
2962 paint->set = TRUE;
2963 paint->inherit = FALSE;
2964 paint->currentcolor = TRUE;
2965 } else if (streq(str, "none") && paint != &style->color) {
2966 paint->type = SP_PAINT_TYPE_NONE;
2967 paint->set = TRUE;
2968 paint->inherit = FALSE;
2969 paint->currentcolor = FALSE;
2970 } else if (strneq(str, "url", 3) && paint != &style->color) {
2971 // this is alloc'd uri, but seems to be shared with a parent
2972 // potentially, so it's not any easy g_free case...
2973 paint->value.paint.uri = extract_uri(str);
2974 if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
2975 paint->type = SP_PAINT_TYPE_NONE;
2976 return;
2977 }
2978 paint->type = SP_PAINT_TYPE_PAINTSERVER;
2979 paint->set = TRUE;
2980 paint->inherit = FALSE;
2981 paint->currentcolor = FALSE;
2982 if (document) {
2983 SPObject *ps = sp_uri_reference_resolve(document, str);
2984 if (ps && SP_IS_PAINT_SERVER(ps)) {
2985 paint->value.paint.server = SP_PAINT_SERVER(ps);
2986 if (style->object && !style->cloned) {
2987 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2988 if (paint == &style->fill) {
2989 style->fill_hreffed = true;
2990 } else {
2991 assert(paint == &style->stroke);
2992 style->stroke_hreffed = true;
2993 }
2994 }
2995 if (style->object || style->cloned) {
2996 SPObject *server = paint->value.paint.server;
2997 sigc::connection release_connection
2998 = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
2999 sigc::connection modified_connection
3000 = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
3001 if (paint == &style->fill) {
3002 style->fill_release_connection = release_connection;
3003 style->fill_modified_connection = modified_connection;
3004 } else {
3005 assert(paint == &style->stroke);
3006 style->stroke_release_connection = release_connection;
3007 style->stroke_modified_connection = modified_connection;
3008 }
3009 }
3010 } else {
3011 paint->value.paint.server = NULL;
3012 }
3013 }
3014 } else {
3015 guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3016 if (rgb0 != 0xff) {
3017 paint->type = SP_PAINT_TYPE_COLOR;
3018 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
3019 paint->set = TRUE;
3020 paint->inherit = FALSE;
3021 paint->currentcolor = FALSE;
3023 while (g_ascii_isspace(*str)) {
3024 ++str;
3025 }
3026 if (strneq(str, "icc-color(", 10)) {
3027 SVGICCColor* tmp = new SVGICCColor();
3028 if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3029 delete tmp;
3030 tmp = 0;
3031 }
3032 paint->iccColor = tmp;
3033 }
3034 }
3035 }
3036 }
3040 /**
3041 * Set SPIFontSize object from string.
3042 */
3043 static void
3044 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3045 {
3046 if (!strcmp(str, "inherit")) {
3047 val->set = TRUE;
3048 val->inherit = TRUE;
3049 } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3050 for (unsigned i = 0; enum_font_size[i].key; i++) {
3051 if (!strcmp(str, enum_font_size[i].key)) {
3052 val->set = TRUE;
3053 val->inherit = FALSE;
3054 val->type = SP_FONT_SIZE_LITERAL;
3055 val->value = enum_font_size[i].value;
3056 return;
3057 }
3058 }
3059 /* Invalid */
3060 return;
3061 } else {
3062 gdouble value;
3063 gchar *e;
3064 /* fixme: Move this to standard place (Lauris) */
3065 value = g_ascii_strtod(str, &e);
3066 if ((gchar const *) e != str) {
3067 if (!*e) {
3068 /* Userspace */
3069 } else if (!strcmp(e, "px")) {
3070 /* Userspace */
3071 } else if (!strcmp(e, "pt")) {
3072 /* Userspace * DEVICESCALE */
3073 value *= PX_PER_PT;
3074 } else if (!strcmp(e, "pc")) {
3075 /* 12pt */
3076 value *= PX_PER_PT * 12.0;
3077 } else if (!strcmp(e, "mm")) {
3078 value *= PX_PER_MM;
3079 } else if (!strcmp(e, "cm")) {
3080 value *= PX_PER_CM;
3081 } else if (!strcmp(e, "in")) {
3082 value *= PX_PER_IN;
3083 } else if (!strcmp(e, "%")) {
3084 /* Percentage */
3085 val->set = TRUE;
3086 val->inherit = FALSE;
3087 val->type = SP_FONT_SIZE_PERCENTAGE;
3088 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3089 return;
3090 } else {
3091 /* Invalid */
3092 return;
3093 }
3094 /* Length */
3095 val->set = TRUE;
3096 val->inherit = FALSE;
3097 val->type = SP_FONT_SIZE_LENGTH;
3098 val->computed = value;
3099 return;
3100 }
3101 }
3102 }
3106 /**
3107 * Set SPIFilter object from string.
3108 */
3109 static void
3110 sp_style_read_ifilter( SPIFilter *f, gchar const *str, SPStyle * style, SPDocument *document)
3111 {
3112 /* Try all possible values: inherit, none, uri */
3113 if (streq(str, "inherit")) {
3114 f->set = TRUE;
3115 f->inherit = TRUE;
3116 f->filter = NULL;
3117 } else if(streq(str, "none")) {
3118 f->set = TRUE;
3119 f->inherit = FALSE;
3120 f->filter = NULL;
3121 } else if (strneq(str, "url", 3)) {
3122 f->uri = extract_uri(str);
3123 if(f->uri == NULL || f->uri[0] == '\0') {
3124 g_warning("Specified filter url is empty");
3125 f->set = TRUE;
3126 f->inherit = FALSE;
3127 f->filter = NULL;
3128 return;
3129 }
3130 f->set = TRUE;
3131 f->inherit = FALSE;
3132 f->filter = NULL;
3133 if (document) {
3134 SPObject *obj;
3135 obj = sp_uri_reference_resolve(document, str);
3136 if (SP_IS_FILTER(obj)) {
3137 f->filter = SP_FILTER(obj);
3138 sp_object_href(SP_OBJECT(f->filter), style);
3139 style->filter_hreffed = true;
3140 //g_signal_connect(G_OBJECT(f->filter), "release",
3141 // G_CALLBACK(sp_style_filter_release), style);
3142 //g_signal_connect(G_OBJECT(f->filter), "modified",
3143 // G_CALLBACK(sp_style_filter_modified), style);
3144 } else {
3145 g_warning("Element '%s' not found or is not a filter", f->uri);
3146 }
3147 }
3149 } else {
3150 /* We shouldn't reach this if SVG input is well-formed */
3151 f->set = FALSE;
3152 f->inherit = FALSE;
3153 f->filter = NULL;
3154 f->uri = NULL;
3155 }
3156 }
3158 /**
3159 * Set SPIEnum object from repr attribute.
3160 */
3161 static void
3162 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3163 gchar const *key, SPStyleEnum const *dict,
3164 bool const can_explicitly_inherit)
3165 {
3166 gchar const *str = repr->attribute(key);
3167 if (str) {
3168 sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3169 }
3170 }
3174 /**
3175 * Set SPILength object from repr attribute.
3176 */
3177 static void
3178 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3179 {
3180 gchar const *str = repr->attribute(key);
3181 if (str) {
3182 sp_style_read_ilength(val, str);
3183 }
3184 }
3186 /**
3187 * Set SPIFontSize object from repr attribute.
3188 */
3189 static void
3190 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3191 {
3192 gchar const *str = repr->attribute(key);
3193 if (str) {
3194 sp_style_read_ifontsize(val, str);
3195 }
3196 }
3199 /**
3200 * Set SPIFloat object from repr attribute.
3201 */
3202 static void
3203 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3204 {
3205 gchar const *str = repr->attribute(key);
3206 if (str) {
3207 sp_style_read_ifloat(val, str);
3208 }
3209 }
3212 /**
3213 * Write SPIFloat object into string.
3214 */
3215 static gint
3216 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3217 SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3218 {
3219 Inkscape::CSSOStringStream os;
3221 if ((flags & SP_STYLE_FLAG_ALWAYS)
3222 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3223 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3224 && (!base->set || (val->value != base->value))))
3225 {
3226 if (val->inherit) {
3227 return g_snprintf(p, len, "%s:inherit;", key);
3228 } else {
3229 os << key << ":" << val->value << ";";
3230 return g_strlcpy(p, os.str().c_str(), len);
3231 }
3232 }
3233 return 0;
3234 }
3237 /**
3238 * Write SPIScale24 object into string.
3239 */
3240 static gint
3241 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3242 SPIScale24 const *const val, SPIScale24 const *const base,
3243 guint const flags)
3244 {
3245 Inkscape::CSSOStringStream os;
3247 if ((flags & SP_STYLE_FLAG_ALWAYS)
3248 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3249 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3250 && (!base->set || (val->value != base->value))))
3251 {
3252 if (val->inherit) {
3253 return g_snprintf(p, len, "%s:inherit;", key);
3254 } else {
3255 os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3256 return g_strlcpy(p, os.str().c_str(), len);
3257 }
3258 }
3259 return 0;
3260 }
3263 /**
3264 * Write SPIEnum object into string.
3265 */
3266 static gint
3267 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3268 SPStyleEnum const *const dict,
3269 SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3270 {
3271 if ((flags & SP_STYLE_FLAG_ALWAYS)
3272 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3273 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3274 && (!base->set || (val->computed != base->computed))))
3275 {
3276 if (val->inherit) {
3277 return g_snprintf(p, len, "%s:inherit;", key);
3278 }
3279 for (unsigned i = 0; dict[i].key; i++) {
3280 if (dict[i].value == static_cast< gint > (val->value) ) {
3281 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3282 }
3283 }
3284 }
3285 return 0;
3286 }
3290 /**
3291 * Write SPIString object into string.
3292 */
3293 static gint
3294 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3295 SPIString const *const val, SPIString const *const base, guint const flags)
3296 {
3297 if ((flags & SP_STYLE_FLAG_ALWAYS)
3298 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3299 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3300 && (!base->set || strcmp(val->value, base->value))))
3301 {
3302 if (val->inherit) {
3303 return g_snprintf(p, len, "%s:inherit;", key);
3304 } else {
3305 return g_snprintf(p, len, "%s:%s;", key, val->value);
3306 }
3307 }
3308 return 0;
3309 }
3312 /**
3313 *
3314 */
3315 static bool
3316 sp_length_differ(SPILength const *const a, SPILength const *const b)
3317 {
3318 if (a->unit != b->unit) {
3319 if (a->unit == SP_CSS_UNIT_EM) return true;
3320 if (a->unit == SP_CSS_UNIT_EX) return true;
3321 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3322 if (b->unit == SP_CSS_UNIT_EM) return true;
3323 if (b->unit == SP_CSS_UNIT_EX) return true;
3324 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3325 }
3327 return (a->computed != b->computed);
3328 }
3332 /**
3333 * Write SPILength object into string.
3334 */
3335 static gint
3336 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3337 SPILength const *const val, SPILength const *const base, guint const flags)
3338 {
3339 Inkscape::CSSOStringStream os;
3341 if ((flags & SP_STYLE_FLAG_ALWAYS)
3342 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3343 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3344 && (!base->set || sp_length_differ(val, base))))
3345 {
3346 if (val->inherit) {
3347 return g_snprintf(p, len, "%s:inherit;", key);
3348 } else {
3349 switch (val->unit) {
3350 case SP_CSS_UNIT_NONE:
3351 os << key << ":" << val->computed << ";";
3352 return g_strlcpy(p, os.str().c_str(), len);
3353 break;
3354 case SP_CSS_UNIT_PX:
3355 os << key << ":" << val->computed << "px;";
3356 return g_strlcpy(p, os.str().c_str(), len);
3357 break;
3358 case SP_CSS_UNIT_PT:
3359 os << key << ":" << val->computed * PT_PER_PX << "pt;";
3360 return g_strlcpy(p, os.str().c_str(), len);
3361 break;
3362 case SP_CSS_UNIT_PC:
3363 os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3364 return g_strlcpy(p, os.str().c_str(), len);
3365 break;
3366 case SP_CSS_UNIT_MM:
3367 os << key << ":" << val->computed * MM_PER_PX << "mm;";
3368 return g_strlcpy(p, os.str().c_str(), len);
3369 break;
3370 case SP_CSS_UNIT_CM:
3371 os << key << ":" << val->computed * CM_PER_PX << "cm;";
3372 return g_strlcpy(p, os.str().c_str(), len);
3373 break;
3374 case SP_CSS_UNIT_IN:
3375 os << key << ":" << val->computed * IN_PER_PX << "in;";
3376 return g_strlcpy(p, os.str().c_str(), len);
3377 break;
3378 case SP_CSS_UNIT_EM:
3379 os << key << ":" << val->value << "em;";
3380 return g_strlcpy(p, os.str().c_str(), len);
3381 break;
3382 case SP_CSS_UNIT_EX:
3383 os << key << ":" << val->value << "ex;";
3384 return g_strlcpy(p, os.str().c_str(), len);
3385 break;
3386 case SP_CSS_UNIT_PERCENT:
3387 os << key << ":" << (val->value * 100.0) << "%;";
3388 return g_strlcpy(p, os.str().c_str(), len);
3389 break;
3390 default:
3391 /* Invalid */
3392 break;
3393 }
3394 }
3395 }
3396 return 0;
3397 }
3400 /**
3401 *
3402 */
3403 static bool
3404 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3405 {
3406 if (a->normal != b->normal) return true;
3407 if (a->normal) return false;
3409 if (a->unit != b->unit) {
3410 if (a->unit == SP_CSS_UNIT_EM) return true;
3411 if (a->unit == SP_CSS_UNIT_EX) return true;
3412 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3413 if (b->unit == SP_CSS_UNIT_EM) return true;
3414 if (b->unit == SP_CSS_UNIT_EX) return true;
3415 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3416 }
3418 return (a->computed != b->computed);
3419 }
3421 /**
3422 * Write SPILengthOrNormal object into string.
3423 */
3424 static gint
3425 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3426 SPILengthOrNormal const *const val,
3427 SPILengthOrNormal const *const base,
3428 guint const flags)
3429 {
3430 if ((flags & SP_STYLE_FLAG_ALWAYS)
3431 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3432 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3433 && (!base->set || sp_lengthornormal_differ(val, base))))
3434 {
3435 if (val->normal) {
3436 return g_snprintf(p, len, "%s:normal;", key);
3437 } else {
3438 SPILength length;
3439 length.set = val->set;
3440 length.inherit = val->inherit;
3441 length.unit = val->unit;
3442 length.value = val->value;
3443 length.computed = val->computed;
3444 return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3445 }
3446 }
3447 return 0;
3448 }
3450 /**
3451 *
3452 */
3453 static bool
3454 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3455 {
3456 return a->underline != b->underline
3457 || a->overline != b->overline
3458 || a->line_through != b->line_through
3459 || a->blink != b->blink;
3460 }
3462 /**
3463 * Write SPITextDecoration object into string.
3464 */
3465 static gint
3466 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3467 SPITextDecoration const *const val,
3468 SPITextDecoration const *const base,
3469 guint const flags)
3470 {
3471 Inkscape::CSSOStringStream os;
3473 if ((flags & SP_STYLE_FLAG_ALWAYS)
3474 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3475 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3476 && (!base->set || sp_textdecoration_differ(val, base))))
3477 {
3478 if (val->inherit) {
3479 return g_snprintf(p, len, "%s:inherit;", key);
3480 } else {
3481 os << key << ":";
3482 if (val->underline || val->overline || val->line_through || val->blink) {
3483 if (val->underline) os << " underline";
3484 if (val->overline) os << " overline";
3485 if (val->line_through) os << " line-through";
3486 if (val->blink) os << " blink";
3487 } else
3488 os << "none";
3489 os << ";";
3490 return g_strlcpy(p, os.str().c_str(), len);
3491 }
3492 }
3493 return 0;
3494 }
3496 /**
3497 *
3498 */
3499 static bool
3500 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3501 {
3502 if (a->type != b->type)
3503 return true;
3504 if (a->type == SP_PAINT_TYPE_COLOR)
3505 return !(sp_color_is_equal(&a->value.color, &b->value.color)
3506 && ((a->iccColor == b->iccColor)
3507 || (a->iccColor && b->iccColor
3508 && (a->iccColor->colorProfile == b->iccColor->colorProfile)
3509 && (a->iccColor->colors == b->iccColor->colors))));
3510 /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3511 * in the string representation. */
3512 if (a->type == SP_PAINT_TYPE_PAINTSERVER)
3513 return (a->value.paint.server != b->value.paint.server);
3514 return false;
3515 }
3519 /**
3520 * Write SPIPaint object into string.
3521 */
3522 static gint
3523 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3524 SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3525 {
3526 if ((flags & SP_STYLE_FLAG_ALWAYS)
3527 || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3528 || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3529 && (!base->set || sp_paint_differ(paint, base))))
3530 {
3531 if (paint->inherit) {
3532 return g_snprintf(b, len, "%s:inherit;", key);
3533 } else if (paint->currentcolor) {
3534 return g_snprintf(b, len, "%s:currentColor;", key);
3535 } else {
3536 switch (paint->type) {
3537 case SP_PAINT_TYPE_COLOR: {
3538 char color_buf[8];
3539 sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
3540 if (paint->iccColor) {
3541 CSSOStringStream css;
3542 css << color_buf << " icc-color(" << paint->iccColor->colorProfile;
3543 for (vector<double>::const_iterator i(paint->iccColor->colors.begin()),
3544 iEnd(paint->iccColor->colors.end());
3545 i != iEnd; ++i) {
3546 css << ", " << *i;
3547 }
3548 css << ')';
3549 return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
3550 } else {
3551 return g_snprintf(b, len, "%s:%s;", key, color_buf);
3552 }
3553 }
3554 case SP_PAINT_TYPE_PAINTSERVER:
3555 return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
3556 default:
3557 break;
3558 }
3559 return g_snprintf(b, len, "%s:none;", key);
3560 }
3561 }
3562 return 0;
3563 }
3566 /**
3567 *
3568 */
3569 static bool
3570 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3571 {
3572 if (a->type != b->type)
3573 return true;
3574 if (a->type == SP_FONT_SIZE_LENGTH) {
3575 if (a->computed != b->computed)
3576 return true;
3577 } else {
3578 if (a->value != b->value)
3579 return true;
3580 }
3581 return false;
3582 }
3585 /**
3586 * Write SPIFontSize object into string.
3587 */
3588 static gint
3589 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3590 SPIFontSize const *const val, SPIFontSize const *const base,
3591 guint const flags)
3592 {
3593 if ((flags & SP_STYLE_FLAG_ALWAYS)
3594 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3595 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3596 && (!base->set || sp_fontsize_differ(val, base))))
3597 {
3598 if (val->inherit) {
3599 return g_snprintf(p, len, "%s:inherit;", key);
3600 } else if (val->type == SP_FONT_SIZE_LITERAL) {
3601 for (unsigned i = 0; enum_font_size[i].key; i++) {
3602 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3603 return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3604 }
3605 }
3606 } else if (val->type == SP_FONT_SIZE_LENGTH) {
3607 Inkscape::CSSOStringStream os;
3608 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
3609 return g_strlcpy(p, os.str().c_str(), len);
3610 } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3611 Inkscape::CSSOStringStream os;
3612 os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3613 return g_strlcpy(p, os.str().c_str(), len);
3614 }
3615 }
3616 return 0;
3617 }
3620 /**
3621 * Write SPIFilter object into string.
3622 */
3623 static gint
3624 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3625 SPIFilter const *const val, SPIFilter const *const base,
3626 guint const flags)
3627 {
3628 if ((flags & SP_STYLE_FLAG_ALWAYS)
3629 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3630 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3631 {
3632 if (val->inherit) {
3633 return g_snprintf(p, len, "%s:inherit;", key);
3634 } else if (val->uri) {
3635 return g_snprintf(p, len, "%s:url(%s);", key, val->uri);
3636 }
3637 }
3640 return 0;
3641 }
3644 /**
3645 * Clear paint object, and disconnect style from paintserver (if present).
3646 */
3647 static void
3648 sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
3649 {
3650 if ((paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
3651 if (paint == &style->fill) {
3652 if (style->fill_hreffed) {
3653 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3654 style->fill_hreffed = false;
3655 }
3656 style->fill_release_connection.disconnect();
3657 style->fill_modified_connection.disconnect();
3658 } else {
3659 assert(paint == &style->stroke); // Only fill & stroke can have a paint server.
3660 if (style->stroke_hreffed) {
3661 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3662 style->stroke_hreffed = false;
3663 }
3664 style->stroke_release_connection.disconnect();
3665 style->stroke_modified_connection.disconnect();
3666 }
3668 paint->value.paint.server = NULL;
3669 }
3670 paint->value.paint.uri = NULL;
3671 paint->type = SP_PAINT_TYPE_NONE;
3672 delete paint->iccColor;
3673 paint->iccColor = NULL;
3674 }
3677 /**
3678 * Clear filter object, and disconnect style from paintserver (if present).
3679 */
3680 static void
3681 sp_style_filter_clear(SPStyle *style, SPIFilter *f)
3682 {
3684 if (style->filter_hreffed) {
3685 sp_object_hunref(SP_OBJECT(f->filter), style);
3686 style->filter_hreffed = false;
3687 }
3688 //style->fill_release_connection.disconnect();
3689 //style->fill_modified_connection.disconnect();
3691 f->filter = NULL;
3693 }
3696 /**
3697 * Clear all style property attributes in object.
3698 */
3699 void
3700 sp_style_unset_property_attrs(SPObject *o)
3701 {
3702 if (!o) return;
3704 SPStyle *style = SP_OBJECT_STYLE(o);
3705 if (!style) return;
3707 Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3708 if (!repr) return;
3710 if (style->opacity.set) {
3711 repr->setAttribute("opacity", NULL);
3712 }
3713 if (style->color.set) {
3714 repr->setAttribute("color", NULL);
3715 }
3716 if (style->fill.set) {
3717 repr->setAttribute("fill", NULL);
3718 }
3719 if (style->fill_opacity.set) {
3720 repr->setAttribute("fill-opacity", NULL);
3721 }
3722 if (style->fill_rule.set) {
3723 repr->setAttribute("fill-rule", NULL);
3724 }
3725 if (style->stroke.set) {
3726 repr->setAttribute("stroke", NULL);
3727 }
3728 if (style->stroke_width.set) {
3729 repr->setAttribute("stroke-width", NULL);
3730 }
3731 if (style->stroke_linecap.set) {
3732 repr->setAttribute("stroke-linecap", NULL);
3733 }
3734 if (style->stroke_linejoin.set) {
3735 repr->setAttribute("stroke-linejoin", NULL);
3736 }
3737 if (style->marker[SP_MARKER_LOC].set) {
3738 repr->setAttribute("marker", NULL);
3739 }
3740 if (style->marker[SP_MARKER_LOC_START].set) {
3741 repr->setAttribute("marker-start", NULL);
3742 }
3743 if (style->marker[SP_MARKER_LOC_MID].set) {
3744 repr->setAttribute("marker-mid", NULL);
3745 }
3746 if (style->marker[SP_MARKER_LOC_END].set) {
3747 repr->setAttribute("marker-end", NULL);
3748 }
3749 if (style->stroke_opacity.set) {
3750 repr->setAttribute("stroke-opacity", NULL);
3751 }
3752 if (style->stroke_dasharray_set) {
3753 repr->setAttribute("stroke-dasharray", NULL);
3754 }
3755 if (style->stroke_dashoffset_set) {
3756 repr->setAttribute("stroke-dashoffset", NULL);
3757 }
3758 if (style->text_private && style->text->font_family.set) {
3759 repr->setAttribute("font-family", NULL);
3760 }
3761 if (style->text_anchor.set) {
3762 repr->setAttribute("text-anchor", NULL);
3763 }
3764 if (style->writing_mode.set) {
3765 repr->setAttribute("writing_mode", NULL);
3766 }
3767 if (style->filter.set) {
3768 repr->setAttribute("filter", NULL);
3769 }
3770 if (style->enable_background.set) {
3771 repr->setAttribute("enable-background", NULL);
3772 }
3773 }
3775 /**
3776 * \pre style != NULL.
3777 * \pre flags in {IFSET, ALWAYS}.
3778 */
3779 SPCSSAttr *
3780 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3781 {
3782 g_return_val_if_fail(style != NULL, NULL);
3783 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3784 (flags == SP_STYLE_FLAG_ALWAYS) ),
3785 NULL);
3786 gchar *style_str = sp_style_write_string(style, flags);
3787 SPCSSAttr *css = sp_repr_css_attr_new();
3788 sp_repr_css_attr_add_from_string(css, style_str);
3789 g_free(style_str);
3790 return css;
3791 }
3794 /**
3795 * \pre object != NULL
3796 * \pre flags in {IFSET, ALWAYS}.
3797 */
3798 SPCSSAttr *
3799 sp_css_attr_from_object(SPObject *object, guint const flags)
3800 {
3801 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3802 (flags == SP_STYLE_FLAG_ALWAYS) ),
3803 NULL);
3804 SPStyle const *const style = SP_OBJECT_STYLE(object);
3805 if (style == NULL)
3806 return NULL;
3807 return sp_css_attr_from_style(style, flags);
3808 }
3810 /**
3811 * Unset any text-related properties
3812 */
3813 SPCSSAttr *
3814 sp_css_attr_unset_text(SPCSSAttr *css)
3815 {
3816 sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3817 sp_repr_css_set_property(css, "font-size", NULL);
3818 sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3819 sp_repr_css_set_property(css, "font-style", NULL);
3820 sp_repr_css_set_property(css, "font-variant", NULL);
3821 sp_repr_css_set_property(css, "font-weight", NULL);
3822 sp_repr_css_set_property(css, "font-stretch", NULL);
3823 sp_repr_css_set_property(css, "font-family", NULL);
3824 sp_repr_css_set_property(css, "text-indent", NULL);
3825 sp_repr_css_set_property(css, "text-align", NULL);
3826 sp_repr_css_set_property(css, "text-decoration", NULL);
3827 sp_repr_css_set_property(css, "line-height", NULL);
3828 sp_repr_css_set_property(css, "letter-spacing", NULL);
3829 sp_repr_css_set_property(css, "word-spacing", NULL);
3830 sp_repr_css_set_property(css, "text-transform", NULL);
3831 sp_repr_css_set_property(css, "direction", NULL);
3832 sp_repr_css_set_property(css, "block-progression", NULL);
3833 sp_repr_css_set_property(css, "writing-mode", NULL);
3834 sp_repr_css_set_property(css, "text-anchor", NULL);
3835 sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3836 sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3837 sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3838 sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3840 return css;
3841 }
3843 bool
3844 is_url(char const *p)
3845 {
3846 if (p == NULL)
3847 return false;
3848 /** \todo
3849 * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3850 * in property values must start with 'url('.
3851 */
3852 return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3853 }
3855 /**
3856 * Unset any properties that contain URI values.
3857 *
3858 * Used for storing style that will be reused across documents when carrying
3859 * the referenced defs is impractical.
3860 */
3861 SPCSSAttr *
3862 sp_css_attr_unset_uris(SPCSSAttr *css)
3863 {
3864 // All properties that may hold <uri> or <paint> according to SVG 1.1
3865 if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3866 if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3867 if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3868 if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3869 if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3870 if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3871 if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3872 if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3873 if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3874 if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3876 return css;
3877 }
3879 /**
3880 * Scale a single-value property.
3881 */
3882 void
3883 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3884 double ex, bool only_with_units = false)
3885 {
3886 gchar const *w = sp_repr_css_property(css, property, NULL);
3887 if (w) {
3888 gchar *units = NULL;
3889 double wd = g_ascii_strtod(w, &units) * ex;
3890 if (w == units) {// nothing converted, non-numeric value
3891 return;
3892 }
3893 if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3894 // only_with_units, but no units found, so do nothing.
3895 return;
3896 }
3897 Inkscape::CSSOStringStream os;
3898 os << wd << units; // reattach units
3899 sp_repr_css_set_property(css, property, os.str().c_str());
3900 }
3901 }
3903 /**
3904 * Scale a list-of-values property.
3905 */
3906 void
3907 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3908 {
3909 gchar const *string = sp_repr_css_property(css, property, NULL);
3910 if (string) {
3911 Inkscape::CSSOStringStream os;
3912 gchar **a = g_strsplit(string, ",", 10000);
3913 bool first = true;
3914 for (gchar **i = a; i != NULL; i++) {
3915 gchar *w = *i;
3916 if (w == NULL)
3917 break;
3918 gchar *units = NULL;
3919 double wd = g_ascii_strtod(w, &units) * ex;
3920 if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3921 g_strfreev(a);
3922 return;
3923 }
3924 if (!first) {
3925 os << ",";
3926 }
3927 os << wd << units; // reattach units
3928 first = false;
3929 }
3930 sp_repr_css_set_property(css, property, os.str().c_str());
3931 g_strfreev(a);
3932 }
3933 }
3935 /**
3936 * Scale any properties that may hold <length> by ex.
3937 */
3938 SPCSSAttr *
3939 sp_css_attr_scale(SPCSSAttr *css, double ex)
3940 {
3941 sp_css_attr_scale_property_single(css, "baseline-shift", ex);
3942 sp_css_attr_scale_property_single(css, "stroke-width", ex);
3943 sp_css_attr_scale_property_list (css, "stroke-dasharray", ex);
3944 sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
3945 sp_css_attr_scale_property_single(css, "font-size", ex);
3946 sp_css_attr_scale_property_single(css, "kerning", ex);
3947 sp_css_attr_scale_property_single(css, "letter-spacing", ex);
3948 sp_css_attr_scale_property_single(css, "word-spacing", ex);
3949 sp_css_attr_scale_property_single(css, "line-height", ex, true);
3951 return css;
3952 }
3955 /**
3956 * Remove quotes from SPIString object value.
3957 *
3958 * \todo FIXME: now used for font family, but perhaps this should apply to
3959 * ALL strings (check CSS spec), in which case this should be part of
3960 * read_istring.
3961 */
3962 static void
3963 css2_unescape_unquote(SPIString *val)
3964 {
3965 if (val->set && val->value && strlen(val->value) >= 2) {
3967 /// \todo unescape all \-escaped chars
3969 int l = strlen(val->value);
3970 if ( ( val->value[0] == '"' && val->value[l - 1] == '"' ) ||
3971 ( val->value[0] == '\'' && val->value[l - 1] == '\'' ) ) {
3972 memcpy(val->value, val->value + 1, l - 2);
3973 val->value[l - 2] = '\0';
3974 }
3975 }
3976 }
3979 /*
3980 Local Variables:
3981 mode:c++
3982 c-file-style:"stroustrup"
3983 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3984 indent-tabs-mode:nil
3985 fill-column:99
3986 End:
3987 */
3988 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :