Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Font] Implement get_char_from_glyph_index function. #74149

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/classes/FontFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
11 changes: 10 additions & 1 deletion doc/classes/TextServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -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">
Expand Down
8 changes: 8 additions & 0 deletions doc/classes/TextServerExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
31 changes: 31 additions & 0 deletions modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ".");
Expand Down
2 changes: 2 additions & 0 deletions modules/text_server_adv/text_server_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -812,6 +813,7 @@ class TextServerAdvanced : public TextServerExtension {
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 &);
Expand Down
4 changes: 4 additions & 0 deletions modules/text_server_fb/text_server_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ".");
Expand Down
1 change: 1 addition & 0 deletions modules/text_server_fb/text_server_fb.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ class TextServerFallback : public TextServerExtension {
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 &);
Expand Down
6 changes: 6 additions & 0 deletions scene/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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() {
}

Expand Down
1 change: 1 addition & 0 deletions scene/resources/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ class FontFile : public Font {

// 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();
Expand Down
7 changes: 7 additions & 0 deletions servers/text/text_server_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions servers/text/text_server_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ class TextServerExtension : public TextServer {
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);
Expand Down
1 change: 1 addition & 0 deletions servers/text_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions servers/text_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ class TextServer : public RefCounted {
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;
Expand Down