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

[TextServer] Add support for retrieving OpenType name strings. #75739

Merged
merged 1 commit into from
May 22, 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
6 changes: 6 additions & 0 deletions doc/classes/Font.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
Returns a set of OpenType feature tags. More info: [url=https://docs.microsoft.com/en-us/typography/opentype/spec/featuretags]OpenType feature tags[/url].
</description>
</method>
<method name="get_ot_name_strings" qualifiers="const">
<return type="Dictionary" />
<description>
Returns [Dictionary] with OpenType font name strings (localized font names, version, description, license information, sample text, etc.).
</description>
</method>
<method name="get_rids" qualifiers="const">
<return type="RID[]" />
<description>
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/TextServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@
Returns font OpenType feature set override.
</description>
</method>
<method name="font_get_ot_name_strings" qualifiers="const">
<return type="Dictionary" />
<param index="0" name="font_rid" type="RID" />
<description>
Returns [Dictionary] with OpenType font name strings (localized font names, version, description, license information, sample text, etc.).
</description>
</method>
<method name="font_get_oversampling" qualifiers="const">
<return type="float" />
<param index="0" name="font_rid" type="RID" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/TextServerExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@
<description>
</description>
</method>
<method name="_font_get_ot_name_strings" qualifiers="virtual const">
<return type="Dictionary" />
<param index="0" name="font_rid" type="RID" />
<description>
</description>
</method>
<method name="_font_get_oversampling" qualifiers="virtual const">
<return type="float" />
<param index="0" name="font_rid" type="RID" />
Expand Down
113 changes: 113 additions & 0 deletions modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,119 @@ String TextServerAdvanced::_font_get_name(const RID &p_font_rid) const {
return fd->font_name;
}

Dictionary TextServerAdvanced::_font_get_ot_name_strings(const RID &p_font_rid) const {
FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, Dictionary());

MutexLock lock(fd->mutex);
Vector2i size = _get_size(fd, 16);
ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());

hb_face_t *hb_face = hb_font_get_face(fd->cache[size]->hb_handle);

unsigned int num_entries = 0;
const hb_ot_name_entry_t *names = hb_ot_name_list_names(hb_face, &num_entries);
HashMap<String, Dictionary> names_for_lang;
for (unsigned int i = 0; i < num_entries; i++) {
String name;
switch (names[i].name_id) {
case HB_OT_NAME_ID_COPYRIGHT: {
name = "copyright";
} break;
case HB_OT_NAME_ID_FONT_FAMILY: {
name = "family_name";
} break;
case HB_OT_NAME_ID_FONT_SUBFAMILY: {
name = "subfamily_name";
} break;
case HB_OT_NAME_ID_UNIQUE_ID: {
name = "unique_identifier";
} break;
case HB_OT_NAME_ID_FULL_NAME: {
name = "full_name";
} break;
case HB_OT_NAME_ID_VERSION_STRING: {
name = "version";
} break;
case HB_OT_NAME_ID_POSTSCRIPT_NAME: {
name = "postscript_name";
} break;
case HB_OT_NAME_ID_TRADEMARK: {
name = "trademark";
} break;
case HB_OT_NAME_ID_MANUFACTURER: {
name = "manufacturer";
} break;
case HB_OT_NAME_ID_DESIGNER: {
name = "designer";
} break;
case HB_OT_NAME_ID_DESCRIPTION: {
name = "description";
} break;
case HB_OT_NAME_ID_VENDOR_URL: {
name = "vendor_url";
} break;
case HB_OT_NAME_ID_DESIGNER_URL: {
name = "designer_url";
} break;
case HB_OT_NAME_ID_LICENSE: {
name = "license";
} break;
case HB_OT_NAME_ID_LICENSE_URL: {
name = "license_url";
} break;
case HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY: {
name = "typographic_family_name";
} break;
case HB_OT_NAME_ID_TYPOGRAPHIC_SUBFAMILY: {
name = "typographic_subfamily_name";
} break;
case HB_OT_NAME_ID_MAC_FULL_NAME: {
name = "full_name_macos";
} break;
case HB_OT_NAME_ID_SAMPLE_TEXT: {
name = "sample_text";
} break;
case HB_OT_NAME_ID_CID_FINDFONT_NAME: {
name = "cid_findfont_name";
} break;
case HB_OT_NAME_ID_WWS_FAMILY: {
name = "weight_width_slope_family_name";
} break;
case HB_OT_NAME_ID_WWS_SUBFAMILY: {
name = "weight_width_slope_subfamily_name";
} break;
case HB_OT_NAME_ID_LIGHT_BACKGROUND: {
name = "light_background_palette";
} break;
case HB_OT_NAME_ID_DARK_BACKGROUND: {
name = "dark_background_palette";
} break;
case HB_OT_NAME_ID_VARIATIONS_PS_PREFIX: {
name = "postscript_name_prefix";
} break;
default: {
name = vformat("unknown_%d", names[i].name_id);
} break;
}
String text;
unsigned int text_size = hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, nullptr, nullptr) + 1;
text.resize(text_size);
hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, &text_size, (uint32_t *)text.ptrw());
if (!text.is_empty()) {
Dictionary &id_string = names_for_lang[String(hb_language_to_string(names[i].language))];
id_string[name] = text;
}
}

Dictionary out;
for (const KeyValue<String, Dictionary> &E : names_for_lang) {
out[E.key] = E.value;
}

return out;
}

void TextServerAdvanced::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND(!fd);
Expand Down
1 change: 1 addition & 0 deletions modules/text_server_adv/text_server_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ class TextServerAdvanced : public TextServerExtension {

MODBIND2(font_set_name, const RID &, const String &);
MODBIND1RC(String, font_get_name, const RID &);
MODBIND1RC(Dictionary, font_get_ot_name_strings, const RID &);

MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);
Expand Down
5 changes: 5 additions & 0 deletions scene/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void Font::_bind_methods() {

ClassDB::bind_method(D_METHOD("get_font_name"), &Font::get_font_name);
ClassDB::bind_method(D_METHOD("get_font_style_name"), &Font::get_font_style_name);
ClassDB::bind_method(D_METHOD("get_ot_name_strings"), &Font::get_ot_name_strings);
ClassDB::bind_method(D_METHOD("get_font_style"), &Font::get_font_style);
ClassDB::bind_method(D_METHOD("get_font_weight"), &Font::get_font_weight);
ClassDB::bind_method(D_METHOD("get_font_stretch"), &Font::get_font_stretch);
Expand Down Expand Up @@ -243,6 +244,10 @@ String Font::get_font_name() const {
return TS->font_get_name(_get_rid());
}

Dictionary Font::get_ot_name_strings() const {
return TS->font_get_ot_name_strings(_get_rid());
}

String Font::get_font_style_name() const {
return TS->font_get_style_name(_get_rid());
}
Expand Down
1 change: 1 addition & 0 deletions scene/resources/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Font : public Resource {

virtual String get_font_name() const;
virtual String get_font_style_name() const;
virtual Dictionary get_ot_name_strings() const;
virtual BitField<TextServer::FontStyle> get_font_style() const;
virtual int get_font_weight() const;
virtual int get_font_stretch() const;
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 @@ -65,6 +65,7 @@ void TextServerExtension::_bind_methods() {

GDVIRTUAL_BIND(_font_set_name, "font_rid", "name");
GDVIRTUAL_BIND(_font_get_name, "font_rid");
GDVIRTUAL_BIND(_font_get_ot_name_strings, "font_rid");

GDVIRTUAL_BIND(_font_set_style_name, "font_rid", "name_style");
GDVIRTUAL_BIND(_font_get_style_name, "font_rid");
Expand Down Expand Up @@ -476,6 +477,12 @@ String TextServerExtension::font_get_name(const RID &p_font_rid) const {
return ret;
}

Dictionary TextServerExtension::font_get_ot_name_strings(const RID &p_font_rid) const {
Dictionary ret;
GDVIRTUAL_CALL(_font_get_ot_name_strings, p_font_rid, ret);
return ret;
}

void TextServerExtension::font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
GDVIRTUAL_CALL(_font_set_antialiasing, p_font_rid, p_antialiasing);
}
Expand Down
2 changes: 2 additions & 0 deletions servers/text/text_server_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ class TextServerExtension : public TextServer {

virtual void font_set_name(const RID &p_font_rid, const String &p_name) override;
virtual String font_get_name(const RID &p_font_rid) const override;
virtual Dictionary font_get_ot_name_strings(const RID &p_font_rid) const override;
GDVIRTUAL2(_font_set_name, RID, const String &);
GDVIRTUAL1RC(String, _font_get_name, RID);
GDVIRTUAL1RC(Dictionary, _font_get_ot_name_strings, RID);

virtual void font_set_style_name(const RID &p_font_rid, const String &p_name) override;
virtual String font_get_style_name(const RID &p_font_rid) const override;
Expand Down
1 change: 1 addition & 0 deletions servers/text_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ void TextServer::_bind_methods() {

ClassDB::bind_method(D_METHOD("font_set_name", "font_rid", "name"), &TextServer::font_set_name);
ClassDB::bind_method(D_METHOD("font_get_name", "font_rid"), &TextServer::font_get_name);
ClassDB::bind_method(D_METHOD("font_get_ot_name_strings", "font_rid"), &TextServer::font_get_ot_name_strings);

ClassDB::bind_method(D_METHOD("font_set_style_name", "font_rid", "name"), &TextServer::font_set_style_name);
ClassDB::bind_method(D_METHOD("font_get_style_name", "font_rid"), &TextServer::font_get_style_name);
Expand Down
1 change: 1 addition & 0 deletions servers/text_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class TextServer : public RefCounted {

virtual void font_set_name(const RID &p_font_rid, const String &p_name) = 0;
virtual String font_get_name(const RID &p_font_rid) const = 0;
virtual Dictionary font_get_ot_name_strings(const RID &p_font_rid) const { return Dictionary(); }

virtual void font_set_style_name(const RID &p_font_rid, const String &p_name) = 0;
virtual String font_get_style_name(const RID &p_font_rid) const = 0;
Expand Down