From ed3f14686d1a8328c384674cb2f7a665245ef0a9 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Sun, 18 Jun 2023 19:26:53 +0200 Subject: [PATCH] Fix: Layouter::GetCharAtPosition counting wrong Bug introduced in commit 60399e --- src/gfx_layout.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index 87b9495409..4ed2204363 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -255,9 +255,9 @@ Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const } /** - * Get the character that is at a position. + * Get the character that is at a pixel position in the first line of the layouted text. * @param x Position in the string. - * @return Index of the position or -1 if no character is at the position. + * @return String offset of the position (bytes) or -1 if no character is at the position. */ ptrdiff_t Layouter::GetCharAtPosition(int x) const { @@ -278,9 +278,8 @@ ptrdiff_t Layouter::GetCharAtPosition(int x) const size_t index = run.GetGlyphToCharMap()[i]; size_t cur_idx = 0; - int char_index = 0; - for (auto str = this->string.begin(); str != this->string.end(); char_index++) { - if (cur_idx == index) return char_index; + for (auto str = this->string.begin(); str != this->string.end();) { + if (cur_idx == index) return str - this->string.begin(); WChar c = Utf8Consume(str); cur_idx += line->GetInternalCharLength(c);