linear-gradient: Remove the hack using NUMBER

Instead, add a "side" member to the struct that keeps sides explicitly.
This commit is contained in:
Benjamin Otte 2016-02-12 04:09:44 +01:00
parent 0645425046
commit 7b301fcece
2 changed files with 32 additions and 34 deletions

View File

@ -142,12 +142,10 @@ gtk_css_image_linear_draw (GtkCssImage *image,
double offset;
int i, last;
if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
if (linear->side)
{
guint side = _gtk_css_number_value_get (linear->angle, 100);
/* special casing the regular cases here so we don't get rounding errors */
switch (side)
switch (linear->side)
{
case 1 << GTK_CSS_RIGHT:
angle = 90;
@ -162,8 +160,8 @@ gtk_css_image_linear_draw (GtkCssImage *image,
angle = 180;
break;
default:
angle = atan2 (side & 1 << GTK_CSS_TOP ? -width : width,
side & 1 << GTK_CSS_LEFT ? -height : height);
angle = atan2 (linear->side & 1 << GTK_CSS_TOP ? -width : width,
linear->side & 1 << GTK_CSS_LEFT ? -height : height);
angle = 180 * angle / G_PI + 90;
break;
}
@ -258,58 +256,54 @@ gtk_css_image_linear_parse (GtkCssImage *image,
if (_gtk_css_parser_try (parser, "to", TRUE))
{
guint side = 0;
for (i = 0; i < 2; i++)
{
if (_gtk_css_parser_try (parser, "left", TRUE))
{
if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
if (linear->side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
{
_gtk_css_parser_error (parser, "Expected 'top', 'bottom' or comma");
return FALSE;
}
side |= (1 << GTK_CSS_LEFT);
linear->side |= (1 << GTK_CSS_LEFT);
}
else if (_gtk_css_parser_try (parser, "right", TRUE))
{
if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
if (linear->side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
{
_gtk_css_parser_error (parser, "Expected 'top', 'bottom' or comma");
return FALSE;
}
side |= (1 << GTK_CSS_RIGHT);
linear->side |= (1 << GTK_CSS_RIGHT);
}
else if (_gtk_css_parser_try (parser, "top", TRUE))
{
if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
if (linear->side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
{
_gtk_css_parser_error (parser, "Expected 'left', 'right' or comma");
return FALSE;
}
side |= (1 << GTK_CSS_TOP);
linear->side |= (1 << GTK_CSS_TOP);
}
else if (_gtk_css_parser_try (parser, "bottom", TRUE))
{
if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
if (linear->side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
{
_gtk_css_parser_error (parser, "Expected 'left', 'right' or comma");
return FALSE;
}
side |= (1 << GTK_CSS_BOTTOM);
linear->side |= (1 << GTK_CSS_BOTTOM);
}
else
break;
}
if (side == 0)
if (linear->side == 0)
{
_gtk_css_parser_error (parser, "Expected side that gradient should go to");
return FALSE;
}
linear->angle = _gtk_css_number_value_new (side, GTK_CSS_NUMBER);
if (!_gtk_css_parser_try (parser, ",", TRUE))
{
_gtk_css_parser_error (parser, "Expected a comma");
@ -329,7 +323,7 @@ gtk_css_image_linear_parse (GtkCssImage *image,
}
}
else
linear->angle = _gtk_css_number_value_new (1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
linear->side = 1 << GTK_CSS_BOTTOM;
do {
GtkCssImageLinearColorStop stop;
@ -379,22 +373,20 @@ gtk_css_image_linear_print (GtkCssImage *image,
else
g_string_append (string, "linear-gradient(");
if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
if (linear->side)
{
guint side = _gtk_css_number_value_get (linear->angle, 100);
if (side != (1 << GTK_CSS_BOTTOM))
if (linear->side != (1 << GTK_CSS_BOTTOM))
{
g_string_append (string, "to");
if (side & (1 << GTK_CSS_TOP))
if (linear->side & (1 << GTK_CSS_TOP))
g_string_append (string, " top");
else if (side & (1 << GTK_CSS_BOTTOM))
else if (linear->side & (1 << GTK_CSS_BOTTOM))
g_string_append (string, " bottom");
if (side & (1 << GTK_CSS_LEFT))
if (linear->side & (1 << GTK_CSS_LEFT))
g_string_append (string, " left");
else if (side & (1 << GTK_CSS_RIGHT))
else if (linear->side & (1 << GTK_CSS_RIGHT))
g_string_append (string, " right");
g_string_append (string, ", ");
@ -440,8 +432,10 @@ gtk_css_image_linear_compute (GtkCssImage *image,
copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
copy->repeating = linear->repeating;
copy->side = linear->side;
copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, style, parent_style);
if (linear->angle)
copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, style, parent_style);
g_array_set_size (copy->stops, linear->stops->len);
for (i = 0; i < linear->stops->len; i++)
@ -492,11 +486,12 @@ gtk_css_image_linear_transition (GtkCssImage *start_image,
result = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
result->repeating = start->repeating;
if (_gtk_css_number_value_get_unit (start->angle) == GTK_CSS_NUMBER ||
_gtk_css_number_value_get_unit (end->angle) == GTK_CSS_NUMBER)
if (start->side != end->side)
goto fail;
result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
result->side = start->side;
if (result->side == 0)
result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
if (result->angle == NULL)
goto fail;
@ -554,7 +549,8 @@ gtk_css_image_linear_equal (GtkCssImage *image1,
guint i;
if (linear1->repeating != linear2->repeating ||
!_gtk_css_value_equal (linear1->angle, linear2->angle) ||
linear1->side != linear2->side ||
(linear1->side == 0 && !_gtk_css_value_equal (linear1->angle, linear2->angle)) ||
linear1->stops->len != linear2->stops->len)
return FALSE;
@ -584,6 +580,7 @@ gtk_css_image_linear_dispose (GObject *object)
linear->stops = NULL;
}
linear->side = 0;
if (linear->angle)
{
_gtk_css_value_unref (linear->angle);

View File

@ -45,7 +45,8 @@ struct _GtkCssImageLinear
{
GtkCssImage parent;
GtkCssValue *angle; /* warning: We use GTK_CSS_NUMBER as an enum for the corners */
guint side; /* side the gradient should go to or 0 for angle */
GtkCssValue *angle;
GArray *stops;
guint repeating :1;
};