From cf714ab2658bfc2afd6e8f0e968b3369a529402c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 1 Jul 2023 21:26:30 -0400 Subject: [PATCH] tools: Add more commands to gtk4-path-tool Add pathops. --- docs/reference/gtk/gtk4-path-tool.rst | 20 +++++ tools/gtk-path-tool-pathops.c | 104 ++++++++++++++++++++++++++ tools/gtk-path-tool.c | 16 ++++ tools/gtk-path-tool.h | 1 + tools/meson.build | 1 + 5 files changed, 142 insertions(+) create mode 100644 tools/gtk-path-tool-pathops.c diff --git a/docs/reference/gtk/gtk4-path-tool.rst b/docs/reference/gtk/gtk4-path-tool.rst index 478ebe8eb2..8ed203082b 100644 --- a/docs/reference/gtk/gtk4-path-tool.rst +++ b/docs/reference/gtk/gtk4-path-tool.rst @@ -14,6 +14,11 @@ SYNOPSIS | | **gtk4-path-tool** stroke [OPTIONS...] | **gtk4-path-tool** offset [OPTIONS...] +| **gtk4-path-tool** simplify [OPTIONS...] +| **gtk4-path-tool** intersection [OPTIONS...] +| **gtk4-path-tool** union [OPTIONS...] +| **gtk4-path-tool** difference [OPTIONS...] +| **gtk4-path-tool** symmetric-difference [OPTIONS...] | **gtk4-path-tool** decompose [OPTIONS...] | **gtk4-path-tool** restrict [OPTIONS...] | **gtk4-path-tool** show [OPTIONS...] @@ -96,6 +101,21 @@ is different from applying a translation transformation. The limit at which to clip miters at line joins. The default value is 4. +Boolean Operations +^^^^^^^^^^^^^^^^^^ + +The ``intersection``, ``union``, ``difference`` and ``symmetric-difference`` commands +perform boolean operations on paths. Given two paths, they create a new path which +encircles the area that is the intersection, union, difference or symmetric difference +of the areas encircled by the paths. + +Simplification +^^^^^^^^^^^^^^ + +The ``simplify`` command removes areas of overlap from a path such that the resulting +path encircles the same area, but every edge in the resulting path is a boundary between +the inside and the outside. + Decomposing ^^^^^^^^^^^ diff --git a/tools/gtk-path-tool-pathops.c b/tools/gtk-path-tool-pathops.c new file mode 100644 index 0000000000..f48be59a87 --- /dev/null +++ b/tools/gtk-path-tool-pathops.c @@ -0,0 +1,104 @@ +/* Copyright 2023 Red Hat, Inc. + * + * GTK+ is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * GLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GTK+; see the file COPYING. If not, + * see . + * + * Author: Matthias Clasen + */ + +#include "config.h" + +#include +#include "gtk-path-tool.h" + +#include + +void +do_pathop (const char *op, int *argc, const char ***argv) +{ + GError *error = NULL; + const char *fill = "winding"; + char **args = NULL; + GOptionContext *context; + GOptionEntry entries[] = { + { "fill-rule", 0, 0, G_OPTION_ARG_STRING, &fill, N_("Fill rule"), N_("RULE") }, + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL, N_("PATH…") }, + { NULL, }, + }; + GskPath *path1, *path2, *result; + GskFillRule fill_rule; + char *prgname; + char *summary; + + prgname = g_strconcat ("gtk4-path-tool ", op, NULL); + summary = g_strdup_printf (_("Apply the %s path operation."), op); + g_set_prgname (prgname); + + context = g_option_context_new (NULL); + g_option_context_set_translation_domain (context, GETTEXT_PACKAGE); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_set_summary (context, summary); + + if (!g_option_context_parse (context, argc, (char ***)argv, &error)) + { + g_printerr ("%s\n", error->message); + g_error_free (error); + exit (1); + } + + g_option_context_free (context); + + if (args == NULL) + { + g_printerr ("%s\n", _("No paths given.")); + exit (1); + } + + path1 = get_path (args[0]); + if (args[1] != NULL) + path2 = get_path (args[1]); + else + path2 = NULL; + + fill_rule = get_enum_value (GSK_TYPE_FILL_RULE, _("fill rule"), fill); + + if (strcmp (op, "simplify") == 0) + result = gsk_path_simplify (path1, fill_rule); + else if (strcmp (op, "union") == 0) + result = gsk_path_union (path1, path2, fill_rule); + else if (strcmp (op, "intersection") == 0) + result = gsk_path_intersection (path1, path2, fill_rule); + else if (strcmp (op, "difference") == 0) + result = gsk_path_difference (path1, path2, fill_rule); + else if (strcmp (op, "symmetric-difference") == 0) + result = gsk_path_symmetric_difference (path1, path2, fill_rule); + else + { + char *msg = g_strdup_printf (_("'%s' is not a supported operation."), op); + g_printerr ("%s\n", msg); + exit (1); + } + + if (result) + { + char *str = gsk_path_to_string (result); + g_print ("%s\n", str); + g_free (str); + } + else + { + g_printerr ("%s\n", _("That didn't work out.")); + exit (1); + } +} diff --git a/tools/gtk-path-tool.c b/tools/gtk-path-tool.c index 235295885e..03fa99336e 100644 --- a/tools/gtk-path-tool.c +++ b/tools/gtk-path-tool.c @@ -40,6 +40,12 @@ usage (void) "Commands:\n" " stroke Stroke the path\n" " offset Offset the path\n" + " simplify Simplify the path\n" + " intersection Intersect two paths\n" + " union Create the union of two paths\n" + " difference Create the difference of two paths\n" + " symmetric-difference\n" + " Create the symmetric difference of two paths\n" " decompose Decompose the path\n" " restrict Restrict the path to a segment\n" " show Display the path in a window\n" @@ -129,8 +135,12 @@ main (int argc, const char *argv[]) if (strcmp (argv[0], "decompose") == 0) do_decompose (&argc, &argv); + else if (strcmp (argv[0], "difference") == 0) + do_pathop (argv[0], &argc, &argv); else if (strcmp (argv[0], "info") == 0) do_info (&argc, &argv); + else if (strcmp (argv[0], "intersection") == 0) + do_pathop (argv[0], &argc, &argv); else if (strcmp (argv[0], "offset") == 0) do_offset (&argc, &argv); else if (strcmp (argv[0], "render") == 0) @@ -139,8 +149,14 @@ main (int argc, const char *argv[]) do_restrict (&argc, &argv); else if (strcmp (argv[0], "show") == 0) do_show (&argc, &argv); + else if (strcmp (argv[0], "simplify") == 0) + do_pathop (argv[0], &argc, &argv); else if (strcmp (argv[0], "stroke") == 0) do_stroke (&argc, &argv); + else if (strcmp (argv[0], "symmetric-difference") == 0) + do_pathop (argv[0], &argc, &argv); + else if (strcmp (argv[0], "union") == 0) + do_pathop (argv[0], &argc, &argv); else usage (); diff --git a/tools/gtk-path-tool.h b/tools/gtk-path-tool.h index 9ae3841605..932aeeabc6 100644 --- a/tools/gtk-path-tool.h +++ b/tools/gtk-path-tool.h @@ -7,6 +7,7 @@ void do_decompose (int *argc, const char ***argv); void do_restrict (int *argc, const char ***argv); void do_render (int *argc, const char ***argv); void do_show (int *argc, const char ***argv); +void do_pathop (const char *op, int *argc, const char ***argv); GskPath *get_path (const char *arg); int get_enum_value (GType type, diff --git a/tools/meson.build b/tools/meson.build index 4b36acbbe7..6358226f74 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -27,6 +27,7 @@ gtk_tools = [ 'gtk-path-tool-decompose.c', 'gtk-path-tool-info.c', 'gtk-path-tool-offset.c', + 'gtk-path-tool-pathops.c', 'gtk-path-tool-render.c', 'gtk-path-tool-restrict.c', 'gtk-path-tool-show.c',