An advanced text layout framework for complex typography in PDF generation
—
Utilities for text decoration (underlines, strike-through) and Unicode script processing for proper text rendering across different writing systems.
Generates decoration lines for text including underlines and strike-through effects. Calculates proper positioning, thickness, and styling for decorations based on font metrics.
/**
* Creates a text decoration engine for generating underlines and strike-through
* @returns Function that adds decoration lines to attributed string lines
*/
function textDecoration(): (line: AttributedString) => AttributedString;Usage Example:
import { textDecoration } from "@react-pdf/textkit";
// Create decoration engine
const decorationEngine = textDecoration();
// Apply decorations to a line with underline and strike attributes
const decoratedLine = decorationEngine(line);
// The engine will add decorationLines to the AttributedString
console.log(decoratedLine.decorationLines);The text decoration engine processes runs with the following attributes:
underline: Creates underline decorationunderlineColor: Sets underline color (default: "black")underlineStyle: Sets underline style (default: "solid")strike: Creates strike-through decorationstrikeColor: Sets strike-through color (default: "black")strikeStyle: Sets strike-through style (default: "solid")Performs Unicode script itemization by analyzing text and grouping consecutive characters with the same script. This is essential for proper text shaping and rendering, especially for complex scripts and mixed-language text.
/**
* Creates a script itemization engine for Unicode script analysis
* @returns Function that groups attributed string characters by Unicode script
*/
function scriptItemizer(): (attributedString: AttributedString) => AttributedString;Usage Example:
import { scriptItemizer } from "@react-pdf/textkit";
// Create script itemizer
const scriptEngine = scriptItemizer();
// Process mixed-script text
const mixedText = fromFragments([
{ string: "Hello مرحبا" } // Latin + Arabic
]);
const itemizedString = scriptEngine(mixedText);
// Results in runs grouped by script:
// Run 1: "Hello " (script: "Latin")
// Run 2: "مرحبا" (script: "Arabic")The script itemizer:
script attribute set to the detected script nameProvides word hyphenation functionality using the hyphen library with English (US) patterns. Splits words into syllable parts for line breaking purposes.
/**
* Creates a word hyphenation engine using English hyphenation patterns
* @returns Function that splits words into hyphenation syllables
*/
function wordHyphenation(): (word: string | null) => string[];Usage Example:
import { wordHyphenation } from "@react-pdf/textkit";
// Create hyphenation engine
const hyphenationEngine = wordHyphenation();
// Hyphenate words
const syllables1 = hyphenationEngine("hyphenation");
// Result: ["hy", "phen", "ation"]
const syllables2 = hyphenationEngine("beautiful");
// Result: ["beau", "ti", "ful"]
const syllables3 = hyphenationEngine("cat");
// Result: ["cat"] (single syllable)
// Handles null input
const syllables4 = hyphenationEngine(null);
// Result: []The hyphenation engine:
\u00ad) in input wordsinterface DecorationLine {
rect: Rect;
opacity: number;
color: string;
style: string;
}
interface Rect {
x: number;
y: number;
width: number;
height: number;
}The text decoration engine calculates decoration properties as follows:
ascent(line) + thickness * 2Math.max(0.5, Math.floor(fontSize / 12))ascent(line) - runAscent(run) / 3underlineColor/strikeColor attributes or defaults to "black"underlineStyle/strikeStyle attributes or defaults to "solid"opacity attribute from runThe script itemizer recognizes all Unicode scripts and creates appropriate groupings for:
Scripts marked as "Common", "Inherited", or "Unknown" are ignored during itemization, allowing surrounding script context to determine grouping.
Install with Tessl CLI
npx tessl i tessl/npm-react-pdf--textkit