rendernodeparser: Allow single values instead of 4

This allows writing:
  colors: red;
instead of
  colors: red red red red;
to draw a red border.
This commit is contained in:
Benjamin Otte 2019-05-14 00:41:49 +02:00
parent c8fc40e793
commit ed0ecf0ff0
3 changed files with 302 additions and 235 deletions

View File

@ -8,8 +8,8 @@
#include "gdk/gdkrgbaprivate.h"
#include "gdk/gdktextureprivate.h"
#include <gtk/css/gtkcss.h>
#include "gtk/css/gtkcssparserprivate.h"
#include "gtk/css/gtkcssdataurlprivate.h"
#include "gtk/css/gtkcssparserprivate.h"
typedef struct _Declaration Declaration;
@ -318,18 +318,60 @@ clear_stops (gpointer inout_stops)
}
}
static gboolean
parse_float4 (GtkCssParser *parser,
gpointer out_floats)
{
float *floats = (float *) out_floats;
double d[4];
int i;
for (i = 0; i < 4 && !gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_EOF); i ++)
{
if (!gtk_css_parser_consume_number (parser, &d[i]))
return FALSE;
}
if (i == 0)
{
gtk_css_parser_error_syntax (parser, "Expected a color");
return FALSE;
}
for (; i < 4; i++)
{
d[i] = d[(i - 1) >> 1];
}
for (i = 0; i < 4; i++)
{
floats[i] = d[i];
}
return TRUE;
}
static gboolean
parse_colors4 (GtkCssParser *parser,
gpointer out_colors)
{
GdkRGBA *colors = (GdkRGBA *)out_colors;
GdkRGBA colors[4];
int i;
for (i = 0; i < 4; i ++)
for (i = 0; i < 4 && !gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_EOF); i ++)
{
if (!gdk_rgba_parser_parse (parser, &colors[i]))
return FALSE;
}
if (i == 0)
{
gtk_css_parser_error_syntax (parser, "Expected a color");
return FALSE;
}
for (; i < 4; i++)
{
colors[i] = colors[(i - 1) >> 1];
}
memcpy (out_colors, colors, sizeof (GdkRGBA) * 4);
return TRUE;
}
@ -688,17 +730,17 @@ static GskRenderNode *
parse_border_node (GtkCssParser *parser)
{
GskRoundedRect outline = GSK_ROUNDED_RECT_INIT (0, 0, 0, 0);
graphene_rect_t widths = GRAPHENE_RECT_INIT (0, 0, 0, 0);
GdkRGBA colors[4] = { { 0, 0, 0, 0 }, {0, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0 } };
float widths[4] = { 1, 1, 1, 1 };
GdkRGBA colors[4] = { { 0, 0, 0, 1 }, {0, 0, 0, 1 }, {0, 0, 0, 1 }, { 0, 0, 0, 1 } };
const Declaration declarations[] = {
{ "outline", parse_rounded_rect, NULL, &outline },
{ "widths", parse_rect, NULL, &widths },
{ "widths", parse_float4, NULL, &widths },
{ "colors", parse_colors4, NULL, &colors }
};
parse_declarations (parser, declarations, G_N_ELEMENTS(declarations));
return gsk_border_node_new (&outline, (float*)&widths, colors);
return gsk_border_node_new (&outline, widths, colors);
}
static GskRenderNode *
@ -970,7 +1012,7 @@ static GskRenderNode *
parse_blur_node (GtkCssParser *parser)
{
GskRenderNode *child = NULL;
double blur_radius = 0.0;
double blur_radius = 1.0;
const Declaration declarations[] = {
{ "blur", parse_double, NULL, &blur_radius },
{ "child", parse_node, clear_node, &child },
@ -1654,31 +1696,56 @@ render_node_print (Printer *p,
case GSK_BORDER_NODE:
{
const GdkRGBA *colors = gsk_border_node_peek_colors (node);
const float *widths = gsk_border_node_peek_widths (node);
guint i, n;
start_node (p, "border");
_indent (p);
g_string_append (p->str, "colors: ");
append_rgba (p->str, &gsk_border_node_peek_colors (node)[0]);
g_string_append_c (p->str, ' ');
append_rgba (p->str, &gsk_border_node_peek_colors (node)[1]);
g_string_append_c (p->str, ' ');
append_rgba (p->str, &gsk_border_node_peek_colors (node)[2]);
g_string_append_c (p->str, ' ');
append_rgba (p->str, &gsk_border_node_peek_colors (node)[3]);
g_string_append (p->str, ";\n");
if (!gdk_rgba_equal (&colors[3], &colors[1]))
n = 4;
else if (!gdk_rgba_equal (&colors[2], &colors[0]))
n = 3;
else if (!gdk_rgba_equal (&colors[1], &colors[0]))
n = 2;
else
n = 1;
if (n > 0)
{
_indent (p);
g_string_append (p->str, "colors: ");
for (i = 0; i < n; i++)
{
if (i > 0)
g_string_append_c (p->str, ' ');
append_rgba (p->str, &colors[i]);
}
g_string_append (p->str, ";\n");
}
append_rounded_rect_param (p, "outline", gsk_border_node_peek_outline (node));
_indent (p);
g_string_append (p->str, "widths: ");
string_append_double (p->str, gsk_border_node_peek_widths (node)[0]);
g_string_append_c (p->str, ' ');
string_append_double (p->str, gsk_border_node_peek_widths (node)[1]);
g_string_append_c (p->str, ' ');
string_append_double (p->str, gsk_border_node_peek_widths (node)[2]);
g_string_append_c (p->str, ' ');
string_append_double (p->str, gsk_border_node_peek_widths (node)[3]);
g_string_append (p->str, ";\n");
if (widths[3] != widths[1])
n = 4;
else if (widths[2] != widths[0])
n = 3;
else if (widths[1] != widths[0])
n = 2;
else
n = 1;
if (n > 0)
{
_indent (p);
g_string_append (p->str, "widths: ");
for (i = 0; i < n; i++)
{
if (i > 0)
g_string_append_c (p->str, ' ');
string_append_double (p->str, widths[i]);
}
g_string_append (p->str, ";\n");
}
end_node (p);
}

View File

@ -16,9 +16,9 @@ transform {
clip: -1 -1 50 26 / 13;
}
border {
colors: rgb(205,199,194) rgb(205,199,194) rgb(205,199,194) rgb(205,199,194);
colors: rgb(205,199,194);
outline: -1 -1 50 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
container {
container {
@ -57,9 +57,9 @@ transform {
}
}
border {
colors: rgb(191,184,177) rgb(191,184,177) rgb(191,184,177) rgb(191,184,177);
colors: rgb(191,184,177);
outline: -1 -1 26 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
}
}
@ -88,9 +88,9 @@ transform {
clip: -1 -1 50 26 / 13;
}
border {
colors: rgb(24,95,180) rgb(24,95,180) rgb(24,95,180) rgb(24,95,180);
colors: rgb(24,95,180);
outline: -1 -1 50 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
transform {
child: container {
@ -146,9 +146,9 @@ transform {
}
}
border {
colors: rgb(24,95,180) rgb(24,95,180) rgb(24,95,180) rgb(24,95,180);
colors: rgb(24,95,180);
outline: -1 -1 26 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
}
transform: translate(24, 0);
@ -181,9 +181,9 @@ transform {
clip: -1 -1 50 26 / 13;
}
border {
colors: rgb(205,199,194) rgb(205,199,194) rgb(205,199,194) rgb(205,199,194);
colors: rgb(205,199,194);
outline: -1 -1 50 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
container {
rounded-clip {
@ -194,9 +194,9 @@ transform {
clip: -1 -1 26 26 / 13;
}
border {
colors: rgb(205,199,194) rgb(205,199,194) rgb(205,199,194) rgb(205,199,194);
colors: rgb(205,199,194);
outline: -1 -1 26 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
}
}
@ -227,9 +227,9 @@ transform {
clip: -1 -1 50 26 / 13;
}
border {
colors: rgb(205,199,194) rgb(205,199,194) rgb(205,199,194) rgb(205,199,194);
colors: rgb(205,199,194);
outline: -1 -1 50 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
transform {
child: container {
@ -241,9 +241,9 @@ transform {
clip: -1 -1 26 26 / 13;
}
border {
colors: rgb(205,199,194) rgb(205,199,194) rgb(205,199,194) rgb(205,199,194);
colors: rgb(205,199,194);
outline: -1 -1 26 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
}
transform: translate(24, 0);
@ -276,9 +276,9 @@ transform {
clip: -1 -1 50 26 / 13;
}
border {
colors: rgb(205,199,194) rgb(205,199,194) rgb(205,199,194) rgb(205,199,194);
colors: rgb(205,199,194);
outline: -1 -1 50 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
container {
container {
@ -317,9 +317,9 @@ transform {
}
}
border {
colors: rgb(191,184,177) rgb(191,184,177) rgb(191,184,177) rgb(191,184,177);
colors: rgb(191,184,177);
outline: -1 -1 26 26 / 13;
widths: 1 1 1 1;
widths: 1;
}
}
}
@ -371,9 +371,9 @@ transform {
}
}
border {
colors: rgb(191,184,177) rgb(191,184,177) rgb(191,184,177) rgb(191,184,177);
colors: rgb(191,184,177);
outline: -1 -1 16 16 / 3;
widths: 1 1 1 1;
widths: 1;
}
}
transform: translate(1, 6);

File diff suppressed because it is too large Load Diff