dillo-browser / dillo

Dillo, a multi-platform graphical web browser
https://dillo-browser.github.io/
GNU General Public License v3.0
549 stars 28 forks source link

Use fixed width integers for CSS length type #174

Closed rodarima closed 10 hours ago

rodarima commented 4 months ago

The current type for a CSS "length" like 80px or 20% is represented internally as a single int type, which doesn't have a fixed length. In C and C++, there is only a guarantee that an int will hold at least 16 bits, but the current assumption is that it will hold at least 32 bits.

| <------   integer value   ------> |

+---+ - - - +---+---+- - - - - -+---+---+---+---+
|          integer part             |   type    |
+---+ - - - +---+---+- - - - - -+---+---+---+---+
| integer part  | decimal fraction  |   type    |
+---+ - - - +---+---+- - - - - -+---+---+---+---+
 n-1          15  14              3   2  1   0

| <------ fixed point value ------> |

Furthermore, as we introduce more length types we are going to run out of length bits for the value itself. A simple option could be to define the length as something like this:

typedef struct CssLength {
    CssLengthType type;
    union {
        int i;
        float f;
    };
} CssLength;

Which will duplicate the size required to store lengths to 64 bits, as the type will likely use 32 bits with padding and another 32 bits for the integer/float value (on 64 bits machines). It has the benefit that on 16 bits machines, the size will halve to 32 bits, as all members will reduce their size.

kalvdans commented 3 months ago

As a first step maybe use int32_t for the type to make sure we get 32 bits even on 16-bit platforms?

rodarima commented 10 hours ago

Merged in #264