cssvalue: Parse font sizes properly

As the last CSS property, font-size now is a proper number (when it's
not a keyword).
This commit is contained in:
Benjamin Otte 2012-12-01 16:22:04 +01:00
parent 891a4ab347
commit 5942099f00
4 changed files with 45 additions and 23 deletions

View File

@ -133,8 +133,8 @@ _gtk_css_border_style_value_get (const GtkCssValue *value)
*/
#define DEFAULT_FONT_SIZE 10
static double
get_default_font_size (GtkStyleProviderPrivate *provider)
double
_gtk_css_font_size_get_default (GtkStyleProviderPrivate *provider)
{
GtkSettings *settings;
PangoFontDescription *description;
@ -173,35 +173,35 @@ gtk_css_value_font_size_compute (GtkCssValue *value,
switch (value->value)
{
case GTK_CSS_FONT_SIZE_XX_SMALL:
font_size = get_default_font_size (provider) * 3. / 5;
font_size = _gtk_css_font_size_get_default (provider) * 3. / 5;
break;
case GTK_CSS_FONT_SIZE_X_SMALL:
font_size = get_default_font_size (provider) * 3. / 4;
font_size = _gtk_css_font_size_get_default (provider) * 3. / 4;
break;
case GTK_CSS_FONT_SIZE_SMALL:
font_size = get_default_font_size (provider) * 8. / 9;
font_size = _gtk_css_font_size_get_default (provider) * 8. / 9;
break;
default:
g_assert_not_reached ();
/* fall thru */
case GTK_CSS_FONT_SIZE_MEDIUM:
font_size = get_default_font_size (provider);
font_size = _gtk_css_font_size_get_default (provider);
break;
case GTK_CSS_FONT_SIZE_LARGE:
font_size = get_default_font_size (provider) * 6. / 5;
font_size = _gtk_css_font_size_get_default (provider) * 6. / 5;
break;
case GTK_CSS_FONT_SIZE_X_LARGE:
font_size = get_default_font_size (provider) * 3. / 2;
font_size = _gtk_css_font_size_get_default (provider) * 3. / 2;
break;
case GTK_CSS_FONT_SIZE_XX_LARGE:
font_size = get_default_font_size (provider) * 2;
font_size = _gtk_css_font_size_get_default (provider) * 2;
break;
case GTK_CSS_FONT_SIZE_SMALLER:
*dependencies = GTK_CSS_DEPENDS_ON_PARENT;
if (parent_values)
font_size = _gtk_css_number_value_get (_gtk_css_computed_values_get_value (parent_values, GTK_CSS_PROPERTY_FONT_SIZE), 100);
else
font_size = get_default_font_size (provider);
font_size = _gtk_css_font_size_get_default (provider);
/* XXX: This is what WebKit does... */
font_size *= 1.2;
break;
@ -210,7 +210,7 @@ gtk_css_value_font_size_compute (GtkCssValue *value,
if (parent_values)
font_size = _gtk_css_number_value_get (_gtk_css_computed_values_get_value (parent_values, GTK_CSS_PROPERTY_FONT_SIZE), 100);
else
font_size = get_default_font_size (provider);
font_size = _gtk_css_font_size_get_default (provider);
/* XXX: This is what WebKit does... */
font_size /= 1.2;
break;

View File

@ -34,6 +34,7 @@ GtkBorderStyle _gtk_css_border_style_value_get (const GtkCssValue *value)
GtkCssValue * _gtk_css_font_size_value_new (GtkCssFontSize size);
GtkCssValue * _gtk_css_font_size_value_try_parse (GtkCssParser *parser);
GtkCssFontSize _gtk_css_font_size_value_get (const GtkCssValue *value);
double _gtk_css_font_size_get_default (GtkStyleProviderPrivate *provider);
GtkCssValue * _gtk_css_font_style_value_new (PangoStyle style);
GtkCssValue * _gtk_css_font_style_value_try_parse (GtkCssParser *parser);

View File

@ -20,6 +20,7 @@
#include "gtkcssnumbervalueprivate.h"
#include "gtkcssenumvalueprivate.h"
#include "gtkcssinitialvalueprivate.h"
#include "gtkstylepropertyprivate.h"
struct _GtkCssValue {
@ -34,6 +35,26 @@ gtk_css_value_number_free (GtkCssValue *value)
g_slice_free (GtkCssValue, value);
}
static double
get_base_font_size (guint property_id,
GtkStyleProviderPrivate *provider,
GtkCssComputedValues *values,
GtkCssComputedValues *parent_values,
GtkCssDependencies *dependencies)
{
if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
{
*dependencies = GTK_CSS_DEPENDS_ON_PARENT;
if (parent_values)
return _gtk_css_number_value_get (_gtk_css_computed_values_get_value (parent_values, GTK_CSS_PROPERTY_FONT_SIZE), 100);
else
return _gtk_css_font_size_get_default (provider);
}
*dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
return _gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100);
}
static GtkCssValue *
gtk_css_value_number_compute (GtkCssValue *number,
guint property_id,
@ -48,6 +69,11 @@ gtk_css_value_number_compute (GtkCssValue *number,
g_assert_not_reached();
/* fall through */
case GTK_CSS_PERCENT:
/* percentages for font sizes are computed, other percentages aren't */
if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
return _gtk_css_number_value_new (number->value / 100.0 *
get_base_font_size (property_id, provider, values, parent_values, dependencies),
GTK_CSS_PX);
case GTK_CSS_NUMBER:
case GTK_CSS_PX:
case GTK_CSS_DEG:
@ -73,16 +99,14 @@ gtk_css_value_number_compute (GtkCssValue *number,
GTK_CSS_PX);
break;
case GTK_CSS_EM:
*dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
return _gtk_css_number_value_new (number->value *
_gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100),
get_base_font_size (property_id, provider, values, parent_values, dependencies),
GTK_CSS_PX);
break;
case GTK_CSS_EX:
/* for now we pretend ex is half of em */
*dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
return _gtk_css_number_value_new (number->value * 0.5 *
_gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100),
get_base_font_size (property_id, provider, values, parent_values, dependencies),
GTK_CSS_PX);
case GTK_CSS_RAD:
return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),

View File

@ -649,19 +649,16 @@ font_size_parse (GtkCssStyleProperty *property,
GtkCssParser *parser)
{
GtkCssValue *value;
gdouble d;
value = _gtk_css_font_size_value_try_parse (parser);
if (value)
return value;
if (!_gtk_css_parser_try_double (parser, &d))
{
_gtk_css_parser_error (parser, "Expected a number");
return NULL;
}
return _gtk_css_number_value_new (d, GTK_CSS_PX);
return _gtk_css_number_value_parse (parser,
GTK_CSS_PARSE_LENGTH
| GTK_CSS_PARSE_PERCENT
| GTK_CSS_POSITIVE_ONLY
| GTK_CSS_NUMBER_AS_PIXELS);
}
static GtkCssValue *