| 1 | /**************************************************************************/ |
| 2 | /* thorvg_svg_in_ot.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifdef GDEXTENSION |
| 32 | // Headers for building as GDExtension plug-in. |
| 33 | |
| 34 | #include <godot_cpp/classes/xml_parser.hpp> |
| 35 | #include <godot_cpp/core/mutex_lock.hpp> |
| 36 | #include <godot_cpp/godot.hpp> |
| 37 | #include <godot_cpp/templates/vector.hpp> |
| 38 | |
| 39 | using namespace godot; |
| 40 | |
| 41 | #else |
| 42 | // Headers for building as built-in module. |
| 43 | |
| 44 | #include "core/error/error_macros.h" |
| 45 | #include "core/io/xml_parser.h" |
| 46 | #include "core/os/memory.h" |
| 47 | #include "core/os/os.h" |
| 48 | #include "core/string/ustring.h" |
| 49 | #include "core/typedefs.h" |
| 50 | #include "core/variant/variant.h" |
| 51 | |
| 52 | #include "modules/modules_enabled.gen.h" // For svg, freetype. |
| 53 | #endif |
| 54 | |
| 55 | #ifdef MODULE_SVG_ENABLED |
| 56 | #ifdef MODULE_FREETYPE_ENABLED |
| 57 | |
| 58 | #include "thorvg_svg_in_ot.h" |
| 59 | |
| 60 | #include "thorvg_bounds_iterator.h" |
| 61 | |
| 62 | #include <freetype/otsvg.h> |
| 63 | #include <ft2build.h> |
| 64 | |
| 65 | #include <math.h> |
| 66 | #include <stdlib.h> |
| 67 | |
| 68 | FT_Error tvg_svg_in_ot_init(FT_Pointer *p_state) { |
| 69 | *p_state = memnew(TVG_State); |
| 70 | |
| 71 | return FT_Err_Ok; |
| 72 | } |
| 73 | |
| 74 | void tvg_svg_in_ot_free(FT_Pointer *p_state) { |
| 75 | TVG_State *state = *reinterpret_cast<TVG_State **>(p_state); |
| 76 | memdelete(state); |
| 77 | } |
| 78 | |
| 79 | FT_Error tvg_svg_in_ot_preset_slot(FT_GlyphSlot p_slot, FT_Bool p_cache, FT_Pointer *p_state) { |
| 80 | TVG_State *state = *reinterpret_cast<TVG_State **>(p_state); |
| 81 | if (!state) { |
| 82 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "SVG in OT state not initialized." ); |
| 83 | } |
| 84 | MutexLock lock(state->mutex); |
| 85 | |
| 86 | FT_SVG_Document document = (FT_SVG_Document)p_slot->other; |
| 87 | FT_Size_Metrics metrics = document->metrics; |
| 88 | |
| 89 | GL_State &gl_state = state->glyph_map[p_slot->glyph_index]; |
| 90 | if (!gl_state.ready) { |
| 91 | Ref<XMLParser> parser; |
| 92 | parser.instantiate(); |
| 93 | parser->_open_buffer((const uint8_t *)document->svg_document, document->svg_document_length); |
| 94 | |
| 95 | float aspect = 1.0f; |
| 96 | String xml_body; |
| 97 | while (parser->read() == OK) { |
| 98 | if (parser->has_attribute("id" )) { |
| 99 | const String &gl_name = parser->get_named_attribute_value("id" ); |
| 100 | if (gl_name.begins_with("glyph" )) { |
| 101 | int dot_pos = gl_name.find("." ); |
| 102 | int64_t gl_idx = gl_name.substr(5, (dot_pos > 0) ? dot_pos - 5 : -1).to_int(); |
| 103 | if (p_slot->glyph_index != gl_idx) { |
| 104 | parser->skip_section(); |
| 105 | continue; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "svg" ) { |
| 110 | if (parser->has_attribute("viewBox" )) { |
| 111 | PackedStringArray vb = parser->get_named_attribute_value("viewBox" ).split(" " ); |
| 112 | |
| 113 | if (vb.size() == 4) { |
| 114 | aspect = vb[2].to_float() / vb[3].to_float(); |
| 115 | } |
| 116 | } |
| 117 | continue; |
| 118 | } |
| 119 | if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { |
| 120 | xml_body += vformat("<%s" , parser->get_node_name()); |
| 121 | for (int i = 0; i < parser->get_attribute_count(); i++) { |
| 122 | xml_body += vformat(" %s=\"%s\"" , parser->get_attribute_name(i), parser->get_attribute_value(i)); |
| 123 | } |
| 124 | |
| 125 | if (parser->is_empty()) { |
| 126 | xml_body += "/>" ; |
| 127 | } else { |
| 128 | xml_body += ">" ; |
| 129 | } |
| 130 | } else if (parser->get_node_type() == XMLParser::NODE_TEXT) { |
| 131 | xml_body += parser->get_node_data(); |
| 132 | } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END) { |
| 133 | xml_body += vformat("</%s>" , parser->get_node_name()); |
| 134 | } |
| 135 | } |
| 136 | String temp_xml_str = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 1 1\">" + xml_body; |
| 137 | CharString temp_xml = temp_xml_str.utf8(); |
| 138 | |
| 139 | std::unique_ptr<tvg::Picture> picture = tvg::Picture::gen(); |
| 140 | tvg::Result result = picture->load(temp_xml.get_data(), temp_xml.length(), "svg+xml" , false); |
| 141 | if (result != tvg::Result::Success) { |
| 142 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to load SVG document (bounds detection)." ); |
| 143 | } |
| 144 | |
| 145 | float min_x = INFINITY, min_y = INFINITY, max_x = -INFINITY, max_y = -INFINITY; |
| 146 | tvg_get_bounds(picture.get(), min_x, min_y, max_x, max_y); |
| 147 | |
| 148 | float new_h = (max_y - min_y); |
| 149 | float new_w = (max_x - min_x); |
| 150 | |
| 151 | if (new_h * aspect >= new_w) { |
| 152 | new_w = (new_h * aspect); |
| 153 | } else { |
| 154 | new_h = (new_w / aspect); |
| 155 | } |
| 156 | |
| 157 | String xml_code_str = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"" + rtos(min_x) + " " + rtos(min_y) + " " + rtos(new_w) + " " + rtos(new_h) + "\">" + xml_body; |
| 158 | gl_state.xml_code = xml_code_str.utf8(); |
| 159 | |
| 160 | picture = tvg::Picture::gen(); |
| 161 | result = picture->load(gl_state.xml_code.get_data(), gl_state.xml_code.length(), "svg+xml" , false); |
| 162 | if (result != tvg::Result::Success) { |
| 163 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to load SVG document (glyph metrics)." ); |
| 164 | } |
| 165 | |
| 166 | float x_svg_to_out, y_svg_to_out; |
| 167 | x_svg_to_out = (float)metrics.x_ppem / new_w; |
| 168 | y_svg_to_out = (float)metrics.y_ppem / new_h; |
| 169 | |
| 170 | gl_state.m.e11 = (double)document->transform.xx / (1 << 16) * x_svg_to_out; |
| 171 | gl_state.m.e12 = -(double)document->transform.xy / (1 << 16) * x_svg_to_out; |
| 172 | gl_state.m.e21 = -(double)document->transform.yx / (1 << 16) * y_svg_to_out; |
| 173 | gl_state.m.e22 = (double)document->transform.yy / (1 << 16) * y_svg_to_out; |
| 174 | gl_state.m.e13 = (double)document->delta.x / 64 * new_w / metrics.x_ppem; |
| 175 | gl_state.m.e23 = -(double)document->delta.y / 64 * new_h / metrics.y_ppem; |
| 176 | gl_state.m.e31 = 0; |
| 177 | gl_state.m.e32 = 0; |
| 178 | gl_state.m.e33 = 1; |
| 179 | |
| 180 | result = picture->transform(gl_state.m); |
| 181 | if (result != tvg::Result::Success) { |
| 182 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to apply transform to SVG document." ); |
| 183 | } |
| 184 | |
| 185 | result = picture->bounds(&gl_state.x, &gl_state.y, &gl_state.w, &gl_state.h, true); |
| 186 | if (result != tvg::Result::Success) { |
| 187 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to get SVG bounds." ); |
| 188 | } |
| 189 | |
| 190 | gl_state.bmp_y = gl_state.h + metrics.descender / 64.f; |
| 191 | gl_state.bmp_x = 0; |
| 192 | |
| 193 | gl_state.ready = true; |
| 194 | } |
| 195 | |
| 196 | p_slot->bitmap_left = (FT_Int)gl_state.bmp_x; |
| 197 | p_slot->bitmap_top = (FT_Int)gl_state.bmp_y; |
| 198 | |
| 199 | float tmp = ceil(gl_state.h); |
| 200 | p_slot->bitmap.rows = (unsigned int)tmp; |
| 201 | tmp = ceil(gl_state.w); |
| 202 | p_slot->bitmap.width = (unsigned int)tmp; |
| 203 | p_slot->bitmap.pitch = (int)p_slot->bitmap.width * 4; |
| 204 | p_slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA; |
| 205 | |
| 206 | float metrics_width, metrics_height; |
| 207 | float horiBearingX, horiBearingY; |
| 208 | float vertBearingX, vertBearingY; |
| 209 | |
| 210 | metrics_width = (float)gl_state.w; |
| 211 | metrics_height = (float)gl_state.h; |
| 212 | horiBearingX = (float)gl_state.x; |
| 213 | horiBearingY = (float)-gl_state.y; |
| 214 | vertBearingX = p_slot->metrics.horiBearingX / 64.0f - p_slot->metrics.horiAdvance / 64.0f / 2; |
| 215 | vertBearingY = (p_slot->metrics.vertAdvance / 64.0f - p_slot->metrics.height / 64.0f) / 2; |
| 216 | |
| 217 | tmp = roundf(metrics_width * 64); |
| 218 | p_slot->metrics.width = (FT_Pos)tmp; |
| 219 | tmp = roundf(metrics_height * 64); |
| 220 | p_slot->metrics.height = (FT_Pos)tmp; |
| 221 | |
| 222 | p_slot->metrics.horiBearingX = (FT_Pos)(horiBearingX * 64); |
| 223 | p_slot->metrics.horiBearingY = (FT_Pos)(horiBearingY * 64); |
| 224 | p_slot->metrics.vertBearingX = (FT_Pos)(vertBearingX * 64); |
| 225 | p_slot->metrics.vertBearingY = (FT_Pos)(vertBearingY * 64); |
| 226 | |
| 227 | if (p_slot->metrics.vertAdvance == 0) { |
| 228 | p_slot->metrics.vertAdvance = (FT_Pos)(metrics_height * 1.2f * 64); |
| 229 | } |
| 230 | |
| 231 | return FT_Err_Ok; |
| 232 | } |
| 233 | |
| 234 | FT_Error tvg_svg_in_ot_render(FT_GlyphSlot p_slot, FT_Pointer *p_state) { |
| 235 | TVG_State *state = *reinterpret_cast<TVG_State **>(p_state); |
| 236 | if (!state) { |
| 237 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "SVG in OT state not initialized." ); |
| 238 | } |
| 239 | MutexLock lock(state->mutex); |
| 240 | |
| 241 | if (!state->glyph_map.has(p_slot->glyph_index)) { |
| 242 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "SVG glyph not loaded." ); |
| 243 | } |
| 244 | |
| 245 | GL_State &gl_state = state->glyph_map[p_slot->glyph_index]; |
| 246 | ERR_FAIL_COND_V_MSG(!gl_state.ready, FT_Err_Invalid_SVG_Document, "SVG glyph not ready." ); |
| 247 | |
| 248 | std::unique_ptr<tvg::Picture> picture = tvg::Picture::gen(); |
| 249 | tvg::Result res = picture->load(gl_state.xml_code.get_data(), gl_state.xml_code.length(), "svg+xml" , false); |
| 250 | if (res != tvg::Result::Success) { |
| 251 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to load SVG document (glyph rendering)." ); |
| 252 | } |
| 253 | res = picture->transform(gl_state.m); |
| 254 | if (res != tvg::Result::Success) { |
| 255 | ERR_FAIL_V_MSG(FT_Err_Invalid_SVG_Document, "Failed to apply transform to SVG document." ); |
| 256 | } |
| 257 | |
| 258 | std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen(); |
| 259 | res = sw_canvas->target((uint32_t *)p_slot->bitmap.buffer, (int)p_slot->bitmap.width, (int)p_slot->bitmap.width, (int)p_slot->bitmap.rows, tvg::SwCanvas::ARGB8888S); |
| 260 | if (res != tvg::Result::Success) { |
| 261 | ERR_FAIL_V_MSG(FT_Err_Invalid_Outline, "Failed to create SVG canvas." ); |
| 262 | } |
| 263 | res = sw_canvas->push(std::move(picture)); |
| 264 | if (res != tvg::Result::Success) { |
| 265 | ERR_FAIL_V_MSG(FT_Err_Invalid_Outline, "Failed to set SVG canvas source." ); |
| 266 | } |
| 267 | res = sw_canvas->draw(); |
| 268 | if (res != tvg::Result::Success) { |
| 269 | ERR_FAIL_V_MSG(FT_Err_Invalid_Outline, "Failed to draw to SVG canvas." ); |
| 270 | } |
| 271 | res = sw_canvas->sync(); |
| 272 | if (res != tvg::Result::Success) { |
| 273 | ERR_FAIL_V_MSG(FT_Err_Invalid_Outline, "Failed to sync SVG canvas." ); |
| 274 | } |
| 275 | |
| 276 | state->glyph_map.erase(p_slot->glyph_index); |
| 277 | |
| 278 | p_slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA; |
| 279 | p_slot->bitmap.num_grays = 256; |
| 280 | p_slot->format = FT_GLYPH_FORMAT_BITMAP; |
| 281 | |
| 282 | return FT_Err_Ok; |
| 283 | } |
| 284 | |
| 285 | SVG_RendererHooks tvg_svg_in_ot_hooks = { |
| 286 | (SVG_Lib_Init_Func)tvg_svg_in_ot_init, |
| 287 | (SVG_Lib_Free_Func)tvg_svg_in_ot_free, |
| 288 | (SVG_Lib_Render_Func)tvg_svg_in_ot_render, |
| 289 | (SVG_Lib_Preset_Slot_Func)tvg_svg_in_ot_preset_slot, |
| 290 | }; |
| 291 | |
| 292 | SVG_RendererHooks *get_tvg_svg_in_ot_hooks() { |
| 293 | return &tvg_svg_in_ot_hooks; |
| 294 | } |
| 295 | |
| 296 | #endif // MODULE_FREETYPE_ENABLED |
| 297 | #endif // MODULE_SVG_ENABLED |
| 298 | |