path-tool: Make render match show

This commit is contained in:
Matthias Clasen 2023-09-03 23:40:42 +02:00
parent 1e9e8d24c3
commit 1e8e7e0c00

View File

@ -29,6 +29,67 @@
#include <gtk/gtk.h>
#include "gtk-path-tool.h"
static gboolean
collect_cb (GskPathOperation op,
const graphene_point_t *pts,
gsize n_pts,
float weight,
gpointer data)
{
GskPathBuilder **builders = (GskPathBuilder **)data;
switch (op)
{
case GSK_PATH_MOVE:
if (builders[0])
gsk_path_builder_move_to (builders[0], pts[0].x, pts[0].y);
if (builders[1])
gsk_path_builder_add_circle (builders[1], &pts[0], 4);
break;
case GSK_PATH_LINE:
case GSK_PATH_CLOSE:
if (builders[0])
gsk_path_builder_line_to (builders[0], pts[1].x, pts[1].y);
if (builders[1])
gsk_path_builder_add_circle (builders[1], &pts[1], 4);
break;
case GSK_PATH_QUAD:
case GSK_PATH_CONIC:
if (builders[0])
{
gsk_path_builder_line_to (builders[0], pts[1].x, pts[1].y);
gsk_path_builder_line_to (builders[0], pts[2].x, pts[2].y);
}
if (builders[1])
gsk_path_builder_add_circle (builders[1], &pts[2], 4);
if (builders[2])
gsk_path_builder_add_circle (builders[2], &pts[1], 3);
break;
case GSK_PATH_CUBIC:
if (builders[0])
{
gsk_path_builder_line_to (builders[0], pts[1].x, pts[1].y);
gsk_path_builder_line_to (builders[0], pts[2].x, pts[2].y);
gsk_path_builder_line_to (builders[0], pts[3].x, pts[3].y);
}
if (builders[1])
gsk_path_builder_add_circle (builders[1], &pts[3], 4);
if (builders[2])
{
gsk_path_builder_add_circle (builders[2], &pts[1], 3);
gsk_path_builder_add_circle (builders[2], &pts[2], 3);
}
break;
default:
g_assert_not_reached ();
}
return TRUE;
}
void
do_render (int *argc,
@ -38,7 +99,10 @@ do_render (int *argc,
const char *fill = "winding";
const char *fg_color = "black";
const char *bg_color = "white";
const char *point_color = "red";
gboolean do_stroke = FALSE;
gboolean show_points = FALSE;
gboolean show_controls = FALSE;
double line_width = 1;
const char *cap = "butt";
const char *join = "miter";
@ -52,9 +116,12 @@ do_render (int *argc,
const GOptionEntry entries[] = {
{ "fill", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &do_stroke, N_("Fill the path (the default)"), NULL },
{ "stroke", 0, 0, G_OPTION_ARG_NONE, &do_stroke, N_("Stroke the path"), NULL },
{ "points", 0, 0, G_OPTION_ARG_NONE, &show_points, N_("Show path points"), NULL },
{ "controls", 0, 0, G_OPTION_ARG_NONE, &show_controls, N_("Show control points"), NULL },
{ "output", 0, 0, G_OPTION_ARG_FILENAME, &output_file, N_("The output file"), N_("FILE") },
{ "fg-color", 0, 0, G_OPTION_ARG_STRING, &fg_color, N_("Foreground color"), N_("COLOR") },
{ "bg-color", 0, 0, G_OPTION_ARG_STRING, &bg_color, N_("Background color"), N_("COLOR") },
{ "point-color", 0, 0, G_OPTION_ARG_STRING, &point_color, N_("Point color"), N_("COLOR") },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL, N_("PATH") },
{ NULL, }
};
@ -73,10 +140,12 @@ do_render (int *argc,
};
GskPath *path;
GskPath *line_path = NULL;
GskPath *point_path = NULL;
GskFillRule fill_rule;
GdkRGBA fg, bg;
GdkRGBA fg, bg, pc;
graphene_rect_t bounds;
GskRenderNode *fg_node, *nodes[2], *node;
GskRenderNode *fg_node, *pc_node, *nodes[5], *node;
GdkSurface *surface;
GskRenderer *renderer;
GdkTexture *texture;
@ -84,6 +153,8 @@ do_render (int *argc,
GskLineCap line_cap;
GskLineJoin line_join;
GskStroke *stroke;
GskPathBuilder *builders[3] = { NULL, NULL, NULL };
int i;
if (gdk_display_get_default () == NULL)
{
@ -137,9 +208,49 @@ do_render (int *argc,
path = get_path (args[0]);
if (show_controls)
{
builders[0] = gsk_path_builder_new ();
builders[1] = gsk_path_builder_new ();
builders[2] = gsk_path_builder_new ();
}
else if (show_points)
{
builders[1] = gsk_path_builder_new ();
}
if (builders[0] || builders[1] || builders[2])
{
gsk_path_foreach (path, -1, collect_cb, builders);
if (builders[0])
line_path = gsk_path_builder_free_to_path (builders[0]);
if (builders[1] || builders[2])
{
GskPath *p1 = NULL;
if (builders[1])
p1 = gsk_path_builder_free_to_path (builders[1]);
if (builders[2])
{
if (p1)
{
gsk_path_builder_add_path (builders[2], p1);
gsk_path_unref (p1);
}
point_path = gsk_path_builder_free_to_path (builders[2]);
}
else
point_path = p1;
}
}
fill_rule = get_enum_value (GSK_TYPE_FILL_RULE, _("fill rule"), fill);
get_color (&fg, fg_color);
get_color (&bg, bg_color);
get_color (&pc, point_color);
line_cap = get_enum_value (GSK_TYPE_LINE_CAP, _("line cap"), cap);
line_join = get_enum_value (GSK_TYPE_LINE_JOIN, _("line join"), join);
@ -157,18 +268,36 @@ do_render (int *argc,
gsk_path_get_bounds (path, &bounds);
graphene_rect_inset (&bounds, -10, -10);
nodes[0] = gsk_color_node_new (&bg, &bounds);
fg_node = gsk_color_node_new (&fg, &bounds);
if (do_stroke)
nodes[1] = gsk_stroke_node_new (fg_node, path, stroke);
else
nodes[1] = gsk_fill_node_new (fg_node, path, fill_rule);
pc_node = gsk_color_node_new (&pc, &bounds);
node = gsk_container_node_new (nodes, 2);
i = 0;
nodes[i++] = gsk_color_node_new (&bg, &bounds);
if (line_path)
{
GskStroke *line_stroke = gsk_stroke_new (1);
gsk_stroke_set_dash (line_stroke, (const float[]) { 1, 1 }, 2);
nodes[i++] = gsk_stroke_node_new (fg_node, line_path, line_stroke);
gsk_stroke_free (line_stroke);
}
if (point_path)
{
nodes[i++] = gsk_fill_node_new (pc_node, point_path, GSK_FILL_RULE_WINDING);
nodes[i++] = gsk_stroke_node_new (fg_node, point_path, stroke);
}
if (do_stroke)
nodes[i++] = gsk_stroke_node_new (fg_node, path, stroke);
else
nodes[i++] = gsk_fill_node_new (fg_node, path, fill_rule);
node = gsk_container_node_new (nodes, i);
gsk_render_node_unref (fg_node);
gsk_render_node_unref (nodes[0]);
gsk_render_node_unref (nodes[1]);
gsk_render_node_unref (pc_node);
for (i--; i >= 0; i--)
gsk_render_node_unref (nodes[i]);
surface = gdk_surface_new_toplevel (gdk_display_get_default ());
renderer = gsk_renderer_new_for_surface (surface);
@ -197,6 +326,8 @@ do_render (int *argc,
gsk_render_node_unref (node);
gsk_path_unref (path);
g_clear_pointer (&line_path, gsk_path_unref);
g_clear_pointer (&point_path, gsk_path_unref);
g_strfreev (args);
}