From 3965ee59cdb10e93adabd01bcb9250751c3fcf7c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 22 Jun 2023 22:42:27 -0400 Subject: [PATCH] API: Add gsk_path_offset Add a function that takes a path, and offsets it by some distance, applying line-join parameters as needed. This commit just adds the api, the implementation will be in the following commit. --- gsk/gskcontour.c | 45 +++++++++++++++++++++++++++++++++++++++++ gsk/gskcontourprivate.h | 6 ++++++ gsk/gskpath.c | 32 +++++++++++++++++++++++++++++ gsk/gskpath.h | 6 ++++++ 4 files changed, 89 insertions(+) diff --git a/gsk/gskcontour.c b/gsk/gskcontour.c index e608f01c10..2011fc725a 100644 --- a/gsk/gskcontour.c +++ b/gsk/gskcontour.c @@ -93,6 +93,11 @@ struct _GskContourClass void (* add_stroke) (const GskContour *contour, GskPathBuilder *builder, GskStroke *stroke); + void (* offset) (const GskContour *contour, + GskPathBuilder *builder, + float distance, + GskLineJoin line_join, + float miter_limit); }; static gsize @@ -520,6 +525,15 @@ gsk_rect_contour_add_stroke (const GskContour *contour, { } +static void +gsk_rect_contour_offset (const GskContour *contour, + GskPathBuilder *builder, + float distance, + GskLineJoin line_join, + float miter_limit) +{ +} + static const GskContourClass GSK_RECT_CONTOUR_CLASS = { sizeof (GskRectContour), @@ -541,6 +555,7 @@ static const GskContourClass GSK_RECT_CONTOUR_CLASS = gsk_rect_contour_get_stroke_bounds, gsk_rect_contour_reverse, gsk_rect_contour_add_stroke, + gsk_rect_contour_offset, }; GskContour * @@ -954,6 +969,15 @@ gsk_circle_contour_add_stroke (const GskContour *contour, { } +static void +gsk_circle_contour_offset (const GskContour *contour, + GskPathBuilder *builder, + float distance, + GskLineJoin line_join, + float miter_limit) +{ +} + static const GskContourClass GSK_CIRCLE_CONTOUR_CLASS = { sizeof (GskCircleContour), @@ -975,6 +999,7 @@ static const GskContourClass GSK_CIRCLE_CONTOUR_CLASS = gsk_circle_contour_get_stroke_bounds, gsk_circle_contour_reverse, gsk_circle_contour_add_stroke, + gsk_circle_contour_offset, }; GskContour * @@ -1819,6 +1844,15 @@ gsk_standard_contour_add_stroke (const GskContour *contour, { } +static void +gsk_standard_contour_offset (const GskContour *contour, + GskPathBuilder *builder, + float distance, + GskLineJoin line_join, + float miter_limit) +{ +} + static const GskContourClass GSK_STANDARD_CONTOUR_CLASS = { sizeof (GskStandardContour), @@ -1840,6 +1874,7 @@ static const GskContourClass GSK_STANDARD_CONTOUR_CLASS = gsk_standard_contour_get_stroke_bounds, gsk_standard_contour_reverse, gsk_standard_contour_add_stroke, + gsk_standard_contour_offset, }; /* You must ensure the contour has enough size allocated, @@ -2027,6 +2062,16 @@ gsk_contour_add_stroke (const GskContour *self, self->klass->add_stroke (self, builder, stroke); } +void +gsk_contour_offset (const GskContour *self, + GskPathBuilder *builder, + float distance, + GskLineJoin line_join, + float miter_limit) +{ + self->klass->offset (self, builder, distance, line_join, miter_limit); +} + void gsk_contour_copy (GskContour *dest, const GskContour *src) diff --git a/gsk/gskcontourprivate.h b/gsk/gskcontourprivate.h index 7ec007c036..93e9102bb3 100644 --- a/gsk/gskcontourprivate.h +++ b/gsk/gskcontourprivate.h @@ -107,6 +107,12 @@ void gsk_contour_default_add_stroke (const GskContou GskPathBuilder *builder, GskStroke *stroke); +void gsk_contour_offset (const GskContour *contour, + GskPathBuilder *builder, + float distance, + GskLineJoin line_join, + float miter_limit); + G_END_DECLS #endif /* __GSK_CONTOUR_PRIVATE_H__ */ diff --git a/gsk/gskpath.c b/gsk/gskpath.c index f155fa14e9..8ddddac551 100644 --- a/gsk/gskpath.c +++ b/gsk/gskpath.c @@ -1394,3 +1394,35 @@ gsk_path_stroke (GskPath *self, return gsk_path_builder_free_to_path (builder); } + +/** + * gsk_path_offset: + * @self: a `GskPath` + * @distance: the distance to offset the path by + * @line_join: how joins are supposed to be drawn + * @miter_limit: miter limit for joins + * + * Create a new path that is offset from @self by @distance. + * + * The offset can be positive (to the right) or negative + * (to the left). The @line_join and @miter_limit arguments + * influence how joins between the offset path segments + * are made. + * + * Returns: a new `GskPath` + */ +GskPath * +gsk_path_offset (GskPath *self, + float distance, + GskLineJoin line_join, + float miter_limit) +{ + GskPathBuilder *builder; + + builder = gsk_path_builder_new (); + + for (int i = 0; i < self->n_contours; i++) + gsk_contour_offset (gsk_path_get_contour (self, i), builder, distance, line_join, miter_limit); + + return gsk_path_builder_free_to_path (builder); +} diff --git a/gsk/gskpath.h b/gsk/gskpath.h index a28f3544d8..3c62c52d82 100644 --- a/gsk/gskpath.h +++ b/gsk/gskpath.h @@ -121,6 +121,12 @@ GDK_AVAILABLE_IN_ALL GskPath * gsk_path_stroke (GskPath *self, GskStroke *stroke); +GDK_AVAILABLE_IN_ALL +GskPath * gsk_path_offset (GskPath *self, + float distance, + GskLineJoin line_join, + float miter_limit); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskPath, gsk_path_unref) G_END_DECLS