Skip to content

Commit

Permalink
Define CssLength as struct instead of int
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rodarima committed Oct 5, 2024
1 parent ee32215 commit ac56aa6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 90 deletions.
71 changes: 28 additions & 43 deletions src/css.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/cssparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright 2004 Sebastian Geerken <[email protected]>
* Copyright 2008-2009 Johannes Hofmann <[email protected]>
* Copyright 2024 Rodrigo Arias Mallo <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -988,7 +989,7 @@ bool CssParser::parseValue(CssPropertyName prop,
(type == CSS_TYPE_LENGTH_PERCENTAGE_NUMBER || fval == 0.0))
ret = true;

val->intVal = CSS_CREATE_LENGTH(fval, lentype);
val->lenVal = CSS_CREATE_LENGTH(fval, lentype);
}
break;

Expand Down Expand Up @@ -1091,7 +1092,7 @@ bool CssParser::parseValue(CssPropertyName prop,
// possibilities are tested in parallel.

bool h[2], v[2];
int pos[2];
CssLength pos[2];
h[0] = v[0] = h[1] = v[1] = false;

// First: collect values in pos[0] and pos[1], and determine whether
Expand Down Expand Up @@ -1134,10 +1135,10 @@ bool CssParser::parseValue(CssPropertyName prop,
// We can assume <length> or <percentage> here ...
CssPropertyValue valTmp;
if (parseValue(prop, CSS_TYPE_LENGTH_PERCENTAGE, &valTmp)) {
pos[i] = valTmp.intVal;
pos[i] = valTmp.lenVal;
ret = true;
} else if (parseValue(prop, CSS_TYPE_SIGNED_LENGTH, &valTmp)) {
pos[i] = valTmp.intVal;
pos[i] = valTmp.lenVal;
ret = true;
} else
// ... but something may still fail.
Expand Down
28 changes: 14 additions & 14 deletions src/html.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2205,48 +2205,48 @@ static bool Html_load_image(BrowserWindow *bw, DilloUrl *url,

static void Html_tag_open_img(DilloHtml *html, const char *tag, int tagsize)
{
int space, border;
int border;
const char *attrbuf;

a_Html_common_image_attrs(html, tag, tagsize);

/* Spacing to the left and right */
if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "hspace"))) {
space = strtol(attrbuf, NULL, 10);
int space = strtol(attrbuf, NULL, 10);
if (space > 0) {
space = CSS_CREATE_LENGTH(space, CSS_LENGTH_TYPE_PX);
CssLength len = CSS_CREATE_LENGTH(space, CSS_LENGTH_TYPE_PX);
html->styleEngine->setNonCssHint (CSS_PROPERTY_MARGIN_LEFT,
CSS_TYPE_LENGTH_PERCENTAGE, space);
CSS_TYPE_LENGTH_PERCENTAGE, len);
html->styleEngine->setNonCssHint (CSS_PROPERTY_MARGIN_RIGHT,
CSS_TYPE_LENGTH_PERCENTAGE, space);
CSS_TYPE_LENGTH_PERCENTAGE, len);
}
}

/* Spacing at the top and bottom */
if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "vspace"))) {
space = strtol(attrbuf, NULL, 10);
int space = strtol(attrbuf, NULL, 10);
if (space > 0) {
space = CSS_CREATE_LENGTH(space, CSS_LENGTH_TYPE_PX);
CssLength len = CSS_CREATE_LENGTH(space, CSS_LENGTH_TYPE_PX);
html->styleEngine->setNonCssHint (CSS_PROPERTY_MARGIN_TOP,
CSS_TYPE_LENGTH_PERCENTAGE, space);
CSS_TYPE_LENGTH_PERCENTAGE, len);
html->styleEngine->setNonCssHint (CSS_PROPERTY_MARGIN_BOTTOM,
CSS_TYPE_LENGTH_PERCENTAGE, space);
CSS_TYPE_LENGTH_PERCENTAGE, len);
}
}

/* Border */
if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "border"))) {
border = strtol(attrbuf, NULL, 10);
if (border >= 0) {
border = CSS_CREATE_LENGTH(border, CSS_LENGTH_TYPE_PX);
CssLength b = CSS_CREATE_LENGTH(border, CSS_LENGTH_TYPE_PX);
html->styleEngine->setNonCssHint (CSS_PROPERTY_BORDER_TOP_WIDTH,
CSS_TYPE_LENGTH_PERCENTAGE, border);
CSS_TYPE_LENGTH_PERCENTAGE, b);
html->styleEngine->setNonCssHint (CSS_PROPERTY_BORDER_BOTTOM_WIDTH,
CSS_TYPE_LENGTH_PERCENTAGE, border);
CSS_TYPE_LENGTH_PERCENTAGE, b);
html->styleEngine->setNonCssHint (CSS_PROPERTY_BORDER_LEFT_WIDTH,
CSS_TYPE_LENGTH_PERCENTAGE, border);
CSS_TYPE_LENGTH_PERCENTAGE, b);
html->styleEngine->setNonCssHint (CSS_PROPERTY_BORDER_RIGHT_WIDTH,
CSS_TYPE_LENGTH_PERCENTAGE, border);
CSS_TYPE_LENGTH_PERCENTAGE, b);

html->styleEngine->setNonCssHint (CSS_PROPERTY_BORDER_TOP_STYLE,
CSS_TYPE_ENUM, BORDER_SOLID);
Expand Down
15 changes: 14 additions & 1 deletion src/html_common.hh
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
* File: html_common.hh
*
* Copyright (C) 2008-2016 Jorge Arellano Cid <[email protected]>
* Copyright (C) 2008-2014 Johannes Hofmann <[email protected]>
* Copyright (C) 2024 Rodrigo Arias Mallo <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/

#ifndef __HTML_COMMON_HH__
#define __HTML_COMMON_HH__

Expand Down Expand Up @@ -268,7 +281,7 @@ void a_Html_pop_tag(DilloHtml *html, int TagIdx);
void a_Html_stash_init(DilloHtml *html);
int32_t a_Html_color_parse(DilloHtml *html, const char *str,
int32_t default_color);
dw::core::style::Length a_Html_parse_length (DilloHtml *html,
CssLength a_Html_parse_length (DilloHtml *html,
const char *attr);
void a_Html_tag_set_align_attr(DilloHtml *html, const char *tag, int tagsize);
bool a_Html_tag_set_valign_attr(DilloHtml *html,
Expand Down
Loading

0 comments on commit ac56aa6

Please sign in to comment.