text/v2: expose single-face layout runs, geometry, and caret positions
Operating System
- Windows
- macOS
- Linux
- FreeBSD
- OpenBSD
- Android
- iOS
- Nintendo Switch
- PlayStation 5
- Xbox
- Web Browsers
This is a platform-independent text API proposal.
What feature would you like to be added?
Add APIs to text/v2 for inspecting the layout of text rendered with one text.Face, separating visual geometry and caret positioning from advance measurement.
Related: #3805.
| Capability | Result |
|---|---|
| Enumerate layout runs | Logical byte ranges separated at resolved bidi run boundaries and line breaks, with directions and enough ordering information to identify visual placement |
| Query layout-run geometry | Positions and layout extents in a common coordinate system for the supplied text |
| Query caret positions | Visual insertion positions for a logical byte index, including explicitly identified alternatives at bidi boundaries |
“Layout run” means a directional part of a line, not a shaping cluster such as a ligature. Exact names, signatures, and result types remain to be designed. Exposing shaping internals is not a goal.
Geometry should describe layout extents, including whitespace advances, rather than visible glyph ink bounds. It should define coordinates for horizontal and vertical text and how empty lines are represented. Geometry and caret positions must agree with rendering.
Caret positioning should handle both endpoints consistently. For a purely RTL line with advance W, the logical beginning is at W and the logical end is at 0. Behavior for byte indices inside a rune, combining sequence, or ligature must be specified.
For example, using ASCII names to avoid the issue renderer reordering Hebrew characters:
Stored sequence: a b c ALEF BET GIMEL d e f
Displayed left to right: a b c GIMEL BET ALEF d e fA logical range covering c ALEF occupies disconnected visual regions. A single advance or a difference between caret coordinates does not describe their geometry.
Scope
- Each operation accepts one
text.Face, which may already be aMultiFacefor font fallback. - Guigui continues to own explicit font/style ranges, wrapping, tabs, placement of rich-text pieces, and editor behavior.
- These APIs provide useful single-face primitives; they do not promise correct paragraph-wide bidi composition merely by concatenating independently laid-out font ranges.
- Context-aware rich-text layout is outside this proposal. If needed later, it could belong in a separate package such as
text/v2/textlayout. - Existing
Measurealready provides whole-text layout dimensions. No additional whole-text bounds API is proposed. - Leave
AdvanceAtbehavior unchanged for compatibility. - Defer the advance-measurement API decision, including whether to introduce a new function. This proposal does not request undeprecating
Advance. - A new public layout object is not a requirement; the API shape should keep simple uses straightforward.
Why is this needed?
AdvanceAt combines measurement endpoint conventions with visual caret positioning. Its compatibility contract cannot serve as a consistent caret API, and subtracting its results cannot generally describe the geometry of mixed LTR/RTL text.
Guigui currently reconstructs parts of text geometry from advance and glyph information. Focused single-face APIs would let it reuse Ebitengine's layout calculations for local run geometry and caret positioning while retaining its rich-text layout system.
The aim is to provide useful primitives without requiring callers who only need simple text operations to adopt a context-aware paragraph layout system.
Authored by OpenAI Codex on behalf of @hajimehoshi.
Source: hajimehoshi/ebiten