Code

df2ba127f6b374ccdb401fb4155afcfe10a0ffd4
[inkscape.git] / src / style.h
1 #ifndef __SP_STYLE_H__
2 #define __SP_STYLE_H__
4 /** \file
5  * SPStyle - a style object for SPItem objects
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *
10  * Copyright (C) 2001-2002 Lauris Kaplinski
11  * Copyright (C) 2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #include "color.h"
17 #include "forward.h"
18 #include "sp-marker-loc.h"
19 #include "sp-filter.h"
21 #include <sigc++/connection.h>
23 namespace Inkscape {
24 namespace XML {
25 class Node;
26 }
27 }
29 class SPCSSAttr;
31 namespace Inkscape {
32 gchar *parse_css_url(gchar const *string);
33 }
35 class SPIFloat;
36 class SPIScale24;
37 class SPIInt;
38 class SPIShort;
39 class SPIEnum;
40 class SPIString;
41 class SPILength;
42 class SPIPaint;
43 class SPIFontSize;
45 /// Float type internal to SPStyle.
46 struct SPIFloat {
47     unsigned set : 1;
48     unsigned inherit : 1;
49     unsigned data : 30;
50     float value;
51 };
53 /*
54  * One might think that the best value for SP_SCALE24_MAX would be ((1<<24)-1), which allows the
55  * greatest possible precision for fitting [0, 1] fractions into 24 bits.
56  *
57  * However, in practice, that gives a problem with 0.5, which falls half way between two fractions
58  * of ((1<<24)-1).  What's worse is that casting double(1<<23) / ((1<<24)-1) to float on x86
59  * produces wrong rounding behaviour, resulting in a fraction of ((1<<23)+2.0f) / (1<<24) rather
60  * than ((1<<23)+1.0f) / (1<<24) as one would expect, let alone ((1<<23)+0.0f) / (1<<24) as one
61  * would ideally like for this example.
62  *
63  * The value (1<<23) is thus best if one considers float conversions alone.
64  *
65  * The value 0xff0000 can exactly represent all 8-bit alpha channel values,
66  * and can exactly represent all multiples of 0.1.  I haven't yet tested whether
67  * rounding bugs still get in the way of conversions to & from float, but my instinct is that
68  * it's fairly safe because 0xff fits three times inside float's significand.
69  *
70  * We should probably use the value 0xffff00 once we support 16 bits per channel and/or LittleCMS,
71  * though that might need to be accompanied by greater use of double instead of float for
72  * colours and opacities, to be safe from rounding bugs.
73  */
74 #define SP_SCALE24_MAX (0xff0000)
75 #define SP_SCALE24_TO_FLOAT(v) ((double) (v) / SP_SCALE24_MAX)
76 #define SP_SCALE24_FROM_FLOAT(v) unsigned(((v) * SP_SCALE24_MAX) + .5)
78 /** Returns a scale24 for the product of two scale24 values. */
79 #define SP_SCALE24_MUL(_v1, _v2) unsigned((double)(_v1) * (_v2) / SP_SCALE24_MAX + .5)
81 /// 24 bit data type internal to SPStyle.
82 struct SPIScale24 {
83     unsigned set : 1;
84     unsigned inherit : 1;
85     unsigned value : 24;
86 };
88 /// Int type internal to SPStyle.
89 struct SPIInt {
90     unsigned set : 1;
91     unsigned inherit : 1;
92     unsigned data : 30;
93     int value;
94 };
96 /// Short type internal to SPStyle.
97 struct SPIShort {
98     unsigned set : 1;
99     unsigned inherit : 1;
100     unsigned data : 14;
101     int value : 16;
102 };
104 /// Enum type internal to SPStyle.
105 struct SPIEnum {
106     unsigned set : 1;
107     unsigned inherit : 1;
108     unsigned value : 8;
109     unsigned computed : 8;
110 };
112 /// String type internal to SPStyle.
113 struct SPIString {
114     unsigned set : 1;
115     unsigned inherit : 1;
116     unsigned data : 30;
117     gchar *value;
118 };
120 enum {
121     SP_CSS_UNIT_NONE,
122     SP_CSS_UNIT_PX,
123     SP_CSS_UNIT_PT,
124     SP_CSS_UNIT_PC,
125     SP_CSS_UNIT_MM,
126     SP_CSS_UNIT_CM,
127     SP_CSS_UNIT_IN,
128     SP_CSS_UNIT_EM,
129     SP_CSS_UNIT_EX,
130     SP_CSS_UNIT_PERCENT
131 };
133 /// Length type internal to SPStyle.
134 struct SPILength {
135     unsigned set : 1;
136     unsigned inherit : 1;
137     unsigned unit : 4;
138     float value;
139     float computed;
140 };
142 #define SP_STYLE_FILL_SERVER(s) (((SPStyle *) (s))->fill.value.paint.server)
143 #define SP_STYLE_STROKE_SERVER(s) (((SPStyle *) (s))->stroke.value.paint.server)
144 #define SP_OBJECT_STYLE_FILL_SERVER(o) (SP_OBJECT (o)->style->fill.value.paint.server)
145 #define SP_OBJECT_STYLE_STROKE_SERVER(o) (SP_OBJECT (o)->style->stroke.value.paint.server)
147 enum {
148     SP_PAINT_TYPE_NONE,
149     SP_PAINT_TYPE_COLOR,
150     SP_PAINT_TYPE_PAINTSERVER,
151     SP_PAINT_TYPE_IMPOSSIBLE
152 };
154 class SVGICCColor;
156 /// Paint type internal to SPStyle.
157 struct SPIPaint {
158     unsigned set : 1;
159     unsigned inherit : 1;
160     unsigned currentcolor : 1;
161     unsigned type : 2;
162     struct {
163         struct {
164             SPPaintServer *server;
165             gchar *uri;
166         } paint;
167         SPColor color;
168         SVGICCColor *iccColor;
169     } value;
170 };
172 struct SPFilterReference;
174 /// Filter type internal to SPStyle
175 struct SPIFilter {
176     unsigned set : 1;
177     unsigned inherit : 1;
178     SPFilterReference *href;
179 };
181 enum {
182     SP_FONT_SIZE_LITERAL,
183     SP_FONT_SIZE_LENGTH,
184     SP_FONT_SIZE_PERCENTAGE
185 };
187 #define SP_FONT_SIZE ((1 << 24) - 1)
189 #define SP_F8_16_TO_FLOAT(v) ((gdouble) (v) / (1 << 16))
190 #define SP_F8_16_FROM_FLOAT(v) ((int) ((v) * ((1 << 16) + 0.9999)))
192 #define SP_STYLE_FLAG_IFSET (1 << 0)
193 #define SP_STYLE_FLAG_IFDIFF (1 << 1)
194 #define SP_STYLE_FLAG_ALWAYS (1 << 2)
196 /// Fontsize type internal to SPStyle.
197 struct SPIFontSize {
198     unsigned set : 1;
199     unsigned inherit : 1;
200     unsigned type : 2;
201     unsigned value : 24;
202     float computed;
203 };
205 /// Text decoration type internal to SPStyle.
206 struct SPITextDecoration {
207     unsigned set : 1;
208     unsigned inherit : 1;
209     unsigned underline : 1;
210     unsigned overline : 1;
211     unsigned line_through : 1;
212     unsigned blink : 1;    // "Conforming user agents are not required to support this value." yay!
213 };
215 /// Extended length type internal to SPStyle.
216 struct SPILengthOrNormal {
217     unsigned set : 1;
218     unsigned inherit : 1;
219     unsigned normal : 1;
220     unsigned unit : 4;
221     float value;
222     float computed;
223 };
225 class SPTextStyle;
227 /// Stroke dash details.
228 class NRVpathDash {
229 public:
230     double offset;
231     int n_dash;
232     double *dash;
233 };
235 /// An SVG style object.
236 struct SPStyle {
237     int refcount;
238     /** Object we are attached to */
239     SPObject *object;
240     /** Our text style component */
241     SPTextStyle *text;
242     unsigned text_private : 1;
244     /* CSS2 */
245     /* Font */
246     /** Size of the font */
247     SPIFontSize font_size;
248     /** Style of the font */
249     SPIEnum font_style;
250     /** Which substyle of the font */
251     SPIEnum font_variant;
252     /** Weight of the font */
253     SPIEnum font_weight;
254     /** Stretch of the font */
255     SPIEnum font_stretch;
257     /** First line indent of paragraphs (css2 16.1) */
258     SPILength text_indent;
259     /** text alignment (css2 16.2) (not to be confused with text-anchor) */
260     SPIEnum text_align;
261     /** text decoration (css2 16.3.1) */
262     SPITextDecoration text_decoration;
263     // 16.3.2 is text-shadow. That's complicated.
264     /** Line spacing (css2 10.8.1) */
265     SPILengthOrNormal line_height;
266     /** letter spacing (css2 16.4) */
267     SPILengthOrNormal letter_spacing;
268     /** word spacing (also css2 16.4) */
269     SPILengthOrNormal word_spacing;
270     /** capitalization (css2 16.5) */
271     SPIEnum text_transform;
273     /* CSS3 Text */
274     /** text direction (css3 text 3.2) */
275     SPIEnum direction;
276     /** block progression (css3 text 3.2) */
277     SPIEnum block_progression;
278     /** Writing mode (css3 text 3.2 and svg1.1 10.7.2) */
279     SPIEnum writing_mode;
281     /* SVG */
282     /** Anchor of the text (svg1.1 10.9.1) */
283     SPIEnum text_anchor;
285     /* Misc attributes */
286     unsigned clip_set : 1;
287     unsigned color_set : 1;
288     unsigned cursor_set : 1;
289     unsigned overflow_set : 1;
290     unsigned clip_path_set : 1;
291     unsigned clip_rule_set : 1;
292     unsigned mask_set : 1;
294     /** display */
295     SPIEnum display;
297     /** overflow */
298     SPIEnum overflow;
300     /** visibility */
301     SPIEnum visibility;
303     /** opacity */
304     SPIScale24 opacity;
306     /** color */
307     SPIPaint color;
309     /** fill */
310     SPIPaint fill;
311     /** fill-opacity */
312     SPIScale24 fill_opacity;
313     /** fill-rule: 0 nonzero, 1 evenodd */
314     SPIEnum fill_rule;
316     /** stroke */
317     SPIPaint stroke;
318     /** stroke-width */
319     SPILength stroke_width;
320     /** stroke-linecap */
321     SPIEnum stroke_linecap;
322     /** stroke-linejoin */
323     SPIEnum stroke_linejoin;
324     /** stroke-miterlimit */
325     SPIFloat stroke_miterlimit;
326     /** stroke-dash* */
327     NRVpathDash stroke_dash;
328     unsigned stroke_dasharray_set : 1;
329     unsigned stroke_dasharray_inherit : 1;
330     unsigned stroke_dashoffset_set : 1;
331     /** stroke-opacity */
332     SPIScale24 stroke_opacity;
334     /** Marker list */
335     SPIString marker[SP_MARKER_LOC_QTY];
337     /** Filter effect */
338     SPIFilter filter;
340     SPIEnum filter_blend_mode;
342    /** normally not used, but duplicates the Gaussian blur deviation (if any) from the attached
343         filter when the style is used for querying */
344     SPILength filter_gaussianBlur_deviation;
346     /** enable-background, used for defining where filter effects get
347      * their background image */
348     SPIEnum enable_background;
350     /// style belongs to a cloned object, must not href anything
351     bool cloned; 
352     /// style has hreffed its fill/stroke paintservers, needs to release.
353     bool fill_hreffed; 
354     bool stroke_hreffed; 
356     sigc::connection release_connection;
358     sigc::connection fill_release_connection;
359     sigc::connection fill_modified_connection;
361     sigc::connection stroke_release_connection;
362     sigc::connection stroke_modified_connection;
363 };
365 SPStyle *sp_style_new(SPDocument *document);
367 SPStyle *sp_style_new_from_object(SPObject *object);
369 SPStyle *sp_style_ref(SPStyle *style);
371 SPStyle *sp_style_unref(SPStyle *style);
373 void sp_style_read_from_object(SPStyle *style, SPObject *object);
375 void sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr);
377 void sp_style_merge_from_style_string(SPStyle *style, gchar const *p);
379 void sp_style_merge_from_parent(SPStyle *style, SPStyle const *parent);
381 void sp_style_merge_from_dying_parent(SPStyle *style, SPStyle const *parent);
383 gchar *sp_style_write_string(SPStyle const *style, guint flags = SP_STYLE_FLAG_IFSET);
385 gchar *sp_style_write_difference(SPStyle const *from, SPStyle const *to);
387 /* SPTextStyle */
389 enum SPCSSFontSize {
390     SP_CSS_FONT_SIZE_XX_SMALL,
391     SP_CSS_FONT_SIZE_X_SMALL,
392     SP_CSS_FONT_SIZE_SMALL,
393     SP_CSS_FONT_SIZE_MEDIUM,
394     SP_CSS_FONT_SIZE_LARGE,
395     SP_CSS_FONT_SIZE_X_LARGE,
396     SP_CSS_FONT_SIZE_XX_LARGE,
397     SP_CSS_FONT_SIZE_SMALLER,
398     SP_CSS_FONT_SIZE_LARGER
399 };
401 enum SPCSSFontStyle {
402     SP_CSS_FONT_STYLE_NORMAL,
403     SP_CSS_FONT_STYLE_ITALIC,
404     SP_CSS_FONT_STYLE_OBLIQUE
405 };
407 enum SPCSSFontVariant {
408     SP_CSS_FONT_VARIANT_NORMAL,
409     SP_CSS_FONT_VARIANT_SMALL_CAPS
410 };
412 enum SPCSSFontWeight {
413     SP_CSS_FONT_WEIGHT_100,
414     SP_CSS_FONT_WEIGHT_200,
415     SP_CSS_FONT_WEIGHT_300,
416     SP_CSS_FONT_WEIGHT_400,
417     SP_CSS_FONT_WEIGHT_500,
418     SP_CSS_FONT_WEIGHT_600,
419     SP_CSS_FONT_WEIGHT_700,
420     SP_CSS_FONT_WEIGHT_800,
421     SP_CSS_FONT_WEIGHT_900,
422     SP_CSS_FONT_WEIGHT_NORMAL,
423     SP_CSS_FONT_WEIGHT_BOLD,
424     SP_CSS_FONT_WEIGHT_LIGHTER,
425     SP_CSS_FONT_WEIGHT_BOLDER
426 };
428 enum SPCSSFontStretch {
429     SP_CSS_FONT_STRETCH_ULTRA_CONDENSED,
430     SP_CSS_FONT_STRETCH_EXTRA_CONDENSED,
431     SP_CSS_FONT_STRETCH_CONDENSED,
432     SP_CSS_FONT_STRETCH_SEMI_CONDENSED,
433     SP_CSS_FONT_STRETCH_NORMAL,
434     SP_CSS_FONT_STRETCH_SEMI_EXPANDED,
435     SP_CSS_FONT_STRETCH_EXPANDED,
436     SP_CSS_FONT_STRETCH_EXTRA_EXPANDED,
437     SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
438     SP_CSS_FONT_STRETCH_NARROWER,
439     SP_CSS_FONT_STRETCH_WIDER
440 };
442 enum SPCSSTextAlign {
443     SP_CSS_TEXT_ALIGN_START,
444     SP_CSS_TEXT_ALIGN_END,
445     SP_CSS_TEXT_ALIGN_LEFT,
446     SP_CSS_TEXT_ALIGN_RIGHT,
447     SP_CSS_TEXT_ALIGN_CENTER,
448     SP_CSS_TEXT_ALIGN_JUSTIFY
449     // also <string> is allowed, but only within table calls
450 };
452 enum SPCSSTextTransform {
453     SP_CSS_TEXT_TRANSFORM_CAPITALIZE,
454     SP_CSS_TEXT_TRANSFORM_UPPERCASE,
455     SP_CSS_TEXT_TRANSFORM_LOWERCASE,
456     SP_CSS_TEXT_TRANSFORM_NONE
457 };
459 enum SPCSSDirection {
460     SP_CSS_DIRECTION_LTR,
461     SP_CSS_DIRECTION_RTL
462 };
464 enum SPCSSBlockProgression {
465     SP_CSS_BLOCK_PROGRESSION_TB,
466     SP_CSS_BLOCK_PROGRESSION_RL,
467     SP_CSS_BLOCK_PROGRESSION_LR
468 };
470 enum SPCSSWritingMode {
471     SP_CSS_WRITING_MODE_LR_TB,
472     SP_CSS_WRITING_MODE_RL_TB,
473     SP_CSS_WRITING_MODE_TB_RL,
474     SP_CSS_WRITING_MODE_TB_LR
475 };
477 enum SPTextAnchor {
478     SP_CSS_TEXT_ANCHOR_START,
479     SP_CSS_TEXT_ANCHOR_MIDDLE,
480     SP_CSS_TEXT_ANCHOR_END
481 };
483 enum SPVisibility {
484     SP_CSS_VISIBILITY_HIDDEN,
485     SP_CSS_VISIBILITY_COLLAPSE,
486     SP_CSS_VISIBILITY_VISIBLE
487 };
489 enum SPOverflow {
490     SP_CSS_OVERFLOW_VISIBLE,
491     SP_CSS_OVERFLOW_HIDDEN,
492     SP_CSS_OVERFLOW_SCROLL,
493     SP_CSS_OVERFLOW_AUTO
494 };
496 /// \todo more display types
497 enum SPCSSDisplay {
498     SP_CSS_DISPLAY_NONE,
499     SP_CSS_DISPLAY_INLINE,
500     SP_CSS_DISPLAY_BLOCK,
501     SP_CSS_DISPLAY_LIST_ITEM,
502     SP_CSS_DISPLAY_RUN_IN,
503     SP_CSS_DISPLAY_COMPACT,
504     SP_CSS_DISPLAY_MARKER,
505     SP_CSS_DISPLAY_TABLE,
506     SP_CSS_DISPLAY_INLINE_TABLE,
507     SP_CSS_DISPLAY_TABLE_ROW_GROUP,
508     SP_CSS_DISPLAY_TABLE_HEADER_GROUP,
509     SP_CSS_DISPLAY_TABLE_FOOTER_GROUP,
510     SP_CSS_DISPLAY_TABLE_ROW,
511     SP_CSS_DISPLAY_TABLE_COLUMN_GROUP,
512     SP_CSS_DISPLAY_TABLE_COLUMN,
513     SP_CSS_DISPLAY_TABLE_CELL,
514     SP_CSS_DISPLAY_TABLE_CAPTION
515 };
517 enum SPEnableBackground {
518     SP_CSS_BACKGROUND_ACCUMULATE,
519     SP_CSS_BACKGROUND_NEW
520 };
522 /// An SPTextStyle has a refcount, a font family, and a font name.
523 struct SPTextStyle {
524     int refcount;
526     /* CSS font properties */
527     SPIString font_family;
529     /** \todo fixme: The 'font' property is ugly, and not working (lauris) */
530     SPIString font;
531 };
533 SPCSSAttr *sp_css_attr_from_style (SPStyle const *const style, guint flags);
534 SPCSSAttr *sp_css_attr_from_object(SPObject *object, guint flags = SP_STYLE_FLAG_IFSET);
535 SPCSSAttr *sp_css_attr_unset_text(SPCSSAttr *css);
536 SPCSSAttr *sp_css_attr_unset_uris(SPCSSAttr *css);
537 SPCSSAttr *sp_css_attr_scale(SPCSSAttr *css, double ex);
539 void sp_style_unset_property_attrs(SPObject *o);
541 void sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive);
543 gchar *attribute_unquote(gchar const *val);
544 gchar *css2_escape_quote(gchar const *val);
546 #endif
549 /*
550   Local Variables:
551   mode:c++
552   c-file-style:"stroustrup"
553   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
554   indent-tabs-mode:nil
555   fill-column:99
556   End:
557 */
558 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :