1 #ifndef SEEN_SP_STYLE_H
2 #define SEEN_SP_STYLE_H
4 /** \file
5 * SPStyle - a style object for SPItem objects
6 */
7 /* Authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Jon A. Cruz <jon@joncruz.org>
10 *
11 * Copyright (C) 2010 Jon A. Cruz
12 * Copyright (C) 2001-2002 Lauris Kaplinski
13 * Copyright (C) 2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #include "color.h"
19 #include "forward.h"
20 #include "sp-marker-loc.h"
21 #include "sp-filter.h"
22 #include "sp-filter-reference.h"
23 #include "uri-references.h"
24 #include "uri.h"
25 #include "sp-paint-server.h"
27 #include <sigc++/connection.h>
29 namespace Inkscape {
30 namespace XML {
31 class Node;
32 }
33 }
35 class SPCSSAttr;
37 class SPIFloat;
38 class SPIScale24;
39 class SPIInt;
40 class SPIShort;
41 class SPIEnum;
42 class SPIString;
43 class SPILength;
44 class SPIPaint;
45 class SPIFontSize;
46 class SPIBaselineShift;
48 /// Float type internal to SPStyle.
49 struct SPIFloat {
50 unsigned set : 1;
51 unsigned inherit : 1;
52 unsigned data : 30;
53 float value;
54 };
56 /*
57 * One might think that the best value for SP_SCALE24_MAX would be ((1<<24)-1), which allows the
58 * greatest possible precision for fitting [0, 1] fractions into 24 bits.
59 *
60 * However, in practice, that gives a problem with 0.5, which falls half way between two fractions
61 * of ((1<<24)-1). What's worse is that casting double(1<<23) / ((1<<24)-1) to float on x86
62 * produces wrong rounding behaviour, resulting in a fraction of ((1<<23)+2.0f) / (1<<24) rather
63 * than ((1<<23)+1.0f) / (1<<24) as one would expect, let alone ((1<<23)+0.0f) / (1<<24) as one
64 * would ideally like for this example.
65 *
66 * The value (1<<23) is thus best if one considers float conversions alone.
67 *
68 * The value 0xff0000 can exactly represent all 8-bit alpha channel values,
69 * and can exactly represent all multiples of 0.1. I haven't yet tested whether
70 * rounding bugs still get in the way of conversions to & from float, but my instinct is that
71 * it's fairly safe because 0xff fits three times inside float's significand.
72 *
73 * We should probably use the value 0xffff00 once we support 16 bits per channel and/or LittleCMS,
74 * though that might need to be accompanied by greater use of double instead of float for
75 * colours and opacities, to be safe from rounding bugs.
76 */
77 #define SP_SCALE24_MAX (0xff0000)
78 #define SP_SCALE24_TO_FLOAT(v) ((double) (v) / SP_SCALE24_MAX)
79 #define SP_SCALE24_FROM_FLOAT(v) unsigned(((v) * SP_SCALE24_MAX) + .5)
81 /** Returns a scale24 for the product of two scale24 values. */
82 #define SP_SCALE24_MUL(_v1, _v2) unsigned((double)(_v1) * (_v2) / SP_SCALE24_MAX + .5)
84 /// 24 bit data type internal to SPStyle.
85 struct SPIScale24 {
86 unsigned set : 1;
87 unsigned inherit : 1;
88 unsigned value : 24;
89 };
91 /// Int type internal to SPStyle.
92 struct SPIInt {
93 unsigned set : 1;
94 unsigned inherit : 1;
95 unsigned data : 30;
96 int value;
97 };
99 /// Short type internal to SPStyle.
100 struct SPIShort {
101 unsigned set : 1;
102 unsigned inherit : 1;
103 unsigned data : 14;
104 int value : 16;
105 };
107 /// Enum type internal to SPStyle.
108 struct SPIEnum {
109 unsigned set : 1;
110 unsigned inherit : 1;
111 unsigned value : 8;
112 unsigned computed : 8;
113 };
115 /// String type internal to SPStyle.
116 struct SPIString {
117 unsigned set : 1;
118 unsigned inherit : 1;
119 unsigned data : 30;
120 gchar *value;
121 };
123 enum {
124 SP_CSS_UNIT_NONE,
125 SP_CSS_UNIT_PX,
126 SP_CSS_UNIT_PT,
127 SP_CSS_UNIT_PC,
128 SP_CSS_UNIT_MM,
129 SP_CSS_UNIT_CM,
130 SP_CSS_UNIT_IN,
131 SP_CSS_UNIT_EM,
132 SP_CSS_UNIT_EX,
133 SP_CSS_UNIT_PERCENT
134 };
136 /// Length type internal to SPStyle.
137 struct SPILength {
138 unsigned set : 1;
139 unsigned inherit : 1;
140 unsigned unit : 4;
141 float value;
142 float computed;
143 };
145 #define SP_STYLE_FILL_SERVER(s) (((SPStyle *) (s))->getFillPaintServer())
146 #define SP_STYLE_STROKE_SERVER(s) (((SPStyle *) (s))->getStrokePaintServer())
148 class SVGICCColor;
150 /// Paint type internal to SPStyle.
151 struct SPIPaint {
152 unsigned set : 1;
153 unsigned inherit : 1;
154 unsigned currentcolor : 1;
155 unsigned int colorSet : 1;
156 unsigned int noneSet : 1;
157 struct {
158 SPPaintServerReference *href;
159 SPColor color;
160 } value;
162 SPIPaint();
164 bool isSet() const { return true; /* set || colorSet*/}
165 bool isSameType( SPIPaint const & other ) const {return (isPaintserver() == other.isPaintserver()) && (colorSet == other.colorSet) && (currentcolor == other.currentcolor);}
167 bool isNoneSet() const {return noneSet;}
169 bool isNone() const {return !currentcolor && !colorSet && !isPaintserver();} // TODO refine
170 bool isColor() const {return colorSet && !isPaintserver();}
171 bool isPaintserver() const {return value.href && value.href->getObject();}
173 void clear();
175 void setColor( float r, float g, float b ) {value.color.set( r, g, b ); colorSet = true;}
176 void setColor( guint32 val ) {value.color.set( val ); colorSet = true;}
177 void setColor( SPColor const& color ) {value.color = color; colorSet = true;}
179 void read( gchar const *str, SPStyle &tyle, SPDocument *document = 0);
180 };
182 /// Filter type internal to SPStyle
183 struct SPIFilter {
184 unsigned set : 1;
185 unsigned inherit : 1;
186 SPFilterReference *href;
187 };
189 enum {
190 SP_FONT_SIZE_LITERAL,
191 SP_FONT_SIZE_LENGTH,
192 SP_FONT_SIZE_PERCENTAGE
193 };
195 enum {
196 SP_BASELINE_SHIFT_LITERAL,
197 SP_BASELINE_SHIFT_LENGTH,
198 SP_BASELINE_SHIFT_PERCENTAGE
199 };
201 #define SP_FONT_SIZE ((1 << 24) - 1)
203 #define SP_F8_16_TO_FLOAT(v) ((gdouble) (v) / (1 << 16))
204 #define SP_F8_16_FROM_FLOAT(v) ((int) ((v) * ((1 << 16) + 0.9999)))
206 #define SP_STYLE_FLAG_IFSET (1 << 0)
207 #define SP_STYLE_FLAG_IFDIFF (1 << 1)
208 #define SP_STYLE_FLAG_ALWAYS (1 << 2)
210 /// Fontsize type internal to SPStyle.
211 struct SPIFontSize {
212 unsigned set : 1;
213 unsigned inherit : 1;
214 unsigned type : 2;
215 unsigned value : 24;
216 float computed;
217 };
219 /// Baseline shift type internal to SPStyle.
220 struct SPIBaselineShift {
221 unsigned set : 1;
222 unsigned inherit : 1;
223 unsigned type : 2;
224 unsigned unit : 4;
225 unsigned literal: 2;
226 float value; // Can be negative
227 float computed;
228 };
230 /// Text decoration type internal to SPStyle.
231 struct SPITextDecoration {
232 unsigned set : 1;
233 unsigned inherit : 1;
234 unsigned underline : 1;
235 unsigned overline : 1;
236 unsigned line_through : 1;
237 unsigned blink : 1; // "Conforming user agents are not required to support this value." yay!
238 };
240 /// Extended length type internal to SPStyle.
241 struct SPILengthOrNormal {
242 unsigned set : 1;
243 unsigned inherit : 1;
244 unsigned normal : 1;
245 unsigned unit : 4;
246 float value;
247 float computed;
248 };
250 class SPTextStyle;
252 /// Stroke dash details.
253 class NRVpathDash {
254 public:
255 double offset;
256 int n_dash;
257 double *dash;
258 };
260 /// An SVG style object.
261 struct SPStyle {
262 int refcount;
264 /** Object we are attached to */
265 SPObject *object;
266 /** Document we are associated with */
267 SPDocument *document;
269 /** Our text style component */
270 SPTextStyle *text;
271 unsigned text_private : 1;
273 /* CSS2 */
274 /* Font */
275 /** Size of the font */
276 SPIFontSize font_size;
277 /** Style of the font */
278 SPIEnum font_style;
279 /** Which substyle of the font */
280 SPIEnum font_variant;
281 /** Weight of the font */
282 SPIEnum font_weight;
283 /** Stretch of the font */
284 SPIEnum font_stretch;
286 /** First line indent of paragraphs (css2 16.1) */
287 SPILength text_indent;
288 /** text alignment (css2 16.2) (not to be confused with text-anchor) */
289 SPIEnum text_align;
290 /** text decoration (css2 16.3.1) */
291 SPITextDecoration text_decoration;
292 // 16.3.2 is text-shadow. That's complicated.
293 /** Line spacing (css2 10.8.1) */
294 SPILengthOrNormal line_height;
295 /** letter spacing (css2 16.4) */
296 SPILengthOrNormal letter_spacing;
297 /** word spacing (also css2 16.4) */
298 SPILengthOrNormal word_spacing;
299 /** capitalization (css2 16.5) */
300 SPIEnum text_transform;
302 /* CSS3 Text */
303 /** text direction (css3 text 3.2) */
304 SPIEnum direction;
305 /** block progression (css3 text 3.2) */
306 SPIEnum block_progression;
307 /** Writing mode (css3 text 3.2 and svg1.1 10.7.2) */
308 SPIEnum writing_mode;
309 /** Baseline shift (svg1.1 10.9.2) */
310 SPIBaselineShift baseline_shift;
312 /* SVG */
313 /** Anchor of the text (svg1.1 10.9.1) */
314 SPIEnum text_anchor;
316 /* Misc attributes */
317 unsigned clip_set : 1;
318 unsigned color_set : 1;
319 unsigned cursor_set : 1;
320 unsigned overflow_set : 1;
321 unsigned clip_path_set : 1;
322 unsigned clip_rule_set : 1;
323 unsigned mask_set : 1;
325 /** display */
326 SPIEnum display;
328 /** overflow */
329 SPIEnum overflow;
331 /** visibility */
332 SPIEnum visibility;
334 /** opacity */
335 SPIScale24 opacity;
337 /** color */
338 SPIPaint color;
340 /** fill */
341 SPIPaint fill;
342 /** fill-opacity */
343 SPIScale24 fill_opacity;
344 /** fill-rule: 0 nonzero, 1 evenodd */
345 SPIEnum fill_rule;
347 /** stroke */
348 SPIPaint stroke;
349 /** stroke-width */
350 SPILength stroke_width;
351 /** stroke-linecap */
352 SPIEnum stroke_linecap;
353 /** stroke-linejoin */
354 SPIEnum stroke_linejoin;
355 /** stroke-miterlimit */
356 SPIFloat stroke_miterlimit;
357 /** stroke-dash* */
358 NRVpathDash stroke_dash;
359 unsigned stroke_dasharray_set : 1;
360 unsigned stroke_dasharray_inherit : 1;
361 unsigned stroke_dashoffset_set : 1;
362 unsigned stroke_dashoffset_inherit : 1;
363 /** stroke-opacity */
364 SPIScale24 stroke_opacity;
366 /** Marker list */
367 SPIString marker[SP_MARKER_LOC_QTY];
369 /** Filter effect */
370 SPIFilter filter;
372 SPIEnum filter_blend_mode;
374 /** normally not used, but duplicates the Gaussian blur deviation (if any) from the attached
375 filter when the style is used for querying */
376 SPILength filter_gaussianBlur_deviation;
378 /** enable-background, used for defining where filter effects get
379 * their background image */
380 SPIEnum enable_background;
382 /// style belongs to a cloned object
383 bool cloned;
385 sigc::connection release_connection;
387 sigc::connection filter_modified_connection;
388 sigc::connection fill_ps_modified_connection;
389 sigc::connection stroke_ps_modified_connection;
391 SPObject *getFilter() { return (filter.href) ? filter.href->getObject() : 0; }
392 SPObject const *getFilter() const { return (filter.href) ? filter.href->getObject() : 0; }
393 gchar const *getFilterURI() const { return (filter.href) ? filter.href->getURI()->toString() : 0; }
395 SPPaintServer *getFillPaintServer() { return (fill.value.href) ? fill.value.href->getObject() : 0; }
396 SPPaintServer const *getFillPaintServer() const { return (fill.value.href) ? fill.value.href->getObject() : 0; }
397 gchar const *getFillURI() const { return (fill.value.href) ? fill.value.href->getURI()->toString() : 0; }
399 SPPaintServer *getStrokePaintServer() { return (stroke.value.href) ? stroke.value.href->getObject() : 0; }
400 SPPaintServer const *getStrokePaintServer() const { return (stroke.value.href) ? stroke.value.href->getObject() : 0; }
401 gchar const *getStrokeURI() const { return (stroke.value.href) ? stroke.value.href->getURI()->toString() : 0; }
402 };
404 SPStyle *sp_style_new(SPDocument *document);
406 SPStyle *sp_style_new_from_object(SPObject *object);
408 SPStyle *sp_style_ref(SPStyle *style);
410 SPStyle *sp_style_unref(SPStyle *style);
412 void sp_style_read_from_object(SPStyle *style, SPObject *object);
414 void sp_style_read_from_prefs(SPStyle *style, Glib::ustring const &path);
416 void sp_style_merge_from_style_string(SPStyle *style, gchar const *p);
418 void sp_style_merge_from_parent(SPStyle *style, SPStyle const *parent);
420 void sp_style_merge_from_dying_parent(SPStyle *style, SPStyle const *parent);
422 gchar *sp_style_write_string(SPStyle const *style, guint flags = SP_STYLE_FLAG_IFSET);
424 gchar *sp_style_write_difference(SPStyle const *from, SPStyle const *to);
426 void sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri);
428 /* SPTextStyle */
430 enum SPCSSFontSize {
431 SP_CSS_FONT_SIZE_XX_SMALL,
432 SP_CSS_FONT_SIZE_X_SMALL,
433 SP_CSS_FONT_SIZE_SMALL,
434 SP_CSS_FONT_SIZE_MEDIUM,
435 SP_CSS_FONT_SIZE_LARGE,
436 SP_CSS_FONT_SIZE_X_LARGE,
437 SP_CSS_FONT_SIZE_XX_LARGE,
438 SP_CSS_FONT_SIZE_SMALLER,
439 SP_CSS_FONT_SIZE_LARGER
440 };
442 enum SPCSSFontStyle {
443 SP_CSS_FONT_STYLE_NORMAL,
444 SP_CSS_FONT_STYLE_ITALIC,
445 SP_CSS_FONT_STYLE_OBLIQUE
446 };
448 enum SPCSSFontVariant {
449 SP_CSS_FONT_VARIANT_NORMAL,
450 SP_CSS_FONT_VARIANT_SMALL_CAPS
451 };
453 enum SPCSSFontWeight {
454 SP_CSS_FONT_WEIGHT_100,
455 SP_CSS_FONT_WEIGHT_200,
456 SP_CSS_FONT_WEIGHT_300,
457 SP_CSS_FONT_WEIGHT_400,
458 SP_CSS_FONT_WEIGHT_500,
459 SP_CSS_FONT_WEIGHT_600,
460 SP_CSS_FONT_WEIGHT_700,
461 SP_CSS_FONT_WEIGHT_800,
462 SP_CSS_FONT_WEIGHT_900,
463 SP_CSS_FONT_WEIGHT_NORMAL,
464 SP_CSS_FONT_WEIGHT_BOLD,
465 SP_CSS_FONT_WEIGHT_LIGHTER,
466 SP_CSS_FONT_WEIGHT_BOLDER
467 };
469 enum SPCSSFontStretch {
470 SP_CSS_FONT_STRETCH_ULTRA_CONDENSED,
471 SP_CSS_FONT_STRETCH_EXTRA_CONDENSED,
472 SP_CSS_FONT_STRETCH_CONDENSED,
473 SP_CSS_FONT_STRETCH_SEMI_CONDENSED,
474 SP_CSS_FONT_STRETCH_NORMAL,
475 SP_CSS_FONT_STRETCH_SEMI_EXPANDED,
476 SP_CSS_FONT_STRETCH_EXPANDED,
477 SP_CSS_FONT_STRETCH_EXTRA_EXPANDED,
478 SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
479 SP_CSS_FONT_STRETCH_NARROWER,
480 SP_CSS_FONT_STRETCH_WIDER
481 };
483 enum SPCSSTextAlign {
484 SP_CSS_TEXT_ALIGN_START,
485 SP_CSS_TEXT_ALIGN_END,
486 SP_CSS_TEXT_ALIGN_LEFT,
487 SP_CSS_TEXT_ALIGN_RIGHT,
488 SP_CSS_TEXT_ALIGN_CENTER,
489 SP_CSS_TEXT_ALIGN_JUSTIFY
490 // also <string> is allowed, but only within table calls
491 };
493 enum SPCSSTextTransform {
494 SP_CSS_TEXT_TRANSFORM_CAPITALIZE,
495 SP_CSS_TEXT_TRANSFORM_UPPERCASE,
496 SP_CSS_TEXT_TRANSFORM_LOWERCASE,
497 SP_CSS_TEXT_TRANSFORM_NONE
498 };
500 enum SPCSSDirection {
501 SP_CSS_DIRECTION_LTR,
502 SP_CSS_DIRECTION_RTL
503 };
505 enum SPCSSBlockProgression {
506 SP_CSS_BLOCK_PROGRESSION_TB,
507 SP_CSS_BLOCK_PROGRESSION_RL,
508 SP_CSS_BLOCK_PROGRESSION_LR
509 };
511 enum SPCSSWritingMode {
512 SP_CSS_WRITING_MODE_LR_TB,
513 SP_CSS_WRITING_MODE_RL_TB,
514 SP_CSS_WRITING_MODE_TB_RL,
515 SP_CSS_WRITING_MODE_TB_LR
516 };
518 enum SPTextAnchor {
519 SP_CSS_TEXT_ANCHOR_START,
520 SP_CSS_TEXT_ANCHOR_MIDDLE,
521 SP_CSS_TEXT_ANCHOR_END
522 };
524 enum SPCSSBaselineShift {
525 SP_CSS_BASELINE_SHIFT_BASELINE,
526 SP_CSS_BASELINE_SHIFT_SUB,
527 SP_CSS_BASELINE_SHIFT_SUPER
528 };
530 enum SPVisibility {
531 SP_CSS_VISIBILITY_HIDDEN,
532 SP_CSS_VISIBILITY_COLLAPSE,
533 SP_CSS_VISIBILITY_VISIBLE
534 };
536 enum SPOverflow {
537 SP_CSS_OVERFLOW_VISIBLE,
538 SP_CSS_OVERFLOW_HIDDEN,
539 SP_CSS_OVERFLOW_SCROLL,
540 SP_CSS_OVERFLOW_AUTO
541 };
543 /// \todo more display types
544 enum SPCSSDisplay {
545 SP_CSS_DISPLAY_NONE,
546 SP_CSS_DISPLAY_INLINE,
547 SP_CSS_DISPLAY_BLOCK,
548 SP_CSS_DISPLAY_LIST_ITEM,
549 SP_CSS_DISPLAY_RUN_IN,
550 SP_CSS_DISPLAY_COMPACT,
551 SP_CSS_DISPLAY_MARKER,
552 SP_CSS_DISPLAY_TABLE,
553 SP_CSS_DISPLAY_INLINE_TABLE,
554 SP_CSS_DISPLAY_TABLE_ROW_GROUP,
555 SP_CSS_DISPLAY_TABLE_HEADER_GROUP,
556 SP_CSS_DISPLAY_TABLE_FOOTER_GROUP,
557 SP_CSS_DISPLAY_TABLE_ROW,
558 SP_CSS_DISPLAY_TABLE_COLUMN_GROUP,
559 SP_CSS_DISPLAY_TABLE_COLUMN,
560 SP_CSS_DISPLAY_TABLE_CELL,
561 SP_CSS_DISPLAY_TABLE_CAPTION
562 };
564 enum SPEnableBackground {
565 SP_CSS_BACKGROUND_ACCUMULATE,
566 SP_CSS_BACKGROUND_NEW
567 };
569 /// An SPTextStyle has a refcount, a font family, and a font name.
570 struct SPTextStyle {
571 int refcount;
573 /* CSS font properties */
574 SPIString font_family;
576 /* Full font name, as font_factory::ConstructFontSpecification would give */
577 SPIString font_specification;
579 /** \todo fixme: The 'font' property is ugly, and not working (lauris) */
580 SPIString font;
581 };
583 SPCSSAttr *sp_css_attr_from_style (SPStyle const *const style, guint flags);
584 SPCSSAttr *sp_css_attr_from_object(SPObject *object, guint flags = SP_STYLE_FLAG_IFSET);
585 SPCSSAttr *sp_css_attr_unset_text(SPCSSAttr *css);
586 SPCSSAttr *sp_css_attr_unset_uris(SPCSSAttr *css);
587 SPCSSAttr *sp_css_attr_scale(SPCSSAttr *css, double ex);
589 void sp_style_unset_property_attrs(SPObject *o);
591 void sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive);
593 gchar *attribute_unquote(gchar const *val);
594 gchar *css2_escape_quote(gchar const *val);
596 #endif // SEEN_SP_STYLE_H
599 /*
600 Local Variables:
601 mode:c++
602 c-file-style:"stroustrup"
603 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
604 indent-tabs-mode:nil
605 fill-column:99
606 End:
607 */
608 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :