Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix 26.6 fixed point conversion in MSDF outline decomposition

Rémi Verschelde59a9a337b4cb
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml
index 67b2d2bc68a..1e81590026c 100644
--- a/doc/classes/ProjectSettings.xml
+++ b/doc/classes/ProjectSettings.xml
@@ -1266,6 +1266,10 @@
<member name="gui/common/text_edit_undo_stack_max_size" type="int" setter="" getter="" default="1024">
Maximum undo/redo history size for [TextEdit] fields.
</member>
+ <member name="gui/fonts/compatibility/msdf_legacy_scaling" type="bool" setter="" getter="" default="false">
+ If set to [code]true[/code], multichannel signed distance field (MSDF) glyph shapes are scaled using the divisor used before Godot 4.8, which was incorrect and made MSDF text render about 6.67% larger than intended.
+ This setting is provided for compatibility with existing projects that relied on the incorrect scaling and would otherwise have their layout shifted by the fix. New projects should leave this disabled.
+ </member>
<member name="gui/fonts/dynamic_fonts/use_oversampling" type="bool" setter="" getter="" default="true">
If set to [code]true[/code] and [member display/window/stretch/mode] is set to [code]"canvas_items"[/code], font and [DPITexture] oversampling is enabled in the main window. Use [member Viewport.oversampling] to control oversampling in other viewports and windows.
</member>
diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp
index b5591184a95..c73d8654aca 100644
--- a/modules/text_server_adv/text_server_adv.cpp
+++ b/modules/text_server_adv/text_server_adv.cpp
@@ -907,6 +907,11 @@ _FORCE_INLINE_ TextServerAdvanced::FontTexturePosition TextServerAdvanced::find_
#ifdef MODULE_MSDFGEN_ENABLED
+// FreeType outline coordinates are 26.6 fixed point, so one pixel is 64 units.
+// Godot versions before 4.8 used an incorrect divisor of 60, which can be
+// restored via the `gui/fonts/compatibility/msdf_legacy_scaling` project setting.
+static double ft_units_per_pixel = 64.0;
+
struct MSContext {
msdfgen::Point2 position;
msdfgen::Shape *shape = nullptr;
@@ -935,7 +940,7 @@ struct MSDFThreadData {
};
static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
- return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
+ return msdfgen::Point2(vector.x / ft_units_per_pixel, vector.y / ft_units_per_pixel);
}
static int ft_move_to(const FT_Vector *to, void *user) {
@@ -8446,6 +8451,11 @@ TextServerAdvanced::TextServerAdvanced() {
_bmp_create_font_funcs();
_update_settings();
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextServerAdvanced::_update_settings));
+#if defined(MODULE_MSDFGEN_ENABLED) && !defined(DISABLE_DEPRECATED)
+ if (GLOBAL_GET("gui/fonts/compatibility/msdf_legacy_scaling")) {
+ ft_units_per_pixel = 60.0;
+ }
+#endif
}
void TextServerAdvanced::_font_clear_system_fallback_cache() {
diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp
index 7857e2f15a0..130db934314 100644
--- a/modules/text_server_fb/text_server_fb.cpp
+++ b/modules/text_server_fb/text_server_fb.cpp
@@ -324,6 +324,11 @@ _FORCE_INLINE_ TextServerFallback::FontTexturePosition TextServerFallback::find_
#ifdef MODULE_MSDFGEN_ENABLED
+// FreeType outline coordinates are 26.6 fixed point, so one pixel is 64 units.
+// Godot versions before 4.8 used an incorrect divisor of 60, which can be
+// restored via the `gui/fonts/compatibility/msdf_legacy_scaling` project setting.
+static double ft_units_per_pixel = 64.0;
+
struct MSContext {
msdfgen::Point2 position;
msdfgen::Shape *shape = nullptr;
@@ -352,7 +357,7 @@ struct MSDFThreadData {
};
static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
- return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
+ return msdfgen::Point2(vector.x / ft_units_per_pixel, vector.y / ft_units_per_pixel);
}
static int ft_move_to(const FT_Vector *to, void *user) {
@@ -5380,6 +5385,11 @@ void TextServerFallback::_update_settings() {
TextServerFallback::TextServerFallback() {
_insert_feature_sets();
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextServerFallback::_update_settings));
+#if defined(MODULE_MSDFGEN_ENABLED) && !defined(DISABLE_DEPRECATED)
+ if (GLOBAL_GET("gui/fonts/compatibility/msdf_legacy_scaling")) {
+ ft_units_per_pixel = 60.0;
+ }
+#endif
}
void TextServerFallback::_font_clear_system_fallback_cache() {
diff --git a/servers/text/text_server.cpp b/servers/text/text_server.cpp
index 0f7cf4e34b5..c58f46ee067 100644
--- a/servers/text/text_server.cpp
+++ b/servers/text/text_server.cpp
@@ -2400,6 +2400,12 @@ TextServer::TextServer() {
GLOBAL_DEF_RST("gui/theme/default_font_generate_mipmaps", false);
GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/theme/lcd_subpixel_layout", PROPERTY_HINT_ENUM, "Disabled,Horizontal RGB,Horizontal BGR,Vertical RGB,Vertical BGR"), 1);
+
+#ifndef DISABLE_DEPRECATED
+ // Compatibility with Godot versions before 4.8 where MSDF glyph shapes were incorrectly scaled up.
+ GLOBAL_DEF_RST("gui/fonts/compatibility/msdf_legacy_scaling", false);
+#endif
+
GLOBAL_DEF_BASIC("internationalization/locale/include_text_server_data", false);
GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "internationalization/locale/line_breaking_strictness", PROPERTY_HINT_ENUM, "Auto,Loose,Normal,Strict"), 0);