[gtk/path-ops: 22/23] path ops: Add a debug api




commit 3744cc468aaa9347d1d6d6a2c639e1d6da09b6f7
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Apr 1 18:48:37 2022 -0400

    path ops: Add a debug api

 gsk/gskpathops.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/glyphs.c   | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)
---
diff --git a/gsk/gskpathops.c b/gsk/gskpathops.c
index d43b9e636d..0384d122f8 100644
--- a/gsk/gskpathops.c
+++ b/gsk/gskpathops.c
@@ -32,6 +32,8 @@
 
 /* {{{ General utilities */
 
+#define RAD_TO_DEG(r) ((r)*180.0/M_PI)
+
 static void
 g_list_insert_after (GList    *l,
                      gpointer  data)
@@ -977,6 +979,59 @@ dump_dotfile (GList      *edges,
   g_string_free (s, TRUE);
 }
 
+static GString *debug_stream;
+
+GDK_AVAILABLE_IN_ALL
+void gsk_path_set_debug_stream (GString *string);
+
+void
+gsk_path_set_debug_stream (GString *string)
+{
+  debug_stream = string;
+}
+
+static void
+add_debug (const char *format,
+           ...)
+{
+  if (debug_stream)
+    {
+      va_list args;
+
+      va_start (args, format);
+      g_string_append_vprintf (debug_stream,  format, args);
+      va_end (args);
+    }
+}
+
+static void
+add_edge_debug (Node *node,
+                Edge *edge,
+                GString *s)
+{
+  if (edge->end == node)
+    g_string_append_printf (s, "/%s: %d %d%d", edge->name, (int)RAD_TO_DEG (edge->end_angle), 
edge->area_left == AREA_IN, edge->area_right == AREA_IN);
+  else
+    g_string_append_printf (s, "/%s: %d %d%d", edge->name, (int)RAD_TO_DEG (edge->start_angle), 
edge->area_right == AREA_IN, edge->area_left == AREA_IN);
+}
+
+static void
+add_node_debug (Node *node)
+{
+  GString *s = g_string_new ("");
+
+  g_string_append_printf (s, "b:%d ", node->boundaries);
+  for (int i = 0; i < node->edges->len; i++)
+    {
+      Edge *edge = g_ptr_array_index (node->edges, i);
+      add_edge_debug (node, edge, s);
+    }
+
+  add_debug ("(%g,%g) %d %s\n", node->p.x, node->p.y, node->inconsistent, s->str);
+
+  g_string_free (s, TRUE);
+}
+
 /* }}} */
 
 /*
@@ -1373,6 +1428,16 @@ gsk_path_op (GskPathOp  operation,
   if (opdata.second)
     gsk_path_measure_unref (opdata.second);
 
+  if (debug_stream)
+    {
+      g_string_truncate (debug_stream, 0);
+      for (l = opdata.nodes; l; l = l->next)
+        {
+          Node *node = l->data;
+          add_node_debug (node);
+        }
+    }
+
   g_list_free_full (opdata.edges, (GDestroyNotify) edge_free);
   g_list_free_full (opdata.nodes, (GDestroyNotify) node_free);
 
diff --git a/tests/glyphs.c b/tests/glyphs.c
index b2c70f128b..4a346da7d7 100644
--- a/tests/glyphs.c
+++ b/tests/glyphs.c
@@ -1,6 +1,8 @@
 #include <gtk/gtk.h>
 #include <gsk/gskcurveprivate.h>
 
+static GString *debug_stream;
+
 #define DEMO_TYPE_WIDGET (demo_widget_get_type ())
 G_DECLARE_FINAL_TYPE (DemoWidget, demo_widget, DEMO, WIDGET, GtkWidget)
 
@@ -292,6 +294,66 @@ point_cb (GskPathOperation        op,
   return TRUE;
 }
 
+static void
+add_debug_string (GtkWidget *widget,
+                  GtkSnapshot *snapshot,
+                  double       x,
+                  double       y,
+                  const char  *text)
+{
+  PangoLayout *layout;
+
+  layout = gtk_widget_create_pango_layout (widget, text);
+
+  gtk_snapshot_save (snapshot);
+
+  draw_point2 (snapshot, &GRAPHENE_POINT_INIT (x, y), &(GdkRGBA) { 1, 0, 1, 1 }, 4);
+
+  gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (x + 10, y + 10));
+  gtk_snapshot_append_layout (snapshot, layout, &(GdkRGBA){0, 0, 0, 1});
+  gtk_snapshot_restore (snapshot);
+
+  g_object_unref (layout);
+}
+
+static void
+add_debug_stream (GtkWidget *widget,
+                  GtkSnapshot *snapshot)
+{
+  char **s;
+
+  s = g_strsplit (debug_stream->str, "\n", -1);
+  for (int i = 0; s[i]; i++)
+    {
+      char *end;
+      char *p = s[i];
+      double x, y;
+      gboolean inconsistent;
+
+      if (p[0] != '(')
+        break;
+       p++;
+
+       x = g_ascii_strtod (p, &end);
+       if (*end != ',')
+         break;
+       p = end + 1;
+
+       y = g_ascii_strtod (p, &end);
+       if (*end != ')')
+         break;
+       p = end + 1;
+
+       inconsistent = g_ascii_strtoll (p, &end, 10);
+       p = end + 1;
+
+       if (inconsistent)
+         add_debug_string (widget, snapshot, x, y, p);
+    }
+
+  g_strfreev (s);
+}
+
 static void
 demo_widget_snapshot (GtkWidget   *widget,
                       GtkSnapshot *snapshot)
@@ -312,6 +374,8 @@ demo_widget_snapshot (GtkWidget   *widget,
   width = self->bounds.origin.x + self->bounds.size.width + 10;
   height =self->bounds.origin.y + self->bounds.size.height + 10;
 
+  add_debug_stream (widget, snapshot);
+
   if (self->do_fill)
     {
       gtk_snapshot_push_fill (snapshot, self->path, self->fill_rule);
@@ -541,6 +605,8 @@ update_text_output (DemoWidget *self)
   g_free (text);
 }
 
+extern void gsk_path_set_debug_stream (GString *string);
+
 static void
 demo_widget_set_path (DemoWidget *self,
                       GskPath    *path1,
@@ -558,6 +624,13 @@ demo_widget_set_path (DemoWidget *self,
 
   self->orig_path1 = gsk_path_ref (path1);
   self->orig_path2 = gsk_path_ref (path2);
+
+  if (!debug_stream)
+    {
+      debug_stream = g_string_new ("");
+      gsk_path_set_debug_stream (debug_stream);
+    }
+
   switch (self->operation)
     {
     case 0:


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