From 4765d0f8c2a351ad2e2d11b32eb8433582d4c119 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Sat, 3 Apr 2021 00:49:57 +0200 Subject: [PATCH] Change: Text Layouter support querying all lines for character at pixel --- src/gfx.cpp | 2 +- src/gfx_layout.cpp | 7 +++++-- src/gfx_layout.h | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index ef2bbc06f3..24329c430e 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -899,7 +899,7 @@ ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize if (x < 0) return -1; Layouter layout(str, INT32_MAX, TC_FROMSTRING, start_fontsize); - return layout.GetCharAtPosition(x); + return layout.GetCharAtPosition(x, 0); } /** diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index 6ae31bfd15..2f479247ec 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -270,11 +270,14 @@ Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const /** * Get the character that is at a pixel position in the first line of the layouted text. * @param x Position in the string. + * @param line_index Which line of the layout to search * @return String offset of the position (bytes) or -1 if no character is at the position. */ -ptrdiff_t Layouter::GetCharAtPosition(int x) const +ptrdiff_t Layouter::GetCharAtPosition(int x, size_t line_index) const { - const auto &line = this->front(); + if (line_index >= this->size()) return -1; + + const auto &line = this->at(line_index); for (int run_index = 0; run_index < line->CountRuns(); run_index++) { const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index); diff --git a/src/gfx_layout.h b/src/gfx_layout.h index 3a5b48d316..0fe321921b 100644 --- a/src/gfx_layout.h +++ b/src/gfx_layout.h @@ -177,7 +177,7 @@ public: Layouter(std::string_view str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL); Dimension GetBounds(); Point GetCharPosition(std::string_view::const_iterator ch) const; - ptrdiff_t GetCharAtPosition(int x) const; + ptrdiff_t GetCharAtPosition(int x, size_t line_index) const; static void ResetFontCache(FontSize size); static void ResetLineCache();