From 21245a872956857b3542b37473307eb886aecfc3 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 21 Aug 2023 21:19:13 -0400 Subject: [PATCH] Make gsk_path_builder_add_contour public This allows to split a path into contours without losing their closedness. --- gsk/gskpathbuilder.c | 61 ++++++++++++++++++++++++++++++++++---------- gsk/gskpathbuilder.h | 6 +++++ gsk/gskpathprivate.h | 4 --- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/gsk/gskpathbuilder.c b/gsk/gskpathbuilder.c index 9a63e1f2b1..947e9ad4d5 100644 --- a/gsk/gskpathbuilder.c +++ b/gsk/gskpathbuilder.c @@ -180,6 +180,9 @@ gsk_path_builder_append_current (GskPathBuilder *self, self->current_point = points[n_points - 1]; } +static void add_contour (GskPathBuilder *self, + GskContour *contour); + static void gsk_path_builder_end_current (GskPathBuilder *self) { @@ -199,7 +202,16 @@ gsk_path_builder_end_current (GskPathBuilder *self) g_array_set_size (self->points, 0); /* do this at the end to avoid inflooping when add_contour calls back here */ - gsk_path_builder_add_contour (self, contour); + add_contour (self, contour); +} + +static void +add_contour (GskPathBuilder *self, + GskContour *contour) +{ + gsk_path_builder_end_current (self); + + self->contours = g_slist_prepend (self->contours, contour); } static void @@ -298,15 +310,6 @@ gsk_path_builder_to_path (GskPathBuilder *self) return path; } -void -gsk_path_builder_add_contour (GskPathBuilder *self, - GskContour *contour) -{ - gsk_path_builder_end_current (self); - - self->contours = g_slist_prepend (self->contours, contour); -} - /** * gsk_path_builder_get_current_point: * @self: a `GskPathBuilder` @@ -350,7 +353,7 @@ gsk_path_builder_add_path (GskPathBuilder *self, { const GskContour *contour = gsk_path_get_contour (path, i); - gsk_path_builder_add_contour (self, gsk_contour_dup (contour)); + add_contour (self, gsk_contour_dup (contour)); } } @@ -374,7 +377,7 @@ gsk_path_builder_add_reverse_path (GskPathBuilder *self, { const GskContour *contour = gsk_path_get_contour (path, i - 1); - gsk_path_builder_add_contour (self, gsk_contour_reverse (contour)); + add_contour (self, gsk_contour_reverse (contour)); } } @@ -1115,7 +1118,8 @@ gsk_path_builder_add_layout (GskPathBuilder *self, * will be connected. * * Note that this method always adds a path with the given start point - * and end point. To add a closed path, use [method@Gsk.PathBuilder.add_path]. + * and end point. To add a closed path, use [method@Gsk.PathBuilder.add_path] + * of [method@Gsk.PathBuilder.add_contour]. * * Since: 4.14 */ @@ -1170,7 +1174,11 @@ gsk_path_builder_add_segment (GskPathBuilder *self, &(GskRealPathPoint) { s->contour, n_ops - 1, 1. }); for (gsize i = (s->contour + 1) % n_contours; i != e->contour; i = (i + 1) % n_contours) - gsk_path_builder_add_contour (self, gsk_contour_dup (gsk_path_get_contour (path, i))); + { + const GskContour *contour = gsk_path_get_contour (path, i); + + add_contour (self, gsk_contour_dup (contour)); + } contour = gsk_path_get_contour (path, e->contour); n_ops = gsk_contour_get_n_ops (contour); @@ -1184,3 +1192,28 @@ out: gsk_path_builder_end_current (self); self->current_point = current; } + +/** + * gsk_path_builder_add_contour: + * @self: a `GskPathBuilder` + * @path: the `GskPath` to take the contour from + * @contour: the index of the contour to take + * + * Adds one of the connected contours of @path to @self. + * + * Use [method@Gsk.Path.get_n_contours] to find out how + * many contours a path has. + * + * Since: 4.14 + */ +void +gsk_path_builder_add_contour (GskPathBuilder *self, + GskPath *path, + gsize contour) +{ + g_return_if_fail (self != NULL); + g_return_if_fail (path != NULL); + g_return_if_fail (contour < gsk_path_get_n_contours (path)); + + add_contour (self, gsk_contour_dup (gsk_path_get_contour (path, contour))); +} diff --git a/gsk/gskpathbuilder.h b/gsk/gskpathbuilder.h index 5eda90b00a..a4a99b41fb 100644 --- a/gsk/gskpathbuilder.h +++ b/gsk/gskpathbuilder.h @@ -76,6 +76,12 @@ void gsk_path_builder_add_segment (GskPathBuilder GskPath *path, const GskPathPoint *start, const GskPathPoint *end); + +GDK_AVAILABLE_IN_4_14 +void gsk_path_builder_add_contour (GskPathBuilder *self, + GskPath *path, + gsize contour); + GDK_AVAILABLE_IN_4_14 void gsk_path_builder_move_to (GskPathBuilder *self, float x, diff --git a/gsk/gskpathprivate.h b/gsk/gskpathprivate.h index ed14663bfb..84dfcc1a1d 100644 --- a/gsk/gskpathprivate.h +++ b/gsk/gskpathprivate.h @@ -51,10 +51,6 @@ gboolean gsk_path_foreach_with_tolerance (GskPath GskPathForeachFunc func, gpointer user_data); - -void gsk_path_builder_add_contour (GskPathBuilder *builder, - GskContour *contour); - void gsk_path_builder_svg_arc_to (GskPathBuilder *builder, float rx, float ry,