Code

Correct behavior of gradient stops to extract color from swatch when color is drag...
[inkscape.git] / src / style.h
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);
181 private:
182     SPIPaint(SPIPaint const&);
183     SPIPaint &operator=(SPIPaint const &);
184 };
186 /// Filter type internal to SPStyle
187 struct SPIFilter {
188     unsigned set : 1;
189     unsigned inherit : 1;
190     SPFilterReference *href;
191 };
193 enum {
194     SP_FONT_SIZE_LITERAL,
195     SP_FONT_SIZE_LENGTH,
196     SP_FONT_SIZE_PERCENTAGE
197 };
199 enum {
200     SP_BASELINE_SHIFT_LITERAL,
201     SP_BASELINE_SHIFT_LENGTH,
202     SP_BASELINE_SHIFT_PERCENTAGE
203 };
205 #define SP_FONT_SIZE ((1 << 24) - 1)
207 #define SP_F8_16_TO_FLOAT(v) ((gdouble) (v) / (1 << 16))
208 #define SP_F8_16_FROM_FLOAT(v) ((int) ((v) * ((1 << 16) + 0.9999)))
210 #define SP_STYLE_FLAG_IFSET (1 << 0)
211 #define SP_STYLE_FLAG_IFDIFF (1 << 1)
212 #define SP_STYLE_FLAG_ALWAYS (1 << 2)
214 /// Fontsize type internal to SPStyle.
215 struct SPIFontSize {
216     unsigned set : 1;
217     unsigned inherit : 1;
218     unsigned type : 2;
219     unsigned value : 24;
220     float computed;
221 };
223 /// Baseline shift type internal to SPStyle.
224 struct SPIBaselineShift {
225     unsigned set : 1;
226     unsigned inherit : 1;
227     unsigned type : 2;
228     unsigned unit : 4;
229     unsigned literal: 2;
230     float value; // Can be negative
231     float computed;
232 };
234 /// Text decoration type internal to SPStyle.
235 struct SPITextDecoration {
236     unsigned set : 1;
237     unsigned inherit : 1;
238     unsigned underline : 1;
239     unsigned overline : 1;
240     unsigned line_through : 1;
241     unsigned blink : 1;    // "Conforming user agents are not required to support this value." yay!
242 };
244 /// Extended length type internal to SPStyle.
245 struct SPILengthOrNormal {
246     unsigned set : 1;
247     unsigned inherit : 1;
248     unsigned normal : 1;
249     unsigned unit : 4;
250     float value;
251     float computed;
252 };
254 class SPTextStyle;
256 /// Stroke dash details.
257 class NRVpathDash {
258 public:
259     double offset;
260     int n_dash;
261     double *dash;
262 };
264 /// An SVG style object.
265 struct SPStyle {
266     int refcount;
268     /** Object we are attached to */
269     SPObject *object;
270     /** Document we are associated with */
271     SPDocument *document;
273     /** Our text style component */
274     SPTextStyle *text;
275     unsigned text_private : 1;
277     /* CSS2 */
278     /* Font */
279     /** Size of the font */
280     SPIFontSize font_size;
281     /** Style of the font */
282     SPIEnum font_style;
283     /** Which substyle of the font */
284     SPIEnum font_variant;
285     /** Weight of the font */
286     SPIEnum font_weight;
287     /** Stretch of the font */
288     SPIEnum font_stretch;
290     /** First line indent of paragraphs (css2 16.1) */
291     SPILength text_indent;
292     /** text alignment (css2 16.2) (not to be confused with text-anchor) */
293     SPIEnum text_align;
294     /** text decoration (css2 16.3.1) */
295     SPITextDecoration text_decoration;
296     // 16.3.2 is text-shadow. That's complicated.
297     /** Line spacing (css2 10.8.1) */
298     SPILengthOrNormal line_height;
299     /** letter spacing (css2 16.4) */
300     SPILengthOrNormal letter_spacing;
301     /** word spacing (also css2 16.4) */
302     SPILengthOrNormal word_spacing;
303     /** capitalization (css2 16.5) */
304     SPIEnum text_transform;
306     /* CSS3 Text */
307     /** text direction (css3 text 3.2) */
308     SPIEnum direction;
309     /** block progression (css3 text 3.2) */
310     SPIEnum block_progression;
311     /** Writing mode (css3 text 3.2 and svg1.1 10.7.2) */
312     SPIEnum writing_mode;
313     /** Baseline shift (svg1.1 10.9.2) */
314     SPIBaselineShift baseline_shift;
316     /* SVG */
317     /** Anchor of the text (svg1.1 10.9.1) */
318     SPIEnum text_anchor;
320     /* Misc attributes */
321     unsigned clip_set : 1;
322     unsigned color_set : 1;
323     unsigned cursor_set : 1;
324     unsigned overflow_set : 1;
325     unsigned clip_path_set : 1;
326     unsigned clip_rule_set : 1;
327     unsigned mask_set : 1;
329     /** display */
330     SPIEnum display;
332     /** overflow */
333     SPIEnum overflow;
335     /** visibility */
336     SPIEnum visibility;
338     /** opacity */
339     SPIScale24 opacity;
341     /** color */
342     SPIPaint color;
344     /** fill */
345     SPIPaint fill;
346     /** fill-opacity */
347     SPIScale24 fill_opacity;
348     /** fill-rule: 0 nonzero, 1 evenodd */
349     SPIEnum fill_rule;
351     /** stroke */
352     SPIPaint stroke;
353     /** stroke-width */
354     SPILength stroke_width;
355     /** stroke-linecap */
356     SPIEnum stroke_linecap;
357     /** stroke-linejoin */
358     SPIEnum stroke_linejoin;
359     /** stroke-miterlimit */
360     SPIFloat stroke_miterlimit;
361     /** stroke-dash* */
362     NRVpathDash stroke_dash;
363     unsigned stroke_dasharray_set : 1;
364     unsigned stroke_dasharray_inherit : 1;
365     unsigned stroke_dashoffset_set : 1;
366     unsigned stroke_dashoffset_inherit : 1;
367     /** stroke-opacity */
368     SPIScale24 stroke_opacity;
370     /** Marker list */
371     SPIString marker[SP_MARKER_LOC_QTY];
373     /** Filter effect */
374     SPIFilter filter;
376     SPIEnum filter_blend_mode;
378    /** normally not used, but duplicates the Gaussian blur deviation (if any) from the attached
379         filter when the style is used for querying */
380     SPILength filter_gaussianBlur_deviation;
382     /** enable-background, used for defining where filter effects get
383      * their background image */
384     SPIEnum enable_background;
386     /// style belongs to a cloned object
387     bool cloned;
389     sigc::connection release_connection;
391     sigc::connection filter_modified_connection;
392     sigc::connection fill_ps_modified_connection;
393     sigc::connection stroke_ps_modified_connection;
395     SPObject *getFilter() { return (filter.href) ? filter.href->getObject() : 0; }
396     SPObject const *getFilter() const { return (filter.href) ? filter.href->getObject() : 0; }
397     gchar const *getFilterURI() const { return (filter.href) ? filter.href->getURI()->toString() : 0; }
399     SPPaintServer *getFillPaintServer() { return (fill.value.href) ? fill.value.href->getObject() : 0; }
400     SPPaintServer const *getFillPaintServer() const { return (fill.value.href) ? fill.value.href->getObject() : 0; }
401     gchar const *getFillURI() const { return (fill.value.href) ? fill.value.href->getURI()->toString() : 0; }
403     SPPaintServer *getStrokePaintServer() { return (stroke.value.href) ? stroke.value.href->getObject() : 0; }
404     SPPaintServer const *getStrokePaintServer() const { return (stroke.value.href) ? stroke.value.href->getObject() : 0; }
405     gchar const  *getStrokeURI() const { return (stroke.value.href) ? stroke.value.href->getURI()->toString() : 0; }
406 };
408 SPStyle *sp_style_new(SPDocument *document);
410 SPStyle *sp_style_new_from_object(SPObject *object);
412 SPStyle *sp_style_ref(SPStyle *style);
414 SPStyle *sp_style_unref(SPStyle *style);
416 void sp_style_read_from_object(SPStyle *style, SPObject *object);
418 void sp_style_read_from_prefs(SPStyle *style, Glib::ustring const &path);
420 void sp_style_merge_from_style_string(SPStyle *style, gchar const *p);
422 void sp_style_merge_from_parent(SPStyle *style, SPStyle const *parent);
424 void sp_style_merge_from_dying_parent(SPStyle *style, SPStyle const *parent);
426 gchar *sp_style_write_string(SPStyle const *style, guint flags = SP_STYLE_FLAG_IFSET);
428 gchar *sp_style_write_difference(SPStyle const *from, SPStyle const *to);
430 void sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri);
432 /* SPTextStyle */
434 enum SPCSSFontSize {
435     SP_CSS_FONT_SIZE_XX_SMALL,
436     SP_CSS_FONT_SIZE_X_SMALL,
437     SP_CSS_FONT_SIZE_SMALL,
438     SP_CSS_FONT_SIZE_MEDIUM,
439     SP_CSS_FONT_SIZE_LARGE,
440     SP_CSS_FONT_SIZE_X_LARGE,
441     SP_CSS_FONT_SIZE_XX_LARGE,
442     SP_CSS_FONT_SIZE_SMALLER,
443     SP_CSS_FONT_SIZE_LARGER
444 };
446 enum SPCSSFontStyle {
447     SP_CSS_FONT_STYLE_NORMAL,
448     SP_CSS_FONT_STYLE_ITALIC,
449     SP_CSS_FONT_STYLE_OBLIQUE
450 };
452 enum SPCSSFontVariant {
453     SP_CSS_FONT_VARIANT_NORMAL,
454     SP_CSS_FONT_VARIANT_SMALL_CAPS
455 };
457 enum SPCSSFontWeight {
458     SP_CSS_FONT_WEIGHT_100,
459     SP_CSS_FONT_WEIGHT_200,
460     SP_CSS_FONT_WEIGHT_300,
461     SP_CSS_FONT_WEIGHT_400,
462     SP_CSS_FONT_WEIGHT_500,
463     SP_CSS_FONT_WEIGHT_600,
464     SP_CSS_FONT_WEIGHT_700,
465     SP_CSS_FONT_WEIGHT_800,
466     SP_CSS_FONT_WEIGHT_900,
467     SP_CSS_FONT_WEIGHT_NORMAL,
468     SP_CSS_FONT_WEIGHT_BOLD,
469     SP_CSS_FONT_WEIGHT_LIGHTER,
470     SP_CSS_FONT_WEIGHT_BOLDER
471 };
473 enum SPCSSFontStretch {
474     SP_CSS_FONT_STRETCH_ULTRA_CONDENSED,
475     SP_CSS_FONT_STRETCH_EXTRA_CONDENSED,
476     SP_CSS_FONT_STRETCH_CONDENSED,
477     SP_CSS_FONT_STRETCH_SEMI_CONDENSED,
478     SP_CSS_FONT_STRETCH_NORMAL,
479     SP_CSS_FONT_STRETCH_SEMI_EXPANDED,
480     SP_CSS_FONT_STRETCH_EXPANDED,
481     SP_CSS_FONT_STRETCH_EXTRA_EXPANDED,
482     SP_CSS_FONT_STRETCH_ULTRA_EXPANDED,
483     SP_CSS_FONT_STRETCH_NARROWER,
484     SP_CSS_FONT_STRETCH_WIDER
485 };
487 enum SPCSSTextAlign {
488     SP_CSS_TEXT_ALIGN_START,
489     SP_CSS_TEXT_ALIGN_END,
490     SP_CSS_TEXT_ALIGN_LEFT,
491     SP_CSS_TEXT_ALIGN_RIGHT,
492     SP_CSS_TEXT_ALIGN_CENTER,
493     SP_CSS_TEXT_ALIGN_JUSTIFY
494     // also <string> is allowed, but only within table calls
495 };
497 enum SPCSSTextTransform {
498     SP_CSS_TEXT_TRANSFORM_CAPITALIZE,
499     SP_CSS_TEXT_TRANSFORM_UPPERCASE,
500     SP_CSS_TEXT_TRANSFORM_LOWERCASE,
501     SP_CSS_TEXT_TRANSFORM_NONE
502 };
504 enum SPCSSDirection {
505     SP_CSS_DIRECTION_LTR,
506     SP_CSS_DIRECTION_RTL
507 };
509 enum SPCSSBlockProgression {
510     SP_CSS_BLOCK_PROGRESSION_TB,
511     SP_CSS_BLOCK_PROGRESSION_RL,
512     SP_CSS_BLOCK_PROGRESSION_LR
513 };
515 enum SPCSSWritingMode {
516     SP_CSS_WRITING_MODE_LR_TB,
517     SP_CSS_WRITING_MODE_RL_TB,
518     SP_CSS_WRITING_MODE_TB_RL,
519     SP_CSS_WRITING_MODE_TB_LR
520 };
522 enum SPTextAnchor {
523     SP_CSS_TEXT_ANCHOR_START,
524     SP_CSS_TEXT_ANCHOR_MIDDLE,
525     SP_CSS_TEXT_ANCHOR_END
526 };
528 enum SPCSSBaselineShift {
529   SP_CSS_BASELINE_SHIFT_BASELINE,
530   SP_CSS_BASELINE_SHIFT_SUB,
531   SP_CSS_BASELINE_SHIFT_SUPER
532 };
534 enum SPVisibility {
535     SP_CSS_VISIBILITY_HIDDEN,
536     SP_CSS_VISIBILITY_COLLAPSE,
537     SP_CSS_VISIBILITY_VISIBLE
538 };
540 enum SPOverflow {
541     SP_CSS_OVERFLOW_VISIBLE,
542     SP_CSS_OVERFLOW_HIDDEN,
543     SP_CSS_OVERFLOW_SCROLL,
544     SP_CSS_OVERFLOW_AUTO
545 };
547 /// \todo more display types
548 enum SPCSSDisplay {
549     SP_CSS_DISPLAY_NONE,
550     SP_CSS_DISPLAY_INLINE,
551     SP_CSS_DISPLAY_BLOCK,
552     SP_CSS_DISPLAY_LIST_ITEM,
553     SP_CSS_DISPLAY_RUN_IN,
554     SP_CSS_DISPLAY_COMPACT,
555     SP_CSS_DISPLAY_MARKER,
556     SP_CSS_DISPLAY_TABLE,
557     SP_CSS_DISPLAY_INLINE_TABLE,
558     SP_CSS_DISPLAY_TABLE_ROW_GROUP,
559     SP_CSS_DISPLAY_TABLE_HEADER_GROUP,
560     SP_CSS_DISPLAY_TABLE_FOOTER_GROUP,
561     SP_CSS_DISPLAY_TABLE_ROW,
562     SP_CSS_DISPLAY_TABLE_COLUMN_GROUP,
563     SP_CSS_DISPLAY_TABLE_COLUMN,
564     SP_CSS_DISPLAY_TABLE_CELL,
565     SP_CSS_DISPLAY_TABLE_CAPTION
566 };
568 enum SPEnableBackground {
569     SP_CSS_BACKGROUND_ACCUMULATE,
570     SP_CSS_BACKGROUND_NEW
571 };
573 /// An SPTextStyle has a refcount, a font family, and a font name.
574 struct SPTextStyle {
575     int refcount;
577     /* CSS font properties */
578     SPIString font_family;
580     /* Full font name, as font_factory::ConstructFontSpecification would give */
581     SPIString font_specification;
583     /** \todo fixme: The 'font' property is ugly, and not working (lauris) */
584     SPIString font;
585 };
587 SPCSSAttr *sp_css_attr_from_style (SPStyle const *const style, guint flags);
588 SPCSSAttr *sp_css_attr_from_object(SPObject *object, guint flags = SP_STYLE_FLAG_IFSET);
589 SPCSSAttr *sp_css_attr_unset_text(SPCSSAttr *css);
590 SPCSSAttr *sp_css_attr_unset_uris(SPCSSAttr *css);
591 SPCSSAttr *sp_css_attr_scale(SPCSSAttr *css, double ex);
593 void sp_style_unset_property_attrs(SPObject *o);
595 void sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive);
597 gchar *attribute_unquote(gchar const *val);
598 gchar *css2_escape_quote(gchar const *val);
600 #endif // SEEN_SP_STYLE_H
603 /*
604   Local Variables:
605   mode:c++
606   c-file-style:"stroustrup"
607   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
608   indent-tabs-mode:nil
609   fill-column:99
610   End:
611 */
612 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :