From 03a511aab48b47f63c732eaa1f14c03e366335f0 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 15 Aug 2023 13:33:23 -0400 Subject: [PATCH] path-tool: Make info show statistics It is somewhat interesting to know how many contours, lines, etc. a path consists of. --- tools/gtk-path-tool-info.c | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/tools/gtk-path-tool-info.c b/tools/gtk-path-tool-info.c index 2bca1f3123..dc05ba910f 100644 --- a/tools/gtk-path-tool-info.c +++ b/tools/gtk-path-tool-info.c @@ -24,6 +24,60 @@ #include +typedef struct +{ + int contours; + int ops; + int lines; + int quads; + int cubics; +} Statistics; + +static gboolean +stats_cb (GskPathOperation op, + const graphene_point_t *pts, + gsize n_pts, + gpointer user_data) +{ + Statistics *stats = user_data; + + stats->ops++; + + switch (op) + { + case GSK_PATH_MOVE: + stats->contours++; + break; + case GSK_PATH_CLOSE: + case GSK_PATH_LINE: + stats->lines++; + break; + case GSK_PATH_QUAD: + stats->quads++; + break; + case GSK_PATH_CUBIC: + stats->cubics++; + break; + default: + g_assert_not_reached (); + } + + return TRUE; +} + +static void +collect_statistics (GskPath *path, + Statistics *stats) +{ + stats->contours = 0; + stats->ops = 0; + stats->lines = 0; + stats->quads = 0; + stats->cubics = 0; + + gsk_path_foreach (path, -1, stats_cb, stats); +} + void do_info (int *argc, const char ***argv) { @@ -65,6 +119,8 @@ do_info (int *argc, const char ***argv) g_print ("%s\n", _("Path is empty.")); else { + Statistics stats; + if (gsk_path_is_closed (path)) g_print ("%s\n", _("Path is closed")); @@ -72,5 +128,27 @@ do_info (int *argc, const char ***argv) g_print ("%s: %g %g %g %g\n", _("Bounds"), bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); + + collect_statistics (path, &stats); + + g_print (_("%d contours"), stats.contours); + g_print ("\n"); + g_print (_("%d operations"), stats.ops); + g_print ("\n"); + if (stats.lines) + { + g_print (_("%d lines"), stats.lines); + g_print ("\n"); + } + if (stats.quads) + { + g_print (_("%d quadratics"), stats.quads); + g_print ("\n"); + } + if (stats.cubics) + { + g_print (_("%d cubics"), stats.cubics); + g_print ("\n"); + } } }