Skip to content

Commit

Permalink
Merge pull request #61772 from bruvzg/ft_ot_collect
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Jun 13, 2022
2 parents e95f4eb + 6e4cdad commit 136f84f
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 5 deletions.
14 changes: 12 additions & 2 deletions doc/classes/FontData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<brief_description>
Font source data and prerendered glyph cache, imported from dynamic or bitmap font.
Supported font formats:
- Dynamic font importer: TrueType (.ttf), OpenType (.otf), WOFF (.woff), WOFF2 (.woff2), Type 1 (.pfb, .pfm).
- Dynamic font importer: TrueType (.ttf), TrueType collection (.ttc), OpenType (.otf), OpenType collection (.otc), WOFF (.woff), WOFF2 (.woff2), Type 1 (.pfb, .pfm).
- Bitmap font importer: AngelCode BMFont (.fnt, .font), text and binary (version 3) format variants.
- Monospace image font importer: All supported image formats.
</brief_description>
Expand Down Expand Up @@ -87,6 +87,12 @@
Returns font descent (number of pixels below the baseline).
</description>
</method>
<method name="get_face_count" qualifiers="const">
<return type="int" />
<description>
Returns number of faces in the TrueType / OpenType collection.
</description>
</method>
<method name="get_glyph_advance" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="cache_index" type="int" />
Expand Down Expand Up @@ -318,7 +324,8 @@
<return type="int" enum="Error" />
<argument index="0" name="path" type="String" />
<description>
Loads a TrueType (.ttf), OpenType (.otf), WOFF (.woff), WOFF2 (.woff2) or Type 1 (.pfb, .pfm) dynamic font from file [code]path[/code].
Loads a TrueType (.ttf), TrueType collection (.ttc), OpenType (.otf), OpenType collection (.otc), WOFF (.woff), WOFF2 (.woff2) or Type 1 (.pfb, .pfm) dynamic font from file [code]path[/code].
[b]Note:[/b] Use [member face_index] to select specific face from the collection file.
[b]Warning:[/b] This method should only be used in the editor or in cases when you need to load external fonts at run-time, such as fonts located at the [code]user://[/code] directory.
</description>
</method>
Expand Down Expand Up @@ -570,6 +577,9 @@
<member name="embolden" type="float" setter="set_embolden" getter="get_embolden" default="0.0">
If is not equal to zero, emboldens the font outlines. Negative values reduce the outline thickness.
</member>
<member name="face_index" type="int" setter="set_face_index" getter="get_face_index" default="0">
Active face index in the TrueType / OpenType collection file.
</member>
<member name="fixed_size" type="int" setter="set_fixed_size" getter="get_fixed_size" default="0">
Font size, used only for the bitmap fonts.
</member>
Expand Down
22 changes: 22 additions & 0 deletions doc/classes/TextServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@
Returns font embolden strength.
</description>
</method>
<method name="font_get_face_count" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns number of faces in the TrueType / OpenType collection.
</description>
</method>
<method name="font_get_face_index" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Recturns an active face index in the TrueType / OpenType collection.
</description>
</method>
<method name="font_get_fixed_size" qualifiers="const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
Expand Down Expand Up @@ -593,6 +607,14 @@
Sets font embolden strength. If [code]strength[/code] is not equal to zero, emboldens the font outlines. Negative values reduce the outline thickness.
</description>
</method>
<method name="font_set_face_index">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="face_index" type="int" />
<description>
Sets an active face index in the TrueType / OpenType collection.
</description>
</method>
<method name="font_set_fixed_size">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
Expand Down
22 changes: 22 additions & 0 deletions doc/classes/TextServerExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@
Returns font embolden strength.
</description>
</method>
<method name="font_get_face_count" qualifiers="virtual const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns number of faces in the TrueType / OpenType collection.
</description>
</method>
<method name="font_get_face_index" qualifiers="virtual const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
<description>
Returns an active face index in the TrueType / OpenType collection.
</description>
</method>
<method name="font_get_fixed_size" qualifiers="virtual const">
<return type="int" />
<argument index="0" name="font_rid" type="RID" />
Expand Down Expand Up @@ -590,6 +604,14 @@
Sets font embolden strength. If [code]strength[/code] is not equal to zero, emboldens the font outlines. Negative values reduce the outline thickness.
</description>
</method>
<method name="font_set_face_index" qualifiers="virtual">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
<argument index="1" name="face_index" type="int" />
<description>
Sets an active face index in the TrueType / OpenType collection.
</description>
</method>
<method name="font_set_fixed_size" qualifiers="virtual">
<return type="void" />
<argument index="0" name="font_rid" type="RID" />
Expand Down
9 changes: 8 additions & 1 deletion editor/import/dynamic_font_import_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,11 @@ void DynamicFontImportSettings::_add_glyph_range_item(int32_t p_start, int32_t p
void DynamicFontImportSettings::_main_prop_changed(const String &p_edited_property) {
// Update font preview.

if (p_edited_property == "antialiased") {
if (p_edited_property == "face_index") {
if (font_preview->get_data_count() > 0) {
font_preview->get_data(0)->set_face_index(import_settings_data->get("face_index"));
}
} else if (p_edited_property == "antialiased") {
if (font_preview->get_data_count() > 0) {
font_preview->get_data(0)->set_antialiased(import_settings_data->get("antialiased"));
}
Expand Down Expand Up @@ -945,6 +949,7 @@ void DynamicFontImportSettings::_notification(int p_what) {
void DynamicFontImportSettings::_re_import() {
HashMap<StringName, Variant> main_settings;

main_settings["face_index"] = import_settings_data->get("face_index");
main_settings["antialiased"] = import_settings_data->get("antialiased");
main_settings["generate_mipmaps"] = import_settings_data->get("generate_mipmaps");
main_settings["multichannel_signed_distance_field"] = import_settings_data->get("multichannel_signed_distance_field");
Expand Down Expand Up @@ -1299,6 +1304,7 @@ void DynamicFontImportSettings::open_settings(const String &p_path) {
import_settings_data->notify_property_list_changed();

if (font_preview->get_data_count() > 0) {
font_preview->get_data(0)->set_face_index(import_settings_data->get("face_index"));
font_preview->get_data(0)->set_antialiased(import_settings_data->get("antialiased"));
font_preview->get_data(0)->set_multichannel_signed_distance_field(import_settings_data->get("multichannel_signed_distance_field"));
font_preview->get_data(0)->set_msdf_pixel_range(import_settings_data->get("msdf_pixel_range"));
Expand Down Expand Up @@ -1360,6 +1366,7 @@ DynamicFontImportSettings *DynamicFontImportSettings::get_singleton() {
DynamicFontImportSettings::DynamicFontImportSettings() {
singleton = this;

options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, "face_index"), 0));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "antialiased"), true));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true));
Expand Down
7 changes: 7 additions & 0 deletions editor/import/resource_importer_dynamic_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ void ResourceImporterDynamicFont::get_recognized_extensions(List<String> *p_exte
if (p_extensions) {
#ifdef MODULE_FREETYPE_ENABLED
p_extensions->push_back("ttf");
p_extensions->push_back("ttc");
p_extensions->push_back("otf");
p_extensions->push_back("otc");
p_extensions->push_back("woff");
p_extensions->push_back("woff2");
p_extensions->push_back("pfb");
Expand Down Expand Up @@ -101,6 +103,8 @@ String ResourceImporterDynamicFont::get_preset_name(int p_idx) const {
void ResourceImporterDynamicFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
bool msdf = p_preset == PRESET_MSDF;

r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "face_index"), 0));

r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "antialiased"), true));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), (msdf) ? true : false));
Expand Down Expand Up @@ -179,6 +183,8 @@ void ResourceImporterDynamicFont::show_advanced_options(const String &p_path) {
Error ResourceImporterDynamicFont::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
print_verbose("Importing dynamic font from: " + p_source_file);

int face_index = p_options["face_index"];

bool antialiased = p_options["antialiased"];
bool generate_mipmaps = p_options["generate_mipmaps"];
bool msdf = p_options["multichannel_signed_distance_field"];
Expand All @@ -200,6 +206,7 @@ Error ResourceImporterDynamicFont::import(const String &p_source_file, const Str
Ref<FontData> font;
font.instantiate();
font->set_data(data);
font->set_face_index(face_index);
font->set_antialiased(antialiased);
font->set_generate_mipmaps(generate_mipmaps);
font->set_multichannel_signed_distance_field(msdf);
Expand Down
74 changes: 73 additions & 1 deletion modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,16 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontDataAdvanced
fargs.memory_size = p_font_data->data_size;
fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &fd->stream;
error = FT_Open_Face(ft_library, &fargs, 0, &fd->face);

int max_index = 0;
FT_Face tmp_face;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (error == 0) {
max_index = tmp_face->num_faces - 1;
}
FT_Done_Face(tmp_face);

error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
if (error) {
FT_Done_Face(fd->face);
fd->face = nullptr;
Expand Down Expand Up @@ -1720,6 +1729,69 @@ void TextServerAdvanced::font_set_data_ptr(const RID &p_font_rid, const uint8_t
fd->data_size = p_data_size;
}

void TextServerAdvanced::font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
ERR_FAIL_COND(p_face_index < 0);
ERR_FAIL_COND(p_face_index >= 0x7FFF);

FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND(!fd);

MutexLock lock(fd->mutex);
if (fd->face_index != p_face_index) {
fd->face_index = p_face_index;
_font_clear_cache(fd);
}
}

int64_t TextServerAdvanced::font_get_face_index(const RID &p_font_rid) const {
FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, 0);

MutexLock lock(fd->mutex);
return fd->face_index;
}

int64_t TextServerAdvanced::font_get_face_count(const RID &p_font_rid) const {
FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, 0);

MutexLock lock(fd->mutex);
int face_count = 0;

if (fd->data_ptr && (fd->data_size > 0)) {
// Init dynamic font.
#ifdef MODULE_FREETYPE_ENABLED
int error = 0;
if (!ft_library) {
error = FT_Init_FreeType(&ft_library);
ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
}

FT_StreamRec stream;
memset(&stream, 0, sizeof(FT_StreamRec));
stream.base = (unsigned char *)fd->data_ptr;
stream.size = fd->data_size;
stream.pos = 0;

FT_Open_Args fargs;
memset(&fargs, 0, sizeof(FT_Open_Args));
fargs.memory_base = (unsigned char *)fd->data_ptr;
fargs.memory_size = fd->data_size;
fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &stream;

FT_Face tmp_face;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (error == 0) {
face_count = tmp_face->num_faces;
}
FT_Done_Face(tmp_face);
#endif
}

return face_count;
}

void TextServerAdvanced::font_set_style(const RID &p_font_rid, int64_t /*FontStyle*/ p_style) {
FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND(!fd);
Expand Down
6 changes: 6 additions & 0 deletions modules/text_server_adv/text_server_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class TextServerAdvanced : public TextServerExtension {
PackedByteArray data;
const uint8_t *data_ptr;
size_t data_size;
int face_index = 0;
mutable ThreadWorkPool work_pool;

~FontDataAdvanced() {
Expand Down Expand Up @@ -473,6 +474,11 @@ class TextServerAdvanced : public TextServerExtension {
virtual void font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) override;
virtual void font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) override;

virtual void font_set_face_index(const RID &p_font_rid, int64_t p_index) override;
virtual int64_t font_get_face_index(const RID &p_font_rid) const override;

virtual int64_t font_get_face_count(const RID &p_font_rid) const override;

virtual void font_set_style(const RID &p_font_rid, int64_t /*FontStyle*/ p_style) override;
virtual int64_t /*FontStyle*/ font_get_style(const RID &p_font_rid) const override;

Expand Down
74 changes: 73 additions & 1 deletion modules/text_server_fb/text_server_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,16 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontDataFallback
fargs.memory_size = p_font_data->data_size;
fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &fd->stream;
error = FT_Open_Face(ft_library, &fargs, 0, &fd->face);

int max_index = 0;
FT_Face tmp_face;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (error == 0) {
max_index = tmp_face->num_faces - 1;
}
FT_Done_Face(tmp_face);

error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
if (error) {
FT_Done_Face(fd->face);
fd->face = nullptr;
Expand Down Expand Up @@ -892,6 +901,69 @@ void TextServerFallback::font_set_style(const RID &p_font_rid, int64_t /*FontSty
fd->style_flags = p_style;
}

void TextServerFallback::font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
ERR_FAIL_COND(p_face_index < 0);
ERR_FAIL_COND(p_face_index >= 0x7FFF);

FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND(!fd);

MutexLock lock(fd->mutex);
if (fd->face_index != p_face_index) {
fd->face_index = p_face_index;
_font_clear_cache(fd);
}
}

int64_t TextServerFallback::font_get_face_index(const RID &p_font_rid) const {
FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, 0);

MutexLock lock(fd->mutex);
return fd->face_index;
}

int64_t TextServerFallback::font_get_face_count(const RID &p_font_rid) const {
FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, 0);

MutexLock lock(fd->mutex);
int face_count = 0;

if (fd->data_ptr && (fd->data_size > 0)) {
// Init dynamic font.
#ifdef MODULE_FREETYPE_ENABLED
int error = 0;
if (!ft_library) {
error = FT_Init_FreeType(&ft_library);
ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
}

FT_StreamRec stream;
memset(&stream, 0, sizeof(FT_StreamRec));
stream.base = (unsigned char *)fd->data_ptr;
stream.size = fd->data_size;
stream.pos = 0;

FT_Open_Args fargs;
memset(&fargs, 0, sizeof(FT_Open_Args));
fargs.memory_base = (unsigned char *)fd->data_ptr;
fargs.memory_size = fd->data_size;
fargs.flags = FT_OPEN_MEMORY;
fargs.stream = &stream;

FT_Face tmp_face;
error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
if (error == 0) {
face_count = tmp_face->num_faces;
}
FT_Done_Face(tmp_face);
#endif
}

return face_count;
}

int64_t /*FontStyle*/ TextServerFallback::font_get_style(const RID &p_font_rid) const {
FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
ERR_FAIL_COND_V(!fd, 0);
Expand Down
Loading

0 comments on commit 136f84f

Please sign in to comment.