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 #define BMAX 8192
55 class SPStyleEnum;
57 /*#########################
58 ## FORWARD DECLARATIONS
59 #########################*/
60 static void sp_style_clear(SPStyle *style);
62 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
64 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
65 static void sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent);
66 static void sp_style_read_dash(SPStyle *style, gchar const *str);
68 static SPTextStyle *sp_text_style_new(void);
69 static void sp_text_style_clear(SPTextStyle *ts);
70 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
71 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
72 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
73 static void sp_style_privatize_text(SPStyle *style);
75 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
76 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
77 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
78 static void sp_style_read_istring(SPIString *val, gchar const *str);
79 static void sp_style_read_ilength(SPILength *val, gchar const *str);
80 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
81 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
82 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
83 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
84 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
85 static void sp_style_read_ifilter(gchar const *str, SPStyle *style, SPDocument *document);
87 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
88 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
89 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
90 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
92 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
93 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
94 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
95 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
96 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
97 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
98 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
99 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);
100 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);
101 static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
103 static void sp_style_filter_clear(SPStyle *style);
105 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
106 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
108 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
109 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
111 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
113 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
114 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
116 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
118 struct SPStyleEnum {
119 gchar const *key;
120 gint value;
121 };
123 static SPStyleEnum const enum_fill_rule[] = {
124 {"nonzero", SP_WIND_RULE_NONZERO},
125 {"evenodd", SP_WIND_RULE_EVENODD},
126 {NULL, -1}
127 };
129 static SPStyleEnum const enum_stroke_linecap[] = {
130 {"butt", SP_STROKE_LINECAP_BUTT},
131 {"round", SP_STROKE_LINECAP_ROUND},
132 {"square", SP_STROKE_LINECAP_SQUARE},
133 {NULL, -1}
134 };
136 static SPStyleEnum const enum_stroke_linejoin[] = {
137 {"miter", SP_STROKE_LINEJOIN_MITER},
138 {"round", SP_STROKE_LINEJOIN_ROUND},
139 {"bevel", SP_STROKE_LINEJOIN_BEVEL},
140 {NULL, -1}
141 };
143 static SPStyleEnum const enum_font_style[] = {
144 {"normal", SP_CSS_FONT_STYLE_NORMAL},
145 {"italic", SP_CSS_FONT_STYLE_ITALIC},
146 {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
147 {NULL, -1}
148 };
150 static SPStyleEnum const enum_font_size[] = {
151 {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
152 {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
153 {"small", SP_CSS_FONT_SIZE_SMALL},
154 {"medium", SP_CSS_FONT_SIZE_MEDIUM},
155 {"large", SP_CSS_FONT_SIZE_LARGE},
156 {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
157 {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
158 {"smaller", SP_CSS_FONT_SIZE_SMALLER},
159 {"larger", SP_CSS_FONT_SIZE_LARGER},
160 {NULL, -1}
161 };
163 static SPStyleEnum const enum_font_variant[] = {
164 {"normal", SP_CSS_FONT_VARIANT_NORMAL},
165 {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
166 {NULL, -1}
167 };
169 static SPStyleEnum const enum_font_weight[] = {
170 {"100", SP_CSS_FONT_WEIGHT_100},
171 {"200", SP_CSS_FONT_WEIGHT_200},
172 {"300", SP_CSS_FONT_WEIGHT_300},
173 {"400", SP_CSS_FONT_WEIGHT_400},
174 {"500", SP_CSS_FONT_WEIGHT_500},
175 {"600", SP_CSS_FONT_WEIGHT_600},
176 {"700", SP_CSS_FONT_WEIGHT_700},
177 {"800", SP_CSS_FONT_WEIGHT_800},
178 {"900", SP_CSS_FONT_WEIGHT_900},
179 {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
180 {"bold", SP_CSS_FONT_WEIGHT_BOLD},
181 {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
182 {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
183 {NULL, -1}
184 };
186 static SPStyleEnum const enum_font_stretch[] = {
187 {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
188 {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
189 {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
190 {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
191 {"normal", SP_CSS_FONT_STRETCH_NORMAL},
192 {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
193 {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
194 {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
195 {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
196 {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
197 {"wider", SP_CSS_FONT_STRETCH_WIDER},
198 {NULL, -1}
199 };
201 static SPStyleEnum const enum_text_align[] = {
202 {"start", SP_CSS_TEXT_ALIGN_START},
203 {"end", SP_CSS_TEXT_ALIGN_END},
204 {"left", SP_CSS_TEXT_ALIGN_LEFT},
205 {"right", SP_CSS_TEXT_ALIGN_RIGHT},
206 {"center", SP_CSS_TEXT_ALIGN_CENTER},
207 {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
208 {NULL, -1}
209 };
211 static SPStyleEnum const enum_text_transform[] = {
212 {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
213 {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
214 {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
215 {"none", SP_CSS_TEXT_TRANSFORM_NONE},
216 {NULL, -1}
217 };
219 static SPStyleEnum const enum_text_anchor[] = {
220 {"start", SP_CSS_TEXT_ANCHOR_START},
221 {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
222 {"end", SP_CSS_TEXT_ANCHOR_END},
223 {NULL, -1}
224 };
226 static SPStyleEnum const enum_direction[] = {
227 {"ltr", SP_CSS_DIRECTION_LTR},
228 {"rtl", SP_CSS_DIRECTION_RTL},
229 {NULL, -1}
230 };
232 static SPStyleEnum const enum_block_progression[] = {
233 {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
234 {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
235 {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
236 {NULL, -1}
237 };
239 static SPStyleEnum const enum_writing_mode[] = {
240 /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
241 * input file said lr. We prefer writing lr-tb on the grounds that the spec says the initial
242 * value is lr-tb rather than lr.
243 *
244 * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
245 * sharing enumerators for different strings may be a bug (once we support ecma script).
246 */
247 {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
248 {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
249 {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
250 {"lr", SP_CSS_WRITING_MODE_LR_TB},
251 {"rl", SP_CSS_WRITING_MODE_RL_TB},
252 {"tb", SP_CSS_WRITING_MODE_TB_RL},
253 {NULL, -1}
254 };
256 static SPStyleEnum const enum_visibility[] = {
257 {"hidden", SP_CSS_VISIBILITY_HIDDEN},
258 {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
259 {"visible", SP_CSS_VISIBILITY_VISIBLE},
260 {NULL, -1}
261 };
263 static SPStyleEnum const enum_overflow[] = {
264 {"visible", SP_CSS_OVERFLOW_VISIBLE},
265 {"hidden", SP_CSS_OVERFLOW_HIDDEN},
266 {"scroll", SP_CSS_OVERFLOW_SCROLL},
267 {"auto", SP_CSS_OVERFLOW_AUTO},
268 {NULL, -1}
269 };
271 static SPStyleEnum const enum_display[] = {
272 {"none", SP_CSS_DISPLAY_NONE},
273 {"inline", SP_CSS_DISPLAY_INLINE},
274 {"block", SP_CSS_DISPLAY_BLOCK},
275 {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
276 {"run-in", SP_CSS_DISPLAY_RUN_IN},
277 {"compact", SP_CSS_DISPLAY_COMPACT},
278 {"marker", SP_CSS_DISPLAY_MARKER},
279 {"table", SP_CSS_DISPLAY_TABLE},
280 {"inline-table", SP_CSS_DISPLAY_INLINE_TABLE},
281 {"table-row-group", SP_CSS_DISPLAY_TABLE_ROW_GROUP},
282 {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
283 {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
284 {"table-row", SP_CSS_DISPLAY_TABLE_ROW},
285 {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
286 {"table-column", SP_CSS_DISPLAY_TABLE_COLUMN},
287 {"table-cell", SP_CSS_DISPLAY_TABLE_CELL},
288 {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
289 {NULL, -1}
290 };
292 static SPStyleEnum const enum_shape_rendering[] = {
293 {"auto", 0},
294 {"optimizeSpeed", 0},
295 {"crispEdges", 0},
296 {"geometricPrecision", 0},
297 {NULL, -1}
298 };
300 static SPStyleEnum const enum_color_rendering[] = {
301 {"auto", 0},
302 {"optimizeSpeed", 0},
303 {"optimizeQuality", 0},
304 {NULL, -1}
305 };
307 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
309 static SPStyleEnum const enum_text_rendering[] = {
310 {"auto", 0},
311 {"optimizeSpeed", 0},
312 {"optimizeLegibility", 0},
313 {"geometricPrecision", 0},
314 {NULL, -1}
315 };
317 static SPStyleEnum const enum_enable_background[] = {
318 {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
319 {"new", SP_CSS_BACKGROUND_NEW},
320 {NULL, -1}
321 };
323 /**
324 * Release callback.
325 */
326 static void
327 sp_style_object_release(SPObject *object, SPStyle *style)
328 {
329 (void)object; // TODO
330 style->object = NULL;
331 }
333 /**
334 * Emit style modified signal on style's object if the filter changed.
335 */
336 static void
337 sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
338 {
339 (void)flags; // TODO
340 SPFilter *filter=static_cast<SPFilter *>(obj);
341 if (style->getFilter() == filter)
342 {
343 if (style->object) {
344 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
345 }
346 }
347 }
349 /**
350 * Gets called when the filter is (re)attached to the style
351 */
352 static void
353 sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
354 {
355 if (old_ref) {
356 style->filter_modified_connection.disconnect();
357 }
358 if ( SP_IS_FILTER(ref))
359 {
360 style->filter_modified_connection =
361 ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
362 }
364 sp_style_filter_ref_modified(ref, 0, style);
365 }
367 /**
368 * Emit style modified signal on style's object if server is style's fill
369 * or stroke paint server.
370 */
371 static void
372 sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
373 {
374 (void)flags; // TODO
375 SPPaintServer *server = static_cast<SPPaintServer *>(obj);
377 if ((style->fill.isPaintserver())
378 && style->getFillPaintServer() == server)
379 {
380 if (style->object) {
381 /** \todo
382 * fixme: I do not know, whether it is optimal - we are
383 * forcing reread of everything (Lauris)
384 */
385 /** \todo
386 * fixme: We have to use object_modified flag, because parent
387 * flag is only available downstreams.
388 */
389 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
390 }
391 } else if ((style->stroke.isPaintserver())
392 && style->getStrokePaintServer() == server)
393 {
394 if (style->object) {
395 /// \todo fixme:
396 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
397 }
398 } else if (server) {
399 g_assert_not_reached();
400 }
401 }
403 /**
404 * Gets called when the paintserver is (re)attached to the style
405 */
406 static void
407 sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
408 {
409 if (old_ref) {
410 style->fill_ps_modified_connection.disconnect();
411 }
412 if (SP_IS_PAINT_SERVER(ref)) {
413 style->fill_ps_modified_connection =
414 ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
415 }
417 sp_style_paint_server_ref_modified(ref, 0, style);
418 }
420 /**
421 * Gets called when the paintserver is (re)attached to the style
422 */
423 static void
424 sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
425 {
426 if (old_ref) {
427 style->stroke_ps_modified_connection.disconnect();
428 }
429 if (SP_IS_PAINT_SERVER(ref)) {
430 style->stroke_ps_modified_connection =
431 ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
432 }
434 sp_style_paint_server_ref_modified(ref, 0, style);
435 }
437 /**
438 * Returns a new SPStyle object with settings as per sp_style_clear().
439 */
440 SPStyle *
441 sp_style_new(SPDocument *document)
442 {
443 SPStyle *const style = g_new0(SPStyle, 1);
445 style->refcount = 1;
446 style->object = NULL;
447 style->document = document;
448 style->text = sp_text_style_new();
449 style->text_private = TRUE;
451 sp_style_clear(style);
453 style->cloned = false;
455 new (&style->release_connection) sigc::connection();
456 new (&style->filter_modified_connection) sigc::connection();
457 new (&style->fill_ps_modified_connection) sigc::connection();
458 new (&style->stroke_ps_modified_connection) sigc::connection();
460 return style;
461 }
464 /**
465 * Creates a new SPStyle object, and attaches it to the specified SPObject.
466 */
467 SPStyle *
468 sp_style_new_from_object(SPObject *object)
469 {
470 g_return_val_if_fail(object != NULL, NULL);
471 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
473 SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
474 style->object = object;
475 style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
477 if (object && SP_OBJECT_IS_CLONED(object)) {
478 style->cloned = true;
479 }
481 return style;
482 }
485 /**
486 * Increase refcount of style.
487 */
488 SPStyle *
489 sp_style_ref(SPStyle *style)
490 {
491 g_return_val_if_fail(style != NULL, NULL);
492 g_return_val_if_fail(style->refcount > 0, NULL);
494 style->refcount += 1;
496 return style;
497 }
500 /**
501 * Decrease refcount of style with possible destruction.
502 */
503 SPStyle *
504 sp_style_unref(SPStyle *style)
505 {
506 g_return_val_if_fail(style != NULL, NULL);
507 g_return_val_if_fail(style->refcount > 0, NULL);
509 style->refcount -= 1;
511 if (style->refcount < 1) {
512 style->release_connection.disconnect();
513 style->release_connection.~connection();
514 if (style->text) sp_text_style_unref(style->text);
516 if (style->fill.value.href) {
517 style->fill_ps_modified_connection.disconnect();
518 delete style->fill.value.href;
519 style->fill.value.href = NULL;
520 }
521 if (style->stroke.value.href) {
522 style->stroke_ps_modified_connection.disconnect();
523 delete style->stroke.value.href;
524 style->stroke.value.href = NULL;
525 }
526 if (style->filter.href) {
527 style->filter_modified_connection.disconnect();
528 delete style->filter.href;
529 style->filter.href = NULL;
530 }
532 style->filter_modified_connection.~connection();
533 style->fill_ps_modified_connection.~connection();
534 style->stroke_ps_modified_connection.~connection();
536 style->fill.clear();
537 style->stroke.clear();
538 sp_style_filter_clear(style);
540 g_free(style->stroke_dash.dash);
541 g_free(style);
542 }
544 return NULL;
545 }
547 /**
548 * Reads the various style parameters for an object from repr.
549 */
550 static void
551 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
552 {
553 g_assert(style != NULL);
554 g_assert(repr != NULL);
555 g_assert(!object || (SP_OBJECT_REPR(object) == repr));
557 sp_style_clear(style);
559 if (object && SP_OBJECT_IS_CLONED(object)) {
560 style->cloned = true;
561 }
563 /* 1. Style attribute */
564 gchar const *val = repr->attribute("style");
565 if (val != NULL) {
566 sp_style_merge_from_style_string(style, val);
567 }
569 if (object) {
570 sp_style_merge_from_object_stylesheet(style, object);
571 } else {
572 /** \todo No stylesheet information. Find out under what circumstances
573 * this occurs, and handle accordingly. (If we really wanted to, we
574 * could probably get stylesheets by going through repr->doc.)
575 */
576 }
578 /* 2. Presentation attributes */
579 /* CSS2 */
580 SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
581 SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
582 SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
583 /* Font */
584 SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
585 SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
586 SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
587 SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
588 SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
589 /* Text (css2 chapter 16) */
590 SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
591 SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
592 if (!style->text_decoration.set) {
593 val = repr->attribute("text-decoration");
594 if (val) {
595 sp_style_read_itextdecoration(&style->text_decoration, val);
596 }
597 }
598 if (!style->line_height.set) {
599 val = repr->attribute("line-height");
600 if (val) {
601 sp_style_read_ilengthornormal(&style->line_height, val);
602 }
603 }
604 if (!style->letter_spacing.set) {
605 val = repr->attribute("letter-spacing");
606 if (val) {
607 sp_style_read_ilengthornormal(&style->letter_spacing, val);
608 }
609 }
610 if (!style->word_spacing.set) {
611 val = repr->attribute("word-spacing");
612 if (val) {
613 sp_style_read_ilengthornormal(&style->word_spacing, val);
614 }
615 }
616 SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
617 SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
618 SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
620 /* SVG */
621 SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
622 enum_writing_mode, true);
623 SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
624 enum_text_anchor, true);
626 /* opacity */
627 if (!style->opacity.set) {
628 val = repr->attribute("opacity");
629 if (val) {
630 sp_style_read_iscale24(&style->opacity, val);
631 }
632 }
633 /* color */
634 if (!style->color.set) {
635 val = repr->attribute("color");
636 if (val) {
637 sp_style_read_icolor(&style->color, val, style, ( object
638 ? SP_OBJECT_DOCUMENT(object)
639 : NULL ));
640 }
641 }
642 /* fill */
643 if (!style->fill.set) {
644 val = repr->attribute("fill");
645 if (val) {
646 sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
647 }
648 }
649 /* fill-opacity */
650 if (!style->fill_opacity.set) {
651 val = repr->attribute("fill-opacity");
652 if (val) {
653 sp_style_read_iscale24(&style->fill_opacity, val);
654 }
655 }
656 /* fill-rule */
657 SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
658 /* stroke */
659 if (!style->stroke.set) {
660 val = repr->attribute("stroke");
661 if (val) {
662 sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
663 }
664 }
665 SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
666 SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
667 SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
668 SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
670 /* markers */
671 if (!style->marker[SP_MARKER_LOC].set) {
672 val = repr->attribute("marker");
673 if (val) {
674 sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
675 }
676 }
677 if (!style->marker[SP_MARKER_LOC_START].set) {
678 val = repr->attribute("marker-start");
679 if (val) {
680 sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
681 }
682 }
683 if (!style->marker[SP_MARKER_LOC_MID].set) {
684 val = repr->attribute("marker-mid");
685 if (val) {
686 sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
687 }
688 }
689 if (!style->marker[SP_MARKER_LOC_END].set) {
690 val = repr->attribute("marker-end");
691 if (val) {
692 sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
693 }
694 }
696 /* stroke-opacity */
697 if (!style->stroke_opacity.set) {
698 val = repr->attribute("stroke-opacity");
699 if (val) {
700 sp_style_read_iscale24(&style->stroke_opacity, val);
701 }
702 }
703 if (!style->stroke_dasharray_set) {
704 val = repr->attribute("stroke-dasharray");
705 if (val) {
706 sp_style_read_dash(style, val);
707 }
708 }
709 if (!style->stroke_dashoffset_set) {
710 /* fixme */
711 val = repr->attribute("stroke-dashoffset");
712 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
713 style->stroke_dashoffset_set = TRUE;
714 } else {
715 style->stroke_dashoffset_set = FALSE;
716 }
717 }
719 /* -inkscape-font-specification */
720 if (!style->text_private || !style->text->font_specification.set) {
721 val = repr->attribute("-inkscape-font-specification");
722 if (val) {
723 if (!style->text_private) sp_style_privatize_text(style);
724 gchar *val_unquoted = attribute_unquote(val);
725 sp_style_read_istring(&style->text->font_specification, val_unquoted);
726 if (val_unquoted) g_free (val_unquoted);
727 }
728 }
730 /* font-family */
731 if (!style->text_private || !style->text->font_family.set) {
732 val = repr->attribute("font-family");
733 if (val) {
734 if (!style->text_private) sp_style_privatize_text(style);
735 gchar *val_unquoted = attribute_unquote(val);
736 sp_style_read_istring(&style->text->font_family, val_unquoted);
737 if (val_unquoted) g_free (val_unquoted);
738 }
739 }
741 /* filter effects */
742 if (!style->filter.set) {
743 val = repr->attribute("filter");
744 if (val) {
745 sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
746 }
747 }
748 SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
749 "enable-background", enum_enable_background, true);
751 /* 3. Merge from parent */
752 if (object) {
753 if (object->parent) {
754 sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
755 }
756 } else {
757 if (sp_repr_parent(repr)) {
758 /// \todo fixme: This is not the prettiest thing (Lauris)
759 SPStyle *parent = sp_style_new(NULL);
760 sp_style_read(parent, NULL, sp_repr_parent(repr));
761 sp_style_merge_from_parent(style, parent);
762 sp_style_unref(parent);
763 }
764 }
765 }
768 /**
769 * Read style properties from object's repr.
770 *
771 * 1. Reset existing object style
772 * 2. Load current effective object style
773 * 3. Load i attributes from immediate parent (which has to be up-to-date)
774 */
775 void
776 sp_style_read_from_object(SPStyle *style, SPObject *object)
777 {
778 g_return_if_fail(style != NULL);
779 g_return_if_fail(object != NULL);
780 g_return_if_fail(SP_IS_OBJECT(object));
782 Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
783 g_return_if_fail(repr != NULL);
785 sp_style_read(style, object, repr);
786 }
789 /**
790 * Read style properties from repr only.
791 */
792 void
793 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
794 {
795 g_return_if_fail(style != NULL);
796 g_return_if_fail(repr != NULL);
798 sp_style_read(style, NULL, repr);
799 }
803 /**
804 *
805 */
806 static void
807 sp_style_privatize_text(SPStyle *style)
808 {
809 SPTextStyle *text = style->text;
810 style->text = sp_text_style_duplicate_unset(style->text);
811 sp_text_style_unref(text);
812 style->text_private = TRUE;
813 }
816 /**
817 * Merge property into style.
818 *
819 * Should be called in order of highest to lowest precedence.
820 * E.g. for a single style string, call from the last declaration to the first,
821 * as CSS says that later declarations override earlier ones.
822 *
823 * \pre val != NULL.
824 */
825 static void
826 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
827 {
828 g_return_if_fail(val != NULL);
830 switch (id) {
831 case SP_PROP_INKSCAPE_FONT_SPEC:
832 if (!style->text_private) sp_style_privatize_text(style);
833 if (!style->text->font_specification.set) {
834 gchar *val_unquoted = attribute_unquote(val);
835 sp_style_read_istring(&style->text->font_specification, val_unquoted);
836 if (val_unquoted) g_free (val_unquoted);
837 }
838 break;
839 /* CSS2 */
840 /* Font */
841 case SP_PROP_FONT_FAMILY:
842 if (!style->text_private) sp_style_privatize_text(style);
843 if (!style->text->font_family.set) {
844 gchar *val_unquoted = attribute_unquote(val);
845 sp_style_read_istring(&style->text->font_family, val_unquoted);
846 if (val_unquoted) g_free (val_unquoted);
847 }
848 break;
849 case SP_PROP_FONT_SIZE:
850 SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
851 break;
852 case SP_PROP_FONT_SIZE_ADJUST:
853 if (strcmp(val, "none") != 0) {
854 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
855 }
856 break;
857 case SP_PROP_FONT_STYLE:
858 SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
859 break;
860 case SP_PROP_FONT_VARIANT:
861 SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
862 break;
863 case SP_PROP_FONT_WEIGHT:
864 SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
865 break;
866 case SP_PROP_FONT_STRETCH:
867 SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
868 break;
869 case SP_PROP_FONT:
870 if (!style->text_private) sp_style_privatize_text(style);
871 if (!style->text->font.set) {
872 g_free(style->text->font.value);
873 style->text->font.value = g_strdup(val);
874 style->text->font.set = TRUE;
875 style->text->font.inherit = (val && !strcmp(val, "inherit"));
876 }
877 break;
878 /* Text */
879 case SP_PROP_TEXT_INDENT:
880 SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
881 break;
882 case SP_PROP_TEXT_ALIGN:
883 SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
884 break;
885 case SP_PROP_TEXT_DECORATION:
886 if (!style->text_decoration.set) {
887 sp_style_read_itextdecoration(&style->text_decoration, val);
888 }
889 break;
890 case SP_PROP_LINE_HEIGHT:
891 if (!style->line_height.set) {
892 sp_style_read_ilengthornormal(&style->line_height, val);
893 }
894 break;
895 case SP_PROP_LETTER_SPACING:
896 if (!style->letter_spacing.set) {
897 sp_style_read_ilengthornormal(&style->letter_spacing, val);
898 }
899 break;
900 case SP_PROP_WORD_SPACING:
901 if (!style->word_spacing.set) {
902 sp_style_read_ilengthornormal(&style->word_spacing, val);
903 }
904 break;
905 case SP_PROP_TEXT_TRANSFORM:
906 SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
907 break;
908 /* Text (css3) */
909 case SP_PROP_DIRECTION:
910 SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
911 break;
912 case SP_PROP_BLOCK_PROGRESSION:
913 SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
914 break;
915 case SP_PROP_WRITING_MODE:
916 SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
917 break;
918 case SP_PROP_TEXT_ANCHOR:
919 SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
920 break;
921 /* Text (unimplemented) */
922 case SP_PROP_TEXT_RENDERING: {
923 /* Ignore the hint. */
924 SPIEnum dummy;
925 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
926 break;
927 }
928 case SP_PROP_ALIGNMENT_BASELINE:
929 g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
930 break;
931 case SP_PROP_BASELINE_SHIFT:
932 g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
933 break;
934 case SP_PROP_DOMINANT_BASELINE:
935 g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
936 break;
937 case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
938 g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
939 break;
940 case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
941 g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
942 break;
943 case SP_PROP_KERNING:
944 g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
945 break;
946 /* Misc */
947 case SP_PROP_CLIP:
948 g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
949 break;
950 case SP_PROP_COLOR:
951 if (!style->color.set) {
952 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
953 }
954 break;
955 case SP_PROP_CURSOR:
956 g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
957 break;
958 case SP_PROP_DISPLAY:
959 SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
960 break;
961 case SP_PROP_OVERFLOW:
962 /** \todo
963 * FIXME: not supported properly yet, we just read and write it,
964 * but act as if it is always "display".
965 */
966 SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
967 break;
968 case SP_PROP_VISIBILITY:
969 SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
970 break;
971 /* SVG */
972 /* Clip/Mask */
973 case SP_PROP_CLIP_PATH:
974 g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
975 break;
976 case SP_PROP_CLIP_RULE:
977 g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
978 break;
979 case SP_PROP_MASK:
980 g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
981 break;
982 case SP_PROP_OPACITY:
983 if (!style->opacity.set) {
984 sp_style_read_iscale24(&style->opacity, val);
985 }
986 break;
987 case SP_PROP_ENABLE_BACKGROUND:
988 SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
989 enum_enable_background, true);
990 break;
991 /* Filter */
992 case SP_PROP_FILTER:
993 if (!style->filter.set && !style->filter.inherit) {
994 sp_style_read_ifilter(val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
995 }
996 break;
997 case SP_PROP_FLOOD_COLOR:
998 g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
999 break;
1000 case SP_PROP_FLOOD_OPACITY:
1001 g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
1002 break;
1003 case SP_PROP_LIGHTING_COLOR:
1004 g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
1005 break;
1006 /* Gradient */
1007 case SP_PROP_STOP_COLOR:
1008 g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
1009 break;
1010 case SP_PROP_STOP_OPACITY:
1011 g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
1012 break;
1013 /* Interactivity */
1014 case SP_PROP_POINTER_EVENTS:
1015 g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
1016 break;
1017 /* Paint */
1018 case SP_PROP_COLOR_INTERPOLATION:
1019 g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
1020 break;
1021 case SP_PROP_COLOR_INTERPOLATION_FILTERS:
1022 g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
1023 break;
1024 case SP_PROP_COLOR_PROFILE:
1025 g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
1026 break;
1027 case SP_PROP_COLOR_RENDERING: {
1028 /* Ignore the hint. */
1029 SPIEnum dummy;
1030 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
1031 break;
1032 }
1033 case SP_PROP_FILL:
1034 if (!style->fill.set) {
1035 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1036 }
1037 break;
1038 case SP_PROP_FILL_OPACITY:
1039 if (!style->fill_opacity.set) {
1040 sp_style_read_iscale24(&style->fill_opacity, val);
1041 }
1042 break;
1043 case SP_PROP_FILL_RULE:
1044 if (!style->fill_rule.set) {
1045 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
1046 }
1047 break;
1048 case SP_PROP_IMAGE_RENDERING: {
1049 /* Ignore the hint. */
1050 SPIEnum dummy;
1051 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
1052 break;
1053 }
1054 case SP_PROP_MARKER:
1055 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1056 /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
1057 if (!style->marker[SP_MARKER_LOC].set) {
1058 g_free(style->marker[SP_MARKER_LOC].value);
1059 style->marker[SP_MARKER_LOC].value = g_strdup(val);
1060 style->marker[SP_MARKER_LOC].set = TRUE;
1061 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
1062 }
1063 break;
1065 case SP_PROP_MARKER_START:
1066 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1067 if (!style->marker[SP_MARKER_LOC_START].set) {
1068 g_free(style->marker[SP_MARKER_LOC_START].value);
1069 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
1070 style->marker[SP_MARKER_LOC_START].set = TRUE;
1071 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
1072 }
1073 break;
1074 case SP_PROP_MARKER_MID:
1075 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1076 if (!style->marker[SP_MARKER_LOC_MID].set) {
1077 g_free(style->marker[SP_MARKER_LOC_MID].value);
1078 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
1079 style->marker[SP_MARKER_LOC_MID].set = TRUE;
1080 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
1081 }
1082 break;
1083 case SP_PROP_MARKER_END:
1084 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
1085 if (!style->marker[SP_MARKER_LOC_END].set) {
1086 g_free(style->marker[SP_MARKER_LOC_END].value);
1087 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
1088 style->marker[SP_MARKER_LOC_END].set = TRUE;
1089 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
1090 }
1091 break;
1093 case SP_PROP_SHAPE_RENDERING: {
1094 /* Ignore the hint. */
1095 SPIEnum dummy;
1096 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
1097 break;
1098 }
1100 case SP_PROP_STROKE:
1101 if (!style->stroke.set) {
1102 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
1103 }
1104 break;
1105 case SP_PROP_STROKE_WIDTH:
1106 SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
1107 break;
1108 case SP_PROP_STROKE_DASHARRAY:
1109 if (!style->stroke_dasharray_set) {
1110 sp_style_read_dash(style, val);
1111 }
1112 break;
1113 case SP_PROP_STROKE_DASHOFFSET:
1114 if (!style->stroke_dashoffset_set) {
1115 /* fixme */
1116 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1117 style->stroke_dashoffset_set = TRUE;
1118 } else {
1119 style->stroke_dashoffset_set = FALSE;
1120 }
1121 }
1122 break;
1123 case SP_PROP_STROKE_LINECAP:
1124 if (!style->stroke_linecap.set) {
1125 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1126 }
1127 break;
1128 case SP_PROP_STROKE_LINEJOIN:
1129 if (!style->stroke_linejoin.set) {
1130 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1131 }
1132 break;
1133 case SP_PROP_STROKE_MITERLIMIT:
1134 if (!style->stroke_miterlimit.set) {
1135 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1136 }
1137 break;
1138 case SP_PROP_STROKE_OPACITY:
1139 if (!style->stroke_opacity.set) {
1140 sp_style_read_iscale24(&style->stroke_opacity, val);
1141 }
1142 break;
1144 default:
1145 g_warning("Invalid style property id: %d value: %s", id, val);
1146 break;
1147 }
1148 }
1150 static void
1151 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1152 {
1153 /** \todo Ensure that property is lcased, as per
1154 * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1155 * Should probably be done in libcroco.
1156 */
1157 unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1158 if (prop_idx != SP_ATTR_INVALID) {
1159 /** \todo
1160 * effic: Test whether the property is already set before trying to
1161 * convert to string. Alternatively, set from CRTerm directly rather
1162 * than converting to string.
1163 */
1164 guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1165 gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1166 sp_style_merge_property(style, prop_idx, str_value);
1167 g_free(str_value);
1168 }
1169 }
1171 static void
1172 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1173 {
1174 #if 0 /* forwards */
1175 for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1176 CRDeclaration *decl = NULL;
1177 cr_prop_list_get_decl(cur, &decl);
1178 sp_style_merge_style_from_decl(style, decl);
1179 }
1180 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1181 if (props) {
1182 sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1183 CRDeclaration *decl = NULL;
1184 cr_prop_list_get_decl(props, &decl);
1185 sp_style_merge_style_from_decl(style, decl);
1186 }
1187 #endif
1188 }
1190 /**
1191 * \pre decl_list != NULL
1192 */
1193 static void
1194 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1195 {
1196 // read the decls from end to start, using head recursion, so that latter declarations override
1197 // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
1198 // because sp_style_merge_style_from_decl only sets properties that are unset
1199 if (decl_list->next) {
1200 sp_style_merge_from_decl_list(style, decl_list->next);
1201 }
1202 sp_style_merge_style_from_decl(style, decl_list);
1203 }
1205 static CRSelEng *
1206 sp_repr_sel_eng()
1207 {
1208 CRSelEng *const ret = cr_sel_eng_new();
1209 cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1211 /** \todo
1212 * Check whether we need to register any pseudo-class handlers.
1213 * libcroco has its own default handlers for first-child and lang.
1214 *
1215 * We probably want handlers for link and arguably visited (though
1216 * inkscape can't visit links at the time of writing). hover etc.
1217 * more useful in inkview than the editor inkscape.
1218 *
1219 * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1220 * the following should be honoured, at least by inkview:
1221 * :hover, :active, :focus, :visited, :link.
1222 */
1224 g_assert(ret);
1225 return ret;
1226 }
1228 static void
1229 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1230 {
1231 static CRSelEng *sel_eng = NULL;
1232 if (!sel_eng) {
1233 sel_eng = sp_repr_sel_eng();
1234 }
1236 CRPropList *props = NULL;
1237 CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1238 object->document->style_cascade,
1239 object->repr,
1240 &props);
1241 g_return_if_fail(status == CR_OK);
1242 /// \todo Check what errors can occur, and handle them properly.
1243 if (props) {
1244 sp_style_merge_from_props(style, props);
1245 cr_prop_list_destroy(props);
1246 }
1247 }
1249 /**
1250 * Parses a style="..." string and merges it with an existing SPStyle.
1251 */
1252 void
1253 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1254 {
1255 /*
1256 * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1257 * ``When CSS styling is used, CSS inline style is specified by including
1258 * semicolon-separated property declarations of the form "name : value"
1259 * within the style attribute''.
1260 *
1261 * That's fairly ambiguous. Is a `value' allowed to contain semicolons?
1262 * Why does it say "including", what else is allowed in the style
1263 * attribute value?
1264 */
1266 CRDeclaration *const decl_list
1267 = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1268 if (decl_list) {
1269 sp_style_merge_from_decl_list(style, decl_list);
1270 cr_declaration_destroy(decl_list);
1271 }
1272 }
1274 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1275 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1277 static void
1278 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1279 {
1280 /* 'font-size' */
1281 if (!child.set || child.inherit) {
1282 /* Inherit the computed value. Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1283 child.computed = parent.computed;
1284 } else if (child.type == SP_FONT_SIZE_LITERAL) {
1285 /** \todo
1286 * fixme: SVG and CSS do not specify clearly, whether we should use
1287 * user or screen coordinates (Lauris)
1288 */
1289 if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1290 child.computed = font_size_table[child.value];
1291 } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1292 child.computed = parent.computed / 1.2;
1293 } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1294 child.computed = parent.computed * 1.2;
1295 } else {
1296 /* Illegal value */
1297 }
1298 } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1299 /* Unlike most other lengths, percentage for font size is relative to parent computed value
1300 * rather than viewport. */
1301 child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1302 }
1303 }
1305 /**
1306 * Sets computed values in \a style, which may involve inheriting from (or in some other way
1307 * calculating from) corresponding computed values of \a parent.
1308 *
1309 * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1310 * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1311 * inherit a value. http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1312 * about what the computed value is (not obvious for lengths).
1313 *
1314 * \pre \a parent's computed values are already up-to-date.
1315 */
1316 void
1317 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1318 {
1319 g_return_if_fail(style != NULL);
1321 /** \todo
1322 * fixme: Check for existing callers that might pass null parent.
1323 * This should probably be g_return_if_fail, or else we should make a
1324 * best attempt to set computed values correctly without having a parent
1325 * (i.e., by assuming parent has initial values).
1326 */
1327 if (!parent)
1328 return;
1330 /* CSS2 */
1331 /* Font */
1332 sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1334 /* 'font-style' */
1335 if (!style->font_style.set || style->font_style.inherit) {
1336 style->font_style.computed = parent->font_style.computed;
1337 }
1339 /* 'font-variant' */
1340 if (!style->font_variant.set || style->font_variant.inherit) {
1341 style->font_variant.computed = parent->font_variant.computed;
1342 }
1344 /* 'font-weight' */
1345 if (!style->font_weight.set || style->font_weight.inherit) {
1346 style->font_weight.computed = parent->font_weight.computed;
1347 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1348 /** \todo
1349 * fixme: This is unconditional, i.e., happens even if parent not
1350 * present.
1351 */
1352 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1353 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1354 style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1355 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1356 unsigned const parent_val = parent->font_weight.computed;
1357 g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1358 // strictly, 'bolder' and 'lighter' should go to the next weight
1359 // expressible in the current font family, but that's difficult to
1360 // find out, so jumping by 3 seems an appropriate approximation
1361 style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1362 ? (unsigned)SP_CSS_FONT_WEIGHT_100
1363 : parent_val - 3);
1364 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1365 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1366 unsigned const parent_val = parent->font_weight.computed;
1367 g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1368 style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1369 ? (unsigned)SP_CSS_FONT_WEIGHT_900
1370 : parent_val + 3);
1371 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1372 }
1374 /* 'font-stretch' */
1375 if (!style->font_stretch.set || style->font_stretch.inherit) {
1376 style->font_stretch.computed = parent->font_stretch.computed;
1377 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1378 unsigned const parent_val = parent->font_stretch.computed;
1379 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1380 ? parent_val
1381 : parent_val - 1);
1382 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1383 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1384 unsigned const parent_val = parent->font_stretch.computed;
1385 g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1386 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1387 ? parent_val
1388 : parent_val + 1);
1389 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1390 }
1392 /* text (css2) */
1393 if (!style->text_indent.set || style->text_indent.inherit) {
1394 style->text_indent.computed = parent->text_indent.computed;
1395 }
1397 if (!style->text_align.set || style->text_align.inherit) {
1398 style->text_align.computed = parent->text_align.computed;
1399 }
1401 if (!style->text_decoration.set || style->text_decoration.inherit) {
1402 style->text_decoration.underline = parent->text_decoration.underline;
1403 style->text_decoration.overline = parent->text_decoration.overline;
1404 style->text_decoration.line_through = parent->text_decoration.line_through;
1405 style->text_decoration.blink = parent->text_decoration.blink;
1406 }
1408 if (!style->line_height.set || style->line_height.inherit) {
1409 style->line_height.computed = parent->line_height.computed;
1410 style->line_height.normal = parent->line_height.normal;
1411 }
1413 if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1414 style->letter_spacing.computed = parent->letter_spacing.computed;
1415 style->letter_spacing.normal = parent->letter_spacing.normal;
1416 }
1418 if (!style->word_spacing.set || style->word_spacing.inherit) {
1419 style->word_spacing.computed = parent->word_spacing.computed;
1420 style->word_spacing.normal = parent->word_spacing.normal;
1421 }
1423 if (!style->text_transform.set || style->text_transform.inherit) {
1424 style->text_transform.computed = parent->text_transform.computed;
1425 }
1427 if (!style->direction.set || style->direction.inherit) {
1428 style->direction.computed = parent->direction.computed;
1429 }
1431 if (!style->block_progression.set || style->block_progression.inherit) {
1432 style->block_progression.computed = parent->block_progression.computed;
1433 }
1435 if (!style->writing_mode.set || style->writing_mode.inherit) {
1436 style->writing_mode.computed = parent->writing_mode.computed;
1437 }
1439 if (!style->text_anchor.set || style->text_anchor.inherit) {
1440 style->text_anchor.computed = parent->text_anchor.computed;
1441 }
1443 if (style->opacity.inherit) {
1444 style->opacity.value = parent->opacity.value;
1445 }
1447 /* Color */
1448 if (!style->color.set || style->color.inherit) {
1449 sp_style_merge_ipaint(style, &style->color, &parent->color);
1450 }
1452 /* Fill */
1453 if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1454 sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1455 }
1457 if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1458 style->fill_opacity.value = parent->fill_opacity.value;
1459 }
1461 if (!style->fill_rule.set || style->fill_rule.inherit) {
1462 style->fill_rule.computed = parent->fill_rule.computed;
1463 }
1465 /* Stroke */
1466 if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1467 sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1468 }
1470 if (!style->stroke_width.set || style->stroke_width.inherit) {
1471 style->stroke_width.computed = parent->stroke_width.computed;
1472 } else {
1473 /* Update computed value for any change in font inherited from parent. */
1474 double const em = style->font_size.computed;
1475 if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1476 style->stroke_width.computed = style->stroke_width.value * em;
1477 } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1478 double const ex = em * 0.5; // fixme: Get x height from libnrtype or pango.
1479 style->stroke_width.computed = style->stroke_width.value * ex;
1480 }
1481 }
1483 if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1484 style->stroke_linecap.computed = parent->stroke_linecap.computed;
1485 }
1487 if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1488 style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1489 }
1491 if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1492 style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1493 }
1495 if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1496 /** \todo
1497 * This code looks wrong. Why does the logic differ from the
1498 * above properties? Similarly dashoffset below.
1499 */
1500 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1501 if (style->stroke_dash.n_dash > 0) {
1502 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1503 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1504 }
1505 }
1507 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1508 style->stroke_dash.offset = parent->stroke_dash.offset;
1509 }
1511 if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1512 style->stroke_opacity.value = parent->stroke_opacity.value;
1513 }
1515 if (style->text && parent->text) {
1516 if (!style->text->font_family.set || style->text->font_family.inherit) {
1517 g_free(style->text->font_family.value);
1518 style->text->font_family.value = g_strdup(parent->text->font_family.value);
1519 }
1520 }
1522 if (style->text && parent->text) {
1523 if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1524 g_free(style->text->font_specification.value);
1525 style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1526 }
1527 }
1529 /* Markers - Free the old value and make copy of the new */
1530 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1531 if (!style->marker[i].set || style->marker[i].inherit) {
1532 g_free(style->marker[i].value);
1533 style->marker[i].value = g_strdup(parent->marker[i].value);
1534 }
1535 }
1537 /* Filter effects */
1538 if (style->filter.inherit) {
1539 sp_style_merge_ifilter(style, &parent->filter);
1540 }
1542 if(style->enable_background.inherit) {
1543 style->enable_background.value = parent->enable_background.value;
1544 }
1545 }
1547 template <typename T>
1548 static void
1549 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1550 {
1551 if ( ( !(child.set) || child.inherit )
1552 && parent.set && !(parent.inherit) )
1553 {
1554 child = parent;
1555 }
1556 }
1558 /**
1559 * Copy SPIString from parent to child.
1560 */
1561 static void
1562 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1563 {
1564 if ( ( !(child.set) || child.inherit )
1565 && parent.set && !(parent.inherit) )
1566 {
1567 g_free(child.value);
1568 child.value = g_strdup(parent.value);
1569 child.set = parent.set;
1570 child.inherit = parent.inherit;
1571 }
1572 }
1574 static void
1575 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1576 SPIPaint &child, SPIPaint const &parent)
1577 {
1578 /** \todo
1579 * I haven't given this much attention. See comments below about
1580 * currentColor, colorProfile, and relative URIs.
1581 */
1582 if (!child.set || child.inherit || child.currentcolor) {
1583 sp_style_merge_ipaint(style, &child, &parent);
1584 child.set = parent.set;
1585 child.inherit = parent.inherit;
1586 }
1587 }
1589 static void
1590 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1591 unsigned const max_computed_val,
1592 unsigned const smaller_val)
1593 {
1594 /* We assume that min computed val is 0, contiguous up to max_computed_val,
1595 then zero or more other absolute values, then smaller_val then larger_val. */
1596 unsigned const min_computed_val = 0;
1597 unsigned const larger_val = smaller_val + 1;
1598 g_return_if_fail(min_computed_val < max_computed_val);
1599 g_return_if_fail(max_computed_val < smaller_val);
1601 if (parent.set && !parent.inherit) {
1602 if (!child.set || child.inherit) {
1603 child.value = parent.value;
1604 child.set = parent.set; // i.e. true
1605 child.inherit = parent.inherit; // i.e. false
1606 } else if (child.value < smaller_val) {
1607 /* Child has absolute value: leave as is. */
1608 } else if ( ( child.value == smaller_val
1609 && parent.value == larger_val )
1610 || ( parent.value == smaller_val
1611 && child.value == larger_val ) )
1612 {
1613 child.set = false;
1614 /*
1615 * Note that this can result in a change in computed value in the
1616 * rare case that the parent's setting was a no-op (i.e. if the
1617 * parent's parent's computed value was already ultra-condensed or
1618 * ultra-expanded). However, I'd guess that the change is for the
1619 * better: I'd guess that if the properties were specified
1620 * relatively, then the intent is to counteract parent's effect.
1621 * I.e. I believe this is the best code even in that case.
1622 */
1623 } else if (child.value == parent.value) {
1624 /* Leave as is. */
1625 /** \todo
1626 * It's unclear what to do if style and parent specify the same
1627 * relative directive (narrower or wider). We can either convert
1628 * to absolute specification or coalesce to a single relative
1629 * request (of half the strength of the original pair).
1630 *
1631 * Converting to a single level of relative specification is a
1632 * better choice if the newly-unlinked clone is itself cloned to
1633 * other contexts (inheriting different font stretchiness): it
1634 * would preserve the effect that it will be narrower than
1635 * the inherited context in each case. The disadvantage is that
1636 * it will ~certainly affect the computed value of the
1637 * newly-unlinked clone.
1638 */
1639 } else {
1640 unsigned const parent_val = parent.computed;
1641 child.value = ( child.value == smaller_val
1642 ? ( parent_val == min_computed_val
1643 ? parent_val
1644 : parent_val - 1 )
1645 : ( parent_val == max_computed_val
1646 ? parent_val
1647 : parent_val + 1 ) );
1648 g_assert(child.value <= max_computed_val);
1649 child.inherit = false;
1650 g_assert(child.set);
1651 }
1652 }
1653 }
1655 template <typename LengthT>
1656 static void
1657 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1658 double const parent_child_em_ratio)
1659 {
1660 if ( ( !(child.set) || child.inherit )
1661 && parent.set && !(parent.inherit) )
1662 {
1663 child = parent;
1664 switch (parent.unit) {
1665 case SP_CSS_UNIT_EM:
1666 case SP_CSS_UNIT_EX:
1667 child.value *= parent_child_em_ratio;
1668 /** \todo
1669 * fixme: Have separate ex ratio parameter.
1670 * Get x height from libnrtype or pango.
1671 */
1672 if (!isFinite(child.value)) {
1673 child.value = child.computed;
1674 child.unit = SP_CSS_UNIT_NONE;
1675 }
1676 break;
1678 default:
1679 break;
1680 }
1681 }
1682 }
1684 static double
1685 get_relative_font_size_frac(SPIFontSize const &font_size)
1686 {
1687 switch (font_size.type) {
1688 case SP_FONT_SIZE_LITERAL: {
1689 switch (font_size.value) {
1690 case SP_CSS_FONT_SIZE_SMALLER:
1691 return 5.0 / 6.0;
1693 case SP_CSS_FONT_SIZE_LARGER:
1694 return 6.0 / 5.0;
1696 default:
1697 g_assert_not_reached();
1698 }
1699 }
1701 case SP_FONT_SIZE_PERCENTAGE:
1702 return SP_F8_16_TO_FLOAT(font_size.value);
1704 case SP_FONT_SIZE_LENGTH:
1705 g_assert_not_reached();
1706 }
1707 g_assert_not_reached();
1708 }
1710 /**
1711 * Combine \a style and \a parent style specifications into a single style specification that
1712 * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1713 *
1714 * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1715 * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1716 * trying to retain the same visual appearance once the parent is removed. Interesting cases are
1717 * when there is unusual interaction with the parent's value (opacity, display) or when the value
1718 * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1719 *
1720 * Doesn't update computed values of \a style. For correctness, you should subsequently call
1721 * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1722 * style was previously up-to-date wrt \a parent.
1723 *
1724 * \pre \a parent's computed values are already up-to-date.
1725 * (\a style's computed values needn't be up-to-date.)
1726 */
1727 void
1728 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1729 {
1730 /** \note
1731 * The general rule for each property is as follows:
1732 *
1733 * If style is set to an absolute value, then leave it as is.
1734 *
1735 * Otherwise (i.e. if style has a relative value):
1736 *
1737 * If parent is set to an absolute value, then set style to the computed value.
1738 *
1739 * Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1740 */
1742 /* We do font-size first, to ensure that em size is up-to-date. */
1743 /** \todo
1744 * fixme: We'll need to have more font-related things up the top once
1745 * we're getting x-height from pango or libnrtype.
1746 */
1748 /* Some things that allow relative specifications. */
1749 {
1750 /* font-size. Note that we update the computed font-size of style,
1751 to assist in em calculations later in this function. */
1752 if (parent->font_size.set && !parent->font_size.inherit) {
1753 if (!style->font_size.set || style->font_size.inherit) {
1754 /* font_size inherits the computed value, so we can use the parent value
1755 * verbatim. */
1756 style->font_size = parent->font_size;
1757 } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1758 /* Child already has absolute size (stored in computed value), so do nothing. */
1759 } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1760 && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1761 /* Child already has absolute size, but we ensure that the computed value
1762 is up-to-date. */
1763 unsigned const ix = style->font_size.value;
1764 g_assert(ix < G_N_ELEMENTS(font_size_table));
1765 style->font_size.computed = font_size_table[ix];
1766 } else {
1767 /* Child has relative size. */
1768 double const child_frac(get_relative_font_size_frac(style->font_size));
1769 style->font_size.set = true;
1770 style->font_size.inherit = false;
1771 style->font_size.computed = parent->font_size.computed * child_frac;
1773 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1774 && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1775 || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1776 {
1777 /* Absolute value. */
1778 style->font_size.type = SP_FONT_SIZE_LENGTH;
1779 /* .value is unused for SP_FONT_SIZE_LENGTH. */
1780 } else {
1781 /* Relative value. */
1782 double const parent_frac(get_relative_font_size_frac(parent->font_size));
1783 style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1784 style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1785 }
1786 }
1787 }
1789 /* 'font-stretch' */
1790 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1791 parent->font_stretch,
1792 SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1793 SP_CSS_FONT_STRETCH_NARROWER);
1795 /* font-weight */
1796 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1797 parent->font_weight,
1798 SP_CSS_FONT_WEIGHT_900,
1799 SP_CSS_FONT_WEIGHT_LIGHTER);
1800 }
1803 /* Enum values that don't have any relative settings (other than `inherit'). */
1804 {
1805 SPIEnum SPStyle::*const fields[] = {
1806 //nyi: SPStyle::clip_rule,
1807 //nyi: SPStyle::color_interpolation,
1808 //nyi: SPStyle::color_interpolation_filters,
1809 //nyi: SPStyle::color_rendering,
1810 &SPStyle::direction,
1811 &SPStyle::fill_rule,
1812 &SPStyle::font_style,
1813 &SPStyle::font_variant,
1814 //nyi: SPStyle::image_rendering,
1815 //nyi: SPStyle::pointer_events,
1816 //nyi: SPStyle::shape_rendering,
1817 &SPStyle::stroke_linecap,
1818 &SPStyle::stroke_linejoin,
1819 &SPStyle::text_anchor,
1820 //nyi: &SPStyle::text_rendering,
1821 &SPStyle::visibility,
1822 &SPStyle::writing_mode
1823 };
1825 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1826 SPIEnum SPStyle::*const fld = fields[i];
1827 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1828 }
1829 }
1831 /* A few other simple inheritance properties. */
1832 {
1833 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1834 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1835 sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1837 /** \todo
1838 * We currently treat text-decoration as if it were a simple inherited
1839 * property (fixme). This code may need changing once we do the
1840 * special fill/stroke inheritance mentioned by the spec.
1841 */
1842 sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1843 parent->text_decoration);
1845 //nyi: font-size-adjust, // <number> | none | inherit
1846 //nyi: glyph-orientation-horizontal,
1847 //nyi: glyph-orientation-vertical,
1848 }
1850 /* Properties that involve length but are easy in other respects. */
1851 {
1852 /* The difficulty with lengths is that font-relative units need adjusting if the font
1853 * varies between parent & child.
1854 *
1855 * Lengths specified in the existing child can stay as they are: its computed font
1856 * specification should stay unchanged, so em & ex lengths should continue to mean the same
1857 * size.
1858 *
1859 * Lengths specified in the dying parent in em or ex need to be scaled according to the
1860 * ratio of em or ex size between parent & child.
1861 */
1862 double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1864 SPILength SPStyle::*const lfields[] = {
1865 &SPStyle::stroke_width,
1866 &SPStyle::text_indent
1867 };
1868 for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1869 SPILength SPStyle::*fld = lfields[i];
1870 sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1871 parent->*fld,
1872 parent_child_em_ratio);
1873 }
1875 SPILengthOrNormal SPStyle::*const nfields[] = {
1876 &SPStyle::letter_spacing,
1877 &SPStyle::line_height,
1878 &SPStyle::word_spacing
1879 };
1880 for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1881 SPILengthOrNormal SPStyle::*fld = nfields[i];
1882 sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1883 parent->*fld,
1884 parent_child_em_ratio);
1885 }
1887 //nyi: &SPStyle::kerning: length or `auto'
1889 /* fixme: Move stroke-dash and stroke-dash-offset here once they
1890 can accept units. */
1891 }
1893 /* Properties that involve a URI but are easy in other respects. */
1894 {
1895 /** \todo
1896 * Could cause problems if original object was in another document
1897 * and it used a relative URL. (At the time of writing, we don't
1898 * allow hrefs to other documents, so this isn't a problem yet.)
1899 * Paint properties also allow URIs.
1900 */
1901 //nyi: cursor, // may involve change in effect, but we can't do much better
1902 //nyi: color-profile,
1904 // Markers (marker-start, marker-mid, marker-end).
1905 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1906 sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1907 }
1908 }
1910 /* CSS2 */
1911 /* Font */
1913 if (style->text && parent->text) {
1914 sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
1915 parent->text->font_specification);
1917 sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1918 parent->text->font_family);
1919 }
1922 /* Properties that don't inherit by default. Most of these need special handling. */
1923 {
1924 /*
1925 * opacity's effect is cumulative; we set the new value to the combined effect. The
1926 * default value for opacity is 1.0, not inherit. (Note that stroke-opacity and
1927 * fill-opacity are quite different from opacity, and don't need any special handling.)
1928 *
1929 * Cases:
1930 * - parent & child were each previously unset, in which case the effective
1931 * opacity value is 1.0, and style should remain unset.
1932 * - parent was previously unset (so computed opacity value of 1.0)
1933 * and child was set to inherit. The merged child should
1934 * get a value of 1.0, and shouldn't inherit (lest the new parent
1935 * has a different opacity value). Given that opacity's default
1936 * value is 1.0 (rather than inherit), we might as well have the
1937 * merged child's opacity be unset.
1938 * - parent was previously unset (so opacity 1.0), and child was set to a number.
1939 * The merged child should retain its existing settings (though it doesn't matter
1940 * if we make it unset if that number was 1.0).
1941 * - parent was inherit and child was unset. Merged child should be set to inherit.
1942 * - parent was inherit and child was inherit. (We can't in general reproduce this
1943 * effect (short of introducing a new group), but setting opacity to inherit is rare.)
1944 * If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1945 * child's value should be set to the product of the two, i.e. the square of the
1946 * inherited value, and should not be marked as inherit. (This decision assumes that it
1947 * is more important to retain the effective opacity than to retain the inheriting
1948 * effect, and assumes that the inheriting effect either isn't important enough to create
1949 * a group or isn't common enough to bother maintaining the code to create a group.) If
1950 * the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1951 * closer to maintaining the effect.
1952 * - parent was inherit and child was set to a numerical value. If the child's value
1953 * was 1.0, then the merged child should have the same settings as the parent.
1954 * If the child's value was 0, then the merged child should also be set to 0.
1955 * If the child's value was anything else, then we do the same as for the inherit/inherit
1956 * case above: have the merged child set to the product of the two opacities and not
1957 * marked as inherit, for the same reasons as for that case.
1958 * - parent was set to a value, and child was unset. The merged child should have
1959 * parent's settings.
1960 * - parent was set to a value, and child was inherit. The merged child should
1961 * be set to the product, i.e. the square of the parent's value.
1962 * - parent & child are each set to a value. The merged child should be set to the
1963 * product.
1964 */
1965 if ( !style->opacity.set
1966 || ( !style->opacity.inherit
1967 && style->opacity.value == SP_SCALE24_MAX ) )
1968 {
1969 style->opacity = parent->opacity;
1970 } else {
1971 /* Ensure that style's computed value is up-to-date. */
1972 if (style->opacity.inherit) {
1973 style->opacity.value = parent->opacity.value;
1974 }
1976 /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1977 style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1978 parent->opacity.value);
1980 style->opacity.inherit = (parent->opacity.inherit
1981 && style->opacity.inherit
1982 && (parent->opacity.value == 0 ||
1983 parent->opacity.value == SP_SCALE24_MAX));
1984 style->opacity.set = ( style->opacity.inherit
1985 || style->opacity.value < SP_SCALE24_MAX );
1986 }
1988 /* display is in principle similar to opacity, but implementation is easier. */
1989 if ( parent->display.set && !parent->display.inherit
1990 && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1991 style->display.value = SP_CSS_DISPLAY_NONE;
1992 style->display.set = true;
1993 style->display.inherit = false;
1994 } else if (style->display.inherit) {
1995 style->display.value = parent->display.value;
1996 style->display.set = parent->display.set;
1997 style->display.inherit = parent->display.inherit;
1998 } else {
1999 /* Leave as is. (display doesn't inherit by default.) */
2000 }
2002 /* enable-background - this is rather complicated, because
2003 * it is valid only when applied to container elements.
2004 * Let's check a simple case anyhow. */
2005 if (parent->enable_background.set
2006 && !parent->enable_background.inherit
2007 && style->enable_background.inherit)
2008 {
2009 style->enable_background.set = true;
2010 style->enable_background.inherit = false;
2011 style->enable_background.value = parent->enable_background.value;
2012 }
2014 if (!style->filter.set || style->filter.inherit)
2015 {
2016 // FIXME:
2017 // instead of just copying over, we need to _really merge_ the two filters by combining their
2018 // filter primitives
2019 sp_style_merge_ifilter(style, &parent->filter);
2020 }
2022 /** \todo
2023 * fixme: Check that we correctly handle all properties that don't
2024 * inherit by default (as shown in
2025 * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2026 */
2027 }
2029 /* SPIPaint properties (including color). */
2030 {
2031 /** \todo
2032 * Think about the issues involved if specified as currentColor or
2033 * if specified relative to colorProfile, and if the currentColor or
2034 * colorProfile differs between parent \& child. See also comments
2035 * elsewhere in this function about URIs.
2036 */
2037 SPIPaint SPStyle::*const fields[] = {
2038 &SPStyle::color,
2039 &SPStyle::fill,
2040 &SPStyle::stroke
2041 };
2042 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2043 SPIPaint SPStyle::*const fld = fields[i];
2044 sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2045 }
2046 }
2048 /* Things from SVG 1.2 or CSS3. */
2049 {
2050 /* Note: If we ever support setting string values for text-align then we'd need strdup
2051 * handling here. */
2052 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2054 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2055 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2056 }
2058 /* Note: this will need length handling once dasharray supports units. */
2059 if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2060 && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2061 {
2062 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2063 if (style->stroke_dash.n_dash > 0) {
2064 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2065 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2066 }
2067 style->stroke_dasharray_set = parent->stroke_dasharray_set;
2068 style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2069 }
2071 /* Note: this will need length handling once dasharray_offset supports units. */
2072 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
2073 style->stroke_dash.offset = parent->stroke_dash.offset;
2074 style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2075 /* fixme: we don't currently allow stroke-dashoffset to be `inherit'. TODO: Try to
2076 * represent it as a normal SPILength; though will need to do something about existing
2077 * users of stroke_dash.offset and stroke_dashoffset_set. */
2078 }
2079 }
2082 static void
2083 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2084 {
2085 // it may be that this style's SPIPaint has not yet created its URIReference;
2086 // now that we have a document, we can create it here
2087 if (!paint->value.href && document) {
2088 paint->value.href = new SPPaintServerReference(document);
2089 paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
2090 }
2092 if (paint->value.href && paint->value.href->getObject())
2093 paint->value.href->detach();
2095 if (paint->value.href) {
2096 try {
2097 paint->value.href->attach(*uri);
2098 } catch (Inkscape::BadURIException &e) {
2099 g_warning("%s", e.what());
2100 paint->value.href->detach();
2101 }
2102 }
2103 }
2105 static void
2106 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2107 {
2108 const Inkscape::URI IURI(uri);
2109 sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2110 }
2112 void
2113 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2114 {
2115 sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2116 }
2118 /**
2119 *
2120 */
2121 static void
2122 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2123 {
2124 if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2125 paint->clear();
2126 paint->currentcolor = TRUE;
2127 paint->setColor(style->color.value.color);
2128 return;
2129 }
2131 paint->clear();
2132 if ( parent->isPaintserver() ) {
2133 if (parent->value.href) {
2134 sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2135 } else {
2136 g_warning("Expected paint server not found.");
2137 }
2138 } else if ( parent->isColor() ) {
2139 paint->setColor( parent->value.color );
2140 } else if ( parent->isNone() ) {
2141 //
2142 } else {
2143 g_assert_not_reached();
2144 }
2145 }
2148 /**
2149 * Merge filter style from parent.
2150 * Filter effects do not inherit by default
2151 */
2152 static void
2153 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2154 {
2155 sp_style_filter_clear(style);
2156 style->filter.set = parent->set;
2157 style->filter.inherit = parent->inherit;
2159 if (style->filter.href && style->filter.href->getObject())
2160 style->filter.href->detach();
2162 if (style->filter.href && parent->href) {
2163 try {
2164 style->filter.href->attach(*parent->href->getURI());
2165 } catch (Inkscape::BadURIException &e) {
2166 g_warning("%s", e.what());
2167 style->filter.href->detach();
2168 }
2169 }
2170 }
2172 /**
2173 * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2174 * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2175 * complete cascaded style to style_clipboard. When you need a CSS string
2176 * for an object in the document tree, you normally call
2177 * sp_style_write_difference instead to take into account the object's parent.
2178 *
2179 * \pre style != NULL.
2180 * \pre flags in {IFSET, ALWAYS}.
2181 * \post ret != NULL.
2182 */
2183 gchar *
2184 sp_style_write_string(SPStyle const *const style, guint const flags)
2185 {
2186 /** \todo
2187 * Merge with write_difference, much duplicate code!
2188 */
2189 g_return_val_if_fail(style != NULL, NULL);
2190 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2191 (flags == SP_STYLE_FLAG_ALWAYS) ),
2192 NULL);
2194 gchar c[BMAX];
2195 gchar *p = c;
2196 *p = '\0';
2198 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2199 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2200 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2201 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2202 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2204 /* Text */
2205 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2206 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2207 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2208 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2209 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2210 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2211 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2212 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2213 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2214 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2216 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2218 /// \todo fixme: Per type methods need default flag too (lauris)
2219 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2220 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2221 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2222 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2223 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2224 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2225 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2226 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2227 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2229 if (style->marker[SP_MARKER_LOC].set) {
2230 p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2231 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2232 p += g_snprintf(p, c + BMAX - p, "marker:none;");
2233 }
2234 if (style->marker[SP_MARKER_LOC_START].set) {
2235 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2236 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2237 p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2238 }
2239 if (style->marker[SP_MARKER_LOC_MID].set) {
2240 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2241 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2242 p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2243 }
2244 if (style->marker[SP_MARKER_LOC_END].set) {
2245 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2246 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2247 p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2248 }
2250 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2252 /** \todo fixme: */
2253 if ((flags == SP_STYLE_FLAG_ALWAYS)
2254 || style->stroke_dasharray_set)
2255 {
2256 if (style->stroke_dasharray_inherit) {
2257 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2258 } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2259 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2260 gint i;
2261 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2262 Inkscape::CSSOStringStream os;
2263 if (i) {
2264 os << ", ";
2265 }
2266 os << style->stroke_dash.dash[i];
2267 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2268 }
2269 if (p < c + BMAX) {
2270 *p++ = ';';
2271 }
2272 } else {
2273 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2274 }
2275 }
2277 /** \todo fixme: */
2278 if (style->stroke_dashoffset_set) {
2279 Inkscape::CSSOStringStream os;
2280 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2281 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2282 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2283 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2284 }
2286 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2288 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2289 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2290 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2292 /* filter: */
2293 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2295 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2297 /* fixme: */
2298 p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2300 /* Get rid of trailing `;'. */
2301 if (p != c) {
2302 --p;
2303 if (*p == ';') {
2304 *p = '\0';
2305 }
2306 }
2308 return g_strdup(c);
2309 }
2312 #define STYLE_BUF_MAX
2315 /**
2316 * Dumps style to CSS string, see sp_style_write_string()
2317 *
2318 * \pre from != NULL.
2319 * \pre to != NULL.
2320 * \post ret != NULL.
2321 */
2322 gchar *
2323 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2324 {
2325 g_return_val_if_fail(from != NULL, NULL);
2326 g_return_val_if_fail(to != NULL, NULL);
2328 gchar c[BMAX], *p = c;
2329 *p = '\0';
2331 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2332 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2333 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2334 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2335 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2337 /* Text */
2338 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2339 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2340 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2341 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2342 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2343 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2344 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2345 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2346 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2347 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2349 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2351 /// \todo fixme: Per type methods need default flag too
2352 if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2353 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2354 }
2355 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2356 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2357 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2358 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2359 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2360 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2361 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2362 &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2363 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2364 &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2365 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2366 &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2367 /** \todo fixme: */
2368 if (from->stroke_dasharray_set) {
2369 if (from->stroke_dasharray_inherit) {
2370 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2371 } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2372 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2373 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2374 Inkscape::CSSOStringStream os;
2375 if (i) {
2376 os << ", ";
2377 }
2378 os << from->stroke_dash.dash[i];
2379 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2380 }
2381 p += g_snprintf(p, c + BMAX - p, ";");
2382 }
2383 }
2384 /* fixme: */
2385 if (from->stroke_dashoffset_set) {
2386 Inkscape::CSSOStringStream os;
2387 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2388 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2389 }
2390 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2392 /* markers */
2393 if (from->marker[SP_MARKER_LOC].value != NULL) {
2394 p += g_snprintf(p, c + BMAX - p, "marker:%s;", from->marker[SP_MARKER_LOC].value);
2395 }
2396 if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2397 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2398 }
2399 if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2400 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", from->marker[SP_MARKER_LOC_MID].value);
2401 }
2402 if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2403 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", from->marker[SP_MARKER_LOC_END].value);
2404 }
2406 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2407 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2408 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2410 /* filter: */
2411 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2413 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2415 p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2417 /** \todo
2418 * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2419 * flag is mainly only for attributes that don't handle explicit unset well.
2420 * We may need to revisit the behaviour of this routine.
2421 */
2423 /* Get rid of trailing `;'. */
2424 if (p != c) {
2425 --p;
2426 if (*p == ';') {
2427 *p = '\0';
2428 }
2429 }
2431 return g_strdup(c);
2432 }
2436 /**
2437 * Reset all style properties.
2438 */
2439 static void
2440 sp_style_clear(SPStyle *style)
2441 {
2442 g_return_if_fail(style != NULL);
2444 style->fill.clear();
2445 style->stroke.clear();
2446 sp_style_filter_clear(style);
2448 if (style->fill.value.href)
2449 delete style->fill.value.href;
2450 if (style->stroke.value.href)
2451 delete style->stroke.value.href;
2452 if (style->filter.href)
2453 delete style->filter.href;
2455 if (style->stroke_dash.dash) {
2456 g_free(style->stroke_dash.dash);
2457 }
2459 /** \todo fixme: Do that text manipulation via parents */
2460 SPObject *object = style->object;
2461 SPDocument *document = style->document;
2462 gint const refcount = style->refcount;
2463 SPTextStyle *text = style->text;
2464 unsigned const text_private = style->text_private;
2466 memset(style, 0, sizeof(SPStyle));
2468 style->refcount = refcount;
2469 style->object = object;
2470 style->document = document;
2472 if (document) {
2473 style->filter.href = new SPFilterReference(document);
2474 style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2476 style->fill.value.href = new SPPaintServerReference(document);
2477 style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2479 style->stroke.value.href = new SPPaintServerReference(document);
2480 style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2481 }
2483 style->text = text;
2484 style->text_private = text_private;
2486 style->text->font_specification.set = FALSE;
2487 style->text->font.set = FALSE;
2488 style->text->font_family.set = FALSE;
2490 style->font_size.set = FALSE;
2491 style->font_size.type = SP_FONT_SIZE_LITERAL;
2492 style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2493 style->font_size.computed = 12.0;
2494 style->font_style.set = FALSE;
2495 style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2496 style->font_variant.set = FALSE;
2497 style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2498 style->font_weight.set = FALSE;
2499 style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2500 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2501 style->font_stretch.set = FALSE;
2502 style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2504 /* text */
2505 style->text_indent.set = FALSE;
2506 style->text_indent.unit = SP_CSS_UNIT_NONE;
2507 style->text_indent.computed = 0.0;
2509 style->text_align.set = FALSE;
2510 style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2512 style->text_decoration.set = FALSE;
2513 style->text_decoration.underline = FALSE;
2514 style->text_decoration.overline = FALSE;
2515 style->text_decoration.line_through = FALSE;
2516 style->text_decoration.blink = FALSE;
2518 style->line_height.set = FALSE;
2519 style->line_height.unit = SP_CSS_UNIT_PERCENT;
2520 style->line_height.normal = TRUE;
2521 style->line_height.value = style->line_height.computed = 1.0;
2523 style->letter_spacing.set = FALSE;
2524 style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2525 style->letter_spacing.normal = TRUE;
2526 style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2528 style->word_spacing.set = FALSE;
2529 style->word_spacing.unit = SP_CSS_UNIT_NONE;
2530 style->word_spacing.normal = TRUE;
2531 style->word_spacing.value = style->word_spacing.computed = 0.0;
2533 style->text_transform.set = FALSE;
2534 style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2536 style->direction.set = FALSE;
2537 style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2539 style->block_progression.set = FALSE;
2540 style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2542 style->writing_mode.set = FALSE;
2543 style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2546 style->text_anchor.set = FALSE;
2547 style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2550 style->opacity.value = SP_SCALE24_MAX;
2551 style->visibility.set = FALSE;
2552 style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2553 style->display.set = FALSE;
2554 style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2555 style->overflow.set = FALSE;
2556 style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2558 style->color.clear();
2559 style->color.setColor(0.0, 0.0, 0.0);
2561 style->fill.clear();
2562 style->fill.setColor(0.0, 0.0, 0.0);
2563 style->fill_opacity.value = SP_SCALE24_MAX;
2564 style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2566 style->stroke.clear();
2567 style->stroke_opacity.value = SP_SCALE24_MAX;
2569 style->stroke_width.set = FALSE;
2570 style->stroke_width.unit = SP_CSS_UNIT_NONE;
2571 style->stroke_width.computed = 1.0;
2573 style->stroke_linecap.set = FALSE;
2574 style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2575 style->stroke_linejoin.set = FALSE;
2576 style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2578 style->stroke_miterlimit.set = FALSE;
2579 style->stroke_miterlimit.value = 4.0;
2581 style->stroke_dash.n_dash = 0;
2582 style->stroke_dash.dash = NULL;
2583 style->stroke_dash.offset = 0.0;
2585 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2586 g_free(style->marker[i].value);
2587 style->marker[i].set = FALSE;
2588 }
2590 style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2591 style->enable_background.set = false;
2592 style->enable_background.inherit = false;
2593 }
2597 /**
2598 *
2599 */
2600 static void
2601 sp_style_read_dash(SPStyle *style, gchar const *str)
2602 {
2603 /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2604 style->stroke_dasharray_set = TRUE;
2606 if (strcmp(str, "inherit") == 0) {
2607 style->stroke_dasharray_inherit = true;
2608 return;
2609 }
2610 style->stroke_dasharray_inherit = false;
2612 NRVpathDash &dash = style->stroke_dash;
2613 g_free(dash.dash);
2614 dash.dash = NULL;
2616 if (strcmp(str, "none") == 0) {
2617 dash.n_dash = 0;
2618 return;
2619 }
2621 gint n_dash = 0;
2622 gdouble d[64];
2623 gchar *e = NULL;
2625 bool LineSolid=true;
2626 while (e != str && n_dash < 64) {
2627 /* TODO: Should allow <length> rather than just a unitless (px) number. */
2628 d[n_dash] = g_ascii_strtod(str, (char **) &e);
2629 if (d[n_dash] > 0.00000001)
2630 LineSolid = false;
2631 if (e != str) {
2632 n_dash += 1;
2633 str = e;
2634 }
2635 while (str && *str && !isalnum(*str)) str += 1;
2636 }
2638 if (LineSolid) {
2639 dash.n_dash = 0;
2640 return;
2641 }
2643 if (n_dash > 0) {
2644 dash.dash = g_new(gdouble, n_dash);
2645 memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2646 dash.n_dash = n_dash;
2647 }
2648 }
2651 /*#########################
2652 ## SPTextStyle operations
2653 #########################*/
2656 /**
2657 * Return new SPTextStyle object with default settings.
2658 */
2659 static SPTextStyle *
2660 sp_text_style_new()
2661 {
2662 SPTextStyle *ts = g_new0(SPTextStyle, 1);
2663 ts->refcount = 1;
2664 sp_text_style_clear(ts);
2666 ts->font_specification.value = g_strdup("Bitstream Vera Sans");
2667 ts->font.value = g_strdup("Bitstream Vera Sans");
2668 ts->font_family.value = g_strdup("Bitstream Vera Sans");
2670 return ts;
2671 }
2674 /**
2675 * Clear text style settings.
2676 */
2677 static void
2678 sp_text_style_clear(SPTextStyle *ts)
2679 {
2680 ts->font_specification.set = FALSE;
2681 ts->font.set = FALSE;
2682 ts->font_family.set = FALSE;
2683 }
2687 /**
2688 * Reduce refcount of text style and possibly free it.
2689 */
2690 static SPTextStyle *
2691 sp_text_style_unref(SPTextStyle *st)
2692 {
2693 st->refcount -= 1;
2695 if (st->refcount < 1) {
2696 g_free(st->font_specification.value);
2697 g_free(st->font.value);
2698 g_free(st->font_family.value);
2699 g_free(st);
2700 }
2702 return NULL;
2703 }
2707 /**
2708 * Return duplicate of text style.
2709 */
2710 static SPTextStyle *
2711 sp_text_style_duplicate_unset(SPTextStyle *st)
2712 {
2713 SPTextStyle *nt = g_new0(SPTextStyle, 1);
2714 nt->refcount = 1;
2716 nt->font_specification.value = g_strdup(st->font_specification.value);
2717 nt->font.value = g_strdup(st->font.value);
2718 nt->font_family.value = g_strdup(st->font_family.value);
2720 return nt;
2721 }
2725 /**
2726 * Write SPTextStyle object into string.
2727 */
2728 static guint
2729 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2730 {
2731 gint d = 0;
2733 // We do not do diffing for text style
2734 if (flags == SP_STYLE_FLAG_IFDIFF)
2735 flags = SP_STYLE_FLAG_IFSET;
2737 d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2738 d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2739 return d;
2740 }
2744 /* The following sp_tyle_read_* functions ignore invalid values, as per
2745 * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2746 *
2747 * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2748 * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2749 * pairs, in which case SVG's error-handling rules
2750 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2751 */
2754 /**
2755 * Set SPIFloat object from string.
2756 */
2757 static void
2758 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2759 {
2760 if (!strcmp(str, "inherit")) {
2761 val->set = TRUE;
2762 val->inherit = TRUE;
2763 } else {
2764 gfloat value;
2765 if (sp_svg_number_read_f(str, &value)) {
2766 val->set = TRUE;
2767 val->inherit = FALSE;
2768 val->value = value;
2769 }
2770 }
2771 }
2775 /**
2776 * Set SPIScale24 object from string.
2777 */
2778 static void
2779 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2780 {
2781 if (!strcmp(str, "inherit")) {
2782 val->set = TRUE;
2783 val->inherit = TRUE;
2784 } else {
2785 gfloat value;
2786 if (sp_svg_number_read_f(str, &value)) {
2787 val->set = TRUE;
2788 val->inherit = FALSE;
2789 value = CLAMP(value, 0.0, 1.0);
2790 val->value = SP_SCALE24_FROM_FLOAT(value);
2791 }
2792 }
2793 }
2795 /**
2796 * Reads a style value and performs lookup based on the given style value enumerations.
2797 */
2798 static void
2799 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2800 bool const can_explicitly_inherit)
2801 {
2802 if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2803 val->set = TRUE;
2804 val->inherit = TRUE;
2805 } else {
2806 for (unsigned i = 0; dict[i].key; i++) {
2807 if (!strcmp(str, dict[i].key)) {
2808 val->set = TRUE;
2809 val->inherit = FALSE;
2810 val->value = dict[i].value;
2811 /* Save copying for values not needing it */
2812 val->computed = val->value;
2813 break;
2814 }
2815 }
2816 }
2817 }
2821 /**
2822 * Set SPIString object from string.
2823 */
2824 static void
2825 sp_style_read_istring(SPIString *val, gchar const *str)
2826 {
2827 g_free(val->value);
2829 if (!strcmp(str, "inherit")) {
2830 val->set = TRUE;
2831 val->inherit = TRUE;
2832 val->value = NULL;
2833 } else {
2834 val->set = TRUE;
2835 val->inherit = FALSE;
2836 val->value = g_strdup(str);
2837 }
2838 }
2842 /**
2843 * Set SPILength object from string.
2844 */
2845 static void
2846 sp_style_read_ilength(SPILength *val, gchar const *str)
2847 {
2848 if (!strcmp(str, "inherit")) {
2849 val->set = TRUE;
2850 val->inherit = TRUE;
2851 } else {
2852 gdouble value;
2853 gchar *e;
2854 /** \todo fixme: Move this to standard place (Lauris) */
2855 value = g_ascii_strtod(str, &e);
2856 if ((gchar const *) e != str) {
2857 /** \todo
2858 * Allow the number of px per inch to vary (document preferences,
2859 * X server or whatever). E.g. don't fill in computed here, do
2860 * it at the same time as percentage units are done.
2861 */
2862 if (!*e) {
2863 /* Userspace */
2864 val->unit = SP_CSS_UNIT_NONE;
2865 val->computed = value;
2866 } else if (!strcmp(e, "px")) {
2867 /* Userspace */
2868 val->unit = SP_CSS_UNIT_PX;
2869 val->computed = value;
2870 } else if (!strcmp(e, "pt")) {
2871 /* Userspace / DEVICESCALE */
2872 val->unit = SP_CSS_UNIT_PT;
2873 val->computed = value * PX_PER_PT;
2874 } else if (!strcmp(e, "pc")) {
2875 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2876 val->unit = SP_CSS_UNIT_PC;
2877 val->computed = value * PX_PER_PT * 12;
2878 } else if (!strcmp(e, "mm")) {
2879 val->unit = SP_CSS_UNIT_MM;
2880 val->computed = value * PX_PER_MM;
2881 } else if (!strcmp(e, "cm")) {
2882 val->unit = SP_CSS_UNIT_CM;
2883 val->computed = value * PX_PER_CM;
2884 } else if (!strcmp(e, "in")) {
2885 val->unit = SP_CSS_UNIT_IN;
2886 val->computed = value * PX_PER_IN;
2887 } else if (!strcmp(e, "em")) {
2888 /* EM square */
2889 val->unit = SP_CSS_UNIT_EM;
2890 val->value = value;
2891 } else if (!strcmp(e, "ex")) {
2892 /* ex square */
2893 val->unit = SP_CSS_UNIT_EX;
2894 val->value = value;
2895 } else if (!strcmp(e, "%")) {
2896 /* Percentage */
2897 val->unit = SP_CSS_UNIT_PERCENT;
2898 val->value = value * 0.01;
2899 } else {
2900 /* Invalid */
2901 return;
2902 }
2903 val->set = TRUE;
2904 val->inherit = FALSE;
2905 }
2906 }
2907 }
2909 /**
2910 * Set SPILengthOrNormal object from string.
2911 */
2912 static void
2913 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2914 {
2915 if (!strcmp(str, "normal")) {
2916 val->set = TRUE;
2917 val->inherit = FALSE;
2918 val->normal = TRUE;
2919 val->unit = SP_CSS_UNIT_NONE;
2920 val->value = val->computed = 0.0;
2921 } else {
2922 SPILength length;
2923 sp_style_read_ilength(&length, str);
2924 val->set = length.set;
2925 val->inherit = length.inherit;
2926 val->normal = FALSE;
2927 val->unit = length.unit;
2928 val->value = length.value;
2929 val->computed = length.computed;
2930 }
2931 }
2933 /**
2934 * Set SPITextDecoration object from string.
2935 */
2936 static void
2937 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2938 {
2939 if (!strcmp(str, "inherit")) {
2940 val->set = TRUE;
2941 val->inherit = TRUE;
2942 } else if (!strcmp(str, "none")) {
2943 val->set = TRUE;
2944 val->inherit = FALSE;
2945 val->underline = FALSE;
2946 val->overline = FALSE;
2947 val->line_through = FALSE;
2948 val->blink = FALSE;
2949 } else {
2950 bool found_underline = false;
2951 bool found_overline = false;
2952 bool found_line_through = false;
2953 bool found_blink = false;
2954 for ( ; *str ; str++ ) {
2955 if (*str == ' ') continue;
2956 if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2957 found_underline = true;
2958 str += 9;
2959 } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2960 found_overline = true;
2961 str += 8;
2962 } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2963 found_line_through = true;
2964 str += 12;
2965 } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2966 found_blink = true;
2967 str += 5;
2968 } else {
2969 return; // invalid value
2970 }
2971 }
2972 if (!(found_underline || found_overline || found_line_through || found_blink)) {
2973 return; // invalid value: empty
2974 }
2975 val->set = TRUE;
2976 val->inherit = FALSE;
2977 val->underline = found_underline;
2978 val->overline = found_overline;
2979 val->line_through = found_line_through;
2980 val->blink = found_blink;
2981 }
2982 }
2984 /**
2985 * Set SPIPaint object from string containing an integer value.
2986 * \param document Ignored
2987 */
2988 static void
2989 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2990 {
2991 (void)style; // TODO
2992 (void)document; // TODO
2993 paint->currentcolor = FALSE; /* currentColor not a valid <color>. */
2994 if (!strcmp(str, "inherit")) {
2995 paint->set = TRUE;
2996 paint->inherit = TRUE;
2997 } else {
2998 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2999 if (rgb0 != 0xff) {
3000 paint->setColor(rgb0);
3001 paint->inherit = FALSE;
3002 }
3003 }
3004 }
3007 /**
3008 * Set SPIPaint object from string.
3009 *
3010 * \pre paint == \&style.fill || paint == \&style.stroke.
3011 */
3012 static void
3013 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3014 {
3015 while (g_ascii_isspace(*str)) {
3016 ++str;
3017 }
3019 paint->clear();
3021 if (streq(str, "inherit")) {
3022 paint->set = TRUE;
3023 paint->inherit = TRUE;
3024 } else {
3025 if ( strneq(str, "url", 3) ) {
3026 gchar *uri = extract_uri( str, &str );
3027 while ( g_ascii_isspace(*str) ) {
3028 ++str;
3029 }
3030 // TODO check on and comment the comparrison "paint != &style->color".
3031 if ( uri && *uri && (paint != &style->color) ) {
3032 paint->set = TRUE;
3034 // it may be that this style's SPIPaint has not yet created its URIReference;
3035 // now that we have a document, we can create it here
3036 if (!paint->value.href && document) {
3037 paint->value.href = new SPPaintServerReference(document);
3038 paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
3039 }
3041 // TODO check what this does in light of move away from union
3042 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3043 }
3044 g_free( uri );
3045 }
3047 if (streq(str, "currentColor") && paint != &style->color) {
3048 paint->set = TRUE;
3049 paint->currentcolor = TRUE;
3050 } else if (streq(str, "none") && paint != &style->color) {
3051 paint->set = TRUE;
3052 paint->noneSet = TRUE;
3053 } else {
3054 guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3055 if (rgb0 != 0xff) {
3056 paint->setColor( rgb0 );
3057 paint->set = TRUE;
3059 while (g_ascii_isspace(*str)) {
3060 ++str;
3061 }
3062 if (strneq(str, "icc-color(", 10)) {
3063 SVGICCColor* tmp = new SVGICCColor();
3064 if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3065 delete tmp;
3066 tmp = 0;
3067 }
3068 paint->value.color.icc = tmp;
3069 }
3070 }
3071 }
3072 }
3073 }
3077 /**
3078 * Set SPIFontSize object from string.
3079 */
3080 static void
3081 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3082 {
3083 if (!strcmp(str, "inherit")) {
3084 val->set = TRUE;
3085 val->inherit = TRUE;
3086 } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3087 for (unsigned i = 0; enum_font_size[i].key; i++) {
3088 if (!strcmp(str, enum_font_size[i].key)) {
3089 val->set = TRUE;
3090 val->inherit = FALSE;
3091 val->type = SP_FONT_SIZE_LITERAL;
3092 val->value = enum_font_size[i].value;
3093 return;
3094 }
3095 }
3096 /* Invalid */
3097 return;
3098 } else {
3099 gdouble value;
3100 gchar *e;
3101 /* fixme: Move this to standard place (Lauris) */
3102 value = g_ascii_strtod(str, &e);
3103 if ((gchar const *) e != str) {
3104 if (!*e) {
3105 /* Userspace */
3106 } else if (!strcmp(e, "px")) {
3107 /* Userspace */
3108 } else if (!strcmp(e, "pt")) {
3109 /* Userspace * DEVICESCALE */
3110 value *= PX_PER_PT;
3111 } else if (!strcmp(e, "pc")) {
3112 /* 12pt */
3113 value *= PX_PER_PT * 12.0;
3114 } else if (!strcmp(e, "mm")) {
3115 value *= PX_PER_MM;
3116 } else if (!strcmp(e, "cm")) {
3117 value *= PX_PER_CM;
3118 } else if (!strcmp(e, "in")) {
3119 value *= PX_PER_IN;
3120 } else if (!strcmp(e, "%")) {
3121 /* Percentage */
3122 val->set = TRUE;
3123 val->inherit = FALSE;
3124 val->type = SP_FONT_SIZE_PERCENTAGE;
3125 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3126 return;
3127 } else {
3128 /* Invalid */
3129 return;
3130 }
3131 /* Length */
3132 val->set = TRUE;
3133 val->inherit = FALSE;
3134 val->type = SP_FONT_SIZE_LENGTH;
3135 val->computed = value;
3136 return;
3137 }
3138 }
3139 }
3143 /**
3144 * Set SPIFilter object from string.
3145 */
3146 static void
3147 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3148 {
3149 SPIFilter *f = &(style->filter);
3150 /* Try all possible values: inherit, none, uri */
3151 if (streq(str, "inherit")) {
3152 f->set = TRUE;
3153 f->inherit = TRUE;
3154 if (f->href && f->href->getObject())
3155 f->href->detach();
3156 } else if(streq(str, "none")) {
3157 f->set = TRUE;
3158 f->inherit = FALSE;
3159 if (f->href && f->href->getObject())
3160 f->href->detach();
3161 } else if (strneq(str, "url", 3)) {
3162 char *uri = extract_uri(str);
3163 if(uri == NULL || uri[0] == '\0') {
3164 g_warning("Specified filter url is empty");
3165 f->set = TRUE;
3166 f->inherit = FALSE;
3167 return;
3168 }
3169 f->set = TRUE;
3170 f->inherit = FALSE;
3171 if (f->href && f->href->getObject())
3172 f->href->detach();
3174 // it may be that this style has not yet created its SPFilterReference;
3175 // now that we have a document, we can create it here
3176 if (!f->href && document) {
3177 f->href = new SPFilterReference(document);
3178 f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3179 }
3181 try {
3182 f->href->attach(Inkscape::URI(uri));
3183 } catch (Inkscape::BadURIException &e) {
3184 g_warning("%s", e.what());
3185 f->href->detach();
3186 }
3187 g_free (uri);
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->isColor() != b->isColor())
3545 || (a->isPaintserver() != b->isPaintserver())
3546 || (a->set != b->set)
3547 || (a->currentcolor != b->currentcolor)
3548 || (a->inherit!= b->inherit) ) {
3549 return true;
3550 }
3552 // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3554 if ( a->isPaintserver() ) {
3555 return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3556 }
3558 if ( a->isColor() ) {
3559 return !( (a->value.color == b->value.color)
3560 && ((a->value.color.icc == b->value.color.icc)
3561 || (a->value.color.icc && b->value.color.icc
3562 && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3563 && (a->value.color.icc->colors == b->value.color.icc->colors))));
3564 /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3565 * in the string representation. */
3566 }
3568 return false;
3569 }
3573 /**
3574 * Write SPIPaint object into string.
3575 */
3576 static gint
3577 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3578 SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3579 {
3580 int retval = 0;
3582 if ((flags & SP_STYLE_FLAG_ALWAYS)
3583 || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3584 || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3585 && (!base->set || sp_paint_differ(paint, base))))
3586 {
3587 CSSOStringStream css;
3589 if (paint->inherit) {
3590 css << "inherit";
3591 } else {
3592 if ( paint->value.href && paint->value.href->getURI() ) {
3593 const gchar* uri = paint->value.href->getURI()->toString();
3594 css << "url(" << uri << ")";
3595 }
3597 if ( paint->noneSet ) {
3598 if ( !css.str().empty() ) {
3599 css << " ";
3600 }
3601 css << "none";
3602 }
3604 if ( paint->currentcolor ) {
3605 if ( !css.str().empty() ) {
3606 css << " ";
3607 }
3608 css << "currentColor";
3609 }
3611 if ( paint->colorSet ) {
3612 if ( !css.str().empty() ) {
3613 css << " ";
3614 }
3615 char color_buf[8];
3616 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3617 css << color_buf;
3618 }
3620 if (paint->value.color.icc) {
3621 if ( !css.str().empty() ) {
3622 css << " ";
3623 }
3624 css << "icc-color(" << paint->value.color.icc->colorProfile;
3625 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3626 iEnd(paint->value.color.icc->colors.end());
3627 i != iEnd; ++i) {
3628 css << ", " << *i;
3629 }
3630 css << ')';
3631 }
3632 }
3634 if ( !css.str().empty() ) {
3635 retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3636 }
3637 }
3639 return retval;
3640 }
3643 /**
3644 *
3645 */
3646 static bool
3647 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3648 {
3649 if (a->type != b->type)
3650 return true;
3651 if (a->type == SP_FONT_SIZE_LENGTH) {
3652 if (a->computed != b->computed)
3653 return true;
3654 } else {
3655 if (a->value != b->value)
3656 return true;
3657 }
3658 return false;
3659 }
3662 /**
3663 * Write SPIFontSize object into string.
3664 */
3665 static gint
3666 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3667 SPIFontSize const *const val, SPIFontSize 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 && (!base->set || sp_fontsize_differ(val, base))))
3674 {
3675 if (val->inherit) {
3676 return g_snprintf(p, len, "%s:inherit;", key);
3677 } else if (val->type == SP_FONT_SIZE_LITERAL) {
3678 for (unsigned i = 0; enum_font_size[i].key; i++) {
3679 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3680 return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3681 }
3682 }
3683 } else if (val->type == SP_FONT_SIZE_LENGTH) {
3684 Inkscape::CSSOStringStream os;
3685 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
3686 return g_strlcpy(p, os.str().c_str(), len);
3687 } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3688 Inkscape::CSSOStringStream os;
3689 os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3690 return g_strlcpy(p, os.str().c_str(), len);
3691 }
3692 }
3693 return 0;
3694 }
3697 /**
3698 * Write SPIFilter object into string.
3699 */
3700 static gint
3701 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
3702 SPIFilter const *const val, SPIFilter const *const base,
3703 guint const flags)
3704 {
3705 (void)base; // TODO
3706 if ((flags & SP_STYLE_FLAG_ALWAYS)
3707 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3708 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
3709 {
3710 if (val->inherit) {
3711 return g_snprintf(p, len, "%s:inherit;", key);
3712 } else if (val->href && val->href->getURI()) {
3713 return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
3714 }
3715 }
3718 return 0;
3719 }
3722 void SPIPaint::clear()
3723 {
3724 set = false;
3725 inherit = false;
3726 currentcolor = false;
3727 colorSet = false;
3728 noneSet = false;
3729 value.color.set( 0 );
3730 if ( value.href && value.href->getObject() )
3731 {
3732 value.href->detach();
3733 }
3734 }
3737 /**
3738 * Clear filter object, and disconnect style from paintserver (if present).
3739 */
3740 static void
3741 sp_style_filter_clear(SPStyle *style)
3742 {
3743 if (style->filter.href && style->filter.href->getObject())
3744 style->filter.href->detach();
3745 }
3748 // FIXME: Everything below this line belongs in a different file - css-chemistry?
3750 void
3751 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
3752 {
3753 Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
3755 if (repr == NULL) return;
3757 g_return_if_fail(linked != NULL);
3759 gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
3761 SPCSSAttr *css = sp_repr_css_attr_new();
3762 sp_repr_css_set_property(css, property, val);
3763 g_free(val);
3764 if (recursive) {
3765 sp_repr_css_change_recursive(repr, css, "style");
3766 } else {
3767 sp_repr_css_change(repr, css, "style");
3768 }
3769 sp_repr_css_attr_unref(css);
3770 }
3773 /**
3774 * Clear all style property attributes in object.
3775 */
3776 void
3777 sp_style_unset_property_attrs(SPObject *o)
3778 {
3779 if (!o) return;
3781 SPStyle *style = SP_OBJECT_STYLE(o);
3782 if (!style) return;
3784 Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3785 if (!repr) return;
3787 if (style->opacity.set) {
3788 repr->setAttribute("opacity", NULL);
3789 }
3790 if (style->color.set) {
3791 repr->setAttribute("color", NULL);
3792 }
3793 if (style->fill.set) {
3794 repr->setAttribute("fill", NULL);
3795 }
3796 if (style->fill_opacity.set) {
3797 repr->setAttribute("fill-opacity", NULL);
3798 }
3799 if (style->fill_rule.set) {
3800 repr->setAttribute("fill-rule", NULL);
3801 }
3802 if (style->stroke.set) {
3803 repr->setAttribute("stroke", NULL);
3804 }
3805 if (style->stroke_width.set) {
3806 repr->setAttribute("stroke-width", NULL);
3807 }
3808 if (style->stroke_linecap.set) {
3809 repr->setAttribute("stroke-linecap", NULL);
3810 }
3811 if (style->stroke_linejoin.set) {
3812 repr->setAttribute("stroke-linejoin", NULL);
3813 }
3814 if (style->marker[SP_MARKER_LOC].set) {
3815 repr->setAttribute("marker", NULL);
3816 }
3817 if (style->marker[SP_MARKER_LOC_START].set) {
3818 repr->setAttribute("marker-start", NULL);
3819 }
3820 if (style->marker[SP_MARKER_LOC_MID].set) {
3821 repr->setAttribute("marker-mid", NULL);
3822 }
3823 if (style->marker[SP_MARKER_LOC_END].set) {
3824 repr->setAttribute("marker-end", NULL);
3825 }
3826 if (style->stroke_opacity.set) {
3827 repr->setAttribute("stroke-opacity", NULL);
3828 }
3829 if (style->stroke_dasharray_set) {
3830 repr->setAttribute("stroke-dasharray", NULL);
3831 }
3832 if (style->stroke_dashoffset_set) {
3833 repr->setAttribute("stroke-dashoffset", NULL);
3834 }
3835 if (style->text_private && style->text->font_specification.set) {
3836 repr->setAttribute("-inkscape-font-specification", NULL);
3837 }
3838 if (style->text_private && style->text->font_family.set) {
3839 repr->setAttribute("font-family", NULL);
3840 }
3841 if (style->text_anchor.set) {
3842 repr->setAttribute("text-anchor", NULL);
3843 }
3844 if (style->writing_mode.set) {
3845 repr->setAttribute("writing_mode", NULL);
3846 }
3847 if (style->filter.set) {
3848 repr->setAttribute("filter", NULL);
3849 }
3850 if (style->enable_background.set) {
3851 repr->setAttribute("enable-background", NULL);
3852 }
3853 }
3855 /**
3856 * \pre style != NULL.
3857 * \pre flags in {IFSET, ALWAYS}.
3858 */
3859 SPCSSAttr *
3860 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3861 {
3862 g_return_val_if_fail(style != NULL, NULL);
3863 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3864 (flags == SP_STYLE_FLAG_ALWAYS) ),
3865 NULL);
3866 gchar *style_str = sp_style_write_string(style, flags);
3867 SPCSSAttr *css = sp_repr_css_attr_new();
3868 sp_repr_css_attr_add_from_string(css, style_str);
3869 g_free(style_str);
3870 return css;
3871 }
3874 /**
3875 * \pre object != NULL
3876 * \pre flags in {IFSET, ALWAYS}.
3877 */
3878 SPCSSAttr *
3879 sp_css_attr_from_object(SPObject *object, guint const flags)
3880 {
3881 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3882 (flags == SP_STYLE_FLAG_ALWAYS) ),
3883 NULL);
3884 SPStyle const *const style = SP_OBJECT_STYLE(object);
3885 if (style == NULL)
3886 return NULL;
3887 return sp_css_attr_from_style(style, flags);
3888 }
3890 /**
3891 * Unset any text-related properties
3892 */
3893 SPCSSAttr *
3894 sp_css_attr_unset_text(SPCSSAttr *css)
3895 {
3896 sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3897 sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
3898 sp_repr_css_set_property(css, "font-size", NULL);
3899 sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3900 sp_repr_css_set_property(css, "font-style", NULL);
3901 sp_repr_css_set_property(css, "font-variant", NULL);
3902 sp_repr_css_set_property(css, "font-weight", NULL);
3903 sp_repr_css_set_property(css, "font-stretch", NULL);
3904 sp_repr_css_set_property(css, "font-family", NULL);
3905 sp_repr_css_set_property(css, "text-indent", NULL);
3906 sp_repr_css_set_property(css, "text-align", NULL);
3907 sp_repr_css_set_property(css, "text-decoration", NULL);
3908 sp_repr_css_set_property(css, "line-height", NULL);
3909 sp_repr_css_set_property(css, "letter-spacing", NULL);
3910 sp_repr_css_set_property(css, "word-spacing", NULL);
3911 sp_repr_css_set_property(css, "text-transform", NULL);
3912 sp_repr_css_set_property(css, "direction", NULL);
3913 sp_repr_css_set_property(css, "block-progression", NULL);
3914 sp_repr_css_set_property(css, "writing-mode", NULL);
3915 sp_repr_css_set_property(css, "text-anchor", NULL);
3916 sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3917 sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3918 sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3919 sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3921 return css;
3922 }
3924 bool
3925 is_url(char const *p)
3926 {
3927 if (p == NULL)
3928 return false;
3929 /** \todo
3930 * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3931 * in property values must start with 'url('.
3932 */
3933 return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3934 }
3936 /**
3937 * Unset any properties that contain URI values.
3938 *
3939 * Used for storing style that will be reused across documents when carrying
3940 * the referenced defs is impractical.
3941 */
3942 SPCSSAttr *
3943 sp_css_attr_unset_uris(SPCSSAttr *css)
3944 {
3945 // All properties that may hold <uri> or <paint> according to SVG 1.1
3946 if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3947 if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3948 if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3949 if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3950 if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3951 if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3952 if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3953 if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3954 if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3955 if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3957 return css;
3958 }
3960 /**
3961 * Scale a single-value property.
3962 */
3963 void
3964 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3965 double ex, bool only_with_units = false)
3966 {
3967 gchar const *w = sp_repr_css_property(css, property, NULL);
3968 if (w) {
3969 gchar *units = NULL;
3970 double wd = g_ascii_strtod(w, &units) * ex;
3971 if (w == units) {// nothing converted, non-numeric value
3972 return;
3973 }
3974 if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3975 // only_with_units, but no units found, so do nothing.
3976 return;
3977 }
3978 Inkscape::CSSOStringStream os;
3979 os << wd << units; // reattach units
3980 sp_repr_css_set_property(css, property, os.str().c_str());
3981 }
3982 }
3984 /**
3985 * Scale a list-of-values property.
3986 */
3987 void
3988 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3989 {
3990 gchar const *string = sp_repr_css_property(css, property, NULL);
3991 if (string) {
3992 Inkscape::CSSOStringStream os;
3993 gchar **a = g_strsplit(string, ",", 10000);
3994 bool first = true;
3995 for (gchar **i = a; i != NULL; i++) {
3996 gchar *w = *i;
3997 if (w == NULL)
3998 break;
3999 gchar *units = NULL;
4000 double wd = g_ascii_strtod(w, &units) * ex;
4001 if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4002 g_strfreev(a);
4003 return;
4004 }
4005 if (!first) {
4006 os << ",";
4007 }
4008 os << wd << units; // reattach units
4009 first = false;
4010 }
4011 sp_repr_css_set_property(css, property, os.str().c_str());
4012 g_strfreev(a);
4013 }
4014 }
4016 /**
4017 * Scale any properties that may hold <length> by ex.
4018 */
4019 SPCSSAttr *
4020 sp_css_attr_scale(SPCSSAttr *css, double ex)
4021 {
4022 sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4023 sp_css_attr_scale_property_single(css, "stroke-width", ex);
4024 sp_css_attr_scale_property_list (css, "stroke-dasharray", ex);
4025 sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4026 sp_css_attr_scale_property_single(css, "font-size", ex);
4027 sp_css_attr_scale_property_single(css, "kerning", ex);
4028 sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4029 sp_css_attr_scale_property_single(css, "word-spacing", ex);
4030 sp_css_attr_scale_property_single(css, "line-height", ex, true);
4032 return css;
4033 }
4036 /**
4037 * Remove quotes and escapes from a string. Returned value must be g_free'd.
4038 * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4039 * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4040 * it parsed for us. So this function is only used to remove those quotes and for
4041 * presentation attributes, without any unescaping. (XML unescaping
4042 * (& etc) is done by XML parser.)
4043 */
4044 gchar *
4045 attribute_unquote(gchar const *val)
4046 {
4047 if (val) {
4048 if (*val == '\'' || *val == '"') {
4049 int l = strlen(val);
4050 if (l >= 2) {
4051 if ( ( val[0] == '"' && val[l - 1] == '"' ) ||
4052 ( val[0] == '\'' && val[l - 1] == '\'' ) ) {
4053 return (g_strndup (val+1, l-2));
4054 }
4055 }
4056 }
4057 }
4059 return (val? g_strdup (val) : NULL);
4060 }
4062 /**
4063 * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4064 */
4065 gchar *
4066 css2_escape_quote(gchar const *val) {
4068 Glib::ustring t;
4069 bool quote = false;
4070 bool last_was_space = false;
4072 for (gchar const *i = val; *i; i++) {
4073 bool is_space = ( *i == ' ' );
4074 if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4075 // ASCII alphanumeric, - and _ don't require quotes
4076 t.push_back(*i);
4077 } else if ( is_space && !last_was_space ) {
4078 // non-consecutive spaces don't require quotes
4079 t.push_back(*i);
4080 } else if (*i=='\'') {
4081 // single quotes require escaping and quotes
4082 t.push_back('\\');
4083 t.push_back(*i);
4084 quote = true;
4085 } else {
4086 // everything else requires quotes
4087 t.push_back(*i);
4088 quote = true;
4089 }
4090 if (i == val && !g_ascii_isalpha(*i)) {
4091 // a non-ASCII/non-alpha initial character requires quotes
4092 quote = true;
4093 }
4094 last_was_space = is_space;
4095 }
4097 if (last_was_space) {
4098 // a trailing space requires quotes
4099 quote = true;
4100 }
4102 if (quote) {
4103 // we use single quotes so the result can be stored in an XML
4104 // attribute without incurring un-aesthetic additional quoting
4105 // (our XML emitter always uses double quotes)
4106 t.insert(t.begin(), '\'');
4107 t.push_back('\'');
4108 }
4110 return (t.empty() ? NULL : g_strdup (t.c_str()));
4111 }
4113 /*
4114 Local Variables:
4115 mode:c++
4116 c-file-style:"stroustrup"
4117 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4118 indent-tabs-mode:nil
4119 fill-column:99
4120 End:
4121 */
4122 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :