GraphReFlyTS

API Reference

computeCharPositions()

Compute per-grapheme boxes for already-broken lines.

Import

import { computeCharPositions } from "@graphrefly/ts/solutions/reactive-layout";

Signature

function computeCharPositions(
	lineBreaks: LineBreaksResult,
	segments: readonly PreparedSegment[],
	lineHeight: number,
	segmentAdapter?: SegmentAdapter,
): readonly CharPosition[]

Parameters

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.

Returns

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

Example

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);

Remarks

  • Already measured: Widths come from the prepared segments; this helper only maps the line cursors into per-grapheme rectangles.

Source

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