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.
import { wrap } from '@oddmash/juliya/en';
const lines = wrap("Read every line naturally.", {
width: 12,
measure: text => [...text].length,
});| Option | Required | Default | Purpose |
|---|---|---|---|
width | Required | - | Positive finite line width |
measure | Required | - | Measure text in matching units |
measureRange | Optional | - | Measure a source range directly |
locale | Optional | ko | Language rules; React also reads the nearest Provider |
strategy | Optional | balanced | Balanced or greedy layout |
breakLongWords | Optional | true | Allow grapheme-safe splits for overlong words |
forcedBreaks | Optional | - | 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.
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.
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.
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.
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.