Skip to content

layoutInlineItems in rich-note demo can overflow maxWidth #3

@Huxpro

Description

@Huxpro

Summary

The layoutInlineItems function in the rich-note demo can produce lines whose accumulated width exceeds maxWidth by ~4px. This happens when chips and text fragments fill a line almost exactly, leaving near-zero remainingWidth for the next text item.

Reproduction

Run the rich-note demo at a width of ~320px. Line 7 ("Keep layoutNextLine() public, tag this P1 , ") computes to 324.2px against a 320px budget.

Root Cause

When remainingWidth is near zero after placing a chip:

  1. The text-case check reservedWidth >= remainingWidth passes (reservedWidth=0, remaining=ε)
  2. layoutNextLine(prepared, cursor, Math.max(1, remainingWidth)) inflates the budget to 1px
  3. layoutNextLine returns at least the first grapheme (e.g. , at ~4px) even though only ε was available
  4. The accumulated lineWidth exceeds safeWidth by the width of that grapheme

Observed on

Lynx port of pretext, where lynx.getTextInfo() measurements may differ slightly from rendering, making tight fits more likely. The algorithm issue exists in the upstream rich-note.ts as well — it just rarely triggers in browsers due to closer measurement/rendering agreement.

What we tried

  • Adding an overflow guard after layoutNextLine returns in the slow path — guard never fires, suggesting the overflow comes from a different code path than expected
  • Tightening the reservedWidth >= remainingWidth check with a 1px minimum — also didn't resolve it
  • Ruling out fontWeight mismatch (getTextInfo doesn't accept fontWeight) — tested with all weights at 400, issue persists

Possible fix direction

The overflow may involve interaction between the fast path (full text fits) and accumulated floating-point imprecision across multiple fragments with INLINE_BOUNDARY_GAP. Needs deeper tracing of the exact arithmetic on the overflowing line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions