An advanced text layout framework for complex typography in PDF generation
—
Specialized engines that handle different aspects of text processing within the layout pipeline. Each engine is responsible for a specific transformation or analysis of the attributed string.
Handles bidirectional text reordering using the Unicode bidirectional algorithm. Analyzes text direction and creates runs with appropriate bidi levels.
/**
* Creates a bidirectional text processing engine
* @returns Function that processes attributed strings for bidi text
*/
function bidi(): (attributedString: AttributedString) => AttributedString;Usage Example:
import { bidi } from "@react-pdf/textkit";
const bidiEngine = bidi();
const processedString = bidiEngine(attributedString);Implements the Knuth & Plass line breaking algorithm with best-fit fallback. Determines optimal line breaks based on available widths and layout constraints.
/**
* Creates a line breaking engine using Knuth & Plass algorithm
* @param options - Layout configuration options
* @returns Function that breaks attributed strings into lines
*/
function linebreaker(options: LayoutOptions): (
attributedString: AttributedString,
availableWidths: number[]
) => AttributedString[];Usage Example:
import { linebreaker } from "@react-pdf/textkit";
const linebreakerEngine = linebreaker({
tolerance: 4,
hyphenationPenalty: 100
});
const lines = linebreakerEngine(attributedString, [200, 200, 200]);Performs text justification using Apple's justification algorithm. Adjusts character and word spacing to achieve proper text alignment.
/**
* Creates a text justification engine
* @param options - Layout configuration options
* @returns Function that justifies attributed string lines
*/
function justification(options: LayoutOptions): (line: AttributedString) => AttributedString;Usage Example:
import { justification } from "@react-pdf/textkit";
const justificationEngine = justification({
expandCharFactor: { priority: 0, unconditional: false },
shrinkCharFactor: { priority: 0, unconditional: false }
});
const justifiedLine = justificationEngine(line);Handles font fallback and substitution when fonts don't contain required glyphs. Selects appropriate fonts from font stacks based on Unicode code points.
/**
* Creates a font substitution engine
* @returns Function that performs font substitution on attributed strings
*/
function fontSubstitution(): (attributedString: AttributedString) => AttributedString;Usage Example:
import { fontSubstitution } from "@react-pdf/textkit";
const fontSubEngine = fontSubstitution();
const substitutedString = fontSubEngine(attributedString);Performs Unicode script itemization by analyzing text and grouping consecutive characters with the same script. Essential for proper text shaping and rendering.
/**
* Creates a script itemization engine
* @returns Function that itemizes attributed strings by Unicode script
*/
function scriptItemizer(): (attributedString: AttributedString) => AttributedString;Usage Example:
import { scriptItemizer } from "@react-pdf/textkit";
const scriptEngine = scriptItemizer();
const itemizedString = scriptEngine(attributedString);Provides word hyphenation functionality using hyphenation patterns. Splits words into syllables for line breaking purposes.
/**
* Creates a word hyphenation engine
* @returns Function that hyphenates words into syllable parts
*/
function wordHyphenation(): (word: string | null) => string[];Usage Example:
import { wordHyphenation } from "@react-pdf/textkit";
const hyphenationEngine = wordHyphenation();
const syllables = hyphenationEngine("hyphenation"); // ['hy', 'phen', 'ation']Generates decoration lines for text including underlines and strike-through effects. Calculates proper positioning and styling for decorations.
/**
* Creates a text decoration engine
* @returns Function that adds decoration lines to attributed strings
*/
function textDecoration(): (line: AttributedString) => AttributedString;Usage Example:
import { textDecoration } from "@react-pdf/textkit";
const decorationEngine = textDecoration();
const decoratedLine = decorationEngine(line);The linebreaker engine accepts several configuration options:
The justification engine supports factor-based spacing adjustments:
interface JustificationFactor {
before: number;
after: number;
priority?: number;
unconstrained?: boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-react-pdf--textkit