summaryrefslogtreecommitdiff
path: root/src/css.hh
diff options
context:
space:
mode:
authorRodrigo Arias Mallo <rodarima@gmail.com>2024-05-31 19:53:14 +0200
committerRodrigo Arias Mallo <rodarima@gmail.com>2024-10-05 09:46:55 +0200
commitac56aa678cfe1666b60821f342ccb7b3d3fa7a0d (patch)
tree2887de1a5a21c8550e4bd52ea8867c18a5e2bac9 /src/css.hh
parentee32215fc1937e8a5e3c26b646ca8563e8de92e9 (diff)
Define CssLength as struct instead of int
The int type doesn't have a fixed size, and is only guarantee to hold 16 bits. The current implementation assumes a size of at least 32 bits, an uses three bits to encode the type of information stored in the rest. To add more types of lengths we would need to take more bits from the value itself. A simpler approach is just to use a enumeration to take care of the type of length and a union to encapsulate the different lengths values.
Diffstat (limited to 'src/css.hh')
-rw-r--r--src/css.hh71
1 files changed, 28 insertions, 43 deletions
diff --git a/src/css.hh b/src/css.hh
index 44328c42..1f670c35 100644
--- a/src/css.hh
+++ b/src/css.hh
@@ -67,28 +67,9 @@ typedef enum {
} CssValueType;
/**
- * Lengths are represented as int in the following way:
- *
- * @verbatim
- *
- * | <------ integer value ------> |
- *
- * +---+ - - - +---+---+- - - - - -+---+---+---+---+
- * | integer part | type |
- * +---+ - - - +---+---+- - - - - -+---+---+---+---+
- * | integer part | decimal fraction | type |
- * +---+ - - - +---+---+- - - - - -+---+---+---+---+
- * n-1 15 14 3 2 1 0
- *
- * | <------ fixed point value ------> |
- *
- * @endverbatim
- *
- * where type is one of the CSS_LENGTH_TYPE_* values.
- * CSS_LENGTH_TYPE_PX values are stored as
- * 29 bit signed integer, all other types as fixed point values.
+ * CSS lengths are represented by the CssLength struct, which can hold
+ * different types of values.
*/
-typedef int CssLength;
typedef enum {
CSS_LENGTH_TYPE_NONE,
@@ -103,53 +84,56 @@ typedef enum {
CSS_LENGTH_TYPE_AUTO /**< This can be used as a simple value. */
} CssLengthType;
-inline CssLength CSS_CREATE_LENGTH (float v, CssLengthType t) {
- 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;
+/* Aligned to 64 bits */
+typedef struct {
+ CssLengthType type;
+ union {
+ int i;
+ float f;
+ };
+} CssLength;
+inline CssLength CSS_CREATE_LENGTH (float v, CssLengthType t) {
+ CssLength l;
+ l.type = t;
switch (t) {
case CSS_LENGTH_TYPE_PX:
- iv = lout::misc::roundInt(v);
- 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;
+ l.i = lout::misc::roundInt(v);
+ break;
case CSS_LENGTH_TYPE_NONE:
case CSS_LENGTH_TYPE_MM:
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;
+ l.f = v;
+ break;
case CSS_LENGTH_TYPE_AUTO:
- return t;
+ l.i = 0;
+ break;
default:
assert(false);
- return CSS_LENGTH_TYPE_AUTO;
+ break;
}
+
+ return l;
}
inline CssLengthType CSS_LENGTH_TYPE (CssLength l) {
- return (CssLengthType) (l & 7);
+ return l.type;
}
inline float CSS_LENGTH_VALUE (CssLength l) {
switch (CSS_LENGTH_TYPE(l)) {
case CSS_LENGTH_TYPE_PX:
- return (float) (l >> 3);
+ return (float) l.i;
case CSS_LENGTH_TYPE_NONE:
case CSS_LENGTH_TYPE_MM:
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);
+ return l.f;
case CSS_LENGTH_TYPE_AUTO:
return 0.0;
default:
@@ -251,12 +235,13 @@ typedef enum {
} CssPropertyName;
typedef struct {
- int32_t posX;
- int32_t posY;
+ CssLength posX;
+ CssLength posY;
} CssBackgroundPosition;
typedef union {
int32_t intVal;
+ CssLength lenVal;
char *strVal;
CssBackgroundPosition *posVal;
} CssPropertyValue;