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 "libcroco/cr-sel-eng.h"
23 #include "xml/croco-node-iface.h"
25 #include "svg/svg.h"
27 #include "display/canvas-bpath.h"
28 #include "attributes.h"
29 #include "document.h"
30 #include "extract-uri.h"
31 #include "marker-status.h"
32 #include "uri-references.h"
33 #include "sp-paint-server.h"
34 #include "streq.h"
35 #include "strneq.h"
36 #include "style.h"
37 #include "svg/css-ostringstream.h"
38 #include "xml/repr.h"
39 #include "unit-constants.h"
40 #include "isnan.h"
42 namespace Inkscape {
44 /**
45 * Parses a CSS url() specification; temporary hack until
46 * style stuff is redone.
47 * \param string the CSS string to parse
48 * \return a newly-allocated URL string (or NULL); free with g_free()
49 */
50 gchar *parse_css_url(gchar const *string) {
51 if (!string)
52 return NULL;
54 gchar const *iter = string;
55 for ( ; g_ascii_isspace(*iter) ; iter = g_utf8_next_char(iter) );
56 if (strncmp(iter, "url(", 4))
57 return NULL;
58 iter += 4;
60 gchar const end_char = *iter;
61 if ( *iter == '"' || *iter == '\'' ) {
62 iter += 1;
63 }
65 GString *temp = g_string_new(NULL);
66 for ( ; *iter ; iter = g_utf8_next_char(iter) ) {
67 if ( *iter == '(' || *iter == ')' ||
68 *iter == '"' || *iter == '\'' ||
69 g_ascii_isspace(*iter) ||
70 g_ascii_iscntrl(*iter) )
71 {
72 break;
73 }
74 if ( *iter == '\\' ) {
75 iter = g_utf8_next_char(iter);
76 }
77 if ( *iter & (gchar)0x80 ) {
78 break;
79 } else {
80 g_string_append_c(temp, *iter);
81 }
82 }
84 if ( *iter == end_char && end_char != ')' ) {
85 iter = g_utf8_next_char(iter);
86 }
87 gchar *result;
88 if ( *iter == ')' ) {
89 result = temp->str;
90 g_string_free(temp, FALSE);
91 } else {
92 result = NULL;
93 g_string_free(temp, TRUE);
94 }
96 return result;
97 }
99 }
101 #define BMAX 8192
103 class SPStyleEnum;
105 /*#########################
106 ## FORWARD DECLARATIONS
107 #########################*/
108 static void sp_style_clear(SPStyle *style);
110 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
112 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
113 static void sp_style_read_dash(SPStyle *style, gchar const *str);
115 static SPTextStyle *sp_text_style_new(void);
116 static void sp_text_style_clear(SPTextStyle *ts);
117 static SPTextStyle *sp_text_style_unref(SPTextStyle *st);
118 static SPTextStyle *sp_text_style_duplicate_unset(SPTextStyle *st);
119 static guint sp_text_style_write(gchar *p, guint len, SPTextStyle const *st, guint flags = SP_STYLE_FLAG_IFSET);
120 static void sp_style_privatize_text(SPStyle *style);
122 static void sp_style_read_ifloat(SPIFloat *val, gchar const *str);
123 static void sp_style_read_iscale24(SPIScale24 *val, gchar const *str);
124 static void sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict, bool can_explicitly_inherit);
125 static void sp_style_read_istring(SPIString *val, gchar const *str);
126 static void sp_style_read_ilength(SPILength *val, gchar const *str);
127 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
128 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
129 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
130 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
131 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
133 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
134 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
135 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
136 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
138 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
139 static gint sp_style_write_iscale24(gchar *p, gint len, gchar const *key, SPIScale24 const *val, SPIScale24 const *base, guint flags);
140 static gint sp_style_write_ienum(gchar *p, gint len, gchar const *key, SPStyleEnum const *dict, SPIEnum const *val, SPIEnum const *base, guint flags);
141 static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIString const *val, SPIString const *base, guint flags);
142 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
143 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
144 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
145 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);
146 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);
148 void css2_unescape_unquote (SPIString *val);
150 static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint, unsigned hunref, unsigned unset);
152 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
153 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
155 #define SPS_READ_ILENGTH_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ilength((v), (s));}
156 #define SPS_READ_PLENGTH_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_plength((v), (r), (k));}
158 #define SPS_READ_PFLOAT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfloat((v), (r), (k));}
160 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
161 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
163 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
165 struct SPStyleEnum {
166 gchar const *key;
167 gint value;
168 };
170 static SPStyleEnum const enum_fill_rule[] = {
171 {"nonzero", SP_WIND_RULE_NONZERO},
172 {"evenodd", SP_WIND_RULE_EVENODD},
173 {NULL, -1}
174 };
176 static SPStyleEnum const enum_stroke_linecap[] = {
177 {"butt", SP_STROKE_LINECAP_BUTT},
178 {"round", SP_STROKE_LINECAP_ROUND},
179 {"square", SP_STROKE_LINECAP_SQUARE},
180 {NULL, -1}
181 };
183 static SPStyleEnum const enum_stroke_linejoin[] = {
184 {"miter", SP_STROKE_LINEJOIN_MITER},
185 {"round", SP_STROKE_LINEJOIN_ROUND},
186 {"bevel", SP_STROKE_LINEJOIN_BEVEL},
187 {NULL, -1}
188 };
190 static SPStyleEnum const enum_font_style[] = {
191 {"normal", SP_CSS_FONT_STYLE_NORMAL},
192 {"italic", SP_CSS_FONT_STYLE_ITALIC},
193 {"oblique", SP_CSS_FONT_STYLE_OBLIQUE},
194 {NULL, -1}
195 };
197 static SPStyleEnum const enum_font_size[] = {
198 {"xx-small", SP_CSS_FONT_SIZE_XX_SMALL},
199 {"x-small", SP_CSS_FONT_SIZE_X_SMALL},
200 {"small", SP_CSS_FONT_SIZE_SMALL},
201 {"medium", SP_CSS_FONT_SIZE_MEDIUM},
202 {"large", SP_CSS_FONT_SIZE_LARGE},
203 {"x-large", SP_CSS_FONT_SIZE_X_LARGE},
204 {"xx-large", SP_CSS_FONT_SIZE_XX_LARGE},
205 {"smaller", SP_CSS_FONT_SIZE_SMALLER},
206 {"larger", SP_CSS_FONT_SIZE_LARGER},
207 {NULL, -1}
208 };
210 static SPStyleEnum const enum_font_variant[] = {
211 {"normal", SP_CSS_FONT_VARIANT_NORMAL},
212 {"small-caps", SP_CSS_FONT_VARIANT_SMALL_CAPS},
213 {NULL, -1}
214 };
216 static SPStyleEnum const enum_font_weight[] = {
217 {"100", SP_CSS_FONT_WEIGHT_100},
218 {"200", SP_CSS_FONT_WEIGHT_200},
219 {"300", SP_CSS_FONT_WEIGHT_300},
220 {"400", SP_CSS_FONT_WEIGHT_400},
221 {"500", SP_CSS_FONT_WEIGHT_500},
222 {"600", SP_CSS_FONT_WEIGHT_600},
223 {"700", SP_CSS_FONT_WEIGHT_700},
224 {"800", SP_CSS_FONT_WEIGHT_800},
225 {"900", SP_CSS_FONT_WEIGHT_900},
226 {"normal", SP_CSS_FONT_WEIGHT_NORMAL},
227 {"bold", SP_CSS_FONT_WEIGHT_BOLD},
228 {"lighter", SP_CSS_FONT_WEIGHT_LIGHTER},
229 {"bolder", SP_CSS_FONT_WEIGHT_BOLDER},
230 {NULL, -1}
231 };
233 static SPStyleEnum const enum_font_stretch[] = {
234 {"ultra-condensed", SP_CSS_FONT_STRETCH_ULTRA_CONDENSED},
235 {"extra-condensed", SP_CSS_FONT_STRETCH_EXTRA_CONDENSED},
236 {"condensed", SP_CSS_FONT_STRETCH_CONDENSED},
237 {"semi-condensed", SP_CSS_FONT_STRETCH_SEMI_CONDENSED},
238 {"normal", SP_CSS_FONT_STRETCH_NORMAL},
239 {"semi-expanded", SP_CSS_FONT_STRETCH_SEMI_EXPANDED},
240 {"expanded", SP_CSS_FONT_STRETCH_EXPANDED},
241 {"extra-expanded", SP_CSS_FONT_STRETCH_EXTRA_EXPANDED},
242 {"ultra-expanded", SP_CSS_FONT_STRETCH_ULTRA_EXPANDED},
243 {"narrower", SP_CSS_FONT_STRETCH_NARROWER},
244 {"wider", SP_CSS_FONT_STRETCH_WIDER},
245 {NULL, -1}
246 };
248 static SPStyleEnum const enum_text_align[] = {
249 {"start", SP_CSS_TEXT_ALIGN_START},
250 {"end", SP_CSS_TEXT_ALIGN_END},
251 {"left", SP_CSS_TEXT_ALIGN_LEFT},
252 {"right", SP_CSS_TEXT_ALIGN_RIGHT},
253 {"center", SP_CSS_TEXT_ALIGN_CENTER},
254 {"justify", SP_CSS_TEXT_ALIGN_JUSTIFY},
255 {NULL, -1}
256 };
258 static SPStyleEnum const enum_text_transform[] = {
259 {"capitalize", SP_CSS_TEXT_TRANSFORM_CAPITALIZE},
260 {"uppercase", SP_CSS_TEXT_TRANSFORM_UPPERCASE},
261 {"lowercase", SP_CSS_TEXT_TRANSFORM_LOWERCASE},
262 {"none", SP_CSS_TEXT_TRANSFORM_NONE},
263 {NULL, -1}
264 };
266 static SPStyleEnum const enum_text_anchor[] = {
267 {"start", SP_CSS_TEXT_ANCHOR_START},
268 {"middle", SP_CSS_TEXT_ANCHOR_MIDDLE},
269 {"end", SP_CSS_TEXT_ANCHOR_END},
270 {NULL, -1}
271 };
273 static SPStyleEnum const enum_direction[] = {
274 {"ltr", SP_CSS_DIRECTION_LTR},
275 {"rtl", SP_CSS_DIRECTION_RTL},
276 {NULL, -1}
277 };
279 static SPStyleEnum const enum_block_progression[] = {
280 {"tb", SP_CSS_BLOCK_PROGRESSION_TB},
281 {"rl", SP_CSS_BLOCK_PROGRESSION_RL},
282 {"lr", SP_CSS_BLOCK_PROGRESSION_LR},
283 {NULL, -1}
284 };
286 static SPStyleEnum const enum_writing_mode[] = {
287 /* Note that using the same enumerator for lr as lr-tb means we write as lr-tb even if the
288 * input file said lr. We prefer writing lr-tb on the grounds that the spec says the initial
289 * value is lr-tb rather than lr.
290 *
291 * ECMA scripts may be surprised to find tb-rl in DOM if they set the attribute to rl, so
292 * sharing enumerators for different strings may be a bug (once we support ecma script).
293 */
294 {"lr-tb", SP_CSS_WRITING_MODE_LR_TB},
295 {"rl-tb", SP_CSS_WRITING_MODE_RL_TB},
296 {"tb-rl", SP_CSS_WRITING_MODE_TB_RL},
297 {"lr", SP_CSS_WRITING_MODE_LR_TB},
298 {"rl", SP_CSS_WRITING_MODE_RL_TB},
299 {"tb", SP_CSS_WRITING_MODE_TB_RL},
300 {NULL, -1}
301 };
303 static SPStyleEnum const enum_visibility[] = {
304 {"hidden", SP_CSS_VISIBILITY_HIDDEN},
305 {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
306 {"visible", SP_CSS_VISIBILITY_VISIBLE},
307 {NULL, -1}
308 };
310 static SPStyleEnum const enum_overflow[] = {
311 {"visible", SP_CSS_OVERFLOW_VISIBLE},
312 {"hidden", SP_CSS_OVERFLOW_HIDDEN},
313 {"scroll", SP_CSS_OVERFLOW_SCROLL},
314 {"auto", SP_CSS_OVERFLOW_AUTO},
315 {NULL, -1}
316 };
318 static SPStyleEnum const enum_display[] = {
319 {"none", SP_CSS_DISPLAY_NONE},
320 {"inline", SP_CSS_DISPLAY_INLINE},
321 {"block", SP_CSS_DISPLAY_BLOCK},
322 {"list-item", SP_CSS_DISPLAY_LIST_ITEM},
323 {"run-in", SP_CSS_DISPLAY_RUN_IN},
324 {"compact", SP_CSS_DISPLAY_COMPACT},
325 {"marker", SP_CSS_DISPLAY_MARKER},
326 {"table", SP_CSS_DISPLAY_TABLE},
327 {"inline-table", SP_CSS_DISPLAY_INLINE_TABLE},
328 {"table-row-group", SP_CSS_DISPLAY_TABLE_ROW_GROUP},
329 {"table-header-group", SP_CSS_DISPLAY_TABLE_HEADER_GROUP},
330 {"table-footer-group", SP_CSS_DISPLAY_TABLE_FOOTER_GROUP},
331 {"table-row", SP_CSS_DISPLAY_TABLE_ROW},
332 {"table-column-group", SP_CSS_DISPLAY_TABLE_COLUMN_GROUP},
333 {"table-column", SP_CSS_DISPLAY_TABLE_COLUMN},
334 {"table-cell", SP_CSS_DISPLAY_TABLE_CELL},
335 {"table-caption", SP_CSS_DISPLAY_TABLE_CAPTION},
336 {NULL, -1}
337 };
339 static SPStyleEnum const enum_shape_rendering[] = {
340 {"auto", 0},
341 {"optimizeSpeed", 0},
342 {"crispEdges", 0},
343 {"geometricPrecision", 0},
344 {NULL, -1}
345 };
347 static SPStyleEnum const enum_color_rendering[] = {
348 {"auto", 0},
349 {"optimizeSpeed", 0},
350 {"optimizeQuality", 0},
351 {NULL, -1}
352 };
354 static SPStyleEnum const *const enum_image_rendering = enum_color_rendering;
356 static SPStyleEnum const enum_text_rendering[] = {
357 {"auto", 0},
358 {"optimizeSpeed", 0},
359 {"optimizeLegibility", 0},
360 {"geometricPrecision", 0},
361 {NULL, -1}
362 };
364 /**
365 * Release callback.
366 */
367 static void
368 sp_style_object_release(SPObject *object, SPStyle *style)
369 {
370 style->object = NULL;
371 }
376 /**
377 * Returns a new SPStyle object with settings as per sp_style_clear().
378 */
379 SPStyle *
380 sp_style_new()
381 {
382 SPStyle *const style = g_new0(SPStyle, 1);
384 style->refcount = 1;
385 style->object = NULL;
386 style->text = sp_text_style_new();
387 style->text_private = TRUE;
389 sp_style_clear(style);
391 style->cloned = false;
392 style->hreffed = false;
393 style->listening = false;
395 return style;
396 }
399 /**
400 * Creates a new SPStyle object, and attaches it to the specified SPObject.
401 */
402 SPStyle *
403 sp_style_new_from_object(SPObject *object)
404 {
405 g_return_val_if_fail(object != NULL, NULL);
406 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
408 SPStyle *style = sp_style_new();
409 style->object = object;
410 g_signal_connect(G_OBJECT(object), "release", G_CALLBACK(sp_style_object_release), style);
412 if (object && SP_OBJECT_IS_CLONED(object)) {
413 style->cloned = true;
414 }
416 return style;
417 }
420 /**
421 * Increase refcount of style.
422 */
423 SPStyle *
424 sp_style_ref(SPStyle *style)
425 {
426 g_return_val_if_fail(style != NULL, NULL);
427 g_return_val_if_fail(style->refcount > 0, NULL);
429 style->refcount += 1;
431 return style;
432 }
435 /**
436 * Decrease refcount of style with possible destruction.
437 */
438 SPStyle *
439 sp_style_unref(SPStyle *style)
440 {
441 g_return_val_if_fail(style != NULL, NULL);
442 g_return_val_if_fail(style->refcount > 0, NULL);
444 style->refcount -= 1;
446 if (style->refcount < 1) {
447 if (style->object)
448 g_signal_handlers_disconnect_matched(G_OBJECT(style->object),
449 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
450 if (style->text) sp_text_style_unref(style->text);
451 sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
452 sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
453 g_free(style->stroke_dash.dash);
454 g_free(style);
455 }
457 return NULL;
458 }
460 /**
461 * Reads the various style parameters for an object from repr.
462 */
463 static void
464 sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
465 {
466 g_assert(style != NULL);
467 g_assert(repr != NULL);
468 g_assert(!object || (SP_OBJECT_REPR(object) == repr));
470 sp_style_clear(style);
472 if (object && SP_OBJECT_IS_CLONED(object)) {
473 style->cloned = true;
474 }
476 /* 1. Style itself */
477 gchar const *val = repr->attribute("style");
478 if (val != NULL) {
479 sp_style_merge_from_style_string(style, val);
480 }
482 if (object) {
483 sp_style_merge_from_object_stylesheet(style, object);
484 } else {
485 /** \todo No stylesheet information. Find out under what circumstances
486 * this occurs, and handle accordingly. (If we really wanted to, we
487 * could probably get stylesheets by going through repr->doc.)
488 */
489 }
491 /* 2. Presentation attributes */
492 /* CSS2 */
493 SPS_READ_PENUM_IF_UNSET(&style->visibility, repr, "visibility", enum_visibility, true);
494 SPS_READ_PENUM_IF_UNSET(&style->display, repr, "display", enum_display, true);
495 SPS_READ_PENUM_IF_UNSET(&style->overflow, repr, "overflow", enum_overflow, true);
496 /* Font */
497 SPS_READ_PFONTSIZE_IF_UNSET(&style->font_size, repr, "font-size");
498 SPS_READ_PENUM_IF_UNSET(&style->font_style, repr, "font-style", enum_font_style, true);
499 SPS_READ_PENUM_IF_UNSET(&style->font_variant, repr, "font-variant", enum_font_variant, true);
500 SPS_READ_PENUM_IF_UNSET(&style->font_weight, repr, "font-weight", enum_font_weight, true);
501 SPS_READ_PENUM_IF_UNSET(&style->font_stretch, repr, "font-stretch", enum_font_stretch, true);
502 /* Text (css2 chapter 16) */
503 SPS_READ_PLENGTH_IF_UNSET(&style->text_indent, repr, "text-indent");
504 SPS_READ_PENUM_IF_UNSET(&style->text_align, repr, "text-align", enum_text_align, true);
505 if (!style->text_decoration.set) {
506 val = repr->attribute("text-decoration");
507 if (val) {
508 sp_style_read_itextdecoration(&style->text_decoration, val);
509 }
510 }
511 if (!style->line_height.set) {
512 val = repr->attribute("line-height");
513 if (val) {
514 sp_style_read_ilengthornormal(&style->line_height, val);
515 }
516 }
517 if (!style->letter_spacing.set) {
518 val = repr->attribute("letter-spacing");
519 if (val) {
520 sp_style_read_ilengthornormal(&style->letter_spacing, val);
521 }
522 }
523 if (!style->word_spacing.set) {
524 val = repr->attribute("word-spacing");
525 if (val) {
526 sp_style_read_ilengthornormal(&style->word_spacing, val);
527 }
528 }
529 SPS_READ_PENUM_IF_UNSET(&style->text_transform, repr, "text-transform", enum_text_transform, true);
530 SPS_READ_PENUM_IF_UNSET(&style->direction, repr, "direction", enum_direction, true);
531 SPS_READ_PENUM_IF_UNSET(&style->block_progression, repr, "block_progression", enum_block_progression, true);
533 /* SVG */
534 SPS_READ_PENUM_IF_UNSET(&style->writing_mode, repr, "writing-mode",
535 enum_writing_mode, true);
536 SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
537 enum_text_anchor, true);
539 /* opacity */
540 if (!style->opacity.set) {
541 val = repr->attribute("opacity");
542 if (val) {
543 sp_style_read_iscale24(&style->opacity, val);
544 }
545 }
546 /* color */
547 if (!style->color.set) {
548 val = repr->attribute("color");
549 if (val) {
550 sp_style_read_icolor(&style->color, val, style, ( object
551 ? SP_OBJECT_DOCUMENT(object)
552 : NULL ));
553 }
554 }
555 /* fill */
556 if (!style->fill.set) {
557 val = repr->attribute("fill");
558 if (val) {
559 sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
560 }
561 }
562 /* fill-opacity */
563 if (!style->fill_opacity.set) {
564 val = repr->attribute("fill-opacity");
565 if (val) {
566 sp_style_read_iscale24(&style->fill_opacity, val);
567 }
568 }
569 /* fill-rule */
570 SPS_READ_PENUM_IF_UNSET(&style->fill_rule, repr, "fill-rule", enum_fill_rule, true);
571 /* stroke */
572 if (!style->stroke.set) {
573 val = repr->attribute("stroke");
574 if (val) {
575 sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
576 }
577 }
578 SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
579 SPS_READ_PENUM_IF_UNSET(&style->stroke_linecap, repr, "stroke-linecap", enum_stroke_linecap, true);
580 SPS_READ_PENUM_IF_UNSET(&style->stroke_linejoin, repr, "stroke-linejoin", enum_stroke_linejoin, true);
581 SPS_READ_PFLOAT_IF_UNSET(&style->stroke_miterlimit, repr, "stroke-miterlimit");
583 /* markers */
584 if (!style->marker[SP_MARKER_LOC].set) {
585 val = repr->attribute("marker");
586 if (val) {
587 sp_style_read_istring(&style->marker[SP_MARKER_LOC], val);
588 }
589 }
590 if (!style->marker[SP_MARKER_LOC_START].set) {
591 val = repr->attribute("marker-start");
592 if (val) {
593 sp_style_read_istring(&style->marker[SP_MARKER_LOC_START], val);
594 }
595 }
596 if (!style->marker[SP_MARKER_LOC_MID].set) {
597 val = repr->attribute("marker-mid");
598 if (val) {
599 sp_style_read_istring(&style->marker[SP_MARKER_LOC_MID], val);
600 }
601 }
602 if (!style->marker[SP_MARKER_LOC_END].set) {
603 val = repr->attribute("marker-end");
604 if (val) {
605 sp_style_read_istring(&style->marker[SP_MARKER_LOC_END], val);
606 }
607 }
609 /* stroke-opacity */
610 if (!style->stroke_opacity.set) {
611 val = repr->attribute("stroke-opacity");
612 if (val) {
613 sp_style_read_iscale24(&style->stroke_opacity, val);
614 }
615 }
616 if (!style->stroke_dasharray_set) {
617 val = repr->attribute("stroke-dasharray");
618 if (val) {
619 sp_style_read_dash(style, val);
620 }
621 }
622 if (!style->stroke_dashoffset_set) {
623 /* fixme */
624 val = repr->attribute("stroke-dashoffset");
625 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
626 style->stroke_dashoffset_set = TRUE;
627 } else {
628 style->stroke_dashoffset_set = FALSE;
629 }
630 }
632 /* font-family */
633 if (!style->text_private || !style->text->font_family.set) {
634 val = repr->attribute("font-family");
635 if (val) {
636 if (!style->text_private) sp_style_privatize_text(style);
637 sp_style_read_istring(&style->text->font_family, val);
638 css2_unescape_unquote(&style->text->font_family);
639 }
640 }
642 /* 3. Merge from parent */
643 if (object) {
644 if (object->parent) {
645 sp_style_merge_from_parent(style, SP_OBJECT_STYLE(object->parent));
646 }
647 } else {
648 if (sp_repr_parent(repr)) {
649 /// \todo fixme: This is not the prettiest thing (Lauris)
650 SPStyle *parent = sp_style_new();
651 sp_style_read(parent, NULL, sp_repr_parent(repr));
652 sp_style_merge_from_parent(style, parent);
653 sp_style_unref(parent);
654 }
655 }
656 }
659 /**
660 * Read style properties from object's repr.
661 *
662 * 1. Reset existing object style
663 * 2. Load current effective object style
664 * 3. Load i attributes from immediate parent (which has to be up-to-date)
665 */
666 void
667 sp_style_read_from_object(SPStyle *style, SPObject *object)
668 {
669 g_return_if_fail(style != NULL);
670 g_return_if_fail(object != NULL);
671 g_return_if_fail(SP_IS_OBJECT(object));
673 Inkscape::XML::Node *repr = SP_OBJECT_REPR(object);
674 g_return_if_fail(repr != NULL);
676 sp_style_read(style, object, repr);
677 }
680 /**
681 * Read style properties from repr only.
682 */
683 void
684 sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
685 {
686 g_return_if_fail(style != NULL);
687 g_return_if_fail(repr != NULL);
689 sp_style_read(style, NULL, repr);
690 }
694 /**
695 *
696 */
697 static void
698 sp_style_privatize_text(SPStyle *style)
699 {
700 SPTextStyle *text = style->text;
701 style->text = sp_text_style_duplicate_unset(style->text);
702 sp_text_style_unref(text);
703 style->text_private = TRUE;
704 }
707 /**
708 * Merge property into style.
709 *
710 * Should be called in order of highest to lowest precedence.
711 * E.g. for a single style string, call from the last declaration to the first,
712 * as CSS says that later declarations override earlier ones.
713 *
714 * \pre val != NULL.
715 */
716 static void
717 sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
718 {
719 g_return_if_fail(val != NULL);
721 switch (id) {
722 /* CSS2 */
723 /* Font */
724 case SP_PROP_FONT_FAMILY:
725 if (!style->text_private) sp_style_privatize_text(style);
726 if (!style->text->font_family.set) {
727 sp_style_read_istring(&style->text->font_family, val);
728 css2_unescape_unquote (&style->text->font_family);
729 }
730 break;
731 case SP_PROP_FONT_SIZE:
732 SPS_READ_IFONTSIZE_IF_UNSET(&style->font_size, val);
733 break;
734 case SP_PROP_FONT_SIZE_ADJUST:
735 if (strcmp(val, "none") != 0) {
736 g_warning("Unimplemented style property id SP_PROP_FONT_SIZE_ADJUST: value: %s", val);
737 }
738 break;
739 case SP_PROP_FONT_STYLE:
740 SPS_READ_IENUM_IF_UNSET(&style->font_style, val, enum_font_style, true);
741 break;
742 case SP_PROP_FONT_VARIANT:
743 SPS_READ_IENUM_IF_UNSET(&style->font_variant, val, enum_font_variant, true);
744 break;
745 case SP_PROP_FONT_WEIGHT:
746 SPS_READ_IENUM_IF_UNSET(&style->font_weight, val, enum_font_weight, true);
747 break;
748 case SP_PROP_FONT_STRETCH:
749 SPS_READ_IENUM_IF_UNSET(&style->font_stretch, val, enum_font_stretch, true);
750 break;
751 case SP_PROP_FONT:
752 if (!style->text_private) sp_style_privatize_text(style);
753 if (!style->text->font.set) {
754 g_free(style->text->font.value);
755 style->text->font.value = g_strdup(val);
756 style->text->font.set = TRUE;
757 style->text->font.inherit = (val && !strcmp(val, "inherit"));
758 }
759 break;
760 /* Text */
761 case SP_PROP_TEXT_INDENT:
762 SPS_READ_ILENGTH_IF_UNSET(&style->text_indent, val);
763 break;
764 case SP_PROP_TEXT_ALIGN:
765 SPS_READ_IENUM_IF_UNSET(&style->text_align, val, enum_text_align, true);
766 break;
767 case SP_PROP_TEXT_DECORATION:
768 if (!style->text_decoration.set) {
769 sp_style_read_itextdecoration(&style->text_decoration, val);
770 }
771 break;
772 case SP_PROP_LINE_HEIGHT:
773 if (!style->line_height.set) {
774 sp_style_read_ilengthornormal(&style->line_height, val);
775 }
776 break;
777 case SP_PROP_LETTER_SPACING:
778 if (!style->letter_spacing.set) {
779 sp_style_read_ilengthornormal(&style->letter_spacing, val);
780 }
781 break;
782 case SP_PROP_WORD_SPACING:
783 if (!style->word_spacing.set) {
784 sp_style_read_ilengthornormal(&style->word_spacing, val);
785 }
786 break;
787 case SP_PROP_TEXT_TRANSFORM:
788 SPS_READ_IENUM_IF_UNSET(&style->text_transform, val, enum_text_transform, true);
789 break;
790 /* Text (css3) */
791 case SP_PROP_DIRECTION:
792 SPS_READ_IENUM_IF_UNSET(&style->direction, val, enum_direction, true);
793 break;
794 case SP_PROP_BLOCK_PROGRESSION:
795 SPS_READ_IENUM_IF_UNSET(&style->block_progression, val, enum_block_progression, true);
796 break;
797 case SP_PROP_WRITING_MODE:
798 SPS_READ_IENUM_IF_UNSET(&style->writing_mode, val, enum_writing_mode, true);
799 break;
800 case SP_PROP_TEXT_ANCHOR:
801 SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
802 break;
803 /* Text (unimplemented) */
804 case SP_PROP_TEXT_RENDERING: {
805 /* Ignore the hint. */
806 SPIEnum dummy;
807 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_text_rendering, true);
808 break;
809 }
810 case SP_PROP_ALIGNMENT_BASELINE:
811 g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
812 break;
813 case SP_PROP_BASELINE_SHIFT:
814 g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
815 break;
816 case SP_PROP_DOMINANT_BASELINE:
817 g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
818 break;
819 case SP_PROP_GLYPH_ORIENTATION_HORIZONTAL:
820 g_warning("Unimplemented style property SP_PROP_ORIENTATION_HORIZONTAL: value: %s", val);
821 break;
822 case SP_PROP_GLYPH_ORIENTATION_VERTICAL:
823 g_warning("Unimplemented style property SP_PROP_ORIENTATION_VERTICAL: value: %s", val);
824 break;
825 case SP_PROP_KERNING:
826 g_warning("Unimplemented style property SP_PROP_KERNING: value: %s", val);
827 break;
828 /* Misc */
829 case SP_PROP_CLIP:
830 g_warning("Unimplemented style property SP_PROP_CLIP: value: %s", val);
831 break;
832 case SP_PROP_COLOR:
833 if (!style->color.set) {
834 sp_style_read_icolor(&style->color, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
835 }
836 break;
837 case SP_PROP_CURSOR:
838 g_warning("Unimplemented style property SP_PROP_CURSOR: value: %s", val);
839 break;
840 case SP_PROP_DISPLAY:
841 SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
842 break;
843 case SP_PROP_OVERFLOW:
844 /** \todo
845 * FIXME: not supported properly yet, we just read and write it,
846 * but act as if it is always "display".
847 */
848 SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
849 break;
850 case SP_PROP_VISIBILITY:
851 SPS_READ_IENUM_IF_UNSET(&style->visibility, val, enum_visibility, true);
852 break;
853 /* SVG */
854 /* Clip/Mask */
855 case SP_PROP_CLIP_PATH:
856 g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
857 break;
858 case SP_PROP_CLIP_RULE:
859 g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
860 break;
861 case SP_PROP_MASK:
862 g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
863 break;
864 case SP_PROP_OPACITY:
865 if (!style->opacity.set) {
866 sp_style_read_iscale24(&style->opacity, val);
867 }
868 break;
869 /* Filter */
870 case SP_PROP_ENABLE_BACKGROUND:
871 g_warning("Unimplemented style property SP_PROP_ENABLE_BACKGROUND: value: %s", val);
872 break;
873 case SP_PROP_FILTER:
874 g_warning("Unimplemented style property SP_PROP_FILTER: value: %s", val);
875 break;
876 case SP_PROP_FLOOD_COLOR:
877 g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
878 break;
879 case SP_PROP_FLOOD_OPACITY:
880 g_warning("Unimplemented style property SP_PROP_FLOOD_OPACITY: value: %s", val);
881 break;
882 case SP_PROP_LIGHTING_COLOR:
883 g_warning("Unimplemented style property SP_PROP_LIGHTING_COLOR: value: %s", val);
884 break;
885 /* Gradient */
886 case SP_PROP_STOP_COLOR:
887 g_warning("Unimplemented style property SP_PROP_STOP_COLOR: value: %s", val);
888 break;
889 case SP_PROP_STOP_OPACITY:
890 g_warning("Unimplemented style property SP_PROP_STOP_OPACITY: value: %s", val);
891 break;
892 /* Interactivity */
893 case SP_PROP_POINTER_EVENTS:
894 g_warning("Unimplemented style property SP_PROP_POINTER_EVENTS: value: %s", val);
895 break;
896 /* Paint */
897 case SP_PROP_COLOR_INTERPOLATION:
898 g_warning("Unimplemented style property SP_PROP_COLOR_INTERPOLATION: value: %s", val);
899 break;
900 case SP_PROP_COLOR_INTERPOLATION_FILTERS:
901 g_warning("Unimplemented style property SP_PROP_INTERPOLATION_FILTERS: value: %s", val);
902 break;
903 case SP_PROP_COLOR_PROFILE:
904 g_warning("Unimplemented style property SP_PROP_COLOR_PROFILE: value: %s", val);
905 break;
906 case SP_PROP_COLOR_RENDERING: {
907 /* Ignore the hint. */
908 SPIEnum dummy;
909 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_color_rendering, true);
910 break;
911 }
912 case SP_PROP_FILL:
913 if (!style->fill.set) {
914 sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
915 }
916 break;
917 case SP_PROP_FILL_OPACITY:
918 if (!style->fill_opacity.set) {
919 sp_style_read_iscale24(&style->fill_opacity, val);
920 }
921 break;
922 case SP_PROP_FILL_RULE:
923 if (!style->fill_rule.set) {
924 sp_style_read_ienum(&style->fill_rule, val, enum_fill_rule, true);
925 }
926 break;
927 case SP_PROP_IMAGE_RENDERING: {
928 /* Ignore the hint. */
929 SPIEnum dummy;
930 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_image_rendering, true);
931 break;
932 }
933 case SP_PROP_MARKER:
934 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
935 /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
936 marker_status("Setting SP_PROP_MARKER");
937 if (!style->marker[SP_MARKER_LOC].set) {
938 g_free(style->marker[SP_MARKER_LOC].value);
939 style->marker[SP_MARKER_LOC].value = g_strdup(val);
940 style->marker[SP_MARKER_LOC].set = TRUE;
941 style->marker[SP_MARKER_LOC].inherit = (val && !strcmp(val, "inherit"));
942 }
943 break;
945 case SP_PROP_MARKER_START:
946 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
947 marker_status("Setting SP_PROP_MARKER_START");
948 if (!style->marker[SP_MARKER_LOC_START].set) {
949 g_free(style->marker[SP_MARKER_LOC_START].value);
950 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
951 style->marker[SP_MARKER_LOC_START].set = TRUE;
952 style->marker[SP_MARKER_LOC_START].inherit = (val && !strcmp(val, "inherit"));
953 }
954 break;
955 case SP_PROP_MARKER_MID:
956 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
957 marker_status("Setting SP_PROP_MARKER_MID");
958 if (!style->marker[SP_MARKER_LOC_MID].set) {
959 g_free(style->marker[SP_MARKER_LOC_MID].value);
960 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
961 style->marker[SP_MARKER_LOC_MID].set = TRUE;
962 style->marker[SP_MARKER_LOC_MID].inherit = (val && !strcmp(val, "inherit"));
963 }
964 break;
965 case SP_PROP_MARKER_END:
966 /* TODO: Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
967 marker_status("Setting SP_PROP_MARKER_END");
968 if (!style->marker[SP_MARKER_LOC_END].set) {
969 g_free(style->marker[SP_MARKER_LOC_END].value);
970 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
971 style->marker[SP_MARKER_LOC_END].set = TRUE;
972 style->marker[SP_MARKER_LOC_END].inherit = (val && !strcmp(val, "inherit"));
973 }
974 break;
976 case SP_PROP_SHAPE_RENDERING: {
977 /* Ignore the hint. */
978 SPIEnum dummy;
979 SPS_READ_IENUM_IF_UNSET(&dummy, val, enum_shape_rendering, true);
980 break;
981 }
983 case SP_PROP_STROKE:
984 if (!style->stroke.set) {
985 sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
986 }
987 break;
988 case SP_PROP_STROKE_WIDTH:
989 SPS_READ_ILENGTH_IF_UNSET(&style->stroke_width, val);
990 break;
991 case SP_PROP_STROKE_DASHARRAY:
992 if (!style->stroke_dasharray_set) {
993 sp_style_read_dash(style, val);
994 }
995 break;
996 case SP_PROP_STROKE_DASHOFFSET:
997 if (!style->stroke_dashoffset_set) {
998 /* fixme */
999 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
1000 style->stroke_dashoffset_set = TRUE;
1001 } else {
1002 style->stroke_dashoffset_set = FALSE;
1003 }
1004 }
1005 break;
1006 case SP_PROP_STROKE_LINECAP:
1007 if (!style->stroke_linecap.set) {
1008 sp_style_read_ienum(&style->stroke_linecap, val, enum_stroke_linecap, true);
1009 }
1010 break;
1011 case SP_PROP_STROKE_LINEJOIN:
1012 if (!style->stroke_linejoin.set) {
1013 sp_style_read_ienum(&style->stroke_linejoin, val, enum_stroke_linejoin, true);
1014 }
1015 break;
1016 case SP_PROP_STROKE_MITERLIMIT:
1017 if (!style->stroke_miterlimit.set) {
1018 sp_style_read_ifloat(&style->stroke_miterlimit, val);
1019 }
1020 break;
1021 case SP_PROP_STROKE_OPACITY:
1022 if (!style->stroke_opacity.set) {
1023 sp_style_read_iscale24(&style->stroke_opacity, val);
1024 }
1025 break;
1027 default:
1028 g_warning("Invalid style property id: %d value: %s", id, val);
1029 break;
1030 }
1031 }
1033 static void
1034 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
1035 {
1036 /** \todo Ensure that property is lcased, as per
1037 * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
1038 * Should probably be done in libcroco.
1039 */
1040 unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
1041 if (prop_idx != SP_ATTR_INVALID) {
1042 /** \todo
1043 * effic: Test whether the property is already set before trying to
1044 * convert to string. Alternatively, set from CRTerm directly rather
1045 * than converting to string.
1046 */
1047 guchar *const str_value_unsigned = cr_term_to_string(decl->value);
1048 gchar *const str_value = reinterpret_cast<gchar *>(str_value_unsigned);
1049 sp_style_merge_property(style, prop_idx, str_value);
1050 g_free(str_value);
1051 }
1052 }
1054 static void
1055 sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
1056 {
1057 #if 0 /* forwards */
1058 for (CRPropList const *cur = props; cur; cur = cr_prop_list_get_next(cur)) {
1059 CRDeclaration *decl = NULL;
1060 cr_prop_list_get_decl(cur, &decl);
1061 sp_style_merge_style_from_decl(style, decl);
1062 }
1063 #else /* in reverse order, as we need later declarations to take precedence over earlier ones. */
1064 if (props) {
1065 sp_style_merge_from_props(style, cr_prop_list_get_next(props));
1066 CRDeclaration *decl = NULL;
1067 cr_prop_list_get_decl(props, &decl);
1068 sp_style_merge_style_from_decl(style, decl);
1069 }
1070 #endif
1071 }
1073 /**
1074 * \pre decl_list != NULL
1075 */
1076 static void
1077 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
1078 {
1079 if (decl_list->next) {
1080 sp_style_merge_from_decl_list(style, decl_list->next);
1081 }
1082 sp_style_merge_style_from_decl(style, decl_list);
1083 }
1085 static CRSelEng *
1086 sp_repr_sel_eng()
1087 {
1088 CRSelEng *const ret = cr_sel_eng_new();
1089 cr_sel_eng_set_node_iface(ret, &Inkscape::XML::croco_node_iface);
1091 /** \todo
1092 * Check whether we need to register any pseudo-class handlers.
1093 * libcroco has its own default handlers for first-child and lang.
1094 *
1095 * We probably want handlers for link and arguably visited (though
1096 * inkscape can't visit links at the time of writing). hover etc.
1097 * more useful in inkview than the editor inkscape.
1098 *
1099 * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
1100 * the following should be honoured, at least by inkview:
1101 * :hover, :active, :focus, :visited, :link.
1102 */
1104 g_assert(ret);
1105 return ret;
1106 }
1108 static void
1109 sp_style_merge_from_object_stylesheet(SPStyle *const style, SPObject const *const object)
1110 {
1111 static CRSelEng *sel_eng = NULL;
1112 if (!sel_eng) {
1113 sel_eng = sp_repr_sel_eng();
1114 }
1116 CRPropList *props = NULL;
1117 CRStatus status = cr_sel_eng_get_matched_properties_from_cascade(sel_eng,
1118 object->document->style_cascade,
1119 object->repr,
1120 &props);
1121 g_return_if_fail(status == CR_OK);
1122 /// \todo Check what errors can occur, and handle them properly.
1123 if (props) {
1124 sp_style_merge_from_props(style, props);
1125 cr_prop_list_destroy(props);
1126 }
1127 }
1129 /**
1130 * Parses a style="..." string and merges it with an existing SPStyle.
1131 */
1132 void
1133 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
1134 {
1135 /*
1136 * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
1137 * ``When CSS styling is used, CSS inline style is specified by including
1138 * semicolon-separated property declarations of the form "name : value"
1139 * within the style attribute''.
1140 *
1141 * That's fairly ambiguous. Is a `value' allowed to contain semicolons?
1142 * Why does it say "including", what else is allowed in the style
1143 * attribute value?
1144 */
1146 CRDeclaration *const decl_list
1147 = cr_declaration_parse_list_from_buf(reinterpret_cast<guchar const *>(p), CR_UTF_8);
1148 if (decl_list) {
1149 sp_style_merge_from_decl_list(style, decl_list);
1150 cr_declaration_destroy(decl_list);
1151 }
1152 }
1154 /** Indexed by SP_CSS_FONT_SIZE_blah. */
1155 static float const font_size_table[] = {6.0, 8.0, 10.0, 12.0, 14.0, 18.0, 24.0};
1157 static void
1158 sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &parent)
1159 {
1160 /* 'font-size' */
1161 if (!child.set || child.inherit) {
1162 /* Inherit the computed value. Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
1163 child.computed = parent.computed;
1164 } else if (child.type == SP_FONT_SIZE_LITERAL) {
1165 /** \todo
1166 * fixme: SVG and CSS do not specify clearly, whether we should use
1167 * user or screen coordinates (Lauris)
1168 */
1169 if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
1170 child.computed = font_size_table[child.value];
1171 } else if (child.value == SP_CSS_FONT_SIZE_SMALLER) {
1172 child.computed = parent.computed / 1.2;
1173 } else if (child.value == SP_CSS_FONT_SIZE_LARGER) {
1174 child.computed = parent.computed * 1.2;
1175 } else {
1176 /* Illegal value */
1177 }
1178 } else if (child.type == SP_FONT_SIZE_PERCENTAGE) {
1179 /* Unlike most other lengths, percentage for font size is relative to parent computed value
1180 * rather than viewport. */
1181 child.computed = parent.computed * SP_F8_16_TO_FLOAT(child.value);
1182 }
1183 }
1185 /**
1186 * Sets computed values in \a style, which may involve inheriting from (or in some other way
1187 * calculating from) corresponding computed values of \a parent.
1188 *
1189 * References: http://www.w3.org/TR/SVG11/propidx.html shows what properties inherit by default.
1190 * http://www.w3.org/TR/SVG11/styling.html#Inheritance gives general rules as to what it means to
1191 * inherit a value. http://www.w3.org/TR/REC-CSS2/cascade.html#computed-value is more precise
1192 * about what the computed value is (not obvious for lengths).
1193 *
1194 * \pre \a parent's computed values are already up-to-date.
1195 */
1196 void
1197 sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
1198 {
1199 g_return_if_fail(style != NULL);
1201 /** \todo
1202 * fixme: Check for existing callers that might pass null parent.
1203 * This should probably be g_return_if_fail, or else we should make a
1204 * best attempt to set computed values correctly without having a parent
1205 * (i.e., by assuming parent has initial values).
1206 */
1207 if (!parent)
1208 return;
1210 /* CSS2 */
1211 /* Font */
1212 sp_style_merge_font_size_from_parent(style->font_size, parent->font_size);
1214 /* 'font-style' */
1215 if (!style->font_style.set || style->font_style.inherit) {
1216 style->font_style.computed = parent->font_style.computed;
1217 }
1219 /* 'font-variant' */
1220 if (!style->font_variant.set || style->font_variant.inherit) {
1221 style->font_variant.computed = parent->font_variant.computed;
1222 }
1224 /* 'font-weight' */
1225 if (!style->font_weight.set || style->font_weight.inherit) {
1226 style->font_weight.computed = parent->font_weight.computed;
1227 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
1228 /** \todo
1229 * fixme: This is unconditional, i.e., happens even if parent not
1230 * present.
1231 */
1232 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
1233 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) {
1234 style->font_weight.computed = SP_CSS_FONT_WEIGHT_700;
1235 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER) {
1236 unsigned const parent_val = parent->font_weight.computed;
1237 g_assert(SP_CSS_FONT_WEIGHT_100 == 0);
1238 // strictly, 'bolder' and 'lighter' should go to the next weight
1239 // expressible in the current font family, but that's difficult to
1240 // find out, so jumping by 3 seems an appropriate approximation
1241 style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
1242 ? (unsigned)SP_CSS_FONT_WEIGHT_100
1243 : parent_val - 3);
1244 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1245 } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
1246 unsigned const parent_val = parent->font_weight.computed;
1247 g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
1248 style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
1249 ? (unsigned)SP_CSS_FONT_WEIGHT_900
1250 : parent_val + 3);
1251 g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
1252 }
1254 /* 'font-stretch' */
1255 if (!style->font_stretch.set || style->font_stretch.inherit) {
1256 style->font_stretch.computed = parent->font_stretch.computed;
1257 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
1258 unsigned const parent_val = parent->font_stretch.computed;
1259 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
1260 ? parent_val
1261 : parent_val - 1);
1262 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1263 } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
1264 unsigned const parent_val = parent->font_stretch.computed;
1265 g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1266 style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
1267 ? parent_val
1268 : parent_val + 1);
1269 g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
1270 }
1272 /* text (css2) */
1273 if (!style->text_indent.set || style->text_indent.inherit) {
1274 style->text_indent.computed = parent->text_indent.computed;
1275 }
1277 if (!style->text_align.set || style->text_align.inherit) {
1278 style->text_align.computed = parent->text_align.computed;
1279 }
1281 if (!style->text_decoration.set || style->text_decoration.inherit) {
1282 style->text_decoration.underline = parent->text_decoration.underline;
1283 style->text_decoration.overline = parent->text_decoration.overline;
1284 style->text_decoration.line_through = parent->text_decoration.line_through;
1285 style->text_decoration.blink = parent->text_decoration.blink;
1286 }
1288 if (!style->line_height.set || style->line_height.inherit) {
1289 style->line_height.computed = parent->line_height.computed;
1290 style->line_height.normal = parent->line_height.normal;
1291 }
1293 if (!style->letter_spacing.set || style->letter_spacing.inherit) {
1294 style->letter_spacing.computed = parent->letter_spacing.computed;
1295 style->letter_spacing.normal = parent->letter_spacing.normal;
1296 }
1298 if (!style->word_spacing.set || style->word_spacing.inherit) {
1299 style->word_spacing.computed = parent->word_spacing.computed;
1300 style->word_spacing.normal = parent->word_spacing.normal;
1301 }
1303 if (!style->text_transform.set || style->text_transform.inherit) {
1304 style->text_transform.computed = parent->text_transform.computed;
1305 }
1307 if (!style->direction.set || style->direction.inherit) {
1308 style->direction.computed = parent->direction.computed;
1309 }
1311 if (!style->block_progression.set || style->block_progression.inherit) {
1312 style->block_progression.computed = parent->block_progression.computed;
1313 }
1315 if (!style->writing_mode.set || style->writing_mode.inherit) {
1316 style->writing_mode.computed = parent->writing_mode.computed;
1317 }
1319 if (!style->text_anchor.set || style->text_anchor.inherit) {
1320 style->text_anchor.computed = parent->text_anchor.computed;
1321 }
1323 if (style->opacity.inherit) {
1324 style->opacity.value = parent->opacity.value;
1325 }
1327 /* Color */
1328 if (!style->color.set || style->color.inherit) {
1329 sp_style_merge_ipaint(style, &style->color, &parent->color);
1330 }
1332 /* Fill */
1333 if (!style->fill.set || style->fill.inherit || style->fill.currentcolor) {
1334 sp_style_merge_ipaint(style, &style->fill, &parent->fill);
1335 }
1337 if (!style->fill_opacity.set || style->fill_opacity.inherit) {
1338 style->fill_opacity.value = parent->fill_opacity.value;
1339 }
1341 if (!style->fill_rule.set || style->fill_rule.inherit) {
1342 style->fill_rule.computed = parent->fill_rule.computed;
1343 }
1345 /* Stroke */
1346 if (!style->stroke.set || style->stroke.inherit || style->stroke.currentcolor) {
1347 sp_style_merge_ipaint(style, &style->stroke, &parent->stroke);
1348 }
1350 if (!style->stroke_width.set || style->stroke_width.inherit) {
1351 style->stroke_width.computed = parent->stroke_width.computed;
1352 } else {
1353 /* Update computed value for any change in font inherited from parent. */
1354 double const em = style->font_size.computed;
1355 if (style->stroke_width.unit == SP_CSS_UNIT_EM) {
1356 style->stroke_width.computed = style->stroke_width.value * em;
1357 } else if (style->stroke_width.unit == SP_CSS_UNIT_EX) {
1358 double const ex = em * 0.5; // fixme: Get x height from libnrtype or pango.
1359 style->stroke_width.computed = style->stroke_width.value * ex;
1360 }
1361 }
1363 if (!style->stroke_linecap.set || style->stroke_linecap.inherit) {
1364 style->stroke_linecap.computed = parent->stroke_linecap.computed;
1365 }
1367 if (!style->stroke_linejoin.set || style->stroke_linejoin.inherit) {
1368 style->stroke_linejoin.computed = parent->stroke_linejoin.computed;
1369 }
1371 if (!style->stroke_miterlimit.set || style->stroke_miterlimit.inherit) {
1372 style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
1373 }
1375 if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
1376 /** \todo
1377 * This code looks wrong. Why does the logic differ from the
1378 * above properties? Similarly dashoffset below.
1379 */
1380 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1381 if (style->stroke_dash.n_dash > 0) {
1382 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1383 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1384 }
1385 }
1387 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1388 style->stroke_dash.offset = parent->stroke_dash.offset;
1389 }
1391 if (!style->stroke_opacity.set || style->stroke_opacity.inherit) {
1392 style->stroke_opacity.value = parent->stroke_opacity.value;
1393 }
1395 if (style->text && parent->text) {
1396 if (!style->text->font_family.set || style->text->font_family.inherit) {
1397 g_free(style->text->font_family.value);
1398 style->text->font_family.value = g_strdup(parent->text->font_family.value);
1399 }
1400 }
1402 /* Markers - Free the old value and make copy of the new */
1403 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1404 if (!style->marker[i].set || style->marker[i].inherit) {
1405 g_free(style->marker[i].value);
1406 style->marker[i].value = g_strdup(parent->marker[i].value);
1407 }
1408 }
1409 }
1411 template <typename T>
1412 static void
1413 sp_style_merge_prop_from_dying_parent(T &child, T const &parent)
1414 {
1415 if ( ( !(child.set) || child.inherit )
1416 && parent.set && !(parent.inherit) )
1417 {
1418 child = parent;
1419 }
1420 }
1422 /**
1423 * Copy SPIString from parent to child.
1424 */
1425 static void
1426 sp_style_merge_string_prop_from_dying_parent(SPIString &child, SPIString const &parent)
1427 {
1428 if ( ( !(child.set) || child.inherit )
1429 && parent.set && !(parent.inherit) )
1430 {
1431 g_free(child.value);
1432 child.value = g_strdup(parent.value);
1433 child.set = parent.set;
1434 child.inherit = parent.inherit;
1435 }
1436 }
1438 static void
1439 sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
1440 SPIPaint &child, SPIPaint const &parent)
1441 {
1442 /** \todo
1443 * I haven't given this much attention. See comments below about
1444 * currentColor, colorProfile, and relative URIs.
1445 */
1446 if (!child.set || child.inherit || child.currentcolor) {
1447 sp_style_merge_ipaint(style, &child, &parent);
1448 child.set = parent.set;
1449 child.inherit = parent.inherit;
1450 }
1451 }
1453 static void
1454 sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &parent,
1455 unsigned const max_computed_val,
1456 unsigned const smaller_val)
1457 {
1458 /* We assume that min computed val is 0, contiguous up to max_computed_val,
1459 then zero or more other absolute values, then smaller_val then larger_val. */
1460 unsigned const min_computed_val = 0;
1461 unsigned const larger_val = smaller_val + 1;
1462 g_return_if_fail(min_computed_val < max_computed_val);
1463 g_return_if_fail(max_computed_val < smaller_val);
1465 if (parent.set && !parent.inherit) {
1466 if (!child.set || child.inherit) {
1467 child.value = parent.value;
1468 child.set = parent.set; // i.e. true
1469 child.inherit = parent.inherit; // i.e. false
1470 } else if (child.value < smaller_val) {
1471 /* Child has absolute value: leave as is. */
1472 } else if ( ( child.value == smaller_val
1473 && parent.value == larger_val )
1474 || ( parent.value == smaller_val
1475 && child.value == larger_val ) )
1476 {
1477 child.set = false;
1478 /*
1479 * Note that this can result in a change in computed value in the
1480 * rare case that the parent's setting was a no-op (i.e. if the
1481 * parent's parent's computed value was already ultra-condensed or
1482 * ultra-expanded). However, I'd guess that the change is for the
1483 * better: I'd guess that if the properties were specified
1484 * relatively, then the intent is to counteract parent's effect.
1485 * I.e. I believe this is the best code even in that case.
1486 */
1487 } else if (child.value == parent.value) {
1488 /* Leave as is. */
1489 /** \todo
1490 * It's unclear what to do if style and parent specify the same
1491 * relative directive (narrower or wider). We can either convert
1492 * to absolute specification or coalesce to a single relative
1493 * request (of half the strength of the original pair).
1494 *
1495 * Converting to a single level of relative specification is a
1496 * better choice if the newly-unlinked clone is itself cloned to
1497 * other contexts (inheriting different font stretchiness): it
1498 * would preserve the effect that it will be narrower than
1499 * the inherited context in each case. The disadvantage is that
1500 * it will ~certainly affect the computed value of the
1501 * newly-unlinked clone.
1502 */
1503 } else {
1504 unsigned const parent_val = parent.computed;
1505 child.value = ( child.value == smaller_val
1506 ? ( parent_val == min_computed_val
1507 ? parent_val
1508 : parent_val - 1 )
1509 : ( parent_val == max_computed_val
1510 ? parent_val
1511 : parent_val + 1 ) );
1512 g_assert(child.value <= max_computed_val);
1513 child.inherit = false;
1514 g_assert(child.set);
1515 }
1516 }
1517 }
1519 template <typename LengthT>
1520 static void
1521 sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &parent,
1522 double const parent_child_em_ratio)
1523 {
1524 if ( ( !(child.set) || child.inherit )
1525 && parent.set && !(parent.inherit) )
1526 {
1527 child = parent;
1528 switch (parent.unit) {
1529 case SP_CSS_UNIT_EM:
1530 case SP_CSS_UNIT_EX:
1531 child.value *= parent_child_em_ratio;
1532 /** \todo
1533 * fixme: Have separate ex ratio parameter.
1534 * Get x height from libnrtype or pango.
1535 */
1536 if (!isFinite(child.value)) {
1537 child.value = child.computed;
1538 child.unit = SP_CSS_UNIT_NONE;
1539 }
1540 break;
1542 default:
1543 break;
1544 }
1545 }
1546 }
1548 static double
1549 get_relative_font_size_frac(SPIFontSize const &font_size)
1550 {
1551 switch (font_size.type) {
1552 case SP_FONT_SIZE_LITERAL: {
1553 switch (font_size.value) {
1554 case SP_CSS_FONT_SIZE_SMALLER:
1555 return 5.0 / 6.0;
1557 case SP_CSS_FONT_SIZE_LARGER:
1558 return 6.0 / 5.0;
1560 default:
1561 g_assert_not_reached();
1562 }
1563 }
1565 case SP_FONT_SIZE_PERCENTAGE:
1566 return SP_F8_16_TO_FLOAT(font_size.value);
1568 case SP_FONT_SIZE_LENGTH:
1569 g_assert_not_reached();
1570 }
1571 g_assert_not_reached();
1572 }
1574 /**
1575 * Combine \a style and \a parent style specifications into a single style specification that
1576 * preserves (as much as possible) the effect of the existing \a style being a child of \a parent.
1577 *
1578 * Called when the parent repr is to be removed (e.g. the parent is a \<use\> element that is being
1579 * unlinked), in which case we copy/adapt property values that are explicitly set in \a parent,
1580 * trying to retain the same visual appearance once the parent is removed. Interesting cases are
1581 * when there is unusual interaction with the parent's value (opacity, display) or when the value
1582 * can be specified as relative to the parent computed value (font-size, font-weight etc.).
1583 *
1584 * Doesn't update computed values of \a style. For correctness, you should subsequently call
1585 * sp_style_merge_from_parent against the new parent (presumably \a parent's parent) even if \a
1586 * style was previously up-to-date wrt \a parent.
1587 *
1588 * \pre \a parent's computed values are already up-to-date.
1589 * (\a style's computed values needn't be up-to-date.)
1590 */
1591 void
1592 sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const parent)
1593 {
1594 /** \note
1595 * The general rule for each property is as follows:
1596 *
1597 * If style is set to an absolute value, then leave it as is.
1598 *
1599 * Otherwise (i.e. if style has a relative value):
1600 *
1601 * If parent is set to an absolute value, then set style to the computed value.
1602 *
1603 * Otherwise, calculate the combined relative value (e.g. multiplying the two percentages).
1604 */
1606 /* We do font-size first, to ensure that em size is up-to-date. */
1607 /** \todo
1608 * fixme: We'll need to have more font-related things up the top once
1609 * we're getting x-height from pango or libnrtype.
1610 */
1612 /* Some things that allow relative specifications. */
1613 {
1614 /* font-size. Note that we update the computed font-size of style,
1615 to assist in em calculations later in this function. */
1616 if (parent->font_size.set && !parent->font_size.inherit) {
1617 if (!style->font_size.set || style->font_size.inherit) {
1618 /* font_size inherits the computed value, so we can use the parent value
1619 * verbatim. */
1620 style->font_size = parent->font_size;
1621 } else if ( style->font_size.type == SP_FONT_SIZE_LENGTH ) {
1622 /* Child already has absolute size (stored in computed value), so do nothing. */
1623 } else if ( style->font_size.type == SP_FONT_SIZE_LITERAL
1624 && style->font_size.value < SP_CSS_FONT_SIZE_SMALLER ) {
1625 /* Child already has absolute size, but we ensure that the computed value
1626 is up-to-date. */
1627 unsigned const ix = style->font_size.value;
1628 g_assert(ix < G_N_ELEMENTS(font_size_table));
1629 style->font_size.computed = font_size_table[ix];
1630 } else {
1631 /* Child has relative size. */
1632 double const child_frac(get_relative_font_size_frac(style->font_size));
1633 style->font_size.set = true;
1634 style->font_size.inherit = false;
1635 style->font_size.computed = parent->font_size.computed * child_frac;
1637 if ( ( parent->font_size.type == SP_FONT_SIZE_LITERAL
1638 && parent->font_size.value < SP_CSS_FONT_SIZE_SMALLER )
1639 || parent->font_size.type == SP_FONT_SIZE_LENGTH )
1640 {
1641 /* Absolute value. */
1642 style->font_size.type = SP_FONT_SIZE_LENGTH;
1643 /* .value is unused for SP_FONT_SIZE_LENGTH. */
1644 } else {
1645 /* Relative value. */
1646 double const parent_frac(get_relative_font_size_frac(parent->font_size));
1647 style->font_size.type = SP_FONT_SIZE_PERCENTAGE;
1648 style->font_size.value = SP_F8_16_FROM_FLOAT(parent_frac * child_frac);
1649 }
1650 }
1651 }
1653 /* 'font-stretch' */
1654 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_stretch,
1655 parent->font_stretch,
1656 SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
1657 SP_CSS_FONT_STRETCH_NARROWER);
1659 /* font-weight */
1660 sp_style_merge_rel_enum_prop_from_dying_parent(style->font_weight,
1661 parent->font_weight,
1662 SP_CSS_FONT_WEIGHT_900,
1663 SP_CSS_FONT_WEIGHT_LIGHTER);
1664 }
1667 /* Enum values that don't have any relative settings (other than `inherit'). */
1668 {
1669 SPIEnum SPStyle::*const fields[] = {
1670 //nyi: SPStyle::clip_rule,
1671 //nyi: SPStyle::color_interpolation,
1672 //nyi: SPStyle::color_interpolation_filters,
1673 //nyi: SPStyle::color_rendering,
1674 &SPStyle::direction,
1675 &SPStyle::fill_rule,
1676 &SPStyle::font_style,
1677 &SPStyle::font_variant,
1678 //nyi: SPStyle::image_rendering,
1679 //nyi: SPStyle::pointer_events,
1680 //nyi: SPStyle::shape_rendering,
1681 &SPStyle::stroke_linecap,
1682 &SPStyle::stroke_linejoin,
1683 &SPStyle::text_anchor,
1684 //nyi: &SPStyle::text_rendering,
1685 &SPStyle::visibility,
1686 &SPStyle::writing_mode
1687 };
1689 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1690 SPIEnum SPStyle::*const fld = fields[i];
1691 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->*fld, parent->*fld);
1692 }
1693 }
1695 /* A few other simple inheritance properties. */
1696 {
1697 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->fill_opacity, parent->fill_opacity);
1698 sp_style_merge_prop_from_dying_parent<SPIScale24>(style->stroke_opacity, parent->stroke_opacity);
1699 sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
1701 /** \todo
1702 * We currently treat text-decoration as if it were a simple inherited
1703 * property (fixme). This code may need changing once we do the
1704 * special fill/stroke inheritance mentioned by the spec.
1705 */
1706 sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
1707 parent->text_decoration);
1709 //nyi: font-size-adjust, // <number> | none | inherit
1710 //nyi: glyph-orientation-horizontal,
1711 //nyi: glyph-orientation-vertical,
1712 }
1714 /* Properties that involve length but are easy in other respects. */
1715 {
1716 /* The difficulty with lengths is that font-relative units need adjusting if the font
1717 * varies between parent & child.
1718 *
1719 * Lengths specified in the existing child can stay as they are: its computed font
1720 * specification should stay unchanged, so em & ex lengths should continue to mean the same
1721 * size.
1722 *
1723 * Lengths specified in the dying parent in em or ex need to be scaled according to the
1724 * ratio of em or ex size between parent & child.
1725 */
1726 double const parent_child_em_ratio = parent->font_size.computed / style->font_size.computed;
1728 SPILength SPStyle::*const lfields[] = {
1729 &SPStyle::stroke_width,
1730 &SPStyle::text_indent
1731 };
1732 for (unsigned i = 0; i < G_N_ELEMENTS(lfields); ++i) {
1733 SPILength SPStyle::*fld = lfields[i];
1734 sp_style_merge_length_prop_from_dying_parent<SPILength>(style->*fld,
1735 parent->*fld,
1736 parent_child_em_ratio);
1737 }
1739 SPILengthOrNormal SPStyle::*const nfields[] = {
1740 &SPStyle::letter_spacing,
1741 &SPStyle::line_height,
1742 &SPStyle::word_spacing
1743 };
1744 for (unsigned i = 0; i < G_N_ELEMENTS(nfields); ++i) {
1745 SPILengthOrNormal SPStyle::*fld = nfields[i];
1746 sp_style_merge_length_prop_from_dying_parent<SPILengthOrNormal>(style->*fld,
1747 parent->*fld,
1748 parent_child_em_ratio);
1749 }
1751 //nyi: &SPStyle::kerning: length or `auto'
1753 /* fixme: Move stroke-dash and stroke-dash-offset here once they
1754 can accept units. */
1755 }
1757 /* Properties that involve a URI but are easy in other respects. */
1758 {
1759 /** \todo
1760 * Could cause problems if original object was in another document
1761 * and it used a relative URL. (At the time of writing, we don't
1762 * allow hrefs to other documents, so this isn't a problem yet.)
1763 * Paint properties also allow URIs.
1764 */
1765 //nyi: cursor, // may involve change in effect, but we can't do much better
1766 //nyi: color-profile,
1768 // Markers (marker-start, marker-mid, marker-end).
1769 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
1770 sp_style_merge_string_prop_from_dying_parent(style->marker[i], parent->marker[i]);
1771 }
1772 }
1774 /* CSS2 */
1775 /* Font */
1777 if (style->text && parent->text) {
1778 sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
1779 parent->text->font_family);
1780 }
1782 /* Properties that don't inherit by default. Most of these need special handling. */
1783 {
1784 /*
1785 * opacity's effect is cumulative; we set the new value to the combined effect. The
1786 * default value for opacity is 1.0, not inherit. (Note that stroke-opacity and
1787 * fill-opacity are quite different from opacity, and don't need any special handling.)
1788 *
1789 * Cases:
1790 * - parent & child were each previously unset, in which case the effective
1791 * opacity value is 1.0, and style should remain unset.
1792 * - parent was previously unset (so computed opacity value of 1.0)
1793 * and child was set to inherit. The merged child should
1794 * get a value of 1.0, and shouldn't inherit (lest the new parent
1795 * has a different opacity value). Given that opacity's default
1796 * value is 1.0 (rather than inherit), we might as well have the
1797 * merged child's opacity be unset.
1798 * - parent was previously unset (so opacity 1.0), and child was set to a number.
1799 * The merged child should retain its existing settings (though it doesn't matter
1800 * if we make it unset if that number was 1.0).
1801 * - parent was inherit and child was unset. Merged child should be set to inherit.
1802 * - parent was inherit and child was inherit. (We can't in general reproduce this
1803 * effect (short of introducing a new group), but setting opacity to inherit is rare.)
1804 * If the inherited value was strictly between 0.0 and 1.0 (exclusive) then the merged
1805 * child's value should be set to the product of the two, i.e. the square of the
1806 * inherited value, and should not be marked as inherit. (This decision assumes that it
1807 * is more important to retain the effective opacity than to retain the inheriting
1808 * effect, and assumes that the inheriting effect either isn't important enough to create
1809 * a group or isn't common enough to bother maintaining the code to create a group.) If
1810 * the inherited value was 0.0 or 1.0, then marking the merged child as inherit comes
1811 * closer to maintaining the effect.
1812 * - parent was inherit and child was set to a numerical value. If the child's value
1813 * was 1.0, then the merged child should have the same settings as the parent.
1814 * If the child's value was 0, then the merged child should also be set to 0.
1815 * If the child's value was anything else, then we do the same as for the inherit/inherit
1816 * case above: have the merged child set to the product of the two opacities and not
1817 * marked as inherit, for the same reasons as for that case.
1818 * - parent was set to a value, and child was unset. The merged child should have
1819 * parent's settings.
1820 * - parent was set to a value, and child was inherit. The merged child should
1821 * be set to the product, i.e. the square of the parent's value.
1822 * - parent & child are each set to a value. The merged child should be set to the
1823 * product.
1824 */
1825 if ( !style->opacity.set
1826 || ( !style->opacity.inherit
1827 && style->opacity.value == SP_SCALE24_MAX ) )
1828 {
1829 style->opacity = parent->opacity;
1830 } else {
1831 /* Ensure that style's computed value is up-to-date. */
1832 if (style->opacity.inherit) {
1833 style->opacity.value = parent->opacity.value;
1834 }
1836 /* Multiplication of opacities occurs even if a child's opacity is set to inherit. */
1837 style->opacity.value = SP_SCALE24_MUL(style->opacity.value,
1838 parent->opacity.value);
1840 style->opacity.inherit = (parent->opacity.inherit
1841 && style->opacity.inherit
1842 && (parent->opacity.value == 0 ||
1843 parent->opacity.value == SP_SCALE24_MAX));
1844 style->opacity.set = ( style->opacity.inherit
1845 || style->opacity.value < SP_SCALE24_MAX );
1846 }
1848 /* display is in principle similar to opacity, but implementation is easier. */
1849 if ( parent->display.set && !parent->display.inherit
1850 && parent->display.value == SP_CSS_DISPLAY_NONE ) {
1851 style->display.value = SP_CSS_DISPLAY_NONE;
1852 style->display.set = true;
1853 style->display.inherit = false;
1854 } else if (style->display.inherit) {
1855 style->display.value = parent->display.value;
1856 style->display.set = parent->display.set;
1857 style->display.inherit = parent->display.inherit;
1858 } else {
1859 /* Leave as is. (display doesn't inherit by default.) */
1860 }
1862 /** \todo
1863 * fixme: Check that we correctly handle all properties that don't
1864 * inherit by default (as shown in
1865 * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
1866 */
1867 }
1869 /* SPIPaint properties (including color). */
1870 {
1871 /** \todo
1872 * Think about the issues involved if specified as currentColor or
1873 * if specified relative to colorProfile, and if the currentColor or
1874 * colorProfile differs between parent \& child. See also comments
1875 * elsewhere in this function about URIs.
1876 */
1877 SPIPaint SPStyle::*const fields[] = {
1878 &SPStyle::color,
1879 &SPStyle::fill,
1880 &SPStyle::stroke
1881 };
1882 for (unsigned i = 0; i < G_N_ELEMENTS(fields); ++i) {
1883 SPIPaint SPStyle::*const fld = fields[i];
1884 sp_style_merge_paint_prop_from_dying_parent(style, style->*fld, parent->*fld);
1885 }
1886 }
1888 /* Things from SVG 1.2 or CSS3. */
1889 {
1890 /* Note: If we ever support setting string values for text-align then we'd need strdup
1891 * handling here. */
1892 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_align, parent->text_align);
1894 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->text_transform, parent->text_transform);
1895 sp_style_merge_prop_from_dying_parent<SPIEnum>(style->block_progression, parent->block_progression);
1896 }
1898 /* Note: this will need length handling once dasharray supports units. */
1899 if ( ( !style->stroke_dasharray_set || style->stroke_dasharray_inherit )
1900 && parent->stroke_dasharray_set && !parent->stroke_dasharray_inherit )
1901 {
1902 style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
1903 if (style->stroke_dash.n_dash > 0) {
1904 style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
1905 memcpy(style->stroke_dash.dash, parent->stroke_dash.dash, style->stroke_dash.n_dash * sizeof(gdouble));
1906 }
1907 style->stroke_dasharray_set = parent->stroke_dasharray_set;
1908 style->stroke_dasharray_inherit = parent->stroke_dasharray_inherit;
1909 }
1911 /* Note: this will need length handling once dasharray_offset supports units. */
1912 if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
1913 style->stroke_dash.offset = parent->stroke_dash.offset;
1914 style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
1915 /* fixme: we don't currently allow stroke-dashoffset to be `inherit'. TODO: Try to
1916 * represent it as a normal SPILength; though will need to do something about existing
1917 * users of stroke_dash.offset and stroke_dashoffset_set. */
1918 }
1919 }
1923 /**
1924 * Disconnects from possible fill and stroke paint servers.
1925 */
1926 static void
1927 sp_style_paint_server_release(SPPaintServer *server, SPStyle *style)
1928 {
1929 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1930 && (server == style->fill.value.paint.server))
1931 {
1932 sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
1933 }
1935 if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
1936 && (server == style->stroke.value.paint.server))
1937 {
1938 sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
1939 }
1940 }
1945 /**
1946 * Emit style modified signal on style's object if server is style's fill
1947 * or stroke paint server.
1948 */
1949 static void
1950 sp_style_paint_server_modified(SPPaintServer *server, guint flags, SPStyle *style)
1951 {
1952 if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1953 && (server == style->fill.value.paint.server))
1954 {
1955 if (style->object) {
1956 /** \todo
1957 * fixme: I do not know, whether it is optimal - we are
1958 * forcing reread of everything (Lauris)
1959 */
1960 /** \todo
1961 * fixme: We have to use object_modified flag, because parent
1962 * flag is only available downstreams.
1963 */
1964 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1965 }
1966 } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
1967 && (server == style->stroke.value.paint.server))
1968 {
1969 if (style->object) {
1970 /// \todo fixme:
1971 style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1972 }
1973 } else {
1974 g_assert_not_reached();
1975 }
1976 }
1980 /**
1981 *
1982 */
1983 static void
1984 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
1985 {
1986 sp_style_paint_clear(style, paint, TRUE, FALSE);
1988 if ((paint->set && paint->currentcolor) || parent->currentcolor) {
1989 paint->currentcolor = TRUE;
1990 paint->type = SP_PAINT_TYPE_COLOR;
1991 sp_color_copy(&paint->value.color, &style->color.value.color);
1992 return;
1993 }
1995 paint->type = parent->type;
1996 switch (paint->type) {
1997 case SP_PAINT_TYPE_COLOR:
1998 sp_color_copy(&paint->value.color, &parent->value.color);
1999 break;
2000 case SP_PAINT_TYPE_PAINTSERVER:
2001 paint->value.paint.server = parent->value.paint.server;
2002 paint->value.paint.uri = parent->value.paint.uri;
2003 if (paint->value.paint.server) {
2004 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
2005 style->hreffed = true;
2006 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2007 }
2008 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
2009 g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
2010 G_CALLBACK(sp_style_paint_server_release), style);
2011 g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
2012 G_CALLBACK(sp_style_paint_server_modified), style);
2013 style->listening = true;
2014 }
2015 }
2016 break;
2017 case SP_PAINT_TYPE_NONE:
2018 break;
2019 default:
2020 g_assert_not_reached();
2021 break;
2022 }
2023 }
2026 /**
2027 * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
2028 * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
2029 * complete cascaded style to style_clipboard. When you need a CSS string
2030 * for an object in the document tree, you normally call
2031 * sp_style_write_difference instead to take into account the object's parent.
2032 *
2033 * \pre style != NULL.
2034 * \pre flags in {IFSET, ALWAYS}.
2035 * \post ret != NULL.
2036 */
2037 gchar *
2038 sp_style_write_string(SPStyle const *const style, guint const flags)
2039 {
2040 /** \todo
2041 * Merge with write_difference, much duplicate code!
2042 */
2043 g_return_val_if_fail(style != NULL, NULL);
2044 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
2045 (flags == SP_STYLE_FLAG_ALWAYS) ),
2046 NULL);
2048 gchar c[BMAX];
2049 gchar *p = c;
2050 *p = '\0';
2052 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &style->font_size, NULL, flags);
2053 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &style->font_style, NULL, flags);
2054 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &style->font_variant, NULL, flags);
2055 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &style->font_weight, NULL, flags);
2056 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &style->font_stretch, NULL, flags);
2058 /* Text */
2059 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &style->text_indent, NULL, flags);
2060 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &style->text_align, NULL, flags);
2061 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &style->text_decoration, NULL, flags);
2062 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &style->line_height, NULL, flags);
2063 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &style->letter_spacing, NULL, flags);
2064 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &style->word_spacing, NULL, flags);
2065 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &style->text_transform, NULL, flags);
2066 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &style->direction, NULL, flags);
2067 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &style->block_progression, NULL, flags);
2068 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
2070 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
2072 /// \todo fixme: Per type methods need default flag too (lauris)
2073 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
2074 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
2075 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
2076 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
2077 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
2078 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
2079 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
2080 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
2081 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
2083 marker_status("sp_style_write_string: Writing markers");
2084 if (style->marker[SP_MARKER_LOC].set) {
2085 p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
2086 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2087 p += g_snprintf(p, c + BMAX - p, "marker:none;");
2088 }
2089 if (style->marker[SP_MARKER_LOC_START].set) {
2090 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
2091 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2092 p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
2093 }
2094 if (style->marker[SP_MARKER_LOC_MID].set) {
2095 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
2096 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2097 p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
2098 }
2099 if (style->marker[SP_MARKER_LOC_END].set) {
2100 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
2101 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2102 p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
2103 }
2105 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
2107 /** \todo fixme: */
2108 if ((flags == SP_STYLE_FLAG_ALWAYS)
2109 || style->stroke_dasharray_set)
2110 {
2111 if (style->stroke_dasharray_inherit) {
2112 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2113 } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
2114 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2115 gint i;
2116 for (i = 0; i < style->stroke_dash.n_dash; i++) {
2117 Inkscape::CSSOStringStream os;
2118 if (i) {
2119 os << ", ";
2120 }
2121 os << style->stroke_dash.dash[i];
2122 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2123 }
2124 if (p < c + BMAX) {
2125 *p++ = ';';
2126 }
2127 } else {
2128 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
2129 }
2130 }
2132 /** \todo fixme: */
2133 if (style->stroke_dashoffset_set) {
2134 Inkscape::CSSOStringStream os;
2135 os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
2136 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2137 } else if (flags == SP_STYLE_FLAG_ALWAYS) {
2138 p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
2139 }
2141 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
2143 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, flags);
2144 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
2145 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
2147 /* fixme: */
2148 p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
2150 /* Get rid of trailing `;'. */
2151 if (p != c) {
2152 --p;
2153 if (*p == ';') {
2154 *p = '\0';
2155 }
2156 }
2158 return g_strdup(c);
2159 }
2162 #define STYLE_BUF_MAX
2165 /**
2166 * Dumps style to CSS string, see sp_style_write_string()
2167 *
2168 * \pre from != NULL.
2169 * \pre to != NULL.
2170 * \post ret != NULL.
2171 */
2172 gchar *
2173 sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
2174 {
2175 g_return_val_if_fail(from != NULL, NULL);
2176 g_return_val_if_fail(to != NULL, NULL);
2178 gchar c[BMAX], *p = c;
2179 *p = '\0';
2181 p += sp_style_write_ifontsize(p, c + BMAX - p, "font-size", &from->font_size, &to->font_size, SP_STYLE_FLAG_IFDIFF);
2182 p += sp_style_write_ienum(p, c + BMAX - p, "font-style", enum_font_style, &from->font_style, &to->font_style, SP_STYLE_FLAG_IFDIFF);
2183 p += sp_style_write_ienum(p, c + BMAX - p, "font-variant", enum_font_variant, &from->font_variant, &to->font_variant, SP_STYLE_FLAG_IFDIFF);
2184 p += sp_style_write_ienum(p, c + BMAX - p, "font-weight", enum_font_weight, &from->font_weight, &to->font_weight, SP_STYLE_FLAG_IFDIFF);
2185 p += sp_style_write_ienum(p, c + BMAX - p, "font-stretch", enum_font_stretch, &from->font_stretch, &to->font_stretch, SP_STYLE_FLAG_IFDIFF);
2187 /* Text */
2188 p += sp_style_write_ilength(p, c + BMAX - p, "text-indent", &from->text_indent, &to->text_indent, SP_STYLE_FLAG_IFDIFF);
2189 p += sp_style_write_ienum(p, c + BMAX - p, "text-align", enum_text_align, &from->text_align, &to->text_align, SP_STYLE_FLAG_IFDIFF);
2190 p += sp_style_write_itextdecoration(p, c + BMAX - p, "text-decoration", &from->text_decoration, &to->text_decoration, SP_STYLE_FLAG_IFDIFF);
2191 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "line-height", &from->line_height, &to->line_height, SP_STYLE_FLAG_IFDIFF);
2192 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "letter-spacing", &from->letter_spacing, &to->letter_spacing, SP_STYLE_FLAG_IFDIFF);
2193 p += sp_style_write_ilengthornormal(p, c + BMAX - p, "word-spacing", &from->word_spacing, &to->word_spacing, SP_STYLE_FLAG_IFDIFF);
2194 p += sp_style_write_ienum(p, c + BMAX - p, "text-transform", enum_text_transform, &from->text_transform, &to->text_transform, SP_STYLE_FLAG_IFDIFF);
2195 p += sp_style_write_ienum(p, c + BMAX - p, "direction", enum_direction, &from->direction, &to->direction, SP_STYLE_FLAG_IFDIFF);
2196 p += sp_style_write_ienum(p, c + BMAX - p, "block-progression", enum_block_progression, &from->block_progression, &to->block_progression, SP_STYLE_FLAG_IFDIFF);
2197 p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
2199 p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
2201 /// \todo fixme: Per type methods need default flag too
2202 if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
2203 p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
2204 }
2205 p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
2206 p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
2207 p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
2208 p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
2209 p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
2210 p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
2211 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
2212 &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
2213 p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
2214 &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
2215 p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
2216 &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
2217 /** \todo fixme: */
2218 if (from->stroke_dasharray_set) {
2219 if (from->stroke_dasharray_inherit) {
2220 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
2221 } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
2222 p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
2223 for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
2224 Inkscape::CSSOStringStream os;
2225 if (i) {
2226 os << ", ";
2227 }
2228 os << from->stroke_dash.dash[i];
2229 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2230 }
2231 p += g_snprintf(p, c + BMAX - p, ";");
2232 }
2233 }
2234 /* fixme: */
2235 if (from->stroke_dashoffset_set) {
2236 Inkscape::CSSOStringStream os;
2237 os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
2238 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
2239 }
2240 p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
2242 /* markers */
2243 marker_status("sp_style_write_difference: Writing markers");
2244 if (from->marker[SP_MARKER_LOC].value != NULL) {
2245 p += g_snprintf(p, c + BMAX - p, "marker:%s;", from->marker[SP_MARKER_LOC].value);
2246 }
2247 if (from->marker[SP_MARKER_LOC_START].value != NULL) {
2248 p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
2249 }
2250 if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
2251 p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", from->marker[SP_MARKER_LOC_MID].value);
2252 }
2253 if (from->marker[SP_MARKER_LOC_END].value != NULL) {
2254 p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", from->marker[SP_MARKER_LOC_END].value);
2255 }
2257 p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &from->visibility, &to->visibility, SP_STYLE_FLAG_IFSET);
2258 p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
2259 p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
2261 p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
2263 /** \todo
2264 * The reason we use IFSET rather than IFDIFF is the belief that the IFDIFF
2265 * flag is mainly only for attributes that don't handle explicit unset well.
2266 * We may need to revisit the behaviour of this routine.
2267 */
2269 /* Get rid of trailing `;'. */
2270 if (p != c) {
2271 --p;
2272 if (*p == ';') {
2273 *p = '\0';
2274 }
2275 }
2277 return g_strdup(c);
2278 }
2282 /**
2283 * Reset all style properties.
2284 */
2285 static void
2286 sp_style_clear(SPStyle *style)
2287 {
2288 g_return_if_fail(style != NULL);
2290 sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
2291 sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
2292 if (style->stroke_dash.dash) {
2293 g_free(style->stroke_dash.dash);
2294 }
2296 /** \todo fixme: Do that text manipulation via parents */
2297 SPObject *object = style->object;
2298 gint const refcount = style->refcount;
2299 SPTextStyle *text = style->text;
2300 unsigned const text_private = style->text_private;
2301 memset(style, 0, sizeof(SPStyle));
2302 style->refcount = refcount;
2303 style->object = object;
2304 style->text = text;
2305 style->text_private = text_private;
2306 /* fixme: */
2307 style->text->font.set = FALSE;
2308 style->text->font_family.set = FALSE;
2310 style->font_size.set = FALSE;
2311 style->font_size.type = SP_FONT_SIZE_LITERAL;
2312 style->font_size.value = SP_CSS_FONT_SIZE_MEDIUM;
2313 style->font_size.computed = 12.0;
2314 style->font_style.set = FALSE;
2315 style->font_style.value = style->font_style.computed = SP_CSS_FONT_STYLE_NORMAL;
2316 style->font_variant.set = FALSE;
2317 style->font_variant.value = style->font_variant.computed = SP_CSS_FONT_VARIANT_NORMAL;
2318 style->font_weight.set = FALSE;
2319 style->font_weight.value = SP_CSS_FONT_WEIGHT_NORMAL;
2320 style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
2321 style->font_stretch.set = FALSE;
2322 style->font_stretch.value = style->font_stretch.computed = SP_CSS_FONT_STRETCH_NORMAL;
2324 /* text */
2325 style->text_indent.set = FALSE;
2326 style->text_indent.unit = SP_CSS_UNIT_NONE;
2327 style->text_indent.computed = 0.0;
2329 style->text_align.set = FALSE;
2330 style->text_align.value = style->text_align.computed = SP_CSS_TEXT_ALIGN_START;
2332 style->text_decoration.set = FALSE;
2333 style->text_decoration.underline = FALSE;
2334 style->text_decoration.overline = FALSE;
2335 style->text_decoration.line_through = FALSE;
2336 style->text_decoration.blink = FALSE;
2338 style->line_height.set = FALSE;
2339 style->line_height.unit = SP_CSS_UNIT_PERCENT;
2340 style->line_height.normal = TRUE;
2341 style->line_height.value = style->line_height.computed = 1.0;
2343 style->letter_spacing.set = FALSE;
2344 style->letter_spacing.unit = SP_CSS_UNIT_NONE;
2345 style->letter_spacing.normal = TRUE;
2346 style->letter_spacing.value = style->letter_spacing.computed = 0.0;
2348 style->word_spacing.set = FALSE;
2349 style->word_spacing.unit = SP_CSS_UNIT_NONE;
2350 style->word_spacing.normal = TRUE;
2351 style->word_spacing.value = style->word_spacing.computed = 0.0;
2353 style->text_transform.set = FALSE;
2354 style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
2356 style->direction.set = FALSE;
2357 style->direction.value = style->direction.computed = SP_CSS_DIRECTION_LTR;
2359 style->block_progression.set = FALSE;
2360 style->block_progression.value = style->block_progression.computed = SP_CSS_BLOCK_PROGRESSION_TB;
2362 style->writing_mode.set = FALSE;
2363 style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
2366 style->text_anchor.set = FALSE;
2367 style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
2370 style->opacity.value = SP_SCALE24_MAX;
2371 style->visibility.set = FALSE;
2372 style->visibility.value = style->visibility.computed = SP_CSS_VISIBILITY_VISIBLE;
2373 style->display.set = FALSE;
2374 style->display.value = style->display.computed = SP_CSS_DISPLAY_INLINE;
2375 style->overflow.set = FALSE;
2376 style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
2378 style->color.type = SP_PAINT_TYPE_COLOR;
2379 sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
2381 style->fill.type = SP_PAINT_TYPE_COLOR;
2382 sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
2383 style->fill_opacity.value = SP_SCALE24_MAX;
2384 style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
2386 style->stroke.type = SP_PAINT_TYPE_NONE;
2387 style->stroke.set = FALSE;
2388 sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
2389 style->stroke_opacity.value = SP_SCALE24_MAX;
2391 style->stroke_width.set = FALSE;
2392 style->stroke_width.unit = SP_CSS_UNIT_NONE;
2393 style->stroke_width.computed = 1.0;
2395 style->stroke_linecap.set = FALSE;
2396 style->stroke_linecap.value = style->stroke_linecap.computed = SP_STROKE_LINECAP_BUTT;
2397 style->stroke_linejoin.set = FALSE;
2398 style->stroke_linejoin.value = style->stroke_linejoin.computed = SP_STROKE_LINEJOIN_MITER;
2400 style->stroke_miterlimit.set = FALSE;
2401 style->stroke_miterlimit.value = 4.0;
2403 style->stroke_dash.n_dash = 0;
2404 style->stroke_dash.dash = NULL;
2405 style->stroke_dash.offset = 0.0;
2407 for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
2408 g_free(style->marker[i].value);
2409 style->marker[i].set = FALSE;
2410 }
2411 }
2415 /**
2416 *
2417 */
2418 static void
2419 sp_style_read_dash(SPStyle *style, gchar const *str)
2420 {
2421 /* Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty */
2422 style->stroke_dasharray_set = TRUE;
2424 if (strcmp(str, "inherit") == 0) {
2425 style->stroke_dasharray_inherit = true;
2426 return;
2427 }
2428 style->stroke_dasharray_inherit = false;
2430 NRVpathDash &dash = style->stroke_dash;
2431 g_free(dash.dash);
2432 dash.dash = NULL;
2434 if (strcmp(str, "none") == 0) {
2435 dash.n_dash = 0;
2436 return;
2437 }
2439 gint n_dash = 0;
2440 gdouble d[64];
2441 gchar *e = NULL;
2443 bool LineSolid=true;
2444 while (e != str && n_dash < 64) {
2445 /* TODO: Should allow <length> rather than just a unitless (px) number. */
2446 d[n_dash] = g_ascii_strtod(str, (char **) &e);
2447 if (d[n_dash] > 0.00000001)
2448 LineSolid = false;
2449 if (e != str) {
2450 n_dash += 1;
2451 str = e;
2452 }
2453 while (str && *str && !isalnum(*str)) str += 1;
2454 }
2456 if (LineSolid) {
2457 dash.n_dash = 0;
2458 return;
2459 }
2461 if (n_dash > 0) {
2462 dash.dash = g_new(gdouble, n_dash);
2463 memcpy(dash.dash, d, sizeof(gdouble) * n_dash);
2464 dash.n_dash = n_dash;
2465 }
2466 }
2469 /*#########################
2470 ## SPTextStyle operations
2471 #########################*/
2474 /**
2475 * Return new SPTextStyle object with default settings.
2476 */
2477 static SPTextStyle *
2478 sp_text_style_new()
2479 {
2480 SPTextStyle *ts = g_new0(SPTextStyle, 1);
2481 ts->refcount = 1;
2482 sp_text_style_clear(ts);
2484 ts->font.value = g_strdup("Bitstream Vera Sans");
2485 ts->font_family.value = g_strdup("Bitstream Vera Sans");
2487 return ts;
2488 }
2491 /**
2492 * Clear text style settings.
2493 */
2494 static void
2495 sp_text_style_clear(SPTextStyle *ts)
2496 {
2497 ts->font.set = FALSE;
2498 ts->font_family.set = FALSE;
2499 }
2503 /**
2504 * Reduce refcount of text style and possibly free it.
2505 */
2506 static SPTextStyle *
2507 sp_text_style_unref(SPTextStyle *st)
2508 {
2509 st->refcount -= 1;
2511 if (st->refcount < 1) {
2512 g_free(st->font.value);
2513 g_free(st->font_family.value);
2514 g_free(st);
2515 }
2517 return NULL;
2518 }
2522 /**
2523 * Return duplicate of text style.
2524 */
2525 static SPTextStyle *
2526 sp_text_style_duplicate_unset(SPTextStyle *st)
2527 {
2528 SPTextStyle *nt = g_new0(SPTextStyle, 1);
2529 nt->refcount = 1;
2531 nt->font.value = g_strdup(st->font.value);
2532 nt->font_family.value = g_strdup(st->font_family.value);
2534 return nt;
2535 }
2539 /**
2540 * Write SPTextStyle object into string.
2541 */
2542 static guint
2543 sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guint flags)
2544 {
2545 gint d = 0;
2547 // We do not do diffing for text style
2548 if (flags == SP_STYLE_FLAG_IFDIFF)
2549 flags = SP_STYLE_FLAG_IFSET;
2551 d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
2552 return d;
2553 }
2557 /* The following sp_tyle_read_* functions ignore invalid values, as per
2558 * http://www.w3.org/TR/REC-CSS2/syndata.html#parsing-errors.
2559 *
2560 * [However, the SVG spec is somewhat unclear as to whether the style attribute should
2561 * be handled as per CSS2 rules or whether it must simply be a set of PROPERTY:VALUE
2562 * pairs, in which case SVG's error-handling rules
2563 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing should instead be applied.]
2564 */
2567 /**
2568 * Set SPIFloat object from string.
2569 */
2570 static void
2571 sp_style_read_ifloat(SPIFloat *val, gchar const *str)
2572 {
2573 if (!strcmp(str, "inherit")) {
2574 val->set = TRUE;
2575 val->inherit = TRUE;
2576 } else {
2577 gfloat value;
2578 if (sp_svg_number_read_f(str, &value)) {
2579 val->set = TRUE;
2580 val->inherit = FALSE;
2581 val->value = value;
2582 }
2583 }
2584 }
2588 /**
2589 * Set SPIScale24 object from string.
2590 */
2591 static void
2592 sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
2593 {
2594 if (!strcmp(str, "inherit")) {
2595 val->set = TRUE;
2596 val->inherit = TRUE;
2597 } else {
2598 gfloat value;
2599 if (sp_svg_number_read_f(str, &value)) {
2600 val->set = TRUE;
2601 val->inherit = FALSE;
2602 value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
2603 val->value = SP_SCALE24_FROM_FLOAT(value);
2604 }
2605 }
2606 }
2608 /**
2609 * Reads a style value and performs lookup based on the given style value enumerations.
2610 */
2611 static void
2612 sp_style_read_ienum(SPIEnum *val, gchar const *str, SPStyleEnum const *dict,
2613 bool const can_explicitly_inherit)
2614 {
2615 if ( can_explicitly_inherit && !strcmp(str, "inherit") ) {
2616 val->set = TRUE;
2617 val->inherit = TRUE;
2618 } else {
2619 for (unsigned i = 0; dict[i].key; i++) {
2620 if (!strcmp(str, dict[i].key)) {
2621 val->set = TRUE;
2622 val->inherit = FALSE;
2623 val->value = dict[i].value;
2624 /* Save copying for values not needing it */
2625 val->computed = val->value;
2626 break;
2627 }
2628 }
2629 }
2630 }
2634 /**
2635 * Set SPIString object from string.
2636 */
2637 static void
2638 sp_style_read_istring(SPIString *val, gchar const *str)
2639 {
2640 g_free(val->value);
2642 if (!strcmp(str, "inherit")) {
2643 val->set = TRUE;
2644 val->inherit = TRUE;
2645 val->value = NULL;
2646 } else {
2647 val->set = TRUE;
2648 val->inherit = FALSE;
2649 val->value = g_strdup(str);
2650 }
2651 }
2655 /**
2656 * Set SPILength object from string.
2657 */
2658 static void
2659 sp_style_read_ilength(SPILength *val, gchar const *str)
2660 {
2661 if (!strcmp(str, "inherit")) {
2662 val->set = TRUE;
2663 val->inherit = TRUE;
2664 } else {
2665 gdouble value;
2666 gchar *e;
2667 /** \todo fixme: Move this to standard place (Lauris) */
2668 value = g_ascii_strtod(str, &e);
2669 if ((gchar const *) e != str) {
2670 /** \todo
2671 * Allow the number of px per inch to vary (document preferences,
2672 * X server or whatever). E.g. don't fill in computed here, do
2673 * it at the same time as percentage units are done.
2674 */
2675 if (!*e) {
2676 /* Userspace */
2677 val->unit = SP_CSS_UNIT_NONE;
2678 val->computed = value;
2679 } else if (!strcmp(e, "px")) {
2680 /* Userspace */
2681 val->unit = SP_CSS_UNIT_PX;
2682 val->computed = value;
2683 } else if (!strcmp(e, "pt")) {
2684 /* Userspace / DEVICESCALE */
2685 val->unit = SP_CSS_UNIT_PT;
2686 val->computed = value * PX_PER_PT;
2687 } else if (!strcmp(e, "pc")) {
2688 /* 1 pica = 12pt; FIXME: add it to SPUnit */
2689 val->unit = SP_CSS_UNIT_PC;
2690 val->computed = value * PX_PER_PT * 12;
2691 } else if (!strcmp(e, "mm")) {
2692 val->unit = SP_CSS_UNIT_MM;
2693 val->computed = value * PX_PER_MM;
2694 } else if (!strcmp(e, "cm")) {
2695 val->unit = SP_CSS_UNIT_CM;
2696 val->computed = value * PX_PER_CM;
2697 } else if (!strcmp(e, "in")) {
2698 val->unit = SP_CSS_UNIT_IN;
2699 val->computed = value * PX_PER_IN;
2700 } else if (!strcmp(e, "em")) {
2701 /* EM square */
2702 val->unit = SP_CSS_UNIT_EM;
2703 val->value = value;
2704 } else if (!strcmp(e, "ex")) {
2705 /* ex square */
2706 val->unit = SP_CSS_UNIT_EX;
2707 val->value = value;
2708 } else if (!strcmp(e, "%")) {
2709 /* Percentage */
2710 val->unit = SP_CSS_UNIT_PERCENT;
2711 val->value = value * 0.01;
2712 } else {
2713 /* Invalid */
2714 return;
2715 }
2716 val->set = TRUE;
2717 val->inherit = FALSE;
2718 }
2719 }
2720 }
2722 /**
2723 * Set SPILengthOrNormal object from string.
2724 */
2725 static void
2726 sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str)
2727 {
2728 if (!strcmp(str, "normal")) {
2729 val->set = TRUE;
2730 val->inherit = FALSE;
2731 val->normal = TRUE;
2732 val->unit = SP_CSS_UNIT_NONE;
2733 val->value = val->computed = 0.0;
2734 } else {
2735 SPILength length;
2736 sp_style_read_ilength(&length, str);
2737 val->set = length.set;
2738 val->inherit = length.inherit;
2739 val->normal = FALSE;
2740 val->unit = length.unit;
2741 val->value = length.value;
2742 val->computed = length.computed;
2743 }
2744 }
2746 /**
2747 * Set SPITextDecoration object from string.
2748 */
2749 static void
2750 sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
2751 {
2752 if (!strcmp(str, "inherit")) {
2753 val->set = TRUE;
2754 val->inherit = TRUE;
2755 } else if (!strcmp(str, "none")) {
2756 val->set = TRUE;
2757 val->inherit = FALSE;
2758 val->underline = FALSE;
2759 val->overline = FALSE;
2760 val->line_through = FALSE;
2761 val->blink = FALSE;
2762 } else {
2763 bool found_underline = false;
2764 bool found_overline = false;
2765 bool found_line_through = false;
2766 bool found_blink = false;
2767 for ( ; *str ; str++ ) {
2768 if (*str == ' ') continue;
2769 if (strneq(str, "underline", 9) && (str[9] == ' ' || str[9] == '\0')) {
2770 found_underline = true;
2771 str += 9;
2772 } else if (strneq(str, "overline", 8) && (str[8] == ' ' || str[8] == '\0')) {
2773 found_overline = true;
2774 str += 8;
2775 } else if (strneq(str, "line-through", 12) && (str[12] == ' ' || str[12] == '\0')) {
2776 found_line_through = true;
2777 str += 12;
2778 } else if (strneq(str, "blink", 5) && (str[5] == ' ' || str[5] == '\0')) {
2779 found_blink = true;
2780 str += 5;
2781 } else {
2782 return; // invalid value
2783 }
2784 }
2785 if (!(found_underline || found_overline || found_line_through || found_blink)) {
2786 return; // invalid value: empty
2787 }
2788 val->set = TRUE;
2789 val->inherit = FALSE;
2790 val->underline = found_underline;
2791 val->overline = found_overline;
2792 val->line_through = found_line_through;
2793 val->blink = found_blink;
2794 }
2795 }
2797 /**
2798 * Set SPIPaint object from string containing an integer value.
2799 * \param document Ignored
2800 */
2801 static void
2802 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2803 {
2804 paint->currentcolor = FALSE; /* currentColor not a valid <color>. */
2805 if (!strcmp(str, "inherit")) {
2806 paint->set = TRUE;
2807 paint->inherit = TRUE;
2808 } else {
2809 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2810 if (rgb0 != 0xff) {
2811 paint->type = SP_PAINT_TYPE_COLOR;
2812 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2813 paint->set = TRUE;
2814 paint->inherit = FALSE;
2815 }
2816 }
2817 }
2820 /**
2821 * Set SPIPaint object from string.
2822 */
2823 static void
2824 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
2825 {
2826 while (isspace(*str)) {
2827 ++str;
2828 }
2830 if (streq(str, "inherit")) {
2831 paint->set = TRUE;
2832 paint->inherit = TRUE;
2833 paint->currentcolor = FALSE;
2834 } else if (streq(str, "currentColor")) {
2835 paint->set = TRUE;
2836 paint->inherit = FALSE;
2837 paint->currentcolor = TRUE;
2838 } else if (streq(str, "none")) {
2839 paint->type = SP_PAINT_TYPE_NONE;
2840 paint->set = TRUE;
2841 paint->inherit = FALSE;
2842 paint->currentcolor = FALSE;
2843 } else if (strneq(str, "url", 3)) {
2844 // this is alloc'd uri, but seems to be shared with a parent
2845 // potentially, so it's not any easy g_free case...
2846 paint->value.paint.uri = extract_uri(str);
2847 if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
2848 paint->type = SP_PAINT_TYPE_NONE;
2849 return;
2850 }
2851 paint->type = SP_PAINT_TYPE_PAINTSERVER;
2852 paint->set = TRUE;
2853 paint->inherit = FALSE;
2854 paint->currentcolor = FALSE;
2855 if (document) {
2856 SPObject *ps = sp_uri_reference_resolve(document, str);
2857 if (ps && SP_IS_PAINT_SERVER(ps)) {
2858 paint->value.paint.server = SP_PAINT_SERVER(ps);
2859 if (style->object && !style->cloned) {
2860 sp_object_href(SP_OBJECT(paint->value.paint.server), style);
2861 style->hreffed = true;
2862 }
2863 if (style->object || style->cloned) {
2864 g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
2865 G_CALLBACK(sp_style_paint_server_release), style);
2866 g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
2867 G_CALLBACK(sp_style_paint_server_modified), style);
2868 style->listening = true;
2869 }
2870 } else {
2871 paint->value.paint.server = NULL;
2872 }
2873 }
2874 } else {
2875 guint32 const rgb0 = sp_svg_read_color(str, 0xff);
2876 if (rgb0 != 0xff) {
2877 paint->type = SP_PAINT_TYPE_COLOR;
2878 sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
2879 paint->set = TRUE;
2880 paint->inherit = FALSE;
2881 paint->currentcolor = FALSE;
2882 }
2883 }
2884 }
2888 /**
2889 * Set SPIFontSize object from string.
2890 */
2891 static void
2892 sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
2893 {
2894 if (!strcmp(str, "inherit")) {
2895 val->set = TRUE;
2896 val->inherit = TRUE;
2897 } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
2898 for (unsigned i = 0; enum_font_size[i].key; i++) {
2899 if (!strcmp(str, enum_font_size[i].key)) {
2900 val->set = TRUE;
2901 val->inherit = FALSE;
2902 val->type = SP_FONT_SIZE_LITERAL;
2903 val->value = enum_font_size[i].value;
2904 return;
2905 }
2906 }
2907 /* Invalid */
2908 return;
2909 } else {
2910 gdouble value;
2911 gchar *e;
2912 /* fixme: Move this to standard place (Lauris) */
2913 value = g_ascii_strtod(str, &e);
2914 if ((gchar const *) e != str) {
2915 if (!*e) {
2916 /* Userspace */
2917 } else if (!strcmp(e, "px")) {
2918 /* Userspace */
2919 } else if (!strcmp(e, "pt")) {
2920 /* Userspace * DEVICESCALE */
2921 value *= PX_PER_PT;
2922 } else if (!strcmp(e, "pc")) {
2923 /* 12pt */
2924 value *= PX_PER_PT * 12.0;
2925 } else if (!strcmp(e, "mm")) {
2926 value *= PX_PER_MM;
2927 } else if (!strcmp(e, "cm")) {
2928 value *= PX_PER_CM;
2929 } else if (!strcmp(e, "in")) {
2930 value *= PX_PER_IN;
2931 } else if (!strcmp(e, "%")) {
2932 /* Percentage */
2933 val->set = TRUE;
2934 val->inherit = FALSE;
2935 val->type = SP_FONT_SIZE_PERCENTAGE;
2936 val->value = SP_F8_16_FROM_FLOAT(value / 100.0);
2937 return;
2938 } else {
2939 /* Invalid */
2940 return;
2941 }
2942 /* Length */
2943 val->set = TRUE;
2944 val->inherit = FALSE;
2945 val->type = SP_FONT_SIZE_LENGTH;
2946 val->computed = value;
2947 return;
2948 }
2949 }
2950 }
2954 /**
2955 * Set SPIEnum object from repr attribute.
2956 */
2957 static void
2958 sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr,
2959 gchar const *key, SPStyleEnum const *dict,
2960 bool const can_explicitly_inherit)
2961 {
2962 gchar const *str = repr->attribute(key);
2963 if (str) {
2964 sp_style_read_ienum(val, str, dict, can_explicitly_inherit);
2965 }
2966 }
2970 /**
2971 * Set SPILength object from repr attribute.
2972 */
2973 static void
2974 sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key)
2975 {
2976 gchar const *str = repr->attribute(key);
2977 if (str) {
2978 sp_style_read_ilength(val, str);
2979 }
2980 }
2982 /**
2983 * Set SPIFontSize object from repr attribute.
2984 */
2985 static void
2986 sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key)
2987 {
2988 gchar const *str = repr->attribute(key);
2989 if (str) {
2990 sp_style_read_ifontsize(val, str);
2991 }
2992 }
2995 /**
2996 * Set SPIFloat object from repr attribute.
2997 */
2998 static void
2999 sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key)
3000 {
3001 gchar const *str = repr->attribute(key);
3002 if (str) {
3003 sp_style_read_ifloat(val, str);
3004 }
3005 }
3008 /**
3009 * Write SPIFloat object into string.
3010 */
3011 static gint
3012 sp_style_write_ifloat(gchar *p, gint const len, gchar const *const key,
3013 SPIFloat const *const val, SPIFloat const *const base, guint const flags)
3014 {
3015 Inkscape::CSSOStringStream os;
3017 if ((flags & SP_STYLE_FLAG_ALWAYS)
3018 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3019 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3020 && (!base->set || (val->value != base->value))))
3021 {
3022 if (val->inherit) {
3023 return g_snprintf(p, len, "%s:inherit;", key);
3024 } else {
3025 os << key << ":" << val->value << ";";
3026 return g_strlcpy(p, os.str().c_str(), len);
3027 }
3028 }
3029 return 0;
3030 }
3033 /**
3034 * Write SPIScale24 object into string.
3035 */
3036 static gint
3037 sp_style_write_iscale24(gchar *p, gint const len, gchar const *const key,
3038 SPIScale24 const *const val, SPIScale24 const *const base,
3039 guint const flags)
3040 {
3041 Inkscape::CSSOStringStream os;
3043 if ((flags & SP_STYLE_FLAG_ALWAYS)
3044 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3045 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3046 && (!base->set || (val->value != base->value))))
3047 {
3048 if (val->inherit) {
3049 return g_snprintf(p, len, "%s:inherit;", key);
3050 } else {
3051 os << key << ":" << SP_SCALE24_TO_FLOAT(val->value) << ";";
3052 return g_strlcpy(p, os.str().c_str(), len);
3053 }
3054 }
3055 return 0;
3056 }
3059 /**
3060 * Write SPIEnum object into string.
3061 */
3062 static gint
3063 sp_style_write_ienum(gchar *p, gint const len, gchar const *const key,
3064 SPStyleEnum const *const dict,
3065 SPIEnum const *const val, SPIEnum const *const base, guint const flags)
3066 {
3067 if ((flags & SP_STYLE_FLAG_ALWAYS)
3068 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3069 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3070 && (!base->set || (val->computed != base->computed))))
3071 {
3072 if (val->inherit) {
3073 return g_snprintf(p, len, "%s:inherit;", key);
3074 }
3075 for (unsigned i = 0; dict[i].key; i++) {
3076 if (dict[i].value == static_cast< gint > (val->value) ) {
3077 return g_snprintf(p, len, "%s:%s;", key, dict[i].key);
3078 }
3079 }
3080 }
3081 return 0;
3082 }
3086 /**
3087 * Write SPIString object into string.
3088 */
3089 static gint
3090 sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
3091 SPIString const *const val, SPIString const *const base, guint const flags)
3092 {
3093 if ((flags & SP_STYLE_FLAG_ALWAYS)
3094 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3095 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3096 && (!base->set || strcmp(val->value, base->value))))
3097 {
3098 if (val->inherit) {
3099 return g_snprintf(p, len, "%s:inherit;", key);
3100 } else {
3101 return g_snprintf(p, len, "%s:%s;", key, val->value);
3102 }
3103 }
3104 return 0;
3105 }
3108 /**
3109 *
3110 */
3111 static bool
3112 sp_length_differ(SPILength const *const a, SPILength const *const b)
3113 {
3114 if (a->unit != b->unit) {
3115 if (a->unit == SP_CSS_UNIT_EM) return true;
3116 if (a->unit == SP_CSS_UNIT_EX) return true;
3117 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3118 if (b->unit == SP_CSS_UNIT_EM) return true;
3119 if (b->unit == SP_CSS_UNIT_EX) return true;
3120 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3121 }
3123 return (a->computed != b->computed);
3124 }
3128 /**
3129 * Write SPILength object into string.
3130 */
3131 static gint
3132 sp_style_write_ilength(gchar *p, gint const len, gchar const *const key,
3133 SPILength const *const val, SPILength const *const base, guint const flags)
3134 {
3135 Inkscape::CSSOStringStream os;
3137 if ((flags & SP_STYLE_FLAG_ALWAYS)
3138 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3139 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3140 && (!base->set || sp_length_differ(val, base))))
3141 {
3142 if (val->inherit) {
3143 return g_snprintf(p, len, "%s:inherit;", key);
3144 } else {
3145 switch (val->unit) {
3146 case SP_CSS_UNIT_NONE:
3147 os << key << ":" << val->computed << ";";
3148 return g_strlcpy(p, os.str().c_str(), len);
3149 break;
3150 case SP_CSS_UNIT_PX:
3151 os << key << ":" << val->computed << "px;";
3152 return g_strlcpy(p, os.str().c_str(), len);
3153 break;
3154 case SP_CSS_UNIT_PT:
3155 os << key << ":" << val->computed * PT_PER_PX << "pt;";
3156 return g_strlcpy(p, os.str().c_str(), len);
3157 break;
3158 case SP_CSS_UNIT_PC:
3159 os << key << ":" << val->computed * PT_PER_PX / 12.0 << "pc;";
3160 return g_strlcpy(p, os.str().c_str(), len);
3161 break;
3162 case SP_CSS_UNIT_MM:
3163 os << key << ":" << val->computed * MM_PER_PX << "mm;";
3164 return g_strlcpy(p, os.str().c_str(), len);
3165 break;
3166 case SP_CSS_UNIT_CM:
3167 os << key << ":" << val->computed * CM_PER_PX << "cm;";
3168 return g_strlcpy(p, os.str().c_str(), len);
3169 break;
3170 case SP_CSS_UNIT_IN:
3171 os << key << ":" << val->computed * IN_PER_PX << "in;";
3172 return g_strlcpy(p, os.str().c_str(), len);
3173 break;
3174 case SP_CSS_UNIT_EM:
3175 os << key << ":" << val->value << "em;";
3176 return g_strlcpy(p, os.str().c_str(), len);
3177 break;
3178 case SP_CSS_UNIT_EX:
3179 os << key << ":" << val->value << "ex;";
3180 return g_strlcpy(p, os.str().c_str(), len);
3181 break;
3182 case SP_CSS_UNIT_PERCENT:
3183 os << key << ":" << (val->value * 100.0) << "%;";
3184 return g_strlcpy(p, os.str().c_str(), len);
3185 break;
3186 default:
3187 /* Invalid */
3188 break;
3189 }
3190 }
3191 }
3192 return 0;
3193 }
3196 /**
3197 *
3198 */
3199 static bool
3200 sp_lengthornormal_differ(SPILengthOrNormal const *const a, SPILengthOrNormal const *const b)
3201 {
3202 if (a->normal != b->normal) return true;
3203 if (a->normal) return false;
3205 if (a->unit != b->unit) {
3206 if (a->unit == SP_CSS_UNIT_EM) return true;
3207 if (a->unit == SP_CSS_UNIT_EX) return true;
3208 if (a->unit == SP_CSS_UNIT_PERCENT) return true;
3209 if (b->unit == SP_CSS_UNIT_EM) return true;
3210 if (b->unit == SP_CSS_UNIT_EX) return true;
3211 if (b->unit == SP_CSS_UNIT_PERCENT) return true;
3212 }
3214 return (a->computed != b->computed);
3215 }
3217 /**
3218 * Write SPILengthOrNormal object into string.
3219 */
3220 static gint
3221 sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key,
3222 SPILengthOrNormal const *const val,
3223 SPILengthOrNormal const *const base,
3224 guint const flags)
3225 {
3226 if ((flags & SP_STYLE_FLAG_ALWAYS)
3227 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3228 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3229 && (!base->set || sp_lengthornormal_differ(val, base))))
3230 {
3231 if (val->normal) {
3232 return g_snprintf(p, len, "%s:normal;", key);
3233 } else {
3234 SPILength length;
3235 length.set = val->set;
3236 length.inherit = val->inherit;
3237 length.unit = val->unit;
3238 length.value = val->value;
3239 length.computed = val->computed;
3240 return sp_style_write_ilength(p, len, key, &length, NULL, SP_STYLE_FLAG_ALWAYS);
3241 }
3242 }
3243 return 0;
3244 }
3246 /**
3247 *
3248 */
3249 static bool
3250 sp_textdecoration_differ(SPITextDecoration const *const a, SPITextDecoration const *const b)
3251 {
3252 return a->underline != b->underline
3253 || a->overline != b->overline
3254 || a->line_through != b->line_through
3255 || a->blink != b->blink;
3256 }
3258 /**
3259 * Write SPITextDecoration object into string.
3260 */
3261 static gint
3262 sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
3263 SPITextDecoration const *const val,
3264 SPITextDecoration const *const base,
3265 guint const flags)
3266 {
3267 Inkscape::CSSOStringStream os;
3269 if ((flags & SP_STYLE_FLAG_ALWAYS)
3270 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3271 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3272 && (!base->set || sp_textdecoration_differ(val, base))))
3273 {
3274 if (val->inherit) {
3275 return g_snprintf(p, len, "%s:inherit;", key);
3276 } else {
3277 os << key << ":";
3278 if (val->underline || val->overline || val->line_through || val->blink) {
3279 if (val->underline) os << " underline";
3280 if (val->overline) os << " overline";
3281 if (val->line_through) os << " line-through";
3282 if (val->blink) os << " blink";
3283 } else
3284 os << "none";
3285 os << ";";
3286 return g_strlcpy(p, os.str().c_str(), len);
3287 }
3288 }
3289 return 0;
3290 }
3292 /**
3293 *
3294 */
3295 static bool
3296 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
3297 {
3298 if (a->type != b->type)
3299 return true;
3300 if (a->type == SP_PAINT_TYPE_COLOR)
3301 return !sp_color_is_equal(&a->value.color, &b->value.color);
3302 if (a->type == SP_PAINT_TYPE_PAINTSERVER)
3303 return (a->value.paint.server != b->value.paint.server);
3304 return false;
3305 }
3309 /**
3310 * Write SPIPaint object into string.
3311 */
3312 static gint
3313 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
3314 SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
3315 {
3316 if ((flags & SP_STYLE_FLAG_ALWAYS)
3317 || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
3318 || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
3319 && (!base->set || sp_paint_differ(paint, base))))
3320 {
3321 if (paint->inherit) {
3322 return g_snprintf(b, len, "%s:inherit;", key);
3323 } else if (paint->currentcolor) {
3324 return g_snprintf(b, len, "%s:currentColor;", key);
3325 } else {
3326 switch (paint->type) {
3327 case SP_PAINT_TYPE_COLOR:
3328 return g_snprintf(b, len, "%s:#%06x;", key, sp_color_get_rgba32_falpha(&paint->value.color, 0.0) >> 8);
3329 break;
3330 case SP_PAINT_TYPE_PAINTSERVER:
3331 return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
3332 break;
3333 default:
3334 break;
3335 }
3336 return g_snprintf(b, len, "%s:none;", key);
3337 }
3338 }
3339 return 0;
3340 }
3343 /**
3344 *
3345 */
3346 static bool
3347 sp_fontsize_differ(SPIFontSize const *const a, SPIFontSize const *const b)
3348 {
3349 if (a->type != b->type)
3350 return true;
3351 if (a->type == SP_FONT_SIZE_LENGTH) {
3352 if (a->computed != b->computed)
3353 return true;
3354 } else {
3355 if (a->value != b->value)
3356 return true;
3357 }
3358 return false;
3359 }
3362 /**
3363 * Write SPIFontSize object into string.
3364 */
3365 static gint
3366 sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
3367 SPIFontSize const *const val, SPIFontSize const *const base,
3368 guint const flags)
3369 {
3370 if ((flags & SP_STYLE_FLAG_ALWAYS)
3371 || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
3372 || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set
3373 && (!base->set || sp_fontsize_differ(val, base))))
3374 {
3375 if (val->inherit) {
3376 return g_snprintf(p, len, "%s:inherit;", key);
3377 } else if (val->type == SP_FONT_SIZE_LITERAL) {
3378 for (unsigned i = 0; enum_font_size[i].key; i++) {
3379 if (enum_font_size[i].value == static_cast< gint > (val->value) ) {
3380 return g_snprintf(p, len, "%s:%s;", key, enum_font_size[i].key);
3381 }
3382 }
3383 } else if (val->type == SP_FONT_SIZE_LENGTH) {
3384 Inkscape::CSSOStringStream os;
3385 os << key << ":" << val->computed << "px;"; // must specify px, see inkscape bug 1221626, mozilla bug 234789
3386 return g_strlcpy(p, os.str().c_str(), len);
3387 } else if (val->type == SP_FONT_SIZE_PERCENTAGE) {
3388 Inkscape::CSSOStringStream os;
3389 os << key << ":" << (SP_F8_16_TO_FLOAT(val->value) * 100.0) << "%;";
3390 return g_strlcpy(p, os.str().c_str(), len);
3391 }
3392 }
3393 return 0;
3394 }
3397 /**
3398 * Clear paint object; conditionally disconnect style from paintserver.
3399 */
3400 static void
3401 sp_style_paint_clear(SPStyle *style, SPIPaint *paint,
3402 unsigned hunref, unsigned unset)
3403 {
3404 if (hunref && (paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
3405 if (style->hreffed) {
3406 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
3407 style->hreffed = false;
3408 }
3409 if (style->listening) {
3410 g_signal_handlers_disconnect_matched(G_OBJECT(paint->value.paint.server),
3411 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
3412 style->listening = false;
3413 }
3414 paint->value.paint.server = NULL;
3415 paint->value.paint.uri = NULL;
3416 paint->type = SP_PAINT_TYPE_NONE;
3417 }
3419 if (unset) {
3420 paint->set = FALSE;
3421 paint->inherit = FALSE;
3422 }
3423 }
3425 /**
3426 * Clear all style property attributes in object.
3427 */
3428 void
3429 sp_style_unset_property_attrs(SPObject *o)
3430 {
3431 if (!o) return;
3433 SPStyle *style = SP_OBJECT_STYLE(o);
3434 if (!style) return;
3436 Inkscape::XML::Node *repr = SP_OBJECT_REPR(o);
3437 if (!repr) return;
3439 if (style->opacity.set) {
3440 repr->setAttribute("opacity", NULL);
3441 }
3442 if (style->color.set) {
3443 repr->setAttribute("color", NULL);
3444 }
3445 if (style->fill.set) {
3446 repr->setAttribute("fill", NULL);
3447 }
3448 if (style->fill_opacity.set) {
3449 repr->setAttribute("fill-opacity", NULL);
3450 }
3451 if (style->fill_rule.set) {
3452 repr->setAttribute("fill-rule", NULL);
3453 }
3454 if (style->stroke.set) {
3455 repr->setAttribute("stroke", NULL);
3456 }
3457 if (style->stroke_width.set) {
3458 repr->setAttribute("stroke-width", NULL);
3459 }
3460 if (style->stroke_linecap.set) {
3461 repr->setAttribute("stroke-linecap", NULL);
3462 }
3463 if (style->stroke_linejoin.set) {
3464 repr->setAttribute("stroke-linejoin", NULL);
3465 }
3466 if (style->marker[SP_MARKER_LOC].set) {
3467 repr->setAttribute("marker", NULL);
3468 }
3469 if (style->marker[SP_MARKER_LOC_START].set) {
3470 repr->setAttribute("marker-start", NULL);
3471 }
3472 if (style->marker[SP_MARKER_LOC_MID].set) {
3473 repr->setAttribute("marker-mid", NULL);
3474 }
3475 if (style->marker[SP_MARKER_LOC_END].set) {
3476 repr->setAttribute("marker-end", NULL);
3477 }
3478 if (style->stroke_opacity.set) {
3479 repr->setAttribute("stroke-opacity", NULL);
3480 }
3481 if (style->stroke_dasharray_set) {
3482 repr->setAttribute("stroke-dasharray", NULL);
3483 }
3484 if (style->stroke_dashoffset_set) {
3485 repr->setAttribute("stroke-dashoffset", NULL);
3486 }
3487 if (style->text_private && style->text->font_family.set) {
3488 repr->setAttribute("font-family", NULL);
3489 }
3490 if (style->text_anchor.set) {
3491 repr->setAttribute("text-anchor", NULL);
3492 }
3493 if (style->writing_mode.set) {
3494 repr->setAttribute("writing_mode", NULL);
3495 }
3496 }
3498 /**
3499 * \pre style != NULL.
3500 * \pre flags in {IFSET, ALWAYS}.
3501 */
3502 SPCSSAttr *
3503 sp_css_attr_from_style(SPStyle const *const style, guint const flags)
3504 {
3505 g_return_val_if_fail(style != NULL, NULL);
3506 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3507 (flags == SP_STYLE_FLAG_ALWAYS) ),
3508 NULL);
3509 gchar *style_str = sp_style_write_string(style, flags);
3510 SPCSSAttr *css = sp_repr_css_attr_new();
3511 sp_repr_css_attr_add_from_string(css, style_str);
3512 g_free(style_str);
3513 return css;
3514 }
3517 /**
3518 * \pre object != NULL
3519 * \pre flags in {IFSET, ALWAYS}.
3520 */
3521 SPCSSAttr *
3522 sp_css_attr_from_object(SPObject *object, guint const flags)
3523 {
3524 g_return_val_if_fail(((flags == SP_STYLE_FLAG_IFSET) ||
3525 (flags == SP_STYLE_FLAG_ALWAYS) ),
3526 NULL);
3527 SPStyle const *const style = SP_OBJECT_STYLE(object);
3528 if (style == NULL)
3529 return NULL;
3530 return sp_css_attr_from_style (style, flags);
3531 }
3533 /**
3534 * Unset any text-related properties
3535 */
3536 SPCSSAttr *
3537 sp_css_attr_unset_text(SPCSSAttr *css)
3538 {
3539 sp_repr_css_set_property(css, "font", NULL); // not implemented yet
3540 sp_repr_css_set_property(css, "font-size", NULL);
3541 sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
3542 sp_repr_css_set_property(css, "font-style", NULL);
3543 sp_repr_css_set_property(css, "font-variant", NULL);
3544 sp_repr_css_set_property(css, "font-weight", NULL);
3545 sp_repr_css_set_property(css, "font-stretch", NULL);
3546 sp_repr_css_set_property(css, "font-family", NULL);
3547 sp_repr_css_set_property(css, "text-indent", NULL);
3548 sp_repr_css_set_property(css, "text-align", NULL);
3549 sp_repr_css_set_property(css, "text-decoration", NULL);
3550 sp_repr_css_set_property(css, "line-height", NULL);
3551 sp_repr_css_set_property(css, "letter-spacing", NULL);
3552 sp_repr_css_set_property(css, "word-spacing", NULL);
3553 sp_repr_css_set_property(css, "text-transform", NULL);
3554 sp_repr_css_set_property(css, "direction", NULL);
3555 sp_repr_css_set_property(css, "block-progression", NULL);
3556 sp_repr_css_set_property(css, "writing-mode", NULL);
3557 sp_repr_css_set_property(css, "text-anchor", NULL);
3558 sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
3559 sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
3560 sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
3561 sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
3563 return css;
3564 }
3566 bool
3567 is_url(char const *p)
3568 {
3569 if (p == NULL)
3570 return false;
3571 /** \todo
3572 * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
3573 * in property values must start with 'url('.
3574 */
3575 return (g_ascii_strncasecmp(p, "url(", 4) == 0);
3576 }
3578 /**
3579 * Unset any properties that contain URI values.
3580 *
3581 * Used for storing style that will be reused across documents when carrying
3582 * the referenced defs is impractical.
3583 */
3584 SPCSSAttr *
3585 sp_css_attr_unset_uris(SPCSSAttr *css)
3586 {
3587 // All properties that may hold <uri> or <paint> according to SVG 1.1
3588 if (is_url(sp_repr_css_property(css, "clip-path", NULL))) sp_repr_css_set_property(css, "clip-path", NULL);
3589 if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
3590 if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
3591 if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
3592 if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
3593 if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
3594 if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
3595 if (is_url(sp_repr_css_property(css, "mask", NULL))) sp_repr_css_set_property(css, "mask", NULL);
3596 if (is_url(sp_repr_css_property(css, "fill", NULL))) sp_repr_css_set_property(css, "fill", NULL);
3597 if (is_url(sp_repr_css_property(css, "stroke", NULL))) sp_repr_css_set_property(css, "stroke", NULL);
3599 return css;
3600 }
3602 /**
3603 * Scale a single-value property.
3604 */
3605 void
3606 sp_css_attr_scale_property_single(SPCSSAttr *css, gchar const *property,
3607 double ex, bool only_with_units = false)
3608 {
3609 gchar const *w = sp_repr_css_property(css, property, NULL);
3610 if (w) {
3611 gchar *units = NULL;
3612 double wd = g_ascii_strtod(w, &units) * ex;
3613 if (w == units) {// nothing converted, non-numeric value
3614 return;
3615 }
3616 if (only_with_units && (units == NULL || *units == '\0' || *units == '%')) {
3617 // only_with_units, but no units found, so do nothing.
3618 return;
3619 }
3620 Inkscape::CSSOStringStream os;
3621 os << wd << units; // reattach units
3622 sp_repr_css_set_property(css, property, os.str().c_str());
3623 }
3624 }
3626 /**
3627 * Scale a list-of-values property.
3628 */
3629 void
3630 sp_css_attr_scale_property_list(SPCSSAttr *css, gchar const *property, double ex)
3631 {
3632 gchar const *string = sp_repr_css_property(css, property, NULL);
3633 if (string) {
3634 Inkscape::CSSOStringStream os;
3635 gchar **a = g_strsplit(string, ",", 10000);
3636 bool first = true;
3637 for (gchar **i = a; i != NULL; i++) {
3638 gchar *w = *i;
3639 if (w == NULL)
3640 break;
3641 gchar *units = NULL;
3642 double wd = g_ascii_strtod(w, &units) * ex;
3643 if (w == units) {// nothing converted, non-numeric value ("none" or "inherit"); do nothing
3644 g_strfreev(a);
3645 return;
3646 }
3647 if (!first) {
3648 os << ",";
3649 }
3650 os << wd << units; // reattach units
3651 first = false;
3652 }
3653 sp_repr_css_set_property(css, property, os.str().c_str());
3654 g_strfreev(a);
3655 }
3656 }
3658 /**
3659 * Scale any properties that may hold <length> by ex.
3660 */
3661 SPCSSAttr *
3662 sp_css_attr_scale(SPCSSAttr *css, double ex)
3663 {
3664 sp_css_attr_scale_property_single(css, "baseline-shift", ex);
3665 sp_css_attr_scale_property_single(css, "stroke-width", ex);
3666 sp_css_attr_scale_property_list (css, "stroke-dasharray", ex);
3667 sp_css_attr_scale_property_single(css, "stroke-dashoffset", ex);
3668 sp_css_attr_scale_property_single(css, "font-size", ex);
3669 sp_css_attr_scale_property_single(css, "kerning", ex);
3670 sp_css_attr_scale_property_single(css, "letter-spacing", ex);
3671 sp_css_attr_scale_property_single(css, "word-spacing", ex);
3672 sp_css_attr_scale_property_single(css, "line-height", ex, true);
3674 return css;
3675 }
3678 /**
3679 * Remove quotes from SPIString object value.
3680 *
3681 * \todo FIXME: now used for font family, but perhaps this should apply to
3682 * ALL strings (check CSS spec), in which case this should be part of
3683 * read_istring.
3684 */
3685 void
3686 css2_unescape_unquote (SPIString *val)
3687 {
3688 if (val->set && val->value && strlen(val->value) >= 2) {
3690 /// \todo unescape all \-escaped chars
3692 int l = strlen(val->value);
3693 if ((val->value[0] == '"' && val->value[l - 1] == '"') ||
3694 (val->value[0] == '\'' && val->value[l - 1] == '\'')) {
3695 memcpy (val->value, val->value+1, l - 2);
3696 val->value[l - 2] = '\0';
3697 }
3698 }
3699 }
3702 /*
3703 Local Variables:
3704 mode:c++
3705 c-file-style:"stroustrup"
3706 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
3707 indent-tabs-mode:nil
3708 fill-column:99
3709 End:
3710 */
3711 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :