prepareWithSegments() is identical to prepare() but returns a richer handle that includes the segment text array. Use it when you need to render lines yourself — on canvas, SVG, WebGL, or server-side — via layoutWithLines(), walkLineRanges(), or layoutNextLine().
Signature
Parameters
The text string to measure. Whitespace is normalized according to the
whiteSpace option (defaults to CSS white-space: normal behavior).CSS font shorthand in the same format as
canvasContext.font. Must exactly match the CSS font declaration for the element being measured.Examples: "16px Inter", "bold 14px Arial", "italic 500 18px \"Helvetica Neue\"".Optional configuration object. Same options as
prepare().Returns
Extends
PreparedText with an additional segments field.When to use
UseprepareWithSegments() instead of prepare() when:
- You need the text content of each laid-out line (for canvas
fillText, SVG<text>, etc.) — use withlayoutWithLines(). - You need per-line geometry without materializing strings (for shrinkwrap or binary-search layouts) — use with
walkLineRanges(). - You need variable per-line widths (for text flowing around floated images) — use with
layoutNextLine().
prepare() + layout() instead. The opaque handle is cheaper to produce.
PreparedTextWithSegments is the unstable escape hatch for experiments and custom rendering. Its internal shape may change between releases.