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