[Font] Implement get_char_from_glyph_index function.

This commit is contained in:
bruvzg 2023-02-27 16:21:28 +02:00
parent 2f34a35722
commit 8459aeaab0
No known key found for this signature in database
GPG Key ID: 7960FCF39844EC38
13 changed files with 83 additions and 1 deletions

View File

@ -115,6 +115,14 @@
Returns thickness of the underline in pixels.
</description>
</method>
<method name="get_char_from_glyph_index" qualifiers="const">
<return type="int" />
<param index="0" name="size" type="int" />
<param index="1" name="glyph_index" type="int" />
<description>
Returns character code associated with [param glyph_index], or [code]0[/code] if [param glyph_index] is invalid. See [method get_glyph_index].
</description>
</method>
<method name="get_embolden" qualifiers="const">
<return type="float" />
<param index="0" name="cache_index" type="int" />

View File

@ -113,6 +113,15 @@
Returns the font ascent (number of pixels above the baseline).
</description>
</method>
<method name="font_get_char_from_glyph_index" qualifiers="const">
<return type="int" />
<param index="0" name="font_rid" type="RID" />
<param index="1" name="size" type="int" />
<param index="2" name="glyph_index" type="int" />
<description>
Returns character code associated with [param glyph_index], or [code]0[/code] if [param glyph_index] is invalid. See [method font_get_glyph_index].
</description>
</method>
<method name="font_get_descent" qualifiers="const">
<return type="float" />
<param index="0" name="font_rid" type="RID" />
@ -191,7 +200,7 @@
<param index="2" name="char" type="int" />
<param index="3" name="variation_selector" type="int" />
<description>
Returns the glyph index of a [param char], optionally modified by the [param variation_selector].
Returns the glyph index of a [param char], optionally modified by the [param variation_selector]. See [method font_get_char_from_glyph_index].
</description>
</method>
<method name="font_get_glyph_list" qualifiers="const">

View File

@ -99,6 +99,14 @@
<description>
</description>
</method>
<method name="_font_get_char_from_glyph_index" qualifiers="virtual const">
<return type="int" />
<param index="0" name="font_rid" type="RID" />
<param index="1" name="size" type="int" />
<param index="2" name="glyph_index" type="int" />
<description>
</description>
</method>
<method name="_font_get_descent" qualifiers="virtual const">
<return type="float" />
<param index="0" name="font_rid" type="RID" />

View File

@ -3082,6 +3082,37 @@ int64_t TextServerAdvanced::_font_get_glyph_index(const RID &p_font_rid, int64_t
#endif
}
int64_t TextServerAdvanced::_font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const {
FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, 0);
MutexLock lock(fd->mutex);
Vector2i size = _get_size(fd, p_size);
ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
#ifdef MODULE_FREETYPE_ENABLED
if (fd->cache[size]->inv_glyph_map.is_empty()) {
FT_Face face = fd->cache[size]->face;
FT_UInt gindex;
FT_ULong charcode = FT_Get_First_Char(face, &gindex);
while (gindex != 0) {
if (charcode != 0) {
fd->cache[size]->inv_glyph_map[gindex] = charcode;
}
charcode = FT_Get_Next_Char(face, charcode, &gindex);
}
}
if (fd->cache[size]->inv_glyph_map.has(p_glyph_index)) {
return fd->cache[size]->inv_glyph_map[p_glyph_index];
} else {
return 0;
}
#else
return p_glyph_index;
#endif
}
bool TextServerAdvanced::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");

View File

@ -272,6 +272,7 @@ class TextServerAdvanced : public TextServerExtension {
Vector2i size;
Vector<ShelfPackTexture> textures;
HashMap<int64_t, int64_t> inv_glyph_map;
HashMap<int32_t, FontGlyph> glyph_map;
HashMap<Vector2i, Vector2> kerning_map;
hb_font_t *hb_handle = nullptr;
@ -812,6 +813,7 @@ public:
MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);
MODBIND2RC(bool, font_has_char, const RID &, int64_t);
MODBIND1RC(String, font_get_supported_chars, const RID &);

View File

@ -2160,6 +2160,10 @@ int64_t TextServerFallback::_font_get_glyph_index(const RID &p_font_rid, int64_t
return (int64_t)p_char;
}
int64_t TextServerFallback::_font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const {
return p_glyph_index;
}
bool TextServerFallback::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
FontFallback *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");

View File

@ -685,6 +685,7 @@ public:
MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);
MODBIND2RC(bool, font_has_char, const RID &, int64_t);
MODBIND1RC(String, font_get_supported_chars, const RID &);

View File

@ -980,6 +980,7 @@ void FontFile::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_opentype_feature_overrides"), &FontFile::get_opentype_feature_overrides);
ClassDB::bind_method(D_METHOD("get_glyph_index", "size", "char", "variation_selector"), &FontFile::get_glyph_index);
ClassDB::bind_method(D_METHOD("get_char_from_glyph_index", "size", "glyph_index"), &FontFile::get_char_from_glyph_index);
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_data", "get_data");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_generate_mipmaps", "get_generate_mipmaps");
@ -2585,6 +2586,11 @@ int32_t FontFile::get_glyph_index(int p_size, char32_t p_char, char32_t p_variat
return TS->font_get_glyph_index(cache[0], p_size, p_char, p_variation_selector);
}
char32_t FontFile::get_char_from_glyph_index(int p_size, int32_t p_glyph_index) const {
_ensure_rid(0);
return TS->font_get_char_from_glyph_index(cache[0], p_size, p_glyph_index);
}
FontFile::FontFile() {
}

View File

@ -359,6 +359,7 @@ public:
// Base font properties.
virtual int32_t get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector = 0x0000) const;
virtual char32_t get_char_from_glyph_index(int p_size, int32_t p_glyph_index) const;
FontFile();
~FontFile();

View File

@ -178,6 +178,7 @@ void TextServerExtension::_bind_methods() {
GDVIRTUAL_BIND(_font_get_kerning, "font_rid", "size", "glyph_pair");
GDVIRTUAL_BIND(_font_get_glyph_index, "font_rid", "size", "char", "variation_selector");
GDVIRTUAL_BIND(_font_get_char_from_glyph_index, "font_rid", "size", "glyph_index");
GDVIRTUAL_BIND(_font_has_char, "font_rid", "char");
GDVIRTUAL_BIND(_font_get_supported_chars, "font_rid");
@ -825,6 +826,12 @@ int64_t TextServerExtension::font_get_glyph_index(const RID &p_font_rid, int64_t
return ret;
}
int64_t TextServerExtension::font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const {
int64_t ret = 0;
GDVIRTUAL_CALL(_font_get_char_from_glyph_index, p_font_rid, p_size, p_glyph_index, ret);
return ret;
}
bool TextServerExtension::font_has_char(const RID &p_font_rid, int64_t p_char) const {
bool ret = false;
GDVIRTUAL_CALL(_font_has_char, p_font_rid, p_char, ret);

View File

@ -294,6 +294,9 @@ public:
virtual int64_t font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector = 0) const override;
GDVIRTUAL4RC(int64_t, _font_get_glyph_index, RID, int64_t, int64_t, int64_t);
virtual int64_t font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const override;
GDVIRTUAL3RC(int64_t, _font_get_char_from_glyph_index, RID, int64_t, int64_t);
virtual bool font_has_char(const RID &p_font_rid, int64_t p_char) const override;
virtual String font_get_supported_chars(const RID &p_font_rid) const override;
GDVIRTUAL2RC(bool, _font_has_char, RID, int64_t);

View File

@ -332,6 +332,7 @@ void TextServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("font_get_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_get_kerning);
ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font_rid", "size", "char", "variation_selector"), &TextServer::font_get_glyph_index);
ClassDB::bind_method(D_METHOD("font_get_char_from_glyph_index", "font_rid", "size", "glyph_index"), &TextServer::font_get_char_from_glyph_index);
ClassDB::bind_method(D_METHOD("font_has_char", "font_rid", "char"), &TextServer::font_has_char);
ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font_rid"), &TextServer::font_get_supported_chars);

View File

@ -359,6 +359,7 @@ public:
virtual Vector2 font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const = 0;
virtual int64_t font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const = 0;
virtual int64_t font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const = 0;
virtual bool font_has_char(const RID &p_font_rid, int64_t p_char) const = 0;
virtual String font_get_supported_chars(const RID &p_font_rid) const = 0;