layout() is the cheap hot path. Given a PreparedText handle from prepare(), a container width, and a line height, it walks the cached segment widths with pure arithmetic to count lines and return the total height. No DOM reads, no canvas calls, no string operations.
Signature
Parameters
The opaque handle returned by
prepare(). The same handle can be reused across any number of layout() calls at different widths — there is no need to re-prepare on resize.Maximum line width in pixels. Lines that exceed this width are wrapped. Corresponds to the element’s content-box width (excluding padding and border).
Line height in pixels. Must match the CSS
line-height for the element being measured. The returned height is lineCount * lineHeight.Returns
Number of wrapped lines. For example, a paragraph that breaks into three lines returns
3.Total block height in pixels. Equal to
lineCount * lineHeight.Performance
On the current benchmark snapshot,layout() takes approximately 0.09ms for a batch of 500 texts (~0.0002ms per text). It is safe to call on every resize event and inside virtual-scroll layout passes.
Usage in a resize handler
PreparedText is width-independent, the prepare() call happens once at load time and layout() is called on every resize without re-measuring.
Line-breaking rules
layout() matches CSS white-space: normal + overflow-wrap: break-word behavior:
- Break before any non-space segment that would overflow the line.
- Trailing whitespace hangs past the line edge and does not trigger a break.
- Segments wider than
maxWidthare broken at grapheme boundaries.