Skip to content

Commit

Permalink
Merge pull request #90196 from bruvzg/ts_tcase
Browse files Browse the repository at this point in the history
[TextServer] Expose ICU title case string conversion to scripting.
  • Loading branch information
akien-mga committed Apr 5, 2024
2 parents fe78457 + 27f6f41 commit ca2d286
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/classes/TextServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,16 @@
[b]Note:[/b] The result may be longer or shorter than the original.
</description>
</method>
<method name="string_to_title" qualifiers="const">
<return type="String" />
<param index="0" name="string" type="String" />
<param index="1" name="language" type="String" default="&quot;&quot;" />
<description>
Returns the string converted to title case.
[b]Note:[/b] Casing is locale dependent and context sensitive if server support [constant FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION] feature (supported by [TextServerAdvanced]).
[b]Note:[/b] The result may be longer or shorter than the original.
</description>
</method>
<method name="string_to_upper" qualifiers="const">
<return type="String" />
<param index="0" name="string" type="String" />
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/TextServerExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,15 @@
Returns the string converted to lowercase.
</description>
</method>
<method name="_string_to_title" qualifiers="virtual const">
<return type="String" />
<param index="0" name="string" type="String" />
<param index="1" name="language" type="String" />
<description>
[b]Optional.[/b]
Returns the string converted to title case.
</description>
</method>
<method name="_string_to_upper" qualifiers="virtual const">
<return type="String" />
<param index="0" name="string" type="String" />
Expand Down
28 changes: 28 additions & 0 deletions modules/text_server_adv/text_server_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6976,6 +6976,34 @@ String TextServerAdvanced::_string_to_lower(const String &p_string, const String
return String::utf16(lower.ptr(), len);
}

String TextServerAdvanced::_string_to_title(const String &p_string, const String &p_language) const {
#ifndef ICU_STATIC_DATA
if (!icu_data_loaded) {
return p_string.capitalize();
}
#endif

if (p_string.is_empty()) {
return p_string;
}
const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;

// Convert to UTF-16.
Char16String utf16 = p_string.utf16();

Vector<char16_t> upper;
UErrorCode err = U_ZERO_ERROR;
int32_t len = u_strToTitle(nullptr, 0, utf16.get_data(), -1, nullptr, lang.ascii().get_data(), &err);
ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
upper.resize(len);
err = U_ZERO_ERROR;
u_strToTitle(upper.ptrw(), len, utf16.get_data(), -1, nullptr, lang.ascii().get_data(), &err);
ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));

// Convert back to UTF-32.
return String::utf16(upper.ptr(), len);
}

PackedInt32Array TextServerAdvanced::_string_get_word_breaks(const String &p_string, const String &p_language, int64_t p_chars_per_line) const {
const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
// Convert to UTF-16.
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 @@ -991,6 +991,7 @@ class TextServerAdvanced : public TextServerExtension {

MODBIND2RC(String, string_to_upper, const String &, const String &);
MODBIND2RC(String, string_to_lower, const String &, const String &);
MODBIND2RC(String, string_to_title, const String &, const String &);

MODBIND0(cleanup);

Expand Down
4 changes: 4 additions & 0 deletions modules/text_server_fb/text_server_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4439,6 +4439,10 @@ String TextServerFallback::_string_to_lower(const String &p_string, const String
return p_string.to_lower();
}

String TextServerFallback::_string_to_title(const String &p_string, const String &p_language) const {
return p_string.capitalize();
}

PackedInt32Array TextServerFallback::_string_get_word_breaks(const String &p_string, const String &p_language, int64_t p_chars_per_line) const {
PackedInt32Array ret;

Expand Down
1 change: 1 addition & 0 deletions modules/text_server_fb/text_server_fb.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ class TextServerFallback : public TextServerExtension {

MODBIND2RC(String, string_to_upper, const String &, const String &);
MODBIND2RC(String, string_to_lower, const String &, const String &);
MODBIND2RC(String, string_to_title, const String &, const String &);

MODBIND0(cleanup);

Expand Down
9 changes: 9 additions & 0 deletions servers/text/text_server_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ void TextServerExtension::_bind_methods() {

GDVIRTUAL_BIND(_string_to_upper, "string", "language");
GDVIRTUAL_BIND(_string_to_lower, "string", "language");
GDVIRTUAL_BIND(_string_to_title, "string", "language");

GDVIRTUAL_BIND(_parse_structured_text, "parser_type", "args", "text");

Expand Down Expand Up @@ -1507,6 +1508,14 @@ String TextServerExtension::string_to_upper(const String &p_string, const String
return p_string;
}

String TextServerExtension::string_to_title(const String &p_string, const String &p_language) const {
String ret;
if (GDVIRTUAL_CALL(_string_to_title, p_string, p_language, ret)) {
return ret;
}
return p_string;
}

String TextServerExtension::string_to_lower(const String &p_string, const String &p_language) const {
String ret;
if (GDVIRTUAL_CALL(_string_to_lower, p_string, p_language, ret)) {
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 @@ -566,8 +566,10 @@ class TextServerExtension : public TextServer {

virtual String string_to_upper(const String &p_string, const String &p_language = "") const override;
virtual String string_to_lower(const String &p_string, const String &p_language = "") const override;
virtual String string_to_title(const String &p_string, const String &p_language = "") const override;
GDVIRTUAL2RC(String, _string_to_upper, const String &, const String &);
GDVIRTUAL2RC(String, _string_to_lower, const String &, const String &);
GDVIRTUAL2RC(String, _string_to_title, const String &, const String &);

TypedArray<Vector3i> parse_structured_text(StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const;
GDVIRTUAL3RC(TypedArray<Vector3i>, _parse_structured_text, StructuredTextParser, const Array &, const String &);
Expand Down
1 change: 1 addition & 0 deletions servers/text_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ void TextServer::_bind_methods() {

ClassDB::bind_method(D_METHOD("string_to_upper", "string", "language"), &TextServer::string_to_upper, DEFVAL(""));
ClassDB::bind_method(D_METHOD("string_to_lower", "string", "language"), &TextServer::string_to_lower, DEFVAL(""));
ClassDB::bind_method(D_METHOD("string_to_title", "string", "language"), &TextServer::string_to_title, DEFVAL(""));

ClassDB::bind_method(D_METHOD("parse_structured_text", "parser_type", "args", "text"), &TextServer::parse_structured_text);

Expand Down
1 change: 1 addition & 0 deletions servers/text_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ class TextServer : public RefCounted {
// Other string operations.
virtual String string_to_upper(const String &p_string, const String &p_language = "") const = 0;
virtual String string_to_lower(const String &p_string, const String &p_language = "") const = 0;
virtual String string_to_title(const String &p_string, const String &p_language = "") const = 0;

TypedArray<Vector3i> parse_structured_text(StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const;

Expand Down

0 comments on commit ca2d286

Please sign in to comment.