From 865564b32f2e3ff951d6634984dbb3185d416f26 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 11 Feb 2024 19:28:43 -0500 Subject: [PATCH] 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. --- gsk/gsktransform.c | 34 +++++++++++++++++++++++++++------- testsuite/gsk/transform.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/gsk/gsktransform.c b/gsk/gsktransform.c index ada7164e37..4ab9e9dc79 100644 --- a/gsk/gsktransform.c +++ b/gsk/gsktransform.c @@ -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 * diff --git a/testsuite/gsk/transform.c b/testsuite/gsk/transform.c index 01220a2b2f..7075ce9533 100644 --- a/testsuite/gsk/transform.c +++ b/testsuite/gsk/transform.c @@ -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 (); }