[pango/line-breaker: 5/31] Add a text flow example




commit 4d3e6334558c998d1156f740e17072969ef4cfc1
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jan 24 01:25:17 2022 -0500

    Add a text flow example
    
    This formats text in two columns.
    Use it like:
    
    columns INPUT OUTPUT

 examples/columns.c   | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 examples/meson.build |  1 +
 2 files changed, 94 insertions(+)
---
diff --git a/examples/columns.c b/examples/columns.c
new file mode 100644
index 00000000..516e646f
--- /dev/null
+++ b/examples/columns.c
@@ -0,0 +1,93 @@
+#include <pango/pango.h>
+#include <pango/pangocairo.h>
+
+int
+main (int argc, char *argv[])
+{
+  const char *filename;
+  PangoContext *context;
+  PangoLineBreaker *breaker;
+  int margin;
+  int x, y, width, height;
+  PangoLines *lines;
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  char *text;
+  gsize length;
+  GError *error = NULL;
+
+  if (argc != 3)
+    {
+      g_printerr ("Usage: %s INPUT_FILENAME OUTPUT_FILENAME\n", argv[0]);
+      return 1;
+    }
+
+  if (!g_file_get_contents (argv[1], &text, &length, &error))
+    {
+      g_printerr ("%s\n", error->message);
+      return 1;
+    }
+
+  filename = argv[2];
+
+  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 600, 600);
+  cr = cairo_create (surface);
+  cairo_set_source_rgb (cr, 1, 1, 1);
+  cairo_paint (cr);
+  cairo_set_source_rgb (cr, 0, 0, 0);
+
+  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 ();
+
+  margin = 30;
+  x = margin * PANGO_SCALE;
+  y = margin * PANGO_SCALE;
+  width = (600/2 - 2 * margin) * PANGO_SCALE;
+  height = (600 - 2 * margin) * PANGO_SCALE;
+
+  while (!pango_line_breaker_done (breaker))
+    {
+      PangoLayoutLine *line;
+      PangoRectangle ext;
+
+      line = pango_line_breaker_next_line (breaker,
+                                           x, width,
+                                           PANGO_WRAP_CHAR,
+                                           PANGO_ELLIPSIZE_NONE);
+
+      pango_layout_line_get_extents (line, NULL, &ext);
+      if (!pango_layout_line_ends_paragraph (line))
+        line = pango_layout_line_justify (line, width);
+      if (y + ext.height > height)
+        {
+          x += 300 * PANGO_SCALE;
+          y = margin * PANGO_SCALE;
+        }
+
+      pango_lines_add_line (lines, line, x, y);
+
+      y += ext.height;
+    }
+
+  pango_cairo_show_lines (cr, lines);
+
+  cairo_surface_write_to_png (surface, filename);
+  g_print ("Output written to %s\n", filename);
+
+  g_object_unref (lines);
+  g_object_unref (breaker);
+
+  cairo_surface_destroy (surface);
+  cairo_destroy (cr);
+
+  g_object_unref (context);
+
+  return 0;
+}
diff --git a/examples/meson.build b/examples/meson.build
index 231d9826..d5f6886a 100644
--- a/examples/meson.build
+++ b/examples/meson.build
@@ -6,6 +6,7 @@ if pango_cairo_backends.contains('png')
     'cairosimple',
     'cairotwisted',
     'parshape',
+    'columns',
   ]
 endif
 


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