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 // baseline-shifts are relative to parent baseline
1400 child.computed += parent.computed;
1401 }
1403 /**
1404 * Sets computed values in \a style, which may involve inheriting from (or in some other way
1405 * calculating from) corresponding computed values of \a parent.
1406 *
1407 * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1408 * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1409 * inherit a value. http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1410 * about what the computed value is (not obvious for lengths).
1411 *
1412 * \pre \a parent's computed values are already up-to-date.
1413 */
1414 void
1415 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1416 {
1417 g_return_if_fail(style != NULL);
1419 /** \todo
1420 * fixme: Check for existing callers that might pass null parent.
1421 * This should probably be g_return_if_fail, or else we should make a
1422 * best attempt to set computed values correctly without having a parent
1423 * (i.e., by assuming parent has initial values).
1424 */
1425 if (!parent)
1426 return;
1428 /* CSS2 */
1429 /* Font */
1430 sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1432 /* 'font-style' */
1433 if (!style->font_style.set || style->font_style.inherit) {
1434 style->font_style.computed = parent->font_style.computed;
1435 }
1437 /* 'font-variant' */
1438 if (!style->font_variant.set || style->font_variant.inherit) {
1439 style->font_variant.computed = parent->font_variant.computed;
1440 }
1442 /* 'font-weight' */
1443 if (!style->font_weight.set || style->font_weight.inherit) {
1444 style->font_weight.computed = parent->font_weight.computed;
1445 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1446 /** \todo
1447 * fixme: This is unconditional, i.e., happens even if parent not
1448 * present.
1449 */
1450 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1451 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1452 style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1453 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1454 unsigned const parent_val = parent->font_weight.computed;
1455 g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1456 // strictly, 'bolder' and 'lighter' should go to the next weight
1457 // expressible in the current font family, but that's difficult to
1458 // find out, so jumping by 3 seems an appropriate approximation
1459 style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1460 ? (unsigned)SP_CSS_FONT_WEIGHT_100
1461 : parent_val - 3);
1462 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1463 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1464 unsigned const parent_val = parent->font_weight.computed;
1465 g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1466 style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1467 ? (unsigned)SP_CSS_FONT_WEIGHT_900
1468 : parent_val + 3);
1469 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1470 }
1472 /* 'font-stretch' */
1473 if (!style->font_stretch.set || style->font_stretch.inherit) {
1474 style->font_stretch.computed = parent->font_stretch.computed;
1475 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1476 unsigned const parent_val = parent->font_stretch.computed;
1477 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1478 ? parent_val
1479 : parent_val - 1);
1480 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1481 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1482 unsigned const parent_val = parent->font_stretch.computed;
1483 g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1484 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1485 ? parent_val
1486 : parent_val + 1);
1487 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1488 }
1490 /* text (css2) */
1491 if (!style->text_indent.set || style->text_indent.inherit) {
1492 style->text_indent.computed = parent->text_indent.computed;
1493 }
1495 if (!style->text_align.set || style->text_align.inherit) {
1496 style->text_align.computed = parent->text_align.computed;
1497 }
1499 if (!style->text_decoration.set || style->text_decoration.inherit) {
1500 style->text_decoration.underline = parent->text_decoration.underline;
1501 style->text_decoration.overline = parent->text_decoration.overline;
1502 style->text_decoration.line_through = parent->text_decoration.line_through;
1503 style->text_decoration.blink = parent->text_decoration.blink;
1504 }
1506 if (!style->line_height.set || style->line_height.inherit) {
1507 style->line_height.value = parent->line_height.value;
1508 style->line_height.computed = parent->line_height.computed;
1509 style->line_height.normal = parent->line_height.normal;
1510 }
1512 if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1513 style->letter_spacing.value = parent->letter_spacing.value;
1514 style->letter_spacing.computed = parent->letter_spacing.computed;
1515 style->letter_spacing.normal = parent->letter_spacing.normal;
1516 }
1518 if (!style->word_spacing.set || style->word_spacing.inherit) {
1519 style->word_spacing.value = parent->word_spacing.value;
1520 style->word_spacing.computed = parent->word_spacing.computed;
1521 style->word_spacing.normal = parent->word_spacing.normal;
1522 }
1524 if (!style->text_transform.set || style->text_transform.inherit) {
1525 style->text_transform.computed = parent->text_transform.computed;
1526 }
1528 if (!style->direction.set || style->direction.inherit) {
1529 style->direction.computed = parent->direction.computed;
1530 }
1532 if (!style->block_progression.set || style->block_progression.inherit) {
1533 style->block_progression.computed = parent->block_progression.computed;
1534 }
1536 if (!style->writing_mode.set || style->writing_mode.inherit) {
1537 style->writing_mode.computed = parent->writing_mode.computed;
1538 }
1540 if (!style->text_anchor.set || style->text_anchor.inherit) {
1541 style->text_anchor.computed = parent->text_anchor.computed;
1542 }
1544 /* Baseline Shift... Some shifts are relative to parent. */
1545 sp_style_merge_baseline_shift_from_parent(style->baseline_shift, parent->baseline_shift,
1546 parent->font_size);
1548 if (style->opacity.inherit) {
1549 style->opacity.value = parent->opacity.value;
1550 }
1552 /* Color */
1553 if (!style->color.set || style->color.inherit) {
1554 sp_style_merge_ipaint(style, &style->color, &parent->color);
1555 }
1557 /* Fill */
1558 if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1559 sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1560 }
1562 if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1563 style->fill_opacity.value = parent->fill_opacity.value;
1564 }
1566 if (!style->fill_rule.set || style->fill_rule.inherit) {
1567 style->fill_rule.computed = parent->fill_rule.computed;
1568 }
1570 /* Stroke */
1571 if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1572 sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1573 }
1575 if (!style->stroke_width.set || style->stroke_width.inherit) {
1576 style->stroke_width.computed = parent->stroke_width.computed;
1577 } else {
1578 /* Update computed value for any change in font inherited from parent. */
1579 double const em = style->font_size.computed;
1580 if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1581 style->stroke_width.computed = style->stroke_width.value * em;
1582 } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1583 double const ex = em * 0.5; // fixme: Get x height from libnrtype or pango.
1584 style->stroke_width.computed = style->stroke_width.value * ex;
1585 }
1586 }
1588 if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1589 style->stroke_linecap.computed = parent->stroke_linecap.computed;
1590 }
1592 if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1593 style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1594 }
1596 if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1597 style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1598 }
1600 if (!style->stroke_dasharray_set || style->stroke_dasharray_inherit) {
1601 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1602 if (style->stroke_dash.n_dash > 0) {
1603 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1604 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1605 }
1606 }
1608 if (!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) {
1609 style->stroke_dash.offset = parent->stroke_dash.offset;
1610 }
1612 if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1613 style->stroke_opacity.value = parent->stroke_opacity.value;
1614 }
1616 if (style->text && parent->text) {
1617 if (!style->text->font_family.set || style->text->font_family.inherit) {
1618 g_free(style->text->font_family.value);
1619 style->text->font_family.value = g_strdup(parent->text->font_family.value);
1620 }
1621 }
1623 if (style->text && parent->text) {
1624 if (!style->text->font_specification.set || style->text->font_specification.inherit) {
1625 g_free(style->text->font_specification.value);
1626 style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
1627 }
1628 }
1630 /* Markers - Free the old value and make copy of the new */
1631 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1632 if (!style->marker[i].set || style->marker[i].inherit) {
1633 g_free(style->marker[i].value);
1634 style->marker[i].value = g_strdup(parent->marker[i].value);
1635 }
1636 }
1638 /* Filter effects */
1639 if (style->filter.inherit) {
1640 sp_style_merge_ifilter(style, &parent->filter);
1641 }
1643 if(style->enable_background.inherit) {
1644 style->enable_background.value = parent->enable_background.value;
1645 }
1646 }
1648 template <typename T>
1649 static void
1650 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1651 {
1652 if ( ( !(child.set) || child.inherit )
1653 && parent.set && !(parent.inherit) )
1654 {
1655 child = parent;
1656 }
1657 }
1659 /**
1660 * Copy SPIString from parent to child.
1661 */
1662 static void
1663 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1664 {
1665 if ( ( !(child.set) || child.inherit )
1666 && parent.set && !(parent.inherit) )
1667 {
1668 g_free(child.value);
1669 child.value = g_strdup(parent.value);
1670 child.set = parent.set;
1671 child.inherit = parent.inherit;
1672 }
1673 }
1675 static void
1676 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1677 SPIPaint &child, SPIPaint const &parent)
1678 {
1679 /** \todo
1680 * I haven't given this much attention. See comments below about
1681 * currentColor, colorProfile, and relative URIs.
1682 */
1683 if (!child.set || child.inherit) {
1684 sp_style_merge_ipaint(style, &child, &parent);
1685 child.set = parent.set;
1686 child.inherit = parent.inherit;
1687 }
1688 }
1690 static void
1691 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1692 unsigned const max_computed_val,
1693 unsigned const smaller_val)
1694 {
1695 /* We assume that min computed val is 0, contiguous up to max_computed_val,
1696 then zero or more other absolute values, then smaller_val then larger_val. */
1697 unsigned const min_computed_val = 0;
1698 unsigned const larger_val = smaller_val + 1;
1699 g_return_if_fail(min_computed_val < max_computed_val);
1700 g_return_if_fail(max_computed_val < smaller_val);
1702 if (parent.set && !parent.inherit) {
1703 if (!child.set || child.inherit) {
1704 child.value = parent.value;
1705 child.set = parent.set; // i.e. true
1706 child.inherit = parent.inherit; // i.e. false
1707 } else if (child.value < smaller_val) {
1708 /* Child has absolute value: leave as is. */
1709 } else if ( ( child.value == smaller_val
1710 && parent.value == larger_val )
1711 || ( parent.value == smaller_val
1712 && child.value == larger_val ) )
1713 {
1714 child.set = false;
1715 /*
1716 * Note that this can result in a change in computed value in the
1717 * rare case that the parent's setting was a no-op (i.e. if the
1718 * parent's parent's computed value was already ultra-condensed or
1719 * ultra-expanded). However, I'd guess that the change is for the
1720 * better: I'd guess that if the properties were specified
1721 * relatively, then the intent is to counteract parent's effect.
1722 * I.e. I believe this is the best code even in that case.
1723 */
1724 } else if (child.value == parent.value) {
1725 /* Leave as is. */
1726 /** \todo
1727 * It's unclear what to do if style and parent specify the same
1728 * relative directive (narrower or wider). We can either convert
1729 * to absolute specification or coalesce to a single relative
1730 * request (of half the strength of the original pair).
1731 *
1732 * Converting to a single level of relative specification is a
1733 * better choice if the newly-unlinked clone is itself cloned to
1734 * other contexts (inheriting different font stretchiness): it
1735 * would preserve the effect that it will be narrower than
1736 * the inherited context in each case. The disadvantage is that
1737 * it will ~certainly affect the computed value of the
1738 * newly-unlinked clone.
1739 */
1740 } else {
1741 unsigned const parent_val = parent.computed;
1742 child.value = ( child.value == smaller_val
1743 ? ( parent_val == min_computed_val
1744 ? parent_val
1745 : parent_val - 1 )
1746 : ( parent_val == max_computed_val
1747 ? parent_val
1748 : parent_val + 1 ) );
1749 g_assert(child.value <= max_computed_val);
1750 child.inherit = false;
1751 g_assert(child.set);
1752 }
1753 }
1754 }
1756 template <typename LengthT>
1757 static void
1758 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1759 double const parent_child_em_ratio)
1760 {
1761 if ( ( !(child.set) || child.inherit )
1762 && parent.set && !(parent.inherit) )
1763 {
1764 child = parent;
1765 switch (parent.unit) {
1766 case SP_CSS_UNIT_EM:
1767 case SP_CSS_UNIT_EX:
1768 child.value *= parent_child_em_ratio;
1769 /** \todo
1770 * fixme: Have separate ex ratio parameter.
1771 * Get x height from libnrtype or pango.
1772 */
1773 if (!IS_FINITE(child.value)) {
1774 child.value = child.computed;
1775 child.unit = SP_CSS_UNIT_NONE;
1776 }
1777 break;
1779 default:
1780 break;
1781 }
1782 }
1783 }
1785 static double
1786 get_relative_font_size_frac(SPIFontSize const &font_size)
1787 {
1788 switch (font_size.type) {
1789 case SP_FONT_SIZE_LITERAL: {
1790 switch (font_size.value) {
1791 case SP_CSS_FONT_SIZE_SMALLER:
1792 return 5.0 / 6.0;
1794 case SP_CSS_FONT_SIZE_LARGER:
1795 return 6.0 / 5.0;
1797 default:
1798 g_assert_not_reached();
1799 }
1800 }
1802 case SP_FONT_SIZE_PERCENTAGE:
1803 return SP_F8_16_TO_FLOAT(font_size.value);
1805 case SP_FONT_SIZE_LENGTH:
1806 g_assert_not_reached();
1807 }
1808 g_assert_not_reached();
1809 }
1811 /**
1812 * Combine \a style and \a parent style specifications into a single style specification that
1813 * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1814 *
1815 * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1816 * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1817 * trying to retain the same visual appearance once the parent is removed. Interesting cases are
1818 * when there is unusual interaction with the parent's value (opacity, display) or when the value
1819 * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1820 *
1821 * Doesn't update computed values of \a style. For correctness, you should subsequently call
1822 * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1823 * style was previously up-to-date wrt \a parent.
1824 *
1825 * \pre \a parent's computed values are already up-to-date.
1826 * (\a style's computed values needn't be up-to-date.)
1827 */
1828 void
1829 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1830 {
1831 /** \note
1832 * The general rule for each property is as follows:
1833 *
1834 * If style is set to an absolute value, then leave it as is.
1835 *
1836 * Otherwise (i.e. if style has a relative value):
1837 *
1838 * If parent is set to an absolute value, then set style to the computed value.
1839 *
1840 * Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1841 */
1843 /* We do font-size first, to ensure that em size is up-to-date. */
1844 /** \todo
1845 * fixme: We'll need to have more font-related things up the top once
1846 * we're getting x-height from pango or libnrtype.
1847 */
1849 /* Some things that allow relative specifications. */
1850 {
1851 /* font-size. Note that we update the computed font-size of style,
1852 to assist in em calculations later in this function. */
1853 if (parent->font_size.set && !parent->font_size.inherit) {
1854 if (!style->font_size.set || style->font_size.inherit) {
1855 /* font_size inherits the computed value, so we can use the parent value
1856 * verbatim. */
1857 style->font_size = parent->font_size;
1858 } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1859 /* Child already has absolute size (stored in computed value), so do nothing. */
1860 } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1861 && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1862 /* Child already has absolute size, but we ensure that the computed value
1863 is up-to-date. */
1864 unsigned const ix = style->font_size.value;
1865 g_assert(ix < G_N_ELEMENTS(font_size_table));
1866 style->font_size.computed = font_size_table[ix];
1867 } else {
1868 /* Child has relative size. */
1869 double const child_frac(get_relative_font_size_frac(style->font_size));
1870 style->font_size.set = true;
1871 style->font_size.inherit = false;
1872 style->font_size.computed = parent->font_size.computed * child_frac;
1874 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1875 && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1876 || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1877 {
1878 /* Absolute value. */
1879 style->font_size.type = SP_FONT_SIZE_LENGTH;
1880 /* .value is unused for SP_FONT_SIZE_LENGTH. */
1881 } else {
1882 /* Relative value. */
1883 double const parent_frac(get_relative_font_size_frac(parent->font_size));
1884 style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1885 style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1886 }
1887 }
1888 }
1890 /* 'font-stretch' */
1891 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1892 parent->font_stretch,
1893 SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1894 SP_CSS_FONT_STRETCH_NARROWER);
1896 /* font-weight */
1897 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1898 parent->font_weight,
1899 SP_CSS_FONT_WEIGHT_900,
1900 SP_CSS_FONT_WEIGHT_LIGHTER);
1901 }
1904 /* Enum values that don't have any relative settings (other than `inherit'). */
1905 {
1906 SPIEnum SPStyle::*const fields[] = {
1907 //nyi: SPStyle::clip_rule,
1908 //nyi: SPStyle::color_interpolation,
1909 //nyi: SPStyle::color_interpolation_filters,
1910 //nyi: SPStyle::color_rendering,
1911 &SPStyle::direction,
1912 &SPStyle::fill_rule,
1913 &SPStyle::font_style,
1914 &SPStyle::font_variant,
1915 //nyi: SPStyle::image_rendering,
1916 //nyi: SPStyle::pointer_events,
1917 //nyi: SPStyle::shape_rendering,
1918 &SPStyle::stroke_linecap,
1919 &SPStyle::stroke_linejoin,
1920 &SPStyle::text_anchor,
1921 //nyi: &SPStyle::text_rendering,
1922 &SPStyle::visibility,
1923 &SPStyle::writing_mode
1924 };
1926 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1927 SPIEnum SPStyle::*const fld = fields[i];
1928 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1929 }
1930 }
1932 /* A few other simple inheritance properties. */
1933 {
1934 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1935 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1936 sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1938 /** \todo
1939 * We currently treat text-decoration as if it were a simple inherited
1940 * property (fixme). This code may need changing once we do the
1941 * special fill/stroke inheritance mentioned by the spec.
1942 */
1943 sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1944 parent->text_decoration);
1946 //nyi: font-size-adjust, // <number> | none | inherit
1947 //nyi: glyph-orientation-horizontal,
1948 //nyi: glyph-orientation-vertical,
1949 }
1951 /* Properties that involve length but are easy in other respects. */
1952 {
1953 /* The difficulty with lengths is that font-relative units need adjusting if the font
1954 * varies between parent & child.
1955 *
1956 * Lengths specified in the existing child can stay as they are: its computed font
1957 * specification should stay unchanged, so em & ex lengths should continue to mean the same
1958 * size.
1959 *
1960 * Lengths specified in the dying parent in em or ex need to be scaled according to the
1961 * ratio of em or ex size between parent & child.
1962 */
1963 double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1965 SPILength SPStyle::*const lfields[] = {
1966 &SPStyle::stroke_width,
1967 &SPStyle::text_indent
1968 };
1969 for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1970 SPILength SPStyle::*fld = lfields[i];
1971 sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1972 parent->*fld,
1973 parent_child_em_ratio);
1974 }
1976 SPILengthOrNormal SPStyle::*const nfields[] = {
1977 &SPStyle::letter_spacing,
1978 &SPStyle::line_height,
1979 &SPStyle::word_spacing
1980 };
1981 for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1982 SPILengthOrNormal SPStyle::*fld = nfields[i];
1983 sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1984 parent->*fld,
1985 parent_child_em_ratio);
1986 }
1988 //nyi: &SPStyle::kerning: length or `auto'
1990 /* fixme: Move stroke-dash and stroke-dash-offset here once they
1991 can accept units. */
1992 }
1994 /* Properties that involve a URI but are easy in other respects. */
1995 {
1996 /** \todo
1997 * Could cause problems if original object was in another document
1998 * and it used a relative URL. (At the time of writing, we don't
1999 * allow hrefs to other documents, so this isn't a problem yet.)
2000 * Paint properties also allow URIs.
2001 */
2002 //nyi: cursor, // may involve change in effect, but we can't do much better
2003 //nyi: color-profile,
2005 // Markers (marker-start, marker-mid, marker-end).
2006 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2007 sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
2008 }
2009 }
2011 /* CSS2 */
2012 /* Font */
2014 if (style->text && parent->text) {
2015 sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
2016 parent->text->font_specification);
2018 sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
2019 parent->text->font_family);
2020 }
2023 /* Properties that don't inherit by default. Most of these need special handling. */
2024 {
2025 /*
2026 * opacity's effect is cumulative; we set the new value to the combined effect. The
2027 * default value for opacity is 1.0, not inherit. (Note that stroke-opacity and
2028 * fill-opacity are quite different from opacity, and don't need any special handling.)
2029 *
2030 * Cases:
2031 * - parent & child were each previously unset, in which case the effective
2032 * opacity value is 1.0, and style should remain unset.
2033 * - parent was previously unset (so computed opacity value of 1.0)
2034 * and child was set to inherit. The merged child should
2035 * get a value of 1.0, and shouldn't inherit (lest the new parent
2036 * has a different opacity value). Given that opacity's default
2037 * value is 1.0 (rather than inherit), we might as well have the
2038 * merged child's opacity be unset.
2039 * - parent was previously unset (so opacity 1.0), and child was set to a number.
2040 * The merged child should retain its existing settings (though it doesn't matter
2041 * if we make it unset if that number was 1.0).
2042 * - parent was inherit and child was unset. Merged child should be set to inherit.
2043 * - parent was inherit and child was inherit. (We can't in general reproduce this
2044 * effect (short of introducing a new group), but setting opacity to inherit is rare.)
2045 * If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
2046 * child's value should be set to the product of the two, i.e. the square of the
2047 * inherited value, and should not be marked as inherit. (This decision assumes that it
2048 * is more important to retain the effective opacity than to retain the inheriting
2049 * effect, and assumes that the inheriting effect either isn't important enough to create
2050 * a group or isn't common enough to bother maintaining the code to create a group.) If
2051 * the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
2052 * closer to maintaining the effect.
2053 * - parent was inherit and child was set to a numerical value. If the child's value
2054 * was 1.0, then the merged child should have the same settings as the parent.
2055 * If the child's value was 0, then the merged child should also be set to 0.
2056 * If the child's value was anything else, then we do the same as for the inherit/inherit
2057 * case above: have the merged child set to the product of the two opacities and not
2058 * marked as inherit, for the same reasons as for that case.
2059 * - parent was set to a value, and child was unset. The merged child should have
2060 * parent's settings.
2061 * - parent was set to a value, and child was inherit. The merged child should
2062 * be set to the product, i.e. the square of the parent's value.
2063 * - parent & child are each set to a value. The merged child should be set to the
2064 * product.
2065 */
2066 if ( !style->opacity.set
2067 || ( !style->opacity.inherit
2068 && style->opacity.value == SP_SCALE24_MAX ) )
2069 {
2070 style->opacity = parent->opacity;
2071 } else {
2072 /* Ensure that style's computed value is up-to-date. */
2073 if (style->opacity.inherit) {
2074 style->opacity.value = parent->opacity.value;
2075 }
2077 /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
2078 style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
2079 parent->opacity.value);
2081 style->opacity.inherit = (parent->opacity.inherit
2082 && style->opacity.inherit
2083 && (parent->opacity.value == 0 ||
2084 parent->opacity.value == SP_SCALE24_MAX));
2085 style->opacity.set = ( style->opacity.inherit
2086 || style->opacity.value < SP_SCALE24_MAX );
2087 }
2089 /* display is in principle similar to opacity, but implementation is easier. */
2090 if ( parent->display.set && !parent->display.inherit
2091 && parent->display.value == SP_CSS_DISPLAY_NONE ) {
2092 style->display.value = SP_CSS_DISPLAY_NONE;
2093 style->display.set = true;
2094 style->display.inherit = false;
2095 } else if (style->display.inherit) {
2096 style->display.value = parent->display.value;
2097 style->display.set = parent->display.set;
2098 style->display.inherit = parent->display.inherit;
2099 } else {
2100 /* Leave as is. (display doesn't inherit by default.) */
2101 }
2103 /* enable-background - this is rather complicated, because
2104 * it is valid only when applied to container elements.
2105 * Let's check a simple case anyhow. */
2106 if (parent->enable_background.set
2107 && !parent->enable_background.inherit
2108 && style->enable_background.inherit)
2109 {
2110 style->enable_background.set = true;
2111 style->enable_background.inherit = false;
2112 style->enable_background.value = parent->enable_background.value;
2113 }
2115 if (!style->filter.set || style->filter.inherit)
2116 {
2117 sp_style_merge_ifilter(style, &parent->filter);
2118 }
2120 /** \todo
2121 * fixme: Check that we correctly handle all properties that don't
2122 * inherit by default (as shown in
2123 * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
2124 */
2125 }
2127 /* SPIPaint properties (including color). */
2128 {
2129 /** \todo
2130 * Think about the issues involved if specified as currentColor or
2131 * if specified relative to colorProfile, and if the currentColor or
2132 * colorProfile differs between parent \& child. See also comments
2133 * elsewhere in this function about URIs.
2134 */
2135 SPIPaint SPStyle::*const fields[] = {
2136 &SPStyle::color,
2137 &SPStyle::fill,
2138 &SPStyle::stroke
2139 };
2140 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
2141 SPIPaint SPStyle::*const fld = fields[i];
2142 sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
2143 }
2144 }
2146 /* Things from SVG 1.2 or CSS3. */
2147 {
2148 /* Note: If we ever support setting string values for text-align then we'd need strdup
2149 * handling here. */
2150 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
2152 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
2153 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
2154 }
2156 /* Note: this will need length handling once dasharray supports units. */
2157 if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
2158 && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
2159 {
2160 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
2161 if (style->stroke_dash.n_dash > 0) {
2162 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
2163 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
2164 }
2165 style->stroke_dasharray_set = parent->stroke_dasharray_set;
2166 style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
2167 }
2169 /* Note: this will need length handling once dasharray_offset supports units. */
2170 if ((!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) && parent->stroke_dashoffset_set && !parent->stroke_dashoffset_inherit) {
2171 style->stroke_dash.offset = parent->stroke_dash.offset;
2172 style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
2173 style->stroke_dashoffset_inherit = parent->stroke_dashoffset_inherit;
2174 /* TODO: Try to
2175 * represent it as a normal SPILength; though will need to do something about existing
2176 * users of stroke_dash.offset and stroke_dashoffset_set. */
2177 }
2178 }
2181 static void
2182 sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
2183 {
2184 // it may be that this style's SPIPaint has not yet created its URIReference;
2185 // now that we have a document, we can create it here
2186 if (!paint->value.href && document) {
2187 paint->value.href = new SPPaintServerReference(document);
2188 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));
2189 }
2191 if (paint->value.href && paint->value.href->getObject())
2192 paint->value.href->detach();
2194 if (paint->value.href) {
2195 try {
2196 paint->value.href->attach(*uri);
2197 } catch (Inkscape::BadURIException &e) {
2198 g_warning("%s", e.what());
2199 paint->value.href->detach();
2200 }
2201 }
2202 }
2204 static void
2205 sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
2206 {
2207 try {
2208 const Inkscape::URI IURI(uri);
2209 sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
2210 } catch (...) {
2211 g_warning("URI failed to parse: %s", uri);
2212 }
2213 }
2215 void
2216 sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
2217 {
2218 sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
2219 }
2221 /**
2222 *
2223 */
2224 static void
2225 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
2226 {
2227 if ((paint->set && paint->currentcolor) || parent->currentcolor) {
2228 bool isset = paint->set;
2229 paint->clear();
2230 paint->set = isset;
2231 paint->currentcolor = TRUE;
2232 paint->setColor(style->color.value.color);
2233 return;
2234 }
2236 paint->clear();
2237 if ( parent->isPaintserver() ) {
2238 if (parent->value.href) {
2239 sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
2240 } else {
2241 g_warning("Expected paint server not found.");
2242 }
2243 } else if ( parent->isColor() ) {
2244 paint->setColor( parent->value.color );
2245 } else if ( parent->isNoneSet() ) {
2246 paint->noneSet = TRUE;
2247 } else if ( parent->isNone() ) {
2248 //
2249 } else {
2250 g_assert_not_reached();
2251 }
2252 }
2255 /**
2256 * Merge filter style from parent.
2257 * Filter effects do not inherit by default
2258 */
2259 static void
2260 sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
2261 {
2262 // FIXME:
2263 // instead of just copying over, we need to _really merge_ the two filters by combining their
2264 // filter primitives
2266 sp_style_filter_clear(style);
2267 style->filter.set = parent->set;
2268 style->filter.inherit = parent->inherit;
2270 if (style->filter.href && style->filter.href->getObject())
2271 style->filter.href->detach();
2273 // it may be that this style has not yet created its SPFilterReference
2274 if (!style->filter.href && style->object && SP_OBJECT_DOCUMENT(style->object)) {
2275 style->filter.href = new SPFilterReference(SP_OBJECT_DOCUMENT(style->object));
2276 style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2277 }
2279 if (style->filter.href && parent->href && parent->href->getObject()) {
2280 try {
2281 style->filter.href->attach(*parent->href->getURI());
2282 } catch (Inkscape::BadURIException &e) {
2283 g_warning("%s", e.what());
2284 style->filter.href->detach();
2285 }
2286 }
2287 }
2289 /**
2290 * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2291 * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2292 * complete cascaded style to style_clipboard. When you need a CSS string
2293 * for an object in the document tree, you normally call
2294 * sp_style_write_difference instead to take into account the object's parent.
2295 *
2296 * \pre style != NULL.
2297 * \pre flags in {IFSET, ALWAYS}.
2298 * \post ret != NULL.
2299 */
2300 gchar *
2301 sp_style_write_string(SPStyle const *const style, guint const flags)
2302 {
2303 /** \todo
2304 * Merge with write_difference, much duplicate code!
2305 */
2306 g_return_val_if_fail(style != NULL, NULL);
2307 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2308 (flags == SP_STYLE_FLAG_ALWAYS) ),
2309 NULL);
2311 gchar c[BMAX];
2312 gchar *p = c;
2313 *p = '\0';
2315 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2316 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2317 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2318 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2319 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2321 /* Text */
2322 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2323 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2324 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2325 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2326 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2327 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2328 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2329 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2330 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2331 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2333 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2334 p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &style->baseline_shift, NULL, flags);
2337 /// \todo fixme: Per type methods need default flag too (lauris)
2339 if (style->opacity.value != SP_SCALE24_MAX) {
2340 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2341 }
2343 if (!style->color.noneSet) { // CSS does not permit "none" for color
2344 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2345 }
2347 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2348 // if fill:none, skip writing fill properties
2349 if (!style->fill.noneSet) {
2350 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2351 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2352 }
2354 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2356 // stroke width affects markers, so write it if there's stroke OR any markers
2357 if (!style->stroke.noneSet ||
2358 style->marker[SP_MARKER_LOC].set ||
2359 style->marker[SP_MARKER_LOC_START].set ||
2360 style->marker[SP_MARKER_LOC_MID].set ||
2361 style->marker[SP_MARKER_LOC_END].set) {
2362 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2363 }
2365 // if stroke:none, skip writing stroke properties
2366 if (!style->stroke.noneSet) {
2367 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2368 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2369 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2370 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2372 /** \todo fixme: */
2373 if ((flags == SP_STYLE_FLAG_ALWAYS)
2374 || style->stroke_dasharray_set)
2375 {
2376 if (style->stroke_dasharray_inherit) {
2377 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2378 } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2379 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2380 gint i;
2381 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2382 Inkscape::CSSOStringStream os;
2383 if (i) {
2384 os << ", ";
2385 }
2386 os << style->stroke_dash.dash[i];
2387 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2388 }
2389 if (p < c + BMAX) {
2390 *p++ = ';';
2391 }
2392 } else {
2393 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2394 }
2395 }
2397 /** \todo fixme: */
2398 if (style->stroke_dashoffset_set) {
2399 if (style->stroke_dashoffset_inherit) {
2400 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2401 } else {
2402 Inkscape::CSSOStringStream os;
2403 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2404 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2405 }
2406 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2407 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2408 }
2409 }
2411 bool marker_none = false;
2412 gchar *master = style->marker[SP_MARKER_LOC].value;
2413 if (style->marker[SP_MARKER_LOC].set) {
2414 p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2415 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2416 p += g_snprintf(p, c + BMAX - p, "marker:none;");
2417 marker_none = true;
2418 }
2419 if (style->marker[SP_MARKER_LOC_START].set
2420 && (!master || strcmp(master, style->marker[SP_MARKER_LOC_START].value))) {
2421 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2422 } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2423 p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2424 }
2425 if (style->marker[SP_MARKER_LOC_MID].set
2426 && (!master || strcmp(master, style->marker[SP_MARKER_LOC_MID].value))) {
2427 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2428 } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2429 p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2430 }
2431 if (style->marker[SP_MARKER_LOC_END].set
2432 && (!master || strcmp(master, style->marker[SP_MARKER_LOC_END].value))) {
2433 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2434 } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
2435 p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2436 }
2438 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2439 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2440 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2442 /* filter: */
2443 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
2445 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
2447 /* fixme: */
2448 p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2450 /* Get rid of trailing `;'. */
2451 if (p != c) {
2452 --p;
2453 if (*p == ';') {
2454 *p = '\0';
2455 }
2456 }
2458 return g_strdup(c);
2459 }
2462 #define STYLE_BUF_MAX
2465 /**
2466 * Dumps style to CSS string, see sp_style_write_string()
2467 *
2468 * \pre from != NULL.
2469 * \pre to != NULL.
2470 * \post ret != NULL.
2471 */
2472 gchar *
2473 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2474 {
2475 g_return_val_if_fail(from != NULL, NULL);
2476 g_return_val_if_fail(to != NULL, NULL);
2478 gchar c[BMAX], *p = c;
2479 *p = '\0';
2481 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2482 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2483 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2484 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2485 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2487 /* Text */
2488 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2489 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2490 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2491 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2492 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2493 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2494 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2495 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2496 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2497 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2499 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2500 p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &from->baseline_shift, &to->baseline_shift, SP_STYLE_FLAG_IFDIFF);
2502 /// \todo fixme: Per type methods need default flag too
2503 if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2504 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2505 }
2507 if (!from->color.noneSet) { // CSS does not permit "none" for color
2508 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2509 }
2511 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2512 // if fill:none, skip writing fill properties
2513 if (!from->fill.noneSet) {
2514 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2515 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2516 }
2518 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2520 // stroke width affects markers, so write it if there's stroke OR any markers
2521 if (!from->stroke.noneSet ||
2522 from->marker[SP_MARKER_LOC].set ||
2523 from->marker[SP_MARKER_LOC_START].set ||
2524 from->marker[SP_MARKER_LOC_MID].set ||
2525 from->marker[SP_MARKER_LOC_END].set) {
2526 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2527 }
2529 // if stroke:none, skip writing stroke properties
2530 if (!from->stroke.noneSet) {
2531 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2532 &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2533 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2534 &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2535 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2536 &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2537 /** \todo fixme: */
2538 if (from->stroke_dasharray_set) {
2539 if (from->stroke_dasharray_inherit) {
2540 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2541 } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2542 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2543 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2544 Inkscape::CSSOStringStream os;
2545 if (i) {
2546 os << ", ";
2547 }
2548 os << from->stroke_dash.dash[i];
2549 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2550 }
2551 p += g_snprintf(p, c + BMAX - p, ";");
2552 }
2553 }
2554 /* fixme: */
2555 if (from->stroke_dashoffset_set) {
2556 if (from->stroke_dashoffset_inherit) {
2557 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
2558 } else {
2559 Inkscape::CSSOStringStream os;
2560 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2561 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2562 }
2563 }
2564 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2565 }
2567 /* markers */
2568 gchar *master = from->marker[SP_MARKER_LOC].value;
2569 if (master != NULL) {
2570 p += g_snprintf(p, c + BMAX - p, "marker:%s;", master);
2571 }
2572 if (from->marker[SP_MARKER_LOC_START].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_START].value))) {
2573 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2574 }
2575 if (from->marker[SP_MARKER_LOC_MID].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_MID].value))) {
2576 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", from->marker[SP_MARKER_LOC_MID].value);
2577 }
2578 if (from->marker[SP_MARKER_LOC_END].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_END].value))) {
2579 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", from->marker[SP_MARKER_LOC_END].value);
2580 }
2582 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2583 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2584 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2586 /* filter: */
2587 p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
2589 p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
2591 p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2593 /** \todo
2594 * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2595 * flag is mainly only for attributes that don't handle explicit unset well.
2596 * We may need to revisit the behaviour of this routine.
2597 */
2599 /* Get rid of trailing `;'. */
2600 if (p != c) {
2601 --p;
2602 if (*p == ';') {
2603 *p = '\0';
2604 }
2605 }
2607 return g_strdup(c);
2608 }
2612 /**
2613 * Reset all style properties.
2614 */
2615 static void
2616 sp_style_clear(SPStyle *style)
2617 {
2618 g_return_if_fail(style != NULL);
2620 style->fill.clear();
2621 style->stroke.clear();
2622 sp_style_filter_clear(style);
2624 if (style->fill.value.href) {
2625 delete style->fill.value.href;
2626 style->fill.value.href = NULL;
2627 }
2628 if (style->stroke.value.href) {
2629 delete style->stroke.value.href;
2630 style->stroke.value.href = NULL;
2631 }
2632 if (style->filter.href) {
2633 delete style->filter.href;
2634 style->filter.href = NULL;
2635 }
2637 if (style->stroke_dash.dash) {
2638 g_free(style->stroke_dash.dash);
2639 }
2641 style->stroke_dasharray_inherit = FALSE;
2642 style->stroke_dashoffset_inherit = FALSE;
2644 /** \todo fixme: Do that text manipulation via parents */
2645 SPObject *object = style->object;
2646 SPDocument *document = style->document;
2647 gint const refcount = style->refcount;
2648 SPTextStyle *text = style->text;
2649 unsigned const text_private = style->text_private;
2651 memset(style, 0, sizeof(SPStyle));
2653 style->refcount = refcount;
2654 style->object = object;
2655 style->document = document;
2657 if (document) {
2658 style->filter.href = new SPFilterReference(document);
2659 style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
2661 style->fill.value.href = new SPPaintServerReference(document);
2662 style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
2664 style->stroke.value.href = new SPPaintServerReference(document);
2665 style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
2666 }
2668 style->text = text;
2669 style->text_private = text_private;
2671 style->text->font_specification.set = FALSE;
2672 style->text->font.set = FALSE;
2673 style->text->font_family.set = FALSE;
2675 style->font_size.set = FALSE;
2676 style->font_size.type = SP_FONT_SIZE_LITERAL;
2677 style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2678 style->font_size.computed = 12.0;
2679 style->font_style.set = FALSE;
2680 style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2681 style->font_variant.set = FALSE;
2682 style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2683 style->font_weight.set = FALSE;
2684 style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2685 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2686 style->font_stretch.set = FALSE;
2687 style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2689 /* text */
2690 style->text_indent.set = FALSE;
2691 style->text_indent.unit = SP_CSS_UNIT_NONE;
2692 style->text_indent.computed = 0.0;
2694 style->text_align.set = FALSE;
2695 style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2697 style->text_decoration.set = FALSE;
2698 style->text_decoration.underline = FALSE;
2699 style->text_decoration.overline = FALSE;
2700 style->text_decoration.line_through = FALSE;
2701 style->text_decoration.blink = FALSE;
2703 style->line_height.set = FALSE;
2704 style->line_height.unit = SP_CSS_UNIT_PERCENT;
2705 style->line_height.normal = TRUE;
2706 style->line_height.value = style->line_height.computed = 1.0;
2708 style->letter_spacing.set = FALSE;
2709 style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2710 style->letter_spacing.normal = TRUE;
2711 style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2713 style->word_spacing.set = FALSE;
2714 style->word_spacing.unit = SP_CSS_UNIT_NONE;
2715 style->word_spacing.normal = TRUE;
2716 style->word_spacing.value = style->word_spacing.computed = 0.0;
2718 style->baseline_shift.set = FALSE;
2719 style->baseline_shift.type = SP_BASELINE_SHIFT_LITERAL;
2720 style->baseline_shift.unit = SP_CSS_UNIT_NONE;
2721 style->baseline_shift.literal = SP_CSS_BASELINE_SHIFT_BASELINE;
2722 style->baseline_shift.value = 0.0;
2723 style->baseline_shift.computed = 0.0;
2725 style->text_transform.set = FALSE;
2726 style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2728 style->direction.set = FALSE;
2729 style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2731 style->block_progression.set = FALSE;
2732 style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2734 style->writing_mode.set = FALSE;
2735 style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2737 style->text_anchor.set = FALSE;
2738 style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2741 style->opacity.value = SP_SCALE24_MAX;
2742 style->visibility.set = FALSE;
2743 style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2744 style->display.set = FALSE;
2745 style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2746 style->overflow.set = FALSE;
2747 style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2749 style->color.clear();
2750 style->color.setColor(0.0, 0.0, 0.0);
2752 style->fill.clear();
2753 style->fill.setColor(0.0, 0.0, 0.0);
2754 style->fill_opacity.value = SP_SCALE24_MAX;
2755 style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2757 style->stroke.clear();
2758 style->stroke_opacity.value = SP_SCALE24_MAX;
2760 style->stroke_width.set = FALSE;
2761 style->stroke_width.unit = SP_CSS_UNIT_NONE;
2762 style->stroke_width.computed = 1.0;
2764 style->stroke_linecap.set = FALSE;
2765 style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2766 style->stroke_linejoin.set = FALSE;
2767 style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2769 style->stroke_miterlimit.set = FALSE;
2770 style->stroke_miterlimit.value = 4.0;
2772 style->stroke_dash.n_dash = 0;
2773 style->stroke_dash.dash = NULL;
2774 style->stroke_dash.offset = 0.0;
2776 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2777 g_free(style->marker[i].value);
2778 style->marker[i].set = FALSE;
2779 }
2781 style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
2782 style->enable_background.set = false;
2783 style->enable_background.inherit = false;
2784 }
2788 /**
2789 *
2790 */
2791 static void
2792 sp_style_read_dash(SPStyle *style, gchar const *str)
2793 {
2794 /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2795 style->stroke_dasharray_set = TRUE;
2797 if (strcmp(str, "inherit") == 0) {
2798 style->stroke_dasharray_inherit = true;
2799 return;
2800 }
2801 style->stroke_dasharray_inherit = false;
2803 NRVpathDash &dash = style->stroke_dash;
2804 g_free(dash.dash);
2805 dash.dash = NULL;
2807 if (strcmp(str, "none") == 0) {
2808 dash.n_dash = 0;
2809 return;
2810 }
2812 gint n_dash = 0;
2813 gdouble d[64];
2814 gchar *e = NULL;
2816 bool LineSolid=true;
2817 while (e != str && n_dash < 64) {
2818 /* TODO: Should allow <length> rather than just a unitless (px) number. */
2819 d[n_dash] = g_ascii_strtod(str, (char **) &e);
2820 if (d[n_dash] > 0.00000001)
2821 LineSolid = false;
2822 if (e != str) {
2823 n_dash += 1;
2824 str = e;
2825 }
2826 while (str && *str && !isalnum(*str)) str += 1;
2827 }
2829 if (LineSolid) {
2830 dash.n_dash = 0;
2831 return;
2832 }
2834 if (n_dash > 0) {
2835 dash.dash = g_new(gdouble, n_dash);
2836 memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2837 dash.n_dash = n_dash;
2838 }
2839 }
2842 /*#########################
2843 ## SPTextStyle operations
2844 #########################*/
2847 /**
2848 * Return new SPTextStyle object with default settings.
2849 */
2850 static SPTextStyle *
2851 sp_text_style_new()
2852 {
2853 SPTextStyle *ts = g_new0(SPTextStyle, 1);
2854 ts->refcount = 1;
2855 sp_text_style_clear(ts);
2857 ts->font_specification.value = g_strdup("Sans");
2858 ts->font.value = g_strdup("Sans");
2859 ts->font_family.value = g_strdup("Sans");
2861 return ts;
2862 }
2865 /**
2866 * Clear text style settings.
2867 */
2868 static void
2869 sp_text_style_clear(SPTextStyle *ts)
2870 {
2871 ts->font_specification.set = FALSE;
2872 ts->font.set = FALSE;
2873 ts->font_family.set = FALSE;
2874 }
2878 /**
2879 * Reduce refcount of text style and possibly free it.
2880 */
2881 static SPTextStyle *
2882 sp_text_style_unref(SPTextStyle *st)
2883 {
2884 st->refcount -= 1;
2886 if (st->refcount < 1) {
2887 g_free(st->font_specification.value);
2888 g_free(st->font.value);
2889 g_free(st->font_family.value);
2890 g_free(st);
2891 }
2893 return NULL;
2894 }
2898 /**
2899 * Return duplicate of text style.
2900 */
2901 static SPTextStyle *
2902 sp_text_style_duplicate_unset(SPTextStyle *st)
2903 {
2904 SPTextStyle *nt = g_new0(SPTextStyle, 1);
2905 nt->refcount = 1;
2907 nt->font_specification.value = g_strdup(st->font_specification.value);
2908 nt->font.value = g_strdup(st->font.value);
2909 nt->font_family.value = g_strdup(st->font_family.value);
2911 return nt;
2912 }
2916 /**
2917 * Write SPTextStyle object into string.
2918 */
2919 static guint
2920 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2921 {
2922 gint d = 0;
2924 // We do not do diffing for text style
2925 if (flags == SP_STYLE_FLAG_IFDIFF)
2926 flags = SP_STYLE_FLAG_IFSET;
2928 d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2929 d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
2930 return d;
2931 }
2935 /* The following sp_tyle_read_* functions ignore invalid values, as per
2936 * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2937 *
2938 * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2939 * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2940 * pairs, in which case SVG's error-handling rules
2941 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2942 */
2945 /**
2946 * Set SPIFloat object from string.
2947 */
2948 static void
2949 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2950 {
2951 if (!strcmp(str, "inherit")) {
2952 val->set = TRUE;
2953 val->inherit = TRUE;
2954 } else {
2955 gfloat value;
2956 if (sp_svg_number_read_f(str, &value)) {
2957 val->set = TRUE;
2958 val->inherit = FALSE;
2959 val->value = value;
2960 }
2961 }
2962 }
2966 /**
2967 * Set SPIScale24 object from string.
2968 */
2969 static void
2970 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2971 {
2972 if (!strcmp(str, "inherit")) {
2973 val->set = TRUE;
2974 val->inherit = TRUE;
2975 } else {
2976 gfloat value;
2977 if (sp_svg_number_read_f(str, &value)) {
2978 val->set = TRUE;
2979 val->inherit = FALSE;
2980 value = CLAMP(value, 0.0, 1.0);
2981 val->value = SP_SCALE24_FROM_FLOAT(value);
2982 }
2983 }
2984 }
2986 /**
2987 * Reads a style value and performs lookup based on the given style value enumerations.
2988 */
2989 static void
2990 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2991 bool const can_explicitly_inherit)
2992 {
2993 if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2994 val->set = TRUE;
2995 val->inherit = TRUE;
2996 } else {
2997 for (unsigned i = 0; dict[i].key; i++) {
2998 if (!strcmp(str, dict[i].key)) {
2999 val->set = TRUE;
3000 val->inherit = FALSE;
3001 val->value = dict[i].value;
3002 /* Save copying for values not needing it */
3003 val->computed = val->value;
3004 break;
3005 }
3006 }
3007 }
3008 }
3012 /**
3013 * Set SPIString object from string.
3014 */
3015 static void
3016 sp_style_read_istring(SPIString *val, gchar const *str)
3017 {
3018 g_free(val->value);
3020 if (!strcmp(str, "inherit")) {
3021 val->set = TRUE;
3022 val->inherit = TRUE;
3023 val->value = NULL;
3024 } else {
3025 val->set = TRUE;
3026 val->inherit = FALSE;
3027 val->value = g_strdup(str);
3028 }
3029 }
3033 /**
3034 * Set SPILength object from string.
3035 */
3036 static void
3037 sp_style_read_ilength(SPILength *val, gchar const *str)
3038 {
3039 if (!strcmp(str, "inherit")) {
3040 val->set = TRUE;
3041 val->inherit = TRUE;
3042 } else {
3043 gdouble value;
3044 gchar *e;
3045 /** \todo fixme: Move this to standard place (Lauris) */
3046 value = g_ascii_strtod(str, &e);
3047 if ((gchar const *) e != str) {
3048 /** \todo
3049 * Allow the number of px per inch to vary (document preferences,
3050 * X server or whatever). E.g. don't fill in computed here, do
3051 * it at the same time as percentage units are done.
3052 */
3053 if (!*e) {
3054 /* Userspace */
3055 val->unit = SP_CSS_UNIT_NONE;
3056 val->computed = value;
3057 } else if (!strcmp(e, "px")) {
3058 /* Userspace */
3059 val->unit = SP_CSS_UNIT_PX;
3060 val->computed = value;
3061 } else if (!strcmp(e, "pt")) {
3062 /* Userspace / DEVICESCALE */
3063 val->unit = SP_CSS_UNIT_PT;
3064 val->computed = value * PX_PER_PT;
3065 } else if (!strcmp(e, "pc")) {
3066 /* 1 pica = 12pt; FIXME: add it to SPUnit */
3067 val->unit = SP_CSS_UNIT_PC;
3068 val->computed = value * PX_PER_PT * 12;
3069 } else if (!strcmp(e, "mm")) {
3070 val->unit = SP_CSS_UNIT_MM;
3071 val->computed = value * PX_PER_MM;
3072 } else if (!strcmp(e, "cm")) {
3073 val->unit = SP_CSS_UNIT_CM;
3074 val->computed = value * PX_PER_CM;
3075 } else if (!strcmp(e, "in")) {
3076 val->unit = SP_CSS_UNIT_IN;
3077 val->computed = value * PX_PER_IN;
3078 } else if (!strcmp(e, "em")) {
3079 /* EM square */
3080 val->unit = SP_CSS_UNIT_EM;
3081 val->value = value;
3082 } else if (!strcmp(e, "ex")) {
3083 /* ex square */
3084 val->unit = SP_CSS_UNIT_EX;
3085 val->value = value;
3086 } else if (!strcmp(e, "%")) {
3087 /* Percentage */
3088 val->unit = SP_CSS_UNIT_PERCENT;
3089 val->value = value * 0.01;
3090 } else {
3091 /* Invalid */
3092 return;
3093 }
3094 val->set = TRUE;
3095 val->inherit = FALSE;
3096 }
3097 }
3098 }
3100 /**
3101 * Set SPILengthOrNormal object from string.
3102 */
3103 static void
3104 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
3105 {
3106 if (!strcmp(str, "normal")) {
3107 val->set = TRUE;
3108 val->inherit = FALSE;
3109 val->normal = TRUE;
3110 val->unit = SP_CSS_UNIT_NONE;
3111 val->value = val->computed = 0.0;
3112 } else {
3113 SPILength length;
3114 sp_style_read_ilength(&length, str);
3115 val->set = length.set;
3116 val->inherit = length.inherit;
3117 val->normal = FALSE;
3118 val->unit = length.unit;
3119 val->value = length.value;
3120 val->computed = length.computed;
3121 }
3122 }
3124 /**
3125 * Set SPITextDecoration object from string.
3126 */
3127 static void
3128 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
3129 {
3130 if (!strcmp(str, "inherit")) {
3131 val->set = TRUE;
3132 val->inherit = TRUE;
3133 } else if (!strcmp(str, "none")) {
3134 val->set = TRUE;
3135 val->inherit = FALSE;
3136 val->underline = FALSE;
3137 val->overline = FALSE;
3138 val->line_through = FALSE;
3139 val->blink = FALSE;
3140 } else {
3141 bool found_underline = false;
3142 bool found_overline = false;
3143 bool found_line_through = false;
3144 bool found_blink = false;
3145 for ( ; *str ; str++ ) {
3146 if (*str == ' ') continue;
3147 if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
3148 found_underline = true;
3149 str += 9;
3150 } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
3151 found_overline = true;
3152 str += 8;
3153 } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
3154 found_line_through = true;
3155 str += 12;
3156 } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
3157 found_blink = true;
3158 str += 5;
3159 } else {
3160 return; // invalid value
3161 }
3162 }
3163 if (!(found_underline || found_overline || found_line_through || found_blink)) {
3164 return; // invalid value: empty
3165 }
3166 val->set = TRUE;
3167 val->inherit = FALSE;
3168 val->underline = found_underline;
3169 val->overline = found_overline;
3170 val->line_through = found_line_through;
3171 val->blink = found_blink;
3172 }
3173 }
3175 /**
3176 * Set SPIPaint object from string containing an integer value.
3177 * \param document Ignored
3178 */
3179 static void
3180 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3181 {
3182 (void)style; // TODO
3183 (void)document; // TODO
3184 paint->currentcolor = FALSE; /* currentColor not a valid <color>. */
3185 if (!strcmp(str, "inherit")) {
3186 paint->set = TRUE;
3187 paint->inherit = TRUE;
3188 } else {
3189 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
3190 if (rgb0 != 0xff) {
3191 paint->setColor(rgb0);
3192 paint->set = TRUE;
3193 paint->inherit = FALSE;
3194 }
3195 }
3196 }
3199 /**
3200 * Set SPIPaint object from string.
3201 *
3202 * \pre paint == \&style.fill || paint == \&style.stroke.
3203 */
3204 static void
3205 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
3206 {
3207 while (g_ascii_isspace(*str)) {
3208 ++str;
3209 }
3211 paint->clear();
3213 if (streq(str, "inherit")) {
3214 paint->set = TRUE;
3215 paint->inherit = TRUE;
3216 } else {
3217 if ( strneq(str, "url", 3) ) {
3218 gchar *uri = extract_uri( str, &str );
3219 while ( g_ascii_isspace(*str) ) {
3220 ++str;
3221 }
3222 // TODO check on and comment the comparrison "paint != &style->color".
3223 if ( uri && *uri && (paint != &style->color) ) {
3224 paint->set = TRUE;
3226 // it may be that this style's SPIPaint has not yet created its URIReference;
3227 // now that we have a document, we can create it here
3228 if (!paint->value.href && document) {
3229 paint->value.href = new SPPaintServerReference(document);
3230 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));
3231 }
3233 // TODO check what this does in light of move away from union
3234 sp_style_set_ipaint_to_uri_string (style, paint, uri);
3235 }
3236 g_free( uri );
3237 }
3239 if (streq(str, "currentColor") && paint != &style->color) {
3240 paint->set = TRUE;
3241 paint->currentcolor = TRUE;
3242 } else if (streq(str, "none") && paint != &style->color) {
3243 paint->set = TRUE;
3244 paint->noneSet = TRUE;
3245 } else {
3246 guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
3247 if (rgb0 != 0xff) {
3248 paint->setColor( rgb0 );
3249 paint->set = TRUE;
3251 while (g_ascii_isspace(*str)) {
3252 ++str;
3253 }
3254 if (strneq(str, "icc-color(", 10)) {
3255 SVGICCColor* tmp = new SVGICCColor();
3256 if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
3257 delete tmp;
3258 tmp = 0;
3259 }
3260 paint->value.color.icc = tmp;
3261 }
3262 if (strneq(str, "device-gray(", 12) ||
3263 strneq(str, "device-rgb(", 11) ||
3264 strneq(str, "device-cmyk(", 12) ||
3265 strneq(str, "device-nchannel(", 16)) {
3266 SVGDeviceColor* tmp = new SVGDeviceColor();
3267 if ( ! sp_svg_read_device_color( str, &str, tmp ) ) {
3268 delete tmp;
3269 tmp = 0;
3270 }
3271 paint->value.color.device = tmp;
3272 }
3273 }
3274 }
3275 }
3276 }
3280 /**
3281 * Set SPIFontSize object from string.
3282 */
3283 static void
3284 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
3285 {
3286 if (!strcmp(str, "inherit")) {
3287 val->set = TRUE;
3288 val->inherit = TRUE;
3289 } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
3290 // xx-small, x-small, etc.
3291 for (unsigned i = 0; enum_font_size[i].key; i++) {
3292 if (!strcmp(str, enum_font_size[i].key)) {
3293 val->set = TRUE;
3294 val->inherit = FALSE;
3295 val->type = SP_FONT_SIZE_LITERAL;
3296 val->value = enum_font_size[i].value;
3297 return;
3298 }
3299 }
3300 /* Invalid */
3301 return;
3302 } else {
3303 gdouble value;
3304 gchar *e;
3305 /* fixme: Move this to standard place (Lauris) */
3306 value = g_ascii_strtod(str, &e);
3307 if ((gchar const *) e != str) {
3308 if (!*e) {
3309 /* Userspace */
3310 } else if (!strcmp(e, "px")) {
3311 /* Userspace */
3312 } else if (!strcmp(e, "pt")) {
3313 /* Userspace * DEVICESCALE */
3314 value *= PX_PER_PT;
3315 } else if (!strcmp(e, "pc")) {
3316 /* 12pt */
3317 value *= PX_PER_PT * 12.0;
3318 } else if (!strcmp(e, "mm")) {
3319 value *= PX_PER_MM;
3320 } else if (!strcmp(e, "cm")) {
3321 value *= PX_PER_CM;
3322 } else if (!strcmp(e, "in")) {
3323 value *= PX_PER_IN;
3324 } else if (!strcmp(e, "%")) {
3325 /* Percentage */
3326 val->set = TRUE;
3327 val->inherit = FALSE;
3328 val->type = SP_FONT_SIZE_PERCENTAGE;
3329 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
3330 return;
3331 } else {
3332 /* Invalid */
3333 return;
3334 }
3335 /* Length */
3336 val->set = TRUE;
3337 val->inherit = FALSE;
3338 val->type = SP_FONT_SIZE_LENGTH;
3339 val->computed = value;
3340 return;
3341 }
3342 }
3343 }
3346 /**
3347 * Set SPIBaselineShift object from string.
3348 */
3349 static void
3350 sp_style_read_ibaselineshift(SPIBaselineShift *val, gchar const *str)
3351 {
3352 if (!strcmp(str, "inherit")) {
3353 val->set = TRUE;
3354 val->inherit = TRUE;
3355 } else if ((*str == 'b') || (*str == 's')) {
3356 // baseline or sub or super
3357 for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
3358 if (!strcmp(str, enum_baseline_shift[i].key)) {
3359 val->set = TRUE;
3360 val->inherit = FALSE;
3361 val->type = SP_BASELINE_SHIFT_LITERAL;
3362 val->literal = enum_baseline_shift[i].value;
3363 return;
3364 }
3365 }
3366 /* Invalid */
3367 return;
3368 } else {
3369 SPILength length;
3370 sp_style_read_ilength(&length, str);
3371 val->set = length.set;
3372 val->inherit = length.inherit;
3373 val->unit = length.unit;
3374 val->value = length.value;
3375 val->computed = length.computed;
3376 if( val->unit == SP_CSS_UNIT_PERCENT ) {
3377 val->type = SP_BASELINE_SHIFT_PERCENTAGE;
3378 } else {
3379 val->type = SP_BASELINE_SHIFT_LENGTH;
3380 }
3381 return;
3382 }
3383 }
3386 /**
3387 * Set SPIFilter object from string.
3388 */
3389 static void
3390 sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
3391 {
3392 SPIFilter *f = &(style->filter);
3393 /* Try all possible values: inherit, none, uri */
3394 if (streq(str, "inherit")) {
3395 f->set = TRUE;
3396 f->inherit = TRUE;
3397 if (f->href && f->href->getObject())
3398 f->href->detach();
3399 } else if(streq(str, "none")) {
3400 f->set = TRUE;
3401 f->inherit = FALSE;
3402 if (f->href && f->href->getObject())
3403 f->href->detach();
3404 } else if (strneq(str, "url", 3)) {
3405 char *uri = extract_uri(str);
3406 if(uri == NULL || uri[0] == '\0') {
3407 g_warning("Specified filter url is empty");
3408 f->set = TRUE;
3409 f->inherit = FALSE;
3410 return;
3411 }
3412 f->set = TRUE;
3413 f->inherit = FALSE;
3414 if (f->href && f->href->getObject())
3415 f->href->detach();
3417 // it may be that this style has not yet created its SPFilterReference;
3418 // now that we have a document, we can create it here
3419 if (!f->href && document) {
3420 f->href = new SPFilterReference(document);
3421 f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
3422 }
3424 try {
3425 f->href->attach(Inkscape::URI(uri));
3426 } catch (Inkscape::BadURIException &e) {
3427 g_warning("%s", e.what());
3428 f->href->detach();
3429 }
3430 g_free (uri);
3432 } else {
3433 /* We shouldn't reach this if SVG input is well-formed */
3434 f->set = FALSE;
3435 f->inherit = FALSE;
3436 if (f->href && f->href->getObject())
3437 f->href->detach();
3438 }
3439 }
3441 /**
3442 * Set SPIEnum object from repr attribute.
3443 */
3444 static void
3445 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
3446 gchar const *key, SPStyleEnum const *dict,
3447 bool const can_explicitly_inherit)
3448 {
3449 gchar const *str = repr->attribute(key);
3450 if (str) {
3451 sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
3452 }
3453 }
3457 /**
3458 * Set SPILength object from repr attribute.
3459 */
3460 static void
3461 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
3462 {
3463 gchar const *str = repr->attribute(key);
3464 if (str) {
3465 sp_style_read_ilength(val, str);
3466 }
3467 }
3469 /**
3470 * Set SPIFontSize object from repr attribute.
3471 */
3472 static void
3473 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
3474 {
3475 gchar const *str = repr->attribute(key);
3476 if (str) {
3477 sp_style_read_ifontsize(val, str);
3478 }
3479 }
3482 /**
3483 * Set SPIBaselineShift object from repr attribute.
3484 */
3485 static void
3486 sp_style_read_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key)
3487 {
3488 gchar const *str = repr->attribute(key);
3489 if (str) {
3490 sp_style_read_ibaselineshift(val, str);
3491 }
3492 }
3495 /**
3496 * Set SPIFloat object from repr attribute.
3497 */
3498 static void
3499 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3500 {
3501 gchar const *str = repr->attribute(key);
3502 if (str) {
3503 sp_style_read_ifloat(val, str);
3504 }
3505 }
3508 /**
3509 * Write SPIFloat object into string.
3510 */
3511 static gint
3512 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3513 SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3514 {
3515 Inkscape::CSSOStringStream os;
3517 if ((flags & SP_STYLE_FLAG_ALWAYS)
3518 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3519 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3520 && (!base->set || (val->value != base->value))))
3521 {
3522 if (val->inherit) {
3523 return g_snprintf(p, len, "%s:inherit;", key);
3524 } else {
3525 os << key << ":" << val->value << ";";
3526 return g_strlcpy(p, os.str().c_str(), len);
3527 }
3528 }
3529 return 0;
3530 }
3533 /**
3534 * Write SPIScale24 object into string.
3535 */
3536 static gint
3537 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3538 SPIScale24 const *const val, SPIScale24 const *const base,
3539 guint const flags)
3540 {
3541 Inkscape::CSSOStringStream os;
3543 if ((flags & SP_STYLE_FLAG_ALWAYS)
3544 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3545 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3546 && (!base->set || (val->value != base->value))))
3547 {
3548 if (val->inherit) {
3549 return g_snprintf(p, len, "%s:inherit;", key);
3550 } else {
3551 os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3552 return g_strlcpy(p, os.str().c_str(), len);
3553 }
3554 }
3555 return 0;
3556 }
3559 /**
3560 * Write SPIEnum object into string.
3561 */
3562 static gint
3563 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3564 SPStyleEnum const *const dict,
3565 SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3566 {
3567 if ((flags & SP_STYLE_FLAG_ALWAYS)
3568 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3569 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3570 && (!base->set || (val->computed != base->computed))))
3571 {
3572 if (val->inherit) {
3573 return g_snprintf(p, len, "%s:inherit;", key);
3574 }
3575 for (unsigned i = 0; dict[i].key; i++) {
3576 if (dict[i].value == static_cast< gint > (val->value) ) {
3577 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3578 }
3579 }
3580 }
3581 return 0;
3582 }
3586 /**
3587 * Write SPIString object into string.
3588 */
3589 static gint
3590 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3591 SPIString const *const val, SPIString const *const base, guint const flags)
3592 {
3593 if ((flags & SP_STYLE_FLAG_ALWAYS)
3594 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3595 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3596 && (!base->set || strcmp(val->value, base->value))))
3597 {
3598 if (val->inherit) {
3599 return g_snprintf(p, len, "%s:inherit;", key);
3600 } else {
3601 gchar *val_quoted = css2_escape_quote(val->value);
3602 if (val_quoted) {
3603 return g_snprintf(p, len, "%s:%s;", key, val_quoted);
3604 g_free (val_quoted);
3605 }
3606 }
3607 }
3608 return 0;
3609 }
3612 /**
3613 *
3614 */
3615 static bool
3616 sp_length_differ(SPILength const *const a, SPILength const *const b)
3617 {
3618 if (a->unit != b->unit) {
3619 if (a->unit == SP_CSS_UNIT_EM) return true;
3620 if (a->unit == SP_CSS_UNIT_EX) return true;
3621 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3622 if (b->unit == SP_CSS_UNIT_EM) return true;
3623 if (b->unit == SP_CSS_UNIT_EX) return true;
3624 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3625 }
3627 return (a->computed != b->computed);
3628 }
3632 /**
3633 * Write SPILength object into string.
3634 */
3635 static gint
3636 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3637 SPILength const *const val, SPILength const *const base, guint const flags)
3638 {
3639 Inkscape::CSSOStringStream os;
3641 if ((flags & SP_STYLE_FLAG_ALWAYS)
3642 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3643 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3644 && (!base->set || sp_length_differ(val, base))))
3645 {
3646 if (val->inherit) {
3647 return g_snprintf(p, len, "%s:inherit;", key);
3648 } else {
3649 switch (val->unit) {
3650 case SP_CSS_UNIT_NONE:
3651 os << key << ":" << val->computed << ";";
3652 return g_strlcpy(p, os.str().c_str(), len);
3653 break;
3654 case SP_CSS_UNIT_PX:
3655 os << key << ":" << val->computed << "px;";
3656 return g_strlcpy(p, os.str().c_str(), len);
3657 break;
3658 case SP_CSS_UNIT_PT:
3659 os << key << ":" << val->computed * PT_PER_PX << "pt;";
3660 return g_strlcpy(p, os.str().c_str(), len);
3661 break;
3662 case SP_CSS_UNIT_PC:
3663 os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3664 return g_strlcpy(p, os.str().c_str(), len);
3665 break;
3666 case SP_CSS_UNIT_MM:
3667 os << key << ":" << val->computed * MM_PER_PX << "mm;";
3668 return g_strlcpy(p, os.str().c_str(), len);
3669 break;
3670 case SP_CSS_UNIT_CM:
3671 os << key << ":" << val->computed * CM_PER_PX << "cm;";
3672 return g_strlcpy(p, os.str().c_str(), len);
3673 break;
3674 case SP_CSS_UNIT_IN:
3675 os << key << ":" << val->computed * IN_PER_PX << "in;";
3676 return g_strlcpy(p, os.str().c_str(), len);
3677 break;
3678 case SP_CSS_UNIT_EM:
3679 os << key << ":" << val->value << "em;";
3680 return g_strlcpy(p, os.str().c_str(), len);
3681 break;
3682 case SP_CSS_UNIT_EX:
3683 os << key << ":" << val->value << "ex;";
3684 return g_strlcpy(p, os.str().c_str(), len);
3685 break;
3686 case SP_CSS_UNIT_PERCENT:
3687 os << key << ":" << (val->value * 100.0) << "%;";
3688 return g_strlcpy(p, os.str().c_str(), len);
3689 break;
3690 default:
3691 /* Invalid */
3692 break;
3693 }
3694 }
3695 }
3696 return 0;
3697 }
3700 /**
3701 *
3702 */
3703 static bool
3704 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3705 {
3706 if (a->normal != b->normal) return true;
3707 if (a->normal) return false;
3709 if (a->unit != b->unit) {
3710 if (a->unit == SP_CSS_UNIT_EM) return true;
3711 if (a->unit == SP_CSS_UNIT_EX) return true;
3712 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3713 if (b->unit == SP_CSS_UNIT_EM) return true;
3714 if (b->unit == SP_CSS_UNIT_EX) return true;
3715 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3716 }
3718 return (a->computed != b->computed);
3719 }
3721 /**
3722 * Write SPILengthOrNormal object into string.
3723 */
3724 static gint
3725 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3726 SPILengthOrNormal const *const val,
3727 SPILengthOrNormal const *const base,
3728 guint const flags)
3729 {
3730 if ((flags & SP_STYLE_FLAG_ALWAYS)
3731 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3732 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3733 && (!base->set || sp_lengthornormal_differ(val, base))))
3734 {
3735 if (val->normal) {
3736 return g_snprintf(p, len, "%s:normal;", key);
3737 } else {
3738 SPILength length;
3739 length.set = val->set;
3740 length.inherit = val->inherit;
3741 length.unit = val->unit;
3742 length.value = val->value;
3743 length.computed = val->computed;
3744 return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3745 }
3746 }
3747 return 0;
3748 }
3750 /**
3751 *
3752 */
3753 static bool
3754 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3755 {
3756 return a->underline != b->underline
3757 || a->overline != b->overline
3758 || a->line_through != b->line_through
3759 || a->blink != b->blink;
3760 }
3762 /**
3763 * Write SPITextDecoration object into string.
3764 */
3765 static gint
3766 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3767 SPITextDecoration const *const val,
3768 SPITextDecoration const *const base,
3769 guint const flags)
3770 {
3771 Inkscape::CSSOStringStream os;
3773 if ((flags & SP_STYLE_FLAG_ALWAYS)
3774 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3775 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3776 && (!base->set || sp_textdecoration_differ(val, base))))
3777 {
3778 if (val->inherit) {
3779 return g_snprintf(p, len, "%s:inherit;", key);
3780 } else {
3781 os << key << ":";
3782 if (val->underline || val->overline || val->line_through || val->blink) {
3783 if (val->underline) os << " underline";
3784 if (val->overline) os << " overline";
3785 if (val->line_through) os << " line-through";
3786 if (val->blink) os << " blink";
3787 } else
3788 os << "none";
3789 os << ";";
3790 return g_strlcpy(p, os.str().c_str(), len);
3791 }
3792 }
3793 return 0;
3794 }
3796 /**
3797 *
3798 */
3799 static bool
3800 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3801 {
3802 if ( (a->isColor() != b->isColor())
3803 || (a->isPaintserver() != b->isPaintserver())
3804 || (a->set != b->set)
3805 || (a->currentcolor != b->currentcolor)
3806 || (a->inherit!= b->inherit) ) {
3807 return true;
3808 }
3810 // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
3812 if ( a->isPaintserver() ) {
3813 return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
3814 }
3816 if ( a->isColor() ) {
3817 return !( (a->value.color == b->value.color)
3818 && ((a->value.color.icc == b->value.color.icc)
3819 || (a->value.color.icc && b->value.color.icc
3820 && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
3821 && (a->value.color.icc->colors == b->value.color.icc->colors))));
3822 /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
3823 * in the string representation. */
3824 }
3826 return false;
3827 }
3831 /**
3832 * Write SPIPaint object into string.
3833 */
3834 static gint
3835 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3836 SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3837 {
3838 int retval = 0;
3840 if ((flags & SP_STYLE_FLAG_ALWAYS)
3841 || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3842 || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3843 && (!base->set || sp_paint_differ(paint, base))))
3844 {
3845 CSSOStringStream css;
3847 if (paint->inherit) {
3848 css << "inherit";
3849 } else {
3850 if ( paint->value.href && paint->value.href->getURI() ) {
3851 const gchar* uri = paint->value.href->getURI()->toString();
3852 css << "url(" << uri << ")";
3853 }
3855 if ( paint->noneSet ) {
3856 if ( !css.str().empty() ) {
3857 css << " ";
3858 }
3859 css << "none";
3860 }
3862 if ( paint->currentcolor ) {
3863 if ( !css.str().empty() ) {
3864 css << " ";
3865 }
3866 css << "currentColor";
3867 }
3869 if ( paint->colorSet && !paint->currentcolor ) {
3870 if ( !css.str().empty() ) {
3871 css << " ";
3872 }
3873 char color_buf[8];
3874 sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
3875 css << color_buf;
3876 }
3878 if (paint->value.color.icc && !paint->currentcolor) {
3879 if ( !css.str().empty() ) {
3880 css << " ";
3881 }
3882 css << "icc-color(" << paint->value.color.icc->colorProfile;
3883 for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
3884 iEnd(paint->value.color.icc->colors.end());
3885 i != iEnd; ++i) {
3886 css << ", " << *i;
3887 }
3888 css << ')';
3889 }
3890 }
3892 if ( !css.str().empty() ) {
3893 retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
3894 }
3895 }
3897 return retval;
3898 }
3901 /**
3902 *
3903 */
3904 static bool
3905 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3906 {
3907 if (a->type != b->type)
3908 return true;
3909 if (a->type == SP_FONT_SIZE_LENGTH) {
3910 if (a->computed != b->computed)
3911 return true;
3912 } else {
3913 if (a->value != b->value)
3914 return true;
3915 }
3916 return false;
3917 }
3920 /**
3921 * Write SPIFontSize object into string.
3922 */
3923 static gint
3924 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3925 SPIFontSize const *const val, SPIFontSize const *const base,
3926 guint const flags)
3927 {
3928 if ((flags & SP_STYLE_FLAG_ALWAYS)
3929 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3930 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3931 && (!base->set || sp_fontsize_differ(val, base))))
3932 {
3933 if (val->inherit) {
3934 return g_snprintf(p, len, "%s:inherit;", key);
3935 } else if (val->type == SP_FONT_SIZE_LITERAL) {
3936 for (unsigned i = 0; enum_font_size[i].key; i++) {
3937 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3938 return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3939 }
3940 }
3941 } else if (val->type == SP_FONT_SIZE_LENGTH) {
3942 Inkscape::CSSOStringStream os;
3943 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
3944 return g_strlcpy(p, os.str().c_str(), len);
3945 } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3946 Inkscape::CSSOStringStream os;
3947 os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3948 return g_strlcpy(p, os.str().c_str(), len);
3949 }
3950 }
3951 return 0;
3952 }
3955 /*
3956 * baseline-shift is relative to parent. The only time it should
3957 * not be written out is if it is zero (or not set).
3958 */
3959 static bool
3960 sp_baseline_shift_notzero(SPIBaselineShift const *const a )
3961 {
3962 if( a->type == SP_BASELINE_SHIFT_LITERAL ) {
3963 if( a->literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
3964 return false;
3965 }
3966 } else {
3967 if( a->value == 0.0 ) {
3968 return false;
3969 }
3970 }
3971 return true;
3972 }
3974 /**
3975 * Write SPIBaselineShift object into string.
3976 */
3977 static gint
3978 sp_style_write_ibaselineshift(gchar *p, gint const len, gchar const *key,
3979 SPIBaselineShift const *const val, SPIBaselineShift const *const base,
3980 guint const flags)
3981 {
3982 if ((flags & SP_STYLE_FLAG_ALWAYS)
3983 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3984 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3985 && (!base->set || sp_baseline_shift_notzero(val) )))
3986 {
3987 if (val->inherit) {
3988 return g_snprintf(p, len, "%s:inherit;", key);
3989 } else if (val->type == SP_BASELINE_SHIFT_LITERAL) {
3990 for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
3991 if (enum_baseline_shift[i].value == static_cast< gint > (val->literal) ) {
3992 return g_snprintf(p, len, "%s:%s;", key, enum_baseline_shift[i].key);
3993 }
3994 }
3995 } else if (val->type == SP_BASELINE_SHIFT_LENGTH) {
3996 if( val->unit == SP_CSS_UNIT_EM || val->unit == SP_CSS_UNIT_EX ) {
3997 Inkscape::CSSOStringStream os;
3998 os << key << ":" << val->value << (val->unit == SP_CSS_UNIT_EM ? "em;" : "ex;");
3999 return g_strlcpy(p, os.str().c_str(), len);
4000 } else {
4001 Inkscape::CSSOStringStream os;
4002 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
4003 return g_strlcpy(p, os.str().c_str(), len);
4004 }
4005 } else if (val->type == SP_BASELINE_SHIFT_PERCENTAGE) {
4006 Inkscape::CSSOStringStream os;
4007 os << key << ":" << (val->value * 100.0) << "%;";
4008 return g_strlcpy(p, os.str().c_str(), len);
4009 }
4010 }
4011 return 0;
4012 }
4016 /**
4017 * Write SPIFilter object into string.
4018 */
4019 static gint
4020 sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
4021 SPIFilter const *const val, SPIFilter const *const base,
4022 guint const flags)
4023 {
4024 (void)base; // TODO
4025 if ((flags & SP_STYLE_FLAG_ALWAYS)
4026 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
4027 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
4028 {
4029 if (val->inherit) {
4030 return g_snprintf(p, len, "%s:inherit;", key);
4031 } else if (val->href && val->href->getURI()) {
4032 return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
4033 }
4034 }
4037 return 0;
4038 }
4041 void SPIPaint::clear()
4042 {
4043 set = false;
4044 inherit = false;
4045 currentcolor = false;
4046 colorSet = false;
4047 noneSet = false;
4048 value.color.set( 0 );
4049 if ( value.href && value.href->getObject() )
4050 {
4051 value.href->detach();
4052 }
4053 }
4056 /**
4057 * Clear filter object, and disconnect style from paintserver (if present).
4058 */
4059 static void
4060 sp_style_filter_clear(SPStyle *style)
4061 {
4062 if (style->filter.href && style->filter.href->getObject())
4063 style->filter.href->detach();
4064 }
4067 // FIXME: Everything below this line belongs in a different file - css-chemistry?
4069 void
4070 sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
4071 {
4072 Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
4074 if (repr == NULL) return;
4076 SPCSSAttr *css = sp_repr_css_attr_new();
4077 if (linked) {
4078 gchar *val = g_strdup_printf("url(#%s)", linked->getId());
4079 sp_repr_css_set_property(css, property, val);
4080 g_free(val);
4081 } else {
4082 sp_repr_css_unset_property(css, "filter");
4083 }
4085 if (recursive) {
4086 sp_repr_css_change_recursive(repr, css, "style");
4087 } else {
4088 sp_repr_css_change(repr, css, "style");
4089 }
4090 sp_repr_css_attr_unref(css);
4091 }
4094 /**
4095 * Clear all style property attributes in object.
4096 */
4097 void
4098 sp_style_unset_property_attrs(SPObject *o)
4099 {
4100 if (!o) return;
4102 SPStyle *style = SP_OBJECT_STYLE(o);
4103 if (!style) return;
4105 Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
4106 if (!repr) return;
4108 if (style->opacity.set) {
4109 repr->setAttribute("opacity", NULL);
4110 }
4111 if (style->color.set) {
4112 repr->setAttribute("color", NULL);
4113 }
4114 if (style->fill.set) {
4115 repr->setAttribute("fill", NULL);
4116 }
4117 if (style->fill_opacity.set) {
4118 repr->setAttribute("fill-opacity", NULL);
4119 }
4120 if (style->fill_rule.set) {
4121 repr->setAttribute("fill-rule", NULL);
4122 }
4123 if (style->stroke.set) {
4124 repr->setAttribute("stroke", NULL);
4125 }
4126 if (style->stroke_width.set) {
4127 repr->setAttribute("stroke-width", NULL);
4128 }
4129 if (style->stroke_linecap.set) {
4130 repr->setAttribute("stroke-linecap", NULL);
4131 }
4132 if (style->stroke_linejoin.set) {
4133 repr->setAttribute("stroke-linejoin", NULL);
4134 }
4135 if (style->marker[SP_MARKER_LOC].set) {
4136 repr->setAttribute("marker", NULL);
4137 }
4138 if (style->marker[SP_MARKER_LOC_START].set) {
4139 repr->setAttribute("marker-start", NULL);
4140 }
4141 if (style->marker[SP_MARKER_LOC_MID].set) {
4142 repr->setAttribute("marker-mid", NULL);
4143 }
4144 if (style->marker[SP_MARKER_LOC_END].set) {
4145 repr->setAttribute("marker-end", NULL);
4146 }
4147 if (style->stroke_opacity.set) {
4148 repr->setAttribute("stroke-opacity", NULL);
4149 }
4150 if (style->stroke_dasharray_set) {
4151 repr->setAttribute("stroke-dasharray", NULL);
4152 }
4153 if (style->stroke_dashoffset_set) {
4154 repr->setAttribute("stroke-dashoffset", NULL);
4155 }
4156 if (style->text_private && style->text->font_specification.set) {
4157 repr->setAttribute("-inkscape-font-specification", NULL);
4158 }
4159 if (style->text_private && style->text->font_family.set) {
4160 repr->setAttribute("font-family", NULL);
4161 }
4162 if (style->text_anchor.set) {
4163 repr->setAttribute("text-anchor", NULL);
4164 }
4165 if (style->writing_mode.set) {
4166 repr->setAttribute("writing_mode", NULL);
4167 }
4168 if (style->filter.set) {
4169 repr->setAttribute("filter", NULL);
4170 }
4171 if (style->enable_background.set) {
4172 repr->setAttribute("enable-background", NULL);
4173 }
4174 }
4176 /**
4177 * \pre style != NULL.
4178 * \pre flags in {IFSET, ALWAYS}.
4179 */
4180 SPCSSAttr *
4181 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
4182 {
4183 g_return_val_if_fail(style != NULL, NULL);
4184 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4185 (flags == SP_STYLE_FLAG_ALWAYS) ),
4186 NULL);
4187 gchar *style_str = sp_style_write_string(style, flags);
4188 SPCSSAttr *css = sp_repr_css_attr_new();
4189 sp_repr_css_attr_add_from_string(css, style_str);
4190 g_free(style_str);
4191 return css;
4192 }
4195 /**
4196 * \pre object != NULL
4197 * \pre flags in {IFSET, ALWAYS}.
4198 */
4199 SPCSSAttr *
4200 sp_css_attr_from_object(SPObject *object, guint const flags)
4201 {
4202 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
4203 (flags == SP_STYLE_FLAG_ALWAYS) ),
4204 NULL);
4205 SPStyle const *const style = SP_OBJECT_STYLE(object);
4206 if (style == NULL)
4207 return NULL;
4208 return sp_css_attr_from_style(style, flags);
4209 }
4211 /**
4212 * Unset any text-related properties
4213 */
4214 SPCSSAttr *
4215 sp_css_attr_unset_text(SPCSSAttr *css)
4216 {
4217 sp_repr_css_set_property(css, "font", NULL); // not implemented yet
4218 sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
4219 sp_repr_css_set_property(css, "font-size", NULL);
4220 sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
4221 sp_repr_css_set_property(css, "font-style", NULL);
4222 sp_repr_css_set_property(css, "font-variant", NULL);
4223 sp_repr_css_set_property(css, "font-weight", NULL);
4224 sp_repr_css_set_property(css, "font-stretch", NULL);
4225 sp_repr_css_set_property(css, "font-family", NULL);
4226 sp_repr_css_set_property(css, "text-indent", NULL);
4227 sp_repr_css_set_property(css, "text-align", NULL);
4228 sp_repr_css_set_property(css, "text-decoration", NULL);
4229 sp_repr_css_set_property(css, "line-height", NULL);
4230 sp_repr_css_set_property(css, "letter-spacing", NULL);
4231 sp_repr_css_set_property(css, "word-spacing", NULL);
4232 sp_repr_css_set_property(css, "text-transform", NULL);
4233 sp_repr_css_set_property(css, "direction", NULL);
4234 sp_repr_css_set_property(css, "block-progression", NULL);
4235 sp_repr_css_set_property(css, "writing-mode", NULL);
4236 sp_repr_css_set_property(css, "text-anchor", NULL);
4237 sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
4238 sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
4239 sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
4240 sp_repr_css_set_property(css, "baseline-shift", NULL);
4242 return css;
4243 }
4245 bool
4246 is_url(char const *p)
4247 {
4248 if (p == NULL)
4249 return false;
4250 /** \todo
4251 * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
4252 * in property values must start with 'url('.
4253 */
4254 return (g_ascii_strncasecmp(p, "url(", 4) == 0);
4255 }
4257 /**
4258 * Unset any properties that contain URI values.
4259 *
4260 * Used for storing style that will be reused across documents when carrying
4261 * the referenced defs is impractical.
4262 */
4263 SPCSSAttr *
4264 sp_css_attr_unset_uris(SPCSSAttr *css)
4265 {
4266 // All properties that may hold <uri> or <paint> according to SVG 1.1
4267 if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
4268 if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
4269 if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
4270 if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
4271 if (is_url(sp_repr_css_property(css, "marker", NULL))) sp_repr_css_set_property(css, "marker", NULL);
4272 if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
4273 if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
4274 if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
4275 if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
4276 if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
4277 if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
4279 return css;
4280 }
4282 /**
4283 * Scale a single-value property.
4284 */
4285 void
4286 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
4287 double ex, bool only_with_units = false)
4288 {
4289 gchar const *w = sp_repr_css_property(css, property, NULL);
4290 if (w) {
4291 gchar *units = NULL;
4292 double wd = g_ascii_strtod(w, &units) * ex;
4293 if (w == units) {// nothing converted, non-numeric value
4294 return;
4295 }
4296 if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
4297 // only_with_units, but no units found, so do nothing.
4298 return;
4299 }
4300 Inkscape::CSSOStringStream os;
4301 os << wd << units; // reattach units
4302 sp_repr_css_set_property(css, property, os.str().c_str());
4303 }
4304 }
4306 /**
4307 * Scale a list-of-values property.
4308 */
4309 void
4310 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
4311 {
4312 gchar const *string = sp_repr_css_property(css, property, NULL);
4313 if (string) {
4314 Inkscape::CSSOStringStream os;
4315 gchar **a = g_strsplit(string, ",", 10000);
4316 bool first = true;
4317 for (gchar **i = a; i != NULL; i++) {
4318 gchar *w = *i;
4319 if (w == NULL)
4320 break;
4321 gchar *units = NULL;
4322 double wd = g_ascii_strtod(w, &units) * ex;
4323 if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
4324 g_strfreev(a);
4325 return;
4326 }
4327 if (!first) {
4328 os << ",";
4329 }
4330 os << wd << units; // reattach units
4331 first = false;
4332 }
4333 sp_repr_css_set_property(css, property, os.str().c_str());
4334 g_strfreev(a);
4335 }
4336 }
4338 /**
4339 * Scale any properties that may hold <length> by ex.
4340 */
4341 SPCSSAttr *
4342 sp_css_attr_scale(SPCSSAttr *css, double ex)
4343 {
4344 sp_css_attr_scale_property_single(css, "baseline-shift", ex);
4345 sp_css_attr_scale_property_single(css, "stroke-width", ex);
4346 sp_css_attr_scale_property_list (css, "stroke-dasharray", ex);
4347 sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
4348 sp_css_attr_scale_property_single(css, "font-size", ex);
4349 sp_css_attr_scale_property_single(css, "kerning", ex);
4350 sp_css_attr_scale_property_single(css, "letter-spacing", ex);
4351 sp_css_attr_scale_property_single(css, "word-spacing", ex);
4352 sp_css_attr_scale_property_single(css, "line-height", ex, true);
4354 return css;
4355 }
4358 /**
4359 * Remove quotes and escapes from a string. Returned value must be g_free'd.
4360 * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
4361 * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
4362 * it parsed for us. So this function is only used to remove those quotes and for
4363 * presentation attributes, without any unescaping. (XML unescaping
4364 * (& etc) is done by XML parser.)
4365 */
4366 gchar *
4367 attribute_unquote(gchar const *val)
4368 {
4369 if (val) {
4370 if (*val == '\'' || *val == '"') {
4371 int l = strlen(val);
4372 if (l >= 2) {
4373 if ( ( val[0] == '"' && val[l - 1] == '"' ) ||
4374 ( val[0] == '\'' && val[l - 1] == '\'' ) ) {
4375 return (g_strndup (val+1, l-2));
4376 }
4377 }
4378 }
4379 }
4381 return (val? g_strdup (val) : NULL);
4382 }
4384 /**
4385 * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
4386 */
4387 gchar *
4388 css2_escape_quote(gchar const *val) {
4390 Glib::ustring t;
4391 bool quote = false;
4392 bool last_was_space = false;
4394 for (gchar const *i = val; *i; i++) {
4395 bool is_space = ( *i == ' ' );
4396 if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
4397 // ASCII alphanumeric, - and _ don't require quotes
4398 t.push_back(*i);
4399 } else if ( is_space && !last_was_space ) {
4400 // non-consecutive spaces don't require quotes
4401 t.push_back(*i);
4402 } else if (*i=='\'') {
4403 // single quotes require escaping and quotes
4404 t.push_back('\\');
4405 t.push_back(*i);
4406 quote = true;
4407 } else {
4408 // everything else requires quotes
4409 t.push_back(*i);
4410 quote = true;
4411 }
4412 if (i == val && !g_ascii_isalpha(*i)) {
4413 // a non-ASCII/non-alpha initial character requires quotes
4414 quote = true;
4415 }
4416 last_was_space = is_space;
4417 }
4419 if (last_was_space) {
4420 // a trailing space requires quotes
4421 quote = true;
4422 }
4424 if (quote) {
4425 // we use single quotes so the result can be stored in an XML
4426 // attribute without incurring un-aesthetic additional quoting
4427 // (our XML emitter always uses double quotes)
4428 t.insert(t.begin(), '\'');
4429 t.push_back('\'');
4430 }
4432 return (t.empty() ? NULL : g_strdup (t.c_str()));
4433 }
4435 /*
4436 Local Variables:
4437 mode:c++
4438 c-file-style:"stroustrup"
4439 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
4440 indent-tabs-mode:nil
4441 fill-column:99
4442 End:
4443 */
4444 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :