DocsCore concepts

Core engine

Pass a positive width and a non-negative measurement function in matching units. Balanced wrapping minimizes line count first, then considers remaining space and language boundaries; greedy selects the furthest fitting boundary.

wrap

Pass a positive finite width and a measurement function returning finite, nonnegative values. Options include locale, strategy, breakLongWords and forcedBreaks.

ts
import { wrap } from '@oddmash/juliya/en';

const lines = wrap("Read every line naturally.", {
  width: 12,
  measure: text => [...text].length,
});
OptionRequiredDefaultPurpose
widthRequired-Positive finite line width
measureRequired-Measure text in matching units
measureRangeOptional-Measure a source range directly
localeOptionalkoLanguage rules; React also reads the nearest Provider
strategyOptionalbalancedBalanced or greedy layout
breakLongWordsOptionaltrueAllow grapheme-safe splits for overlong words
forcedBreaksOptional-Source UTF-16 boundary offsets

Width and measurement units

The example counts code points; CSS pixels require a canvas or DOM measurement in pixels with the same font and spacing.

ts
import { createCanvasMeasure } from '@oddmash/juliya/dom';
await document.fonts.ready;
const measure = createCanvasMeasure({ font: '400 24px sans-serif' });

Choose a strategy

Balanced minimizes line count, then considers boundary costs and remaining width. Greedy chooses the furthest fitting legal boundary on each line.

ts
const balanced = wrap(text, { width, measure, strategy: 'balanced' });
const greedy = wrap(text, { width, measure, strategy: 'greedy' });

Return value

A WrappedLine contains text, UTF-16 start/end offsets, measured width and source breakAfter. Join text and breakAfter to restore source exactly.

ts
const restored = lines.map(line => line.text + line.breakAfter).join('');

Empty strings and long words

Empty text returns one zero-width line. With breakLongWords false, a long word can overflow; a grapheme wider than available width can overflow in either mode.

Sentence segmentation

segmentSentences(text, locale) returns source-preserving segments with UTF-16 offsets. Use splitLines for explicit and forced breaks without measurement.

ts
import { segmentSentences } from '@oddmash/juliya';
const segments = segmentSentences(text, 'en');

Limitations

Language scoring is heuristic, not complete morphological analysis. Measurements should generally grow with appended text.