cssfiltervalue: Use gtk_css_parser_consume_function()

This commit is contained in:
Benjamin Otte 2019-03-25 00:40:59 +01:00
parent aa50e92c65
commit 38227dc972

View File

@ -729,96 +729,46 @@ gtk_css_filter_value_is_none (const GtkCssValue *value)
return value->n_filters == 0; return value->n_filters == 0;
} }
static gboolean static guint
gtk_css_filter_parse (GtkCssFilter *filter, gtk_css_filter_parse_number (GtkCssParser *parser,
GtkCssParser *parser) guint n,
gpointer data)
{ {
if (_gtk_css_parser_try (parser, "brightness(", TRUE)) GtkCssValue **values = data;
{
filter->type = GTK_CSS_FILTER_BRIGHTNESS;
filter->brightness.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); values[n] = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT);
if (filter->brightness.value == NULL) if (values[n] == NULL)
return FALSE; return 0;
}
else if (_gtk_css_parser_try (parser, "contrast(", TRUE))
{
filter->type = GTK_CSS_FILTER_CONTRAST;
filter->contrast.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); return 1;
if (filter->contrast.value == NULL) }
return FALSE;
}
else if (_gtk_css_parser_try (parser, "grayscale(", TRUE))
{
filter->type = GTK_CSS_FILTER_GRAYSCALE;
filter->grayscale.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); static guint
if (filter->grayscale.value == NULL) gtk_css_filter_parse_length (GtkCssParser *parser,
return FALSE; guint n,
} gpointer data)
else if (_gtk_css_parser_try (parser, "hue-rotate(", TRUE)) {
{ GtkCssValue **values = data;
filter->type = GTK_CSS_FILTER_HUE_ROTATE;
filter->hue_rotate.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE); values[n] = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_LENGTH);
if (filter->hue_rotate.value == NULL) if (values[n] == NULL)
return FALSE; return 0;
}
else if (_gtk_css_parser_try (parser, "invert(", TRUE))
{
filter->type = GTK_CSS_FILTER_INVERT;
filter->invert.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); return 1;
if (filter->invert.value == NULL) }
return FALSE;
}
else if (_gtk_css_parser_try (parser, "opacity(", TRUE))
{
filter->type = GTK_CSS_FILTER_OPACITY;
filter->opacity.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); static guint
if (filter->opacity.value == NULL) gtk_css_filter_parse_angle (GtkCssParser *parser,
return FALSE; guint n,
} gpointer data)
else if (_gtk_css_parser_try (parser, "saturate(", TRUE)) {
{ GtkCssValue **values = data;
filter->type = GTK_CSS_FILTER_SATURATE;
filter->saturate.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); values[n] = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
if (filter->saturate.value == NULL) if (values[n] == NULL)
return FALSE; return 0;
}
else if (_gtk_css_parser_try (parser, "sepia(", TRUE))
{
filter->type = GTK_CSS_FILTER_SEPIA;
filter->sepia.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_PARSE_PERCENT); return 1;
if (filter->sepia.value == NULL)
return FALSE;
}
else if (_gtk_css_parser_try (parser, "blur(", TRUE))
{
filter->type = GTK_CSS_FILTER_BLUR;
filter->blur.value = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_LENGTH);
if (filter->blur.value == NULL)
return FALSE;
}
else
{
_gtk_css_parser_error (parser, "unknown syntax for filter");
return FALSE;
}
if (!_gtk_css_parser_try (parser, ")", TRUE))
{
gtk_css_filter_clear (filter);
_gtk_css_parser_error (parser, "Expected closing ')'");
return FALSE;
}
return TRUE;
} }
GtkCssValue * GtkCssValue *
@ -828,25 +778,91 @@ gtk_css_filter_value_parse (GtkCssParser *parser)
GArray *array; GArray *array;
guint i; guint i;
if (_gtk_css_parser_try (parser, "none", TRUE)) if (gtk_css_parser_try_ident (parser, "none"))
return gtk_css_filter_value_new_none (); return gtk_css_filter_value_new_none ();
array = g_array_new (FALSE, FALSE, sizeof (GtkCssFilter)); array = g_array_new (FALSE, FALSE, sizeof (GtkCssFilter));
do { while (TRUE)
{
GtkCssFilter filter; GtkCssFilter filter;
if (!gtk_css_filter_parse (&filter, parser)) if (gtk_css_parser_has_function (parser, "blur"))
{ {
for (i = 0; i < array->len; i++) if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_length, &filter.blur.value))
goto fail;
filter.type = GTK_CSS_FILTER_BLUR;
}
else if (gtk_css_parser_has_function (parser, "brightness"))
{ {
gtk_css_filter_clear (&g_array_index (array, GtkCssFilter, i)); if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.brightness.value))
goto fail;
filter.type = GTK_CSS_FILTER_BRIGHTNESS;
} }
g_array_free (array, TRUE); else if (gtk_css_parser_has_function (parser, "contrast"))
return NULL; {
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.contrast.value))
goto fail;
filter.type = GTK_CSS_FILTER_CONTRAST;
} }
else if (gtk_css_parser_has_function (parser, "grayscale"))
{
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.grayscale.value))
goto fail;
filter.type = GTK_CSS_FILTER_GRAYSCALE;
}
else if (gtk_css_parser_has_function (parser, "hue-rotate"))
{
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_angle, &filter.blur.value))
goto fail;
filter.type = GTK_CSS_FILTER_HUE_ROTATE;
}
else if (gtk_css_parser_has_function (parser, "invert"))
{
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.invert.value))
goto fail;
filter.type = GTK_CSS_FILTER_INVERT;
}
else if (gtk_css_parser_has_function (parser, "opacity"))
{
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.opacity.value))
goto fail;
filter.type = GTK_CSS_FILTER_OPACITY;
}
else if (gtk_css_parser_has_function (parser, "saturate"))
{
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.saturate.value))
goto fail;
filter.type = GTK_CSS_FILTER_SATURATE;
}
else if (gtk_css_parser_has_function (parser, "sepia"))
{
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_filter_parse_number, &filter.sepia.value))
goto fail;
filter.type = GTK_CSS_FILTER_SEPIA;
}
else
{
break;
}
g_array_append_val (array, filter); g_array_append_val (array, filter);
} while (!gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_SEMICOLON)); }
if (array->len == 0)
{
_gtk_css_parser_error (parser, "Expected a filter");
goto fail;
}
value = gtk_css_filter_value_alloc (array->len); value = gtk_css_filter_value_alloc (array->len);
memcpy (value->filters, array->data, sizeof (GtkCssFilter) * array->len); memcpy (value->filters, array->data, sizeof (GtkCssFilter) * array->len);
@ -854,6 +870,14 @@ gtk_css_filter_value_parse (GtkCssParser *parser)
g_array_free (array, TRUE); g_array_free (array, TRUE);
return value; return value;
fail:
for (i = 0; i < array->len; i++)
{
gtk_css_filter_clear (&g_array_index (array, GtkCssFilter, i));
}
g_array_free (array, TRUE);
return NULL;
} }
void void