Skip to content
PACKAGES

computeCharPositions()

Compute per-grapheme boxes for already-broken lines.

import { computeCharPositions } from "@graphrefly/ts/solutions/reactive-layout";
function computeCharPositions(
lineBreaks: LineBreaksResult,
segments: readonly PreparedSegment[],
lineHeight: number,
segmentAdapter?: SegmentAdapter,
): readonly CharPosition[]
ParameterTypeDescription
lineBreaksLineBreaksResultLine break result whose cursors identify the visible grapheme ranges.
segmentsreadonly PreparedSegment[]Prepared segments used to produce the line breaks.
lineHeightnumberHeight assigned to every line box.
segmentAdapterSegmentAdapterOptional segmentation adapter; defaults to the built-in adapter.

Character-position boxes in visual order, each with x, y, width, height, and zero-based line index.

import {
analyzeAndMeasure,
computeCharPositions,
computeLineBreaks,
} from "@graphrefly/ts/solutions/reactive-layout";
const segments = analyzeAndMeasure(
"Hello world",
"16px system-ui",
{ measureSegment: (segment) => ({ width: segment.length * 8 }) },
new Map(),
);
const lineBreaks = computeLineBreaks(segments, 64);
const positions = computeCharPositions(lineBreaks, segments, 20);
  • Already measured: Widths come from the prepared segments; this helper only maps the line cursors into per-grapheme rectangles.

packages/ts/src/solutions/reactive-layout/line.ts