An advanced text layout framework for complex typography in PDF generation
—
The layout engine is the central component that orchestrates the complete 13-step text layout process. It accepts an AttributedString and a Container object to layout text into, using various helper engines to perform specific layout tasks.
Creates a layout engine configured with the specified processing engines.
/**
* Creates a layout engine configured with the specified processing engines
* @param engines - Collection of text processing engines
* @returns Layout function that processes attributed strings
*/
function layoutEngine(engines: Engines): (
attributedString: AttributedString,
container: Container,
options?: LayoutOptions
) => AttributedString;Usage Example:
import layoutEngine, {
bidi,
linebreaker,
justification,
textDecoration,
scriptItemizer,
wordHyphenation,
fontSubstitution
} from "@react-pdf/textkit";
// Configure engines
const engines = {
bidi: bidi(),
linebreaker: linebreaker({ tolerance: 4 }),
justification: justification({}),
textDecoration: textDecoration(),
scriptItemizer: scriptItemizer(),
wordHyphenation: wordHyphenation(),
fontSubstitution: fontSubstitution()
};
// Create layout engine
const layout = layoutEngine(engines);
// Use the layout engine
const result = layout(attributedString, container, options);The layout engine processes text through a 13-step pipeline:
interface Engines {
bidi: typeof bidi;
linebreaker: typeof linebreaker;
justification: typeof justification;
textDecoration: typeof textDecoration;
scriptItemizer: typeof scriptItemizer;
wordHyphenation?: typeof wordHyphenation;
fontSubstitution: typeof fontSubstitution;
}
interface Container extends Rect {
truncateMode?: 'ellipsis';
maxLines?: number;
excludeRects?: Rect[];
}
interface LayoutOptions {
hyphenationCallback?: (word: string) => string[];
tolerance?: number;
hyphenationPenalty?: number;
expandCharFactor?: JustificationFactor;
shrinkCharFactor?: JustificationFactor;
expandWhitespaceFactor?: JustificationFactor;
shrinkWhitespaceFactor?: JustificationFactor;
}
interface JustificationFactor {
before: number;
after: number;
priority?: number;
unconstrained?: boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-react-pdf--textkit