[pango/line-breaker: 14/32] Add a parshape example




commit 24b46e7f8cf404016c40c55f754bccf3aa972b82
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jan 18 08:04:24 2022 -0500

    Add a parshape example

 examples/meson.build |   1 +
 examples/parshape.c  | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+)
---
diff --git a/examples/meson.build b/examples/meson.build
index 0ec3860e..231d9826 100644
--- a/examples/meson.build
+++ b/examples/meson.build
@@ -5,6 +5,7 @@ if pango_cairo_backends.contains('png')
     'cairoshape',
     'cairosimple',
     'cairotwisted',
+    'parshape',
   ]
 endif
 
diff --git a/examples/parshape.c b/examples/parshape.c
new file mode 100644
index 00000000..0a233f4d
--- /dev/null
+++ b/examples/parshape.c
@@ -0,0 +1,127 @@
+#include <pango/pango.h>
+#include <pango/pangocairo.h>
+
+int
+main (int argc, char *argv[])
+{
+  const char *filename;
+  PangoContext *context;
+  PangoLineBreaker *breaker;
+  int x, y, width;
+  PangoLines *lines;
+  int inc, m, w, w2;
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  const char *text =
+      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+      "Nullam fringilla, est ut feugiat ultrices, elit lacus ultricies nibh, id commodo tortor nisi id elit. 
"
+      "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. "
+      "Morbi vel elit erat. Maecenas dignissim, dui et pharetra rutrum, tellus lectus rutrum mi, a convallis 
libero nisi quis tellus. "
+      "Nulla facilisi. Nullam eleifend lobortis nisl, in porttitor tellus malesuada vitae. "
+      "Aenean lacus tellus, pellentesque quis molestie quis, fringilla in arcu. "
+      "Duis elementum, tellus sed tristique semper, metus metus accumsan augue, et porttitor augue orci a 
libero. "
+      "Ut sed justo ac felis placerat laoreet sed id sem. Proin mattis tincidunt odio vitae tristique. "
+      "Morbi massa libero, congue vitae scelerisque vel, ultricies vel nisl. "
+      "Vestibulum in tortor diam, quis aliquet quam. Praesent ut justo neque, tempus rutrum est. "
+      "Duis eu lectus quam. Vivamus eget metus a mauris molestie venenatis pulvinar eleifend nisi. "
+      "Nulla facilisi. Pellentesque at dolor sit amet purus dapibus pulvinar molestie quis neque. "
+      "Suspendisse feugiat quam quis dolor accumsan cursus.";
+
+  if (argc != 2)
+    {
+      g_printerr ("Usage: %s OUTPUT_FILENAME\n", argv[0]);
+      return 1;
+    }
+
+  filename = argv[1];
+
+  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 400, 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 ();
+
+  m = 200;
+  w = 10;
+  w2 = -200;
+  inc = 40;
+
+  y = 40 * PANGO_SCALE;
+  x = (m - w / 2) * PANGO_SCALE;
+  width = w * PANGO_SCALE;
+
+  while (!pango_line_breaker_done (breaker))
+    {
+      PangoLine *line;
+      PangoRectangle ext;
+      gboolean ltr;
+
+      line = pango_line_breaker_next_line (breaker,
+                                           x, width,
+                                           PANGO_LINE_WRAP_CHAR,
+                                           PANGO_ELLIPSIZE_NONE);
+
+      pango_line_get_extents (line, NULL, &ext);
+      line = pango_line_justify (line, width);
+      pango_lines_add_line (lines, line, x, y - ext.y);
+
+      ltr = pango_line_breaker_get_direction (breaker) == PANGO_DIRECTION_LTR;
+
+      if (w2 > 0 && ltr && x <= m * PANGO_SCALE)
+        x = (m + w2 / 2) * PANGO_SCALE;
+      else if (w2 > 0 && !ltr && x > m * PANGO_SCALE)
+        x = (m - w2 / 2) * PANGO_SCALE;
+      else
+        {
+          y += ext.height;
+
+          w += inc;
+          w2 += inc;
+          if (w + inc >= 340 || w + inc < 0)
+            inc = - inc;
+
+          if (w2 > 0)
+            width = ((w - w2) / 2) * PANGO_SCALE;
+          else
+            width = w * PANGO_SCALE;
+
+          if (ltr)
+            x = (m - w / 2) * PANGO_SCALE;
+          else
+            x = (m + w / 2) * PANGO_SCALE;
+        }
+    }
+
+  for (int i = 0; i < pango_lines_get_line_count (lines); i++)
+    {
+      PangoLine *line = pango_lines_get_line (lines, i, &x, &y);
+
+      cairo_save (cr);
+      cairo_move_to (cr, x / (double)PANGO_SCALE, y / (double)PANGO_SCALE);
+      pango_cairo_show_line (cr, line);
+      cairo_restore (cr);
+    }
+
+  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;
+}


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