2012-01-16 17:28:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-16 17:28:58 +00:00
|
|
|
*
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcssimagelinearprivate.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2012-11-08 16:25:26 +00:00
|
|
|
#include "gtkcsscolorvalueprivate.h"
|
2012-04-04 16:23:49 +00:00
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2020-01-10 12:02:33 +00:00
|
|
|
#include "gtkcsscolorvalueprivate.h"
|
2012-01-16 17:28:58 +00:00
|
|
|
#include "gtkcssprovider.h"
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkCssImageLinear, _gtk_css_image_linear, GTK_TYPE_CSS_IMAGE)
|
|
|
|
|
|
|
|
static void
|
2019-12-30 08:51:49 +00:00
|
|
|
gtk_css_image_linear_get_repeating_start_end (GtkCssImageLinear *linear,
|
|
|
|
double length,
|
|
|
|
double *start,
|
|
|
|
double *end)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
const GtkCssImageLinearColorStop *stop;
|
2012-01-16 17:28:58 +00:00
|
|
|
double pos;
|
|
|
|
guint i;
|
2012-10-06 19:54:10 +00:00
|
|
|
|
2019-12-30 08:51:49 +00:00
|
|
|
g_assert (linear->repeating);
|
2012-10-06 19:54:10 +00:00
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
stop = &linear->color_stops[0];
|
2019-12-30 08:51:49 +00:00
|
|
|
if (stop->offset == NULL)
|
|
|
|
*start = 0;
|
|
|
|
else
|
|
|
|
*start = _gtk_css_number_value_get (stop->offset, length) / length;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2019-12-30 08:51:49 +00:00
|
|
|
*end = *start;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
for (i = 0; i < linear->n_stops; i++)
|
2012-10-06 19:54:10 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
stop = &linear->color_stops[i];
|
2019-12-30 08:51:49 +00:00
|
|
|
|
|
|
|
if (stop->offset == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pos = _gtk_css_number_value_get (stop->offset, length) / length;
|
|
|
|
|
|
|
|
*end = MAX (pos, *end);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2019-12-30 08:51:49 +00:00
|
|
|
|
|
|
|
if (stop->offset == NULL)
|
|
|
|
*end = MAX (*end, 1.0);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_linear_compute_start_point (double angle_in_degrees,
|
|
|
|
double width,
|
|
|
|
double height,
|
2019-12-30 08:46:15 +00:00
|
|
|
double *out_x,
|
|
|
|
double *out_y,
|
|
|
|
double *out_length)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
double angle, c, slope, perpendicular;
|
2019-12-30 08:46:15 +00:00
|
|
|
double x, y, length;
|
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
angle = fmod (angle_in_degrees, 360);
|
|
|
|
if (angle < 0)
|
|
|
|
angle += 360;
|
|
|
|
|
|
|
|
if (angle == 0)
|
|
|
|
{
|
2019-12-30 08:46:15 +00:00
|
|
|
*out_x = 0;
|
|
|
|
*out_y = -height;
|
|
|
|
*out_length = height;
|
2012-01-16 17:28:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (angle == 90)
|
|
|
|
{
|
2019-12-30 08:46:15 +00:00
|
|
|
*out_x = width;
|
|
|
|
*out_y = 0;
|
|
|
|
*out_length = width;
|
2012-01-16 17:28:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (angle == 180)
|
|
|
|
{
|
2019-12-30 08:46:15 +00:00
|
|
|
*out_x = 0;
|
|
|
|
*out_y = height;
|
|
|
|
*out_length = height;
|
2012-01-16 17:28:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (angle == 270)
|
|
|
|
{
|
2019-12-30 08:46:15 +00:00
|
|
|
*out_x = -width;
|
|
|
|
*out_y = 0;
|
|
|
|
*out_length = width;
|
2012-01-16 17:28:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The tan() is confusing because the angle is clockwise
|
|
|
|
* from 'to top' */
|
|
|
|
perpendicular = tan (angle * G_PI / 180);
|
|
|
|
slope = -1 / perpendicular;
|
|
|
|
|
|
|
|
if (angle > 180)
|
|
|
|
width = - width;
|
|
|
|
if (angle < 90 || angle > 270)
|
|
|
|
height = - height;
|
2019-12-30 08:46:15 +00:00
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
/* Compute c (of y = mx + c) of perpendicular */
|
|
|
|
c = height - perpendicular * width;
|
|
|
|
|
2019-12-30 08:46:15 +00:00
|
|
|
x = c / (slope - perpendicular);
|
|
|
|
y = perpendicular * x + c;
|
|
|
|
|
|
|
|
length = sqrt (x * x + y * y);
|
|
|
|
|
|
|
|
*out_x = x;
|
|
|
|
*out_y = y;
|
|
|
|
*out_length = length;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2019-12-18 08:48:25 +00:00
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
static void
|
2016-12-15 04:42:31 +00:00
|
|
|
gtk_css_image_linear_snapshot (GtkCssImage *image,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
double width,
|
|
|
|
double height)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
|
2016-12-21 03:48:56 +00:00
|
|
|
GskColorStop *stops;
|
2022-11-18 03:24:18 +00:00
|
|
|
double angle; /* actual angle of the gradient line in degrees */
|
2012-01-16 17:28:58 +00:00
|
|
|
double x, y; /* coordinates of start point */
|
|
|
|
double length; /* distance in pixels for 100% */
|
|
|
|
double start, end; /* position of first/last point on gradient line - with gradient line being [0, 1] */
|
|
|
|
double offset;
|
|
|
|
int i, last;
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2015-01-08 18:30:19 +00:00
|
|
|
/* special casing the regular cases here so we don't get rounding errors */
|
2016-02-12 03:09:44 +00:00
|
|
|
switch (linear->side)
|
2015-01-08 18:30:19 +00:00
|
|
|
{
|
|
|
|
case 1 << GTK_CSS_RIGHT:
|
|
|
|
angle = 90;
|
|
|
|
break;
|
|
|
|
case 1 << GTK_CSS_LEFT:
|
|
|
|
angle = 270;
|
|
|
|
break;
|
|
|
|
case 1 << GTK_CSS_TOP:
|
|
|
|
angle = 0;
|
|
|
|
break;
|
|
|
|
case 1 << GTK_CSS_BOTTOM:
|
|
|
|
angle = 180;
|
|
|
|
break;
|
|
|
|
default:
|
2016-02-12 03:09:44 +00:00
|
|
|
angle = atan2 (linear->side & 1 << GTK_CSS_TOP ? -width : width,
|
|
|
|
linear->side & 1 << GTK_CSS_LEFT ? -height : height);
|
2015-01-08 18:30:19 +00:00
|
|
|
angle = 180 * angle / G_PI + 90;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-08 18:30:19 +00:00
|
|
|
angle = _gtk_css_number_value_get (linear->angle, 100);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 18:30:19 +00:00
|
|
|
gtk_css_image_linear_compute_start_point (angle,
|
|
|
|
width, height,
|
2019-12-30 08:46:15 +00:00
|
|
|
&x, &y,
|
|
|
|
&length);
|
2015-01-08 18:30:19 +00:00
|
|
|
|
2019-12-30 08:51:49 +00:00
|
|
|
if (linear->repeating)
|
2016-12-26 16:04:56 +00:00
|
|
|
{
|
2019-12-30 08:51:49 +00:00
|
|
|
gtk_css_image_linear_get_repeating_start_end (linear, length, &start, &end);
|
2016-12-26 16:04:56 +00:00
|
|
|
|
2019-12-30 08:51:49 +00:00
|
|
|
if (start == end)
|
|
|
|
{
|
|
|
|
/* repeating gradients with all color stops sharing the same offset
|
|
|
|
* get the color of the last color stop */
|
2020-01-11 08:05:07 +00:00
|
|
|
const GtkCssImageLinearColorStop *stop = &linear->color_stops[linear->n_stops - 1];
|
2019-12-30 08:51:49 +00:00
|
|
|
|
|
|
|
gtk_snapshot_append_color (snapshot,
|
2020-01-10 12:02:33 +00:00
|
|
|
gtk_css_color_value_get_rgba (stop->color),
|
2019-12-30 08:51:49 +00:00
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start = 0;
|
|
|
|
end = 1;
|
2016-12-26 16:04:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
offset = start;
|
|
|
|
last = -1;
|
2020-01-11 08:05:07 +00:00
|
|
|
stops = g_newa (GskColorStop, linear->n_stops);
|
2016-12-21 03:48:56 +00:00
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
for (i = 0; i < linear->n_stops; i++)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
|
2012-01-16 17:28:58 +00:00
|
|
|
double pos, step;
|
|
|
|
|
2012-04-04 16:23:49 +00:00
|
|
|
if (stop->offset == NULL)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
pos = 0.0;
|
2020-01-11 08:05:07 +00:00
|
|
|
else if (i + 1 == linear->n_stops)
|
2012-01-16 17:28:58 +00:00
|
|
|
pos = 1.0;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2019-03-29 04:01:33 +00:00
|
|
|
{
|
|
|
|
pos = _gtk_css_number_value_get (stop->offset, length) / length;
|
|
|
|
pos = CLAMP (pos, 0.0, 1.0);
|
|
|
|
}
|
2012-01-16 17:28:58 +00:00
|
|
|
|
|
|
|
pos = MAX (pos, offset);
|
|
|
|
step = (pos - offset) / (i - last);
|
|
|
|
for (last = last + 1; last <= i; last++)
|
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
stop = &linear->color_stops[last];
|
2012-01-16 17:28:58 +00:00
|
|
|
|
|
|
|
offset += step;
|
|
|
|
|
2016-12-15 04:42:31 +00:00
|
|
|
stops[last].offset = (offset - start) / (end - start);
|
2020-01-10 12:02:33 +00:00
|
|
|
stops[last].color = *gtk_css_color_value_get_rgba (stop->color);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset = pos;
|
|
|
|
last = i;
|
|
|
|
}
|
|
|
|
|
2016-12-15 04:42:31 +00:00
|
|
|
if (linear->repeating)
|
|
|
|
{
|
2017-12-13 08:05:49 +00:00
|
|
|
gtk_snapshot_append_repeating_linear_gradient (
|
|
|
|
snapshot,
|
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height),
|
|
|
|
&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)),
|
2016-12-15 04:42:31 +00:00
|
|
|
stops,
|
2020-01-11 08:05:07 +00:00
|
|
|
linear->n_stops);
|
2016-12-15 04:42:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-13 08:05:49 +00:00
|
|
|
gtk_snapshot_append_linear_gradient (
|
|
|
|
snapshot,
|
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height),
|
|
|
|
&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)),
|
2016-12-15 04:42:31 +00:00
|
|
|
stops,
|
2020-01-11 08:05:07 +00:00
|
|
|
linear->n_stops);
|
2017-01-11 09:08:58 +00:00
|
|
|
}
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
static guint
|
|
|
|
gtk_css_image_linear_parse_color_stop (GtkCssImageLinear *self,
|
2020-01-11 08:05:07 +00:00
|
|
|
GtkCssParser *parser,
|
|
|
|
GArray *stop_array)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-03-31 11:39:58 +00:00
|
|
|
GtkCssImageLinearColorStop stop;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
stop.color = _gtk_css_color_value_parse (parser);
|
|
|
|
if (stop.color == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (gtk_css_number_value_can_parse (parser))
|
|
|
|
{
|
|
|
|
stop.offset = _gtk_css_number_value_parse (parser,
|
|
|
|
GTK_CSS_PARSE_PERCENT
|
|
|
|
| GTK_CSS_PARSE_LENGTH);
|
|
|
|
if (stop.offset == NULL)
|
|
|
|
{
|
|
|
|
_gtk_css_value_unref (stop.color);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 10:44:07 +00:00
|
|
|
else
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-03-31 11:39:58 +00:00
|
|
|
stop.offset = NULL;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
g_array_append_val (stop_array, stop);
|
2019-03-31 11:39:58 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_css_image_linear_parse_first_arg (GtkCssImageLinear *linear,
|
2020-01-11 08:05:07 +00:00
|
|
|
GtkCssParser *parser,
|
|
|
|
GArray *stop_array)
|
2019-03-31 11:39:58 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2019-03-24 13:03:52 +00:00
|
|
|
if (gtk_css_parser_try_ident (parser, "to"))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2019-03-24 13:03:52 +00:00
|
|
|
if (gtk_css_parser_try_ident (parser, "left"))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected 'top', 'bottom' or comma");
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2016-02-12 03:09:44 +00:00
|
|
|
linear->side |= (1 << GTK_CSS_LEFT);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2019-03-24 13:03:52 +00:00
|
|
|
else if (gtk_css_parser_try_ident (parser, "right"))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected 'top', 'bottom' or comma");
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2016-02-12 03:09:44 +00:00
|
|
|
linear->side |= (1 << GTK_CSS_RIGHT);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2019-03-24 13:03:52 +00:00
|
|
|
else if (gtk_css_parser_try_ident (parser, "top"))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected 'left', 'right' or comma");
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2016-02-12 03:09:44 +00:00
|
|
|
linear->side |= (1 << GTK_CSS_TOP);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2019-03-24 13:03:52 +00:00
|
|
|
else if (gtk_css_parser_try_ident (parser, "bottom"))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected 'left', 'right' or comma");
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2016-02-12 03:09:44 +00:00
|
|
|
linear->side |= (1 << GTK_CSS_BOTTOM);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side == 0)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected side that gradient should go to");
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
return 1;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2016-02-12 05:45:06 +00:00
|
|
|
else if (gtk_css_number_value_can_parse (parser))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2012-04-04 16:23:49 +00:00
|
|
|
linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
|
|
|
|
if (linear->angle == NULL)
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2020-01-11 08:05:07 +00:00
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
return 1;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
else
|
2019-03-31 11:39:58 +00:00
|
|
|
{
|
|
|
|
linear->side = 1 << GTK_CSS_BOTTOM;
|
2020-01-11 08:05:07 +00:00
|
|
|
if (!gtk_css_image_linear_parse_color_stop (linear, parser, stop_array))
|
2019-03-31 11:39:58 +00:00
|
|
|
return 0;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkCssImageLinear *self;
|
|
|
|
GArray *stop_array;
|
|
|
|
} ParseData;
|
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
static guint
|
|
|
|
gtk_css_image_linear_parse_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
2020-01-11 08:05:07 +00:00
|
|
|
gpointer user_data)
|
2019-03-31 11:39:58 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
ParseData *parse_data = user_data;
|
|
|
|
GtkCssImageLinear *self = parse_data->self;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
if (arg == 0)
|
2020-01-11 08:05:07 +00:00
|
|
|
return gtk_css_image_linear_parse_first_arg (self, parser, parse_data->stop_array);
|
2019-03-31 11:39:58 +00:00
|
|
|
else
|
2020-01-11 08:05:07 +00:00
|
|
|
return gtk_css_image_linear_parse_color_stop (self, parser, parse_data->stop_array);
|
2019-03-31 11:39:58 +00:00
|
|
|
}
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_image_linear_parse (GtkCssImage *image,
|
|
|
|
GtkCssParser *parser)
|
|
|
|
{
|
|
|
|
GtkCssImageLinear *self = GTK_CSS_IMAGE_LINEAR (image);
|
2020-01-11 08:05:07 +00:00
|
|
|
ParseData parse_data;
|
|
|
|
gboolean success;
|
2016-04-18 18:25:11 +00:00
|
|
|
|
2019-03-31 11:39:58 +00:00
|
|
|
if (gtk_css_parser_has_function (parser, "repeating-linear-gradient"))
|
|
|
|
self->repeating = TRUE;
|
|
|
|
else if (gtk_css_parser_has_function (parser, "linear-gradient"))
|
|
|
|
self->repeating = FALSE;
|
|
|
|
else
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Not a linear gradient");
|
2012-01-16 17:28:58 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
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;
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_linear_print (GtkCssImage *image,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
|
|
|
|
guint i;
|
|
|
|
|
2012-01-17 10:44:07 +00:00
|
|
|
if (linear->repeating)
|
|
|
|
g_string_append (string, "repeating-linear-gradient(");
|
|
|
|
else
|
|
|
|
g_string_append (string, "linear-gradient(");
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side != (1 << GTK_CSS_BOTTOM))
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
g_string_append (string, "to");
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side & (1 << GTK_CSS_TOP))
|
2012-01-16 17:28:58 +00:00
|
|
|
g_string_append (string, " top");
|
2016-02-12 03:09:44 +00:00
|
|
|
else if (linear->side & (1 << GTK_CSS_BOTTOM))
|
2012-01-16 17:28:58 +00:00
|
|
|
g_string_append (string, " bottom");
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->side & (1 << GTK_CSS_LEFT))
|
2012-01-16 17:28:58 +00:00
|
|
|
g_string_append (string, " left");
|
2016-02-12 03:09:44 +00:00
|
|
|
else if (linear->side & (1 << GTK_CSS_RIGHT))
|
2012-01-16 17:28:58 +00:00
|
|
|
g_string_append (string, " right");
|
|
|
|
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-04 16:23:49 +00:00
|
|
|
_gtk_css_value_print (linear->angle, string);
|
2012-01-16 17:28:58 +00:00
|
|
|
g_string_append (string, ", ");
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
for (i = 0; i < linear->n_stops; i++)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
|
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
if (i > 0)
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
|
2012-04-04 16:23:49 +00:00
|
|
|
_gtk_css_value_print (stop->color, string);
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2012-04-04 16:23:49 +00:00
|
|
|
if (stop->offset)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
g_string_append (string, " ");
|
2012-04-04 16:23:49 +00:00
|
|
|
_gtk_css_value_print (stop->offset, string);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 08:05:07 +00:00
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssImage *
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_css_image_linear_compute (GtkCssImage *image,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProvider *provider,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssStyle *parent_style)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
|
|
|
GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
|
|
|
|
GtkCssImageLinear *copy;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
|
2012-01-17 10:44:07 +00:00
|
|
|
copy->repeating = linear->repeating;
|
2016-02-12 03:09:44 +00:00
|
|
|
copy->side = linear->side;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (linear->angle)
|
|
|
|
copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, style, parent_style);
|
2020-01-11 08:05:07 +00:00
|
|
|
|
|
|
|
copy->n_stops = linear->n_stops;
|
|
|
|
copy->color_stops = g_malloc (sizeof (GtkCssImageLinearColorStop) * copy->n_stops);
|
|
|
|
for (i = 0; i < linear->n_stops; i++)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
|
|
|
|
GtkCssImageLinearColorStop *scopy = ©->color_stops[i];
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2015-02-14 01:27:39 +00:00
|
|
|
scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, style, parent_style);
|
2020-01-11 08:05:07 +00:00
|
|
|
|
2012-04-04 16:23:49 +00:00
|
|
|
if (stop->offset)
|
2012-08-28 13:29:56 +00:00
|
|
|
{
|
2015-02-14 01:27:39 +00:00
|
|
|
scopy->offset = _gtk_css_value_compute (stop->offset, property_id, provider, style, parent_style);
|
2012-08-28 13:29:56 +00:00
|
|
|
}
|
2012-04-04 16:23:49 +00:00
|
|
|
else
|
2012-08-28 13:29:56 +00:00
|
|
|
{
|
|
|
|
scopy->offset = NULL;
|
|
|
|
}
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_CSS_IMAGE (copy);
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:30:02 +00:00
|
|
|
static GtkCssImage *
|
|
|
|
gtk_css_image_linear_transition (GtkCssImage *start_image,
|
|
|
|
GtkCssImage *end_image,
|
|
|
|
guint property_id,
|
|
|
|
double progress)
|
|
|
|
{
|
|
|
|
GtkCssImageLinear *start, *end, *result;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
start = GTK_CSS_IMAGE_LINEAR (start_image);
|
|
|
|
|
|
|
|
if (end_image == NULL)
|
2013-05-28 13:14:55 +00:00
|
|
|
return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
|
2012-10-02 17:30:02 +00:00
|
|
|
|
|
|
|
if (!GTK_IS_CSS_IMAGE_LINEAR (end_image))
|
|
|
|
return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
|
|
|
|
|
|
|
|
end = GTK_CSS_IMAGE_LINEAR (end_image);
|
|
|
|
|
|
|
|
if ((start->repeating != end->repeating)
|
2020-01-11 08:05:07 +00:00
|
|
|
|| (start->n_stops != end->n_stops))
|
2012-10-02 17:30:02 +00:00
|
|
|
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->repeating = start->repeating;
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
if (start->side != end->side)
|
2016-01-07 21:10:10 +00:00
|
|
|
goto fail;
|
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
result->side = start->side;
|
|
|
|
if (result->side == 0)
|
|
|
|
result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
|
2012-10-02 17:30:02 +00:00
|
|
|
if (result->angle == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
/* Maximum amountof stops */
|
|
|
|
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];
|
2012-10-02 17:30:02 +00:00
|
|
|
|
|
|
|
if ((start_stop->offset != NULL) != (end_stop->offset != NULL))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (start_stop->offset == NULL)
|
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
stop->offset = NULL;
|
2012-10-02 17:30:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
stop->offset = _gtk_css_value_transition (start_stop->offset,
|
|
|
|
end_stop->offset,
|
|
|
|
property_id,
|
|
|
|
progress);
|
|
|
|
if (stop->offset == NULL)
|
2012-10-02 17:30:02 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
stop->color = _gtk_css_value_transition (start_stop->color,
|
|
|
|
end_stop->color,
|
|
|
|
property_id,
|
|
|
|
progress);
|
|
|
|
if (stop->color == NULL)
|
2012-10-02 17:30:02 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
if (stop->offset)
|
|
|
|
_gtk_css_value_unref (stop->offset);
|
2012-10-02 17:30:02 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
result->n_stops ++;
|
2012-10-02 17:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_CSS_IMAGE (result);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
g_object_unref (result);
|
|
|
|
return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
|
|
|
|
}
|
|
|
|
|
2012-10-31 23:22:39 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_image_linear_equal (GtkCssImage *image1,
|
|
|
|
GtkCssImage *image2)
|
|
|
|
{
|
2019-08-17 16:55:35 +00:00
|
|
|
GtkCssImageLinear *linear1 = (GtkCssImageLinear *) image1;
|
|
|
|
GtkCssImageLinear *linear2 = (GtkCssImageLinear *) image2;
|
2012-10-31 23:22:39 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (linear1->repeating != linear2->repeating ||
|
2016-02-12 03:09:44 +00:00
|
|
|
linear1->side != linear2->side ||
|
|
|
|
(linear1->side == 0 && !_gtk_css_value_equal (linear1->angle, linear2->angle)) ||
|
2020-01-11 08:05:07 +00:00
|
|
|
linear1->n_stops != linear2->n_stops)
|
2012-10-31 23:22:39 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
for (i = 0; i < linear1->n_stops; i++)
|
2012-10-31 23:22:39 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
const GtkCssImageLinearColorStop *stop1 = &linear1->color_stops[i];
|
|
|
|
const GtkCssImageLinearColorStop *stop2 = &linear2->color_stops[i];
|
2012-10-31 23:22:39 +00:00
|
|
|
|
|
|
|
if (!_gtk_css_value_equal0 (stop1->offset, stop2->offset) ||
|
|
|
|
!_gtk_css_value_equal (stop1->color, stop2->color))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
static void
|
|
|
|
gtk_css_image_linear_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object);
|
2020-01-11 08:05:07 +00:00
|
|
|
guint i;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2020-01-11 08:05:07 +00:00
|
|
|
for (i = 0; i < linear->n_stops; i ++)
|
2012-01-16 17:28:58 +00:00
|
|
|
{
|
2020-01-11 08:05:07 +00:00
|
|
|
GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
|
|
|
|
|
|
|
|
_gtk_css_value_unref (stop->color);
|
|
|
|
if (stop->offset)
|
|
|
|
_gtk_css_value_unref (stop->offset);
|
2012-01-16 17:28:58 +00:00
|
|
|
}
|
2020-01-11 08:05:07 +00:00
|
|
|
g_free (linear->color_stops);
|
2012-01-16 17:28:58 +00:00
|
|
|
|
2016-02-12 03:09:44 +00:00
|
|
|
linear->side = 0;
|
2012-04-04 16:23:49 +00:00
|
|
|
if (linear->angle)
|
|
|
|
{
|
|
|
|
_gtk_css_value_unref (linear->angle);
|
|
|
|
linear->angle = NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
G_OBJECT_CLASS (_gtk_css_image_linear_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:38:49 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_image_linear_is_computed (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
|
|
|
|
guint i;
|
|
|
|
gboolean computed = TRUE;
|
|
|
|
|
|
|
|
computed = !linear->angle || gtk_css_value_is_computed (linear->angle);
|
|
|
|
|
|
|
|
for (i = 0; i < linear->n_stops; i ++)
|
|
|
|
{
|
|
|
|
const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
|
|
|
|
|
|
|
|
if (stop->offset && !gtk_css_value_is_computed (stop->offset))
|
|
|
|
{
|
|
|
|
computed = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_css_value_is_computed (stop->color))
|
|
|
|
{
|
|
|
|
computed = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return computed;
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:28:58 +00:00
|
|
|
static void
|
|
|
|
_gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
|
|
|
|
{
|
|
|
|
GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2016-12-15 04:42:31 +00:00
|
|
|
image_class->snapshot = gtk_css_image_linear_snapshot;
|
2012-01-16 17:28:58 +00:00
|
|
|
image_class->parse = gtk_css_image_linear_parse;
|
|
|
|
image_class->print = gtk_css_image_linear_print;
|
|
|
|
image_class->compute = gtk_css_image_linear_compute;
|
2012-10-31 23:22:39 +00:00
|
|
|
image_class->equal = gtk_css_image_linear_equal;
|
2012-10-02 17:30:02 +00:00
|
|
|
image_class->transition = gtk_css_image_linear_transition;
|
2020-01-11 13:38:49 +00:00
|
|
|
image_class->is_computed = gtk_css_image_linear_is_computed;
|
2012-01-16 17:28:58 +00:00
|
|
|
|
|
|
|
object_class->dispose = gtk_css_image_linear_dispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_css_image_linear_init (GtkCssImageLinear *linear)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|