perf(input-field): O(n) cursor hit test via Font::NearestCursorByte (#56)
InputField_HitTestCursor mapped a click x to a cursor byte offset by calling Font::GetLineWidth on every prefix value[0..i] for i=1..size. Each call re-walks from byte 0, so the hit test was O(n^2) glyph-metric lookups (each an uncached stbtt cmap binary search). It also iterated raw byte boundaries, so on multibyte UTF-8 input it could return an offset splitting a codepoint — an invalid position for the byte-based cursorPos. Add Font::NearestCursorByte: one left-to-right cumulative-advance pass (O(n) metrics, scale computed once) over codepoint boundaries, returning the byte offset of the boundary nearest the target. Cumulative advance is monotonic, so the scan stops past the closest boundary. The hit test now delegates to it. Adds tests/InputFieldHitTest, a pure-CPU regression test (no Vulkan device/window): a px sweep across ASCII and multibyte strings compared against an independent brute-force nearest-boundary oracle, plus the left/right/empty/end-of-text edge cases and a codepoint-boundary invariant. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
c58b68ec40
commit
d712218f17
6 changed files with 280 additions and 16 deletions
|
|
@ -60,6 +60,15 @@ namespace Crafter {
|
|||
stbtt_fontinfo font;
|
||||
Font(const std::filesystem::path& font);
|
||||
std::uint32_t GetLineWidth(const std::string_view text, float size);
|
||||
// Map a target advance (px from the line start) to the nearest UTF-8
|
||||
// codepoint boundary, returning its BYTE offset into `text`. A single
|
||||
// left-to-right pass accumulates per-glyph advances (O(n) metric
|
||||
// lookups), instead of re-walking the prefix for every boundary as a
|
||||
// naive caller would. Cumulative advance is monotonic (advances are
|
||||
// non-negative), so the scan stops at the first boundary past the
|
||||
// closest one. Returned offsets land on codepoint boundaries, which are
|
||||
// exactly the valid positions for a byte-based text cursor.
|
||||
std::size_t NearestCursorByte(std::string_view text, float size, float target);
|
||||
float LineHeight(float size);
|
||||
float AscentPx(float size);
|
||||
float ScaleForSize(float size);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue