[pango/line-breaker: 13/18] Add a renderer comparison test




commit 9790991618202925a36e372ddf7022dc4f63ac0f
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jan 14 22:13:22 2022 -0500

    Add a renderer comparison test

 tests/meson.build     |   1 +
 tests/test-renderer.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)
---
diff --git a/tests/meson.build b/tests/meson.build
index 83732b5a..def88bbe 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -51,6 +51,7 @@ if cairo_dep.found()
     [ 'cxx-test', [ 'cxx-test.cpp' ], [ libpangocairo_dep, gobject_dep, harfbuzz_dep ] ],
     [ 'test-harfbuzz', [ 'test-harfbuzz.c' ], [ libpangocairo_dep, gobject_dep, harfbuzz_dep ] ],
     [ 'test-break', [ 'test-break.c', 'test-common.c', 'validate-log-attrs.c' ], [libpangocairo_dep, 
glib_dep, harfbuzz_dep ] ],
+    [ 'test-renderer', [ 'test-renderer.c' ], [ libpangocairo_dep ] ],
   ]
 
   if build_pangoft2
diff --git a/tests/test-renderer.c b/tests/test-renderer.c
new file mode 100644
index 00000000..59a6e531
--- /dev/null
+++ b/tests/test-renderer.c
@@ -0,0 +1,125 @@
+#include <pango/pango.h>
+#include <pango/pangocairo.h>
+
+int
+main (int argc, char *argv[])
+{
+  const char *file = "out.png";
+  PangoContext *context;
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  const char *text =
+      "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"
+      "Nullam fringilla, est ut feugiat ultrices, elit lacus ultricies nibh, id commodo tortor nisi id 
elit.\n"
+      "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.\n"
+      "Morbi vel elit erat. Maecenas dignissim, dui et pharetra rutrum, tellus lectus rutrum mi, a convallis 
libero nisi quis tellus.\n"
+      "Nulla facilisi. Nullam eleifend lobortis nisl, in porttitor tellus malesuada vitae.\n"
+      "Aenean lacus tellus, pellentesque quis molestie quis, fringilla in arcu.\n"
+      "Duis elementum, tellus sed tristique semper, metus metus accumsan augue, et porttitor augue orci a 
libero.\n"
+      "Ut sed justo ac felis placerat laoreet sed id sem. Proin mattis tincidunt odio vitae tristique.\n"
+      "Morbi massa libero, congue vitae scelerisque vel, ultricies vel nisl.\n"
+      "Vestibulum in tortor diam, quis aliquet quam. Praesent ut justo neque, tempus rutrum est.\n"
+      "Duis eu lectus quam. Vivamus eget metus a mauris molestie venenatis pulvinar eleifend nisi.\n"
+      "Nulla facilisi. Pellentesque at dolor sit amet purus dapibus pulvinar molestie quis neque.\n"
+      "Suspendisse feugiat quam quis dolor accumsan cursus.";
+
+  //text = "ABC\nDEF\nGHI";
+  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 400, 400);
+  cr = cairo_create (surface);
+
+  if (argc > 1 && strcmp (argv[1], "--layout") == 0)
+    {
+      PangoLayout *layout = pango_layout_new (context);
+
+      g_print ("Using %s\n", G_OBJECT_TYPE_NAME (layout));
+
+      pango_layout_set_text (layout, text, -1);
+      pango_layout_set_width (layout, 200 * PANGO_SCALE);
+      pango_layout_set_line_spacing (layout, 1.);
+
+      pango_cairo_show_layout (cr, layout);
+
+      g_object_unref (layout);
+    }
+  else if (argc > 1 && strcmp (argv[1], "--simple-layout") == 0)
+    {
+      PangoSimpleLayout *layout = pango_simple_layout_new (context);
+
+      g_print ("Using %s\n", G_OBJECT_TYPE_NAME (layout));
+
+      pango_simple_layout_set_text (layout, text, -1);
+      pango_simple_layout_set_width (layout, 200 * PANGO_SCALE);
+
+      pango_cairo_show_lines (cr, pango_simple_layout_get_lines (layout));
+
+      g_object_unref (layout);
+    }
+  else if (argc > 1 && strcmp (argv[1], "--line-breaker") == 0)
+    {
+      PangoLineBreaker *breaker;
+      int x, y, width;
+      PangoLines *lines;
+
+      breaker = pango_line_breaker_new (context);
+
+      g_print ("Using %s\n", G_OBJECT_TYPE_NAME (breaker));
+
+      pango_line_breaker_add_text (breaker, text, -1, NULL);
+
+      lines = pango_lines_new ();
+
+      x = y = 0;
+      width = 200 * PANGO_SCALE;
+
+      while (!pango_line_breaker_done (breaker))
+        {
+          PangoLine *line;
+          PangoRectangle ext;
+
+          line = pango_line_breaker_next_line (breaker,
+                                               x, width,
+                                               PANGO_WRAP_WORD,
+                                               PANGO_ELLIPSIZE_NONE,
+                                               PANGO_ALIGNMENT_LEFT);
+          pango_lines_add_line (lines, line, x, y);
+          pango_line_get_extents (line, &ext);
+          y += ext.height;
+        }
+
+      for (int i = 0; i < pango_lines_get_n_lines (lines); i++)
+        {
+          PangoLine *line;
+          PangoRectangle ext;
+
+          pango_lines_get_line (lines, i, &line, &x, &y);
+
+          pango_line_get_extents (line, &ext);
+          cairo_move_to (cr, (x + ext.x) / (double)PANGO_SCALE, (y - ext.y) / (double)PANGO_SCALE);
+          pango_cairo_show_line (cr, line);
+        }
+
+      g_object_unref (lines);
+
+      g_object_unref (breaker);
+    }
+  else
+    {
+      g_print ("Use --layout, --simple-layout or --line-breaker\n");
+      file = NULL;
+    }
+
+  if (file)
+    {
+      cairo_surface_write_to_png (surface, file);
+      g_print ("Output written to %s\n", file);
+    }
+
+  cairo_surface_destroy (surface);
+  cairo_destroy (cr);
+
+  g_object_unref (context);
+
+  return 0;
+}


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