cssimagelinear: Use count+array for the color stops, not GArray

This makse sense but will also make later changes to GtkCssImageLinear
simpler.
This commit is contained in:
Timm Bäder 2020-01-11 09:05:07 +01:00
parent 3401150cca
commit 00aa4f0597
2 changed files with 100 additions and 86 deletions

View File

@ -36,13 +36,13 @@ gtk_css_image_linear_get_repeating_start_end (GtkCssImageLinear *linear,
double *start, double *start,
double *end) double *end)
{ {
GtkCssImageLinearColorStop *stop; const GtkCssImageLinearColorStop *stop;
double pos; double pos;
guint i; guint i;
g_assert (linear->repeating); g_assert (linear->repeating);
stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0); stop = &linear->color_stops[0];
if (stop->offset == NULL) if (stop->offset == NULL)
*start = 0; *start = 0;
else else
@ -50,9 +50,9 @@ gtk_css_image_linear_get_repeating_start_end (GtkCssImageLinear *linear,
*end = *start; *end = *start;
for (i = 0; i < linear->stops->len; i++) for (i = 0; i < linear->n_stops; i++)
{ {
stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i); stop = &linear->color_stops[i];
if (stop->offset == NULL) if (stop->offset == NULL)
continue; continue;
@ -190,8 +190,7 @@ gtk_css_image_linear_snapshot (GtkCssImage *image,
{ {
/* repeating gradients with all color stops sharing the same offset /* repeating gradients with all color stops sharing the same offset
* get the color of the last color stop */ * get the color of the last color stop */
GtkCssImageLinearColorStop *stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, const GtkCssImageLinearColorStop *stop = &linear->color_stops[linear->n_stops - 1];
linear->stops->len - 1);
gtk_snapshot_append_color (snapshot, gtk_snapshot_append_color (snapshot,
gtk_css_color_value_get_rgba (stop->color), gtk_css_color_value_get_rgba (stop->color),
@ -207,20 +206,18 @@ gtk_css_image_linear_snapshot (GtkCssImage *image,
offset = start; offset = start;
last = -1; last = -1;
stops = g_newa (GskColorStop, linear->stops->len); stops = g_newa (GskColorStop, linear->n_stops);
for (i = 0; i < linear->stops->len; i++) for (i = 0; i < linear->n_stops; i++)
{ {
GtkCssImageLinearColorStop *stop; const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
double pos, step; double pos, step;
stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
if (stop->offset == NULL) if (stop->offset == NULL)
{ {
if (i == 0) if (i == 0)
pos = 0.0; pos = 0.0;
else if (i + 1 == linear->stops->len) else if (i + 1 == linear->n_stops)
pos = 1.0; pos = 1.0;
else else
continue; continue;
@ -235,7 +232,7 @@ gtk_css_image_linear_snapshot (GtkCssImage *image,
step = (pos - offset) / (i - last); step = (pos - offset) / (i - last);
for (last = last + 1; last <= i; last++) for (last = last + 1; last <= i; last++)
{ {
stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last); stop = &linear->color_stops[last];
offset += step; offset += step;
@ -255,7 +252,7 @@ gtk_css_image_linear_snapshot (GtkCssImage *image,
&GRAPHENE_POINT_INIT (width / 2 + x * (start - 0.5), height / 2 + y * (start - 0.5)), &GRAPHENE_POINT_INIT (width / 2 + x * (start - 0.5), height / 2 + y * (start - 0.5)),
&GRAPHENE_POINT_INIT (width / 2 + x * (end - 0.5), height / 2 + y * (end - 0.5)), &GRAPHENE_POINT_INIT (width / 2 + x * (end - 0.5), height / 2 + y * (end - 0.5)),
stops, stops,
linear->stops->len); linear->n_stops);
} }
else else
{ {
@ -265,13 +262,14 @@ gtk_css_image_linear_snapshot (GtkCssImage *image,
&GRAPHENE_POINT_INIT (width / 2 + x * (start - 0.5), height / 2 + y * (start - 0.5)), &GRAPHENE_POINT_INIT (width / 2 + x * (start - 0.5), height / 2 + y * (start - 0.5)),
&GRAPHENE_POINT_INIT (width / 2 + x * (end - 0.5), height / 2 + y * (end - 0.5)), &GRAPHENE_POINT_INIT (width / 2 + x * (end - 0.5), height / 2 + y * (end - 0.5)),
stops, stops,
linear->stops->len); linear->n_stops);
} }
} }
static guint static guint
gtk_css_image_linear_parse_color_stop (GtkCssImageLinear *self, gtk_css_image_linear_parse_color_stop (GtkCssImageLinear *self,
GtkCssParser *parser) GtkCssParser *parser,
GArray *stop_array)
{ {
GtkCssImageLinearColorStop stop; GtkCssImageLinearColorStop stop;
@ -295,14 +293,15 @@ gtk_css_image_linear_parse_color_stop (GtkCssImageLinear *self,
stop.offset = NULL; stop.offset = NULL;
} }
g_array_append_val (self->stops, stop); g_array_append_val (stop_array, stop);
return 1; return 1;
} }
static guint static guint
gtk_css_image_linear_parse_first_arg (GtkCssImageLinear *linear, gtk_css_image_linear_parse_first_arg (GtkCssImageLinear *linear,
GtkCssParser *parser) GtkCssParser *parser,
GArray *stop_array)
{ {
guint i; guint i;
@ -363,31 +362,37 @@ gtk_css_image_linear_parse_first_arg (GtkCssImageLinear *linear,
linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE); linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
if (linear->angle == NULL) if (linear->angle == NULL)
return 0; return 0;
return 1; return 1;
} }
else else
{ {
linear->side = 1 << GTK_CSS_BOTTOM; linear->side = 1 << GTK_CSS_BOTTOM;
if (!gtk_css_image_linear_parse_color_stop (linear, parser)) if (!gtk_css_image_linear_parse_color_stop (linear, parser, stop_array))
return 0; return 0;
return 2; return 2;
} }
} }
typedef struct
{
GtkCssImageLinear *self;
GArray *stop_array;
} ParseData;
static guint static guint
gtk_css_image_linear_parse_arg (GtkCssParser *parser, gtk_css_image_linear_parse_arg (GtkCssParser *parser,
guint arg, guint arg,
gpointer data) gpointer user_data)
{ {
GtkCssImageLinear *self = data; ParseData *parse_data = user_data;
GtkCssImageLinear *self = parse_data->self;
if (arg == 0) if (arg == 0)
return gtk_css_image_linear_parse_first_arg (self, parser); return gtk_css_image_linear_parse_first_arg (self, parser, parse_data->stop_array);
else else
return gtk_css_image_linear_parse_color_stop (self, parser); return gtk_css_image_linear_parse_color_stop (self, parser, parse_data->stop_array);
} }
static gboolean static gboolean
@ -395,6 +400,8 @@ gtk_css_image_linear_parse (GtkCssImage *image,
GtkCssParser *parser) GtkCssParser *parser)
{ {
GtkCssImageLinear *self = GTK_CSS_IMAGE_LINEAR (image); GtkCssImageLinear *self = GTK_CSS_IMAGE_LINEAR (image);
ParseData parse_data;
gboolean success;
if (gtk_css_parser_has_function (parser, "repeating-linear-gradient")) if (gtk_css_parser_has_function (parser, "repeating-linear-gradient"))
self->repeating = TRUE; self->repeating = TRUE;
@ -406,7 +413,22 @@ gtk_css_image_linear_parse (GtkCssImage *image,
return FALSE; return FALSE;
} }
return gtk_css_parser_consume_function (parser, 3, G_MAXUINT, gtk_css_image_linear_parse_arg, self); parse_data.self = self;
parse_data.stop_array = g_array_new (TRUE, FALSE, sizeof (GtkCssImageLinearColorStop));
success = gtk_css_parser_consume_function (parser, 3, G_MAXUINT, gtk_css_image_linear_parse_arg, &parse_data);
if (!success)
{
g_array_free (parse_data.stop_array, TRUE);
}
else
{
self->n_stops = parse_data.stop_array->len;
self->color_stops = (GtkCssImageLinearColorStop *)g_array_free (parse_data.stop_array, FALSE);
}
return success;
} }
static void static void
@ -446,15 +468,13 @@ gtk_css_image_linear_print (GtkCssImage *image,
g_string_append (string, ", "); g_string_append (string, ", ");
} }
for (i = 0; i < linear->stops->len; i++) for (i = 0; i < linear->n_stops; i++)
{ {
GtkCssImageLinearColorStop *stop; const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
if (i > 0) if (i > 0)
g_string_append (string, ", "); g_string_append (string, ", ");
stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
_gtk_css_value_print (stop->color, string); _gtk_css_value_print (stop->color, string);
if (stop->offset) if (stop->offset)
@ -463,7 +483,7 @@ gtk_css_image_linear_print (GtkCssImage *image,
_gtk_css_value_print (stop->offset, string); _gtk_css_value_print (stop->offset, string);
} }
} }
g_string_append (string, ")"); g_string_append (string, ")");
} }
@ -484,17 +504,16 @@ gtk_css_image_linear_compute (GtkCssImage *image,
if (linear->angle) if (linear->angle)
copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, style, parent_style); 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++)
{
GtkCssImageLinearColorStop *stop, *scopy;
stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i); copy->n_stops = linear->n_stops;
scopy = &g_array_index (copy->stops, GtkCssImageLinearColorStop, i); copy->color_stops = g_malloc (sizeof (GtkCssImageLinearColorStop) * copy->n_stops);
for (i = 0; i < linear->n_stops; i++)
{
const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
GtkCssImageLinearColorStop *scopy = &copy->color_stops[i];
scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, style, parent_style); scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, style, parent_style);
if (stop->offset) if (stop->offset)
{ {
scopy->offset = _gtk_css_value_compute (stop->offset, property_id, provider, style, parent_style); scopy->offset = _gtk_css_value_compute (stop->offset, property_id, provider, style, parent_style);
@ -528,7 +547,7 @@ gtk_css_image_linear_transition (GtkCssImage *start_image,
end = GTK_CSS_IMAGE_LINEAR (end_image); end = GTK_CSS_IMAGE_LINEAR (end_image);
if ((start->repeating != end->repeating) if ((start->repeating != end->repeating)
|| (start->stops->len != end->stops->len)) || (start->n_stops != end->n_stops))
return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress); return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
result = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL); result = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
@ -542,43 +561,45 @@ gtk_css_image_linear_transition (GtkCssImage *start_image,
result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress); result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
if (result->angle == NULL) if (result->angle == NULL)
goto fail; goto fail;
for (i = 0; i < start->stops->len; i++)
{
GtkCssImageLinearColorStop stop, *start_stop, *end_stop;
start_stop = &g_array_index (start->stops, GtkCssImageLinearColorStop, i); /* Maximum amountof stops */
end_stop = &g_array_index (end->stops, GtkCssImageLinearColorStop, i); result->color_stops = g_malloc (sizeof (GtkCssImageLinearColorStop) * start->n_stops);
result->n_stops = 0;
for (i = 0; i < start->n_stops; i++)
{
const GtkCssImageLinearColorStop *start_stop = &start->color_stops[i];
const GtkCssImageLinearColorStop *end_stop = &end->color_stops[i];
GtkCssImageLinearColorStop *stop = &result->color_stops[i];
if ((start_stop->offset != NULL) != (end_stop->offset != NULL)) if ((start_stop->offset != NULL) != (end_stop->offset != NULL))
goto fail; goto fail;
if (start_stop->offset == NULL) if (start_stop->offset == NULL)
{ {
stop.offset = NULL; stop->offset = NULL;
} }
else else
{ {
stop.offset = _gtk_css_value_transition (start_stop->offset, stop->offset = _gtk_css_value_transition (start_stop->offset,
end_stop->offset, end_stop->offset,
property_id, property_id,
progress); progress);
if (stop.offset == NULL) if (stop->offset == NULL)
goto fail; goto fail;
} }
stop.color = _gtk_css_value_transition (start_stop->color, stop->color = _gtk_css_value_transition (start_stop->color,
end_stop->color, end_stop->color,
property_id, property_id,
progress); progress);
if (stop.color == NULL) if (stop->color == NULL)
{ {
if (stop.offset) if (stop->offset)
_gtk_css_value_unref (stop.offset); _gtk_css_value_unref (stop->offset);
goto fail; goto fail;
} }
g_array_append_val (result->stops, stop); result->n_stops ++;
} }
return GTK_CSS_IMAGE (result); return GTK_CSS_IMAGE (result);
@ -599,15 +620,13 @@ gtk_css_image_linear_equal (GtkCssImage *image1,
if (linear1->repeating != linear2->repeating || if (linear1->repeating != linear2->repeating ||
linear1->side != linear2->side || linear1->side != linear2->side ||
(linear1->side == 0 && !_gtk_css_value_equal (linear1->angle, linear2->angle)) || (linear1->side == 0 && !_gtk_css_value_equal (linear1->angle, linear2->angle)) ||
linear1->stops->len != linear2->stops->len) linear1->n_stops != linear2->n_stops)
return FALSE; return FALSE;
for (i = 0; i < linear1->stops->len; i++) for (i = 0; i < linear1->n_stops; i++)
{ {
GtkCssImageLinearColorStop *stop1, *stop2; const GtkCssImageLinearColorStop *stop1 = &linear1->color_stops[i];
const GtkCssImageLinearColorStop *stop2 = &linear2->color_stops[i];
stop1 = &g_array_index (linear1->stops, GtkCssImageLinearColorStop, i);
stop2 = &g_array_index (linear2->stops, GtkCssImageLinearColorStop, i);
if (!_gtk_css_value_equal0 (stop1->offset, stop2->offset) || if (!_gtk_css_value_equal0 (stop1->offset, stop2->offset) ||
!_gtk_css_value_equal (stop1->color, stop2->color)) !_gtk_css_value_equal (stop1->color, stop2->color))
@ -621,12 +640,17 @@ static void
gtk_css_image_linear_dispose (GObject *object) gtk_css_image_linear_dispose (GObject *object)
{ {
GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object); GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object);
guint i;
if (linear->stops) for (i = 0; i < linear->n_stops; i ++)
{ {
g_array_free (linear->stops, TRUE); GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
linear->stops = NULL;
_gtk_css_value_unref (stop->color);
if (stop->offset)
_gtk_css_value_unref (stop->offset);
} }
g_free (linear->color_stops);
linear->side = 0; linear->side = 0;
if (linear->angle) if (linear->angle)
@ -654,20 +678,8 @@ _gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
object_class->dispose = gtk_css_image_linear_dispose; object_class->dispose = gtk_css_image_linear_dispose;
} }
static void
gtk_css_image_clear_color_stop (gpointer color_stop)
{
GtkCssImageLinearColorStop *stop = color_stop;
_gtk_css_value_unref (stop->color);
if (stop->offset)
_gtk_css_value_unref (stop->offset);
}
static void static void
_gtk_css_image_linear_init (GtkCssImageLinear *linear) _gtk_css_image_linear_init (GtkCssImageLinear *linear)
{ {
linear->stops = g_array_new (FALSE, FALSE, sizeof (GtkCssImageLinearColorStop));
g_array_set_clear_func (linear->stops, gtk_css_image_clear_color_stop);
} }

View File

@ -45,10 +45,12 @@ struct _GtkCssImageLinear
{ {
GtkCssImage parent; GtkCssImage parent;
guint side; /* side the gradient should go to or 0 for angle */ guint side; /* side the gradient should go to or 0 for angle */
GtkCssValue *angle;
GArray *stops;
guint repeating :1; guint repeating :1;
GtkCssValue *angle;
guint n_stops;
GtkCssImageLinearColorStop *color_stops;
}; };
struct _GtkCssImageLinearClass struct _GtkCssImageLinearClass