tools: Add more commands to gtk4-path-tool

Add pathops.
This commit is contained in:
Matthias Clasen
2023-07-01 21:26:30 -04:00
parent ef122df3e3
commit cf714ab265
5 changed files with 142 additions and 0 deletions

View File

@@ -14,6 +14,11 @@ SYNOPSIS
|
| **gtk4-path-tool** stroke [OPTIONS...] <PATH>
| **gtk4-path-tool** offset [OPTIONS...] <PATH>
| **gtk4-path-tool** simplify [OPTIONS...] <PATH>
| **gtk4-path-tool** intersection [OPTIONS...] <PATH> <PATH>
| **gtk4-path-tool** union [OPTIONS...] <PATH> <PATH>
| **gtk4-path-tool** difference [OPTIONS...] <PATH> <PATH>
| **gtk4-path-tool** symmetric-difference [OPTIONS...] <PATH> <PATH>
| **gtk4-path-tool** decompose [OPTIONS...] <PATH>
| **gtk4-path-tool** restrict [OPTIONS...] <PATH>
| **gtk4-path-tool** show [OPTIONS...] <PATH>
@@ -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
^^^^^^^^^^^

View File

@@ -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 <http://www.gnu.org/licenses/>.
*
* Author: Matthias Clasen
*/
#include "config.h"
#include <gtk/gtk.h>
#include "gtk-path-tool.h"
#include <glib/gi18n-lib.h>
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);
}
}

View File

@@ -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 ();

View File

@@ -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,

View File

@@ -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',