Commit cdd4ed66 authored by Benjamin Otte's avatar Benjamin Otte

styleproperty: Use factored-out gradient parser

parent 2a6c1682
......@@ -30,6 +30,7 @@
#include <cairo-gobject.h>
#include "gtkanimationdescription.h"
#include "gtkcssimagegradientprivate.h"
#include "gtkcssprovider.h"
#include "gtkcsstypesprivate.h"
#include "gtkgradient.h"
......@@ -705,188 +706,10 @@ gradient_value_parse (GtkCssParser *parser,
GValue *value)
{
GtkGradient *gradient;
cairo_pattern_type_t type;
gdouble coords[6];
guint i;
if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
{
_gtk_css_parser_error (parser,
"Expected '-gtk-gradient'");
return FALSE;
}
if (!_gtk_css_parser_try (parser, "(", TRUE))
{
_gtk_css_parser_error (parser,
"Expected '(' after '-gtk-gradient'");
return FALSE;
}
/* Parse gradient type */
if (_gtk_css_parser_try (parser, "linear", TRUE))
type = CAIRO_PATTERN_TYPE_LINEAR;
else if (_gtk_css_parser_try (parser, "radial", TRUE))
type = CAIRO_PATTERN_TYPE_RADIAL;
else
{
_gtk_css_parser_error (parser,
"Gradient type must be 'radial' or 'linear'");
return FALSE;
}
/* Parse start/stop position parameters */
for (i = 0; i < 2; i++)
{
if (! _gtk_css_parser_try (parser, ",", TRUE))
{
_gtk_css_parser_error (parser,
"Expected ','");
return FALSE;
}
if (_gtk_css_parser_try (parser, "left", TRUE))
coords[i * 3] = 0;
else if (_gtk_css_parser_try (parser, "right", TRUE))
coords[i * 3] = 1;
else if (_gtk_css_parser_try (parser, "center", TRUE))
coords[i * 3] = 0.5;
else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
{
_gtk_css_parser_error (parser,
"Expected a valid X coordinate");
return FALSE;
}
if (_gtk_css_parser_try (parser, "top", TRUE))
coords[i * 3 + 1] = 0;
else if (_gtk_css_parser_try (parser, "bottom", TRUE))
coords[i * 3 + 1] = 1;
else if (_gtk_css_parser_try (parser, "center", TRUE))
coords[i * 3 + 1] = 0.5;
else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
{
_gtk_css_parser_error (parser,
"Expected a valid Y coordinate");
return FALSE;
}
if (type == CAIRO_PATTERN_TYPE_RADIAL)
{
/* Parse radius */
if (! _gtk_css_parser_try (parser, ",", TRUE))
{
_gtk_css_parser_error (parser,
"Expected ','");
return FALSE;
}
if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
{
_gtk_css_parser_error (parser,
"Expected a numer for the radius");
return FALSE;
}
}
}
if (type == CAIRO_PATTERN_TYPE_LINEAR)
gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
else
gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
coords[3], coords[4], coords[5]);
while (_gtk_css_parser_try (parser, ",", TRUE))
{
GtkSymbolicColor *color;
gdouble position;
if (_gtk_css_parser_try (parser, "from", TRUE))
{
position = 0;
if (!_gtk_css_parser_try (parser, "(", TRUE))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected '('");
return FALSE;
}
}
else if (_gtk_css_parser_try (parser, "to", TRUE))
{
position = 1;
if (!_gtk_css_parser_try (parser, "(", TRUE))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected '('");
return FALSE;
}
}
else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
{
if (!_gtk_css_parser_try (parser, "(", TRUE))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected '('");
return FALSE;
}
if (!_gtk_css_parser_try_double (parser, &position))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected a valid number");
return FALSE;
}
if (!_gtk_css_parser_try (parser, ",", TRUE))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected a comma");
return FALSE;
}
}
else
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Not a valid color-stop definition");
return FALSE;
}
color = _gtk_css_parser_read_symbolic_color (parser);
if (color == NULL)
{
gtk_gradient_unref (gradient);
return FALSE;
}
gtk_gradient_add_color_stop (gradient, position, color);
gtk_symbolic_color_unref (color);
if (!_gtk_css_parser_try (parser, ")", TRUE))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected ')'");
return FALSE;
}
}
if (!_gtk_css_parser_try (parser, ")", TRUE))
{
gtk_gradient_unref (gradient);
_gtk_css_parser_error (parser,
"Expected ')'");
return FALSE;
}
gradient = _gtk_gradient_parse (parser);
if (gradient == NULL)
return FALSE;
g_value_take_boxed (value, gradient);
return TRUE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment