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.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 if (!style->filter.set || style->filter.inherit)
1934 {
1935 // FIXME: (1) this is a temp hack, as it must correctly handle ->filter_hreffed; (2)
1936 // instead of just copying over, we need to _merge_ the two filters by combining their
1937 // filter primitives
1938 style->filter.set = parent->filter.set;
1939 style->filter.inherit = parent->filter.inherit;
1940 style->filter.filter = parent->filter.filter;
1941 style->filter.uri = parent->filter.uri;
1942 }
1944 /** \todo
1945 * fixme: Check that we correctly handle all properties that don't
1946 * inherit by default (as shown in
1947 * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1948 */
1949 }
1951 /* SPIPaint properties (including color). */
1952 {
1953 /** \todo
1954 * Think about the issues involved if specified as currentColor or
1955 * if specified relative to colorProfile, and if the currentColor or
1956 * colorProfile differs between parent \& child. See also comments
1957 * elsewhere in this function about URIs.
1958 */
1959 SPIPaint SPStyle::*const fields[] = {
1960 &SPStyle::color,
1961 &SPStyle::fill,
1962 &SPStyle::stroke
1963 };
1964 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1965 SPIPaint SPStyle::*const fld = fields[i];
1966 sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
1967 }
1968 }
1970 /* Things from SVG 1.2 or CSS3. */
1971 {
1972 /* Note: If we ever support setting string values for text-align then we'd need strdup
1973 * handling here. */
1974 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
1976 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
1977 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
1978 }
1980 /* Note: this will need length handling once dasharray supports units. */
1981 if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
1982 && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
1983 {
1984 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1985 if (style->stroke_dash.n_dash > 0) {
1986 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1987 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1988 }
1989 style->stroke_dasharray_set = parent->stroke_dasharray_set;
1990 style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
1991 }
1993 /* Note: this will need length handling once dasharray_offset supports units. */
1994 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1995 style->stroke_dash.offset = parent->stroke_dash.offset;
1996 style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
1997 /* fixme: we don't currently allow stroke-dashoffset to be `inherit'. TODO: Try to
1998 * represent it as a normal SPILength; though will need to do something about existing
1999 * users of stroke_dash.offset and stroke_dashoffset_set. */
2000 }
2001 }
2005 /**
2006 * Disconnects from possible fill and stroke paint servers.
2007 */
2008 static void
2009 sp_style_paint_server_release(SPObject *obj, SPStyle *style)
2010 {
2011 SPPaintServer *server=static_cast<SPPaintServer *>(obj);
2012 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2013 && (server == style->fill.value.paint.server))
2014 {
2015 sp_style_paint_clear(style, &style->fill);
2016 }
2018 if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2019 && (server == style->stroke.value.paint.server))
2020 {
2021 sp_style_paint_clear(style, &style->stroke);
2022 }
2023 }
2028 /**
2029 * Emit style modified signal on style's object if server is style's fill
2030 * or stroke paint server.
2031 */
2032 static void
2033 sp_style_paint_server_modified(SPObject *obj, guint flags, SPStyle *style)
2034 {
2035 SPPaintServer *server = static_cast<SPPaintServer *>(obj);
2036 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
2037 && (server == style->fill.value.paint.server))
2038 {
2039 if (style->object) {
2040 /** \todo
2041 * fixme: I do not know, whether it is optimal - we are
2042 * forcing reread of everything (Lauris)
2043 */
2044 /** \todo
2045 * fixme: We have to use object_modified flag, because parent
2046 * flag is only available downstreams.
2047 */
2048 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2049 }
2050 } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
2051 && (server == style->stroke.value.paint.server))
2052 {
2053 if (style->object) {
2054 /// \todo fixme:
2055 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
2056 }
2057 } else {
2058 g_assert_not_reached();
2059 }
2060 }
2064 /**
2065 *
2066 */
2067 static void
2068 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2069 {
2070 sp_style_paint_clear(style, paint);
2072 if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2073 paint->currentcolor = TRUE;
2074 paint->type = SP_PAINT_TYPE_COLOR;
2075 sp_color_copy(&paint->value.color, &style->color.value.color);
2076 return;
2077 }
2079 paint->type = parent->type;
2080 switch (paint->type) {
2081 case SP_PAINT_TYPE_COLOR:
2082 sp_color_copy(&paint->value.color, &parent->value.color);
2083 break;
2084 case SP_PAINT_TYPE_PAINTSERVER:
2085 paint->value.paint.server = parent->value.paint.server;
2086 paint->value.paint.uri = parent->value.paint.uri;
2087 if (paint->value.paint.server) {
2088 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
2089 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2090 if (paint == &style->fill) {
2091 style->fill_hreffed = true;
2092 } else {
2093 assert(paint == &style->stroke);
2094 style->stroke_hreffed = true;
2095 }
2096 }
2097 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
2098 SPObject *server = paint->value.paint.server;
2099 sigc::connection release_connection
2100 = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
2101 sigc::connection modified_connection
2102 = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
2103 if (paint == &style->fill) {
2104 style->fill_release_connection = release_connection;
2105 style->fill_modified_connection = modified_connection;
2106 } else {
2107 assert(paint == &style->stroke);
2108 style->stroke_release_connection = release_connection;
2109 style->stroke_modified_connection = modified_connection;
2110 }
2111 }
2112 }
2113 break;
2114 case SP_PAINT_TYPE_NONE:
2115 break;
2116 default:
2117 g_assert_not_reached();
2118 break;
2119 }
2120 }
2123 /**
2124 * Merge filter style from parent.
2125 * Filter effects do not inherit by default
2126 */
2127 static void
2128 sp_style_merge_ifilter(SPStyle *style, SPIFilter *child, SPIFilter const *parent)
2129 {
2130 sp_style_filter_clear(style, child);
2131 child->set = parent->set;
2132 child->inherit = parent->inherit;
2133 child->filter = parent->filter;
2134 child->uri = parent->uri;
2135 sp_object_href(SP_OBJECT(child), style);
2136 style->filter_hreffed = true;
2137 }
2139 /**
2140 * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2141 * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2142 * complete cascaded style to style_clipboard. When you need a CSS string
2143 * for an object in the document tree, you normally call
2144 * sp_style_write_difference instead to take into account the object's parent.
2145 *
2146 * \pre style != NULL.
2147 * \pre flags in {IFSET, ALWAYS}.
2148 * \post ret != NULL.
2149 */
2150 gchar *
2151 sp_style_write_string(SPStyle const *const style, guint const flags)
2152 {
2153 /** \todo
2154 * Merge with write_difference, much duplicate code!
2155 */
2156 g_return_val_if_fail(style != NULL, NULL);
2157 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2158 (flags == SP_STYLE_FLAG_ALWAYS) ),
2159 NULL);
2161 gchar c[BMAX];
2162 gchar *p = c;
2163 *p = '\0';
2165 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2166 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2167 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2168 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2169 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2171 /* Text */
2172 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2173 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2174 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2175 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2176 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2177 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2178 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2179 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2180 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2181 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2183 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2185 /// \todo fixme: Per type methods need default flag too (lauris)
2186 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2187 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2188 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2189 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2190 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2191 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2192 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2193 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2194 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2196 marker_status("sp_style_write_string: Writing markers");
2197 if (style->marker[SP_MARKER_LOC].set) {
2198 p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2199 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2200 p += g_snprintf(p, c + BMAX - p, "marker:none;");
2201 }
2202 if (style->marker[SP_MARKER_LOC_START].set) {
2203 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2204 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2205 p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2206 }
2207 if (style->marker[SP_MARKER_LOC_MID].set) {
2208 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2209 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2210 p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2211 }
2212 if (style->marker[SP_MARKER_LOC_END].set) {
2213 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2214 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2215 p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2216 }
2218 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2220 /** \todo fixme: */
2221 if ((flags == SP_STYLE_FLAG_ALWAYS)
2222 || style->stroke_dasharray_set)
2223 {
2224 if (style->stroke_dasharray_inherit) {
2225 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2226 } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2227 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2228 gint i;
2229 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2230 Inkscape::CSSOStringStream os;
2231 if (i) {
2232 os << ", ";
2233 }
2234 os << style->stroke_dash.dash[i];
2235 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2236 }
2237 if (p < c + BMAX) {
2238 *p++ = ';';
2239 }
2240 } else {
2241 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2242 }
2243 }
2245 /** \todo fixme: */
2246 if (style->stroke_dashoffset_set) {
2247 Inkscape::CSSOStringStream os;
2248 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2249 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2250 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2251 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2252 }
2254 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2256 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2257 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2258 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2260 /* filter: */
2261 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2263 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2265 /* fixme: */
2266 p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2268 /* Get rid of trailing `;'. */
2269 if (p != c) {
2270 --p;
2271 if (*p == ';') {
2272 *p = '\0';
2273 }
2274 }
2276 return g_strdup(c);
2277 }
2280 #define STYLE_BUF_MAX
2283 /**
2284 * Dumps style to CSS string, see sp_style_write_string()
2285 *
2286 * \pre from != NULL.
2287 * \pre to != NULL.
2288 * \post ret != NULL.
2289 */
2290 gchar *
2291 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2292 {
2293 g_return_val_if_fail(from != NULL, NULL);
2294 g_return_val_if_fail(to != NULL, NULL);
2296 gchar c[BMAX], *p = c;
2297 *p = '\0';
2299 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2300 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2301 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2302 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2303 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2305 /* Text */
2306 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2307 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2308 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2309 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2310 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2311 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2312 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2313 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2314 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2315 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2317 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2319 /// \todo fixme: Per type methods need default flag too
2320 if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2321 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2322 }
2323 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2324 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2325 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2326 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2327 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2328 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2329 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2330 &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2331 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2332 &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2333 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2334 &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2335 /** \todo fixme: */
2336 if (from->stroke_dasharray_set) {
2337 if (from->stroke_dasharray_inherit) {
2338 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2339 } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2340 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2341 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2342 Inkscape::CSSOStringStream os;
2343 if (i) {
2344 os << ", ";
2345 }
2346 os << from->stroke_dash.dash[i];
2347 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2348 }
2349 p += g_snprintf(p, c + BMAX - p, ";");
2350 }
2351 }
2352 /* fixme: */
2353 if (from->stroke_dashoffset_set) {
2354 Inkscape::CSSOStringStream os;
2355 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2356 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2357 }
2358 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2360 /* markers */
2361 marker_status("sp_style_write_difference: Writing markers");
2362 if (from->marker[SP_MARKER_LOC].value != NULL) {
2363 p += g_snprintf(p, c + BMAX - p, "marker:%s;", from->marker[SP_MARKER_LOC].value);
2364 }
2365 if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2366 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2367 }
2368 if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2369 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", from->marker[SP_MARKER_LOC_MID].value);
2370 }
2371 if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2372 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", from->marker[SP_MARKER_LOC_END].value);
2373 }
2375 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2376 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2377 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2379 /* filter: */
2380 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2382 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2384 p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2386 /** \todo
2387 * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2388 * flag is mainly only for attributes that don't handle explicit unset well.
2389 * We may need to revisit the behaviour of this routine.
2390 */
2392 /* Get rid of trailing `;'. */
2393 if (p != c) {
2394 --p;
2395 if (*p == ';') {
2396 *p = '\0';
2397 }
2398 }
2400 return g_strdup(c);
2401 }
2405 /**
2406 * Reset all style properties.
2407 */
2408 static void
2409 sp_style_clear(SPStyle *style)
2410 {
2411 g_return_if_fail(style != NULL);
2413 sp_style_paint_clear(style, &style->fill);
2414 sp_style_paint_clear(style, &style->stroke);
2415 sp_style_filter_clear(style, &style->filter);
2416 if (style->stroke_dash.dash) {
2417 g_free(style->stroke_dash.dash);
2418 }
2420 /** \todo fixme: Do that text manipulation via parents */
2421 SPObject *object = style->object;
2422 gint const refcount = style->refcount;
2423 SPTextStyle *text = style->text;
2424 unsigned const text_private = style->text_private;
2425 memset(style, 0, sizeof(SPStyle));
2426 style->refcount = refcount;
2427 style->object = object;
2428 style->text = text;
2429 style->text_private = text_private;
2430 /* fixme: */
2431 style->text->font.set = FALSE;
2432 style->text->font_family.set = FALSE;
2434 style->font_size.set = FALSE;
2435 style->font_size.type = SP_FONT_SIZE_LITERAL;
2436 style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2437 style->font_size.computed = 12.0;
2438 style->font_style.set = FALSE;
2439 style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2440 style->font_variant.set = FALSE;
2441 style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2442 style->font_weight.set = FALSE;
2443 style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2444 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2445 style->font_stretch.set = FALSE;
2446 style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2448 /* text */
2449 style->text_indent.set = FALSE;
2450 style->text_indent.unit = SP_CSS_UNIT_NONE;
2451 style->text_indent.computed = 0.0;
2453 style->text_align.set = FALSE;
2454 style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2456 style->text_decoration.set = FALSE;
2457 style->text_decoration.underline = FALSE;
2458 style->text_decoration.overline = FALSE;
2459 style->text_decoration.line_through = FALSE;
2460 style->text_decoration.blink = FALSE;
2462 style->line_height.set = FALSE;
2463 style->line_height.unit = SP_CSS_UNIT_PERCENT;
2464 style->line_height.normal = TRUE;
2465 style->line_height.value = style->line_height.computed = 1.0;
2467 style->letter_spacing.set = FALSE;
2468 style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2469 style->letter_spacing.normal = TRUE;
2470 style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2472 style->word_spacing.set = FALSE;
2473 style->word_spacing.unit = SP_CSS_UNIT_NONE;
2474 style->word_spacing.normal = TRUE;
2475 style->word_spacing.value = style->word_spacing.computed = 0.0;
2477 style->text_transform.set = FALSE;
2478 style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2480 style->direction.set = FALSE;
2481 style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2483 style->block_progression.set = FALSE;
2484 style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2486 style->writing_mode.set = FALSE;
2487 style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2490 style->text_anchor.set = FALSE;
2491 style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2494 style->opacity.value = SP_SCALE24_MAX;
2495 style->visibility.set = FALSE;
2496 style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2497 style->display.set = FALSE;
2498 style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2499 style->overflow.set = FALSE;
2500 style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2502 style->color.type = SP_PAINT_TYPE_COLOR;
2503 sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
2505 style->fill.type = SP_PAINT_TYPE_COLOR;
2506 sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
2507 style->fill_opacity.value = SP_SCALE24_MAX;
2508 style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2510 style->stroke.type = SP_PAINT_TYPE_NONE;
2511 style->stroke.set = FALSE;
2512 sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
2513 style->stroke_opacity.value = SP_SCALE24_MAX;
2515 style->stroke_width.set = FALSE;
2516 style->stroke_width.unit = SP_CSS_UNIT_NONE;
2517 style->stroke_width.computed = 1.0;
2519 style->stroke_linecap.set = FALSE;
2520 style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2521 style->stroke_linejoin.set = FALSE;
2522 style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2524 style->stroke_miterlimit.set = FALSE;
2525 style->stroke_miterlimit.value = 4.0;
2527 style->stroke_dash.n_dash = 0;
2528 style->stroke_dash.dash = NULL;
2529 style->stroke_dash.offset = 0.0;
2531 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2532 g_free(style->marker[i].value);
2533 style->marker[i].set = FALSE;
2534 }
2536 style->filter.set = FALSE;
2537 //Are these really needed?
2538 style->filter.inherit = FALSE;
2539 style->filter.uri = NULL;
2540 style->filter.filter = FALSE;
2542 style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2543 style->enable_background.set = false;
2544 style->enable_background.inherit = false;
2545 }
2549 /**
2550 *
2551 */
2552 static void
2553 sp_style_read_dash(SPStyle *style, gchar const *str)
2554 {
2555 /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2556 style->stroke_dasharray_set = TRUE;
2558 if (strcmp(str, "inherit") == 0) {
2559 style->stroke_dasharray_inherit = true;
2560 return;
2561 }
2562 style->stroke_dasharray_inherit = false;
2564 NRVpathDash &dash = style->stroke_dash;
2565 g_free(dash.dash);
2566 dash.dash = NULL;
2568 if (strcmp(str, "none") == 0) {
2569 dash.n_dash = 0;
2570 return;
2571 }
2573 gint n_dash = 0;
2574 gdouble d[64];
2575 gchar *e = NULL;
2577 bool LineSolid=true;
2578 while (e != str && n_dash < 64) {
2579 /* TODO: Should allow <length> rather than just a unitless (px) number. */
2580 d[n_dash] = g_ascii_strtod(str, (char **) &e);
2581 if (d[n_dash] > 0.00000001)
2582 LineSolid = false;
2583 if (e != str) {
2584 n_dash += 1;
2585 str = e;
2586 }
2587 while (str && *str && !isalnum(*str)) str += 1;
2588 }
2590 if (LineSolid) {
2591 dash.n_dash = 0;
2592 return;
2593 }
2595 if (n_dash > 0) {
2596 dash.dash = g_new(gdouble, n_dash);
2597 memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2598 dash.n_dash = n_dash;
2599 }
2600 }
2603 /*#########################
2604 ## SPTextStyle operations
2605 #########################*/
2608 /**
2609 * Return new SPTextStyle object with default settings.
2610 */
2611 static SPTextStyle *
2612 sp_text_style_new()
2613 {
2614 SPTextStyle *ts = g_new0(SPTextStyle, 1);
2615 ts->refcount = 1;
2616 sp_text_style_clear(ts);
2618 ts->font.value = g_strdup("Bitstream Vera Sans");
2619 ts->font_family.value = g_strdup("Bitstream Vera Sans");
2621 return ts;
2622 }
2625 /**
2626 * Clear text style settings.
2627 */
2628 static void
2629 sp_text_style_clear(SPTextStyle *ts)
2630 {
2631 ts->font.set = FALSE;
2632 ts->font_family.set = FALSE;
2633 }
2637 /**
2638 * Reduce refcount of text style and possibly free it.
2639 */
2640 static SPTextStyle *
2641 sp_text_style_unref(SPTextStyle *st)
2642 {
2643 st->refcount -= 1;
2645 if (st->refcount < 1) {
2646 g_free(st->font.value);
2647 g_free(st->font_family.value);
2648 g_free(st);
2649 }
2651 return NULL;
2652 }
2656 /**
2657 * Return duplicate of text style.
2658 */
2659 static SPTextStyle *
2660 sp_text_style_duplicate_unset(SPTextStyle *st)
2661 {
2662 SPTextStyle *nt = g_new0(SPTextStyle, 1);
2663 nt->refcount = 1;
2665 nt->font.value = g_strdup(st->font.value);
2666 nt->font_family.value = g_strdup(st->font_family.value);
2668 return nt;
2669 }
2673 /**
2674 * Write SPTextStyle object into string.
2675 */
2676 static guint
2677 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2678 {
2679 gint d = 0;
2681 // We do not do diffing for text style
2682 if (flags == SP_STYLE_FLAG_IFDIFF)
2683 flags = SP_STYLE_FLAG_IFSET;
2685 d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2686 return d;
2687 }
2691 /* The following sp_tyle_read_* functions ignore invalid values, as per
2692 * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2693 *
2694 * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2695 * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2696 * pairs, in which case SVG's error-handling rules
2697 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2698 */
2701 /**
2702 * Set SPIFloat object from string.
2703 */
2704 static void
2705 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2706 {
2707 if (!strcmp(str, "inherit")) {
2708 val->set = TRUE;
2709 val->inherit = TRUE;
2710 } else {
2711 gfloat value;
2712 if (sp_svg_number_read_f(str, &value)) {
2713 val->set = TRUE;
2714 val->inherit = FALSE;
2715 val->value = value;
2716 }
2717 }
2718 }
2722 /**
2723 * Set SPIScale24 object from string.
2724 */
2725 static void
2726 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2727 {
2728 if (!strcmp(str, "inherit")) {
2729 val->set = TRUE;
2730 val->inherit = TRUE;
2731 } else {
2732 gfloat value;
2733 if (sp_svg_number_read_f(str, &value)) {
2734 val->set = TRUE;
2735 val->inherit = FALSE;
2736 value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
2737 val->value = SP_SCALE24_FROM_FLOAT(value);
2738 }
2739 }
2740 }
2742 /**
2743 * Reads a style value and performs lookup based on the given style value enumerations.
2744 */
2745 static void
2746 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2747 bool const can_explicitly_inherit)
2748 {
2749 if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2750 val->set = TRUE;
2751 val->inherit = TRUE;
2752 } else {
2753 for (unsigned i = 0; dict[i].key; i++) {
2754 if (!strcmp(str, dict[i].key)) {
2755 val->set = TRUE;
2756 val->inherit = FALSE;
2757 val->value = dict[i].value;
2758 /* Save copying for values not needing it */
2759 val->computed = val->value;
2760 break;
2761 }
2762 }
2763 }
2764 }
2768 /**
2769 * Set SPIString object from string.
2770 */
2771 static void
2772 sp_style_read_istring(SPIString *val, gchar const *str)
2773 {
2774 g_free(val->value);
2776 if (!strcmp(str, "inherit")) {
2777 val->set = TRUE;
2778 val->inherit = TRUE;
2779 val->value = NULL;
2780 } else {
2781 val->set = TRUE;
2782 val->inherit = FALSE;
2783 val->value = g_strdup(str);
2784 }
2785 }
2789 /**
2790 * Set SPILength object from string.
2791 */
2792 static void
2793 sp_style_read_ilength(SPILength *val, gchar const *str)
2794 {
2795 if (!strcmp(str, "inherit")) {
2796 val->set = TRUE;
2797 val->inherit = TRUE;
2798 } else {
2799 gdouble value;
2800 gchar *e;
2801 /** \todo fixme: Move this to standard place (Lauris) */
2802 value = g_ascii_strtod(str, &e);
2803 if ((gchar const *) e != str) {
2804 /** \todo
2805 * Allow the number of px per inch to vary (document preferences,
2806 * X server or whatever). E.g. don't fill in computed here, do
2807 * it at the same time as percentage units are done.
2808 */
2809 if (!*e) {
2810 /* Userspace */
2811 val->unit = SP_CSS_UNIT_NONE;
2812 val->computed = value;
2813 } else if (!strcmp(e, "px")) {
2814 /* Userspace */
2815 val->unit = SP_CSS_UNIT_PX;
2816 val->computed = value;
2817 } else if (!strcmp(e, "pt")) {
2818 /* Userspace / DEVICESCALE */
2819 val->unit = SP_CSS_UNIT_PT;
2820 val->computed = value * PX_PER_PT;
2821 } else if (!strcmp(e, "pc")) {
2822 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2823 val->unit = SP_CSS_UNIT_PC;
2824 val->computed = value * PX_PER_PT * 12;
2825 } else if (!strcmp(e, "mm")) {
2826 val->unit = SP_CSS_UNIT_MM;
2827 val->computed = value * PX_PER_MM;
2828 } else if (!strcmp(e, "cm")) {
2829 val->unit = SP_CSS_UNIT_CM;
2830 val->computed = value * PX_PER_CM;
2831 } else if (!strcmp(e, "in")) {
2832 val->unit = SP_CSS_UNIT_IN;
2833 val->computed = value * PX_PER_IN;
2834 } else if (!strcmp(e, "em")) {
2835 /* EM square */
2836 val->unit = SP_CSS_UNIT_EM;
2837 val->value = value;
2838 } else if (!strcmp(e, "ex")) {
2839 /* ex square */
2840 val->unit = SP_CSS_UNIT_EX;
2841 val->value = value;
2842 } else if (!strcmp(e, "%")) {
2843 /* Percentage */
2844 val->unit = SP_CSS_UNIT_PERCENT;
2845 val->value = value * 0.01;
2846 } else {
2847 /* Invalid */
2848 return;
2849 }
2850 val->set = TRUE;
2851 val->inherit = FALSE;
2852 }
2853 }
2854 }
2856 /**
2857 * Set SPILengthOrNormal object from string.
2858 */
2859 static void
2860 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2861 {
2862 if (!strcmp(str, "normal")) {
2863 val->set = TRUE;
2864 val->inherit = FALSE;
2865 val->normal = TRUE;
2866 val->unit = SP_CSS_UNIT_NONE;
2867 val->value = val->computed = 0.0;
2868 } else {
2869 SPILength length;
2870 sp_style_read_ilength(&length, str);
2871 val->set = length.set;
2872 val->inherit = length.inherit;
2873 val->normal = FALSE;
2874 val->unit = length.unit;
2875 val->value = length.value;
2876 val->computed = length.computed;
2877 }
2878 }
2880 /**
2881 * Set SPITextDecoration object from string.
2882 */
2883 static void
2884 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2885 {
2886 if (!strcmp(str, "inherit")) {
2887 val->set = TRUE;
2888 val->inherit = TRUE;
2889 } else if (!strcmp(str, "none")) {
2890 val->set = TRUE;
2891 val->inherit = FALSE;
2892 val->underline = FALSE;
2893 val->overline = FALSE;
2894 val->line_through = FALSE;
2895 val->blink = FALSE;
2896 } else {
2897 bool found_underline = false;
2898 bool found_overline = false;
2899 bool found_line_through = false;
2900 bool found_blink = false;
2901 for ( ; *str ; str++ ) {
2902 if (*str == ' ') continue;
2903 if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2904 found_underline = true;
2905 str += 9;
2906 } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2907 found_overline = true;
2908 str += 8;
2909 } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2910 found_line_through = true;
2911 str += 12;
2912 } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2913 found_blink = true;
2914 str += 5;
2915 } else {
2916 return; // invalid value
2917 }
2918 }
2919 if (!(found_underline || found_overline || found_line_through || found_blink)) {
2920 return; // invalid value: empty
2921 }
2922 val->set = TRUE;
2923 val->inherit = FALSE;
2924 val->underline = found_underline;
2925 val->overline = found_overline;
2926 val->line_through = found_line_through;
2927 val->blink = found_blink;
2928 }
2929 }
2931 /**
2932 * Set SPIPaint object from string containing an integer value.
2933 * \param document Ignored
2934 */
2935 static void
2936 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2937 {
2938 paint->currentcolor = FALSE; /* currentColor not a valid <color>. */
2939 if (!strcmp(str, "inherit")) {
2940 paint->set = TRUE;
2941 paint->inherit = TRUE;
2942 } else {
2943 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2944 if (rgb0 != 0xff) {
2945 paint->type = SP_PAINT_TYPE_COLOR;
2946 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2947 paint->set = TRUE;
2948 paint->inherit = FALSE;
2949 }
2950 }
2951 }
2954 /**
2955 * Set SPIPaint object from string.
2956 *
2957 * \pre paint == \&style.fill || paint == \&style.stroke.
2958 */
2959 static void
2960 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2961 {
2962 while (isspace(*str)) {
2963 ++str;
2964 }
2966 if (streq(str, "inherit")) {
2967 paint->set = TRUE;
2968 paint->inherit = TRUE;
2969 paint->currentcolor = FALSE;
2970 } else if (streq(str, "currentColor") && paint != &style->color) {
2971 paint->set = TRUE;
2972 paint->inherit = FALSE;
2973 paint->currentcolor = TRUE;
2974 } else if (streq(str, "none") && paint != &style->color) {
2975 paint->type = SP_PAINT_TYPE_NONE;
2976 paint->set = TRUE;
2977 paint->inherit = FALSE;
2978 paint->currentcolor = FALSE;
2979 } else if (strneq(str, "url", 3) && paint != &style->color) {
2980 // this is alloc'd uri, but seems to be shared with a parent
2981 // potentially, so it's not any easy g_free case...
2982 paint->value.paint.uri = extract_uri(str);
2983 if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
2984 paint->type = SP_PAINT_TYPE_NONE;
2985 return;
2986 }
2987 paint->type = SP_PAINT_TYPE_PAINTSERVER;
2988 paint->set = TRUE;
2989 paint->inherit = FALSE;
2990 paint->currentcolor = FALSE;
2991 if (document) {
2992 SPObject *ps = sp_uri_reference_resolve(document, str);
2993 if (ps && SP_IS_PAINT_SERVER(ps)) {
2994 paint->value.paint.server = SP_PAINT_SERVER(ps);
2995 if (style->object && !style->cloned) {
2996 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2997 if (paint == &style->fill) {
2998 style->fill_hreffed = true;
2999 } else {
3000 assert(paint == &style->stroke);
3001 style->stroke_hreffed = true;
3002 }
3003 }
3004 if (style->object || style->cloned) {
3005 SPObject *server = paint->value.paint.server;
3006 sigc::connection release_connection
3007 = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
3008 sigc::connection modified_connection
3009 = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
3010 if (paint == &style->fill) {
3011 style->fill_release_connection = release_connection;
3012 style->fill_modified_connection = modified_connection;
3013 } else {
3014 assert(paint == &style->stroke);
3015 style->stroke_release_connection = release_connection;
3016 style->stroke_modified_connection = modified_connection;
3017 }
3018 }
3019 } else {
3020 paint->value.paint.server = NULL;
3021 }
3022 }
3023 } else {
3024 guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3025 if (rgb0 != 0xff) {
3026 paint->type = SP_PAINT_TYPE_COLOR;
3027 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
3028 paint->set = TRUE;
3029 paint->inherit = FALSE;
3030 paint->currentcolor = FALSE;
3032 while (g_ascii_isspace(*str)) {
3033 ++str;
3034 }
3035 if (strneq(str, "icc-color(", 10)) {
3036 SVGICCColor* tmp = new SVGICCColor();
3037 if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3038 delete tmp;
3039 tmp = 0;
3040 }
3041 paint->iccColor = tmp;
3042 }
3043 }
3044 }
3045 }
3049 /**
3050 * Set SPIFontSize object from string.
3051 */
3052 static void
3053 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3054 {
3055 if (!strcmp(str, "inherit")) {
3056 val->set = TRUE;
3057 val->inherit = TRUE;
3058 } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3059 for (unsigned i = 0; enum_font_size[i].key; i++) {
3060 if (!strcmp(str, enum_font_size[i].key)) {
3061 val->set = TRUE;
3062 val->inherit = FALSE;
3063 val->type = SP_FONT_SIZE_LITERAL;
3064 val->value = enum_font_size[i].value;
3065 return;
3066 }
3067 }
3068 /* Invalid */
3069 return;
3070 } else {
3071 gdouble value;
3072 gchar *e;
3073 /* fixme: Move this to standard place (Lauris) */
3074 value = g_ascii_strtod(str, &e);
3075 if ((gchar const *) e != str) {
3076 if (!*e) {
3077 /* Userspace */
3078 } else if (!strcmp(e, "px")) {
3079 /* Userspace */
3080 } else if (!strcmp(e, "pt")) {
3081 /* Userspace * DEVICESCALE */
3082 value *= PX_PER_PT;
3083 } else if (!strcmp(e, "pc")) {
3084 /* 12pt */
3085 value *= PX_PER_PT * 12.0;
3086 } else if (!strcmp(e, "mm")) {
3087 value *= PX_PER_MM;
3088 } else if (!strcmp(e, "cm")) {
3089 value *= PX_PER_CM;
3090 } else if (!strcmp(e, "in")) {
3091 value *= PX_PER_IN;
3092 } else if (!strcmp(e, "%")) {
3093 /* Percentage */
3094 val->set = TRUE;
3095 val->inherit = FALSE;
3096 val->type = SP_FONT_SIZE_PERCENTAGE;
3097 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3098 return;
3099 } else {
3100 /* Invalid */
3101 return;
3102 }
3103 /* Length */
3104 val->set = TRUE;
3105 val->inherit = FALSE;
3106 val->type = SP_FONT_SIZE_LENGTH;
3107 val->computed = value;
3108 return;
3109 }
3110 }
3111 }
3115 /**
3116 * Set SPIFilter object from string.
3117 */
3118 static void
3119 sp_style_read_ifilter( SPIFilter *f, gchar const *str, SPStyle * style, SPDocument *document)
3120 {
3121 /* Try all possible values: inherit, none, uri */
3122 if (streq(str, "inherit")) {
3123 f->set = TRUE;
3124 f->inherit = TRUE;
3125 f->filter = NULL;
3126 } else if(streq(str, "none")) {
3127 f->set = TRUE;
3128 f->inherit = FALSE;
3129 f->filter = NULL;
3130 } else if (strneq(str, "url", 3)) {
3131 f->uri = extract_uri(str);
3132 if(f->uri == NULL || f->uri[0] == '\0') {
3133 g_warning("Specified filter url is empty");
3134 f->set = TRUE;
3135 f->inherit = FALSE;
3136 f->filter = NULL;
3137 return;
3138 }
3139 f->set = TRUE;
3140 f->inherit = FALSE;
3141 f->filter = NULL;
3142 if (document) {
3143 SPObject *obj;
3144 obj = sp_uri_reference_resolve(document, str);
3145 if (SP_IS_FILTER(obj)) {
3146 f->filter = SP_FILTER(obj);
3147 sp_object_href(SP_OBJECT(f->filter), style);
3148 style->filter_hreffed = true;
3149 //g_signal_connect(G_OBJECT(f->filter), "release",
3150 // G_CALLBACK(sp_style_filter_release), style);
3151 //g_signal_connect(G_OBJECT(f->filter), "modified",
3152 // G_CALLBACK(sp_style_filter_modified), style);
3153 } else {
3154 g_warning("Element '%s' not found or is not a filter", f->uri);
3155 }
3156 }
3158 } else {
3159 /* We shouldn't reach this if SVG input is well-formed */
3160 f->set = FALSE;
3161 f->inherit = FALSE;
3162 f->filter = NULL;
3163 f->uri = NULL;
3164 }
3165 }
3167 /**
3168 * Set SPIEnum object from repr attribute.
3169 */
3170 static void
3171 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3172 gchar const *key, SPStyleEnum const *dict,
3173 bool const can_explicitly_inherit)
3174 {
3175 gchar const *str = repr->attribute(key);
3176 if (str) {
3177 sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3178 }
3179 }
3183 /**
3184 * Set SPILength object from repr attribute.
3185 */
3186 static void
3187 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3188 {
3189 gchar const *str = repr->attribute(key);
3190 if (str) {
3191 sp_style_read_ilength(val, str);
3192 }
3193 }
3195 /**
3196 * Set SPIFontSize object from repr attribute.
3197 */
3198 static void
3199 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3200 {
3201 gchar const *str = repr->attribute(key);
3202 if (str) {
3203 sp_style_read_ifontsize(val, str);
3204 }
3205 }
3208 /**
3209 * Set SPIFloat object from repr attribute.
3210 */
3211 static void
3212 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3213 {
3214 gchar const *str = repr->attribute(key);
3215 if (str) {
3216 sp_style_read_ifloat(val, str);
3217 }
3218 }
3221 /**
3222 * Write SPIFloat object into string.
3223 */
3224 static gint
3225 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3226 SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3227 {
3228 Inkscape::CSSOStringStream os;
3230 if ((flags & SP_STYLE_FLAG_ALWAYS)
3231 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3232 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3233 && (!base->set || (val->value != base->value))))
3234 {
3235 if (val->inherit) {
3236 return g_snprintf(p, len, "%s:inherit;", key);
3237 } else {
3238 os << key << ":" << val->value << ";";
3239 return g_strlcpy(p, os.str().c_str(), len);
3240 }
3241 }
3242 return 0;
3243 }
3246 /**
3247 * Write SPIScale24 object into string.
3248 */
3249 static gint
3250 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3251 SPIScale24 const *const val, SPIScale24 const *const base,
3252 guint const flags)
3253 {
3254 Inkscape::CSSOStringStream os;
3256 if ((flags & SP_STYLE_FLAG_ALWAYS)
3257 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3258 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3259 && (!base->set || (val->value != base->value))))
3260 {
3261 if (val->inherit) {
3262 return g_snprintf(p, len, "%s:inherit;", key);
3263 } else {
3264 os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3265 return g_strlcpy(p, os.str().c_str(), len);
3266 }
3267 }
3268 return 0;
3269 }
3272 /**
3273 * Write SPIEnum object into string.
3274 */
3275 static gint
3276 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3277 SPStyleEnum const *const dict,
3278 SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3279 {
3280 if ((flags & SP_STYLE_FLAG_ALWAYS)
3281 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3282 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3283 && (!base->set || (val->computed != base->computed))))
3284 {
3285 if (val->inherit) {
3286 return g_snprintf(p, len, "%s:inherit;", key);
3287 }
3288 for (unsigned i = 0; dict[i].key; i++) {
3289 if (dict[i].value == static_cast< gint > (val->value) ) {
3290 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3291 }
3292 }
3293 }
3294 return 0;
3295 }
3299 /**
3300 * Write SPIString object into string.
3301 */
3302 static gint
3303 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3304 SPIString const *const val, SPIString const *const base, guint const flags)
3305 {
3306 if ((flags & SP_STYLE_FLAG_ALWAYS)
3307 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3308 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3309 && (!base->set || strcmp(val->value, base->value))))
3310 {
3311 if (val->inherit) {
3312 return g_snprintf(p, len, "%s:inherit;", key);
3313 } else {
3314 return g_snprintf(p, len, "%s:%s;", key, val->value);
3315 }
3316 }
3317 return 0;
3318 }
3321 /**
3322 *
3323 */
3324 static bool
3325 sp_length_differ(SPILength const *const a, SPILength const *const b)
3326 {
3327 if (a->unit != b->unit) {
3328 if (a->unit == SP_CSS_UNIT_EM) return true;
3329 if (a->unit == SP_CSS_UNIT_EX) return true;
3330 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3331 if (b->unit == SP_CSS_UNIT_EM) return true;
3332 if (b->unit == SP_CSS_UNIT_EX) return true;
3333 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3334 }
3336 return (a->computed != b->computed);
3337 }
3341 /**
3342 * Write SPILength object into string.
3343 */
3344 static gint
3345 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3346 SPILength const *const val, SPILength const *const base, guint const flags)
3347 {
3348 Inkscape::CSSOStringStream os;
3350 if ((flags & SP_STYLE_FLAG_ALWAYS)
3351 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3352 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3353 && (!base->set || sp_length_differ(val, base))))
3354 {
3355 if (val->inherit) {
3356 return g_snprintf(p, len, "%s:inherit;", key);
3357 } else {
3358 switch (val->unit) {
3359 case SP_CSS_UNIT_NONE:
3360 os << key << ":" << val->computed << ";";
3361 return g_strlcpy(p, os.str().c_str(), len);
3362 break;
3363 case SP_CSS_UNIT_PX:
3364 os << key << ":" << val->computed << "px;";
3365 return g_strlcpy(p, os.str().c_str(), len);
3366 break;
3367 case SP_CSS_UNIT_PT:
3368 os << key << ":" << val->computed * PT_PER_PX << "pt;";
3369 return g_strlcpy(p, os.str().c_str(), len);
3370 break;
3371 case SP_CSS_UNIT_PC:
3372 os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3373 return g_strlcpy(p, os.str().c_str(), len);
3374 break;
3375 case SP_CSS_UNIT_MM:
3376 os << key << ":" << val->computed * MM_PER_PX << "mm;";
3377 return g_strlcpy(p, os.str().c_str(), len);
3378 break;
3379 case SP_CSS_UNIT_CM:
3380 os << key << ":" << val->computed * CM_PER_PX << "cm;";
3381 return g_strlcpy(p, os.str().c_str(), len);
3382 break;
3383 case SP_CSS_UNIT_IN:
3384 os << key << ":" << val->computed * IN_PER_PX << "in;";
3385 return g_strlcpy(p, os.str().c_str(), len);
3386 break;
3387 case SP_CSS_UNIT_EM:
3388 os << key << ":" << val->value << "em;";
3389 return g_strlcpy(p, os.str().c_str(), len);
3390 break;
3391 case SP_CSS_UNIT_EX:
3392 os << key << ":" << val->value << "ex;";
3393 return g_strlcpy(p, os.str().c_str(), len);
3394 break;
3395 case SP_CSS_UNIT_PERCENT:
3396 os << key << ":" << (val->value * 100.0) << "%;";
3397 return g_strlcpy(p, os.str().c_str(), len);
3398 break;
3399 default:
3400 /* Invalid */
3401 break;
3402 }
3403 }
3404 }
3405 return 0;
3406 }
3409 /**
3410 *
3411 */
3412 static bool
3413 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3414 {
3415 if (a->normal != b->normal) return true;
3416 if (a->normal) return false;
3418 if (a->unit != b->unit) {
3419 if (a->unit == SP_CSS_UNIT_EM) return true;
3420 if (a->unit == SP_CSS_UNIT_EX) return true;
3421 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3422 if (b->unit == SP_CSS_UNIT_EM) return true;
3423 if (b->unit == SP_CSS_UNIT_EX) return true;
3424 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3425 }
3427 return (a->computed != b->computed);
3428 }
3430 /**
3431 * Write SPILengthOrNormal object into string.
3432 */
3433 static gint
3434 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3435 SPILengthOrNormal const *const val,
3436 SPILengthOrNormal const *const base,
3437 guint const flags)
3438 {
3439 if ((flags & SP_STYLE_FLAG_ALWAYS)
3440 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3441 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3442 && (!base->set || sp_lengthornormal_differ(val, base))))
3443 {
3444 if (val->normal) {
3445 return g_snprintf(p, len, "%s:normal;", key);
3446 } else {
3447 SPILength length;
3448 length.set = val->set;
3449 length.inherit = val->inherit;
3450 length.unit = val->unit;
3451 length.value = val->value;
3452 length.computed = val->computed;
3453 return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3454 }
3455 }
3456 return 0;
3457 }
3459 /**
3460 *
3461 */
3462 static bool
3463 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3464 {
3465 return a->underline != b->underline
3466 || a->overline != b->overline
3467 || a->line_through != b->line_through
3468 || a->blink != b->blink;
3469 }
3471 /**
3472 * Write SPITextDecoration object into string.
3473 */
3474 static gint
3475 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3476 SPITextDecoration const *const val,
3477 SPITextDecoration const *const base,
3478 guint const flags)
3479 {
3480 Inkscape::CSSOStringStream os;
3482 if ((flags & SP_STYLE_FLAG_ALWAYS)
3483 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3484 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3485 && (!base->set || sp_textdecoration_differ(val, base))))
3486 {
3487 if (val->inherit) {
3488 return g_snprintf(p, len, "%s:inherit;", key);
3489 } else {
3490 os << key << ":";
3491 if (val->underline || val->overline || val->line_through || val->blink) {
3492 if (val->underline) os << " underline";
3493 if (val->overline) os << " overline";
3494 if (val->line_through) os << " line-through";
3495 if (val->blink) os << " blink";
3496 } else
3497 os << "none";
3498 os << ";";
3499 return g_strlcpy(p, os.str().c_str(), len);
3500 }
3501 }
3502 return 0;
3503 }
3505 /**
3506 *
3507 */
3508 static bool
3509 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3510 {
3511 if (a->type != b->type)
3512 return true;
3513 if (a->type == SP_PAINT_TYPE_COLOR)
3514 return !(sp_color_is_equal(&a->value.color, &b->value.color)
3515 && ((a->iccColor == b->iccColor)
3516 || (a->iccColor && b->iccColor
3517 && (a->iccColor->colorProfile == b->iccColor->colorProfile)
3518 && (a->iccColor->colors == b->iccColor->colors))));
3519 /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3520 * in the string representation. */
3521 if (a->type == SP_PAINT_TYPE_PAINTSERVER)
3522 return (a->value.paint.server != b->value.paint.server);
3523 return false;
3524 }
3528 /**
3529 * Write SPIPaint object into string.
3530 */
3531 static gint
3532 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3533 SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3534 {
3535 if ((flags & SP_STYLE_FLAG_ALWAYS)
3536 || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3537 || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3538 && (!base->set || sp_paint_differ(paint, base))))
3539 {
3540 if (paint->inherit) {
3541 return g_snprintf(b, len, "%s:inherit;", key);
3542 } else if (paint->currentcolor) {
3543 return g_snprintf(b, len, "%s:currentColor;", key);
3544 } else {
3545 switch (paint->type) {
3546 case SP_PAINT_TYPE_COLOR: {
3547 char color_buf[8];
3548 sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
3549 if (paint->iccColor) {
3550 CSSOStringStream css;
3551 css << color_buf << " icc-color(" << paint->iccColor->colorProfile;
3552 for (vector<double>::const_iterator i(paint->iccColor->colors.begin()),
3553 iEnd(paint->iccColor->colors.end());
3554 i != iEnd; ++i) {
3555 css << ", " << *i;
3556 }
3557 css << ')';
3558 return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
3559 } else {
3560 return g_snprintf(b, len, "%s:%s;", key, color_buf);
3561 }
3562 }
3563 case SP_PAINT_TYPE_PAINTSERVER:
3564 return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
3565 default:
3566 break;
3567 }
3568 return g_snprintf(b, len, "%s:none;", key);
3569 }
3570 }
3571 return 0;
3572 }
3575 /**
3576 *
3577 */
3578 static bool
3579 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3580 {
3581 if (a->type != b->type)
3582 return true;
3583 if (a->type == SP_FONT_SIZE_LENGTH) {
3584 if (a->computed != b->computed)
3585 return true;
3586 } else {
3587 if (a->value != b->value)
3588 return true;
3589 }
3590 return false;
3591 }
3594 /**
3595 * Write SPIFontSize object into string.
3596 */
3597 static gint
3598 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3599 SPIFontSize const *const val, SPIFontSize const *const base,
3600 guint const flags)
3601 {
3602 if ((flags & SP_STYLE_FLAG_ALWAYS)
3603 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3604 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3605 && (!base->set || sp_fontsize_differ(val, base))))
3606 {
3607 if (val->inherit) {
3608 return g_snprintf(p, len, "%s:inherit;", key);
3609 } else if (val->type == SP_FONT_SIZE_LITERAL) {
3610 for (unsigned i = 0; enum_font_size[i].key; i++) {
3611 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3612 return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3613 }
3614 }
3615 } else if (val->type == SP_FONT_SIZE_LENGTH) {
3616 Inkscape::CSSOStringStream os;
3617 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
3618 return g_strlcpy(p, os.str().c_str(), len);
3619 } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3620 Inkscape::CSSOStringStream os;
3621 os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3622 return g_strlcpy(p, os.str().c_str(), len);
3623 }
3624 }
3625 return 0;
3626 }
3629 /**
3630 * Write SPIFilter object into string.
3631 */
3632 static gint
3633 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3634 SPIFilter const *const val, SPIFilter const *const base,
3635 guint const flags)
3636 {
3637 if ((flags & SP_STYLE_FLAG_ALWAYS)
3638 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3639 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3640 {
3641 if (val->inherit) {
3642 return g_snprintf(p, len, "%s:inherit;", key);
3643 } else if (val->uri) {
3644 return g_snprintf(p, len, "%s:url(%s);", key, val->uri);
3645 }
3646 }
3649 return 0;
3650 }
3653 /**
3654 * Clear paint object, and disconnect style from paintserver (if present).
3655 */
3656 static void
3657 sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
3658 {
3659 if ((paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
3660 if (paint == &style->fill) {
3661 if (style->fill_hreffed) {
3662 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3663 style->fill_hreffed = false;
3664 }
3665 style->fill_release_connection.disconnect();
3666 style->fill_modified_connection.disconnect();
3667 } else {
3668 assert(paint == &style->stroke); // Only fill & stroke can have a paint server.
3669 if (style->stroke_hreffed) {
3670 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3671 style->stroke_hreffed = false;
3672 }
3673 style->stroke_release_connection.disconnect();
3674 style->stroke_modified_connection.disconnect();
3675 }
3677 paint->value.paint.server = NULL;
3678 }
3679 paint->value.paint.uri = NULL;
3680 paint->type = SP_PAINT_TYPE_NONE;
3681 delete paint->iccColor;
3682 paint->iccColor = NULL;
3683 }
3686 /**
3687 * Clear filter object, and disconnect style from paintserver (if present).
3688 */
3689 static void
3690 sp_style_filter_clear(SPStyle *style, SPIFilter *f)
3691 {
3693 if (style->filter_hreffed) {
3694 sp_object_hunref(SP_OBJECT(f->filter), style);
3695 style->filter_hreffed = false;
3696 }
3697 //style->fill_release_connection.disconnect();
3698 //style->fill_modified_connection.disconnect();
3700 f->filter = NULL;
3702 }
3705 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3707 void
3708 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3709 {
3710 Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3712 if (repr == NULL) return;
3714 g_return_if_fail(linked != NULL);
3716 gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3718 SPCSSAttr *css = sp_repr_css_attr_new();
3719 sp_repr_css_set_property(css, property, val);
3720 g_free(val);
3721 if (recursive) {
3722 sp_repr_css_change_recursive(repr, css, "style");
3723 } else {
3724 sp_repr_css_change(repr, css, "style");
3725 }
3726 sp_repr_css_attr_unref(css);
3727 }
3730 /**
3731 * Clear all style property attributes in object.
3732 */
3733 void
3734 sp_style_unset_property_attrs(SPObject *o)
3735 {
3736 if (!o) return;
3738 SPStyle *style = SP_OBJECT_STYLE(o);
3739 if (!style) return;
3741 Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3742 if (!repr) return;
3744 if (style->opacity.set) {
3745 repr->setAttribute("opacity", NULL);
3746 }
3747 if (style->color.set) {
3748 repr->setAttribute("color", NULL);
3749 }
3750 if (style->fill.set) {
3751 repr->setAttribute("fill", NULL);
3752 }
3753 if (style->fill_opacity.set) {
3754 repr->setAttribute("fill-opacity", NULL);
3755 }
3756 if (style->fill_rule.set) {
3757 repr->setAttribute("fill-rule", NULL);
3758 }
3759 if (style->stroke.set) {
3760 repr->setAttribute("stroke", NULL);
3761 }
3762 if (style->stroke_width.set) {
3763 repr->setAttribute("stroke-width", NULL);
3764 }
3765 if (style->stroke_linecap.set) {
3766 repr->setAttribute("stroke-linecap", NULL);
3767 }
3768 if (style->stroke_linejoin.set) {
3769 repr->setAttribute("stroke-linejoin", NULL);
3770 }
3771 if (style->marker[SP_MARKER_LOC].set) {
3772 repr->setAttribute("marker", NULL);
3773 }
3774 if (style->marker[SP_MARKER_LOC_START].set) {
3775 repr->setAttribute("marker-start", NULL);
3776 }
3777 if (style->marker[SP_MARKER_LOC_MID].set) {
3778 repr->setAttribute("marker-mid", NULL);
3779 }
3780 if (style->marker[SP_MARKER_LOC_END].set) {
3781 repr->setAttribute("marker-end", NULL);
3782 }
3783 if (style->stroke_opacity.set) {
3784 repr->setAttribute("stroke-opacity", NULL);
3785 }
3786 if (style->stroke_dasharray_set) {
3787 repr->setAttribute("stroke-dasharray", NULL);
3788 }
3789 if (style->stroke_dashoffset_set) {
3790 repr->setAttribute("stroke-dashoffset", NULL);
3791 }
3792 if (style->text_private && style->text->font_family.set) {
3793 repr->setAttribute("font-family", NULL);
3794 }
3795 if (style->text_anchor.set) {
3796 repr->setAttribute("text-anchor", NULL);
3797 }
3798 if (style->writing_mode.set) {
3799 repr->setAttribute("writing_mode", NULL);
3800 }
3801 if (style->filter.set) {
3802 repr->setAttribute("filter", NULL);
3803 }
3804 if (style->enable_background.set) {
3805 repr->setAttribute("enable-background", NULL);
3806 }
3807 }
3809 /**
3810 * \pre style != NULL.
3811 * \pre flags in {IFSET, ALWAYS}.
3812 */
3813 SPCSSAttr *
3814 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3815 {
3816 g_return_val_if_fail(style != NULL, NULL);
3817 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3818 (flags == SP_STYLE_FLAG_ALWAYS) ),
3819 NULL);
3820 gchar *style_str = sp_style_write_string(style, flags);
3821 SPCSSAttr *css = sp_repr_css_attr_new();
3822 sp_repr_css_attr_add_from_string(css, style_str);
3823 g_free(style_str);
3824 return css;
3825 }
3828 /**
3829 * \pre object != NULL
3830 * \pre flags in {IFSET, ALWAYS}.
3831 */
3832 SPCSSAttr *
3833 sp_css_attr_from_object(SPObject *object, guint const flags)
3834 {
3835 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3836 (flags == SP_STYLE_FLAG_ALWAYS) ),
3837 NULL);
3838 SPStyle const *const style = SP_OBJECT_STYLE(object);
3839 if (style == NULL)
3840 return NULL;
3841 return sp_css_attr_from_style(style, flags);
3842 }
3844 /**
3845 * Unset any text-related properties
3846 */
3847 SPCSSAttr *
3848 sp_css_attr_unset_text(SPCSSAttr *css)
3849 {
3850 sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3851 sp_repr_css_set_property(css, "font-size", NULL);
3852 sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3853 sp_repr_css_set_property(css, "font-style", NULL);
3854 sp_repr_css_set_property(css, "font-variant", NULL);
3855 sp_repr_css_set_property(css, "font-weight", NULL);
3856 sp_repr_css_set_property(css, "font-stretch", NULL);
3857 sp_repr_css_set_property(css, "font-family", NULL);
3858 sp_repr_css_set_property(css, "text-indent", NULL);
3859 sp_repr_css_set_property(css, "text-align", NULL);
3860 sp_repr_css_set_property(css, "text-decoration", NULL);
3861 sp_repr_css_set_property(css, "line-height", NULL);
3862 sp_repr_css_set_property(css, "letter-spacing", NULL);
3863 sp_repr_css_set_property(css, "word-spacing", NULL);
3864 sp_repr_css_set_property(css, "text-transform", NULL);
3865 sp_repr_css_set_property(css, "direction", NULL);
3866 sp_repr_css_set_property(css, "block-progression", NULL);
3867 sp_repr_css_set_property(css, "writing-mode", NULL);
3868 sp_repr_css_set_property(css, "text-anchor", NULL);
3869 sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3870 sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3871 sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3872 sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3874 return css;
3875 }
3877 bool
3878 is_url(char const *p)
3879 {
3880 if (p == NULL)
3881 return false;
3882 /** \todo
3883 * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3884 * in property values must start with 'url('.
3885 */
3886 return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3887 }
3889 /**
3890 * Unset any properties that contain URI values.
3891 *
3892 * Used for storing style that will be reused across documents when carrying
3893 * the referenced defs is impractical.
3894 */
3895 SPCSSAttr *
3896 sp_css_attr_unset_uris(SPCSSAttr *css)
3897 {
3898 // All properties that may hold <uri> or <paint> according to SVG 1.1
3899 if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3900 if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3901 if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3902 if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3903 if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3904 if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3905 if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3906 if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3907 if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3908 if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3910 return css;
3911 }
3913 /**
3914 * Scale a single-value property.
3915 */
3916 void
3917 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3918 double ex, bool only_with_units = false)
3919 {
3920 gchar const *w = sp_repr_css_property(css, property, NULL);
3921 if (w) {
3922 gchar *units = NULL;
3923 double wd = g_ascii_strtod(w, &units) * ex;
3924 if (w == units) {// nothing converted, non-numeric value
3925 return;
3926 }
3927 if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3928 // only_with_units, but no units found, so do nothing.
3929 return;
3930 }
3931 Inkscape::CSSOStringStream os;
3932 os << wd << units; // reattach units
3933 sp_repr_css_set_property(css, property, os.str().c_str());
3934 }
3935 }
3937 /**
3938 * Scale a list-of-values property.
3939 */
3940 void
3941 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3942 {
3943 gchar const *string = sp_repr_css_property(css, property, NULL);
3944 if (string) {
3945 Inkscape::CSSOStringStream os;
3946 gchar **a = g_strsplit(string, ",", 10000);
3947 bool first = true;
3948 for (gchar **i = a; i != NULL; i++) {
3949 gchar *w = *i;
3950 if (w == NULL)
3951 break;
3952 gchar *units = NULL;
3953 double wd = g_ascii_strtod(w, &units) * ex;
3954 if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3955 g_strfreev(a);
3956 return;
3957 }
3958 if (!first) {
3959 os << ",";
3960 }
3961 os << wd << units; // reattach units
3962 first = false;
3963 }
3964 sp_repr_css_set_property(css, property, os.str().c_str());
3965 g_strfreev(a);
3966 }
3967 }
3969 /**
3970 * Scale any properties that may hold <length> by ex.
3971 */
3972 SPCSSAttr *
3973 sp_css_attr_scale(SPCSSAttr *css, double ex)
3974 {
3975 sp_css_attr_scale_property_single(css, "baseline-shift", ex);
3976 sp_css_attr_scale_property_single(css, "stroke-width", ex);
3977 sp_css_attr_scale_property_list (css, "stroke-dasharray", ex);
3978 sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
3979 sp_css_attr_scale_property_single(css, "font-size", ex);
3980 sp_css_attr_scale_property_single(css, "kerning", ex);
3981 sp_css_attr_scale_property_single(css, "letter-spacing", ex);
3982 sp_css_attr_scale_property_single(css, "word-spacing", ex);
3983 sp_css_attr_scale_property_single(css, "line-height", ex, true);
3985 return css;
3986 }
3989 /**
3990 * Remove quotes from SPIString object value.
3991 *
3992 * \todo FIXME: now used for font family, but perhaps this should apply to
3993 * ALL strings (check CSS spec), in which case this should be part of
3994 * read_istring.
3995 */
3996 static void
3997 css2_unescape_unquote(SPIString *val)
3998 {
3999 if (val->set && val->value && strlen(val->value) >= 2) {
4001 /// \todo unescape all \-escaped chars
4003 int l = strlen(val->value);
4004 if ( ( val->value[0] == '"' && val->value[l - 1] == '"' ) ||
4005 ( val->value[0] == '\'' && val->value[l - 1] == '\'' ) ) {
4006 memcpy(val->value, val->value + 1, l - 2);
4007 val->value[l - 2] = '\0';
4008 }
4009 }
4010 }
4013 /*
4014 Local Variables:
4015 mode:c++
4016 c-file-style:"stroustrup"
4017 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4018 indent-tabs-mode:nil
4019 fill-column:99
4020 End:
4021 */
4022 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :