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

[3.x] Fix Chinese&Japanese erroneous newline #45290

Closed
wants to merge 8 commits into from
Closed
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
45 changes: 34 additions & 11 deletions scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ int RichTextLabel::_process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int &
cw = tab_size * font->get_char_size(' ').width;
}

if (end > 0 && w + cw + begin > p_width) {
if (end > 0 && (w + cw + begin > p_width || (text->isCJK && wofs - backtrack + w + cw > p_width))) {
break; //don't allow lines longer than assigned width
}

Expand Down Expand Up @@ -1596,21 +1596,44 @@ void RichTextLabel::add_text(const String &p_text) {
else
line = p_text.substr(pos, end - pos);

if (line.length() > 0) {
#define IS_CJK(index) (line[index] >= 0x3040 && ((line[index] >= 0x31c0 && line[index] <= 0x9fff) || line[index] <= 0x30ff || (line[index] >= 0xf900 && line[index] <= 0xfaff)))
//add more rule here,to support more languages 3040~30ff 31c0~9fff f900~faff

if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
//append text condition!
ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
ti->text += line;
_invalidate_current_line(main);

} else {
//append item condition
int lipos = 0;
while (lipos < line.length()) {
if (IS_CJK(lipos)) {
//Chinese or Japanese word condition
int o; //length of CJK
for (o = 1; o < (line.length() - lipos) && IS_CJK(lipos + o); o++) {
}
ItemText *item = memnew(ItemText);
item->text = line;
item->isCJK = true;
item->text = line.substr(lipos, o);
_add_item(item, false);
lipos += o;
} else {
//English word condition
int o; //length of English words
for (o = 1; o < (line.length() - lipos) && !(IS_CJK(lipos + o)); o++) {
}
if (lipos == 0 && current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT &&
!(static_cast<ItemText *>(current->subitems.back()->get())->isCJK)) {
//append text condition!
ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
ti->text += line.substr(lipos, o);
_invalidate_current_line(main);

} else {
//append item condition
ItemText *item = memnew(ItemText);
item->text = line.substr(lipos, o);
item->isCJK = false;
_add_item(item, false);
}
lipos += o;
}
}
#undef IS_CJK

if (eol) {

Expand Down
1 change: 1 addition & 0 deletions scene/gui/rich_text_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class RichTextLabel : public Control {

struct ItemText : public Item {
String text;
bool isCJK;
ItemText() { type = ITEM_TEXT; }
};

Expand Down