Skip to content

Commit

Permalink
Fix TextServer::shaped_text_*_character_pos for the first character…
Browse files Browse the repository at this point in the history
… of wrapped string. Allow starting/ending RTL selection before line start.
  • Loading branch information
bruvzg committed Aug 8, 2024
1 parent 739019e commit 932acce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const V
bool line_clicked = false;
float text_rect_begin = 0.0;
int char_pos = -1;
bool char_clicked = false;
Line &l = p_frame->lines[p_line];
MutexLock lock(l.text_buf->get_mutex());

Expand Down Expand Up @@ -1578,6 +1579,9 @@ float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const V
}

if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
if (!p_meta) {
char_pos = rtl ? TS->shaped_text_get_range(rid).y : TS->shaped_text_get_range(rid).x;
}
if ((!rtl && p_click.x >= rect.position.x) || (rtl && p_click.x <= rect.position.x + rect.size.x)) {
if (p_meta) {
int64_t glyph_idx = TS->shaped_text_hit_test_grapheme(rid, p_click.x - rect.position.x);
Expand All @@ -1592,6 +1596,7 @@ float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const V
obj_rect.position.y += baseline_y;
if (p_click.y >= obj_rect.position.y && p_click.y <= obj_rect.position.y + obj_rect.size.y) {
char_pos = glyphs[glyph_idx].start;
char_clicked = true;
}
break;
}
Expand All @@ -1602,26 +1607,29 @@ float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const V
float fd = TS->font_get_descent(glyphs[glyph_idx].font_rid, glyphs[glyph_idx].font_size);
if (p_click.y >= baseline_y - fa && p_click.y <= baseline_y + fd) {
char_pos = glyphs[glyph_idx].start;
char_clicked = true;
}
} else if (!(glyphs[glyph_idx].flags & TextServer::GRAPHEME_IS_VIRTUAL)) {
// Hex code box.
Vector2 gl_size = TS->get_hex_code_box_size(glyphs[glyph_idx].font_size, glyphs[glyph_idx].index);
if (p_click.y >= baseline_y - gl_size.y * 0.9 && p_click.y <= baseline_y + gl_size.y * 0.2) {
char_pos = glyphs[glyph_idx].start;
char_clicked = true;
}
}
}
} else {
char_pos = TS->shaped_text_hit_test_position(rid, p_click.x - rect.position.x);
char_pos = TS->shaped_text_closest_character_pos(rid, char_pos);
char_clicked = true;
}
}
line_clicked = true;
text_rect_begin = rtl ? rect.position.x + rect.size.x : rect.position.x;
}

// If table hit was detected, and line hit is in the table bounds use table hit.
if (table_hit && (((char_pos + p_frame->lines[p_line].char_offset) >= table_range.x && (char_pos + p_frame->lines[p_line].char_offset) <= table_range.y) || char_pos == -1)) {
if (table_hit && (((char_pos + p_frame->lines[p_line].char_offset) >= table_range.x && (char_pos + p_frame->lines[p_line].char_offset) <= table_range.y) || !char_clicked)) {
if (r_click_frame != nullptr) {
*r_click_frame = table_click_frame;
}
Expand Down
6 changes: 3 additions & 3 deletions servers/text_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ int64_t TextServer::shaped_text_prev_grapheme_pos(const RID &p_shaped, int64_t p

int64_t TextServer::shaped_text_prev_character_pos(const RID &p_shaped, int64_t p_pos) const {
const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
int64_t prev = 0;
int64_t prev = shaped_text_get_range(p_shaped).x;
for (const int32_t &E : chars) {
if (E >= p_pos) {
return prev;
Expand All @@ -1575,7 +1575,7 @@ int64_t TextServer::shaped_text_prev_character_pos(const RID &p_shaped, int64_t

int64_t TextServer::shaped_text_next_character_pos(const RID &p_shaped, int64_t p_pos) const {
const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
int64_t prev = 0;
int64_t prev = shaped_text_get_range(p_shaped).x;
for (const int32_t &E : chars) {
if (E > p_pos) {
return E;
Expand All @@ -1587,7 +1587,7 @@ int64_t TextServer::shaped_text_next_character_pos(const RID &p_shaped, int64_t

int64_t TextServer::shaped_text_closest_character_pos(const RID &p_shaped, int64_t p_pos) const {
const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
int64_t prev = 0;
int64_t prev = shaped_text_get_range(p_shaped).x;
for (const int32_t &E : chars) {
if (E == p_pos) {
return E;
Expand Down

0 comments on commit 932acce

Please sign in to comment.