/** * Structured Text Extraction * * Provides layout-aware text extraction from PDF pages. * Organizes text into a hierarchy: Page → Block → Line → Char */ import type { Page } from './document.js'; import { Rect } from './geometry.js'; /** * Quad - four-corner bounding box for rotated text */ export interface Quad { /** Upper-left corner */ ul: { x: number; y: number; }; /** Upper-right corner */ ur: { x: number; y: number; }; /** Lower-left corner */ ll: { x: number; y: number; }; /** Lower-right corner */ lr: { x: number; y: number; }; } /** * Text block type */ export declare enum STextBlockType { /** Regular text block */ Text = 0, /** Image block */ Image = 1, /** List item */ List = 2, /** Table cell */ Table = 3 } /** * Writing mode for text lines */ export declare enum WritingMode { /** Horizontal left-to-right */ HorizontalLtr = 0, /** Horizontal right-to-left */ HorizontalRtl = 1, /** Vertical top-to-bottom */ VerticalTtb = 2, /** Vertical bottom-to-top */ VerticalBtt = 3 } /** * Structured text character */ export interface STextCharData { /** Unicode character */ c: string; /** Character quad (4 corners) */ quad: Quad; /** Font size */ size: number; /** Font name */ fontName: string; } /** * Structured text line */ export interface STextLineData { /** Writing mode */ wmode: WritingMode; /** Bounding box */ bbox: Rect; /** Baseline coordinate */ baseline: number; /** Text direction */ dir: { x: number; y: number; }; /** Characters in this line */ chars: STextCharData[]; } /** * Structured text block */ export interface STextBlockData { /** Block type */ blockType: STextBlockType; /** Bounding box */ bbox: Rect; /** Lines in this block */ lines: STextLineData[]; } /** * Structured Text Page * * Represents extracted text from a PDF page with layout information. * Provides hierarchical access to blocks, lines, and characters. * * @example * ```typescript * const doc = Document.open('document.pdf'); * const page = doc.loadPage(0); * const stext = STextPage.fromPage(page); * * // Get all text * console.log(stext.getText()); * * // Search for text * const hits = stext.search('keyword'); * console.log(`Found ${hits.length} matches`); * * stext.drop(); * page.drop(); * doc.close(); * ``` */ export declare class STextPage { /** * The raw context reference, stored exactly as the Page provides it * so we can convert to bigint for stext-specific native calls. */ private _ctx; /** * The stext handle returned by the native addon (bigint). */ private _stextHandle; /** * Cached page bounds from the source Page, used as a fallback when * the native getSTextPageBounds symbol is unavailable. */ private _pageBounds; /** * Reference to the original page for fallback text extraction. */ private _page; private _dropped; private constructor(); /** Convert a NativeContext to the bigint form required by stext FFI calls. */ private _ctxBigInt; /** * Create a structured text page from a document page * * @param page - The page to extract text from * @returns A new STextPage instance * * @example * ```typescript * const stext = STextPage.fromPage(page); * ``` */ static fromPage(page: Page): STextPage; /** * Get all text as a single string * * @returns Plain text content * * @example * ```typescript * const text = stext.getText(); * console.log(text); * ``` */ getText(): string; /** * Search for text in the page * * @param needle - The text to search for * @param maxHits - Maximum number of hits to return (default: 500) * @returns Array of quads (bounding boxes) for matches * * @example * ```typescript * const hits = stext.search('important'); * for (const hit of hits) { * console.log('Found at:', hit); * } * ``` */ search(needle: string, maxHits?: number): Quad[]; /** * Get the page bounds * * @returns Rectangle representing the page bounds * * @example * ```typescript * const bounds = stext.getBounds(); * console.log(`Page size: ${bounds.width} x ${bounds.height}`); * ``` */ getBounds(): Rect; /** * Drop (free) the structured text page * * Must be called when done to free resources. * * @example * ```typescript * const stext = STextPage.fromPage(page); * // ... use stext ... * stext.drop(); * ``` */ drop(): void; /** * Check if the structured text page has been dropped * * @returns true if dropped, false otherwise */ isDropped(): boolean; /** * Get blocks from the page * * Returns the hierarchical structure of text blocks, lines, and characters. * Note: This requires full FFI implementation. For now, returns simplified structure. * * @returns Array of text blocks * * @example * ```typescript * const blocks = stext.getBlocks(); * for (const block of blocks) { * console.log(`Block type: ${block.blockType}`); * for (const line of block.lines) { * console.log(` Line: ${line.chars.map(c => c.c).join('')}`); * } * } * ``` */ getBlocks(): STextBlockData[]; /** * Get the number of blocks on the page * * @returns Number of blocks * * @example * ```typescript * const count = stext.blockCount(); * console.log(`Page has ${count} blocks`); * ``` */ blockCount(): number; /** * Get the number of characters on the page * * @returns Total character count * * @example * ```typescript * const count = stext.charCount(); * console.log(`Page has ${count} characters`); * ``` */ charCount(): number; /** * Get blocks of a specific type * * @param blockType - The block type to filter by * @returns Array of blocks matching the type * * @example * ```typescript * const textBlocks = stext.getBlocksOfType(STextBlockType.Text); * const imageBlocks = stext.getBlocksOfType(STextBlockType.Image); * ``` */ getBlocksOfType(blockType: STextBlockType): STextBlockData[]; /** * Get the native handle (for advanced use) * * @internal */ get handle(): bigint; private _checkDropped; } /** * Convert a quad to a rectangle (axis-aligned bounding box) * * @param quad - The quad to convert * @returns Rectangle enclosing the quad * * @example * ```typescript * const hits = stext.search('text'); * const rect = quadToRect(hits[0]); * ``` */ export declare function quadToRect(quad: Quad): Rect; /** * Check if two quads overlap * * @param q1 - First quad * @param q2 - Second quad * @returns true if quads overlap, false otherwise */ export declare function quadsOverlap(q1: Quad, q2: Quad): boolean; //# sourceMappingURL=stext.d.ts.map