[pango/matthiasc/for-main: 18/18] pango-viewer: Visualize caret positions




commit 5a8df58b45c964e90867b6a732356959c803f2bf
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Aug 25 10:23:46 2021 -0400

    pango-viewer: Visualize caret positions
    
    With --annotate=256, show caret positions.
    This will be helpful in improving our handling
    of caret positions inside clusters.

 utils/viewer-pangocairo.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/utils/viewer-pangocairo.c b/utils/viewer-pangocairo.c
index 77201666..8203ca9c 100644
--- a/utils/viewer-pangocairo.c
+++ b/utils/viewer-pangocairo.c
@@ -157,7 +157,8 @@ enum {
   ANNOTATE_RUN_EXTENTS       =  32,
   ANNOTATE_CLUSTER_EXTENTS   =  64,
   ANNOTATE_CHAR_EXTENTS      = 128,
-  ANNOTATE_LAST              = 256,
+  ANNOTATE_CARET_POSITIONS   = 256,
+  ANNOTATE_LAST              = 512,
 };
 
 static void
@@ -405,6 +406,53 @@ render_callback (PangoLayout *layout,
           pango_layout_iter_free (iter);
           cairo_restore (cr);
         }
+
+      if (annotate & ANNOTATE_CARET_POSITIONS)
+        {
+          /* draw the caret positions in purple */
+          cairo_save (cr);
+          cairo_set_source_rgba (cr, 1.0, 0.0, 1.0, 0.5);
+
+          iter = pango_layout_get_iter (layout);
+          do
+            {
+              PangoRectangle rect;
+              PangoLayoutRun *run;
+              const char *text, *start, *end, *p;
+
+              pango_layout_iter_get_run_extents (iter, NULL, &rect);
+              run = pango_layout_iter_get_run_readonly (iter);
+
+              if (!run)
+                continue;
+
+              text = pango_layout_get_text (layout);
+              start =text + run->item->offset;
+              end = start + run->item->length;
+              for (p = start; p < end; p = g_utf8_next_char (p))
+                {
+                  int x, y;
+
+                  pango_glyph_string_index_to_x (run->glyphs,
+                                                 text + run->item->offset,
+                                                 run->item->length,
+                                                 &run->item->analysis,
+                                                 p - start,
+                                                 FALSE,
+                                                 &x);
+                  x += rect.x;
+                  y = pango_layout_iter_get_baseline (iter);
+
+                  cairo_arc (cr, x / PANGO_SCALE, y / PANGO_SCALE, 3.0, 0, 2*G_PI);
+                  cairo_close_path (cr);
+                  cairo_fill (cr);
+                }
+            }
+          while (pango_layout_iter_next_run (iter));
+          pango_layout_iter_free (iter);
+
+          cairo_restore (cr);
+        }
     }
 
   cairo_move_to (cr, 0, 0);
@@ -559,7 +607,9 @@ pangocairo_view_get_option_group (const PangoViewer *klass G_GNUC_UNUSED)
      "\t\t\t\t\t\t\t  16 - line extents\n"
      "\t\t\t\t\t\t\t  32 - run extents\n"
      "\t\t\t\t\t\t\t  64 - cluster extents\n"
-     "\t\t\t\t\t\t\t 128 - char extents", "FLAGS"},
+     "\t\t\t\t\t\t\t 128 - char extents\n",
+     "\t\t\t\t\t\t\t 256 - caret positions",
+     "FLAGS"},
     {NULL}
   };
   GOptionGroup *group;


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