From 68dc012b32eb79ed8ebac730484d69c473306f7f Mon Sep 17 00:00:00 2001 From: KitsuneAlex Date: Tue, 14 May 2024 01:58:08 +0200 Subject: [PATCH] Add msdf_shape_remove_contour and msdf_contour_remove_edge functions --- core/msdfgen-c.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ msdfgen-c.h | 16 ++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/core/msdfgen-c.cpp b/core/msdfgen-c.cpp index 38e2ed9..e308f3b 100644 --- a/core/msdfgen-c.cpp +++ b/core/msdfgen-c.cpp @@ -176,6 +176,28 @@ MSDF_API int msdf_shape_add_contour(msdf_shape_handle shape, msdf_contour_const_ return MSDF_SUCCESS; } +MSDF_API int msdf_shape_remove_contour(msdf_shape_handle shape, msdf_contour_const_handle contour) { + if(shape == nullptr || contour == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + auto& contours = reinterpret_cast(shape)->contours;//NOLINT + ptrdiff_t index = 0; + bool found = false; + for(const auto& contour_ref : contours) { + if(&contour_ref != reinterpret_cast(contour)) { + ++index; + continue; + } + found = true; + break; + } + if(!found) { + return MSDF_ERR_INVALID_ARG; + } + contours.erase(contours.cbegin() + index); + return MSDF_SUCCESS; +} + MSDF_API int msdf_shape_get_contour_count(msdf_shape_const_handle shape, size_t* contour_count) { if(shape == nullptr || contour_count == nullptr) { return MSDF_ERR_INVALID_ARG; @@ -298,6 +320,28 @@ MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_segment_han return MSDF_SUCCESS; } +MSDF_API int msdf_contour_remove_edge(msdf_contour_handle contour, msdf_segment_handle segment) { + if(contour == nullptr || segment == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + auto& edges = reinterpret_cast(contour)->edges; + ptrdiff_t index = 0; + bool found = false; + for(const msdfgen::EdgeSegment* edge : edges) { + if(edge != reinterpret_cast(segment)) { + ++index; + continue; + } + found = true; + break; + } + if(!found) { + return MSDF_ERR_INVALID_ARG; + } + edges.erase(edges.cbegin() + index); + return MSDF_SUCCESS; +} + MSDF_API int msdf_contour_get_edge_count(msdf_contour_const_handle contour, size_t* edge_count) { if(contour == nullptr || edge_count == nullptr) { return MSDF_ERR_INVALID_ARG; diff --git a/msdfgen-c.h b/msdfgen-c.h index e896e47..f7b37b5 100644 --- a/msdfgen-c.h +++ b/msdfgen-c.h @@ -197,6 +197,14 @@ MSDF_API int msdf_shape_get_bounds(msdf_shape_const_handle shape, msdf_bounds_t* */ MSDF_API int msdf_shape_add_contour(msdf_shape_handle shape, msdf_contour_const_handle contour); +/** + * Removes the given contour from the given shape if present. + * @param shape A pointer to a shape object to remove the given contour from. + * @param contour A pointer to the contour to remove from the shape. + * @returns @code MSDF_SUCCESS@endcode on success, otherwise one of the constants prefixed with @code MSDF_ERR_@endcode. + */ +MSDF_API int msdf_shape_remove_contour(msdf_shape_handle shape, msdf_contour_const_handle contour); + /** * Retrieves the number of contours allocated within the given shape object. * @param shape A pointer to a shape object from which to retrieve the contour count. @@ -325,6 +333,14 @@ MSDF_API int msdf_contour_alloc(msdf_contour_handle* contour); */ MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_segment_handle segment); +/** + * Removes the given edge from the given contour if present. + * @param contour A pointer to the contour to remove the given edge (segment) from. + * @param segment A pointer to the segment to remove from the given contour. + * @returns @code MSDF_SUCCESS@endcode on success, otherwise one of the constants prefixed with @code MSDF_ERR_@endcode. + */ +MSDF_API int msdf_contour_remove_edge(msdf_contour_handle contour, msdf_segment_handle segment); + /** * Retrieves the edge count of the given contour. * @param contour A pointer to the contour to retrieve the edge count from.