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