Skip to content

Commit

Permalink
Merge pull request #80848 from bruvzg/rtl_lang
Browse files Browse the repository at this point in the history
[RTL] Adds "lang" tag to allow overriding language specific text rendering without starting a new paragraph.
  • Loading branch information
akien-mga committed Aug 21, 2023
2 parents ddb7430 + 299808c commit 8e0346b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@
Adds a [code][font][/code] tag with an italics font to the tag stack. This is the same as adding an [code][i][/code] tag if not currently in a [code][b][/code] tag.
</description>
</method>
<method name="push_language">
<return type="void" />
<param index="0" name="language" type="String" />
<description>
Adds language code used for text shaping algorithm and Open-Type font features.
</description>
</method>
<method name="push_list">
<return type="void" />
<param index="0" name="level" type="int" />
Expand Down
22 changes: 21 additions & 1 deletion scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,10 @@ String RichTextLabel::_find_language(Item *p_item) {
Item *item = p_item;

while (item) {
if (item->type == ITEM_PARAGRAPH) {
if (item->type == ITEM_LANGUAGE) {
ItemLanguage *p = static_cast<ItemLanguage *>(item);
return p->language;
} else if (item->type == ITEM_PARAGRAPH) {
ItemParagraph *p = static_cast<ItemParagraph *>(item);
return p->language;
}
Expand Down Expand Up @@ -3443,6 +3446,17 @@ void RichTextLabel::push_meta(const Variant &p_meta) {
_add_item(item, true);
}

void RichTextLabel::push_language(const String &p_language) {
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemLanguage *item = memnew(ItemLanguage);

item->language = p_language;
_add_item(item, true);
}

void RichTextLabel::push_hint(const String &p_string) {
_stop_thread();
MutexLock data_lock(data_mutex);
Expand Down Expand Up @@ -4224,6 +4238,11 @@ void RichTextLabel::append_text(const String &p_bbcode) {
push_indent(indent_level);
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag.begins_with("lang=")) {
String lang = tag.substr(5, tag.length()).unquote();
push_language(lang);
pos = brk_end + 1;
tag_stack.push_front("lang");
} else if (tag == "p") {
push_paragraph(HORIZONTAL_ALIGNMENT_LEFT);
pos = brk_end + 1;
Expand Down Expand Up @@ -5609,6 +5628,7 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_list", "level", "type", "capitalize", "bullet"), &RichTextLabel::push_list, DEFVAL(String::utf8("")));
ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta);
ClassDB::bind_method(D_METHOD("push_hint", "description"), &RichTextLabel::push_hint);
ClassDB::bind_method(D_METHOD("push_language", "language"), &RichTextLabel::push_language);
ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
ClassDB::bind_method(D_METHOD("push_table", "columns", "inline_align", "align_to_row"), &RichTextLabel::push_table, DEFVAL(INLINE_ALIGNMENT_TOP), DEFVAL(-1));
Expand Down
9 changes: 8 additions & 1 deletion scene/gui/rich_text_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class RichTextLabel : public Control {
ITEM_HINT,
ITEM_DROPCAP,
ITEM_CUSTOMFX,
ITEM_CONTEXT
ITEM_CONTEXT,
ITEM_LANGUAGE,
};

enum MenuItems {
Expand Down Expand Up @@ -237,6 +238,11 @@ class RichTextLabel : public Control {
ItemHint() { type = ITEM_HINT; }
};

struct ItemLanguage : public Item {
String language;
ItemLanguage() { type = ITEM_LANGUAGE; }
};

struct ItemParagraph : public Item {
HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
String language;
Expand Down Expand Up @@ -615,6 +621,7 @@ class RichTextLabel : public Control {
void push_outline_color(const Color &p_color);
void push_underline();
void push_strikethrough();
void push_language(const String &p_language);
void push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction = Control::TEXT_DIRECTION_INHERITED, const String &p_language = "", TextServer::StructuredTextParser p_st_parser = TextServer::STRUCTURED_TEXT_DEFAULT, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE, const PackedFloat32Array &p_tab_stops = PackedFloat32Array());
void push_indent(int p_level);
void push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet = String::utf8(""));
Expand Down

0 comments on commit 8e0346b

Please sign in to comment.