CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-pdf--textkit

An advanced text layout framework for complex typography in PDF generation

Pending
Overview
Eval results
Files

engines.mddocs/

Text Processing Engines

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.

Capabilities

Bidirectional Text Engine

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);

Line Breaking Engine

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]);

Justification Engine

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);

Font Substitution Engine

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);

Script Itemization Engine

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);

Word Hyphenation Engine

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']

Text Decoration Engine

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);

Engine Configuration

Linebreaker Options

The linebreaker engine accepts several configuration options:

  • tolerance: Acceptable badness level for line breaks (default: 4)
  • hyphenationPenalty: Penalty for hyphenation points (default: 100 for justified, 600 for others)
  • hyphenationCallback: Custom hyphenation function

Justification Options

The justification engine supports factor-based spacing adjustments:

  • expandCharFactor: Character expansion parameters
  • shrinkCharFactor: Character shrinking parameters
  • expandWhitespaceFactor: Whitespace expansion parameters
  • shrinkWhitespaceFactor: Whitespace shrinking parameters
interface JustificationFactor {
  before: number;
  after: number;
  priority?: number;
  unconstrained?: boolean;
}

Install with Tessl CLI

npx tessl i tessl/npm-react-pdf--textkit

docs

attributed-string.md

decoration-script.md

engines.md

index.md

layout-engine.md

tile.json