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