diff options
author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2009-07-06 22:19:22 +0200 |
---|---|---|
committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2009-07-06 22:19:22 +0200 |
commit | 79dae260891d6fc2689d82907f8a11be65491776 (patch) | |
tree | c16c2050a62b7168d1879f543e31e074e70cc4ad /src | |
parent | 00478d5500000df83738d35da3360a89900ee22c (diff) |
adjust CssLength
CssLength now stores px and mm values as pure integers similar to
what dw::core::style::Length does.
Diffstat (limited to 'src')
-rw-r--r-- | src/css.hh | 59 |
1 files changed, 53 insertions, 6 deletions
@@ -51,14 +51,20 @@ typedef enum { /* * Lengths are represented as int in the following way: * + * | <------ integer value ------> | + * + * +---+ - - - +---+---+- - - - - -+---+---+---+---+ + * | integer part | type | * +---+ - - - +---+---+- - - - - -+---+---+---+---+ * | integer part | decimal fraction | type | * +---+ - - - +---+---+- - - - - -+---+---+---+---+ - * n-1 19 18 3 2 1 0 + * n-1 15 14 3 2 1 0 * * | <------ fixed point value ------> | * * where type is one of the CSS_LENGTH_TYPE_* values. + * CSS_LENGTH_TYPE_PX and CSS_LENGTH_TYPE_MM are stored as + * 29 bit signed integer, all other types as fixed point values. */ typedef int CssLength; @@ -76,17 +82,58 @@ typedef enum { } CssLengthType; inline CssLength CSS_CREATE_LENGTH (float v, CssLengthType t) { - return ((int) (v * (1 << 19)) & ~7 ) | t; -} - -inline float CSS_LENGTH_VALUE (CssLength l) { - return ((float)(l & ~7)) / (1 << 19); + static const int CSS_LENGTH_FRAC_MAX = (1 << (32 - 15 - 1)) - 1; + static const int CSS_LENGTH_INT_MAX = 1 << (32 - 4) - 1; + int iv; + + switch (t) { + case CSS_LENGTH_TYPE_PX: + case CSS_LENGTH_TYPE_MM: + iv = (int) (v + 0.5); + if (iv > CSS_LENGTH_INT_MAX) + iv = CSS_LENGTH_INT_MAX; + else if (iv < -CSS_LENGTH_INT_MAX) + iv = -CSS_LENGTH_INT_MAX; + return iv << 3 | t; + case CSS_LENGTH_TYPE_EM: + case CSS_LENGTH_TYPE_EX: + case CSS_LENGTH_TYPE_PERCENTAGE: + case CSS_LENGTH_TYPE_RELATIVE: + if (v > CSS_LENGTH_FRAC_MAX) + v = CSS_LENGTH_FRAC_MAX; + else if (v < -CSS_LENGTH_FRAC_MAX) + v = -CSS_LENGTH_FRAC_MAX; + return ((int) (v * (1 << 15)) & ~7 ) | t; + case CSS_LENGTH_TYPE_AUTO: + return t; + default: + assert(false); + return CSS_LENGTH_TYPE_AUTO; + } } inline CssLengthType CSS_LENGTH_TYPE (CssLength l) { return (CssLengthType) (l & 7); } +inline float CSS_LENGTH_VALUE (CssLength l) { + switch (CSS_LENGTH_TYPE(l)) { + case CSS_LENGTH_TYPE_PX: + case CSS_LENGTH_TYPE_MM: + return (float) (l >> 3); + case CSS_LENGTH_TYPE_EM: + case CSS_LENGTH_TYPE_EX: + case CSS_LENGTH_TYPE_PERCENTAGE: + case CSS_LENGTH_TYPE_RELATIVE: + return ((float)(l & ~7)) / (1 << 15); + case CSS_LENGTH_TYPE_AUTO: + return 0.0; + default: + assert(false); + return 0.0; + } +} + typedef enum { CSS_PROPERTY_BACKGROUND_ATTACHMENT, CSS_PROPERTY_BACKGROUND_COLOR, |