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




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

    Add a parshape example

 utils/meson.build |   8 ++++
 utils/parshape.c  | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 132 insertions(+)
---
diff --git a/utils/meson.build b/utils/meson.build
index 6d06dbec..cba9b317 100644
--- a/utils/meson.build
+++ b/utils/meson.build
@@ -99,4 +99,12 @@ if cairo_dep.found()
                                    '-DPACKAGE_VERSION="@0@"'.format(meson.project_version()),
                                   ])
 
+  parshape = executable('parshape', [ 'parshape.c' ],
+                        dependencies: [ pango_deps, libpango_dep, libpangocairo_dep],
+                        include_directories: [ root_inc ],
+                        install: false,
+                        c_args: [
+                         '-DPACKAGE_NAME="@0@"'.format(meson.project_name()),
+                         '-DPACKAGE_VERSION="@0@"'.format(meson.project_version()),
+                        ])
 endif
diff --git a/utils/parshape.c b/utils/parshape.c
new file mode 100644
index 00000000..d5dbf623
--- /dev/null
+++ b/utils/parshape.c
@@ -0,0 +1,124 @@
+#include <pango/pango.h>
+#include <pango/pangocairo.h>
+
+int
+main (int argc, char *argv[])
+{
+  const char *file = "parshape.png";
+  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.";
+
+  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_n_lines (lines); i++)
+    {
+      PangoLine *line;
+
+      pango_lines_get_line (lines, i, &line, &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);
+    }
+
+  if (file)
+    {
+      cairo_surface_write_to_png (surface, file);
+      g_print ("Output written to %s\n", file);
+    }
+
+  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]