feat: modules moved and engine moved to submodule

This commit is contained in:
Jan van der Weide 2025-04-12 18:40:44 +02:00
parent dfb5e645cd
commit c33d2130cc
5136 changed files with 225275 additions and 64485 deletions

View file

@ -1,6 +1,8 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
import methods
Import("env")
Import("env_modules")
@ -8,28 +10,21 @@ env_text_server_adv = env_modules.Clone()
def make_icu_data(target, source, env):
dst = target[0].srcnode().abspath
buffer = methods.get_buffer(str(source[0]))
with methods.generated_wrapper(str(target[0])) as file:
file.write(f"""\
/* (C) 2016 and later: Unicode, Inc. and others. */
/* License & terms of use: https://www.unicode.org/copyright.html */
with open(dst, "w", encoding="utf-8", newline="\n") as g:
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
g.write("/* (C) 2016 and later: Unicode, Inc. and others. */\n")
g.write("/* License & terms of use: https://www.unicode.org/copyright.html */\n")
g.write("#ifndef _ICU_DATA_H\n")
g.write("#define _ICU_DATA_H\n")
g.write('#include "unicode/utypes.h"\n')
g.write('#include "unicode/udata.h"\n')
g.write('#include "unicode/uversion.h"\n')
#include <unicode/utypes.h>
#include <unicode/udata.h>
#include <unicode/uversion.h>
with open(source[0].srcnode().abspath, "rb") as f:
buf = f.read()
g.write('extern "C" U_EXPORT const size_t U_ICUDATA_SIZE = ' + str(len(buf)) + ";\n")
g.write('extern "C" U_EXPORT const unsigned char U_ICUDATA_ENTRY_POINT[] = {\n')
for i in range(len(buf)):
g.write("\t" + str(buf[i]) + ",\n")
g.write("};\n")
g.write("#endif")
extern "C" U_EXPORT const size_t U_ICUDATA_SIZE = {len(buffer)};
extern "C" U_EXPORT const unsigned char U_ICUDATA_ENTRY_POINT[] = {{
{methods.format_buffer(buffer, 1)}
}};
""")
# Thirdparty source files
@ -40,7 +35,7 @@ msdfgen_enabled = "msdfgen" in env.module_list
if "svg" in env.module_list:
env_text_server_adv.Prepend(
CPPPATH=[
CPPEXTPATH=[
"#thirdparty/thorvg/inc",
"#thirdparty/thorvg/src/common",
"#thirdparty/thorvg/src/renderer",
@ -122,7 +117,7 @@ if env["builtin_harfbuzz"]:
"src/hb-subset-instancer-iup.cc",
"src/hb-subset-instancer-solver.cc",
"src/hb-subset-plan.cc",
"src/hb-subset-repacker.cc",
"src/hb-subset-serialize.cc",
"src/hb-subset.cc",
"src/hb-ucd.cc",
"src/hb-unicode.cc",
@ -140,11 +135,11 @@ if env["builtin_harfbuzz"]:
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
env_harfbuzz.Prepend(CPPPATH=["#thirdparty/harfbuzz/src"])
env_harfbuzz.Prepend(CPPEXTPATH=["#thirdparty/harfbuzz/src"])
env_harfbuzz.Append(CCFLAGS=["-DHAVE_ICU"])
if env["builtin_icu4c"]:
env_harfbuzz.Prepend(CPPPATH=["#thirdparty/icu4c/common/", "#thirdparty/icu4c/i18n/"])
env_harfbuzz.Prepend(CPPEXTPATH=["#thirdparty/icu4c/common/", "#thirdparty/icu4c/i18n/"])
env_harfbuzz.Append(
CCFLAGS=[
"-DU_STATIC_IMPLEMENTATION",
@ -167,15 +162,15 @@ if env["builtin_harfbuzz"]:
]
)
if env["builtin_freetype"]:
env_harfbuzz.Prepend(CPPPATH=["#thirdparty/freetype/include"])
env_harfbuzz.Prepend(CPPEXTPATH=["#thirdparty/freetype/include"])
if env["builtin_graphite"] and env["graphite"]:
env_harfbuzz.Prepend(CPPPATH=["#thirdparty/graphite/include"])
env_harfbuzz.Prepend(CPPEXTPATH=["#thirdparty/graphite/include"])
env_harfbuzz.Append(CCFLAGS=["-DGRAPHITE2_STATIC"])
if env["platform"] in ["android", "linuxbsd", "web"]:
env_harfbuzz.Append(CCFLAGS=["-DHAVE_PTHREAD"])
env_text_server_adv.Prepend(CPPPATH=["#thirdparty/harfbuzz/src"])
env_text_server_adv.Prepend(CPPEXTPATH=["#thirdparty/harfbuzz/src"])
lib = env_harfbuzz.add_library("harfbuzz_builtin", thirdparty_sources)
thirdparty_obj += lib
@ -238,7 +233,7 @@ if env["builtin_graphite"] and freetype_enabled and env["graphite"]:
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
env_graphite.Prepend(CPPPATH=["#thirdparty/graphite/src", "#thirdparty/graphite/include"])
env_graphite.Prepend(CPPEXTPATH=["#thirdparty/graphite/src", "#thirdparty/graphite/include"])
env_graphite.Append(
CCFLAGS=[
"-DGRAPHITE2_STATIC",
@ -478,15 +473,10 @@ if env["builtin_icu4c"]:
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
if env.editor_build:
env_icu.CommandNoCache(
"#thirdparty/icu4c/icudata.gen.h", "#thirdparty/icu4c/icudt_godot.dat", env.Run(make_icu_data)
)
env_text_server_adv.Prepend(CPPPATH=["#thirdparty/icu4c/"])
else:
if not env.editor_build:
thirdparty_sources += ["icu_data/icudata_stub.cpp"]
env_icu.Prepend(CPPPATH=["#thirdparty/icu4c/common/", "#thirdparty/icu4c/i18n/"])
env_icu.Prepend(CPPEXTPATH=["#thirdparty/icu4c/common/", "#thirdparty/icu4c/i18n/"])
env_icu.Append(
CXXFLAGS=[
"-DU_STATIC_IMPLEMENTATION",
@ -515,11 +505,18 @@ if env["builtin_icu4c"]:
if env.editor_build:
env_text_server_adv.Append(CXXFLAGS=["-DICU_STATIC_DATA"])
env_text_server_adv.Prepend(CPPPATH=["#thirdparty/icu4c/common/", "#thirdparty/icu4c/i18n/"])
env_text_server_adv.Prepend(CPPEXTPATH=["#thirdparty/icu4c/common/", "#thirdparty/icu4c/i18n/"])
lib = env_icu.add_library("icu_builtin", thirdparty_sources)
thirdparty_obj += lib
if env.editor_build:
icudata = env_icu.CommandNoCache(
"#thirdparty/icu4c/icudata.gen.h", "#thirdparty/icu4c/icudt_godot.dat", env.Run(make_icu_data)
)
env_text_server_adv.Prepend(CPPEXTPATH=["#thirdparty/icu4c/"])
env_icu.Depends(lib, icudata)
# Needs to be appended to arrive after libscene in the linker call,
# but we don't want it to arrive *after* system libs, so manual hack
# LIBS contains first SCons Library objects ("SCons.Node.FS.File object")
@ -539,19 +536,15 @@ if env["builtin_icu4c"]:
module_obj = []
if env["builtin_msdfgen"] and msdfgen_enabled:
# Treat msdfgen headers as system headers to avoid raising warnings. Not supported on MSVC.
env_text_server_adv.Append(CPPDEFINES=[("MSDFGEN_PUBLIC", "")])
if not env.msvc:
env_text_server_adv.Append(CPPFLAGS=["-isystem", Dir("#thirdparty/msdfgen").path])
else:
env_text_server_adv.Prepend(CPPPATH=["#thirdparty/msdfgen"])
env_text_server_adv.Prepend(CPPEXTPATH=["#thirdparty/msdfgen"])
if env["builtin_freetype"] and freetype_enabled:
env_text_server_adv.Append(CPPDEFINES=["FT_CONFIG_OPTION_USE_BROTLI"])
env_text_server_adv.Prepend(CPPPATH=["#thirdparty/freetype/include"])
env_text_server_adv.Prepend(CPPEXTPATH=["#thirdparty/freetype/include"])
if env["builtin_graphite"] and freetype_enabled and env["graphite"]:
env_text_server_adv.Prepend(CPPPATH=["#thirdparty/graphite/include"])
env_text_server_adv.Prepend(CPPEXTPATH=["#thirdparty/graphite/include"])
env_text_server_adv.add_source_files(module_obj, "*.cpp")
env.modules_sources += module_obj

View file

@ -362,7 +362,7 @@ thirdparty_harfbuzz_sources = [
"src/hb-subset-instancer-iup.cc",
"src/hb-subset-instancer-solver.cc",
"src/hb-subset-plan.cc",
"src/hb-subset-repacker.cc",
"src/hb-subset-serialize.cc",
"src/hb-subset.cc",
"src/hb-ucd.cc",
"src/hb-unicode.cc",

View file

@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef TEXT_SERVER_ADV_REGISTER_TYPES_H
#define TEXT_SERVER_ADV_REGISTER_TYPES_H
#pragma once
#ifdef GDEXTENSION
#include <godot_cpp/core/class_db.hpp>
@ -40,5 +39,3 @@ using namespace godot;
void initialize_text_server_adv_module(ModuleInitializationLevel p_level);
void uninitialize_text_server_adv_module(ModuleInitializationLevel p_level);
#endif // TEXT_SERVER_ADV_REGISTER_TYPES_H

View file

@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef SCRIPT_ITERATOR_H
#define SCRIPT_ITERATOR_H
#pragma once
#ifdef GDEXTENSION
@ -74,5 +73,3 @@ private:
public:
ScriptIterator(const String &p_string, int p_start, int p_length);
};
#endif // SCRIPT_ITERATOR_H

View file

@ -60,23 +60,17 @@ using namespace godot;
// Built-in ICU data.
#ifdef ICU_STATIC_DATA
#include "icudata.gen.h"
#include <icudata.gen.h>
#endif
// Thirdparty headers.
#ifdef MODULE_MSDFGEN_ENABLED
#ifdef _MSC_VER
#pragma warning(disable : 4458)
#endif
#include <core/EdgeHolder.h>
#include <core/ShapeDistanceFinder.h>
#include <core/contour-combiners.h>
#include <core/edge-selectors.h>
#include <msdfgen.h>
#ifdef _MSC_VER
#pragma warning(default : 4458)
#endif
#endif
#ifdef MODULE_SVG_ENABLED
@ -1318,11 +1312,12 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data,
} break;
}
FT_GlyphSlot slot = fd->face->glyph;
bool from_svg = (slot->format == FT_GLYPH_FORMAT_SVG); // Need to check before FT_Render_Glyph as it will change format to bitmap.
if (!outline) {
if (!p_font_data->msdf) {
error = FT_Render_Glyph(fd->face->glyph, aa_mode);
error = FT_Render_Glyph(slot, aa_mode);
}
FT_GlyphSlot slot = fd->face->glyph;
if (!error) {
if (p_font_data->msdf) {
#ifdef MODULE_MSDFGEN_ENABLED
@ -1363,6 +1358,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data,
cleanup_stroker:
FT_Stroker_Done(stroker);
}
gl.from_svg = from_svg;
E = fd->glyph_map.insert(p_glyph, gl);
r_glyph = E->value;
return gl.found;
@ -1417,25 +1413,17 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &fd->stream;
int max_index = 0;
FT_Face tmp_face = nullptr;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (tmp_face && error == 0) {
max_index = tmp_face->num_faces - 1;
}
if (tmp_face) {
FT_Done_Face(tmp_face);
}
error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
error = FT_Open_Face(ft_library, &fargs, p_font_data->face_index, &fd->face);
if (error) {
FT_Done_Face(fd->face);
fd->face = nullptr;
if (fd->face) {
FT_Done_Face(fd->face);
fd->face = nullptr;
}
memdelete(fd);
if (p_silent) {
return false;
} else {
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "' (face_index=" + String::num_int64(p_font_data->face_index) + ").");
}
}
}
@ -1451,10 +1439,10 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
int best_match = 0;
int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
int diff = Math::abs(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
int ndiff = Math::abs(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
if (ndiff < diff) {
best_match = i;
diff = ndiff;
@ -2446,6 +2434,24 @@ bool TextServerAdvanced::_font_is_force_autohinter(const RID &p_font_rid) const
return fd->force_autohinter;
}
void TextServerAdvanced::_font_set_modulate_color_glyphs(const RID &p_font_rid, bool p_modulate) {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL(fd);
MutexLock lock(fd->mutex);
if (fd->modulate_color_glyphs != p_modulate) {
fd->modulate_color_glyphs = p_modulate;
}
}
bool TextServerAdvanced::_font_is_modulate_color_glyphs(const RID &p_font_rid) const {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL_V(fd, false);
MutexLock lock(fd->mutex);
return fd->modulate_color_glyphs;
}
void TextServerAdvanced::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL(fd);
@ -3312,6 +3318,10 @@ RID TextServerAdvanced::_font_get_glyph_texture_rid(const RID &p_font_rid, const
if (ffsd->textures[fgl.texture_idx].dirty) {
ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
Ref<Image> img = tex.image;
if (fgl.from_svg) {
// Same as the "fix alpha border" process option when importing SVGs
img->fix_alpha_edges();
}
if (fd->mipmaps && !img->has_mipmaps()) {
img = tex.image->duplicate();
img->generate_mipmaps();
@ -3360,6 +3370,10 @@ Size2 TextServerAdvanced::_font_get_glyph_texture_size(const RID &p_font_rid, co
if (ffsd->textures[fgl.texture_idx].dirty) {
ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
Ref<Image> img = tex.image;
if (fgl.from_svg) {
// Same as the "fix alpha border" process option when importing SVGs
img->fix_alpha_edges();
}
if (fd->mipmaps && !img->has_mipmaps()) {
img = tex.image->duplicate();
img->generate_mipmaps();
@ -3798,7 +3812,7 @@ void TextServerAdvanced::_font_draw_glyph(const RID &p_font_rid, const RID &p_ca
if (fgl.texture_idx != -1) {
Color modulate = p_color;
#ifdef MODULE_FREETYPE_ENABLED
if (ffsd->face && ffsd->textures[fgl.texture_idx].image.is_valid() && (ffsd->textures[fgl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
if (!fd->modulate_color_glyphs && ffsd->face && ffsd->textures[fgl.texture_idx].image.is_valid() && (ffsd->textures[fgl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
modulate.r = modulate.g = modulate.b = 1.0;
}
#endif
@ -3806,6 +3820,10 @@ void TextServerAdvanced::_font_draw_glyph(const RID &p_font_rid, const RID &p_ca
if (ffsd->textures[fgl.texture_idx].dirty) {
ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
Ref<Image> img = tex.image;
if (fgl.from_svg) {
// Same as the "fix alpha border" process option when importing SVGs
img->fix_alpha_edges();
}
if (fd->mipmaps && !img->has_mipmaps()) {
img = tex.image->duplicate();
img->generate_mipmaps();
@ -4192,6 +4210,8 @@ void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *
p_shaped->uthk = 0.0;
p_shaped->glyphs.clear();
p_shaped->glyphs_logical.clear();
p_shaped->runs.clear();
p_shaped->runs_dirty = true;
p_shaped->overrun_trim_data = TrimData();
p_shaped->utf16 = Char16String();
for (int i = 0; i < p_shaped->bidi_iter.size(); i++) {
@ -4474,6 +4494,213 @@ Variant TextServerAdvanced::_shaped_get_span_embedded_object(const RID &p_shaped
}
}
String TextServerAdvanced::_shaped_get_span_text(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, String());
ShapedTextDataAdvanced *span_sd = sd;
if (sd->parent.is_valid()) {
span_sd = shaped_owner.get_or_null(sd->parent);
ERR_FAIL_NULL_V(span_sd, String());
}
ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), String());
return span_sd->text.substr(span_sd->spans[p_index].start, span_sd->spans[p_index].end - span_sd->spans[p_index].start);
}
Variant TextServerAdvanced::_shaped_get_span_object(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, Variant());
ShapedTextDataAdvanced *span_sd = sd;
if (sd->parent.is_valid()) {
span_sd = shaped_owner.get_or_null(sd->parent);
ERR_FAIL_NULL_V(span_sd, Variant());
}
ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), Variant());
return span_sd->spans[p_index].embedded_key;
}
void TextServerAdvanced::_generate_runs(ShapedTextDataAdvanced *p_sd) const {
ERR_FAIL_NULL(p_sd);
p_sd->runs.clear();
ShapedTextDataAdvanced *span_sd = p_sd;
if (p_sd->parent.is_valid()) {
span_sd = shaped_owner.get_or_null(p_sd->parent);
ERR_FAIL_NULL(span_sd);
}
int sd_size = p_sd->glyphs.size();
const Glyph *sd_gl = p_sd->glyphs.ptr();
int span_count = span_sd->spans.size();
int span = -1;
int span_start = -1;
int span_end = -1;
TextRun run;
for (int i = 0; i < sd_size; i += sd_gl[i].count) {
const Glyph &gl = sd_gl[i];
if (gl.start < 0 || gl.end < 0) {
continue;
}
if (gl.start < span_start || gl.start >= span_end) {
span = -1;
span_start = -1;
span_end = -1;
for (int j = 0; j < span_count; j++) {
if (gl.start >= span_sd->spans[j].start && gl.end <= span_sd->spans[j].end) {
span = j;
span_start = span_sd->spans[j].start;
span_end = span_sd->spans[j].end;
break;
}
}
}
if (run.font_rid != gl.font_rid || run.font_size != gl.font_size || run.span_index != span || run.rtl != bool(gl.flags & GRAPHEME_IS_RTL)) {
if (run.span_index >= 0) {
p_sd->runs.push_back(run);
}
run.range = Vector2i(gl.start, gl.end);
run.font_rid = gl.font_rid;
run.font_size = gl.font_size;
run.rtl = bool(gl.flags & GRAPHEME_IS_RTL);
run.span_index = span;
}
run.range.x = MIN(run.range.x, gl.start);
run.range.y = MAX(run.range.y, gl.end);
}
if (run.span_index >= 0) {
p_sd->runs.push_back(run);
}
p_sd->runs_dirty = false;
}
int64_t TextServerAdvanced::_shaped_get_run_count(const RID &p_shaped) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, 0);
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
return sd->runs.size();
}
String TextServerAdvanced::_shaped_get_run_text(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, String());
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String());
return sd->text.substr(sd->runs[p_index].range.x - sd->start, sd->runs[p_index].range.y - sd->runs[p_index].range.x);
}
Vector2i TextServerAdvanced::_shaped_get_run_range(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, Vector2i());
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Vector2i());
return sd->runs[p_index].range;
}
RID TextServerAdvanced::_shaped_get_run_font_rid(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, RID());
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), RID());
return sd->runs[p_index].font_rid;
}
int TextServerAdvanced::_shaped_get_run_font_size(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, 0);
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), 0);
return sd->runs[p_index].font_size;
}
String TextServerAdvanced::_shaped_get_run_language(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, String());
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String());
int span_idx = sd->runs[p_index].span_index;
ShapedTextDataAdvanced *span_sd = sd;
if (sd->parent.is_valid()) {
span_sd = shaped_owner.get_or_null(sd->parent);
ERR_FAIL_NULL_V(span_sd, String());
}
ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), String());
return span_sd->spans[span_idx].language;
}
TextServer::Direction TextServerAdvanced::_shaped_get_run_direction(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, TextServer::DIRECTION_LTR);
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), TextServer::DIRECTION_LTR);
return sd->runs[p_index].rtl ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
}
Variant TextServerAdvanced::_shaped_get_run_object(const RID &p_shaped, int64_t p_index) const {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, Variant());
MutexLock lock(sd->mutex);
if (!sd->valid.is_set()) {
const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
}
if (sd->runs_dirty) {
_generate_runs(sd);
}
ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Variant());
int span_idx = sd->runs[p_index].span_index;
ShapedTextDataAdvanced *span_sd = sd;
if (sd->parent.is_valid()) {
span_sd = shaped_owner.get_or_null(sd->parent);
ERR_FAIL_NULL_V(span_sd, Variant());
}
ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), Variant());
return span_sd->spans[span_idx].embedded_key;
}
void TextServerAdvanced::_shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL(sd);
@ -4557,6 +4784,13 @@ bool TextServerAdvanced::_shaped_text_add_object(const RID &p_shaped, const Vari
return true;
}
String TextServerAdvanced::_shaped_get_text(const RID &p_shaped) const {
const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, String());
return sd->text;
}
bool TextServerAdvanced::_shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, double p_baseline) {
ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
ERR_FAIL_NULL_V(sd, false);
@ -4753,6 +4987,8 @@ bool TextServerAdvanced::_shape_substr(ShapedTextDataAdvanced *p_new_sd, const S
p_new_sd->sort_valid = false;
p_new_sd->upos = p_sd->upos;
p_new_sd->uthk = p_sd->uthk;
p_new_sd->runs.clear();
p_new_sd->runs_dirty = true;
if (p_length > 0) {
p_new_sd->text = p_sd->text.substr(p_start - p_sd->start, p_length);
@ -5650,8 +5886,13 @@ bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) {
i++;
}
int r_end = sd->spans[i].end;
UBreakIterator *bi = ubrk_open(UBRK_LINE, (language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale().ascii().get_data() : language.ascii().get_data(), data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err);
if (U_FAILURE(err)) {
UBreakIterator *bi = _create_line_break_iterator_for_locale(language, &err);
if (!U_FAILURE(err) && bi) {
ubrk_setText(bi, data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err);
}
if (U_FAILURE(err) || !bi) {
// No data loaded - use fallback.
for (int j = r_start; j < r_end; j++) {
char32_t c = sd->text[j - sd->start];
@ -5679,8 +5920,8 @@ bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) {
sd->break_inserts++;
}
}
ubrk_close(bi);
}
ubrk_close(bi);
i++;
}
sd->break_ops_valid = true;
@ -6108,17 +6349,15 @@ Glyph TextServerAdvanced::_shape_single_glyph(ShapedTextDataAdvanced *p_sd, char
return gl;
}
_FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs) {
Array keys = p_source.keys();
Array values = p_source.values();
for (int i = 0; i < keys.size(); i++) {
int32_t value = values[i];
_FORCE_INLINE_ void TextServerAdvanced::_add_features(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs) {
for (const KeyValue<Variant, Variant> &key_value : p_source) {
int32_t value = key_value.value;
if (value >= 0) {
hb_feature_t feature;
if (keys[i].is_string()) {
feature.tag = _name_to_tag(keys[i]);
if (key_value.key.is_string()) {
feature.tag = _name_to_tag(key_value.key);
} else {
feature.tag = keys[i];
feature.tag = key_value.key;
}
feature.value = value;
feature.start = 0;
@ -6128,6 +6367,24 @@ _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source
}
}
UBreakIterator *TextServerAdvanced::_create_line_break_iterator_for_locale(const String &p_language, UErrorCode *r_err) const {
// Creating UBreakIterator (ubrk_open) is surprisingly costly.
// However, cloning (ubrk_clone) is cheaper, so we keep around blueprints to accelerate creating new ones.
const String language = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
_THREAD_SAFE_METHOD_
const HashMap<String, UBreakIterator *>::Iterator key_value = line_break_iterators_per_language.find(language);
if (key_value) {
return ubrk_clone(key_value->value, r_err);
}
UBreakIterator *bi = ubrk_open(UBRK_LINE, language.ascii().get_data(), nullptr, 0, r_err);
if (U_FAILURE(*r_err) || !bi) {
return nullptr;
}
line_break_iterators_per_language.insert(language, bi);
return ubrk_clone(bi, r_err);
}
void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index, int64_t p_prev_start, int64_t p_prev_end, RID p_prev_font) {
RID f;
int fs = p_sd->spans[p_span].font_size;
@ -6271,8 +6528,8 @@ void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_star
hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)p_sd->text.ptr(), p_sd->text.length(), p_start, p_end - p_start);
Vector<hb_feature_t> ftrs;
_add_featuers(_font_get_opentype_feature_overrides(f), ftrs);
_add_featuers(p_sd->spans[p_span].features, ftrs);
_add_features(_font_get_opentype_feature_overrides(f), ftrs);
_add_features(p_sd->spans[p_span].features, ftrs);
hb_shape(hb_font, p_sd->hb_buffer, ftrs.is_empty() ? nullptr : &ftrs[0], ftrs.size());
@ -6521,6 +6778,8 @@ bool TextServerAdvanced::_shaped_text_shape(const RID &p_shaped) {
} else {
bidi_ranges = sd->bidi_override;
}
sd->runs.clear();
sd->runs_dirty = true;
for (int ov = 0; ov < bidi_ranges.size(); ov++) {
// Create BiDi iterator.
@ -7703,6 +7962,9 @@ TextServerAdvanced::~TextServerAdvanced() {
uset_close(allowed);
allowed = nullptr;
}
for (const KeyValue<String, UBreakIterator *> &bi : line_break_iterators_per_language) {
ubrk_close(bi.value);
}
std::atexit(u_cleanup);
}

View file

@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef TEXT_SERVER_ADV_H
#define TEXT_SERVER_ADV_H
#pragma once
/*************************************************************************/
/* ICU/HarfBuzz/Graphite backed Text Server implementation with BiDi, */
@ -95,11 +94,6 @@ using namespace godot;
// Thirdparty headers.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#include <unicode/ubidi.h>
#include <unicode/ubrk.h>
#include <unicode/uchar.h>
@ -113,10 +107,6 @@ using namespace godot;
#include <unicode/ustring.h>
#include <unicode/utypes.h>
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef MODULE_FREETYPE_ENABLED
#include <ft2build.h>
#include FT_FREETYPE_H
@ -177,6 +167,10 @@ class TextServerAdvanced : public TextServerExtension {
mutable USpoofChecker *sc_spoof = nullptr;
mutable USpoofChecker *sc_conf = nullptr;
mutable HashMap<String, UBreakIterator *> line_break_iterators_per_language;
UBreakIterator *_create_line_break_iterator_for_locale(const String &p_language, UErrorCode *r_err) const;
// Font cache data.
#ifdef MODULE_FREETYPE_ENABLED
@ -272,6 +266,7 @@ class TextServerAdvanced : public TextServerExtension {
Rect2 rect;
Rect2 uv_rect;
Vector2 advance;
bool from_svg = false;
};
struct FontForSizeAdvanced {
@ -326,6 +321,7 @@ class TextServerAdvanced : public TextServerExtension {
int fixed_size = 0;
bool allow_system_fallback = true;
bool force_autohinter = false;
bool modulate_color_glyphs = false;
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
bool keep_rounding_remainders = true;
@ -404,7 +400,7 @@ class TextServerAdvanced : public TextServerExtension {
_FORCE_INLINE_ Variant::Type _get_tag_type(int64_t p_tag) const;
_FORCE_INLINE_ bool _get_tag_hidden(int64_t p_tag) const;
_FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {
String sty_name = p_sty_name.replace(" ", "").replace("-", "");
String sty_name = p_sty_name.remove_chars(" -");
if (sty_name.contains("thin") || sty_name.contains("hairline")) {
return 100;
} else if (sty_name.contains("extralight") || sty_name.contains("ultralight")) {
@ -431,7 +427,7 @@ class TextServerAdvanced : public TextServerExtension {
return 400;
}
_FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {
String sty_name = p_sty_name.replace(" ", "").replace("-", "");
String sty_name = p_sty_name.remove_chars(" -");
if (sty_name.contains("ultracondensed")) {
return 50;
} else if (sty_name.contains("extracondensed")) {
@ -462,6 +458,14 @@ class TextServerAdvanced : public TextServerExtension {
Vector<Glyph> ellipsis_glyph_buf;
};
struct TextRun {
Vector2i range;
RID font_rid;
int font_size = 0;
bool rtl = false;
int64_t span_index = -1;
};
struct ShapedTextDataAdvanced {
Mutex mutex;
@ -493,6 +497,9 @@ class TextServerAdvanced : public TextServerExtension {
int first_span = 0; // First span in the parent ShapedTextData.
int last_span = 0;
Vector<TextRun> runs;
bool runs_dirty = true;
struct EmbeddedObject {
int start = -1;
int end = -1;
@ -668,6 +675,7 @@ class TextServerAdvanced : public TextServerExtension {
mutable HashMap<String, PackedByteArray> system_font_data;
void _update_chars(ShapedTextDataAdvanced *p_sd) const;
void _generate_runs(ShapedTextDataAdvanced *p_sd) const;
void _realign(ShapedTextDataAdvanced *p_sd) const;
int64_t _convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const;
int64_t _convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;
@ -677,7 +685,7 @@ class TextServerAdvanced : public TextServerExtension {
Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, const RID &p_font, int64_t p_font_size);
_FORCE_INLINE_ RID _find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text);
_FORCE_INLINE_ void _add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs);
_FORCE_INLINE_ void _add_features(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs);
Mutex ft_mutex;
@ -803,6 +811,9 @@ public:
MODBIND2(font_set_force_autohinter, const RID &, bool);
MODBIND1RC(bool, font_is_force_autohinter, const RID &);
MODBIND2(font_set_modulate_color_glyphs, const RID &, bool);
MODBIND1RC(bool, font_is_modulate_color_glyphs, const RID &);
MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);
MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);
@ -957,12 +968,24 @@ public:
MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);
MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);
MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);
MODBIND1RC(String, shaped_get_text, const RID &);
MODBIND1RC(int64_t, shaped_get_span_count, const RID &);
MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);
MODBIND2RC(Variant, shaped_get_span_embedded_object, const RID &, int64_t);
MODBIND2RC(String, shaped_get_span_text, const RID &, int64_t);
MODBIND2RC(Variant, shaped_get_span_object, const RID &, int64_t);
MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);
MODBIND1RC(int64_t, shaped_get_run_count, const RID &);
MODBIND2RC(String, shaped_get_run_text, const RID &, int64_t);
MODBIND2RC(Vector2i, shaped_get_run_range, const RID &, int64_t);
MODBIND2RC(RID, shaped_get_run_font_rid, const RID &, int64_t);
MODBIND2RC(int, shaped_get_run_font_size, const RID &, int64_t);
MODBIND2RC(String, shaped_get_run_language, const RID &, int64_t);
MODBIND2RC(Direction, shaped_get_run_direction, const RID &, int64_t);
MODBIND2RC(Variant, shaped_get_run_object, const RID &, int64_t);
MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);
MODBIND1RC(RID, shaped_text_get_parent, const RID &);
@ -1025,5 +1048,3 @@ public:
TextServerAdvanced();
~TextServerAdvanced();
};
#endif // TEXT_SERVER_ADV_H

View file

@ -28,6 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "thorvg_svg_in_ot.h"
#ifdef GDEXTENSION
// Headers for building as GDExtension plug-in.
@ -55,8 +57,6 @@ using namespace godot;
#ifdef MODULE_SVG_ENABLED
#ifdef MODULE_FREETYPE_ENABLED
#include "thorvg_svg_in_ot.h"
#include <freetype/otsvg.h>
#include <ft2build.h>

View file

@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef THORVG_SVG_IN_OT_H
#define THORVG_SVG_IN_OT_H
#pragma once
#ifdef GDEXTENSION
// Headers for building as GDExtension plug-in.
@ -95,5 +94,3 @@ SVG_RendererHooks *get_tvg_svg_in_ot_hooks();
#endif // MODULE_FREETYPE_ENABLED
#endif // MODULE_SVG_ENABLED
#endif // THORVG_SVG_IN_OT_H