From e87cccefca917e45f928c132097c5f444f7e6a2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?=
<7645683+bruvzg@users.noreply.github.com>
Date: Thu, 12 Jun 2025 09:26:52 +0300
Subject: [PATCH] Add `line_breaking_strictness` project setting.
---
doc/classes/ProjectSettings.xml | 8 ++++++++
main/main.cpp | 1 +
modules/text_server_adv/text_server_adv.cpp | 14 +++++++++++++-
modules/text_server_adv/text_server_adv.h | 8 ++++++++
4 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml
index 540f6745073..c9466f59deb 100644
--- a/doc/classes/ProjectSettings.xml
+++ b/doc/classes/ProjectSettings.xml
@@ -1646,6 +1646,14 @@
[b]Note:[/b] "ICU / HarfBuzz / Graphite" text server data includes dictionaries for Burmese, Chinese, Japanese, Khmer, Lao and Thai as well as Unicode Standard Annex #29 and Unicode Standard Annex #14 word and line breaking rules. Data is about 4 MB large.
[b]Note:[/b] [TextServerFallback] does not use additional data.
+
+ Default strictness of line-breaking rules. Can be overridden by adding [code]@lb={auto,loose,normal,strict}[/code] to the language code.
+ - [b]Auto[/b] ([code]0[/code]) - strictness is based on the length of the line.
+ - [b]Loose[/b] ([code]1[/code]) - the least restrictive set of line-breaking rules. Typically used for short lines.
+ - [b]Normal[/b] ([code]2[/code]) - the most common set of line-breaking rules.
+ - [b]Strict[/b] ([code]3[/code]) - the most stringent set of line-breaking rules.
+ See [url=https://www.w3.org/TR/css-text-3/#line-break-property]Line Breaking Strictness: the line-break property[/url] for more info.
+
If non-empty, this locale will be used instead of the automatically detected system locale.
[b]Note:[/b] This setting also applies to the exported project. To only affect testing within the editor, override this setting with an [code]editor[/code] [url=$DOCS_URL/tutorials/export/feature_tags.html]feature tag[/url] for localization testing purposes.
diff --git a/main/main.cpp b/main/main.cpp
index 30d6ba11bc7..e7a35bf0e25 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -2638,6 +2638,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}
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);
OS::get_singleton()->_allow_hidpi = GLOBAL_DEF("display/window/dpi/allow_hidpi", true);
OS::get_singleton()->_allow_layered = GLOBAL_DEF_RST("display/window/per_pixel_transparency/allowed", false);
diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp
index 632749f9a60..75200e2dbb5 100644
--- a/modules/text_server_adv/text_server_adv.cpp
+++ b/modules/text_server_adv/text_server_adv.cpp
@@ -6513,7 +6513,17 @@ UBreakIterator *TextServerAdvanced::_create_line_break_iterator_for_locale(const
// Creating UBreakIterator (ubrk_open) is surprisingly costly.
// However, cloning (ubrk_clone) is cheaper, so we keep around blueprints to accelerate creating new ones.
- const String language = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
+ String language = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
+ if (!language.contains("@")) {
+ if (lb_strictness == LB_LOOSE) {
+ language += "@lb=loose";
+ } else if (lb_strictness == LB_NORMAL) {
+ language += "@lb=normal";
+ } else if (lb_strictness == LB_STRICT) {
+ language += "@lb=strict";
+ }
+ }
+
_THREAD_SAFE_METHOD_
const HashMap::Iterator key_value = line_break_iterators_per_language.find(language);
if (key_value) {
@@ -8066,12 +8076,14 @@ bool TextServerAdvanced::_is_valid_letter(uint64_t p_unicode) const {
void TextServerAdvanced::_update_settings() {
lcd_subpixel_layout.set((TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout"));
+ lb_strictness = (LineBreakStrictness)(int)GLOBAL_GET("internationalization/locale/line_breaking_strictness");
}
TextServerAdvanced::TextServerAdvanced() {
_insert_num_systems_lang();
_insert_feature_sets();
_bmp_create_font_funcs();
+ _update_settings();
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextServerAdvanced::_update_settings));
}
diff --git a/modules/text_server_adv/text_server_adv.h b/modules/text_server_adv/text_server_adv.h
index bf8f8c75072..a6b67afa930 100644
--- a/modules/text_server_adv/text_server_adv.h
+++ b/modules/text_server_adv/text_server_adv.h
@@ -152,7 +152,15 @@ class TextServerAdvanced : public TextServerExtension {
HashMap feature_sets;
HashMap feature_sets_inv;
+ enum LineBreakStrictness {
+ LB_AUTO,
+ LB_LOOSE,
+ LB_NORMAL,
+ LB_STRICT,
+ };
+
SafeNumeric lcd_subpixel_layout{ TextServer::FontLCDSubpixelLayout::FONT_LCD_SUBPIXEL_LAYOUT_NONE };
+ LineBreakStrictness lb_strictness = LB_AUTO;
void _update_settings();
void _insert_num_systems_lang();