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);