More accurate legacy font size handling

svn path=/trunk/netsurf/; revision=12695
This commit is contained in:
John Mark Bell 2011-08-31 21:12:57 +00:00
parent 1cb60828ea
commit 088c91b127
2 changed files with 97 additions and 15 deletions

View File

@ -103,21 +103,6 @@ small { font-size: .83em; }
strike, s { text-decoration: line-through; }
u { text-decoration: underline; }
font[size="1"] { font-size: x-small; }
font[size="2"] { font-size: small; }
font[size="3"] { font-size: medium; }
font[size="4"] { font-size: large; }
font[size="5"] { font-size: x-large; }
font[size="6"] { font-size: xx-large; }
font[size="7"] { font-size: xx-large; }
/* And now, some hackery for relative font size */
font[size="-1"] { font-size: small }
font[size="-2"] { font-size: x-small }
font[size="+1"] { font-size: large }
font[size="+2"] { font-size: x-large }
font[size="+3"] { font-size: xx-large }
font[size="+4"] { font-size: xx-large }
hr { display: block; background-color: #000; height: 1px;
margin: 4px auto; border: 1px #d9d9d9 inset; }
hr[noshade] { background-color: #888; height: 2px; border: none; }

View File

@ -98,6 +98,8 @@ static bool parse_dimension(const char *data, bool strict,
css_fixed *length, css_unit *unit);
static bool parse_number(const char *data, bool non_negative, bool real,
css_fixed *value, size_t *consumed);
static bool parse_font_size(const char *size, uint8_t *val,
css_fixed *len, css_unit *unit);
static css_computed_style *nscss_get_initial_style(nscss_select_ctx *ctx,
css_allocator_fn, void *pw);
@ -1767,6 +1769,27 @@ css_error node_presentational_hint(void *pw, void *node,
xmlFree(align);
return CSS_OK;
} else if (property == CSS_PROP_FONT_SIZE) {
xmlChar *size;
if (strcmp((const char *) n->name, "font") == 0)
size = xmlGetProp(n, (const xmlChar *) "size");
else
size = NULL;
if (size == NULL)
return CSS_PROPERTY_NOT_SET;
if (parse_font_size((const char *) size, &hint->status,
&hint->data.length.value,
&hint->data.length.unit) == false) {
xmlFree(size);
return CSS_PROPERTY_NOT_SET;
}
xmlFree(size);
return CSS_OK;
} else if (property == CSS_PROP_HEIGHT) {
xmlChar *height;
@ -2721,6 +2744,80 @@ bool parse_number(const char *data, bool maybe_negative, bool real,
return true;
}
/**
* Parse a font @size attribute
*
* \param size Data to parse (NUL-terminated)
* \param val Pointer to location to receive enum value
* \param len Pointer to location to receive length
* \param unit Pointer to location to receive unit
* \return True on success, false on failure
*/
bool parse_font_size(const char *size, uint8_t *val,
css_fixed *len, css_unit *unit)
{
static const uint8_t size_map[] = {
CSS_FONT_SIZE_XX_SMALL,
CSS_FONT_SIZE_SMALL,
CSS_FONT_SIZE_MEDIUM,
CSS_FONT_SIZE_LARGE,
CSS_FONT_SIZE_X_LARGE,
CSS_FONT_SIZE_XX_LARGE,
CSS_FONT_SIZE_DIMENSION /* xxx-large (see below) */
};
const char *p = size;
char mode;
int value = 0;
/* Skip whitespace */
while (*p != '\0' && isWhitespace(*p))
p++;
mode = *p;
/* Skip +/- */
if (mode == '+' || mode == '-')
p++;
/* Need at least one digit */
if (*p < '0' || *p > '9') {
return false;
}
/* Consume digits, computing value */
while ('0' <= *p && *p <= '9') {
value = value * 10 + (*p - '0');
p++;
}
/* Resolve relative sizes */
if (mode == '+')
value += 3;
else if (mode == '-')
value = 3 - value;
/* Clamp to range [1,7] */
if (value < 1)
value = 1;
else if (value > 7)
value = 7;
if (value == 7) {
/* Manufacture xxx-large */
*len = FDIV(FMUL(INTTOFIX(3), INTTOFIX(option_font_size)),
F_10);
} else {
/* Len is irrelevant */
*len = 0;
}
*unit = CSS_UNIT_PT;
*val = size_map[value - 1];
return true;
}
/******************************************************************************
* Utility functions *
******************************************************************************/