Finalize API, use handles for segments, don't expose edges in C

This commit is contained in:
KitsuneAlex 2024-05-03 22:55:47 +02:00
parent a267f24caa
commit ad0aef556d
No known key found for this signature in database
GPG Key ID: 6B0CE864BB69B7D0
3 changed files with 162 additions and 165 deletions

View File

@ -148,21 +148,16 @@ int main() {
```c++ ```c++
#include <msdfgen_c.h> #include <msdfgen_c.h>
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; msdf_shape_handle shape = NULL;
if(msdf_shape_alloc(&shape) != MSDF_SUCCESS) { if(msdf_shape_alloc(&shape) != MSDF_SUCCESS) {
return; // Handle error accordingly return; // Handle error accordingly
} }
// Create shape data from font using FreeType, STB etc here.. // 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; msdf_transform_t transform;
transform.scale.x = 1.0; transform.scale.x = 1.0;
transform.scale.y = 1.0; transform.scale.y = 1.0;
@ -170,15 +165,13 @@ int main(int num_args, char** args) {
transform.translation.y = 4.0; transform.translation.y = 4.0;
transform.distance_mapping.lower = 4.0; transform.distance_mapping.lower = 4.0;
transform.distance_mapping.upper = 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; 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 // Make sure to free heap allocated resources when we're done
msdf_shape_free(shape); msdf_shape_free(shape);
msdf_bitmap_free(bitmap); msdf_bitmap_free(&bitmap);
return 0; return 0;
} }
``` ```

View File

@ -82,34 +82,32 @@ MSDF_API const msdf_allocator_t* msdf_allocator_get() {
// msdf_bitmap // 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) { if(width < 0 || height < 0) {
return MSDF_ERR_INVALID_SIZE; return MSDF_ERR_INVALID_SIZE;
} }
if(bitmap == nullptr) { if(bitmap == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
auto* wrapper = msdf_alloc<msdf_bitmap_t>(); bitmap->type = type;
wrapper->type = type; bitmap->width = width;
wrapper->width = width; bitmap->height = height;
wrapper->height = height;
switch(type) { switch(type) {
case MSDF_BITMAP_TYPE_SDF: case MSDF_BITMAP_TYPE_SDF:
wrapper->handle = msdf_new<SDFBitmap>(width, height); bitmap->handle = msdf_new<SDFBitmap>(width, height);
break; break;
case MSDF_BITMAP_TYPE_PSDF: case MSDF_BITMAP_TYPE_PSDF:
wrapper->handle = msdf_new<PSDFBitmap>(width, height); bitmap->handle = msdf_new<PSDFBitmap>(width, height);
break; break;
case MSDF_BITMAP_TYPE_MSDF: case MSDF_BITMAP_TYPE_MSDF:
wrapper->handle = msdf_new<MSDFBitmap>(width, height); bitmap->handle = msdf_new<MSDFBitmap>(width, height);
break; break;
case MSDF_BITMAP_TYPE_MTSDF: case MSDF_BITMAP_TYPE_MTSDF:
wrapper->handle = msdf_new<MTSDFBitmap>(width, height); bitmap->handle = msdf_new<MTSDFBitmap>(width, height);
break; break;
default: default:
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*bitmap = wrapper;
return MSDF_SUCCESS; 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; 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) { if(bitmap == nullptr || size == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
@ -200,11 +198,11 @@ MSDF_API int msdf_shape_alloc(msdf_shape_handle* shape) {
return MSDF_SUCCESS; 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) { if(shape == nullptr || bounds == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*reinterpret_cast<msdfgen::Shape::Bounds*>(bounds) = reinterpret_cast<msdfgen::Shape*>(shape)->getBounds(); *reinterpret_cast<msdfgen::Shape::Bounds*>(bounds) = reinterpret_cast<const msdfgen::Shape*>(shape)->getBounds();
return MSDF_SUCCESS; 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) { if(shape == nullptr || contour == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*contour = reinterpret_cast<msdf_contour_handle>(&reinterpret_cast<msdfgen::Shape*>(shape)->addContour());
return MSDF_SUCCESS; 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) { if(shape == nullptr || contour_count == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*contour_count = reinterpret_cast<msdfgen::Shape*>(shape)->contours.size(); *contour_count = reinterpret_cast<const msdfgen::Shape*>(shape)->contours.size();
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
MSDF_API int msdf_shape_get_contour(msdf_shape_handle shape, const size_t index, msdf_contour_handle* contours) { MSDF_API int msdf_shape_get_contour(msdf_shape_const_handle shape, const size_t index, msdf_contour_const_handle* contour) {
if(shape == nullptr || contours == nullptr) { if(shape == nullptr || contour == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*contours = reinterpret_cast<msdf_contour_handle>(&reinterpret_cast<msdfgen::Shape*>(shape)->contours[index]); *contour = reinterpret_cast<msdf_contour_const_handle>(&reinterpret_cast<const msdfgen::Shape*>(shape)->contours[index]);
return MSDF_SUCCESS; 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) { if(shape == nullptr || edge_count == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*edge_count = reinterpret_cast<msdfgen::Shape*>(shape)->edgeCount(); *edge_count = reinterpret_cast<const msdfgen::Shape*>(shape)->edgeCount();
return MSDF_SUCCESS; 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) { if(shape == nullptr || inverse_y_axis == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*inverse_y_axis = reinterpret_cast<msdfgen::Shape*>(shape)->inverseYAxis ? MSDF_TRUE : MSDF_FALSE; *inverse_y_axis = reinterpret_cast<const msdfgen::Shape*>(shape)->inverseYAxis ? MSDF_TRUE : MSDF_FALSE;
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
@ -297,27 +296,29 @@ MSDF_API int msdf_contour_alloc(msdf_contour_handle* contour) {
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_edge_holder_handle* edge) { MSDF_API int msdf_contour_add_edge(msdf_contour_handle contour, msdf_segment_handle* segment) {
if(contour == nullptr || edge == nullptr) { if(contour == nullptr || segment == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*edge = reinterpret_cast<msdf_edge_holder_handle>(&reinterpret_cast<msdfgen::Contour*>(contour)->addEdge()); msdfgen::EdgeSegment* p_segment = reinterpret_cast<msdfgen::Contour*>(contour)->addEdge();
*segment = reinterpret_cast<msdf_segment_handle>(p_segment);
return MSDF_SUCCESS; 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) { if(contour == nullptr || edge_count == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*edge_count = reinterpret_cast<msdfgen::Contour*>(contour)->edges.size(); *edge_count = reinterpret_cast<const msdfgen::Contour*>(contour)->edges.size();
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
MSDF_API int msdf_contour_get_edge(msdf_contour_handle contour, const size_t index, msdf_edge_holder_handle* edges) { MSDF_API int msdf_contour_get_edge(msdf_contour_const_handle contour, const size_t index, msdf_segment_const_handle* segment) {
if(contour == nullptr || edges == nullptr) { if(contour == nullptr || segment == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*edges = reinterpret_cast<msdf_edge_holder_handle>(&reinterpret_cast<msdfgen::Contour*>(contour)->edges[index]); const msdfgen::EdgeSegment* p_segment = reinterpret_cast<const msdfgen::Contour*>(contour)->edges[index];
*segment = reinterpret_cast<msdf_segment_const_handle>(p_segment);
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
@ -341,11 +342,11 @@ MSDF_API int msdf_contour_bound_miters(msdf_contour_handle contour,
return MSDF_SUCCESS; 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) { if(contour == nullptr || winding == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*winding = reinterpret_cast<msdfgen::Contour*>(contour)->winding(); *winding = reinterpret_cast<const msdfgen::Contour*>(contour)->winding();
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
@ -361,61 +362,65 @@ MSDF_API void msdf_contour_free(msdf_contour_handle contour) {
msdf_delete(reinterpret_cast<msdfgen::Contour*>(contour)); msdf_delete(reinterpret_cast<msdfgen::Contour*>(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_edge_holder_handle>(msdf_new<msdfgen::EdgeHolder>(static_cast<msdfgen::EdgeSegment*>(segment->handle)));
return MSDF_SUCCESS;
}
void msdf_edge_free(msdf_edge_holder_handle edge) {
msdf_delete(reinterpret_cast<msdfgen::EdgeHolder*>(edge));
}
// msdf_segment // 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) { if(type < 0 || type >= MSDF_SEGMENT_TYPE_MAX) {
return MSDF_ERR_INVALID_TYPE; return MSDF_ERR_INVALID_TYPE;
} }
if(segment == nullptr) { if(segment == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
auto* wrapper = msdf_alloc<msdf_segment_t>();
wrapper->type = type;
switch(type) { switch(type) {
case MSDF_SEGMENT_TYPE_LINEAR: case MSDF_SEGMENT_TYPE_LINEAR:
wrapper->handle = msdf_new<msdfgen::LinearSegment>(msdfgen::Point2 {}, msdfgen::Point2 {}); *segment = reinterpret_cast<msdf_segment_handle>(msdf_new<msdfgen::LinearSegment>(msdfgen::Point2 {}, msdfgen::Point2 {}));
break; break;
case MSDF_SEGMENT_TYPE_QUADRATIC: case MSDF_SEGMENT_TYPE_QUADRATIC:
wrapper->handle = msdf_new<msdfgen::QuadraticSegment>(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}); *segment = reinterpret_cast<msdf_segment_handle>(
msdf_new<msdfgen::QuadraticSegment>(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}));
break; break;
case MSDF_SEGMENT_TYPE_CUBIC: case MSDF_SEGMENT_TYPE_CUBIC:
wrapper->handle = *segment = reinterpret_cast<msdf_segment_handle>(
msdf_new<msdfgen::CubicSegment>(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}); msdf_new<msdfgen::CubicSegment>(msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}, msdfgen::Point2 {}));
break; break;
default: default:
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*segment = wrapper;
return MSDF_SUCCESS; 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<const msdfgen::EdgeSegment*>(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) { if(segment == nullptr || point_count == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
switch(segment->type) { switch(reinterpret_cast<const msdfgen::EdgeSegment*>(segment)->type()) {
case MSDF_SEGMENT_TYPE_LINEAR: case msdfgen::LinearSegment::EdgeType::EDGE_TYPE:
*point_count = 2; *point_count = 2;
break; break;
case MSDF_SEGMENT_TYPE_QUADRATIC: case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE:
*point_count = 3; *point_count = 3;
break; break;
case MSDF_SEGMENT_TYPE_CUBIC: case msdfgen::CubicSegment::EdgeType::EDGE_TYPE:
*point_count = 4; *point_count = 4;
break; break;
default: default:
@ -425,7 +430,7 @@ MSDF_API int msdf_segment_get_point_count(const msdf_segment_t* segment, size_t*
return MSDF_SUCCESS; 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) { if(segment == nullptr || point == nullptr) {
return MSDF_ERR_INVALID_ARG; 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) { if(index >= point_count) {
return MSDF_ERR_INVALID_INDEX; return MSDF_ERR_INVALID_INDEX;
} }
switch(segment->type) { const auto* p_segment = reinterpret_cast<const msdfgen::EdgeSegment*>(segment);
case MSDF_SEGMENT_TYPE_LINEAR: switch(p_segment->type()) {
*reinterpret_cast<msdfgen::Point2*>(point) = static_cast<msdfgen::LinearSegment*>(segment->handle)->p[index]; case msdfgen::LinearSegment::EdgeType::EDGE_TYPE:
*reinterpret_cast<msdfgen::Point2*>(point) = dynamic_cast<const msdfgen::LinearSegment*>(p_segment)->p[index];
break; break;
case MSDF_SEGMENT_TYPE_QUADRATIC: case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE:
*reinterpret_cast<msdfgen::Point2*>(point) = static_cast<msdfgen::QuadraticSegment*>(segment->handle)->p[index]; *reinterpret_cast<msdfgen::Point2*>(point) = dynamic_cast<const msdfgen::QuadraticSegment*>(p_segment)->p[index];
break; break;
case MSDF_SEGMENT_TYPE_CUBIC: case msdfgen::CubicSegment::EdgeType::EDGE_TYPE:
*reinterpret_cast<msdfgen::Point2*>(point) = static_cast<msdfgen::CubicSegment*>(segment->handle)->p[index]; *reinterpret_cast<msdfgen::Point2*>(point) = dynamic_cast<const msdfgen::CubicSegment*>(p_segment)->p[index];
break; break;
default: default:
return MSDF_ERR_INVALID_ARG; 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; 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) { if(segment == nullptr || point == nullptr) {
return MSDF_ERR_INVALID_ARG; 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) { if(index >= point_count) {
return MSDF_ERR_INVALID_INDEX; return MSDF_ERR_INVALID_INDEX;
} }
switch(segment->type) { auto* p_segment = reinterpret_cast<msdfgen::EdgeSegment*>(segment);
case MSDF_SEGMENT_TYPE_LINEAR: switch(p_segment->type()) {
static_cast<msdfgen::LinearSegment*>(segment->handle)->p[index] = *reinterpret_cast<const msdfgen::Point2*>(point); case msdfgen::LinearSegment::EdgeType::EDGE_TYPE:
dynamic_cast<msdfgen::LinearSegment*>(p_segment)->p[index] = *reinterpret_cast<const msdfgen::Point2*>(point);
break; break;
case MSDF_SEGMENT_TYPE_QUADRATIC: case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE:
static_cast<msdfgen::QuadraticSegment*>(segment->handle)->p[index] = *reinterpret_cast<const msdfgen::Point2*>(point); dynamic_cast<msdfgen::QuadraticSegment*>(p_segment)->p[index] = *reinterpret_cast<const msdfgen::Point2*>(point);
break; break;
case MSDF_SEGMENT_TYPE_CUBIC: case msdfgen::CubicSegment::EdgeType::EDGE_TYPE:
static_cast<msdfgen::CubicSegment*>(segment->handle)->p[index] = *reinterpret_cast<const msdfgen::Point2*>(point); dynamic_cast<msdfgen::CubicSegment*>(p_segment)->p[index] = *reinterpret_cast<const msdfgen::Point2*>(point);
break; break;
default: default:
return MSDF_ERR_INVALID_ARG; 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; 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) { if(segment == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
static_cast<msdfgen::EdgeSegment*>(segment->handle)->color = static_cast<msdfgen::EdgeColor>(color); reinterpret_cast<msdfgen::EdgeSegment*>(segment)->color = static_cast<msdfgen::EdgeColor>(color);
return MSDF_SUCCESS; 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) { if(segment == nullptr || color == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*color = static_cast<int>(static_cast<msdfgen::EdgeSegment*>(segment->handle)->color); *color = static_cast<int>(reinterpret_cast<const msdfgen::EdgeSegment*>(segment)->color);
return MSDF_SUCCESS; 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) { if(segment == nullptr || direction == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*reinterpret_cast<msdfgen::Vector2*>(direction) = static_cast<msdfgen::EdgeSegment*>(segment->handle)->direction(param); *reinterpret_cast<msdfgen::Vector2*>(direction) = reinterpret_cast<const msdfgen::EdgeSegment*>(segment)->direction(param);
return MSDF_SUCCESS; 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) { if(segment == nullptr || direction_change == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*reinterpret_cast<msdfgen::Vector2*>(direction_change) = static_cast<msdfgen::EdgeSegment*>(segment->handle)->directionChange(param); *reinterpret_cast<msdfgen::Vector2*>(direction_change) = reinterpret_cast<const msdfgen::EdgeSegment*>(segment)->directionChange(param);
return MSDF_SUCCESS; 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) { if(segment == nullptr || point == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
*reinterpret_cast<msdfgen::Point2*>(point) = static_cast<msdfgen::EdgeSegment*>(segment->handle)->point(param); *reinterpret_cast<msdfgen::Point2*>(point) = reinterpret_cast<const msdfgen::EdgeSegment*>(segment)->point(param);
return MSDF_SUCCESS; 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) { if(segment == nullptr || bounds == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
static_cast<msdfgen::EdgeSegment*>(segment->handle)->bound(bounds->l, bounds->b, bounds->r, bounds->t); reinterpret_cast<const msdfgen::EdgeSegment*>(segment)->bound(bounds->l, bounds->b, bounds->r, bounds->t);
return MSDF_SUCCESS; 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) { if(segment == nullptr || point == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
static_cast<msdfgen::EdgeSegment*>(segment->handle)->moveStartPoint(*reinterpret_cast<const msdfgen::Point2*>(point)); reinterpret_cast<msdfgen::EdgeSegment*>(segment)->moveStartPoint(*reinterpret_cast<const msdfgen::Point2*>(point));
return MSDF_SUCCESS; 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) { if(segment == nullptr || point == nullptr) {
return MSDF_ERR_INVALID_ARG; return MSDF_ERR_INVALID_ARG;
} }
static_cast<msdfgen::EdgeSegment*>(segment->handle)->moveEndPoint(*reinterpret_cast<const msdfgen::Point2*>(point)); reinterpret_cast<msdfgen::EdgeSegment*>(segment)->moveEndPoint(*reinterpret_cast<const msdfgen::Point2*>(point));
return MSDF_SUCCESS; return MSDF_SUCCESS;
} }
MSDF_API void msdf_segment_free(msdf_segment_t* segment) { MSDF_API void msdf_segment_free(msdf_segment_handle segment) {
switch(segment->type) { auto* p_segment = reinterpret_cast<msdfgen::EdgeSegment*>(segment);
case MSDF_SEGMENT_TYPE_LINEAR: switch(p_segment->type()) {
msdf_delete(static_cast<msdfgen::LinearSegment*>(segment->handle)); case msdfgen::LinearSegment::EdgeType::EDGE_TYPE:
msdf_delete(dynamic_cast<msdfgen::LinearSegment*>(p_segment));
break; break;
case MSDF_SEGMENT_TYPE_QUADRATIC: case msdfgen::QuadraticSegment::EdgeType::EDGE_TYPE:
msdf_delete(static_cast<msdfgen::QuadraticSegment*>(segment->handle)); msdf_delete(dynamic_cast<msdfgen::QuadraticSegment*>(p_segment));
break; break;
case MSDF_SEGMENT_TYPE_CUBIC: case msdfgen::CubicSegment::EdgeType::EDGE_TYPE:
msdf_delete(static_cast<msdfgen::CubicSegment*>(segment->handle)); msdf_delete(dynamic_cast<msdfgen::CubicSegment*>(p_segment));
break; break;
default: default:
return; return;
} }
msdf_free(segment);
} }
// Main msdfgen APIs // 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) { if(output == nullptr || shape == nullptr || transform == nullptr) {
return MSDF_ERR_INVALID_ARG; 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; 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) { if(output == nullptr || shape == nullptr || transform == nullptr) {
return MSDF_ERR_INVALID_ARG; 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; 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) { if(output == nullptr || shape == nullptr || transform == nullptr) {
return MSDF_ERR_INVALID_ARG; 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; 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) { if(output == nullptr || shape == nullptr || transform == nullptr) {
return MSDF_ERR_INVALID_ARG; 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, 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_transform_t* transform,
const msdf_config_t* config) { const msdf_config_t* config) {
if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { 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, 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_transform_t* transform,
const msdf_config_t* config) { const msdf_config_t* config) {
if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { 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, 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_transform_t* transform,
const msdf_multichannel_config_t* config) { const msdf_multichannel_config_t* config) {
if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { 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, 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_transform_t* transform,
const msdf_multichannel_config_t* config) { const msdf_multichannel_config_t* config) {
if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) {

View File

@ -56,7 +56,11 @@
#define MSDF_EDGE_COLOR_CYAN 6 #define MSDF_EDGE_COLOR_CYAN 6
#define MSDF_EDGE_COLOR_WHITE 7 #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 // Macros for allocating default MSDF bitmap types
#define MSDF_ALLOC_SDF_BITMAP(w, h) msdf_bitmap_alloc(MSDF_BITMAP_TYPE_SDF, w, h) #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; void* handle;
} msdf_bitmap_t; } msdf_bitmap_t;
typedef struct msdf_segment {
int type;
void* handle;
} msdf_segment_t;
// Opaque handle types // Opaque handle types
MSDF_DEFINE_HANDLE_TYPE(msdf_shape); MSDF_DEFINE_HANDLE_TYPE(msdf_shape);
MSDF_DEFINE_HANDLE_TYPE(msdf_contour); MSDF_DEFINE_HANDLE_TYPE(msdf_contour);
MSDF_DEFINE_HANDLE_TYPE(msdf_edge_holder); MSDF_DEFINE_HANDLE_TYPE(msdf_segment);
// Exported API functions // Exported API functions
MSDF_API void msdf_allocator_set(const msdf_allocator_t* allocator); MSDF_API void msdf_allocator_set(const msdf_allocator_t* allocator);
MSDF_API const msdf_allocator_t* msdf_allocator_get(); 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_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_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 void msdf_bitmap_free(msdf_bitmap_t* bitmap);
MSDF_API int msdf_shape_alloc(msdf_shape_handle* shape); 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_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_count(msdf_shape_const_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_contour(msdf_shape_const_handle shape, size_t index, msdf_contour_const_handle* contour);
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);
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);
MSDF_API int msdf_shape_normalize(msdf_shape_handle shape); 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_validate(msdf_shape_handle shape, int* result);
MSDF_API int msdf_shape_bound(msdf_shape_handle shape, msdf_bounds_t* bounds); 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 void msdf_shape_free(msdf_shape_handle shape);
MSDF_API int msdf_contour_alloc(msdf_contour_handle* contour); 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_add_edge(msdf_contour_handle contour, msdf_segment_handle* segment);
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);
MSDF_API int msdf_contour_get_edge(msdf_contour_handle contour, size_t index, msdf_edge_holder_handle* edge); 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(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_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 int msdf_contour_reverse(msdf_contour_handle contour);
MSDF_API void msdf_contour_free(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 int msdf_segment_alloc(int type, msdf_segment_handle* segment);
MSDF_API void msdf_edge_free(msdf_edge_holder_handle edge); 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_generate_sdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform);
MSDF_API int msdf_segment_get_point_count(const msdf_segment_t* segment, size_t* point_count); MSDF_API int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform);
MSDF_API int msdf_segment_get_point(const msdf_segment_t* segment, size_t index, msdf_vector2_t* point); MSDF_API int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform);
MSDF_API int msdf_segment_set_point(msdf_segment_t* segment, size_t index, const msdf_vector2_t* point); MSDF_API int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform);
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_with_config(msdf_bitmap_t* output, 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_transform_t* transform,
const msdf_config_t* config); const msdf_config_t* config);
MSDF_API int msdf_generate_psdf_with_config(msdf_bitmap_t* output, 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_transform_t* transform,
const msdf_config_t* config); const msdf_config_t* config);
MSDF_API int msdf_generate_msdf_with_config(msdf_bitmap_t* output, 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_transform_t* transform,
const msdf_multichannel_config_t* config); const msdf_multichannel_config_t* config);
MSDF_API int msdf_generate_mtsdf_with_config(msdf_bitmap_t* output, 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_transform_t* transform,
const msdf_multichannel_config_t* config); const msdf_multichannel_config_t* config);