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