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] Add option to disable embedded bitmaps loading. #89383

Merged
merged 1 commit into from
Mar 11, 2024
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
3 changes: 3 additions & 0 deletions doc/classes/FontFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@
<member name="data" type="PackedByteArray" setter="set_data" getter="get_data" default="PackedByteArray()">
Contents of the dynamic font source file.
</member>
<member name="disable_embedded_bitmaps" type="bool" setter="set_disable_embedded_bitmaps" getter="get_disable_embedded_bitmaps" default="true">
If set to [code]true[/code], embedded font bitmap loading is disabled (bitmap-only and color fonts ignore this property).
</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
3 changes: 3 additions & 0 deletions doc/classes/ResourceImporterDynamicFont.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<member name="compress" type="bool" setter="" getter="" default="true">
If [code]true[/code], uses lossless compression for the resulting font.
</member>
<member name="disable_embedded_bitmaps" type="bool" setter="" getter="" default="true">
If set to [code]true[/code], embedded font bitmap loading is disabled (bitmap-only and color fonts ignore this property).
</member>
<member name="fallbacks" type="Array" setter="" getter="" default="[]">
List of font fallbacks to use if a glyph isn't found in this dynamic font. Fonts at the beginning of the array are attempted first, but fallback fonts that don't support the glyph's language and script are attempted last (see [member language_support] and [member script_support]). See also [member allow_system_fallback].
</member>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/SystemFont.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<member name="antialiasing" type="int" setter="set_antialiasing" getter="get_antialiasing" enum="TextServer.FontAntialiasing" default="1">
Font anti-aliasing mode.
</member>
<member name="disable_embedded_bitmaps" type="bool" setter="set_disable_embedded_bitmaps" getter="get_disable_embedded_bitmaps" default="true">
If set to [code]true[/code], embedded font bitmap loading is disabled (bitmap-only and color fonts ignore this property).
</member>
<member name="font_italic" type="bool" setter="set_font_italic" getter="get_font_italic" default="false">
If set to [code]true[/code], italic or oblique font is preferred.
</member>
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/TextServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@
Returns the font descent (number of pixels below the baseline).
</description>
</method>
<method name="font_get_disable_embedded_bitmaps" qualifiers="const">
<return type="bool" />
<param index="0" name="font_rid" type="RID" />
<description>
Returns whether the font's embedded bitmap loading is disabled.
</description>
</method>
<method name="font_get_embolden" qualifiers="const">
<return type="float" />
<param index="0" name="font_rid" type="RID" />
Expand Down Expand Up @@ -672,6 +679,14 @@
Sets the font descent (number of pixels below the baseline).
</description>
</method>
<method name="font_set_disable_embedded_bitmaps">
<return type="void" />
<param index="0" name="font_rid" type="RID" />
<param index="1" name="disable_embedded_bitmaps" type="bool" />
<description>
If set to [code]true[/code], embedded font bitmap loading is disabled (bitmap-only and color fonts ignore this property).
</description>
</method>
<method name="font_set_embolden">
<return type="void" />
<param index="0" name="font_rid" type="RID" />
Expand Down
13 changes: 13 additions & 0 deletions doc/classes/TextServerExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
<description>
</description>
</method>
<method name="_font_get_disable_embedded_bitmaps" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="font_rid" type="RID" />
<description>
</description>
</method>
<method name="_font_get_embolden" qualifiers="virtual const">
<return type="float" />
<param index="0" name="font_rid" type="RID" />
Expand Down Expand Up @@ -588,6 +594,13 @@
<description>
</description>
</method>
<method name="_font_set_disable_embedded_bitmaps" qualifiers="virtual">
<return type="void" />
<param index="0" name="font_rid" type="RID" />
<param index="1" name="disable_embedded_bitmaps" type="bool" />
<description>
</description>
</method>
<method name="_font_set_embolden" qualifiers="virtual">
<return type="void" />
<param index="0" name="font_rid" type="RID" />
Expand Down
4 changes: 4 additions & 0 deletions editor/import/dynamic_font_import_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ void DynamicFontImportSettingsDialog::_main_prop_changed(const String &p_edited_
font_preview->set_antialiasing((TextServer::FontAntialiasing)import_settings_data->get("antialiasing").operator int());
} else if (p_edited_property == "generate_mipmaps") {
font_preview->set_generate_mipmaps(import_settings_data->get("generate_mipmaps"));
} else if (p_edited_property == "disable_embedded_bitmaps") {
font_preview->set_disable_embedded_bitmaps(import_settings_data->get("disable_embedded_bitmaps"));
} else if (p_edited_property == "multichannel_signed_distance_field") {
font_preview->set_multichannel_signed_distance_field(import_settings_data->get("multichannel_signed_distance_field"));
_variation_selected();
Expand Down Expand Up @@ -938,6 +940,7 @@ void DynamicFontImportSettingsDialog::_re_import() {
main_settings["face_index"] = import_settings_data->get("face_index");
main_settings["antialiasing"] = import_settings_data->get("antialiasing");
main_settings["generate_mipmaps"] = import_settings_data->get("generate_mipmaps");
main_settings["disable_embedded_bitmaps"] = import_settings_data->get("disable_embedded_bitmaps");
main_settings["multichannel_signed_distance_field"] = import_settings_data->get("multichannel_signed_distance_field");
main_settings["msdf_pixel_range"] = import_settings_data->get("msdf_pixel_range");
main_settings["msdf_size"] = import_settings_data->get("msdf_size");
Expand Down Expand Up @@ -1245,6 +1248,7 @@ DynamicFontImportSettingsDialog::DynamicFontImportSettingsDialog() {

options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel"), 1));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps"), true));
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));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), 8));
options_general.push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_RANGE, "1,250,1"), 48));
Expand Down
3 changes: 3 additions & 0 deletions editor/import/resource_importer_dynamic_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void ResourceImporterDynamicFont::get_import_options(const String &p_path, List<

r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel"), 1));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps"), true));
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));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), 8));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_RANGE, "1,250,1"), 48));
Expand Down Expand Up @@ -145,6 +146,7 @@ Error ResourceImporterDynamicFont::import(const String &p_source_file, const Str

int antialiasing = p_options["antialiasing"];
bool generate_mipmaps = p_options["generate_mipmaps"];
bool disable_embedded_bitmaps = p_options["disable_embedded_bitmaps"];
bool msdf = p_options["multichannel_signed_distance_field"];
int px_range = p_options["msdf_pixel_range"];
int px_size = p_options["msdf_size"];
Expand All @@ -165,6 +167,7 @@ Error ResourceImporterDynamicFont::import(const String &p_source_file, const Str
font.instantiate();
font->set_data(data);
font->set_antialiasing((TextServer::FontAntialiasing)antialiasing);
font->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
font->set_generate_mipmaps(generate_mipmaps);
font->set_multichannel_signed_distance_field(msdf);
font->set_msdf_pixel_range(px_range);
Expand Down
22 changes: 21 additions & 1 deletion modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data,
if (p_font_data->force_autohinter) {
flags |= FT_LOAD_FORCE_AUTOHINT;
}
if (outline) {
if (outline || (p_font_data->disable_embedded_bitmaps && !FT_HAS_COLOR(fd->face))) {
flags |= FT_LOAD_NO_BITMAP;
} else if (FT_HAS_COLOR(fd->face)) {
flags |= FT_LOAD_COLOR;
Expand Down Expand Up @@ -2177,6 +2177,25 @@ TextServer::FontAntialiasing TextServerAdvanced::_font_get_antialiasing(const RI
return fd->antialiasing;
}

void TextServerAdvanced::_font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL(fd);

MutexLock lock(fd->mutex);
if (fd->disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
_font_clear_cache(fd);
fd->disable_embedded_bitmaps = p_disable_embedded_bitmaps;
}
}

bool TextServerAdvanced::_font_get_disable_embedded_bitmaps(const RID &p_font_rid) const {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL_V(fd, false);

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

void TextServerAdvanced::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
FontAdvanced *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL(fd);
Expand Down Expand Up @@ -4997,6 +5016,7 @@ RID TextServerAdvanced::_find_sys_font_for_text(const RID &p_fdef, const String
}

_font_set_antialiasing(sysf.rid, key.antialiasing);
_font_set_disable_embedded_bitmaps(sysf.rid, key.disable_embedded_bitmaps);
_font_set_generate_mipmaps(sysf.rid, key.mipmaps);
_font_set_multichannel_signed_distance_field(sysf.rid, key.msdf);
_font_set_msdf_pixel_range(sysf.rid, key.msdf_range);
Expand Down
10 changes: 8 additions & 2 deletions modules/text_server_adv/text_server_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class TextServerAdvanced : public TextServerExtension {
Mutex mutex;

TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
bool disable_embedded_bitmaps = true;
bool mipmaps = false;
bool msdf = false;
int msdf_range = 14;
Expand Down Expand Up @@ -559,6 +560,7 @@ class TextServerAdvanced : public TextServerExtension {
struct SystemFontKey {
String font_name;
TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
bool disable_embedded_bitmaps = true;
bool italic = false;
bool mipmaps = false;
bool msdf = false;
Expand All @@ -578,7 +580,7 @@ class TextServerAdvanced : public TextServerExtension {
float baseline_offset = 0.0;

bool operator==(const SystemFontKey &p_b) const {
return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (variation_coordinates == p_b.variation_coordinates) && (oversampling == p_b.oversampling) && (embolden == p_b.embolden) && (transform == p_b.transform) && (extra_spacing[SPACING_TOP] == p_b.extra_spacing[SPACING_TOP]) && (extra_spacing[SPACING_BOTTOM] == p_b.extra_spacing[SPACING_BOTTOM]) && (extra_spacing[SPACING_SPACE] == p_b.extra_spacing[SPACING_SPACE]) && (extra_spacing[SPACING_GLYPH] == p_b.extra_spacing[SPACING_GLYPH]) && (baseline_offset == p_b.baseline_offset);
return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (disable_embedded_bitmaps == p_b.disable_embedded_bitmaps) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (variation_coordinates == p_b.variation_coordinates) && (oversampling == p_b.oversampling) && (embolden == p_b.embolden) && (transform == p_b.transform) && (extra_spacing[SPACING_TOP] == p_b.extra_spacing[SPACING_TOP]) && (extra_spacing[SPACING_BOTTOM] == p_b.extra_spacing[SPACING_BOTTOM]) && (extra_spacing[SPACING_SPACE] == p_b.extra_spacing[SPACING_SPACE]) && (extra_spacing[SPACING_GLYPH] == p_b.extra_spacing[SPACING_GLYPH]) && (baseline_offset == p_b.baseline_offset);
}

SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerAdvanced *p_fb) {
Expand All @@ -587,6 +589,7 @@ class TextServerAdvanced : public TextServerExtension {
weight = p_weight;
stretch = p_stretch;
antialiasing = p_fb->_font_get_antialiasing(p_font);
disable_embedded_bitmaps = p_fb->_font_get_disable_embedded_bitmaps(p_font);
mipmaps = p_fb->_font_get_generate_mipmaps(p_font);
msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);
msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);
Expand Down Expand Up @@ -637,7 +640,7 @@ class TextServerAdvanced : public TextServerExtension {
hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_SPACE], hash);
hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_GLYPH], hash);
hash = hash_murmur3_one_double(p_a.baseline_offset, hash);
return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12), hash));
return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12) | ((int)p_a.disable_embedded_bitmaps << 14), hash));
}
};
mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;
Expand Down Expand Up @@ -751,6 +754,9 @@ class TextServerAdvanced : public TextServerExtension {
MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);

MODBIND2(font_set_disable_embedded_bitmaps, const RID &, bool);
MODBIND1RC(bool, font_get_disable_embedded_bitmaps, const RID &);

MODBIND2(font_set_generate_mipmaps, const RID &, bool);
MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);

Expand Down
22 changes: 21 additions & 1 deletion modules/text_server_fb/text_server_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data,
if (p_font_data->force_autohinter) {
flags |= FT_LOAD_FORCE_AUTOHINT;
}
if (outline) {
if (outline || (p_font_data->disable_embedded_bitmaps && !FT_HAS_COLOR(fd->face))) {
flags |= FT_LOAD_NO_BITMAP;
} else if (FT_HAS_COLOR(fd->face)) {
flags |= FT_LOAD_COLOR;
Expand Down Expand Up @@ -1170,6 +1170,25 @@ TextServer::FontAntialiasing TextServerFallback::_font_get_antialiasing(const RI
return fd->antialiasing;
}

void TextServerFallback::_font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) {
FontFallback *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL(fd);

MutexLock lock(fd->mutex);
if (fd->disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
_font_clear_cache(fd);
fd->disable_embedded_bitmaps = p_disable_embedded_bitmaps;
}
}

bool TextServerFallback::_font_get_disable_embedded_bitmaps(const RID &p_font_rid) const {
FontFallback *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL_V(fd, false);

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

void TextServerFallback::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
FontFallback *fd = _get_font_data(p_font_rid);
ERR_FAIL_NULL(fd);
Expand Down Expand Up @@ -3805,6 +3824,7 @@ RID TextServerFallback::_find_sys_font_for_text(const RID &p_fdef, const String
}

_font_set_antialiasing(sysf.rid, key.antialiasing);
_font_set_disable_embedded_bitmaps(sysf.rid, key.disable_embedded_bitmaps);
_font_set_generate_mipmaps(sysf.rid, key.mipmaps);
_font_set_multichannel_signed_distance_field(sysf.rid, key.msdf);
_font_set_msdf_pixel_range(sysf.rid, key.msdf_range);
Expand Down
Loading
Loading