/* * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR * --------------------------------------------- * A utility by Viktor Chlumsky, (c) 2014 - 2024 * * The technique used to generate multi-channel distance fields in this code * has been developed by Viktor Chlumsky in 2014 for his master's thesis, * "Shape Decomposition for Multi-Channel Distance Fields". It provides improved * quality of sharp corners in glyphs and other 2D shapes compared to monochrome * distance fields. To reconstruct an image of the shape, apply the median of * three operation on the triplet of sampled signed distance values. * */ #include "../msdfgen-ext-c.h" #include "../msdfgen-ext.h" /* * A C-API modeled closely after the msdfgen C++ API * to allow C-programs and other language runtimes to * use the msdfgen library. Originally written for LWJGL. * * @since 11/05/2024 * @author Alexander Hinze */ extern "C" { #ifdef MSDFGEN_DYNAMIC_FREETYPE // Runtime loader API MSDF_API int msdf_ft_set_load_callback(msdf_ft_load_callback callback) { if(callback == nullptr) { return MSDF_ERR_INVALID_ARG; } msdfgen::setFreetypeLoadCallback(callback); return MSDF_SUCCESS; } MSDF_API msdf_ft_load_callback msdf_ft_get_load_callback() { return msdfgen::getFreetypeLoadCallback(); } #endif MSDF_API int msdf_ft_init(msdf_ft_handle* handle) { if(handle == nullptr) { return MSDF_ERR_INVALID_ARG; } *handle = reinterpret_cast(msdfgen::initializeFreetype()); return MSDF_SUCCESS; } int msdf_ft_adopt_font(void* face, msdf_ft_font_handle* font) { if(face == nullptr || font == nullptr) { return MSDF_ERR_INVALID_ARG; } *font = reinterpret_cast(msdfgen::adoptFreetypeFontRaw(face)); return MSDF_SUCCESS; } MSDF_API int msdf_ft_load_font(msdf_ft_handle handle, const char* filename, msdf_ft_font_handle* font) { if(handle == nullptr || filename == nullptr || font == nullptr) { return MSDF_ERR_INVALID_ARG; } *font = reinterpret_cast(msdfgen::loadFont(reinterpret_cast(handle), filename)); return MSDF_SUCCESS; } MSDF_API int msdf_ft_load_font_data(msdf_ft_handle handle, const void* data, const size_t size, msdf_ft_font_handle* font) { if(handle == nullptr || data == nullptr || font == nullptr) { return MSDF_ERR_INVALID_ARG; } *font = reinterpret_cast(msdfgen::loadFontData(reinterpret_cast(handle), static_cast(data), static_cast(size))); return MSDF_SUCCESS; } MSDF_API int msdf_ft_font_load_glyph(msdf_ft_font_handle font, const unsigned cp, msdf_shape_handle* shape) { if(font == nullptr || shape == nullptr) { return MSDF_ERR_INVALID_ARG; } auto* actual_shape = new msdfgen::Shape(); msdfgen::loadGlyph(*actual_shape, reinterpret_cast(font), cp); *shape = reinterpret_cast(actual_shape); return MSDF_SUCCESS; } MSDF_API void msdf_ft_font_destroy(msdf_ft_font_handle handle) { if(handle == nullptr) { return; } msdfgen::destroyFont(reinterpret_cast(handle)); } MSDF_API void msdf_ft_deinit(msdf_ft_handle handle) { if(handle == nullptr) { return; } msdfgen::deinitializeFreetype(reinterpret_cast(handle)); } }