gsk: Make matrix transforms roundtrip better

When we parse a 2D matrix, we produce a 2D transform, and we
can easily regenerate a 2D matrix from it.

Tests included.
This commit is contained in:
Matthias Clasen 2024-02-11 19:28:43 -05:00
parent 3ae6da10b1
commit 865564b32f
2 changed files with 55 additions and 7 deletions

View File

@ -382,15 +382,35 @@ gsk_matrix_transform_print (GskTransform *transform,
guint i;
float f[16];
g_string_append (string, "matrix3d(");
graphene_matrix_to_float (&self->matrix, f);
for (i = 0; i < 16; i++)
if (transform->category >= GSK_TRANSFORM_CATEGORY_2D)
{
if (i > 0)
g_string_append (string, ", ");
string_append_double (string, f[i]);
g_string_append (string, "matrix(");
graphene_matrix_to_float (&self->matrix, f);
string_append_double (string, f[0]);
g_string_append (string, ", ");
string_append_double (string, f[1]);
g_string_append (string, ", ");
string_append_double (string, f[4]);
g_string_append (string, ", ");
string_append_double (string, f[5]);
g_string_append (string, ", ");
string_append_double (string, f[12]);
g_string_append (string, ", ");
string_append_double (string, f[13]);
g_string_append (string, ")");
}
else
{
g_string_append (string, "matrix3d(");
graphene_matrix_to_float (&self->matrix, f);
for (i = 0; i < 16; i++)
{
if (i > 0)
g_string_append (string, ", ");
string_append_double (string, f[i]);
}
g_string_append (string, ")");
}
g_string_append (string, ")");
}
static GskTransform *

View File

@ -967,6 +967,33 @@ test_matrix_transform (void)
gsk_transform_unref (t2);
}
static void
test_matrix_roundtrip (void)
{
GskTransform *transform;
gboolean res;
const char *input;
char *str;
input = "matrix(2, 3, 4, 5, 6, 7)";
res = gsk_transform_parse (input, &transform);
g_assert_true (res);
g_assert_true (gsk_transform_get_category (transform) >= GSK_TRANSFORM_CATEGORY_2D);
str = gsk_transform_to_string (transform);
g_assert_cmpstr (str, ==, input);
g_free (str);
input = "matrix3d(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17)";
res = gsk_transform_parse (input, &transform);
g_assert_true (res);
g_assert_true (gsk_transform_get_category (transform) < GSK_TRANSFORM_CATEGORY_2D);
str = gsk_transform_to_string (transform);
g_assert_cmpstr (str, ==, input);
g_free (str);
}
int
main (int argc,
char *argv[])
@ -991,6 +1018,7 @@ main (int argc,
g_test_add_func ("/transform/rotate", test_rotate_transform);
g_test_add_func ("/transform/rotate3d", test_rotate3d_transform);
g_test_add_func ("/transform/matrix", test_matrix_transform);
g_test_add_func ("/transform/matrix/roundtrip", test_matrix_roundtrip);
return g_test_run ();
}