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
Changes from 1 commit
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
35 changes: 24 additions & 11 deletions scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1596,19 +1596,32 @@ void RichTextLabel::add_text(const String &p_text) {
else
line = p_text.substr(pos, end - pos);

if (line.length() > 0) {

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
while (lipos < line.length()) {
if (line[lipos] >= 0x3040 && line[lipos] < 0xfaff) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This range includes multiple non CJK blocks. Probably should be limited to 3400 — 4DBF, 4E00 — 9FFF, F900 — FAFF and 20000 — 2A6DF, 2F800 — 2FA1F (last two won't work on Windows).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the reference, master branch use ICU break iterator with the following rules set:line_normal_cj.txt and 4MB dictionary: cjdict.txt.

If I understand correctly, this approach should work for pure ideographs, but not for mixed syllabary + ideographs (Okurigana). But since ICU based breaking won't be backported to 3.2, it's probably better than nothing.

Also, I'm not sure if it's good for performance to add a new ItemText for each word, it might be better to do it in the _process_line instead.

//Chinese or Japanese word condition
ItemText *item = memnew(ItemText);
item->text = line;
item->text = line.substr(lipos, 1);
_add_item(item, false);
//append one by one
lipos++;
} else {
//English word condition
int o;//length of English words
for (o = 1; o < (line.length() - lipos) && (line[lipos + o] < 0x3040 || line[lipos + o] > 0xfaff); o++) {
}
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.substr(lipos, o);
_invalidate_current_line(main);

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

Expand Down