[pango/visible-things: 40/40] pango-view: Optionally show trailing space



commit 038f611cd07d0da65c7e8b36c108ed169c83cddc
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jul 13 11:48:42 2019 -0400

    pango-view: Optionally show trailing space
    
    This may not be useful to merge, but it shows
    how post-processing lines can be used.

 utils/viewer-pangocairo.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
---
diff --git a/utils/viewer-pangocairo.c b/utils/viewer-pangocairo.c
index 9054b9fe..5143c7d4 100644
--- a/utils/viewer-pangocairo.c
+++ b/utils/viewer-pangocairo.c
@@ -27,6 +27,7 @@
 #include <pango/pangocairo.h>
 
 static int opt_annotate = 0;
+static gboolean opt_show_trailing_space;
 
 typedef struct
 {
@@ -130,6 +131,35 @@ pangocairo_view_destroy_surface (gpointer instance,
   g_slice_free (CairoSurface, surface);
 }
 
+static void
+post_process_last_runs (const char     *text,
+                        PangoGlyphItem *prev,
+                        PangoGlyphItem *last)
+{
+  int i;
+  PangoGlyphItem *run;
+
+  for (run = last; run; run = prev, prev = NULL)
+    {
+      for (i = run->glyphs->num_glyphs - 1; i >= 0; i--)
+        {
+          gunichar ch;
+          int pos;
+
+          pos = run->glyphs->log_clusters[i];
+          ch = g_utf8_get_char (text + run->item->offset + pos);
+          if (ch == 0x20)
+            run->glyphs->glyphs[i].glyph = PANGO_GET_UNKNOWN_GLYPH (0x2423);
+          else if (ch == 0x2028)
+            run->glyphs->glyphs[i].glyph = PANGO_GET_UNKNOWN_GLYPH ('\n');
+          else
+            break;
+        }
+      if (i > 0)
+        break;
+    }
+}
+
 static void
 render_callback (PangoLayout *layout,
                 int          x,
@@ -140,6 +170,25 @@ render_callback (PangoLayout *layout,
   cairo_t *cr = (cairo_t *) context;
   int annotate = (GPOINTER_TO_INT (state) + opt_annotate) % 4;
 
+  if (opt_show_trailing_space)
+    {
+      PangoLayoutIter *iter;
+      PangoLayoutRun *prev, *run, *next;
+      const char *text;
+
+      text = pango_layout_get_text (layout);
+      iter = pango_layout_get_iter (layout);
+      prev = run = next = NULL;
+      do {
+        prev = run;
+        run = next;
+        next = pango_layout_iter_get_run (iter);
+        if (next == NULL && run != NULL) /* end of line */
+          post_process_last_runs (text, prev, run);
+      } while (pango_layout_iter_next_run (iter));
+      pango_layout_iter_free (iter);
+    }
+
   cairo_save (cr);
   cairo_translate (cr, x, y);
 
@@ -442,6 +491,8 @@ pangocairo_view_get_option_group (const PangoViewer *klass G_GNUC_UNUSED)
   {
     {"annotate",       0, 0, G_OPTION_ARG_INT, &opt_annotate,
      "Annotate the output",                            "1, 2 or 3"},
+    {"show-trailing-space",    0, 0, G_OPTION_ARG_NONE, &opt_show_trailing_space,
+     "Show trailing space",                            NULL},
     {NULL}
   };
   GOptionGroup *group;


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