css: Fix relative font sizes

This regression was introduced in aeac2b54.

We need percentage values to stay non-computed, since we otherwise
fail to compute relative font sizes properly. But we want percentages
not to stick around in relative colors, so tweak things to be more
aggressive with simplication when creating relative color values.

Update affected tests.

Fixes: #6868
This commit is contained in:
Matthias Clasen 2024-07-25 16:20:51 -06:00
parent 29cb5051d4
commit b3451dda04
3 changed files with 29 additions and 47 deletions

View File

@ -978,7 +978,8 @@ gtk_css_color_value_new_relative (GtkCssValue *origin,
{
if (values[i])
{
if (!gtk_css_value_is_computed (values[i]))
if (!gtk_css_value_is_computed (values[i]) &&
!gtk_css_number_value_has_percent (values[i]))
computed = FALSE;
if (gtk_css_value_contains_current_color (values[i]))
resolved = FALSE;

View File

@ -365,7 +365,6 @@ gtk_css_value_number_compute (GtkCssValue *number,
}
else
{
GtkCssValue *result;
double value = number->dimension.value;
switch (number->dimension.unit)
@ -374,68 +373,50 @@ gtk_css_value_number_compute (GtkCssValue *number,
/* percentages for font sizes are computed, other percentages aren't */
if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
{
result = gtk_css_dimension_value_new (value / 100.0 *
get_base_font_size_px (property_id, provider, style, parent_style),
GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value / 100.0 *
get_base_font_size_px (property_id, provider, style, parent_style),
GTK_CSS_PX);
}
G_GNUC_FALLTHROUGH;
case GTK_CSS_NUMBER:
case GTK_CSS_PX:
case GTK_CSS_DEG:
case GTK_CSS_S:
result = gtk_css_dimension_value_new (value, number->dimension.unit);
break;
return gtk_css_dimension_value_new (value, number->dimension.unit);
case GTK_CSS_PT:
result = gtk_css_dimension_value_new (value * get_dpi (style) / 72.0, GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value * get_dpi (style) / 72.0, GTK_CSS_PX);
case GTK_CSS_PC:
result = gtk_css_dimension_value_new (value * get_dpi (style) / 72.0 * 12.0, GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value * get_dpi (style) / 72.0 * 12.0, GTK_CSS_PX);
case GTK_CSS_IN:
result = gtk_css_dimension_value_new (value * get_dpi (style), GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value * get_dpi (style), GTK_CSS_PX);
case GTK_CSS_CM:
result = gtk_css_dimension_value_new (value * get_dpi (style) * 0.39370078740157477, GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value * get_dpi (style) * 0.39370078740157477, GTK_CSS_PX);
case GTK_CSS_MM:
result = gtk_css_dimension_value_new (value * get_dpi (style) * 0.039370078740157477, GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value * get_dpi (style) * 0.039370078740157477, GTK_CSS_PX);
case GTK_CSS_EM:
result = gtk_css_dimension_value_new (value *
get_base_font_size_px (property_id, provider, style, parent_style),
GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value *
get_base_font_size_px (property_id, provider, style, parent_style),
GTK_CSS_PX);
case GTK_CSS_EX:
/* for now we pretend ex is half of em */
result = gtk_css_dimension_value_new (value * 0.5 *
get_base_font_size_px (property_id, provider, style, parent_style),
GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value * 0.5 *
get_base_font_size_px (property_id, provider, style, parent_style),
GTK_CSS_PX);
case GTK_CSS_REM:
result = gtk_css_dimension_value_new (value *
gtk_css_font_size_get_default_px (provider, style),
GTK_CSS_PX);
break;
return gtk_css_dimension_value_new (value *
gtk_css_font_size_get_default_px (provider, style),
GTK_CSS_PX);
case GTK_CSS_RAD:
result = gtk_css_dimension_value_new (value * 360.0 / (2 * G_PI), GTK_CSS_DEG);
break;
return gtk_css_dimension_value_new (value * 360.0 / (2 * G_PI), GTK_CSS_DEG);
case GTK_CSS_GRAD:
result = gtk_css_dimension_value_new (value * 360.0 / 400.0, GTK_CSS_DEG);
break;
return gtk_css_dimension_value_new (value * 360.0 / 400.0, GTK_CSS_DEG);
case GTK_CSS_TURN:
result = gtk_css_dimension_value_new (value * 360.0, GTK_CSS_DEG);
break;
return gtk_css_dimension_value_new (value * 360.0, GTK_CSS_DEG);
case GTK_CSS_MS:
result = gtk_css_dimension_value_new (value / 1000.0, GTK_CSS_S);
break;
return gtk_css_dimension_value_new (value / 1000.0, GTK_CSS_S);
default:
g_assert_not_reached();
}
result->is_computed = TRUE;
return result;
}
}
@ -720,9 +701,9 @@ gtk_css_dimension_value_new (double value,
{ &GTK_CSS_VALUE_NUMBER, 1, 1, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PX, 64 }} },
};
static GtkCssValue percent_singletons[] = {
{ &GTK_CSS_VALUE_NUMBER, 1, 1, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PERCENT, 0 }} },
{ &GTK_CSS_VALUE_NUMBER, 1, 1, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PERCENT, 50 }} },
{ &GTK_CSS_VALUE_NUMBER, 1, 1, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PERCENT, 100 }} },
{ &GTK_CSS_VALUE_NUMBER, 1, 0, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PERCENT, 0 }} },
{ &GTK_CSS_VALUE_NUMBER, 1, 0, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PERCENT, 50 }} },
{ &GTK_CSS_VALUE_NUMBER, 1, 0, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_PERCENT, 100 }} },
};
static GtkCssValue second_singletons[] = {
{ &GTK_CSS_VALUE_NUMBER, 1, 1, 0, 0, TYPE_DIMENSION, {{ GTK_CSS_S, 0 }} },

View File

@ -108,7 +108,7 @@ test_number_value (gconstpointer data)
if (test->is_computed)
g_assert_true (res == value);
g_assert_true (gtk_css_value_is_computed (res));
g_assert_true (gtk_css_number_value_has_percent (res) || gtk_css_value_is_computed (res));
if (test->computed)
{
@ -140,7 +140,7 @@ static CssColorValueTest color_tests[] = {
{ "rgba(255, 255, 128, 0.1)", TRUE, FALSE, "rgba(255,255,128,0.1)", "rgba(255,255,128,0.1)" },
{ "currentcolor", TRUE, TRUE, "currentcolor", "currentcolor", "color(srgb 1 0 0)", "color(srgb 1 0 0)" },
{ "color(from color(srgb 0.5 0.5 0.2) srgb 0.5 calc(r * g) b / calc(alpha / 2))", TRUE, FALSE, "color(srgb 0.5 0.25 0.2 / 0.5)", "color(srgb 0.5 0.25 0.2 / 0.5)" },
{ "rgb(from currentcolor r g 40% / 50%)", FALSE, TRUE, "color(from currentcolor srgb r g 40% / 50%)", "color(from currentcolor srgb r g 40% / 50%)", "color(srgb 1 0 0)", "color(srgb 1 0 0.4 / 0.5)" },
{ "rgb(from currentcolor r g 40% / 50%)", TRUE, TRUE, "color(from currentcolor srgb r g 40% / 50%)", "color(from currentcolor srgb r g 40% / 50%)", "color(srgb 1 0 0)", "color(srgb 1 0 0.4 / 0.5)" },
{ "rgb(from darkgoldenrod r g 100 / 50%)", TRUE, FALSE, "color(srgb 0.721569 0.52549 0.392157 / 0.5)", "color(srgb 0.721569 0.52549 0.392157 / 0.5)" },
{ "rgb(from white 100% 100% 100% / 100%)", TRUE, FALSE, "color(srgb 1 1 1)", "color(srgb 1 1 1)" },
{ "color(from white srgb 100% 100% 100% / 100%)", TRUE, FALSE, "color(srgb 1 1 1)", "color(srgb 1 1 1)" },