Skip to content

Commit 8e096d4

Browse files
committed
Merge pull request #109415 from bruvzg/caret_range
[TextServer] Fix caret hit test rounding.
2 parents 7f96fc5 + d13b536 commit 8e096d4

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

scene/gui/line_edit.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,9 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) {
18251825
}
18261826

18271827
int ofs = std::ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset));
1828+
if (ofs == -1) {
1829+
return;
1830+
}
18281831
if (!caret_mid_grapheme_enabled) {
18291832
ofs = TS->shaped_text_closest_character_pos(text_rid, ofs);
18301833
}

scene/gui/rich_text_label.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,9 +1770,11 @@ float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const V
17701770
}
17711771
}
17721772
} else {
1773-
char_pos = TS->shaped_text_hit_test_position(rid, p_click.x - rect.position.x);
1774-
char_pos = TS->shaped_text_closest_character_pos(rid, char_pos);
1775-
char_clicked = true;
1773+
int click_char_pos = TS->shaped_text_hit_test_position(rid, p_click.x - rect.position.x);
1774+
if (click_char_pos != -1) {
1775+
char_pos = TS->shaped_text_closest_character_pos(rid, click_char_pos);
1776+
char_clicked = true;
1777+
}
17761778
}
17771779
}
17781780
line_clicked = true;

scene/gui/text_edit.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5034,6 +5034,9 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line
50345034
}
50355035

50365036
int col = TS->shaped_text_hit_test_position(text_rid, colx);
5037+
if (col == -1) {
5038+
return Point2i(-1, -1);
5039+
}
50375040
if (!caret_mid_grapheme_enabled) {
50385041
col = TS->shaped_text_closest_character_pos(text_rid, col);
50395042
}
@@ -8197,6 +8200,9 @@ int TextEdit::_get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) con
81978200
p_px -= wrap_indent;
81988201
}
81998202
int ofs = TS->shaped_text_hit_test_position(text_rid, p_px);
8203+
if (ofs == -1) {
8204+
return 0;
8205+
}
82008206
if (!caret_mid_grapheme_enabled) {
82018207
ofs = TS->shaped_text_closest_character_pos(text_rid, ofs);
82028208
}

servers/text_server.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ int64_t TextServer::shaped_text_hit_test_position(const RID &p_shaped, double p_
15391539
// Cursor placement hit test.
15401540

15411541
// Place caret to the left of the leftmost grapheme, or to position 0 if string is empty.
1542-
if (p_coords <= 0) {
1542+
if (Math::floor(p_coords) <= 0) {
15431543
if (v_size > 0) {
15441544
if ((glyphs[0].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
15451545
return glyphs[0].end;
@@ -1552,7 +1552,7 @@ int64_t TextServer::shaped_text_hit_test_position(const RID &p_shaped, double p_
15521552
}
15531553

15541554
// Place caret to the right of the rightmost grapheme, or to position 0 if string is empty.
1555-
if (p_coords >= shaped_text_get_width(p_shaped)) {
1555+
if (Math::ceil(p_coords) >= shaped_text_get_width(p_shaped)) {
15561556
if (v_size > 0) {
15571557
if ((glyphs[v_size - 1].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
15581558
return glyphs[v_size - 1].start;
@@ -1622,7 +1622,8 @@ int64_t TextServer::shaped_text_hit_test_position(const RID &p_shaped, double p_
16221622
}
16231623
off += glyphs[i].advance * glyphs[i].repeat;
16241624
}
1625-
return 0;
1625+
1626+
return -1;
16261627
}
16271628

16281629
Vector2 TextServer::shaped_text_get_grapheme_bounds(const RID &p_shaped, int64_t p_pos) const {

0 commit comments

Comments
 (0)