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