From 263c909349cb64d988bba26eb62c01a3a0b19791 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Fri, 26 May 2023 16:40:53 -0500 Subject: [PATCH] Fix text_server_adv compiling as a GDExtension --- .../gdextension_build/text_server_adv.gdextension | 1 + modules/text_server_adv/register_types.cpp | 4 ++-- modules/text_server_adv/text_server_adv.cpp | 8 +++++--- modules/text_server_adv/thorvg_svg_in_ot.cpp | 15 +++++++++++++++ modules/text_server_adv/thorvg_svg_in_ot.h | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/modules/text_server_adv/gdextension_build/text_server_adv.gdextension b/modules/text_server_adv/gdextension_build/text_server_adv.gdextension index c12fcfdfdfc..bcf3661a8ca 100644 --- a/modules/text_server_adv/gdextension_build/text_server_adv.gdextension +++ b/modules/text_server_adv/gdextension_build/text_server_adv.gdextension @@ -1,6 +1,7 @@ [configuration] entry_symbol = "textserver_advanced_init" +compatibility_minimum = 4.1 [libraries] diff --git a/modules/text_server_adv/register_types.cpp b/modules/text_server_adv/register_types.cpp index 51a4d8171e5..ba67fef70f3 100644 --- a/modules/text_server_adv/register_types.cpp +++ b/modules/text_server_adv/register_types.cpp @@ -62,8 +62,8 @@ using namespace godot; extern "C" { -GDExtensionBool GDE_EXPORT textserver_advanced_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) { - GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); +GDExtensionBool GDE_EXPORT textserver_advanced_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) { + GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization); init_obj.register_initializer(&initialize_text_server_adv_module); init_obj.register_terminator(&uninitialize_text_server_adv_module); diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index eb0cb54caf2..b8010e66925 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -2112,10 +2112,12 @@ Dictionary TextServerAdvanced::_font_get_ot_name_strings(const RID &p_font_rid) name = vformat("unknown_%d", names[i].name_id); } break; } - String text; unsigned int text_size = hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, nullptr, nullptr) + 1; - text.resize(text_size); - hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, &text_size, (uint32_t *)text.ptrw()); + // @todo After godot-cpp#1141 is fixed, use text.resize() and write directly to text.wptr() instead of using a temporary buffer. + char32_t *buffer = memnew_arr(char32_t, text_size); + hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, &text_size, (uint32_t *)buffer); + String text(buffer); + memdelete_arr(buffer); if (!text.is_empty()) { Dictionary &id_string = names_for_lang[String(hb_language_to_string(names[i].language))]; id_string[name] = text; diff --git a/modules/text_server_adv/thorvg_svg_in_ot.cpp b/modules/text_server_adv/thorvg_svg_in_ot.cpp index c227f35561f..9836d683619 100644 --- a/modules/text_server_adv/thorvg_svg_in_ot.cpp +++ b/modules/text_server_adv/thorvg_svg_in_ot.cpp @@ -150,10 +150,21 @@ FT_Error tvg_svg_in_ot_preset_slot(FT_GlyphSlot p_slot, FT_Bool p_cache, FT_Poin } String xml_code_str = "" + xml_body; +#ifndef GDEXTENSION gl_state.xml_code = xml_code_str.utf8(); +#else + CharString xml_code = xml_code_str.utf8(); + gl_state.xml_code_length = xml_code.length(); + gl_state.xml_code = memnew_arr(char, gl_state.xml_code_length); + memcpy(gl_state.xml_code, xml_code.get_data(), gl_state.xml_code_length); +#endif picture = tvg::Picture::gen(); +#ifndef GDEXTENSION result = picture->load(gl_state.xml_code.get_data(), gl_state.xml_code.length(), "svg+xml", false); +#else + result = picture->load(gl_state.xml_code, gl_state.xml_code_length, "svg+xml", false); +#endif if (result != tvg::Result::Success) { ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to load SVG document (glyph metrics)."); } @@ -241,7 +252,11 @@ FT_Error tvg_svg_in_ot_render(FT_GlyphSlot p_slot, FT_Pointer *p_state) { ERR_FAIL_COND_V_MSG(!gl_state.ready, FT_Err_Invalid_SVG_Document, "SVG glyph not ready."); std::unique_ptr picture = tvg::Picture::gen(); +#ifndef GDEXTENSION tvg::Result res = picture->load(gl_state.xml_code.get_data(), gl_state.xml_code.length(), "svg+xml", false); +#else + tvg::Result res = picture->load(gl_state.xml_code, gl_state.xml_code_length, "svg+xml", false); +#endif if (res != tvg::Result::Success) { ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to load SVG document (glyph rendering)."); } diff --git a/modules/text_server_adv/thorvg_svg_in_ot.h b/modules/text_server_adv/thorvg_svg_in_ot.h index 034fffb5e64..5e79c8e444d 100644 --- a/modules/text_server_adv/thorvg_svg_in_ot.h +++ b/modules/text_server_adv/thorvg_svg_in_ot.h @@ -66,8 +66,22 @@ struct GL_State { float y = 0; float w = 0; float h = 0; +#ifndef GDEXTENSION CharString xml_code; +#else + // @todo After godot-cpp#1142 is fixed, use CharString just like when compiled as a Godot module. + char *xml_code = nullptr; + int xml_code_length = 0; +#endif tvg::Matrix m; + +#ifdef GDEXTENSION + ~GL_State() { + if (xml_code) { + memdelete_arr(xml_code); + } + } +#endif }; struct TVG_State {