Implement msdfgen::adoptFreetypeFontRaw & msdf_ft_adopt_font C-API function for linkless FreeType interop

This commit is contained in:
KitsuneAlex 2024-05-15 01:46:04 +02:00
parent 241d109db6
commit 695769cf0f
No known key found for this signature in database
GPG Key ID: 6B0CE864BB69B7D0
4 changed files with 26 additions and 0 deletions

View File

@ -30,6 +30,7 @@ class FreetypeHandle {
};
class FontHandle {
friend FontHandle *adoptFreetypeFontRaw(void *ftFace);
friend FontHandle *adoptFreetypeFont(FT_Face ftFace);
friend FontHandle *loadFont(FreetypeHandle *library, const char *filename);
friend FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
@ -134,6 +135,13 @@ void deinitializeFreetype(FreetypeHandle *library) {
delete library;
}
FontHandle *adoptFreetypeFontRaw(void *ftFace) {
FontHandle *handle = new FontHandle;
handle->face = static_cast<FT_Face>(ftFace);
handle->ownership = false;
return handle;
}
FontHandle *adoptFreetypeFont(FT_Face ftFace) {
FontHandle *handle = new FontHandle;
handle->face = ftFace;

View File

@ -69,6 +69,8 @@ FontHandle *adoptFreetypeFont(FT_Face ftFace);
FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline, double scale = MSDFGEN_LEGACY_FONT_COORDINATE_SCALE);
#endif
/// Creates a FontHandle from FT_Face opaque pointer (mostly used for C-API)
FontHandle *adoptFreetypeFontRaw(void *ftFace);
/// Loads a font file and returns its handle.
FontHandle *loadFont(FreetypeHandle *library, const char *filename);
/// Loads a font from binary data and returns its handle.

View File

@ -34,6 +34,14 @@ MSDF_API int msdf_ft_init(msdf_ft_handle* handle) {
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<msdf_ft_font_handle>(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;

View File

@ -48,6 +48,14 @@ MSDF_API int msdf_ft_init(msdf_ft_handle* handle);
*/
MSDF_API int msdf_ft_load_font(msdf_ft_handle handle, const char* filename, msdf_ft_font_handle* font);
/**
* Adopts the given FreeType FT_Face pointer as a font handle.
* @param face An opaque pointer to the FT_Face to adopt.
* @param font A pointer to an address to be populated with the newly allocated font handle.
* @returns @code MSDF_SUCCESS@endcode on success, otherwise one of the constants prefixed with @code MSDF_ERR_@endcode.
*/
MSDF_API int msdf_ft_adopt_font(void* face, msdf_ft_font_handle* font);
/**
* Loads a TrueType font from the given buffer and populates
* the given font handle with the address of the newly loaded font.