[pango/block-cursor-fixes] Add a layout extents check



commit cb072fb969d37b6c52d483673fa391a4f3710abb
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Aug 19 21:53:18 2021 -0400

    Add a layout extents check
    
    Add a check that tests that layout, line, run,
    cluster and char extents are nested as expected.

 tests/testmisc.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
---
diff --git a/tests/testmisc.c b/tests/testmisc.c
index 8ebfec80..99b4a5d8 100644
--- a/tests/testmisc.c
+++ b/tests/testmisc.c
@@ -418,6 +418,77 @@ test_index_to_x (void)
   g_object_unref (context);
 }
 
+static gboolean
+pango_rectangle_contains (const PangoRectangle *r1,
+                          const PangoRectangle *r2)
+{
+  return r2->x >= r1->x &&
+         r2->y >= r1->y &&
+         r2->x + r2->width <= r1->x + r1->width &&
+         r2->y + r2->height <= r1->y + r1->height;
+}
+
+static void
+test_extents (void)
+{
+  PangoContext *context;
+  const char *tests[] = {
+    "Some long text that has multiple lines that are wrapped by Pango."
+  };
+
+  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+
+  for (int i = 0; i < G_N_ELEMENTS (tests); i++)
+    {
+      PangoLayout *layout;
+      PangoLayoutIter *iter;
+      PangoRectangle layout_extents;
+      PangoRectangle line_extents;
+      PangoRectangle run_extents;
+      PangoRectangle cluster_extents;
+      PangoRectangle char_extents;
+
+      layout = pango_layout_new (context);
+      pango_layout_set_text (layout, tests[i], -1);
+      pango_layout_set_width (layout, 80 * PANGO_SCALE);
+
+      pango_layout_get_extents (layout, NULL, &layout_extents);
+
+      iter = pango_layout_get_iter (layout);
+
+      do
+        {
+          pango_layout_iter_get_line_extents (iter, NULL, &line_extents);
+          pango_layout_iter_get_run_extents (iter, NULL, &run_extents);
+          pango_layout_iter_get_cluster_extents (iter, NULL, &cluster_extents);
+          pango_layout_iter_get_char_extents (iter, &char_extents);
+
+          if (!pango_rectangle_contains (&cluster_extents, &char_extents))
+            g_print ("at index %d: cluster %d %d %d %d char %d %d %d %d\n",
+                     pango_layout_iter_get_index (iter),
+                     cluster_extents.x,
+                     cluster_extents.y,
+                     cluster_extents.width,
+                     cluster_extents.height,
+                     char_extents.x,
+                     char_extents.y,
+                     char_extents.width,
+                     char_extents.height);
+
+          g_assert_true (pango_rectangle_contains (&layout_extents, &line_extents));
+          g_assert_true (pango_rectangle_contains (&line_extents, &run_extents));
+          g_assert_true (pango_rectangle_contains (&run_extents, &cluster_extents));
+          g_assert_true (pango_rectangle_contains (&cluster_extents, &char_extents));
+        }
+      while (pango_layout_iter_next_char (iter));
+
+      pango_layout_iter_free (iter);
+      g_object_unref (layout);
+    }
+
+  g_object_unref (context);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -442,6 +513,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/bidi/get-cursor-crash", test_get_cursor_crash);
   g_test_add_func ("/bidi/get-cursor", test_get_cursor);
   g_test_add_func ("/layout/index-to-x", test_index_to_x);
+  g_test_add_func ("/layout/extents", test_extents);
 
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]