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>
607 KiB
607 KiB