From ad0aef556dfec92ad3c11e129b942a3eb49bebbf Mon Sep 17 00:00:00 2001 From: KitsuneAlex Date: Fri, 3 May 2024 22:55:47 +0200 Subject: [PATCH] Finalize API, use handles for segments, don't expose edges in C --- README.md | 23 ++--- core/msdfgen_c.cpp | 223 +++++++++++++++++++++++---------------------- msdfgen_c.h | 81 ++++++++-------- 3 files changed, 162 insertions(+), 165 deletions(-) diff --git a/README.md b/README.md index 0de3527..32a76f6 100644 --- a/README.md +++ b/README.md @@ -148,21 +148,16 @@ int main() { ```c++ #include -inline void create_shape(msdf_shape_handle* shape, char value) { +int main(int num_args, char** args) { + msdf_bitmap_t bitmap; + if(msdf_bitmap_alloc(MSDF_BITMAP_TYPE_MSDF, 16, 16, &bitmap) != MSDF_SUCCESS) { + return 1; + } msdf_shape_handle shape = NULL; if(msdf_shape_alloc(&shape) != MSDF_SUCCESS) { return; // Handle error accordingly } // Create shape data from font using FreeType, STB etc here.. -} - -int main(int num_args, char** args) { - msdf_bitmap_t* bitmap = NULL; - if(msdf_bitmap_alloc(MSDF_BITMAP_TYPE_MSDF, 16, 16, &bitmap) != MSDF_SUCCESS) { - return 1; - } - msdf_shape_handle shape = NULL; - create_shape(&shape, 'A'); msdf_transform_t transform; transform.scale.x = 1.0; transform.scale.y = 1.0; @@ -170,15 +165,13 @@ int main(int num_args, char** args) { transform.translation.y = 4.0; transform.distance_mapping.lower = 4.0; transform.distance_mapping.upper = 4.0; - if(msdf_generate_msdf(bitmap, shape, &transform) != MSDF_SUCCESS) { + if(msdf_generate_msdf(&bitmap, shape, &transform) != MSDF_SUCCESS) { return 1; } - - // DO SOMETHING WITH THE GENERATED GLYPH BITMAP HERE.. - + // DO SOMETHING WITH THE GENERATED GLYPH BITMAP HERE.. // Make sure to free heap allocated resources when we're done msdf_shape_free(shape); - msdf_bitmap_free(bitmap); + msdf_bitmap_free(&bitmap); return 0; } ``` diff --git a/core/msdfgen_c.cpp b/core/msdfgen_c.cpp index 9d8cd28..bc6122c 100644 --- a/core/msdfgen_c.cpp +++ b/core/msdfgen_c.cpp @@ -82,34 +82,32 @@ MSDF_API const msdf_allocator_t* msdf_allocator_get() { // msdf_bitmap -MSDF_API int msdf_bitmap_alloc(const int type, const int width, const int height, msdf_bitmap_t** bitmap) { +MSDF_API int msdf_bitmap_alloc(const int type, const int width, const int height, msdf_bitmap_t* bitmap) { if(width < 0 || height < 0) { return MSDF_ERR_INVALID_SIZE; } if(bitmap == nullptr) { return MSDF_ERR_INVALID_ARG; } - auto* wrapper = msdf_alloc(); - wrapper->type = type; - wrapper->width = width; - wrapper->height = height; + bitmap->type = type; + bitmap->width = width; + bitmap->height = height; switch(type) { case MSDF_BITMAP_TYPE_SDF: - wrapper->handle = msdf_new(width, height); + bitmap->handle = msdf_new(width, height); break; case MSDF_BITMAP_TYPE_PSDF: - wrapper->handle = msdf_new(width, height); + bitmap->handle = msdf_new(width, height); break; case MSDF_BITMAP_TYPE_MSDF: - wrapper->handle = msdf_new(width, height); + bitmap->handle = msdf_new(width, height); break; case MSDF_BITMAP_TYPE_MTSDF: - wrapper->handle = msdf_new(width, height); + bitmap->handle = msdf_new(width, height); break; default: return MSDF_ERR_INVALID_ARG; } - *bitmap = wrapper; return MSDF_SUCCESS; } @@ -131,7 +129,7 @@ MSDF_API int msdf_bitmap_get_channel_count(const msdf_bitmap_t* bitmap, int* cha return MSDF_SUCCESS; } -MSDF_API int msdf_bitmap_get_size(const msdf_bitmap_t* bitmap, size_t* size) { +MSDF_API int msdf_bitmap_get_byte_size(const msdf_bitmap_t* bitmap, size_t* size) { if(bitmap == nullptr || size == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -200,11 +198,11 @@ MSDF_API int msdf_shape_alloc(msdf_shape_handle* shape) { return MSDF_SUCCESS; } -MSDF_API int msdf_shape_get_bounds(msdf_shape_handle shape, msdf_bounds_t* bounds) { +MSDF_API int msdf_shape_get_bounds(msdf_shape_const_handle shape, msdf_bounds_t* bounds) { if(shape == nullptr || bounds == nullptr) { return MSDF_ERR_INVALID_ARG; } - *reinterpret_cast(bounds) = reinterpret_cast(shape)->getBounds(); + *reinterpret_cast(bounds) = reinterpret_cast(shape)->getBounds(); return MSDF_SUCCESS; } @@ -212,38 +210,39 @@ MSDF_API int msdf_shape_add_contour(msdf_shape_handle shape, msdf_contour_handle if(shape == nullptr || contour == nullptr) { return MSDF_ERR_INVALID_ARG; } + *contour = reinterpret_cast(&reinterpret_cast(shape)->addContour()); return MSDF_SUCCESS; } -MSDF_API int msdf_shape_get_contour_count(msdf_shape_handle shape, size_t* contour_count) { +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; } - *contour_count = reinterpret_cast(shape)->contours.size(); + *contour_count = reinterpret_cast(shape)->contours.size(); return MSDF_SUCCESS; } -MSDF_API int msdf_shape_get_contour(msdf_shape_handle shape, const size_t index, msdf_contour_handle* contours) { - if(shape == nullptr || contours == nullptr) { +MSDF_API int msdf_shape_get_contour(msdf_shape_const_handle shape, const size_t index, msdf_contour_const_handle* contour) { + if(shape == nullptr || contour == nullptr) { return MSDF_ERR_INVALID_ARG; } - *contours = reinterpret_cast(&reinterpret_cast(shape)->contours[index]); + *contour = reinterpret_cast(&reinterpret_cast(shape)->contours[index]); return MSDF_SUCCESS; } -MSDF_API int msdf_shape_get_edge_count(msdf_shape_handle shape, size_t* edge_count) { +MSDF_API int msdf_shape_get_edge_count(msdf_shape_const_handle shape, size_t* edge_count) { if(shape == nullptr || edge_count == nullptr) { return MSDF_ERR_INVALID_ARG; } - *edge_count = reinterpret_cast(shape)->edgeCount(); + *edge_count = reinterpret_cast(shape)->edgeCount(); return MSDF_SUCCESS; } -MSDF_API int msdf_shape_has_inverse_y_axis(msdf_shape_handle shape, int* inverse_y_axis) { +MSDF_API int msdf_shape_has_inverse_y_axis(msdf_shape_const_handle shape, int* inverse_y_axis) { if(shape == nullptr || inverse_y_axis == nullptr) { return MSDF_ERR_INVALID_ARG; } - *inverse_y_axis = reinterpret_cast(shape)->inverseYAxis ? MSDF_TRUE : MSDF_FALSE; + *inverse_y_axis = reinterpret_cast(shape)->inverseYAxis ? MSDF_TRUE : MSDF_FALSE; return MSDF_SUCCESS; } @@ -297,27 +296,29 @@ MSDF_API int msdf_contour_alloc(msdf_contour_handle* contour) { return MSDF_SUCCESS; } -MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_edge_holder_handle* edge) { - if(contour == nullptr || edge == nullptr) { +MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_segment_handle* segment) { + if(contour == nullptr || segment == nullptr) { return MSDF_ERR_INVALID_ARG; } - *edge = reinterpret_cast(&reinterpret_cast(contour)->addEdge()); + msdfgen::EdgeSegment* p_segment = reinterpret_cast(contour)->addEdge(); + *segment = reinterpret_cast(p_segment); return MSDF_SUCCESS; } -MSDF_API int msdf_contour_get_edge_count(msdf_contour_handle contour, size_t* edge_count) { +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; } - *edge_count = reinterpret_cast(contour)->edges.size(); + *edge_count = reinterpret_cast(contour)->edges.size(); return MSDF_SUCCESS; } -MSDF_API int msdf_contour_get_edge(msdf_contour_handle contour, const size_t index, msdf_edge_holder_handle* edges) { - if(contour == nullptr || edges == nullptr) { +MSDF_API int msdf_contour_get_edge(msdf_contour_const_handle contour, const size_t index, msdf_segment_const_handle* segment) { + if(contour == nullptr || segment == nullptr) { return MSDF_ERR_INVALID_ARG; } - *edges = reinterpret_cast(&reinterpret_cast(contour)->edges[index]); + const msdfgen::EdgeSegment* p_segment = reinterpret_cast(contour)->edges[index]; + *segment = reinterpret_cast(p_segment); return MSDF_SUCCESS; } @@ -341,11 +342,11 @@ MSDF_API int msdf_contour_bound_miters(msdf_contour_handle contour, return MSDF_SUCCESS; } -MSDF_API int msdf_contour_get_winding(msdf_contour_handle contour, int* winding) { +MSDF_API int msdf_contour_get_winding(msdf_contour_const_handle contour, int* winding) { if(contour == nullptr || winding == nullptr) { return MSDF_ERR_INVALID_ARG; } - *winding = reinterpret_cast(contour)->winding(); + *winding = reinterpret_cast(contour)->winding(); return MSDF_SUCCESS; } @@ -361,61 +362,65 @@ MSDF_API void msdf_contour_free(msdf_contour_handle contour) { msdf_delete(reinterpret_cast(contour)); } -// msdf_edge_holder - -int msdf_edge_alloc(msdf_segment_t* segment, msdf_edge_holder_handle* edge) { - if(edge == nullptr || segment == nullptr) { - return MSDF_ERR_INVALID_ARG; - } - *edge = reinterpret_cast(msdf_new(static_cast(segment->handle))); - return MSDF_SUCCESS; -} - -void msdf_edge_free(msdf_edge_holder_handle edge) { - msdf_delete(reinterpret_cast(edge)); -} - // msdf_segment -MSDF_API int msdf_segment_alloc(const int type, msdf_segment_t** segment) { +MSDF_API int msdf_segment_alloc(const int type, msdf_segment_handle* segment) { if(type < 0 || type >= MSDF_SEGMENT_TYPE_MAX) { return MSDF_ERR_INVALID_TYPE; } if(segment == nullptr) { return MSDF_ERR_INVALID_ARG; } - auto* wrapper = msdf_alloc(); - wrapper->type = type; switch(type) { case MSDF_SEGMENT_TYPE_LINEAR: - wrapper->handle = msdf_new(msdfgen::Point2 {}, msdfgen::Point2 {}); + *segment = reinterpret_cast(msdf_new(msdfgen::Point2 {}, msdfgen::Point2 {})); break; case MSDF_SEGMENT_TYPE_QUADRATIC: - wrapper->handle = msdf_new(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}); + *segment = reinterpret_cast( + msdf_new(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {})); break; case MSDF_SEGMENT_TYPE_CUBIC: - wrapper->handle = - msdf_new(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}); + *segment = reinterpret_cast( + msdf_new(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {})); break; default: return MSDF_ERR_INVALID_ARG; } - *segment = wrapper; return MSDF_SUCCESS; } -MSDF_API int msdf_segment_get_point_count(const msdf_segment_t* segment, size_t* point_count) { +MSDF_API int msdf_segment_get_type(msdf_segment_const_handle segment, int* type) { + if(segment == nullptr || type == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + switch(reinterpret_cast(segment)->type()) { + case msdfgen::LinearSegment::EdgeType::EDGE_TYPE: + *type = MSDF_SEGMENT_TYPE_LINEAR; + break; + case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE: + *type = MSDF_SEGMENT_TYPE_QUADRATIC; + break; + case msdfgen::CubicSegment::EdgeType::EDGE_TYPE: + *type = MSDF_SEGMENT_TYPE_CUBIC; + break; + default: + return MSDF_ERR_INVALID_TYPE; + } + return MSDF_SUCCESS; +} + +MSDF_API int msdf_segment_get_point_count(msdf_segment_const_handle segment, size_t* point_count) { if(segment == nullptr || point_count == nullptr) { return MSDF_ERR_INVALID_ARG; } - switch(segment->type) { - case MSDF_SEGMENT_TYPE_LINEAR: + switch(reinterpret_cast(segment)->type()) { + case msdfgen::LinearSegment::EdgeType::EDGE_TYPE: *point_count = 2; break; - case MSDF_SEGMENT_TYPE_QUADRATIC: + case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE: *point_count = 3; break; - case MSDF_SEGMENT_TYPE_CUBIC: + case msdfgen::CubicSegment::EdgeType::EDGE_TYPE: *point_count = 4; break; default: @@ -425,7 +430,7 @@ MSDF_API int msdf_segment_get_point_count(const msdf_segment_t* segment, size_t* return MSDF_SUCCESS; } -MSDF_API int msdf_segment_get_point(const msdf_segment_t* segment, const size_t index, msdf_vector2_t* point) { +MSDF_API int msdf_segment_get_point(msdf_segment_const_handle segment, const size_t index, msdf_vector2_t* point) { if(segment == nullptr || point == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -436,15 +441,16 @@ MSDF_API int msdf_segment_get_point(const msdf_segment_t* segment, const size_t if(index >= point_count) { return MSDF_ERR_INVALID_INDEX; } - switch(segment->type) { - case MSDF_SEGMENT_TYPE_LINEAR: - *reinterpret_cast(point) = static_cast(segment->handle)->p[index]; + const auto* p_segment = reinterpret_cast(segment); + switch(p_segment->type()) { + case msdfgen::LinearSegment::EdgeType::EDGE_TYPE: + *reinterpret_cast(point) = dynamic_cast(p_segment)->p[index]; break; - case MSDF_SEGMENT_TYPE_QUADRATIC: - *reinterpret_cast(point) = static_cast(segment->handle)->p[index]; + case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE: + *reinterpret_cast(point) = dynamic_cast(p_segment)->p[index]; break; - case MSDF_SEGMENT_TYPE_CUBIC: - *reinterpret_cast(point) = static_cast(segment->handle)->p[index]; + case msdfgen::CubicSegment::EdgeType::EDGE_TYPE: + *reinterpret_cast(point) = dynamic_cast(p_segment)->p[index]; break; default: return MSDF_ERR_INVALID_ARG; @@ -452,7 +458,7 @@ MSDF_API int msdf_segment_get_point(const msdf_segment_t* segment, const size_t return MSDF_SUCCESS; } -MSDF_API int msdf_segment_set_point(msdf_segment_t* segment, const size_t index, const msdf_vector2_t* point) { +MSDF_API int msdf_segment_set_point(msdf_segment_handle segment, const size_t index, const msdf_vector2_t* point) { if(segment == nullptr || point == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -463,15 +469,16 @@ MSDF_API int msdf_segment_set_point(msdf_segment_t* segment, const size_t index, if(index >= point_count) { return MSDF_ERR_INVALID_INDEX; } - switch(segment->type) { - case MSDF_SEGMENT_TYPE_LINEAR: - static_cast(segment->handle)->p[index] = *reinterpret_cast(point); + auto* p_segment = reinterpret_cast(segment); + switch(p_segment->type()) { + case msdfgen::LinearSegment::EdgeType::EDGE_TYPE: + dynamic_cast(p_segment)->p[index] = *reinterpret_cast(point); break; - case MSDF_SEGMENT_TYPE_QUADRATIC: - static_cast(segment->handle)->p[index] = *reinterpret_cast(point); + case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE: + dynamic_cast(p_segment)->p[index] = *reinterpret_cast(point); break; - case MSDF_SEGMENT_TYPE_CUBIC: - static_cast(segment->handle)->p[index] = *reinterpret_cast(point); + case msdfgen::CubicSegment::EdgeType::EDGE_TYPE: + dynamic_cast(p_segment)->p[index] = *reinterpret_cast(point); break; default: return MSDF_ERR_INVALID_ARG; @@ -479,90 +486,90 @@ MSDF_API int msdf_segment_set_point(msdf_segment_t* segment, const size_t index, return MSDF_SUCCESS; } -MSDF_API int msdf_segment_set_color(msdf_segment_t* segment, int color) { +MSDF_API int msdf_segment_set_color(msdf_segment_handle segment, int color) { if(segment == nullptr) { return MSDF_ERR_INVALID_ARG; } - static_cast(segment->handle)->color = static_cast(color); + reinterpret_cast(segment)->color = static_cast(color); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_get_color(const msdf_segment_t* segment, int* color) { +MSDF_API int msdf_segment_get_color(msdf_segment_const_handle segment, int* color) { if(segment == nullptr || color == nullptr) { return MSDF_ERR_INVALID_ARG; } - *color = static_cast(static_cast(segment->handle)->color); + *color = static_cast(reinterpret_cast(segment)->color); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_get_direction(const msdf_segment_t* segment, double param, msdf_vector2_t* direction) { +MSDF_API int msdf_segment_get_direction(msdf_segment_const_handle segment, const double param, msdf_vector2_t* direction) { if(segment == nullptr || direction == nullptr) { return MSDF_ERR_INVALID_ARG; } - *reinterpret_cast(direction) = static_cast(segment->handle)->direction(param); + *reinterpret_cast(direction) = reinterpret_cast(segment)->direction(param); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_get_direction_change(const msdf_segment_t* segment, double param, msdf_vector2_t* direction_change) { +MSDF_API int msdf_segment_get_direction_change(msdf_segment_const_handle segment, double param, msdf_vector2_t* direction_change) { if(segment == nullptr || direction_change == nullptr) { return MSDF_ERR_INVALID_ARG; } - *reinterpret_cast(direction_change) = static_cast(segment->handle)->directionChange(param); + *reinterpret_cast(direction_change) = reinterpret_cast(segment)->directionChange(param); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_point(const msdf_segment_t* segment, double param, msdf_vector2_t* point) { +MSDF_API int msdf_segment_point(msdf_segment_const_handle segment, const double param, msdf_vector2_t* point) { if(segment == nullptr || point == nullptr) { return MSDF_ERR_INVALID_ARG; } - *reinterpret_cast(point) = static_cast(segment->handle)->point(param); + *reinterpret_cast(point) = reinterpret_cast(segment)->point(param); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_bound(const msdf_segment_t* segment, msdf_bounds_t* bounds) { +MSDF_API int msdf_segment_bound(msdf_segment_const_handle segment, msdf_bounds_t* bounds) { if(segment == nullptr || bounds == nullptr) { return MSDF_ERR_INVALID_ARG; } - static_cast(segment->handle)->bound(bounds->l, bounds->b, bounds->r, bounds->t); + reinterpret_cast(segment)->bound(bounds->l, bounds->b, bounds->r, bounds->t); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_move_start_point(msdf_segment_t* segment, const msdf_vector2_t* point) { +MSDF_API int msdf_segment_move_start_point(msdf_segment_handle segment, const msdf_vector2_t* point) { if(segment == nullptr || point == nullptr) { return MSDF_ERR_INVALID_ARG; } - static_cast(segment->handle)->moveStartPoint(*reinterpret_cast(point)); + reinterpret_cast(segment)->moveStartPoint(*reinterpret_cast(point)); return MSDF_SUCCESS; } -MSDF_API int msdf_segment_move_end_point(msdf_segment_t* segment, const msdf_vector2_t* point) { +MSDF_API int msdf_segment_move_end_point(msdf_segment_handle segment, const msdf_vector2_t* point) { if(segment == nullptr || point == nullptr) { return MSDF_ERR_INVALID_ARG; } - static_cast(segment->handle)->moveEndPoint(*reinterpret_cast(point)); + reinterpret_cast(segment)->moveEndPoint(*reinterpret_cast(point)); return MSDF_SUCCESS; } -MSDF_API void msdf_segment_free(msdf_segment_t* segment) { - switch(segment->type) { - case MSDF_SEGMENT_TYPE_LINEAR: - msdf_delete(static_cast(segment->handle)); +MSDF_API void msdf_segment_free(msdf_segment_handle segment) { + auto* p_segment = reinterpret_cast(segment); + switch(p_segment->type()) { + case msdfgen::LinearSegment::EdgeType::EDGE_TYPE: + msdf_delete(dynamic_cast(p_segment)); break; - case MSDF_SEGMENT_TYPE_QUADRATIC: - msdf_delete(static_cast(segment->handle)); + case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE: + msdf_delete(dynamic_cast(p_segment)); break; - case MSDF_SEGMENT_TYPE_CUBIC: - msdf_delete(static_cast(segment->handle)); + case msdfgen::CubicSegment::EdgeType::EDGE_TYPE: + msdf_delete(dynamic_cast(p_segment)); break; default: return; } - msdf_free(segment); } // Main msdfgen APIs -int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform) { +int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -576,7 +583,7 @@ int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf return MSDF_SUCCESS; } -int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform) { +int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -590,7 +597,7 @@ int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msd return MSDF_SUCCESS; } -int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform) { +int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -604,7 +611,7 @@ int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msd return MSDF_SUCCESS; } -int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform) { +int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -619,7 +626,7 @@ int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_handle shape, const ms } int msdf_generate_sdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { @@ -638,7 +645,7 @@ int msdf_generate_sdf_with_config(msdf_bitmap_t* output, } int msdf_generate_psdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { @@ -657,7 +664,7 @@ int msdf_generate_psdf_with_config(msdf_bitmap_t* output, } int msdf_generate_msdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_multichannel_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { @@ -680,7 +687,7 @@ int msdf_generate_msdf_with_config(msdf_bitmap_t* output, } int msdf_generate_mtsdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_multichannel_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { diff --git a/msdfgen_c.h b/msdfgen_c.h index c59bedd..dc36243 100644 --- a/msdfgen_c.h +++ b/msdfgen_c.h @@ -56,7 +56,11 @@ #define MSDF_EDGE_COLOR_CYAN 6 #define MSDF_EDGE_COLOR_WHITE 7 -#define MSDF_DEFINE_HANDLE_TYPE(n) typedef struct n* n##_handle// NOLINT +// NOLINTBEGIN +#define MSDF_DEFINE_HANDLE_TYPE(n) \ + typedef struct n* n##_handle; \ + typedef const struct n* n##_const_handle +// NOLINTEND // Macros for allocating default MSDF bitmap types #define MSDF_ALLOC_SDF_BITMAP(w, h) msdf_bitmap_alloc(MSDF_BITMAP_TYPE_SDF, w, h) @@ -121,33 +125,28 @@ typedef struct msdf_bitmap { void* handle; } msdf_bitmap_t; -typedef struct msdf_segment { - int type; - void* handle; -} msdf_segment_t; - // Opaque handle types MSDF_DEFINE_HANDLE_TYPE(msdf_shape); MSDF_DEFINE_HANDLE_TYPE(msdf_contour); -MSDF_DEFINE_HANDLE_TYPE(msdf_edge_holder); +MSDF_DEFINE_HANDLE_TYPE(msdf_segment); // Exported API functions MSDF_API void msdf_allocator_set(const msdf_allocator_t* allocator); MSDF_API const msdf_allocator_t* msdf_allocator_get(); -MSDF_API int msdf_bitmap_alloc(int type, int width, int height, msdf_bitmap_t** bitmap); +MSDF_API int msdf_bitmap_alloc(int type, int width, int height, msdf_bitmap_t* bitmap); MSDF_API int msdf_bitmap_get_channel_count(const msdf_bitmap_t* bitmap, int* channel_count); MSDF_API int msdf_bitmap_get_pixels(const msdf_bitmap_t* bitmap, void** pixels); -MSDF_API int msdf_bitmap_get_size(const msdf_bitmap_t* bitmap, size_t* size); +MSDF_API int msdf_bitmap_get_byte_size(const msdf_bitmap_t* bitmap, size_t* size); MSDF_API void msdf_bitmap_free(msdf_bitmap_t* bitmap); MSDF_API int msdf_shape_alloc(msdf_shape_handle* shape); -MSDF_API int msdf_shape_get_bounds(msdf_shape_handle shape, msdf_bounds_t* bounds); +MSDF_API int msdf_shape_get_bounds(msdf_shape_const_handle shape, msdf_bounds_t* bounds); MSDF_API int msdf_shape_add_contour(msdf_shape_handle shape, msdf_contour_handle* contour); -MSDF_API int msdf_shape_get_contour_count(msdf_shape_handle shape, size_t* contour_count); -MSDF_API int msdf_shape_get_contour(msdf_shape_handle shape, size_t index, msdf_contour_handle* contours); -MSDF_API int msdf_shape_get_edge_count(msdf_shape_handle shape, size_t* edge_count); -MSDF_API int msdf_shape_has_inverse_y_axis(msdf_shape_handle shape, int* inverse_y_axis); +MSDF_API int msdf_shape_get_contour_count(msdf_shape_const_handle shape, size_t* contour_count); +MSDF_API int msdf_shape_get_contour(msdf_shape_const_handle shape, size_t index, msdf_contour_const_handle* contour); +MSDF_API int msdf_shape_get_edge_count(msdf_shape_const_handle shape, size_t* edge_count); +MSDF_API int msdf_shape_has_inverse_y_axis(msdf_shape_const_handle shape, int* inverse_y_axis); MSDF_API int msdf_shape_normalize(msdf_shape_handle shape); MSDF_API int msdf_shape_validate(msdf_shape_handle shape, int* result); MSDF_API int msdf_shape_bound(msdf_shape_handle shape, msdf_bounds_t* bounds); @@ -155,51 +154,49 @@ MSDF_API int msdf_shape_bound_miters(msdf_shape_handle shape, msdf_bounds_t* bou MSDF_API void msdf_shape_free(msdf_shape_handle shape); MSDF_API int msdf_contour_alloc(msdf_contour_handle* contour); -MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_edge_holder_handle* edge); -MSDF_API int msdf_contour_get_edge_count(msdf_contour_handle contour, size_t* edge_count); -MSDF_API int msdf_contour_get_edge(msdf_contour_handle contour, size_t index, msdf_edge_holder_handle* edge); +MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_segment_handle* segment); +MSDF_API int msdf_contour_get_edge_count(msdf_contour_const_handle contour, size_t* edge_count); +MSDF_API int msdf_contour_get_edge(msdf_contour_const_handle contour, size_t index, msdf_segment_const_handle* segment); MSDF_API int msdf_contour_bound(msdf_contour_handle contour, msdf_bounds_t* bounds); MSDF_API int msdf_contour_bound_miters(msdf_contour_handle contour, msdf_bounds_t* bounds, double border, double miter_limit, int polarity); -MSDF_API int msdf_contour_get_winding(msdf_contour_handle contour, int* winding); +MSDF_API int msdf_contour_get_winding(msdf_contour_const_handle contour, int* winding); MSDF_API int msdf_contour_reverse(msdf_contour_handle contour); MSDF_API void msdf_contour_free(msdf_contour_handle contour); -MSDF_API int msdf_edge_alloc(msdf_segment_t* segment, msdf_edge_holder_handle* edge); -MSDF_API void msdf_edge_free(msdf_edge_holder_handle edge); +MSDF_API int msdf_segment_alloc(int type, msdf_segment_handle* segment); +MSDF_API int msdf_segment_get_type(msdf_segment_const_handle segment, int* type); +MSDF_API int msdf_segment_get_point_count(msdf_segment_const_handle segment, size_t* point_count); +MSDF_API int msdf_segment_get_point(msdf_segment_const_handle segment, size_t index, msdf_vector2_t* point); +MSDF_API int msdf_segment_set_point(msdf_segment_handle segment, size_t index, const msdf_vector2_t* point); +MSDF_API int msdf_segment_set_color(msdf_segment_handle segment, int color); +MSDF_API int msdf_segment_get_color(msdf_segment_const_handle segment, int* color); +MSDF_API int msdf_segment_get_direction(msdf_segment_const_handle segment, double param, msdf_vector2_t* direction); +MSDF_API int msdf_segment_get_direction_change(msdf_segment_const_handle segment, double param, msdf_vector2_t* direction_change); +MSDF_API int msdf_segment_point(msdf_segment_const_handle segment, double param, msdf_vector2_t* point); +MSDF_API int msdf_segment_bound(msdf_segment_const_handle segment, msdf_bounds_t* bounds); +MSDF_API int msdf_segment_move_start_point(msdf_segment_handle segment, const msdf_vector2_t* point); +MSDF_API int msdf_segment_move_end_point(msdf_segment_handle segment, const msdf_vector2_t* point); +MSDF_API void msdf_segment_free(msdf_segment_handle segment); -MSDF_API int msdf_segment_alloc(int type, msdf_segment_t** segment); -MSDF_API int msdf_segment_get_point_count(const msdf_segment_t* segment, size_t* point_count); -MSDF_API int msdf_segment_get_point(const msdf_segment_t* segment, size_t index, msdf_vector2_t* point); -MSDF_API int msdf_segment_set_point(msdf_segment_t* segment, size_t index, const msdf_vector2_t* point); -MSDF_API int msdf_segment_set_color(msdf_segment_t* segment, int color); -MSDF_API int msdf_segment_get_color(const msdf_segment_t* segment, int* color); -MSDF_API int msdf_segment_get_direction(const msdf_segment_t* segment, double param, msdf_vector2_t* direction); -MSDF_API int msdf_segment_get_direction_change(const msdf_segment_t* segment, double param, msdf_vector2_t* direction_change); -MSDF_API int msdf_segment_point(const msdf_segment_t* segment, double param, msdf_vector2_t* point); -MSDF_API int msdf_segment_bound(const msdf_segment_t* segment, msdf_bounds_t* bounds); -MSDF_API int msdf_segment_move_start_point(msdf_segment_t* segment, const msdf_vector2_t* point); -MSDF_API int msdf_segment_move_end_point(msdf_segment_t* segment, const msdf_vector2_t* point); -MSDF_API void msdf_segment_free(msdf_segment_t* segment); - -MSDF_API int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform); -MSDF_API int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform); -MSDF_API int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform); -MSDF_API int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_handle shape, const msdf_transform_t* transform); +MSDF_API int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform); +MSDF_API int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform); +MSDF_API int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform); +MSDF_API int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform); MSDF_API int msdf_generate_sdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_config_t* config); MSDF_API int msdf_generate_psdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_config_t* config); MSDF_API int msdf_generate_msdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_multichannel_config_t* config); MSDF_API int msdf_generate_mtsdf_with_config(msdf_bitmap_t* output, - msdf_shape_handle shape, + msdf_shape_const_handle shape, const msdf_transform_t* transform, const msdf_multichannel_config_t* config);