[Complex Text Layouts] Implement ICU / HarfBuzz based TextServer module.
This commit is contained in:
parent
b9f441e81e
commit
200828276e
@ -3,6 +3,38 @@
|
||||
Import("env")
|
||||
Import("env_modules")
|
||||
|
||||
env_text_server_adv = env_modules.Clone()
|
||||
|
||||
|
||||
def make_icu_data(target, source, env):
|
||||
import os
|
||||
|
||||
dst = target[0].srcnode().abspath
|
||||
|
||||
g = open(dst, "w", encoding="utf-8")
|
||||
|
||||
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: http://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')
|
||||
|
||||
f = open(source[0].srcnode().abspath, "rb")
|
||||
buf = f.read()
|
||||
import os.path
|
||||
|
||||
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")
|
||||
|
||||
|
||||
if env["builtin_harfbuzz"]:
|
||||
env_harfbuzz = env_modules.Clone()
|
||||
|
||||
@ -93,7 +125,13 @@ if env["builtin_harfbuzz"]:
|
||||
]
|
||||
)
|
||||
env_harfbuzz.Append(
|
||||
CCFLAGS=["-DHAVE_ICU_BUILTIN", "-DHAVE_ICU", "-DHAVE_FREETYPE", "-DHAVE_GRAPHITE2", "-DGRAPHITE2_STATIC",]
|
||||
CCFLAGS=[
|
||||
"-DHAVE_ICU_BUILTIN",
|
||||
"-DHAVE_ICU",
|
||||
"-DHAVE_FREETYPE",
|
||||
"-DHAVE_GRAPHITE2",
|
||||
"-DGRAPHITE2_STATIC",
|
||||
]
|
||||
)
|
||||
env_harfbuzz.disable_warnings()
|
||||
env_thirdparty = env_harfbuzz.Clone()
|
||||
@ -158,7 +196,13 @@ if env["builtin_graphite"]:
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
|
||||
env_graphite.Append(CPPPATH=["#thirdparty/graphite/src", "#thirdparty/graphite/include"])
|
||||
env_graphite.Append(CCFLAGS=["-DGRAPHITE2_STATIC", "-DGRAPHITE2_NTRACING", "-DGRAPHITE2_NFILEFACE"])
|
||||
env_graphite.Append(
|
||||
CCFLAGS=[
|
||||
"-DGRAPHITE2_STATIC",
|
||||
"-DGRAPHITE2_NTRACING",
|
||||
"-DGRAPHITE2_NFILEFACE",
|
||||
]
|
||||
)
|
||||
env_graphite.disable_warnings()
|
||||
env_thirdparty = env_graphite.Clone()
|
||||
env_thirdparty.disable_warnings()
|
||||
@ -353,7 +397,7 @@ if env["builtin_icu"]:
|
||||
"common/uset.cpp",
|
||||
"common/uset_props.cpp",
|
||||
"common/usetiter.cpp",
|
||||
"common/ushape.cpp",
|
||||
# "common/ushape.cpp",
|
||||
"common/usprep.cpp",
|
||||
"common/ustack.cpp",
|
||||
"common/ustr_cnv.cpp",
|
||||
@ -382,12 +426,39 @@ if env["builtin_icu"]:
|
||||
"common/wintz.cpp",
|
||||
]
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
thirdparty_sources += ["icu_data/icudata_stub.cpp"]
|
||||
|
||||
icu_data_name = "icudt68l.dat"
|
||||
|
||||
if env_icu["tools"]:
|
||||
env_icu.Depends("#thirdparty/icu4c/icudata.gen.h", "#thirdparty/icu4c/" + icu_data_name)
|
||||
env_icu.Command("#thirdparty/icu4c/icudata.gen.h", "#thirdparty/icu4c/" + icu_data_name, make_icu_data)
|
||||
else:
|
||||
thirdparty_sources += ["icu_data/icudata_stub.cpp"]
|
||||
|
||||
env_icu.Append(CPPPATH=["#thirdparty/icu4c/common/"])
|
||||
env_icu.Append(
|
||||
CXXFLAGS=["-DU_STATIC_IMPLEMENTATION", "-DU_COMMON_IMPLEMENTATION", "-DPKGDATA_MODE=static",]
|
||||
CXXFLAGS=[
|
||||
"-DU_STATIC_IMPLEMENTATION",
|
||||
"-DU_COMMON_IMPLEMENTATION",
|
||||
"-DUCONFIG_NO_COLLATION",
|
||||
"-DUCONFIG_NO_CONVERSION",
|
||||
"-DUCONFIG_NO_FORMATTING",
|
||||
"-DUCONFIG_NO_SERVICE",
|
||||
"-DUCONFIG_NO_IDNA",
|
||||
"-DUCONFIG_NO_FILE_IO",
|
||||
"-DUCONFIG_NO_TRANSLITERATION",
|
||||
"-DPKGDATA_MODE=static",
|
||||
"-DICU_DATA_NAME=" + icu_data_name,
|
||||
]
|
||||
)
|
||||
env_text_server_adv.Append(
|
||||
CXXFLAGS=[
|
||||
"-DICU_DATA_NAME=" + icu_data_name,
|
||||
]
|
||||
)
|
||||
|
||||
if env_icu.msvc:
|
||||
env_icu.AppendUnique(CCFLAGS=["/utf-8"])
|
||||
|
||||
env_icu.disable_warnings()
|
||||
env_thirdparty = env_icu.Clone()
|
||||
@ -407,7 +478,9 @@ if env["builtin_icu"]:
|
||||
if not inserted:
|
||||
env.Append(LIBS=[lib])
|
||||
|
||||
env_text_server_adv = env_modules.Clone()
|
||||
if env_text_server_adv["tools"]:
|
||||
env_text_server_adv.Append(CXXFLAGS=["-DICU_STATIC_DATA"])
|
||||
|
||||
env_text_server_adv.Append(
|
||||
CPPPATH=[
|
||||
"#thirdparty/harfbuzz/src",
|
||||
|
575
modules/text_server_adv/bitmap_font_adv.cpp
Normal file
575
modules/text_server_adv/bitmap_font_adv.cpp
Normal file
@ -0,0 +1,575 @@
|
||||
/*************************************************************************/
|
||||
/* bitmap_font_adv.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "bitmap_font_adv.h"
|
||||
|
||||
/*************************************************************************/
|
||||
/* hb_bmp_font_t HarfBuzz Bitmap font interface */
|
||||
/*************************************************************************/
|
||||
|
||||
struct hb_bmp_font_t {
|
||||
BitmapFontDataAdvanced *face = nullptr;
|
||||
float font_size = 0;
|
||||
bool unref = false; /* Whether to destroy bm_face when done. */
|
||||
};
|
||||
|
||||
static hb_bmp_font_t *_hb_bmp_font_create(BitmapFontDataAdvanced *p_face, float p_font_size, bool p_unref) {
|
||||
hb_bmp_font_t *bm_font = reinterpret_cast<hb_bmp_font_t *>(calloc(1, sizeof(hb_bmp_font_t)));
|
||||
|
||||
if (!bm_font) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bm_font->face = p_face;
|
||||
bm_font->font_size = p_font_size;
|
||||
bm_font->unref = p_unref;
|
||||
|
||||
return bm_font;
|
||||
}
|
||||
|
||||
static void _hb_bmp_font_destroy(void *data) {
|
||||
hb_bmp_font_t *bm_font = reinterpret_cast<hb_bmp_font_t *>(data);
|
||||
free(bm_font);
|
||||
}
|
||||
|
||||
static hb_bool_t hb_bmp_get_nominal_glyph(hb_font_t *font, void *font_data, hb_codepoint_t unicode, hb_codepoint_t *glyph, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(unicode)) {
|
||||
if (bm_font->face->has_char(0xF000u + unicode)) {
|
||||
*glyph = 0xF000u + unicode;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*glyph = unicode;
|
||||
return true;
|
||||
}
|
||||
|
||||
static hb_position_t hb_bmp_get_glyph_h_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(glyph)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bm_font->face->get_advance(glyph, bm_font->font_size).x * 64;
|
||||
}
|
||||
|
||||
static hb_position_t hb_bmp_get_glyph_v_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(glyph)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -bm_font->face->get_advance(glyph, bm_font->font_size).y * 64;
|
||||
}
|
||||
|
||||
static hb_position_t hb_bmp_get_glyph_h_kerning(hb_font_t *font, void *font_data, hb_codepoint_t left_glyph, hb_codepoint_t right_glyph, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(left_glyph)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(right_glyph)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bm_font->face->get_kerning(left_glyph, right_glyph, bm_font->font_size).x * 64;
|
||||
}
|
||||
|
||||
static hb_bool_t hb_bmp_get_glyph_v_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(glyph)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*x = bm_font->face->get_advance(glyph, bm_font->font_size).x * 32;
|
||||
*y = bm_font->face->get_ascent(bm_font->font_size) * 64;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static hb_bool_t hb_bmp_get_glyph_extents(hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!bm_font->face->has_char(glyph)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
extents->x_bearing = 0;
|
||||
extents->y_bearing = 0;
|
||||
extents->width = bm_font->face->get_size(glyph, bm_font->font_size).x * 64;
|
||||
extents->height = bm_font->face->get_size(glyph, bm_font->font_size).y * 64;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static hb_bool_t hb_bmp_get_font_h_extents(hb_font_t *font, void *font_data, hb_font_extents_t *metrics, void *user_data) {
|
||||
const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
|
||||
|
||||
if (!bm_font->face) {
|
||||
return false;
|
||||
}
|
||||
|
||||
metrics->ascender = bm_font->face->get_ascent(bm_font->font_size);
|
||||
metrics->descender = bm_font->face->get_descent(bm_font->font_size);
|
||||
metrics->line_gap = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static hb_font_funcs_t *_hb_bmp_get_font_funcs() {
|
||||
hb_font_funcs_t *funcs = hb_font_funcs_create();
|
||||
|
||||
hb_font_funcs_set_font_h_extents_func(funcs, hb_bmp_get_font_h_extents, nullptr, nullptr);
|
||||
//hb_font_funcs_set_font_v_extents_func (funcs, hb_bmp_get_font_v_extents, nullptr, nullptr);
|
||||
hb_font_funcs_set_nominal_glyph_func(funcs, hb_bmp_get_nominal_glyph, nullptr, nullptr);
|
||||
//hb_font_funcs_set_variation_glyph_func (funcs, hb_bmp_get_variation_glyph, nullptr, nullptr);
|
||||
hb_font_funcs_set_glyph_h_advance_func(funcs, hb_bmp_get_glyph_h_advance, nullptr, nullptr);
|
||||
hb_font_funcs_set_glyph_v_advance_func(funcs, hb_bmp_get_glyph_v_advance, nullptr, nullptr);
|
||||
//hb_font_funcs_set_glyph_h_origin_func(funcs, hb_bmp_get_glyph_h_origin, nullptr, nullptr);
|
||||
hb_font_funcs_set_glyph_v_origin_func(funcs, hb_bmp_get_glyph_v_origin, nullptr, nullptr);
|
||||
hb_font_funcs_set_glyph_h_kerning_func(funcs, hb_bmp_get_glyph_h_kerning, nullptr, nullptr);
|
||||
//hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_bmp_get_glyph_v_kerning, nullptr, nullptr);
|
||||
hb_font_funcs_set_glyph_extents_func(funcs, hb_bmp_get_glyph_extents, nullptr, nullptr);
|
||||
//hb_font_funcs_set_glyph_contour_point_func (funcs, hb_bmp_get_glyph_contour_point, nullptr, nullptr);
|
||||
//hb_font_funcs_set_glyph_name_func (funcs, hb_bmp_get_glyph_name, nullptr, nullptr);
|
||||
//hb_font_funcs_set_glyph_from_name_func (funcs, hb_bmp_get_glyph_from_name, nullptr, nullptr);
|
||||
|
||||
hb_font_funcs_make_immutable(funcs);
|
||||
|
||||
return funcs;
|
||||
}
|
||||
|
||||
static void _hb_bmp_font_set_funcs(hb_font_t *p_font, BitmapFontDataAdvanced *p_face, int p_size, bool p_unref) {
|
||||
hb_font_set_funcs(p_font, _hb_bmp_get_font_funcs(), _hb_bmp_font_create(p_face, p_size, p_unref), _hb_bmp_font_destroy);
|
||||
}
|
||||
|
||||
hb_font_t *hb_bmp_font_create(BitmapFontDataAdvanced *p_face, int p_size, hb_destroy_func_t p_destroy) {
|
||||
hb_font_t *font;
|
||||
hb_face_t *face = hb_face_create(nullptr, 0);
|
||||
|
||||
font = hb_font_create(face);
|
||||
hb_face_destroy(face);
|
||||
_hb_bmp_font_set_funcs(font, p_face, p_size, false);
|
||||
return font;
|
||||
}
|
||||
|
||||
/*************************************************************************/
|
||||
/* BitmapFontDataAdvanced */
|
||||
/*************************************************************************/
|
||||
|
||||
Error BitmapFontDataAdvanced::load_from_file(const String &p_filename, int p_base_size) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
//fnt format used by angelcode bmfont
|
||||
//http://www.angelcode.com/products/bmfont/
|
||||
|
||||
FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
|
||||
ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_filename + ".");
|
||||
|
||||
while (true) {
|
||||
String line = f->get_line();
|
||||
|
||||
int delimiter = line.find(" ");
|
||||
String type = line.substr(0, delimiter);
|
||||
int pos = delimiter + 1;
|
||||
Map<String, String> keys;
|
||||
|
||||
while (pos < line.size() && line[pos] == ' ') {
|
||||
pos++;
|
||||
}
|
||||
|
||||
while (pos < line.size()) {
|
||||
int eq = line.find("=", pos);
|
||||
if (eq == -1) {
|
||||
break;
|
||||
}
|
||||
String key = line.substr(pos, eq - pos);
|
||||
int end = -1;
|
||||
String value;
|
||||
if (line[eq + 1] == '"') {
|
||||
end = line.find("\"", eq + 2);
|
||||
if (end == -1) {
|
||||
break;
|
||||
}
|
||||
value = line.substr(eq + 2, end - 1 - eq - 1);
|
||||
pos = end + 1;
|
||||
} else {
|
||||
end = line.find(" ", eq + 1);
|
||||
if (end == -1) {
|
||||
end = line.size();
|
||||
}
|
||||
value = line.substr(eq + 1, end - eq);
|
||||
pos = end;
|
||||
}
|
||||
|
||||
while (pos < line.size() && line[pos] == ' ') {
|
||||
pos++;
|
||||
}
|
||||
|
||||
keys[key] = value;
|
||||
}
|
||||
|
||||
if (type == "info") {
|
||||
if (keys.has("size")) {
|
||||
base_size = keys["size"].to_int();
|
||||
}
|
||||
} else if (type == "common") {
|
||||
if (keys.has("lineHeight")) {
|
||||
height = keys["lineHeight"].to_int();
|
||||
}
|
||||
if (keys.has("base")) {
|
||||
ascent = keys["base"].to_int();
|
||||
}
|
||||
} else if (type == "page") {
|
||||
if (keys.has("file")) {
|
||||
String base_dir = p_filename.get_base_dir();
|
||||
String file = base_dir.plus_file(keys["file"]);
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
Ref<Texture2D> tex = ResourceLoader::load(file);
|
||||
if (tex.is_null()) {
|
||||
ERR_PRINT("Can't load font texture!");
|
||||
} else {
|
||||
ERR_FAIL_COND_V_MSG(tex.is_null(), ERR_FILE_CANT_READ, "It's not a reference to a valid Texture object.");
|
||||
textures.push_back(tex);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (type == "char") {
|
||||
Character c;
|
||||
char32_t idx = 0;
|
||||
if (keys.has("id")) {
|
||||
idx = keys["id"].to_int();
|
||||
}
|
||||
if (keys.has("x")) {
|
||||
c.rect.position.x = keys["x"].to_int();
|
||||
}
|
||||
if (keys.has("y")) {
|
||||
c.rect.position.y = keys["y"].to_int();
|
||||
}
|
||||
if (keys.has("width")) {
|
||||
c.rect.size.width = keys["width"].to_int();
|
||||
}
|
||||
if (keys.has("height")) {
|
||||
c.rect.size.height = keys["height"].to_int();
|
||||
}
|
||||
if (keys.has("xoffset")) {
|
||||
c.align.x = keys["xoffset"].to_int();
|
||||
}
|
||||
if (keys.has("yoffset")) {
|
||||
c.align.y = keys["yoffset"].to_int();
|
||||
}
|
||||
if (keys.has("page")) {
|
||||
c.texture_idx = keys["page"].to_int();
|
||||
}
|
||||
if (keys.has("xadvance")) {
|
||||
c.advance.x = keys["xadvance"].to_int();
|
||||
}
|
||||
if (keys.has("yadvance")) {
|
||||
c.advance.x = keys["yadvance"].to_int();
|
||||
}
|
||||
if (c.advance.x < 0) {
|
||||
c.advance.x = c.rect.size.width + 1;
|
||||
}
|
||||
if (c.advance.y < 0) {
|
||||
c.advance.y = c.rect.size.height + 1;
|
||||
}
|
||||
char_map[idx] = c;
|
||||
} else if (type == "kerning") {
|
||||
KerningPairKey kpk;
|
||||
float k = 0;
|
||||
if (keys.has("first")) {
|
||||
kpk.A = keys["first"].to_int();
|
||||
}
|
||||
if (keys.has("second")) {
|
||||
kpk.B = keys["second"].to_int();
|
||||
}
|
||||
if (keys.has("amount")) {
|
||||
k = keys["amount"].to_int();
|
||||
}
|
||||
kerning_map[kpk] = k;
|
||||
}
|
||||
|
||||
if (f->eof_reached()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (base_size == 0) {
|
||||
base_size = height;
|
||||
}
|
||||
|
||||
valid = true;
|
||||
|
||||
memdelete(f);
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error BitmapFontDataAdvanced::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(p_data == nullptr, ERR_CANT_CREATE);
|
||||
ERR_FAIL_COND_V(p_size != sizeof(TextServer::BitmapFontData), ERR_CANT_CREATE);
|
||||
|
||||
const TextServer::BitmapFontData *data = (const TextServer::BitmapFontData *)p_data;
|
||||
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
Ref<Image> image = memnew(Image(data->img));
|
||||
Ref<ImageTexture> tex = memnew(ImageTexture);
|
||||
tex->create_from_image(image);
|
||||
|
||||
textures.push_back(tex);
|
||||
}
|
||||
|
||||
for (int i = 0; i < data->charcount; i++) {
|
||||
const int *c = &data->char_rects[i * 8];
|
||||
|
||||
Character chr;
|
||||
chr.rect.position.x = c[1];
|
||||
chr.rect.position.y = c[2];
|
||||
chr.rect.size.x = c[3];
|
||||
chr.rect.size.y = c[4];
|
||||
chr.texture_idx = 0;
|
||||
if (c[7] < 0) {
|
||||
chr.advance.x = c[3];
|
||||
} else {
|
||||
chr.advance.x = c[7];
|
||||
}
|
||||
chr.align = Vector2(c[6], c[5]);
|
||||
char_map[c[0]] = chr;
|
||||
}
|
||||
|
||||
for (int i = 0; i < data->kerning_count; i++) {
|
||||
KerningPairKey kpk;
|
||||
kpk.A = data->kernings[i * 3 + 0];
|
||||
kpk.B = data->kernings[i * 3 + 1];
|
||||
|
||||
if (data->kernings[i * 3 + 2] == 0 && kerning_map.has(kpk)) {
|
||||
kerning_map.erase(kpk);
|
||||
} else {
|
||||
kerning_map[kpk] = data->kernings[i * 3 + 2];
|
||||
}
|
||||
}
|
||||
|
||||
height = data->height;
|
||||
ascent = data->ascent;
|
||||
|
||||
base_size = p_base_size;
|
||||
if (base_size == 0) {
|
||||
base_size = height;
|
||||
}
|
||||
|
||||
valid = true;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
float BitmapFontDataAdvanced::get_height(int p_size) const {
|
||||
ERR_FAIL_COND_V(!valid, 0.f);
|
||||
return height * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
float BitmapFontDataAdvanced::get_ascent(int p_size) const {
|
||||
ERR_FAIL_COND_V(!valid, 0.f);
|
||||
return ascent * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
float BitmapFontDataAdvanced::get_descent(int p_size) const {
|
||||
ERR_FAIL_COND_V(!valid, 0.f);
|
||||
return (height - ascent) * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
float BitmapFontDataAdvanced::get_underline_position(int p_size) const {
|
||||
ERR_FAIL_COND_V(!valid, 0.f);
|
||||
return 2 * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
float BitmapFontDataAdvanced::get_underline_thickness(int p_size) const {
|
||||
ERR_FAIL_COND_V(!valid, 0.f);
|
||||
return 1 * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
void BitmapFontDataAdvanced::set_distance_field_hint(bool p_distance_field) {
|
||||
distance_field_hint = p_distance_field;
|
||||
}
|
||||
|
||||
bool BitmapFontDataAdvanced::get_distance_field_hint() const {
|
||||
return distance_field_hint;
|
||||
}
|
||||
|
||||
float BitmapFontDataAdvanced::get_base_size() const {
|
||||
return base_size;
|
||||
}
|
||||
|
||||
hb_font_t *BitmapFontDataAdvanced::get_hb_handle(int p_size) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, nullptr);
|
||||
if (!cache.has(p_size)) {
|
||||
cache[p_size] = hb_bmp_font_create(this, p_size, nullptr);
|
||||
}
|
||||
return cache[p_size];
|
||||
}
|
||||
|
||||
bool BitmapFontDataAdvanced::has_char(char32_t p_char) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, false);
|
||||
return char_map.has(p_char);
|
||||
}
|
||||
|
||||
String BitmapFontDataAdvanced::get_supported_chars() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, String());
|
||||
String chars;
|
||||
const uint32_t *k = nullptr;
|
||||
while ((k = char_map.next(k))) {
|
||||
chars += char32_t(*k);
|
||||
}
|
||||
return chars;
|
||||
}
|
||||
|
||||
Vector2 BitmapFontDataAdvanced::get_advance(uint32_t p_char, int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, Vector2());
|
||||
const Character *c = char_map.getptr(p_char);
|
||||
ERR_FAIL_COND_V(c == nullptr, Vector2());
|
||||
|
||||
return c->advance * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
Vector2 BitmapFontDataAdvanced::get_align(uint32_t p_char, int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, Vector2());
|
||||
const Character *c = char_map.getptr(p_char);
|
||||
ERR_FAIL_COND_V(c == nullptr, Vector2());
|
||||
|
||||
return c->align * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
Vector2 BitmapFontDataAdvanced::get_size(uint32_t p_char, int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, Vector2());
|
||||
const Character *c = char_map.getptr(p_char);
|
||||
ERR_FAIL_COND_V(c == nullptr, Vector2());
|
||||
|
||||
return c->rect.size * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
Vector2 BitmapFontDataAdvanced::get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
ERR_FAIL_COND_V(!valid, Vector2());
|
||||
KerningPairKey kpk;
|
||||
kpk.A = p_char;
|
||||
kpk.B = p_next;
|
||||
|
||||
const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
|
||||
if (E) {
|
||||
return Vector2(-E->get() * (float(p_size) / float(base_size)), 0.f);
|
||||
} else {
|
||||
return Vector2();
|
||||
}
|
||||
}
|
||||
|
||||
Vector2 BitmapFontDataAdvanced::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
if (p_index == 0) {
|
||||
return Vector2();
|
||||
}
|
||||
ERR_FAIL_COND_V(!valid, Vector2());
|
||||
const Character *c = char_map.getptr(p_index);
|
||||
|
||||
ERR_FAIL_COND_V(c == nullptr, Vector2());
|
||||
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
|
||||
if (c->texture_idx != -1) {
|
||||
Point2 cpos = p_pos;
|
||||
cpos += c->align * (float(p_size) / float(base_size));
|
||||
cpos.y -= ascent * (float(p_size) / float(base_size));
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
if (distance_field_hint) {
|
||||
RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true);
|
||||
}
|
||||
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, c->rect.size * (float(p_size) / float(base_size))), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, RID(), RID(), Color(1, 1, 1, 1), false);
|
||||
if (distance_field_hint) {
|
||||
RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c->advance * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
Vector2 BitmapFontDataAdvanced::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
if (p_index == 0) {
|
||||
return Vector2();
|
||||
}
|
||||
ERR_FAIL_COND_V(!valid, Vector2());
|
||||
const Character *c = char_map.getptr(p_index);
|
||||
|
||||
ERR_FAIL_COND_V(c == nullptr, Vector2());
|
||||
ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
|
||||
|
||||
// Not supported, return advance for compatibility.
|
||||
|
||||
return c->advance * (float(p_size) / float(base_size));
|
||||
}
|
||||
|
||||
BitmapFontDataAdvanced::~BitmapFontDataAdvanced() {
|
||||
for (Map<float, hb_font_t *>::Element *E = cache.front(); E; E = E->next()) {
|
||||
hb_font_destroy(E->get());
|
||||
}
|
||||
}
|
115
modules/text_server_adv/bitmap_font_adv.h
Normal file
115
modules/text_server_adv/bitmap_font_adv.h
Normal file
@ -0,0 +1,115 @@
|
||||
/*************************************************************************/
|
||||
/* bitmap_font_adv.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef BITMAP_FONT_ADV_H
|
||||
#define BITMAP_FONT_ADV_H
|
||||
|
||||
#include "font_adv.h"
|
||||
|
||||
struct BitmapFontDataAdvanced : public FontDataAdvanced {
|
||||
_THREAD_SAFE_CLASS_
|
||||
|
||||
private:
|
||||
Vector<Ref<Texture2D>> textures;
|
||||
|
||||
struct Character {
|
||||
int texture_idx = 0;
|
||||
Rect2 rect;
|
||||
Vector2 align;
|
||||
Vector2 advance = Vector2(-1, -1);
|
||||
};
|
||||
|
||||
struct KerningPairKey {
|
||||
union {
|
||||
struct {
|
||||
uint32_t A, B;
|
||||
};
|
||||
|
||||
uint64_t pair = 0;
|
||||
};
|
||||
|
||||
_FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
|
||||
};
|
||||
|
||||
HashMap<uint32_t, Character> char_map;
|
||||
Map<KerningPairKey, int> kerning_map;
|
||||
Map<float, hb_font_t *> cache;
|
||||
|
||||
float height = 0.f;
|
||||
float ascent = 0.f;
|
||||
float base_size = 0.f;
|
||||
bool distance_field_hint = false;
|
||||
|
||||
public:
|
||||
virtual void clear_cache() override{};
|
||||
|
||||
virtual Error load_from_file(const String &p_filename, int p_base_size) override;
|
||||
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override;
|
||||
|
||||
virtual float get_height(int p_size) const override;
|
||||
virtual float get_ascent(int p_size) const override;
|
||||
virtual float get_descent(int p_size) const override;
|
||||
|
||||
virtual float get_underline_position(int p_size) const override;
|
||||
virtual float get_underline_thickness(int p_size) const override;
|
||||
|
||||
virtual void set_antialiased(bool p_antialiased) override{};
|
||||
virtual bool get_antialiased() const override { return false; };
|
||||
|
||||
virtual void set_hinting(TextServer::Hinting p_hinting) override{};
|
||||
virtual TextServer::Hinting get_hinting() const override { return TextServer::HINTING_NONE; };
|
||||
|
||||
virtual void set_distance_field_hint(bool p_distance_field) override;
|
||||
virtual bool get_distance_field_hint() const override;
|
||||
|
||||
virtual void set_force_autohinter(bool p_enabeld) override{};
|
||||
virtual bool get_force_autohinter() const override { return false; };
|
||||
|
||||
virtual bool has_outline() const override { return false; };
|
||||
virtual float get_base_size() const override;
|
||||
|
||||
virtual hb_font_t *get_hb_handle(int p_size) override;
|
||||
|
||||
virtual bool has_char(char32_t p_char) const override;
|
||||
virtual String get_supported_chars() const override;
|
||||
|
||||
virtual Vector2 get_advance(uint32_t p_char, int p_size) const override;
|
||||
Vector2 get_align(uint32_t p_char, int p_size) const;
|
||||
Vector2 get_size(uint32_t p_char, int p_size) const;
|
||||
virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const override;
|
||||
virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const override { return (uint32_t)p_char; };
|
||||
|
||||
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
|
||||
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
|
||||
|
||||
virtual ~BitmapFontDataAdvanced();
|
||||
};
|
||||
|
||||
#endif // BITMAP_FONT_ADV_H
|
@ -1,5 +1,6 @@
|
||||
def can_build(env, platform):
|
||||
return True
|
||||
return env.module_check_dependencies("text_server_adv", ["freetype"])
|
||||
|
||||
|
||||
def configure(env):
|
||||
pass
|
||||
|
973
modules/text_server_adv/dynamic_font_adv.cpp
Normal file
973
modules/text_server_adv/dynamic_font_adv.cpp
Normal file
@ -0,0 +1,973 @@
|
||||
/*************************************************************************/
|
||||
/* dynamic_font_adv.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "dynamic_font_adv.h"
|
||||
|
||||
#include FT_STROKER_H
|
||||
#include FT_ADVANCES_H
|
||||
|
||||
HashMap<String, Vector<uint8_t>> DynamicFontDataAdvanced::font_mem_cache;
|
||||
|
||||
DynamicFontDataAdvanced::DataAtSize *DynamicFontDataAdvanced::get_data_for_size(int p_size, int p_outline_size) {
|
||||
ERR_FAIL_COND_V(!valid, nullptr);
|
||||
ERR_FAIL_COND_V(p_size < 0 || p_size > UINT16_MAX, nullptr);
|
||||
ERR_FAIL_COND_V(p_outline_size < 0 || p_outline_size > UINT16_MAX, nullptr);
|
||||
|
||||
CacheID id;
|
||||
id.size = p_size;
|
||||
id.outline_size = p_outline_size;
|
||||
|
||||
DataAtSize *fds = nullptr;
|
||||
Map<CacheID, DataAtSize *>::Element *E = nullptr;
|
||||
if (p_outline_size != 0) {
|
||||
E = size_cache_outline.find(id);
|
||||
} else {
|
||||
E = size_cache.find(id);
|
||||
}
|
||||
|
||||
if (E != nullptr) {
|
||||
fds = E->get();
|
||||
} else {
|
||||
// FT_OPEN_STREAM is extremely slow only on Android.
|
||||
if (OS::get_singleton()->get_name() == "Android" && font_mem == nullptr && font_path != String()) {
|
||||
if (font_mem_cache.has(font_path)) {
|
||||
font_mem = font_mem_cache[font_path].ptr();
|
||||
font_mem_size = font_mem_cache[font_path].size();
|
||||
} else {
|
||||
FileAccess *f = FileAccess::open(font_path, FileAccess::READ);
|
||||
if (!f) {
|
||||
ERR_FAIL_V_MSG(nullptr, "Cannot open font file '" + font_path + "'.");
|
||||
}
|
||||
|
||||
size_t len = f->get_len();
|
||||
font_mem_cache[font_path] = Vector<uint8_t>();
|
||||
Vector<uint8_t> &fontdata = font_mem_cache[font_path];
|
||||
fontdata.resize(len);
|
||||
f->get_buffer(fontdata.ptrw(), len);
|
||||
font_mem = fontdata.ptr();
|
||||
font_mem_size = len;
|
||||
f->close();
|
||||
}
|
||||
}
|
||||
|
||||
int error = 0;
|
||||
fds = memnew(DataAtSize);
|
||||
if (font_mem == nullptr && font_path != String()) {
|
||||
FileAccess *f = FileAccess::open(font_path, FileAccess::READ);
|
||||
if (!f) {
|
||||
memdelete(fds);
|
||||
ERR_FAIL_V_MSG(nullptr, "Cannot open font file '" + font_path + "'.");
|
||||
}
|
||||
|
||||
memset(&fds->stream, 0, sizeof(FT_StreamRec));
|
||||
fds->stream.base = nullptr;
|
||||
fds->stream.size = f->get_len();
|
||||
fds->stream.pos = 0;
|
||||
fds->stream.descriptor.pointer = f;
|
||||
fds->stream.read = _ft_stream_io;
|
||||
fds->stream.close = _ft_stream_close;
|
||||
|
||||
FT_Open_Args fargs;
|
||||
memset(&fargs, 0, sizeof(FT_Open_Args));
|
||||
fargs.flags = FT_OPEN_STREAM;
|
||||
fargs.stream = &fds->stream;
|
||||
error = FT_Open_Face(library, &fargs, 0, &fds->face);
|
||||
} else if (font_mem) {
|
||||
memset(&fds->stream, 0, sizeof(FT_StreamRec));
|
||||
fds->stream.base = (unsigned char *)font_mem;
|
||||
fds->stream.size = font_mem_size;
|
||||
fds->stream.pos = 0;
|
||||
|
||||
FT_Open_Args fargs;
|
||||
memset(&fargs, 0, sizeof(FT_Open_Args));
|
||||
fargs.memory_base = (unsigned char *)font_mem;
|
||||
fargs.memory_size = font_mem_size;
|
||||
fargs.flags = FT_OPEN_MEMORY;
|
||||
fargs.stream = &fds->stream;
|
||||
error = FT_Open_Face(library, &fargs, 0, &fds->face);
|
||||
|
||||
} else {
|
||||
memdelete(fds);
|
||||
ERR_FAIL_V_MSG(nullptr, "DynamicFont uninitialized.");
|
||||
}
|
||||
|
||||
if (error == FT_Err_Unknown_File_Format) {
|
||||
memdelete(fds);
|
||||
ERR_FAIL_V_MSG(nullptr, "Unknown font format.");
|
||||
} else if (error) {
|
||||
memdelete(fds);
|
||||
ERR_FAIL_V_MSG(nullptr, "Error loading font.");
|
||||
}
|
||||
|
||||
oversampling = TS->font_get_oversampling();
|
||||
|
||||
if (FT_HAS_COLOR(fds->face) && fds->face->num_fixed_sizes > 0) {
|
||||
int best_match = 0;
|
||||
int diff = ABS(p_size - ((int64_t)fds->face->available_sizes[0].width));
|
||||
fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[0].width;
|
||||
for (int i = 1; i < fds->face->num_fixed_sizes; i++) {
|
||||
int ndiff = ABS(p_size - ((int64_t)fds->face->available_sizes[i].width));
|
||||
if (ndiff < diff) {
|
||||
best_match = i;
|
||||
diff = ndiff;
|
||||
fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[i].width;
|
||||
}
|
||||
}
|
||||
FT_Select_Size(fds->face, best_match);
|
||||
} else {
|
||||
FT_Set_Pixel_Sizes(fds->face, 0, p_size * oversampling);
|
||||
}
|
||||
|
||||
fds->size = p_size;
|
||||
fds->ascent = (fds->face->size->metrics.ascender / 64.0) / oversampling * fds->scale_color_font;
|
||||
fds->descent = (-fds->face->size->metrics.descender / 64.0) / oversampling * fds->scale_color_font;
|
||||
fds->underline_position = -fds->face->underline_position / 64.0 / oversampling * fds->scale_color_font;
|
||||
fds->underline_thickness = fds->face->underline_thickness / 64.0 / oversampling * fds->scale_color_font;
|
||||
|
||||
//Load os2 TTF pable
|
||||
fds->os2 = (TT_OS2 *)FT_Get_Sfnt_Table(fds->face, FT_SFNT_OS2);
|
||||
|
||||
fds->hb_handle = hb_ft_font_create(fds->face, nullptr);
|
||||
if (fds->hb_handle == nullptr) {
|
||||
memdelete(fds);
|
||||
ERR_FAIL_V_MSG(nullptr, "Error loading HB font.");
|
||||
}
|
||||
if (p_outline_size != 0) {
|
||||
size_cache_outline[id] = fds;
|
||||
} else {
|
||||
size_cache[id] = fds;
|
||||
}
|
||||
}
|
||||
|
||||
return fds;
|
||||
}
|
||||
|
||||
unsigned long DynamicFontDataAdvanced::_ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count) {
|
||||
FileAccess *f = (FileAccess *)stream->descriptor.pointer;
|
||||
|
||||
if (f->get_position() != offset) {
|
||||
f->seek(offset);
|
||||
}
|
||||
if (count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return f->get_buffer(buffer, count);
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::_ft_stream_close(FT_Stream stream) {
|
||||
FileAccess *f = (FileAccess *)stream->descriptor.pointer;
|
||||
f->close();
|
||||
memdelete(f);
|
||||
}
|
||||
|
||||
Dictionary DynamicFontDataAdvanced::get_feature_list() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
|
||||
if (fds == nullptr) {
|
||||
return Dictionary();
|
||||
}
|
||||
|
||||
Dictionary out;
|
||||
// Read feature flags.
|
||||
unsigned int count = hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, NULL, NULL);
|
||||
if (count != 0) {
|
||||
hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
|
||||
hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, &count, feature_tags);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
out[feature_tags[i]] = 1;
|
||||
}
|
||||
memfree(feature_tags);
|
||||
}
|
||||
count = hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, NULL, NULL);
|
||||
if (count != 0) {
|
||||
hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
|
||||
hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, &count, feature_tags);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
out[feature_tags[i]] = 1;
|
||||
}
|
||||
memfree(feature_tags);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
DynamicFontDataAdvanced::TexturePosition DynamicFontDataAdvanced::find_texture_pos_for_glyph(DynamicFontDataAdvanced::DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) {
|
||||
TexturePosition ret;
|
||||
ret.index = -1;
|
||||
ret.x = 0;
|
||||
ret.y = 0;
|
||||
|
||||
int mw = p_width;
|
||||
int mh = p_height;
|
||||
|
||||
for (int i = 0; i < p_data->textures.size(); i++) {
|
||||
const CharTexture &ct = p_data->textures[i];
|
||||
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
if (ct.texture->get_format() != p_image_format) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (mw > ct.texture_size || mh > ct.texture_size) { //too big for this texture
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.y = 0x7FFFFFFF;
|
||||
ret.x = 0;
|
||||
|
||||
for (int j = 0; j < ct.texture_size - mw; j++) {
|
||||
int max_y = 0;
|
||||
|
||||
for (int k = j; k < j + mw; k++) {
|
||||
int y = ct.offsets[k];
|
||||
if (y > max_y) {
|
||||
max_y = y;
|
||||
}
|
||||
}
|
||||
|
||||
if (max_y < ret.y) {
|
||||
ret.y = max_y;
|
||||
ret.x = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret.y == 0x7FFFFFFF || ret.y + mh > ct.texture_size) {
|
||||
continue; //fail, could not fit it here
|
||||
}
|
||||
|
||||
ret.index = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret.index == -1) {
|
||||
//could not find texture to fit, create one
|
||||
ret.x = 0;
|
||||
ret.y = 0;
|
||||
|
||||
int texsize = MAX(p_data->size * oversampling * 8, 256);
|
||||
if (mw > texsize) {
|
||||
texsize = mw; //special case, adapt to it?
|
||||
}
|
||||
if (mh > texsize) {
|
||||
texsize = mh; //special case, adapt to it?
|
||||
}
|
||||
|
||||
texsize = next_power_of_2(texsize);
|
||||
|
||||
texsize = MIN(texsize, 4096);
|
||||
|
||||
CharTexture tex;
|
||||
tex.texture_size = texsize;
|
||||
tex.imgdata.resize(texsize * texsize * p_color_size); //grayscale alpha
|
||||
|
||||
{
|
||||
//zero texture
|
||||
uint8_t *w = tex.imgdata.ptrw();
|
||||
ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
|
||||
// Initialize the texture to all-white pixels to prevent artifacts when the
|
||||
// font is displayed at a non-default scale with filtering enabled.
|
||||
if (p_color_size == 2) {
|
||||
for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8
|
||||
w[i + 0] = 255;
|
||||
w[i + 1] = 0;
|
||||
}
|
||||
} else if (p_color_size == 4) {
|
||||
for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8
|
||||
w[i + 0] = 255;
|
||||
w[i + 1] = 255;
|
||||
w[i + 2] = 255;
|
||||
w[i + 3] = 0;
|
||||
}
|
||||
} else {
|
||||
ERR_FAIL_V(ret);
|
||||
}
|
||||
}
|
||||
tex.offsets.resize(texsize);
|
||||
for (int i = 0; i < texsize; i++) { //zero offsets
|
||||
tex.offsets.write[i] = 0;
|
||||
}
|
||||
|
||||
p_data->textures.push_back(tex);
|
||||
ret.index = p_data->textures.size() - 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
DynamicFontDataAdvanced::Character DynamicFontDataAdvanced::Character::not_found() {
|
||||
Character ch;
|
||||
return ch;
|
||||
}
|
||||
|
||||
DynamicFontDataAdvanced::Character DynamicFontDataAdvanced::bitmap_to_character(DynamicFontDataAdvanced::DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) {
|
||||
int w = bitmap.width;
|
||||
int h = bitmap.rows;
|
||||
|
||||
int mw = w + rect_margin * 2;
|
||||
int mh = h + rect_margin * 2;
|
||||
|
||||
ERR_FAIL_COND_V(mw > 4096, Character::not_found());
|
||||
ERR_FAIL_COND_V(mh > 4096, Character::not_found());
|
||||
|
||||
int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
|
||||
Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
|
||||
|
||||
TexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh);
|
||||
ERR_FAIL_COND_V(tex_pos.index < 0, Character::not_found());
|
||||
|
||||
//fit character in char texture
|
||||
|
||||
CharTexture &tex = p_data->textures.write[tex_pos.index];
|
||||
|
||||
{
|
||||
uint8_t *wr = tex.imgdata.ptrw();
|
||||
|
||||
for (int i = 0; i < h; i++) {
|
||||
for (int j = 0; j < w; j++) {
|
||||
int ofs = ((i + tex_pos.y + rect_margin) * tex.texture_size + j + tex_pos.x + rect_margin) * color_size;
|
||||
ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), Character::not_found());
|
||||
switch (bitmap.pixel_mode) {
|
||||
case FT_PIXEL_MODE_MONO: {
|
||||
int byte = i * bitmap.pitch + (j >> 3);
|
||||
int bit = 1 << (7 - (j % 8));
|
||||
wr[ofs + 0] = 255; //grayscale as 1
|
||||
wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
|
||||
} break;
|
||||
case FT_PIXEL_MODE_GRAY:
|
||||
wr[ofs + 0] = 255; //grayscale as 1
|
||||
wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
|
||||
break;
|
||||
case FT_PIXEL_MODE_BGRA: {
|
||||
int ofs_color = i * bitmap.pitch + (j << 2);
|
||||
wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
|
||||
wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
|
||||
wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
|
||||
wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
|
||||
} break;
|
||||
// TODO: FT_PIXEL_MODE_LCD
|
||||
default:
|
||||
ERR_FAIL_V_MSG(Character::not_found(), "Font uses unsupported pixel format: " + itos(bitmap.pixel_mode) + ".");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//blit to image and texture
|
||||
{
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
Ref<Image> img = memnew(Image(tex.texture_size, tex.texture_size, 0, require_format, tex.imgdata));
|
||||
|
||||
if (tex.texture.is_null()) {
|
||||
tex.texture.instance();
|
||||
tex.texture->create_from_image(img);
|
||||
} else {
|
||||
tex.texture->update(img); //update
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update height array
|
||||
for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
|
||||
tex.offsets.write[k] = tex_pos.y + mh;
|
||||
}
|
||||
|
||||
Character chr;
|
||||
chr.align = Vector2(xofs, -yofs) * p_data->scale_color_font / oversampling;
|
||||
chr.advance = advance * p_data->scale_color_font / oversampling;
|
||||
chr.texture_idx = tex_pos.index;
|
||||
chr.found = true;
|
||||
|
||||
chr.rect_uv = Rect2(tex_pos.x + rect_margin, tex_pos.y + rect_margin, w, h);
|
||||
chr.rect = chr.rect_uv;
|
||||
chr.rect.position /= oversampling;
|
||||
chr.rect.size *= (p_data->scale_color_font / oversampling);
|
||||
return chr;
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::update_glyph(int p_size, uint32_t p_index) {
|
||||
DataAtSize *fds = get_data_for_size(p_size, false);
|
||||
ERR_FAIL_COND(fds == nullptr);
|
||||
|
||||
if (fds->glyph_map.has(p_index)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Character character = Character::not_found();
|
||||
FT_GlyphSlot slot = fds->face->glyph;
|
||||
|
||||
if (p_index == 0) {
|
||||
fds->glyph_map[p_index] = character;
|
||||
return;
|
||||
}
|
||||
|
||||
int ft_hinting;
|
||||
switch (hinting) {
|
||||
case TextServer::HINTING_NONE:
|
||||
ft_hinting = FT_LOAD_NO_HINTING;
|
||||
break;
|
||||
case TextServer::HINTING_LIGHT:
|
||||
ft_hinting = FT_LOAD_TARGET_LIGHT;
|
||||
break;
|
||||
default:
|
||||
ft_hinting = FT_LOAD_TARGET_NORMAL;
|
||||
break;
|
||||
}
|
||||
|
||||
FT_Fixed v, h;
|
||||
FT_Get_Advance(fds->face, p_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting, &h);
|
||||
FT_Get_Advance(fds->face, p_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting | FT_LOAD_VERTICAL_LAYOUT, &v);
|
||||
|
||||
int error = FT_Load_Glyph(fds->face, p_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting);
|
||||
if (error) {
|
||||
fds->glyph_map[p_index] = character;
|
||||
return;
|
||||
}
|
||||
|
||||
error = FT_Render_Glyph(fds->face->glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
|
||||
if (!error) {
|
||||
character = bitmap_to_character(fds, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
|
||||
}
|
||||
|
||||
fds->glyph_map[p_index] = character;
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::update_glyph_outline(int p_size, int p_outline_size, uint32_t p_index) {
|
||||
DataAtSize *fds = get_data_for_size(p_size, p_outline_size);
|
||||
ERR_FAIL_COND(fds == nullptr);
|
||||
|
||||
if (fds->glyph_map.has(p_index)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Character character = Character::not_found();
|
||||
if (p_index == 0) {
|
||||
fds->glyph_map[p_index] = character;
|
||||
return;
|
||||
}
|
||||
|
||||
int error = FT_Load_Glyph(fds->face, p_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
|
||||
if (error) {
|
||||
fds->glyph_map[p_index] = character;
|
||||
return;
|
||||
}
|
||||
|
||||
FT_Stroker stroker;
|
||||
if (FT_Stroker_New(library, &stroker) != 0) {
|
||||
fds->glyph_map[p_index] = character;
|
||||
return;
|
||||
}
|
||||
|
||||
FT_Stroker_Set(stroker, (int)(p_outline_size * oversampling * 64.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
|
||||
FT_Glyph glyph;
|
||||
FT_BitmapGlyph glyph_bitmap;
|
||||
|
||||
if (FT_Get_Glyph(fds->face->glyph, &glyph) != 0) {
|
||||
goto cleanup_stroker;
|
||||
}
|
||||
if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
|
||||
goto cleanup_glyph;
|
||||
}
|
||||
if (FT_Glyph_To_Bitmap(&glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0) {
|
||||
goto cleanup_glyph;
|
||||
}
|
||||
|
||||
glyph_bitmap = (FT_BitmapGlyph)glyph;
|
||||
character = bitmap_to_character(fds, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2());
|
||||
|
||||
cleanup_glyph:
|
||||
FT_Done_Glyph(glyph);
|
||||
cleanup_stroker:
|
||||
FT_Stroker_Done(stroker);
|
||||
|
||||
fds->glyph_map[p_index] = character;
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::clear_cache() {
|
||||
_THREAD_SAFE_METHOD_
|
||||
for (Map<CacheID, DataAtSize *>::Element *E = size_cache.front(); E; E = E->next()) {
|
||||
memdelete(E->get());
|
||||
}
|
||||
size_cache.clear();
|
||||
for (Map<CacheID, DataAtSize *>::Element *E = size_cache_outline.front(); E; E = E->next()) {
|
||||
memdelete(E->get());
|
||||
}
|
||||
size_cache_outline.clear();
|
||||
}
|
||||
|
||||
Error DynamicFontDataAdvanced::load_from_file(const String &p_filename, int p_base_size) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
if (library == nullptr) {
|
||||
int error = FT_Init_FreeType(&library);
|
||||
ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
|
||||
}
|
||||
clear_cache();
|
||||
|
||||
font_path = p_filename;
|
||||
base_size = p_base_size;
|
||||
|
||||
valid = true;
|
||||
DataAtSize *fds = get_data_for_size(base_size); // load base size.
|
||||
if (fds == nullptr) {
|
||||
valid = false;
|
||||
ERR_FAIL_V(ERR_CANT_CREATE);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error DynamicFontDataAdvanced::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
if (library == nullptr) {
|
||||
int error = FT_Init_FreeType(&library);
|
||||
ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
|
||||
}
|
||||
clear_cache();
|
||||
|
||||
font_mem = p_data;
|
||||
font_mem_size = p_size;
|
||||
base_size = p_base_size;
|
||||
|
||||
valid = true;
|
||||
DataAtSize *fds = get_data_for_size(base_size); // load base size.
|
||||
if (fds == nullptr) {
|
||||
valid = false;
|
||||
ERR_FAIL_V(ERR_CANT_CREATE);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_height(int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 0.f);
|
||||
return fds->ascent + fds->descent;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_ascent(int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 0.f);
|
||||
return fds->ascent;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_descent(int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 0.f);
|
||||
return fds->descent;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_underline_position(int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 0.f);
|
||||
return fds->underline_position;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_underline_thickness(int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 0.f);
|
||||
return fds->underline_thickness;
|
||||
}
|
||||
|
||||
bool DynamicFontDataAdvanced::is_script_supported(uint32_t p_script) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, false);
|
||||
|
||||
unsigned int count = hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, NULL, NULL);
|
||||
if (count != 0) {
|
||||
hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
|
||||
hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, &count, script_tags);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (p_script == script_tags[i]) {
|
||||
memfree(script_tags);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
memfree(script_tags);
|
||||
}
|
||||
count = hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, NULL, NULL);
|
||||
if (count != 0) {
|
||||
hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
|
||||
hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, &count, script_tags);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (p_script == script_tags[i]) {
|
||||
memfree(script_tags);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
memfree(script_tags);
|
||||
}
|
||||
|
||||
if (!fds->os2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (p_script) {
|
||||
case HB_SCRIPT_COMMON:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 4) || (fds->os2->ulUnicodeRange1 & 1L << 5) || (fds->os2->ulUnicodeRange1 & 1L << 6) || (fds->os2->ulUnicodeRange1 & 1L << 31) || (fds->os2->ulUnicodeRange2 & 1L << 0) || (fds->os2->ulUnicodeRange2 & 1L << 1) || (fds->os2->ulUnicodeRange2 & 1L << 2) || (fds->os2->ulUnicodeRange2 & 1L << 3) || (fds->os2->ulUnicodeRange2 & 1L << 4) || (fds->os2->ulUnicodeRange2 & 1L << 5) || (fds->os2->ulUnicodeRange2 & 1L << 6) || (fds->os2->ulUnicodeRange2 & 1L << 7) || (fds->os2->ulUnicodeRange2 & 1L << 8) || (fds->os2->ulUnicodeRange2 & 1L << 9) || (fds->os2->ulUnicodeRange2 & 1L << 10) || (fds->os2->ulUnicodeRange2 & 1L << 11) || (fds->os2->ulUnicodeRange2 & 1L << 12) || (fds->os2->ulUnicodeRange2 & 1L << 13) || (fds->os2->ulUnicodeRange2 & 1L << 14) || (fds->os2->ulUnicodeRange2 & 1L << 15) || (fds->os2->ulUnicodeRange2 & 1L << 30) || (fds->os2->ulUnicodeRange3 & 1L << 0) || (fds->os2->ulUnicodeRange3 & 1L << 1) || (fds->os2->ulUnicodeRange3 & 1L << 2) || (fds->os2->ulUnicodeRange3 & 1L << 4) || (fds->os2->ulUnicodeRange3 & 1L << 5) || (fds->os2->ulUnicodeRange3 & 1L << 18) || (fds->os2->ulUnicodeRange3 & 1L << 24) || (fds->os2->ulUnicodeRange3 & 1L << 25) || (fds->os2->ulUnicodeRange3 & 1L << 26) || (fds->os2->ulUnicodeRange3 & 1L << 27) || (fds->os2->ulUnicodeRange3 & 1L << 28) || (fds->os2->ulUnicodeRange4 & 1L << 3) || (fds->os2->ulUnicodeRange4 & 1L << 6) || (fds->os2->ulUnicodeRange4 & 1L << 15) || (fds->os2->ulUnicodeRange4 & 1L << 23) || (fds->os2->ulUnicodeRange4 & 1L << 24) || (fds->os2->ulUnicodeRange4 & 1L << 26);
|
||||
case HB_SCRIPT_LATIN:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 0) || (fds->os2->ulUnicodeRange1 & 1L << 1) || (fds->os2->ulUnicodeRange1 & 1L << 2) || (fds->os2->ulUnicodeRange1 & 1L << 3) || (fds->os2->ulUnicodeRange1 & 1L << 29);
|
||||
case HB_SCRIPT_GREEK:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 7) || (fds->os2->ulUnicodeRange1 & 1L << 30);
|
||||
case HB_SCRIPT_COPTIC:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 8);
|
||||
case HB_SCRIPT_CYRILLIC:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 9);
|
||||
case HB_SCRIPT_ARMENIAN:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 10);
|
||||
case HB_SCRIPT_HEBREW:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 11);
|
||||
case HB_SCRIPT_VAI:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 12);
|
||||
case HB_SCRIPT_ARABIC:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 13) || (fds->os2->ulUnicodeRange2 & 1L << 31) || (fds->os2->ulUnicodeRange3 & 1L << 3);
|
||||
case HB_SCRIPT_NKO:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 14);
|
||||
case HB_SCRIPT_DEVANAGARI:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 15);
|
||||
case HB_SCRIPT_BENGALI:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 16);
|
||||
case HB_SCRIPT_GURMUKHI:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 17);
|
||||
case HB_SCRIPT_GUJARATI:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 18);
|
||||
case HB_SCRIPT_ORIYA:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 19);
|
||||
case HB_SCRIPT_TAMIL:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 20);
|
||||
case HB_SCRIPT_TELUGU:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 21);
|
||||
case HB_SCRIPT_KANNADA:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 22);
|
||||
case HB_SCRIPT_MALAYALAM:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 23);
|
||||
case HB_SCRIPT_THAI:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 24);
|
||||
case HB_SCRIPT_LAO:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 25);
|
||||
case HB_SCRIPT_GEORGIAN:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 26);
|
||||
case HB_SCRIPT_BALINESE:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 27);
|
||||
case HB_SCRIPT_HANGUL:
|
||||
return (fds->os2->ulUnicodeRange1 & 1L << 28) || (fds->os2->ulUnicodeRange2 & 1L << 20) || (fds->os2->ulUnicodeRange2 & 1L << 24);
|
||||
case HB_SCRIPT_HAN:
|
||||
return (fds->os2->ulUnicodeRange2 & 1L << 21) || (fds->os2->ulUnicodeRange2 & 1L << 22) || (fds->os2->ulUnicodeRange2 & 1L << 23) || (fds->os2->ulUnicodeRange2 & 1L << 26) || (fds->os2->ulUnicodeRange2 & 1L << 27) || (fds->os2->ulUnicodeRange2 & 1L << 29);
|
||||
case HB_SCRIPT_HIRAGANA:
|
||||
return (fds->os2->ulUnicodeRange2 & 1L << 17);
|
||||
case HB_SCRIPT_KATAKANA:
|
||||
return (fds->os2->ulUnicodeRange2 & 1L << 18);
|
||||
case HB_SCRIPT_BOPOMOFO:
|
||||
return (fds->os2->ulUnicodeRange2 & 1L << 19);
|
||||
case HB_SCRIPT_TIBETAN:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 6);
|
||||
case HB_SCRIPT_SYRIAC:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 7);
|
||||
case HB_SCRIPT_THAANA:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 8);
|
||||
case HB_SCRIPT_SINHALA:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 9);
|
||||
case HB_SCRIPT_MYANMAR:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 10);
|
||||
case HB_SCRIPT_ETHIOPIC:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 11);
|
||||
case HB_SCRIPT_CHEROKEE:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 12);
|
||||
case HB_SCRIPT_CANADIAN_SYLLABICS:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 13);
|
||||
case HB_SCRIPT_OGHAM:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 14);
|
||||
case HB_SCRIPT_RUNIC:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 15);
|
||||
case HB_SCRIPT_KHMER:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 16);
|
||||
case HB_SCRIPT_MONGOLIAN:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 17);
|
||||
case HB_SCRIPT_YI:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 19);
|
||||
case HB_SCRIPT_HANUNOO:
|
||||
case HB_SCRIPT_TAGBANWA:
|
||||
case HB_SCRIPT_BUHID:
|
||||
case HB_SCRIPT_TAGALOG:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 20);
|
||||
case HB_SCRIPT_OLD_ITALIC:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 21);
|
||||
case HB_SCRIPT_GOTHIC:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 22);
|
||||
case HB_SCRIPT_DESERET:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 23);
|
||||
case HB_SCRIPT_LIMBU:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 29);
|
||||
case HB_SCRIPT_TAI_LE:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 30);
|
||||
case HB_SCRIPT_NEW_TAI_LUE:
|
||||
return (fds->os2->ulUnicodeRange3 & 1L << 31);
|
||||
case HB_SCRIPT_BUGINESE:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 0);
|
||||
case HB_SCRIPT_GLAGOLITIC:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 1);
|
||||
case HB_SCRIPT_TIFINAGH:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 2);
|
||||
case HB_SCRIPT_SYLOTI_NAGRI:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 4);
|
||||
case HB_SCRIPT_LINEAR_B:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 5);
|
||||
case HB_SCRIPT_UGARITIC:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 7);
|
||||
case HB_SCRIPT_OLD_PERSIAN:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 8);
|
||||
case HB_SCRIPT_SHAVIAN:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 9);
|
||||
case HB_SCRIPT_OSMANYA:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 10);
|
||||
case HB_SCRIPT_CYPRIOT:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 11);
|
||||
case HB_SCRIPT_KHAROSHTHI:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 12);
|
||||
case HB_SCRIPT_TAI_VIET:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 13);
|
||||
case HB_SCRIPT_CUNEIFORM:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 14);
|
||||
case HB_SCRIPT_SUNDANESE:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 16);
|
||||
case HB_SCRIPT_LEPCHA:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 17);
|
||||
case HB_SCRIPT_OL_CHIKI:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 18);
|
||||
case HB_SCRIPT_SAURASHTRA:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 19);
|
||||
case HB_SCRIPT_KAYAH_LI:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 20);
|
||||
case HB_SCRIPT_REJANG:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 21);
|
||||
case HB_SCRIPT_CHAM:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 22);
|
||||
case HB_SCRIPT_ANATOLIAN_HIEROGLYPHS:
|
||||
return (fds->os2->ulUnicodeRange4 & 1L << 25);
|
||||
default:
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::set_antialiased(bool p_antialiased) {
|
||||
if (antialiased != p_antialiased) {
|
||||
clear_cache();
|
||||
antialiased = p_antialiased;
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicFontDataAdvanced::get_antialiased() const {
|
||||
return antialiased;
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::set_force_autohinter(bool p_enabled) {
|
||||
if (force_autohinter != p_enabled) {
|
||||
clear_cache();
|
||||
force_autohinter = p_enabled;
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicFontDataAdvanced::get_force_autohinter() const {
|
||||
return force_autohinter;
|
||||
}
|
||||
|
||||
void DynamicFontDataAdvanced::set_hinting(TextServer::Hinting p_hinting) {
|
||||
if (hinting != p_hinting) {
|
||||
clear_cache();
|
||||
hinting = p_hinting;
|
||||
}
|
||||
}
|
||||
|
||||
TextServer::Hinting DynamicFontDataAdvanced::get_hinting() const {
|
||||
return hinting;
|
||||
}
|
||||
|
||||
bool DynamicFontDataAdvanced::has_outline() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_base_size() const {
|
||||
return base_size;
|
||||
}
|
||||
|
||||
String DynamicFontDataAdvanced::get_supported_chars() const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, String());
|
||||
|
||||
String chars;
|
||||
|
||||
FT_UInt gindex;
|
||||
FT_ULong charcode = FT_Get_First_Char(fds->face, &gindex);
|
||||
while (gindex != 0) {
|
||||
if (charcode != 0) {
|
||||
chars += char32_t(charcode);
|
||||
}
|
||||
charcode = FT_Get_Next_Char(fds->face, charcode, &gindex);
|
||||
}
|
||||
|
||||
return chars;
|
||||
}
|
||||
|
||||
float DynamicFontDataAdvanced::get_font_scale(int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 1.0f);
|
||||
|
||||
return fds->scale_color_font / oversampling;
|
||||
}
|
||||
|
||||
bool DynamicFontDataAdvanced::has_char(char32_t p_char) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, false);
|
||||
|
||||
const_cast<DynamicFontDataAdvanced *>(this)->update_glyph(base_size, FT_Get_Char_Index(fds->face, p_char));
|
||||
Character ch = fds->glyph_map[FT_Get_Char_Index(fds->face, p_char)];
|
||||
|
||||
return (ch.found);
|
||||
}
|
||||
|
||||
hb_font_t *DynamicFontDataAdvanced::get_hb_handle(int p_size) {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, nullptr);
|
||||
|
||||
return fds->hb_handle;
|
||||
}
|
||||
|
||||
uint32_t DynamicFontDataAdvanced::get_glyph_index(char32_t p_char, char32_t p_variation_selector) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, 0);
|
||||
|
||||
if (p_variation_selector == 0x0000) {
|
||||
return FT_Get_Char_Index(fds->face, p_char);
|
||||
} else {
|
||||
return FT_Face_GetCharVariantIndex(fds->face, p_char, p_variation_selector);
|
||||
}
|
||||
}
|
||||
|
||||
Vector2 DynamicFontDataAdvanced::get_advance(uint32_t p_index, int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, Vector2());
|
||||
|
||||
const_cast<DynamicFontDataAdvanced *>(this)->update_glyph(p_size, p_index);
|
||||
Character ch = fds->glyph_map[p_index];
|
||||
|
||||
return ch.advance;
|
||||
}
|
||||
|
||||
Vector2 DynamicFontDataAdvanced::get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, Vector2());
|
||||
|
||||
FT_Vector delta;
|
||||
FT_Get_Kerning(fds->face, p_char, p_next, FT_KERNING_DEFAULT, &delta);
|
||||
return Vector2(delta.x, delta.y);
|
||||
}
|
||||
|
||||
Vector2 DynamicFontDataAdvanced::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, Vector2());
|
||||
|
||||
const_cast<DynamicFontDataAdvanced *>(this)->update_glyph(p_size, p_index);
|
||||
Character ch = fds->glyph_map[p_index];
|
||||
|
||||
Vector2 advance;
|
||||
if (ch.found) {
|
||||
ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
|
||||
|
||||
if (ch.texture_idx != -1) {
|
||||
Point2 cpos = p_pos;
|
||||
cpos += ch.align;
|
||||
Color modulate = p_color;
|
||||
if (FT_HAS_COLOR(fds->face)) {
|
||||
modulate.r = modulate.g = modulate.b = 1.0;
|
||||
}
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
RID texture = fds->textures[ch.texture_idx].texture->get_rid();
|
||||
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, RID(), RID(), Color(1, 1, 1, 1), false);
|
||||
}
|
||||
}
|
||||
|
||||
advance = ch.advance;
|
||||
}
|
||||
|
||||
return advance;
|
||||
}
|
||||
|
||||
Vector2 DynamicFontDataAdvanced::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
|
||||
_THREAD_SAFE_METHOD_
|
||||
DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size, p_outline_size);
|
||||
ERR_FAIL_COND_V(fds == nullptr, Vector2());
|
||||
|
||||
const_cast<DynamicFontDataAdvanced *>(this)->update_glyph_outline(p_size, p_outline_size, p_index);
|
||||
Character ch = fds->glyph_map[p_index];
|
||||
|
||||
Vector2 advance;
|
||||
if (ch.found) {
|
||||
ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
|
||||
|
||||
if (ch.texture_idx != -1) {
|
||||
Point2 cpos = p_pos;
|
||||
cpos += ch.align;
|
||||
Color modulate = p_color;
|
||||
if (FT_HAS_COLOR(fds->face)) {
|
||||
modulate.r = modulate.g = modulate.b = 1.0;
|
||||
}
|
||||
if (RenderingServer::get_singleton() != nullptr) {
|
||||
RID texture = fds->textures[ch.texture_idx].texture->get_rid();
|
||||
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, RID(), RID(), Color(1, 1, 1, 1), false);
|
||||
}
|
||||
}
|
||||
|
||||
advance = ch.advance;
|
||||
}
|
||||
|
||||
return advance;
|
||||
}
|
||||
|
||||
DynamicFontDataAdvanced::~DynamicFontDataAdvanced() {
|
||||
clear_cache();
|
||||
if (library != nullptr) {
|
||||
FT_Done_FreeType(library);
|
||||
}
|
||||
}
|
188
modules/text_server_adv/dynamic_font_adv.h
Normal file
188
modules/text_server_adv/dynamic_font_adv.h
Normal file
@ -0,0 +1,188 @@
|
||||
/*************************************************************************/
|
||||
/* dynamic_font_adv.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef DYNAMIC_FONT_ADV_H
|
||||
#define DYNAMIC_FONT_ADV_H
|
||||
|
||||
#include "font_adv.h"
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_TRUETYPE_TABLES_H
|
||||
|
||||
#include <hb-ft.h>
|
||||
#include <hb-ot.h>
|
||||
|
||||
struct DynamicFontDataAdvanced : public FontDataAdvanced {
|
||||
_THREAD_SAFE_CLASS_
|
||||
|
||||
private:
|
||||
struct CharTexture {
|
||||
Vector<uint8_t> imgdata;
|
||||
int texture_size = 0;
|
||||
Vector<int> offsets;
|
||||
Ref<ImageTexture> texture;
|
||||
};
|
||||
|
||||
struct Character {
|
||||
bool found = false;
|
||||
int texture_idx = 0;
|
||||
Rect2 rect;
|
||||
Rect2 rect_uv;
|
||||
Vector2 align;
|
||||
Vector2 advance = Vector2(-1, -1);
|
||||
|
||||
static Character not_found();
|
||||
};
|
||||
|
||||
struct TexturePosition {
|
||||
int index = 0;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
};
|
||||
|
||||
struct CacheID {
|
||||
union {
|
||||
struct {
|
||||
uint32_t size : 16;
|
||||
uint32_t outline_size : 16;
|
||||
};
|
||||
uint32_t key;
|
||||
};
|
||||
bool operator<(CacheID right) const {
|
||||
return key < right.key;
|
||||
}
|
||||
CacheID() {
|
||||
key = 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct DataAtSize {
|
||||
FT_Face face = nullptr;
|
||||
TT_OS2 *os2 = nullptr;
|
||||
FT_StreamRec stream;
|
||||
|
||||
int size = 0;
|
||||
float scale_color_font = 1.f;
|
||||
float ascent = 0;
|
||||
float descent = 0;
|
||||
float underline_position = 0;
|
||||
float underline_thickness = 0;
|
||||
|
||||
Vector<CharTexture> textures;
|
||||
HashMap<uint32_t, Character> glyph_map;
|
||||
|
||||
hb_font_t *hb_handle = nullptr;
|
||||
~DataAtSize() {
|
||||
if (hb_handle != nullptr) {
|
||||
hb_font_destroy(hb_handle);
|
||||
}
|
||||
if (face != nullptr) {
|
||||
FT_Done_Face(face);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
FT_Library library = nullptr;
|
||||
|
||||
// Source data.
|
||||
const uint8_t *font_mem = nullptr;
|
||||
int font_mem_size = 0;
|
||||
String font_path;
|
||||
static HashMap<String, Vector<uint8_t>> font_mem_cache;
|
||||
|
||||
float rect_margin = 1.f;
|
||||
int base_size = 16;
|
||||
float oversampling = 1.f;
|
||||
bool antialiased = true;
|
||||
bool force_autohinter = false;
|
||||
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
|
||||
|
||||
Map<CacheID, DataAtSize *> size_cache;
|
||||
Map<CacheID, DataAtSize *> size_cache_outline;
|
||||
|
||||
static unsigned long _ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count);
|
||||
static void _ft_stream_close(FT_Stream stream);
|
||||
|
||||
DataAtSize *get_data_for_size(int p_size, int p_outline_size = 0);
|
||||
|
||||
TexturePosition find_texture_pos_for_glyph(DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height);
|
||||
Character bitmap_to_character(DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance);
|
||||
_FORCE_INLINE_ void update_glyph(int p_size, uint32_t p_index);
|
||||
_FORCE_INLINE_ void update_glyph_outline(int p_size, int p_outline_size, uint32_t p_index);
|
||||
|
||||
public:
|
||||
virtual void clear_cache() override;
|
||||
|
||||
virtual Error load_from_file(const String &p_filename, int p_base_size) override;
|
||||
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override;
|
||||
|
||||
virtual float get_height(int p_size) const override;
|
||||
virtual float get_ascent(int p_size) const override;
|
||||
virtual float get_descent(int p_size) const override;
|
||||
|
||||
virtual Dictionary get_feature_list() const override;
|
||||
|
||||
virtual float get_underline_position(int p_size) const override;
|
||||
virtual float get_underline_thickness(int p_size) const override;
|
||||
|
||||
virtual void set_antialiased(bool p_antialiased) override;
|
||||
virtual bool get_antialiased() const override;
|
||||
|
||||
virtual void set_hinting(TextServer::Hinting p_hinting) override;
|
||||
virtual TextServer::Hinting get_hinting() const override;
|
||||
|
||||
virtual void set_force_autohinter(bool p_enabled) override;
|
||||
virtual bool get_force_autohinter() const override;
|
||||
|
||||
virtual void set_distance_field_hint(bool p_distance_field) override{};
|
||||
virtual bool get_distance_field_hint() const override { return false; };
|
||||
|
||||
virtual bool has_outline() const override;
|
||||
virtual float get_base_size() const override;
|
||||
|
||||
virtual bool is_script_supported(uint32_t p_script) const override;
|
||||
|
||||
virtual bool has_char(char32_t p_char) const override;
|
||||
virtual String get_supported_chars() const override;
|
||||
virtual float get_font_scale(int p_size) const override;
|
||||
|
||||
virtual hb_font_t *get_hb_handle(int p_size) override;
|
||||
virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const override;
|
||||
virtual Vector2 get_advance(uint32_t p_index, int p_size) const override;
|
||||
virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const override;
|
||||
|
||||
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
|
||||
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
|
||||
|
||||
virtual ~DynamicFontDataAdvanced() override;
|
||||
};
|
||||
|
||||
#endif // DYNAMIC_FONT_ADV_H
|
90
modules/text_server_adv/font_adv.h
Normal file
90
modules/text_server_adv/font_adv.h
Normal file
@ -0,0 +1,90 @@
|
||||
/*************************************************************************/
|
||||
/* font_adv.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef FONT_ADV_H
|
||||
#define FONT_ADV_H
|
||||
|
||||
#include "servers/text_server.h"
|
||||
|
||||
#include <hb.h>
|
||||
|
||||
struct FontDataAdvanced {
|
||||
Map<String, bool> lang_support_overrides;
|
||||
Map<String, bool> script_support_overrides;
|
||||
bool valid = false;
|
||||
|
||||
virtual void clear_cache() = 0;
|
||||
|
||||
virtual Error load_from_file(const String &p_filename, int p_base_size) = 0;
|
||||
virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) = 0;
|
||||
|
||||
virtual float get_height(int p_size) const = 0;
|
||||
virtual float get_ascent(int p_size) const = 0;
|
||||
virtual float get_descent(int p_size) const = 0;
|
||||
|
||||
virtual Dictionary get_feature_list() const { return Dictionary(); };
|
||||
|
||||
virtual float get_underline_position(int p_size) const = 0;
|
||||
virtual float get_underline_thickness(int p_size) const = 0;
|
||||
|
||||
virtual void set_antialiased(bool p_antialiased) = 0;
|
||||
virtual bool get_antialiased() const = 0;
|
||||
|
||||
virtual void set_hinting(TextServer::Hinting p_hinting) = 0;
|
||||
virtual TextServer::Hinting get_hinting() const = 0;
|
||||
|
||||
virtual void set_distance_field_hint(bool p_distance_field) = 0;
|
||||
virtual bool get_distance_field_hint() const = 0;
|
||||
|
||||
virtual void set_force_autohinter(bool p_enabeld) = 0;
|
||||
virtual bool get_force_autohinter() const = 0;
|
||||
|
||||
virtual bool has_outline() const = 0;
|
||||
virtual float get_base_size() const = 0;
|
||||
|
||||
virtual bool is_lang_supported(const String &p_lang) const { return false; };
|
||||
virtual bool is_script_supported(uint32_t p_script) const { return false; };
|
||||
|
||||
virtual bool has_char(char32_t p_char) const = 0;
|
||||
virtual String get_supported_chars() const = 0;
|
||||
virtual float get_font_scale(int p_size) const { return 1.0f; };
|
||||
|
||||
virtual hb_font_t *get_hb_handle(int p_size) = 0;
|
||||
virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const = 0;
|
||||
virtual Vector2 get_advance(uint32_t p_char, int p_size) const = 0;
|
||||
virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const = 0;
|
||||
|
||||
virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0;
|
||||
virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0;
|
||||
|
||||
virtual ~FontDataAdvanced(){};
|
||||
};
|
||||
|
||||
#endif // FONT_ADV_H
|
43
modules/text_server_adv/register_types.cpp
Normal file
43
modules/text_server_adv/register_types.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/*************************************************************************/
|
||||
/* register_types.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "register_types.h"
|
||||
|
||||
#include "text_server_adv.h"
|
||||
|
||||
void preregister_text_server_adv_types() {
|
||||
TextServerAdvanced::register_server();
|
||||
}
|
||||
|
||||
void register_text_server_adv_types() {
|
||||
}
|
||||
|
||||
void unregister_text_server_adv_types() {
|
||||
}
|
40
modules/text_server_adv/register_types.h
Normal file
40
modules/text_server_adv/register_types.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*************************************************************************/
|
||||
/* register_types.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef TEXT_SERVER_ADV_REGISTER_TYPES_H
|
||||
#define TEXT_SERVER_ADV_REGISTER_TYPES_H
|
||||
|
||||
#define MODULE_TEXT_SERVER_ADV_HAS_PREREGISTER
|
||||
|
||||
void preregister_text_server_adv_types();
|
||||
void register_text_server_adv_types();
|
||||
void unregister_text_server_adv_types();
|
||||
|
||||
#endif // TEXT_SERVER_ADV_REGISTER_TYPES_H
|
109
modules/text_server_adv/script_iterator.cpp
Normal file
109
modules/text_server_adv/script_iterator.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
/*************************************************************************/
|
||||
/* script_iterator.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "script_iterator.h"
|
||||
|
||||
bool ScriptIterator::same_script(int32_t p_script_one, int32_t p_script_two) {
|
||||
return p_script_one <= USCRIPT_INHERITED || p_script_two <= USCRIPT_INHERITED || p_script_one == p_script_two;
|
||||
}
|
||||
|
||||
ScriptIterator::ScriptIterator(const String &p_string, int p_start, int p_length) {
|
||||
struct ParenStackEntry {
|
||||
int pair_index;
|
||||
UScriptCode script_code;
|
||||
};
|
||||
|
||||
if (p_start >= p_length) {
|
||||
p_start = p_length - 1;
|
||||
}
|
||||
|
||||
if (p_start < 0) {
|
||||
p_start = 0;
|
||||
}
|
||||
|
||||
ParenStackEntry paren_stack[128];
|
||||
|
||||
int script_start;
|
||||
int script_end = p_start;
|
||||
UScriptCode script_code;
|
||||
int paren_sp = -1;
|
||||
int start_sp = paren_sp;
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
|
||||
do {
|
||||
script_code = USCRIPT_COMMON;
|
||||
for (script_start = script_end; script_end < p_length; script_end++) {
|
||||
UChar32 ch = p_string[script_end];
|
||||
UScriptCode sc = uscript_getScript(ch, &err);
|
||||
if (U_FAILURE(err)) {
|
||||
ERR_FAIL_MSG(u_errorName(err));
|
||||
}
|
||||
if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) != U_BPT_NONE) {
|
||||
if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_OPEN) {
|
||||
paren_stack[++paren_sp].pair_index = ch;
|
||||
paren_stack[paren_sp].script_code = script_code;
|
||||
} else if (paren_sp >= 0) {
|
||||
UChar32 paired_ch = u_getBidiPairedBracket(ch);
|
||||
while (paren_sp >= 0 && paren_stack[paren_sp].pair_index != paired_ch) {
|
||||
paren_sp -= 1;
|
||||
}
|
||||
if (paren_sp < start_sp)
|
||||
start_sp = paren_sp;
|
||||
if (paren_sp >= 0)
|
||||
sc = paren_stack[paren_sp].script_code;
|
||||
}
|
||||
}
|
||||
|
||||
if (same_script(script_code, sc)) {
|
||||
if (script_code <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
|
||||
script_code = sc;
|
||||
while (start_sp < paren_sp) {
|
||||
paren_stack[++start_sp].script_code = script_code;
|
||||
}
|
||||
}
|
||||
if ((u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_CLOSE) && paren_sp >= 0) {
|
||||
paren_sp -= 1;
|
||||
if (start_sp >= 0) {
|
||||
start_sp -= 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ScriptRange rng;
|
||||
rng.script = hb_icu_script_to_script(script_code);
|
||||
rng.start = script_start;
|
||||
rng.end = script_end;
|
||||
|
||||
script_ranges.push_back(rng);
|
||||
} while (script_end < p_length);
|
||||
}
|
61
modules/text_server_adv/script_iterator.h
Normal file
61
modules/text_server_adv/script_iterator.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*************************************************************************/
|
||||
/* script_iterator.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef SCRIPT_ITERATOR_H
|
||||
#define SCRIPT_ITERATOR_H
|
||||
|
||||
#include "servers/text_server.h"
|
||||
|
||||
#include <unicode/uchar.h>
|
||||
#include <unicode/uloc.h>
|
||||
#include <unicode/uscript.h>
|
||||
#include <unicode/ustring.h>
|
||||
#include <unicode/utypes.h>
|
||||
|
||||
#include <hb-icu.h>
|
||||
#include <hb.h>
|
||||
|
||||
class ScriptIterator {
|
||||
public:
|
||||
struct ScriptRange {
|
||||
int start;
|
||||
int end;
|
||||
hb_script_t script;
|
||||
};
|
||||
Vector<ScriptRange> script_ranges;
|
||||
|
||||
private:
|
||||
static bool same_script(int32_t p_script_one, int32_t p_script_two);
|
||||
|
||||
public:
|
||||
ScriptIterator(const String &p_string, int p_start, int p_length);
|
||||
};
|
||||
|
||||
#endif //SCRIPT_ITERATOR_H
|
2520
modules/text_server_adv/text_server_adv.cpp
Normal file
2520
modules/text_server_adv/text_server_adv.cpp
Normal file
File diff suppressed because it is too large
Load Diff
244
modules/text_server_adv/text_server_adv.h
Normal file
244
modules/text_server_adv/text_server_adv.h
Normal file
@ -0,0 +1,244 @@
|
||||
/*************************************************************************/
|
||||
/* text_server_adv.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef TEXT_SERVER_ADV_H
|
||||
#define TEXT_SERVER_ADV_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* ICU/HarfBuzz/Graphite backed Text Server implementation with BiDi, */
|
||||
/* shaping and advanced font features support. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "servers/text_server.h"
|
||||
|
||||
#include "core/templates/rid_owner.h"
|
||||
#include "scene/resources/texture.h"
|
||||
#include "script_iterator.h"
|
||||
|
||||
#include <unicode/ubidi.h>
|
||||
#include <unicode/ubrk.h>
|
||||
#include <unicode/uchar.h>
|
||||
#include <unicode/uclean.h>
|
||||
#include <unicode/udata.h>
|
||||
#include <unicode/uiter.h>
|
||||
#include <unicode/uloc.h>
|
||||
#include <unicode/uscript.h>
|
||||
#include <unicode/ustring.h>
|
||||
#include <unicode/utypes.h>
|
||||
|
||||
#include <hb-icu.h>
|
||||
#include <hb.h>
|
||||
|
||||
#include "font_adv.h"
|
||||
|
||||
class TextServerAdvanced : public TextServer {
|
||||
GDCLASS(TextServerAdvanced, TextServer);
|
||||
_THREAD_SAFE_CLASS_
|
||||
|
||||
static String interface_name;
|
||||
static uint32_t interface_features;
|
||||
|
||||
uint8_t *icu_data = nullptr;
|
||||
|
||||
struct ShapedTextDataAdvanced : public ShapedTextData {
|
||||
/* Intermediate data */
|
||||
Char16String utf16;
|
||||
Vector<UBiDi *> bidi_iter;
|
||||
Vector<Vector2i> bidi_override;
|
||||
ScriptIterator *script_iter = nullptr;
|
||||
hb_buffer_t *hb_buffer = nullptr;
|
||||
|
||||
~ShapedTextDataAdvanced() {
|
||||
for (int i = 0; i < bidi_iter.size(); i++) {
|
||||
ubidi_close(bidi_iter[i]);
|
||||
}
|
||||
if (script_iter) {
|
||||
memdelete(script_iter);
|
||||
}
|
||||
if (hb_buffer) {
|
||||
hb_buffer_destroy(hb_buffer);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
float oversampling = 1.f;
|
||||
mutable RID_PtrOwner<FontDataAdvanced> font_owner;
|
||||
mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner;
|
||||
|
||||
int _convert_pos(const ShapedTextDataAdvanced *p_sd, int p_pos) const;
|
||||
int _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int p_pos) const;
|
||||
void _shape_run(ShapedTextDataAdvanced *p_sd, int32_t p_start, int32_t p_end, hb_script_t p_script, hb_direction_t p_direction, Vector<RID> p_fonts, int p_span, int p_fb_index);
|
||||
TextServer::Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, RID p_font, int p_font_size);
|
||||
|
||||
protected:
|
||||
static void _bind_methods(){};
|
||||
|
||||
void full_copy(ShapedTextDataAdvanced *p_shaped);
|
||||
void invalidate(ShapedTextDataAdvanced *p_shaped);
|
||||
|
||||
public:
|
||||
virtual bool has_feature(Feature p_feature) override;
|
||||
virtual String get_name() const override;
|
||||
|
||||
virtual void free(RID p_rid) override;
|
||||
virtual bool has(RID p_rid) override;
|
||||
virtual bool load_support_data(const String &p_filename) override;
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
virtual String get_support_data_filename() override { return _MKSTR(ICU_DATA_NAME); };
|
||||
virtual String get_support_data_info() override { return String("ICU break iteration data (") + _MKSTR(ICU_DATA_NAME) + String(")."); };
|
||||
virtual bool save_support_data(const String &p_filename) override;
|
||||
#endif
|
||||
|
||||
virtual bool is_locale_right_to_left(const String &p_locale) override;
|
||||
|
||||
virtual int32_t name_to_tag(const String &p_name) override;
|
||||
virtual String tag_to_name(int32_t p_tag) override;
|
||||
|
||||
/* Font interface */
|
||||
virtual RID create_font_system(const String &p_name, int p_base_size = 16) override;
|
||||
virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override;
|
||||
virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override;
|
||||
|
||||
virtual float font_get_height(RID p_font, int p_size) const override;
|
||||
virtual float font_get_ascent(RID p_font, int p_size) const override;
|
||||
virtual float font_get_descent(RID p_font, int p_size) const override;
|
||||
|
||||
virtual float font_get_underline_position(RID p_font, int p_size) const override;
|
||||
virtual float font_get_underline_thickness(RID p_font, int p_size) const override;
|
||||
|
||||
virtual void font_set_antialiased(RID p_font, bool p_antialiased) override;
|
||||
virtual bool font_get_antialiased(RID p_font) const override;
|
||||
|
||||
virtual Dictionary font_get_feature_list(RID p_font) const override;
|
||||
|
||||
virtual void font_set_hinting(RID p_font, Hinting p_hinting) override;
|
||||
virtual Hinting font_get_hinting(RID p_font) const override;
|
||||
|
||||
virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) override;
|
||||
virtual bool font_get_distance_field_hint(RID p_font) const override;
|
||||
|
||||
virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) override;
|
||||
virtual bool font_get_force_autohinter(RID p_font) const override;
|
||||
|
||||
virtual bool font_has_char(RID p_font, char32_t p_char) const override;
|
||||
virtual String font_get_supported_chars(RID p_font) const override;
|
||||
|
||||
virtual bool font_has_outline(RID p_font) const override;
|
||||
virtual float font_get_base_size(RID p_font) const override;
|
||||
|
||||
virtual bool font_is_language_supported(RID p_font, const String &p_language) const override;
|
||||
virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) override;
|
||||
virtual bool font_get_language_support_override(RID p_font, const String &p_language) override;
|
||||
virtual void font_remove_language_support_override(RID p_font, const String &p_language) override;
|
||||
Vector<String> font_get_language_support_overrides(RID p_font) override;
|
||||
|
||||
virtual bool font_is_script_supported(RID p_font, const String &p_script) const override;
|
||||
virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) override;
|
||||
virtual bool font_get_script_support_override(RID p_font, const String &p_script) override;
|
||||
virtual void font_remove_script_support_override(RID p_font, const String &p_script) override;
|
||||
Vector<String> font_get_script_support_overrides(RID p_font) override;
|
||||
|
||||
virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const override;
|
||||
virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const override;
|
||||
virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const override;
|
||||
|
||||
virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
|
||||
virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override;
|
||||
|
||||
virtual float font_get_oversampling() const override;
|
||||
virtual void font_set_oversampling(float p_oversampling) override;
|
||||
|
||||
virtual Vector<String> get_system_fonts() const override;
|
||||
|
||||
/* Shaped text buffer interface */
|
||||
|
||||
virtual RID create_shaped_text(Direction p_direction = DIRECTION_AUTO, Orientation p_orientation = ORIENTATION_HORIZONTAL) override;
|
||||
|
||||
virtual void shaped_text_clear(RID p_shaped) override;
|
||||
|
||||
virtual void shaped_text_set_direction(RID p_shaped, Direction p_direction = DIRECTION_AUTO) override;
|
||||
virtual Direction shaped_text_get_direction(RID p_shaped) const override;
|
||||
|
||||
virtual void shaped_text_set_bidi_override(RID p_shaped, const Vector<Vector2i> &p_override) override;
|
||||
|
||||
virtual void shaped_text_set_orientation(RID p_shaped, Orientation p_orientation = ORIENTATION_HORIZONTAL) override;
|
||||
virtual Orientation shaped_text_get_orientation(RID p_shaped) const override;
|
||||
|
||||
virtual void shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) override;
|
||||
virtual bool shaped_text_get_preserve_invalid(RID p_shaped) const override;
|
||||
|
||||
virtual void shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) override;
|
||||
virtual bool shaped_text_get_preserve_control(RID p_shaped) const override;
|
||||
|
||||
virtual bool shaped_text_add_string(RID p_shaped, const String &p_text, const Vector<RID> &p_fonts, int p_size, const Dictionary &p_opentype_features = Dictionary(), const String &p_language = "") override;
|
||||
virtual bool shaped_text_add_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align = VALIGN_CENTER, int p_length = 1) override;
|
||||
virtual bool shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align = VALIGN_CENTER) override;
|
||||
|
||||
virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const override;
|
||||
virtual RID shaped_text_get_parent(RID p_shaped) const override;
|
||||
|
||||
virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
|
||||
virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) override;
|
||||
|
||||
virtual bool shaped_text_shape(RID p_shaped) override;
|
||||
virtual bool shaped_text_update_breaks(RID p_shaped) override;
|
||||
virtual bool shaped_text_update_justification_ops(RID p_shaped) override;
|
||||
|
||||
virtual bool shaped_text_is_ready(RID p_shaped) const override;
|
||||
|
||||
virtual Vector<Glyph> shaped_text_get_glyphs(RID p_shaped) const override;
|
||||
|
||||
virtual Vector2i shaped_text_get_range(RID p_shaped) const override;
|
||||
|
||||
virtual Vector<Glyph> shaped_text_sort_logical(RID p_shaped) override;
|
||||
|
||||
virtual Array shaped_text_get_objects(RID p_shaped) const override;
|
||||
virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const override;
|
||||
|
||||
virtual Size2 shaped_text_get_size(RID p_shaped) const override;
|
||||
virtual float shaped_text_get_ascent(RID p_shaped) const override;
|
||||
virtual float shaped_text_get_descent(RID p_shaped) const override;
|
||||
virtual float shaped_text_get_width(RID p_shaped) const override;
|
||||
virtual float shaped_text_get_underline_position(RID p_shaped) const override;
|
||||
virtual float shaped_text_get_underline_thickness(RID p_shaped) const override;
|
||||
|
||||
virtual String format_number(const String &p_string, const String &p_language = "") const override;
|
||||
virtual String parse_number(const String &p_string, const String &p_language = "") const override;
|
||||
virtual String percent_sign(const String &p_language = "") const override;
|
||||
|
||||
static TextServer *create_func(Error &r_error, void *p_user_data);
|
||||
static void register_server();
|
||||
|
||||
TextServerAdvanced();
|
||||
~TextServerAdvanced();
|
||||
};
|
||||
|
||||
#endif // TEXT_SERVER_ADV_H
|
Loading…
Reference in New Issue
Block a user