cssvalue: Add enum values for the pango enums

And fix the parser to conform to the CSS spec while at it.
This commit is contained in:
Benjamin Otte 2012-03-28 09:04:54 +02:00
parent b65d17dda8
commit d63b11e56b
6 changed files with 241 additions and 67 deletions

View File

@ -102,3 +102,157 @@ _gtk_css_border_style_value_get (const GtkCssValue *value)
return value->value;
}
/* PangoStyle */
static const GtkCssValueClass GTK_CSS_VALUE_FONT_STYLE = {
gtk_css_value_enum_free,
gtk_css_value_enum_equal,
gtk_css_value_enum_print
};
static GtkCssValue font_style_values[] = {
{ &GTK_CSS_VALUE_FONT_STYLE, 1, PANGO_STYLE_NORMAL, "normal" },
{ &GTK_CSS_VALUE_FONT_STYLE, 1, PANGO_STYLE_OBLIQUE, "oblique" },
{ &GTK_CSS_VALUE_FONT_STYLE, 1, PANGO_STYLE_ITALIC, "italic" }
};
GtkCssValue *
_gtk_css_font_style_value_new (PangoStyle font_style)
{
g_return_val_if_fail (font_style < G_N_ELEMENTS (font_style_values), NULL);
return _gtk_css_value_ref (&font_style_values[font_style]);
}
GtkCssValue *
_gtk_css_font_style_value_try_parse (GtkCssParser *parser)
{
guint i;
g_return_val_if_fail (parser != NULL, NULL);
for (i = 0; i < G_N_ELEMENTS (font_style_values); i++)
{
if (_gtk_css_parser_try (parser, font_style_values[i].name, TRUE))
return _gtk_css_value_ref (&font_style_values[i]);
}
return NULL;
}
PangoStyle
_gtk_css_font_style_value_get (const GtkCssValue *value)
{
g_return_val_if_fail (value->class == &GTK_CSS_VALUE_FONT_STYLE, PANGO_STYLE_NORMAL);
return value->value;
}
/* PangoVariant */
static const GtkCssValueClass GTK_CSS_VALUE_FONT_VARIANT = {
gtk_css_value_enum_free,
gtk_css_value_enum_equal,
gtk_css_value_enum_print
};
static GtkCssValue font_variant_values[] = {
{ &GTK_CSS_VALUE_FONT_VARIANT, 1, PANGO_VARIANT_NORMAL, "normal" },
{ &GTK_CSS_VALUE_FONT_VARIANT, 1, PANGO_VARIANT_SMALL_CAPS, "small-caps" }
};
GtkCssValue *
_gtk_css_font_variant_value_new (PangoVariant font_variant)
{
g_return_val_if_fail (font_variant < G_N_ELEMENTS (font_variant_values), NULL);
return _gtk_css_value_ref (&font_variant_values[font_variant]);
}
GtkCssValue *
_gtk_css_font_variant_value_try_parse (GtkCssParser *parser)
{
guint i;
g_return_val_if_fail (parser != NULL, NULL);
for (i = 0; i < G_N_ELEMENTS (font_variant_values); i++)
{
if (_gtk_css_parser_try (parser, font_variant_values[i].name, TRUE))
return _gtk_css_value_ref (&font_variant_values[i]);
}
return NULL;
}
PangoVariant
_gtk_css_font_variant_value_get (const GtkCssValue *value)
{
g_return_val_if_fail (value->class == &GTK_CSS_VALUE_FONT_VARIANT, PANGO_VARIANT_NORMAL);
return value->value;
}
/* PangoWeight */
static const GtkCssValueClass GTK_CSS_VALUE_FONT_WEIGHT = {
gtk_css_value_enum_free,
gtk_css_value_enum_equal,
gtk_css_value_enum_print
};
static GtkCssValue font_weight_values[] = {
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_THIN, "100" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_ULTRALIGHT, "200" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_LIGHT, "300" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_NORMAL, "normal" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_MEDIUM, "500" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_SEMIBOLD, "600" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_BOLD, "bold" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_ULTRABOLD, "800" },
{ &GTK_CSS_VALUE_FONT_WEIGHT, 1, PANGO_WEIGHT_HEAVY, "900" }
};
GtkCssValue *
_gtk_css_font_weight_value_new (PangoWeight font_weight)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (font_weight_values); i++)
{
if (font_weight_values[i].value == font_weight)
return _gtk_css_value_ref (&font_weight_values[i]);
}
g_return_val_if_reached (NULL);
}
GtkCssValue *
_gtk_css_font_weight_value_try_parse (GtkCssParser *parser)
{
guint i;
g_return_val_if_fail (parser != NULL, NULL);
for (i = 0; i < G_N_ELEMENTS (font_weight_values); i++)
{
if (_gtk_css_parser_try (parser, font_weight_values[i].name, TRUE))
return _gtk_css_value_ref (&font_weight_values[i]);
}
/* special cases go here */
if (_gtk_css_parser_try (parser, "400", TRUE))
return _gtk_css_value_ref (&font_weight_values[3]);
if (_gtk_css_parser_try (parser, "700", TRUE))
return _gtk_css_value_ref (&font_weight_values[6]);
return NULL;
}
PangoWeight
_gtk_css_font_weight_value_get (const GtkCssValue *value)
{
g_return_val_if_fail (value->class == &GTK_CSS_VALUE_FONT_WEIGHT, PANGO_WEIGHT_NORMAL);
return value->value;
}

View File

@ -30,6 +30,18 @@ GtkCssValue * _gtk_css_border_style_value_new (GtkBorderStyle border
GtkCssValue * _gtk_css_border_style_value_try_parse (GtkCssParser *parser);
GtkBorderStyle _gtk_css_border_style_value_get (const GtkCssValue *value);
GtkCssValue * _gtk_css_font_style_value_new (PangoStyle style);
GtkCssValue * _gtk_css_font_style_value_try_parse (GtkCssParser *parser);
PangoStyle _gtk_css_font_style_value_get (const GtkCssValue *value);
GtkCssValue * _gtk_css_font_variant_value_new (PangoVariant variant);
GtkCssValue * _gtk_css_font_variant_value_try_parse (GtkCssParser *parser);
PangoVariant _gtk_css_font_variant_value_get (const GtkCssValue *value);
GtkCssValue * _gtk_css_font_weight_value_new (PangoWeight weight);
GtkCssValue * _gtk_css_font_weight_value_try_parse (GtkCssParser *parser);
PangoWeight _gtk_css_font_weight_value_get (const GtkCssValue *value);
G_END_DECLS

View File

@ -438,18 +438,15 @@ parse_font (GtkCssShorthandProperty *shorthand,
}
if (mask & PANGO_FONT_MASK_STYLE)
{
values[1] = _gtk_css_value_new_from_enum (PANGO_TYPE_STYLE,
pango_font_description_get_style (desc));
values[1] = _gtk_css_font_style_value_new (pango_font_description_get_style (desc));
}
if (mask & PANGO_FONT_MASK_VARIANT)
{
values[2] = _gtk_css_value_new_from_enum (PANGO_TYPE_VARIANT,
pango_font_description_get_variant (desc));
values[2] = _gtk_css_font_variant_value_new (pango_font_description_get_variant (desc));
}
if (mask & PANGO_FONT_MASK_WEIGHT)
{
values[3] = _gtk_css_value_new_from_enum (PANGO_TYPE_WEIGHT,
pango_font_description_get_weight (desc));
values[3] = _gtk_css_font_weight_value_new (pango_font_description_get_weight (desc));
}
if (mask & PANGO_FONT_MASK_SIZE)
{
@ -748,15 +745,15 @@ pack_font_description (GtkCssShorthandProperty *shorthand,
v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data);
if (v)
pango_font_description_set_style (description, _gtk_css_value_get_pango_style (v));
pango_font_description_set_style (description, _gtk_css_font_style_value_get (v));
v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data);
if (v)
pango_font_description_set_variant (description, _gtk_css_value_get_pango_variant (v));
pango_font_description_set_variant (description, _gtk_css_font_variant_value_get (v));
v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data);
if (v)
pango_font_description_set_weight (description, _gtk_css_value_get_pango_weight (v));
pango_font_description_set_weight (description, _gtk_css_font_weight_value_get (v));
g_value_init (value, PANGO_TYPE_FONT_DESCRIPTION);
g_value_take_boxed (value, description);

View File

@ -301,15 +301,28 @@ parse_pango_style (GtkCssStyleProperty *property,
GtkCssParser *parser,
GFile *base)
{
int value;
GtkCssValue *value = _gtk_css_font_style_value_try_parse (parser);
if (!_gtk_css_parser_try_enum (parser, PANGO_TYPE_STYLE, &value))
{
_gtk_css_parser_error (parser, "unknown value for property");
return NULL;
}
if (value == NULL)
_gtk_css_parser_error (parser, "unknown value for property");
return _gtk_css_value_new_from_enum (PANGO_TYPE_STYLE, value);
return value;
}
static void
query_pango_style (GtkCssStyleProperty *property,
const GtkCssValue *css_value,
GValue *value)
{
g_value_init (value, PANGO_TYPE_STYLE);
g_value_set_enum (value, _gtk_css_font_style_value_get (css_value));
}
static GtkCssValue *
assign_pango_style (GtkCssStyleProperty *property,
const GValue *value)
{
return _gtk_css_font_style_value_new (g_value_get_enum (value));
}
static GtkCssValue *
@ -317,15 +330,28 @@ parse_pango_weight (GtkCssStyleProperty *property,
GtkCssParser *parser,
GFile *base)
{
int value;
GtkCssValue *value = _gtk_css_font_weight_value_try_parse (parser);
if (!_gtk_css_parser_try_enum (parser, PANGO_TYPE_WEIGHT, &value))
{
_gtk_css_parser_error (parser, "unknown value for property");
return NULL;
}
if (value == NULL)
_gtk_css_parser_error (parser, "unknown value for property");
return _gtk_css_value_new_from_enum (PANGO_TYPE_WEIGHT, value);
return value;
}
static void
query_pango_weight (GtkCssStyleProperty *property,
const GtkCssValue *css_value,
GValue *value)
{
g_value_init (value, PANGO_TYPE_WEIGHT);
g_value_set_enum (value, _gtk_css_font_weight_value_get (css_value));
}
static GtkCssValue *
assign_pango_weight (GtkCssStyleProperty *property,
const GValue *value)
{
return _gtk_css_font_weight_value_new (g_value_get_enum (value));
}
static GtkCssValue *
@ -333,15 +359,28 @@ parse_pango_variant (GtkCssStyleProperty *property,
GtkCssParser *parser,
GFile *base)
{
int value;
GtkCssValue *value = _gtk_css_font_variant_value_try_parse (parser);
if (!_gtk_css_parser_try_enum (parser, PANGO_TYPE_VARIANT, &value))
{
_gtk_css_parser_error (parser, "unknown value for property");
return NULL;
}
if (value == NULL)
_gtk_css_parser_error (parser, "unknown value for property");
return _gtk_css_value_new_from_enum (PANGO_TYPE_VARIANT, value);
return value;
}
static void
query_pango_variant (GtkCssStyleProperty *property,
const GtkCssValue *css_value,
GValue *value)
{
g_value_init (value, PANGO_TYPE_VARIANT);
g_value_set_enum (value, _gtk_css_font_variant_value_get (css_value));
}
static GtkCssValue *
assign_pango_variant (GtkCssStyleProperty *property,
const GValue *value)
{
return _gtk_css_font_variant_value_new (g_value_get_enum (value));
}
static GtkCssValue *
@ -1222,34 +1261,30 @@ _gtk_css_style_property_init_properties (void)
parse_pango_style,
NULL,
NULL,
query_simple,
assign_simple,
query_pango_style,
assign_pango_style,
NULL,
_gtk_css_value_new_from_enum (PANGO_TYPE_STYLE,
PANGO_STYLE_NORMAL));
_gtk_css_font_style_value_new (PANGO_STYLE_NORMAL));
gtk_css_style_property_register ("font-variant",
PANGO_TYPE_VARIANT,
GTK_STYLE_PROPERTY_INHERIT,
parse_pango_variant,
NULL,
NULL,
query_simple,
assign_simple,
query_pango_variant,
assign_pango_variant,
NULL,
_gtk_css_value_new_from_enum (PANGO_TYPE_VARIANT,
PANGO_VARIANT_NORMAL));
/* xxx: need to parse this properly, ie parse the numbers */
_gtk_css_font_variant_value_new (PANGO_VARIANT_NORMAL));
gtk_css_style_property_register ("font-weight",
PANGO_TYPE_WEIGHT,
GTK_STYLE_PROPERTY_INHERIT,
parse_pango_weight,
NULL,
NULL,
query_simple,
assign_simple,
query_pango_weight,
assign_pango_weight,
NULL,
_gtk_css_value_new_from_enum (PANGO_TYPE_WEIGHT,
PANGO_WEIGHT_NORMAL));
_gtk_css_font_weight_value_new (PANGO_WEIGHT_NORMAL));
gtk_css_style_property_register ("text-shadow",
G_TYPE_NONE,

View File

@ -605,27 +605,6 @@ _gtk_css_value_get_border_corner_radius (const GtkCssValue *value)
return value->u.ptr;
}
PangoStyle
_gtk_css_value_get_pango_style (const GtkCssValue *value)
{
g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_STYLE), 0);
return value->u.gint;
}
PangoVariant
_gtk_css_value_get_pango_variant (const GtkCssValue *value)
{
g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_VARIANT), 0);
return value->u.gint;
}
PangoWeight
_gtk_css_value_get_pango_weight (const GtkCssValue *value)
{
g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_WEIGHT), 0);
return value->u.gint;
}
GtkGradient *
_gtk_css_value_get_gradient (const GtkCssValue *value)
{

View File

@ -108,9 +108,6 @@ const GtkCssBackgroundSize *_gtk_css_value_get_background_size (const
const GtkCssBackgroundPosition *_gtk_css_value_get_background_position (const GtkCssValue *value);
const GtkCssBorderCornerRadius *_gtk_css_value_get_border_corner_radius (const GtkCssValue *value);
const GtkCssBorderImageRepeat *_gtk_css_value_get_border_image_repeat (const GtkCssValue *value);
PangoStyle _gtk_css_value_get_pango_style (const GtkCssValue *value);
PangoVariant _gtk_css_value_get_pango_variant (const GtkCssValue *value);
PangoWeight _gtk_css_value_get_pango_weight (const GtkCssValue *value);
GtkGradient *_gtk_css_value_get_gradient (const GtkCssValue *value);
G_END_DECLS