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