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