UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

712 lines (711 loc) 28.7 kB
/** * Helper that reads a specified number of bits using the arithmetic decoder, * maintaining a rolling context state for JBIG2 procedures. * * @private */ export declare class _PdfBitReader { private prev; /** * Reads `length` bits using the arithmetic decoder with the provided contexts, * updating the internal rolling context (`prev`) and returning the accumulated value. * * @private * @param {number} length The number of bits to read. * @param {any} decoder The arithmetic decoder supplying `_readBit(contexts, state)`. * @param {any} contexts The JBIG2 context states used by the decoder. * @returns {number} The unsigned integer composed from the read bits. */ _readBits(length: number, decoder: any, contexts: any): number; } /** * Caches and supplies JBIG2 context state arrays (lazily initialized), * keyed by a procedure or identifier. * * @private */ export declare class _PdfContextCache { private cache; getContexts(id: string | number): Int8Array; } /** * Holds the decoding window over the input data and lazily exposes * the arithmetic decoder and the shared JBIG2 context cache. * * @private */ export declare class _PdfDecodingContext { /** * Backing data buffer to decode from. * * @private */ _data: Uint8Array; /** * Start offset within the data buffer. * * @private */ _start: number; /** * End offset within the data buffer. * * @private */ _end: number; constructor(data: Uint8Array, start: number, end: number); readonly decoder: any; readonly contextCache: any; } /** * Segment visitor that decodes JBIG2 segments generic/text/halftone/symbol tables, * manages symbol/pattern/table caches, and composites bitmaps into the page buffer. * * @private */ export declare class _PdfSimpleSegmentVisitor { /** * Holds per-page decoding metadata and parameters. * * @private */ _currentPageInfo: any; /** * Destination pixel buffer used during composition. * * @private */ _buffer: Uint8ClampedArray; /** * Custom decoding tables supplied by the stream or caller. * * @private */ _customTables: any; /** * Symbol dictionary used for JBIG2 text/region decoding. * * @private */ _symbols: any; /** * Pattern dictionary used for halftone/region reconstruction. * * @private */ _patterns: any; /** * Cache of standard Huffman tables keyed by table id. * * @private */ _standardTablesCache: { [key: number]: _PdfHuffmanTable; }; /** * Neighborhood coding templates for generic regions. * * @private */ _codingTemplates: { x: number; y: number; }[][]; /** * Refinement region coding and reference templates. * * @private */ _refinementTemplates: { coding: { x: number; y: number; }[]; reference: { x: number; y: number; }[]; }[]; /** * Context states reused for generic region decoding. * * @private */ _reusedContexts: number[]; /** * Context states reused for refinement region decoding. * * @private */ _refinementReusedContexts: number[]; /** * Initializes page-level state from the `PageInformation` segment and allocates * the destination bit buffer (row-major, bit-packed). * * @private * @param {any} info The page information array (first entry contains width/height and flags). * @returns {void} */ _onPageInformation(info: any): void; /** * Composites a decoded bitmap into the page buffer using the specified * combination operator at the region position. * * @private * @param {any} regionInfo The region info with `x`, `y`, `width`, `height`. * @param {any} bitmap The 2D bitmap array (rows of 0/1) to draw. * @returns {void} */ _drawBitmap(regionInfo: any, bitmap: any): void; /** * Decodes and draws an Immediate Generic Region segment. * * @private * @param {any} region The parsed region descriptor. * @param {Uint8Array} data The segment data buffer. * @param {number} start The start offset within `data`. * @param {number} end The end offset within `data`. * @returns {void} */ _onImmediateGenericRegion(region: any, data: Uint8Array, start: number, end: number): void; /** * Resolves a custom Huffman table by index among the referred segments/custom tables. * * @private * @param {number} index The zero-based custom table index to retrieve. * @param {number[]} referredTo The list of referred segment numbers. * @param {any} customTables The dictionary of custom tables by segment id. * @returns {any} The matching custom Huffman table. */ _getCustomHuffmanTable(index: number, referredTo: number[], customTables: any): any; /** * Builds all Huffman tables required for a Huffman-coded text region: * symbol ID table and delta tables (S/DS/DT). * * @private * @param {any} textRegion The text region parameters. * @param {any} referredTo Referred segment ids. * @param {any} customTables Custom Huffman tables dictionary. * @param {number} numberOfSymbols Total symbols available for coding. * @param {_PdfReader} reader The bit reader used to decode table definitions. * @returns {any} An object containing `symbolIDTable`, `tableFirstS`, `tableDeltaS`, `tableDeltaT`. */ _getTextRegionHuffmanTables(textRegion: any, //eslint-disable-line referredTo: any, //eslint-disable-line customTables: any, //eslint-disable-line numberOfSymbols: number, reader: _PdfReader): any; /** * Returns a predefined (standard) Huffman table by its B.n number, caching it for reuse. * * @private * @param {number} number The standard table identifier (B.1..B.15 etc.). * @returns {_PdfHuffmanTable} The standard Huffman table instance. */ _getStandardTable(number: number): _PdfHuffmanTable; /** * Prepares Huffman tables for symbol dictionary decoding: delta height/width, bitmap size, * and aggregate instances, resolving from standard or custom tables. * * @private * @param {{huffmanDHSelector:number, huffmanDWSelector:number, bitmapSizeSelector:boolean, aggregationInstancesSelector:boolean}} dictionary The dictionary flags/selectors. * @param {any} referredTo Referred segment ids. * @param {any} customTables Custom Huffman tables dictionary. * @returns {{tableDeltaHeight:number, tableDeltaWidth:number, tableBitmapSize:number, tableAggregateInstances:any}} The required Huffman tables. */ _getSymbolDictionaryHuffmanTables(dictionary: { huffmanDHSelector: number; huffmanDWSelector: number; bitmapSizeSelector: boolean; aggregationInstancesSelector: boolean; }, referredTo: any, customTables: any): { tableDeltaHeight: number; tableDeltaWidth: number; tableBitmapSize: number; tableAggregateInstances: any; }; /** * Reads a raw (uncompressed) bitmap from the bit stream into a 2D array. * * @private * @param {any} reader The bit reader providing `_readBit()` and `byteAlign()`. * @param {number} width The bitmap width in pixels. * @param {number} height The bitmap height in pixels. * @returns {Uint8Array[]} The decoded bitmap (rows of 0/1 values). */ _readUncompressedBitmap(reader: any, width: number, height: number): Uint8Array[]; /** * Decodes an MMR (fax) compressed bitmap into a 2D array of bits. * * @private * @param {any} input The input providing `readNextChar()` bytes. * @param {number} width The bitmap width in pixels. * @param {number} height The bitmap height in pixels. * @param {boolean} endOfBlock Whether to consume an end-of-block marker. * @returns {Uint8Array[]} The decoded bitmap (rows of 0/1 values). */ _decodeMmrBitmap(input: any, width: number, height: number, endOfBlock: boolean): Uint8Array[]; /** * Decodes a Symbol Dictionary segment and stores the resulting symbol bitmaps * keyed by the current segment id. * * @private * @param {any} dictionary The symbol dictionary parameters. * @param {any} currentSegment The current segment number. * @param {any} referredSegments The list of referred segment ids. * @param {Uint8Array} data The data buffer. * @param {number} start Start offset. * @param {number} end End offset. * @returns {void} */ _onSymbolDictionary(dictionary: any, currentSegment: any, referredSegments: any, // eslint-disable-line data: Uint8Array, start: number, end: number): void; /** * Decodes and draws an Immediate Text Region (optionally Huffman-coded), * composing the decoded symbols into the page bitmap. * * @private * @param {any} region The text region parameters. * @param {string[]} referredSegments Referred segment ids (as strings). * @param {Uint8Array} data The data buffer. * @param {number} start Start offset. * @param {number} end End offset. * @returns {void} */ _onImmediateTextRegion(region: any, referredSegments: string[], data: Uint8Array, start: number, end: number): void; /** * Decodes a Pattern Dictionary segment and stores patterns by segment id. * * @private * @param {any} dictionary The pattern dictionary parameters. * @param {string} currentSegment The current segment number. * @param {Uint8Array} data The data buffer. * @param {number} start Start offset. * @param {number} end End offset. * @returns {void} */ _onPatternDictionary(dictionary: any, currentSegment: string, data: Uint8Array, start: number, end: number): void; /** * Decodes and draws an Immediate Halftone Region using previously decoded patterns. * * @private * @param {any} region The halftone region parameters. * @param {string[]} referredSegments Ids referencing the pattern dictionary. * @param {Uint8Array} data The data buffer. * @param {number} start Start offset. * @param {number} end End offset. * @returns {void} */ _onImmediateHalftoneRegion(region: any, referredSegments: string[], data: Uint8Array, start: number, end: number): void; /** * Decodes a Tables segment and caches the resulting custom Huffman tables under the segment id. * * @private * @param {string} currentSegment The current segment number. * @param {Uint8Array} data The data buffer. * @param {number} start Start offset in `data`. * @param {number} end End offset in `data`. * @returns {void} */ _onTables(currentSegment: string, data: Uint8Array, start: number, end: number): void; /** * Decodes a custom Huffman table from a Tables segment payload. * * @private * @param {Uint8Array} data The data buffer. * @param {number} start Start offset in `data`. * @param {number} end End offset in `data`. * @returns {_PdfHuffmanTable} The constructed custom Huffman table. */ _decodeTablesSegment(data: Uint8Array, start: number, end: number): _PdfHuffmanTable; /** * Fast-path decoding for Generic Region template 0 with default AT positions, * producing a bitmap using the specified arithmetic decoder contexts. * * @private * @param {number} width Bitmap width in pixels. * @param {number} height Bitmap height in pixels. * @param {_PdfDecodingContext} decodingContext The decoding context providing decoder and contexts. * @returns {Uint8Array[]} The decoded bitmap. */ _decodeBitmapTemplate0(width: number, height: number, decodingContext: _PdfDecodingContext): Uint8Array[]; /** * Decodes a Generic Region bitmap (MMR or arithmetic-coded) with the specified template, * prediction mode, optional skip mask, and AT positions. * * @private * @param {boolean} mmr Whether the bitmap is MMR-compressed. * @param {number} width Bitmap width. * @param {number} height Bitmap height. * @param {number} templateIndex JBIG2 template index (0..3). * @param {boolean} prediction When true, uses LTP prediction. * @param {boolean[][]} skip Optional skip mask (true = skip decoding at pixel). * @param {{x:number, y:number}[]} at Additional relative AT positions. * @param {_PdfDecodingContext} decodingContext The decoding context. * @returns {Uint8Array[]} The decoded bitmap. */ _decodeBitmap(mmr: boolean, width: number, height: number, templateIndex: number, prediction: boolean, skip: boolean[][], at: { x: number; y: number; }[], decodingContext: _PdfDecodingContext): Uint8Array[]; /** * Decodes a Refinement Region bitmap using coding and reference templates applied * against a reference bitmap with given offsets. * * @private * @param {number} width Output width. * @param {number} height Output height. * @param {number} templateIndex Refinement template index (0/1). * @param {any} referenceBitmap The reference bitmap (2D 0/1). * @param {number} offsetX X offset into the reference. * @param {number} offsetY Y offset into the reference. * @param {any} prediction Prediction flag (unused/unsupported when true). * @param {any} at Additional AT positions. * @param {_PdfDecodingContext} decodingContext The decoding context. * @returns {Uint8Array[]} The decoded refinement bitmap. */ _decodeRefinement(width: number, height: number, templateIndex: number, referenceBitmap: any, // eslint-disable-line offsetX: number, offsetY: number, prediction: any, at: any, decodingContext: _PdfDecodingContext): Uint8Array[]; /** * Decodes a Symbol Dictionary, producing and (optionally) exporting symbol bitmaps * using either arithmetic or Huffman coding with optional refinement. * * @private * @param {any} huffman Whether Huffman coding is used. * @param {any} refinement Whether refinement coding is used. * @param {any} symbols Existing input symbol bitmaps. * @param {number} numberOfNewSymbols Number of new symbols to decode. * @param {number} numberOfExportedSymbols Number of symbols to export. * @param {any} huffmanTables Huffman tables bundle when `huffman` is true. * @param {number} templateIndex Generic Region template for symbol bitmaps. * @param {{x:number,y:number}[]} at AT positions for generic template. * @param {number} refinementTemplateIndex Refinement template index. * @param {any} refinementAt AT positions for refinement. * @param {_PdfDecodingContext} decodingContext The decoding context. * @param {any} huffmanInput The Huffman bit reader when `huffman` is true. * @returns {any[]} The exported symbol bitmaps. */ _decodeSymbolDictionary(huffman: any, refinement: any, symbols: any, numberOfNewSymbols: number, // eslint-disable-line numberOfExportedSymbols: number, huffmanTables: any, templateIndex: number, // eslint-disable-line at: { x: number; y: number; }[], refinementTemplateIndex: number, refinementAt: any, // eslint-disable-line decodingContext: _PdfDecodingContext, huffmanInput: any): any[]; /** * Decodes a signed integer using the specified JBIG2 integer procedure contexts. * * @private * @param {any} contextCache The context cache providing states by procedure name. * @param {any} procedure The procedure id/name (e.g., 'IADW', 'IADT', 'IAID'). * @param {any} decoder The arithmetic decoder. * @returns {number} The decoded signed integer (32-bit range). */ _decodeInteger(contextCache: any, procedure: any, decoder: any): number; /** * Decodes an unsigned integer of the given bit-length using the IAID procedure. * * @private * @param {any} contextCache The context cache. * @param {any} decoder The arithmetic decoder. * @param {any} codeLength The number of bits to decode. * @returns {number} The decoded unsigned value. */ _decodeImageData(contextCache: any, decoder: any, codeLength: any): number; /** * Decodes a Text Region by placing symbol bitmaps (with optional refinement) * onto a target bitmap using the specified combination operator. * * @private * @param {any} huffman Whether Huffman coding is used. * @param {any} refinement Whether refinement coding is used. * @param {number} width Region width. * @param {number} height Region height. * @param {any} defaultPixelValue Initial fill value for the region rows (0/1). * @param {any} numberOfSymbolInstances Number of symbol placements. * @param {any} stripSize The strip height. * @param {any} inputSymbols The list of available symbol bitmaps. * @param {any} symbolCodeLength Bit-length for symbol ids (IAID). * @param {any} transposed Whether symbols are laid out transposed. * @param {any} dsOffset Delta-S offset. * @param {any} referenceCorner Reference corner selector. * @param {any} combinationOperator Composition operator (e.g., 0 = OR, 2 = XOR). * @param {any} huffmanTables Huffman tables bundle when `huffman` is true. * @param {any} refinementTemplateIndex Refinement template index. * @param {any} refinementAt AT positions for refinement. * @param {any} decodingContext The decoding context. * @param {any} logStripSize Log2 of strip size (when Huffman-coded). * @param {any} huffmanInput The Huffman input reader when `huffman` is true. * @returns {any} The decoded text region bitmap (2D array). */ _decodeTextRegion(huffman: any, refinement: any, width: number, height: number, defaultPixelValue: any, // eslint-disable-line numberOfSymbolInstances: any, stripSize: any, inputSymbols: any, symbolCodeLength: any, // eslint-disable-line transposed: any, dsOffset: any, referenceCorner: any, combinationOperator: any, huffmanTables: any, // eslint-disable-line refinementTemplateIndex: any, refinementAt: any, decodingContext: any, logStripSize: any, // eslint-disable-line huffmanInput: any): any; /** * Decodes a Pattern Dictionary into an array of pattern bitmaps by slicing a collective bitmap. * * @private * @param {any} mmr Whether MMR compression is used. * @param {number} patternWidth Pattern tile width. * @param {number} patternHeight Pattern tile height. * @param {any} maxPatternIndex Maximum pattern index (inclusive). * @param {any} template Generic Region template index. * @param {any} decodingContext The decoding context. * @returns {any} An array of pattern bitmaps (2D arrays). */ _decodePatternDictionary(mmr: any, patternWidth: number, patternHeight: number, maxPatternIndex: any, // eslint-disable-line template: any, decodingContext: any): any; /** * Decodes a Halftone Region by assembling patterns based on gray-scale bit planes * and placing them onto the target bitmap using the specified grid. * * @private * @param {any} mmr Whether bit planes are MMR-compressed. * @param {any} patterns The pattern dictionary (array of bitmaps). * @param {any} template Generic Region template index for bit planes. * @param {any} regionWidth Region width. * @param {any} regionHeight Region height. * @param {any} defaultPixelValue Initial row fill value (0/1). * @param {any} enableSkip Whether skip is enabled (unsupported here). * @param {any} combinationOperator Composition operator (0 = OR required). * @param {number} gridWidth Grid width in cells. * @param {number} gridHeight Grid height in cells. * @param {number} gridOffsetX Fixed-point X offset (8.8). * @param {number} gridOffsetY Fixed-point Y offset (8.8). * @param {number} gridVectorX Fixed-point X vector (8.8). * @param {any} gridVectorY Fixed-point Y vector (8.8). * @param {any} decodingContext The decoding context. * @returns {any} The decoded halftone region bitmap (2D array). */ _decodeHalftoneRegion(mmr: any, patterns: any, template: any, regionWidth: any, regionHeight: any, // eslint-disable-line defaultPixelValue: any, enableSkip: any, combinationOperator: any, gridWidth: number, // eslint-disable-line gridHeight: number, gridOffsetX: number, gridOffsetY: number, gridVectorX: number, gridVectorY: any, decodingContext: any): any; } /** * Represents a single Huffman table entry, including range bounds, * prefix length/code, and flags. * * @private */ export declare class _PdfHuffmanLine { isoob: boolean; rangeLow: number; prefixLength: number; rangeLength: number; prefixCode: number; isLowerRange: boolean; constructor(lineData: any); } /** * Node in a Huffman decoding tree that can be extended from prefix codes * and used to decode values from a bit reader. * * @private */ export declare class _PdfHuffmanTreeNode { children: _PdfHuffmanTreeNode[]; isLeaf: boolean; rangeLength: number; rangeLow: number; isLowerRange: boolean; isoob: boolean; constructor(line: _PdfHuffmanLine); /** * Inserts a Huffman line into the decoding tree according to its prefix code. * * @private * @param {_PdfHuffmanLine} line The Huffman line defining a code range or OOB. * @param {number} shift The remaining bit shift (prefixLength - 1 .. 0). * @returns {void} */ _buildTree(line: _PdfHuffmanLine, shift: number): void; /** * Decodes a value by traversing the tree using bits from the reader, * and applies the range adjustment when needed. * * @private * @param {any} reader The reader exposing `_readBit()` / `_readBits(n)`. * @returns {number | null} The decoded value, or `null` if OOB was reached. */ _decodeNode(reader: any): number | null; } /** * Canonical Huffman table that assigns prefix codes to lines, builds a decode tree, * and provides value decoding against a bit reader. * * @private */ export declare class _PdfHuffmanTable { rootNode: _PdfHuffmanTreeNode; constructor(lines: _PdfHuffmanLine[], prefixCodesDone: boolean); decode(reader: any): number | null; assignPrefixCodes(lines: _PdfHuffmanLine[]): void; } /** * MSB first bit reader over a byte array for Huffman/bit level parsing, * with byte alignment support. * * @private */ export declare class _PdfReader { data: Uint8Array; start: number; end: number; position: number; shift: number; currentByte: number; constructor(data: Uint8Array, start: number, end: number); /** * Reads a single bit from the underlying byte stream, refilling as needed. * * @private * @returns {number} The next bit (0 or 1). * @throws {Error} If the end of input is reached prematurely. */ _readBit(): number; /** * Reads `numBits` bits MSB-first and returns the aggregated value. * * @private * @param {number} numBits The number of bits to read. * @returns {number} The unsigned integer composed from the read bits. */ _readBits(numBits: number): number; byteAlign(): void; next(): number; } /** * JBIG2 image parser that reads headers and segments, dispatches work to the visitor, * and produces unpacked image data. * * @private */ export declare class _PdfJbig2Image { width: number; height: number; /** * Field length of the region segment information. * * @private */ _regionSegmentInformationFieldLength: number; /** * Lookup of segment type names by id. * * @private */ _segmentTypes: (string | null)[]; /** * Parses a sequence of JBIG2 chunks and returns the bit-packed page buffer. * * @private * @param {any} chunks The array of chunk objects containing `data`, `start`, and `end`. * @returns {any} The bit-packed image buffer of the last processed page. */ _parseChunks(chunks: any): any; /** * Parses a complete JBIG2 stream and returns the unpacked 8-bit grayscale image data. * * @private * @param {any} data The input JBIG2 byte array. * @returns {any} The unpacked image data buffer. */ _parse(data: any): any; /** * Processes a list of segments by dispatching each to the segment visitor. * * @private * @param {any} segments The parsed JBIG2 segments. * @param {_PdfSimpleSegmentVisitor} visitor The visitor handling segment types. * @returns {any} as process segment. */ _processSegments(segments: any, visitor: _PdfSimpleSegmentVisitor): any; /** * Parses an entire JBIG2 file/stream (with header), processes segments, and * converts the bit-packed page buffer to 8-bit grayscale image data. * * @private * @param {Uint8Array} data The full JBIG2 data. * @returns {{imgData: Uint8ClampedArray, width: number, height: number}} Decoded image buffer and dimensions. */ _parseJbig2(data: Uint8Array): { imgData: Uint8ClampedArray; width: number; height: number; }; /** * Parses and processes JBIG2 segments from chunked inputs (e.g., inline images), * returning the bit-packed page buffer. * * @private * @param {any} chunks The chunk array with `data`, `start`, and `end`. * @returns {any} The bit-packed image buffer. */ _parseJbig2Chunks(chunks: any): any; /** * Reads a segment header starting at `start`, validating type, extracting flags, * referred-to segments, page association, and length. * * @private * @param {Uint8Array} data The data buffer. * @param {number} start The start offset of the header. * @returns {{number:number, type:number, typeName:string|null, deferredNonRetain:boolean, retainBits:number[], pageAssociation:number, length:number, referredTo:number[], headerEnd:number}} The parsed header structure. */ _readSegmentHeader(data: Uint8Array, start: number): { number: number; type: number; typeName: string | null; deferredNonRetain: boolean; retainBits: number[]; pageAssociation: number; length: number; referredTo: number[]; headerEnd: number; }; /** * Iterates the stream to collect segments until EOF or EndOfFile segment. * * @private * @param {any} header The file header (randomAccess/numberOfPages). * @param {any} data The data buffer. * @param {number} start Start offset. * @param {number} end End offset. * @returns {any} The list of segments with parsed headers and data ranges. */ _readSegments(header: any, data: any, start: number, end: number): any; /** * Dispatches a single segment to the appropriate visitor callback based on its type, * parsing type-specific payload arguments beforehand. * * @private * @param {{header:{type:number, number:number, referredTo:number}, data:Uint8Array, start:number, end:number}} segment The segment to process. // eslint-disable-line * @param {any} visitor The segment visitor instance. * @returns {void} */ _processSegment(segment: { header: { type: number; number: number; referredTo: number; }; data: Uint8Array; start: number; end: number; }, visitor: any): void; /** * Reads a `RegionSegmentInformation` structure (width, height, position, operator). * * @private * @param {Uint8Array} data The data buffer. * @param {number} start Start offset of the structure. * @returns {{width:number, height:number, x:number, y:number, combinationOperator:number}} The parsed region information. */ _readRegionSegmentInformation(data: Uint8Array, start: number): { width: number; height: number; x: number; y: number; combinationOperator: number; }; }