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