Prove tap-to-position is scroll-offset independent; fix float32 decomposition bug

The screen->line tap mapping only needs the sub-line scroll remainder
(tapLocalY adds r, never the full scroll) because the visible glyph
layout is window-relative and IMEWindowStartByte re-anchors it to the
file. That holds for every scroll offset IF the window start line
k=floor(s/lh) and the sub-line remainder r stay consistent.

Property test (4000 random scroll/tap pairs, asserting against an
independent drawn-geometry ground truth, not the tap code's own math)
exposed a real bug: int(s/lh) in the Dp float32 domain rounds the
quotient to nearest and can round UP across an integer boundary while
the float64 mod still reflects the line below. In a sub-pixel-wide
band of scroll offsets the window started one line too far while the
draw shift lagged by one line - the whole rendered window (and every
tapped line) shifted by one.

Fix: one shared float64 floor decomposition (scrollDecompose) used by
the window start (visibleByteRangePrecise/Estimate), the renderer's
sub-line shift (visibleScrollOffset), the tap mapping (tapLocalY), and
chunk prefetching. Also route the shaper's line height (previously
ignored by visibleByteRangePrecise) through VisibleByteRange.

On-device cross-check: at s=4246.9 (r=13.3) and s=4210.7 (r=10.7),
taps on visually identified lines typed markers that landed on exactly
those lines in the file on disk; profiler ScrollDP, screenshot,
formula, and disk all agreed.

Docs: scroll decomposition invariant in architecture.md §6.2, pipeline
hop 2 in doc/README.md, Phase 10 in development_plan.md.
This commit is contained in:
gmp
2026-08-17 09:57:21 -04:00
parent 7f2d3144eb
commit e761436908
6 changed files with 280 additions and 29 deletions
+10 -4
View File
@@ -105,10 +105,16 @@ hops, not inside a space):
this is the only space `adb`/screenshots touch.
2. **app dp → text-local dp** — the tap/drag handlers
(`localX = x − EditorRegion.X`, `localY = tapLocalY(...)`).
**Y adds only the sub-line scroll remainder** (`ScrollOffset mod
lineHeight`), never the full scroll: the visible glyph layout is
window-relative, so adding the full scroll maps a tap to a line far
below the window (the Phase 7 tap-to-position bug).
**Y adds only the sub-line scroll remainder**, never the full scroll:
the visible glyph layout is window-relative, so adding the full scroll
maps a tap to a line far below the window (the Phase 7 tap-to-position
bug). The remainder and the window's start line come from ONE shared
float64 floor decomposition of `ScrollOffset` (line k, remainder r, with
k·lineHeight ≤ ScrollOffset < (k+1)·lineHeight): the window starts at
content line k, the renderer shifts the windowed layout up by r, and a
tap a dp below the region top maps to content line k +
⌊(a+r)/lineHeight⌋ = ⌊(a+ScrollOffset)/lineHeight⌋ — the line actually
under the finger, for every scroll offset (architecture.md §6.2).
3. **text-local dp → window-relative glyph byte** —
`visualLine = y / lineHeight`, then glyph x-search within that line
group. `GlyphLayout.ByteOffsets` are relative to the **top of the
+13
View File
@@ -233,6 +233,19 @@ Only the visible byte range is shaped and drawn each frame:
- `VisibleByteRange` maps scroll offset + viewport height → `[startLine,
endLine]` via the `LineIndex`, then to a byte range. The range is always
bounded by real lines of the document.
- **Scroll decomposition invariant.** The scroll offset s is split into a
content line k and sub-line remainder r (k·lh ≤ s < (k+1)·lh) by a single
float64 floor decomposition, and the three consumers of that split MUST
stay in lockstep: the window start line (VisibleByteRange), the renderer's
sub-line shift (the windowed layout is drawn shifted up by r), and the tap
mapping (`tapLocalY` adds r). With them consistent, a tap a dp below the
region top always maps to content line k + ⌊(a+r)/lh⌋ =
⌊(a+s)/lh⌋ — the line actually under the finger — for every s ≥ 0. The
decomposition must be computed in float64: a raw `int(s/lh)` in the Dp
float32 domain can round the quotient UP across an integer boundary while
a float64 mod still reflects the line below, so the window start and the
remainder disagree by one line in a sub-pixel-wide band of offsets and the
whole rendered window (hence every tapped line) shifts by one.
- **Never shape the whole file.** Lesson learned (Phase 3): the shaper's
internal `document` retains the backing array of its largest layout forever
(`reset()` keeps the cap), so one whole-file layout permanently inflated
+47 -3
View File
@@ -1,8 +1,9 @@
# Development Plan: reach a lean, usable Android text editor
Status: v8, 2026-08-17 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
Status: v9, 2026-08-17 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
verification + tap-to-position-cursor fix + selection + real-file e2e +
Android arrow/shift workaround + touch selection). Written
Android arrow/shift workaround + touch selection + scroll-offset tap proof &
float32 decomposition fix). Written
against the **live** repo `/home/gmp/pad`. v1 (the widget-rebuild plan) is
superseded — see §12 for why. Doc reorganization (2026-08-16): the over-detailed
docs (`*_implementation_plan.md`, `touch.md`, `element_model.md`,
@@ -24,7 +25,13 @@ refactor); fixed with `tapLocalY`. (2) the `GlyphLayout` byte offsets are
window-relative but the four cursor functions (tap, Home, End, vertical move)
treated them as absolute, so the cursor snapped to the window top; fixed by adding
the `IMEWindowStartByte` window base at each cursor boundary (`glyphBase`). Both
have regression tests.
have regression tests. Phase 10 (2026-08-17) proved the tap mapping
scroll-offset independent by property test (4000 random scroll/tap pairs) and
on-device (marker typed on a visually identified line landed on exactly that
line at two fractional scroll positions); the property test exposed a float32
`int(s/lh)` rounding bug that could shift the rendered window — and every
tapped line — by one in a sub-pixel band of scroll offsets, now fixed with a
single shared float64 floor decomposition (see §5, Phase 10).
Phase 8 (2026-08-16/17) added **text selection** (shift+arrow extend; insert/
backspace/delete replace the selection; IME unions its range with the active
selection) with rendering + IME wiring. It also added **real-file e2e tests**
@@ -410,6 +417,43 @@ contracts; the highlights:
menu close-on-tap, plain-tap caret placement, and scroll-drag not firing a
long press.
### Phase 10 — proving tap-to-position is scroll-offset independent — DONE (2026-08-17)
Question: does the screen→line tap mapping account for the scroll offset, and
can it be shown to map a screen tap to the right document line at *any* scroll
offset? The answer was "yes, except one razor-thin band" — and the exception is
now fixed and property-tested.
- **The mapping is structurally scroll-aware.** The visible glyph layout is
window-relative, so a tap only needs the *sub-line* remainder of the scroll
offset (`tapLocalY` adds r, never the full scroll), and the window start
line k = floor(s/lh) re-anchors window-relative bytes to absolute file bytes
(`IMEWindowStartByte`). With k and r consistent, a tap a dp below the region
top always lands on content line k + ⌊(a+r)/lh⌋ = ⌊(a+s)/lh⌋ — the line
under the finger — for every s ≥ 0, wrapped or not (wrap only changes which
real line a display line belongs to, never the drawn geometry).
- **Bug found by the property test:** `int(s/lh)` in the Dp float32 domain
rounds the quotient to nearest, and can round UP across an integer boundary
while the float64 remainder still reflects the line below. In a
sub-pixel-wide band of scroll offsets the window started one line too far
while the draw shift said one line back — the whole rendered window (and
every tapped line) was off by one. Fixed with a single shared float64 floor
decomposition (`scrollDecompose`) used by the window start
(`visibleByteRangePrecise`/`Estimate`), the renderer's sub-line shift
(`visibleScrollOffset`), the tap mapping (`tapLocalY`), and chunk
prefetching — the three consumers cannot disagree by construction.
- **Proof:** `tap_scroll_property_test.go` — 4000 random (scroll offset, tap
position) pairs (integer-line, near-boundary, and arbitrary fractional
offsets) assert the cursor lands on the line whose *independently computed*
drawn range contains the finger (ground truth ⌊(a+s)/lh⌋, not the tap code's
own math). Failed on the exact-boundary case before the fix; green after.
- **On-device cross-check:** at two scroll positions (s = 4246.9 dp, r = 13.3;
s = 4210.7 dp, r = 10.7, both with a fractional sub-line remainder read
from the profiler CSV), a tap on a visually identified line typed a marker
that landed on exactly that line in the file on disk (line 264 and line 258
of a 300-line file). Screenshot, profiler ScrollDP, formula, and disk all
agreed.
## 6. File-size decision (re-framed)
v1 framed this as "accept a limit vs build a windowed editor." The live repo