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