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

layout-engine.mddocs/

Layout Engine

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.

Capabilities

Layout Engine Factory

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

Layout Processing Pipeline

The layout engine processes text through a 13-step pipeline:

  1. Split into paragraphs - Separate text into paragraph units
  2. Get bidi runs and paragraph direction - Handle bidirectional text
  3. Font substitution - Map to resolved font runs
  4. Script itemization - Group text by Unicode script
  5. Font shaping - Convert text to glyphs
  6. Line breaking - Determine optimal line breaks
  7. Bidi reordering - Reorder text for proper display
  8. Justification - Adjust spacing for text alignment
  9. Get rectangles - Calculate line and exclusion rectangles
  10. Perform line breaking - Apply break points to fragments
  11. Ellipsize - Truncate lines if necessary
  12. Bidi reordering - Final reordering pass
  13. Justification - Final justification pass

Types

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

docs

attributed-string.md

decoration-script.md

engines.md

index.md

layout-engine.md

tile.json