UNPKG

@syncfusion/ej2-pdf

Version:

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

980 lines (979 loc) 29.6 kB
import { _TrueTypeTableInfo, _TrueTypeHorizontalHeaderTable, _TrueTypeNameTable, _TrueTypeHeadTable, _TrueTypeOS2Table, _TrueTypePostTable, _TrueTypeCmapSubTable } from './ttf-table'; import { Dictionary } from '../pdf-primitives'; import { _TrueTypeCmapEncoding } from '../../core/enumerator'; /** * Reads and parses truetype and opentype font data into accessible tables and metrics. * * @private */ export declare class _TrueTypeReader { /** * Raw TrueType font data. * * @private */ _fontData: Uint8Array; /** * Size (in bytes) of a 32-bit integer. * * @private */ readonly _int32Size: number; /** * Current offset within the font data. * * @private */ _offset: number; /** * Table directory mapping table names to offsets and sizes. * * @private */ _tableDirectory: Dictionary<string, _TrueTypeTableInfo>; /** * Indicates whether the provided data represents a TrueType font. * * @private */ _isFont: boolean; /** * Indicates whether the font uses Macintosh-specific structures. * * @private */ _isMacTtf: boolean; /** * Lowest encountered table position used for validation. * * @private */ _lowestPosition: number; /** * Extracted TrueType metrics. * * @private */ _metrics: _TrueTypeMetrics; /** * Maximum glyph index used for Macintosh cmap. * * @private */ _maxMacIndex: number; /** * Indicates whether the font resource is present. * * @private */ _isFontPresent: boolean; /** * Indicates whether the font is a Macintosh font. * * @private */ _isMacFont: boolean; /** * Tracks the lowest code point missing from the glyph set. * * @private */ _missedGlyphs: number; /** * List of required TrueType table names. * * @private */ _tableNames: string[]; /** * Entry selector values used to build the search tree. * * @private */ _entrySelectors: number[]; /** * Glyph advance width array indexed by glyph ID. * * @private */ _width: number[]; /** * Indicates whether the 'loca' table uses short format. * * @private */ _bIsLocaShort: boolean; /** * Dictionary of glyphs for Macintosh encoding. * * @private */ _macintoshDictionary: Dictionary<number, _TrueTypeGlyph>; /** * Dictionary of glyphs for Microsoft encoding. * * @private */ _microsoftDictionary: Dictionary<number, _TrueTypeGlyph>; /** * Internal cache of Macintosh glyphs. * * @private */ _internalMacintoshGlyphs: Dictionary<number, _TrueTypeGlyph>; /** * Internal cache of Microsoft glyphs. * * @private */ _internalMicrosoftGlyphs: Dictionary<number, _TrueTypeGlyph>; /** * Indicates whether the font is OpenType flavored. * * @private */ _isOpenType: boolean; readonly macintosh: Dictionary<number, _TrueTypeGlyph>; /** * Gets the internal microsoft glyph index mapping used for unicode mode. * * @private * @returns {Dictionary<number, _TrueTypeGlyph>} Microsoft glyph dictionary. */ readonly _microsoft: Dictionary<number, _TrueTypeGlyph>; /** * Gets the internal subset glyph map for macintosh indices. * * @private * @returns {Dictionary<number, _TrueTypeGlyph>} Microsoft glyph dictionary. */ readonly _macintoshGlyphs: Dictionary<number, _TrueTypeGlyph>; /** * Gets the internal subset glyph map for microsoft indices. * * @private * @returns {Dictionary<number, _TrueTypeGlyph>} Internal microsoft glyphs map. */ readonly _microsoftGlyphs: Dictionary<number, _TrueTypeGlyph>; constructor(fontData: Uint8Array); /** * Initializes internal metrics, reads core tables, and sets basic font properties. * * @private * @returns {void} nothing. */ _initialize(): void; /** * Reads the table directory and builds the internal table map from the font data. * * @private * @returns {void} nothing. */ _readFontDictionary(): void; /** * Normalizes table offsets when the directory is not at the expected position. * * @private * @returns {void} nothing. */ _fixOffsets(): void; /** * Checks the font container header and detects truetype collection and opentype signatures. * * @private * @returns {number} The sfnt version value. * @throws {Error} When the data is not a valid TTC/TTF. */ _check(): number; /** * Reads the name table and collects naming records for the font. * * @private * @returns {_TrueTypeNameTable} The parsed name table. */ _readNameTable(): _TrueTypeNameTable; /** * Reads the head table and extracts global font header values. * * @private * @returns {_TrueTypeHeadTable} The parsed head table. */ _readHeadTable(): _TrueTypeHeadTable; /** * Reads the horizontal header table and extracts ascender descender and metrics info. * * @private * @returns {_TrueTypeHorizontalHeaderTable} The parsed horizontal header table. */ _readHorizontalHeaderTable(): _TrueTypeHorizontalHeaderTable; /** * Reads the os two table and extracts typographic and character range data. * * @private * @returns {_TrueTypeOS2Table} The parsed OS/2 table. */ _readOS2Table(): _TrueTypeOS2Table; /** * Reads the post table and extracts italic angle fixed pitch and related fields. * * @private * @returns {_TrueTypePostTable} The parsed post table. */ _readPostTable(): _TrueTypePostTable; /** * Reads the horizontal metrics table and computes glyph advance widths in font units. * Reads the horizontal metrics table and computes glyph advance widths in font units. * * @private * @param {number} glyphCount - Number of horizontal metrics (hMetrics) to read. * @param {number} unitsPerEm - Units per em from the head table. * @returns {number[]} An array of glyph widths in 1000 UPM space. */ _readWidthTable(glyphCount: number, unitsPerEm: number): number[]; /** * Reads the cmap table and loads subtable descriptors for character to glyph mapping. * * @private * @returns {_TrueTypeCmapSubTable[]} An array of parsed cmap subtables. */ _readCmapTable(): _TrueTypeCmapSubTable[]; /** * Reads a cmap subtable and dispatches to the specific format handler based on encoding. * * @private * @param {_TrueTypeCmapSubTable} subTable - The subtable descriptor to populate. * @returns {void} nothing. */ _readCmapSubTable(subTable: _TrueTypeCmapSubTable): void; /** * Parses the apple cmap format and registers macintosh glyph mappings and widths. * * @private * @param {_TrueTypeCmapSubTable} subTable - The cmap subtable descriptor. * @param {_TrueTypeCmapEncoding} encoding - Resolved cmap encoding. * @returns {void} nothing. */ _readAppleCmapTable(subTable: _TrueTypeCmapSubTable, encoding: _TrueTypeCmapEncoding): void; /** * Parses the microsoft cmap format and registers unicode or symbol glyph mappings and widths. * * @private * @param {_TrueTypeCmapSubTable} subTable - The cmap subtable descriptor. * @param {_TrueTypeCmapEncoding} encoding - Resolved cmap encoding. * @returns {void} nothing. */ _readMicrosoftCmapTable(subTable: _TrueTypeCmapSubTable, encoding: _TrueTypeCmapEncoding): void; /** * Parses the trimmed cmap format and registers a compact range of macintosh glyphs. * * @private * @param {_TrueTypeCmapSubTable} subTable - The cmap subtable descriptor. * @param {_TrueTypeCmapEncoding} encoding - Resolved cmap encoding. * @returns {void} nothing. */ _readTrimmedCmapTable(subTable: _TrueTypeCmapSubTable, encoding: _TrueTypeCmapEncoding): void; /** * Reads the compact font format raw bytes when present in the font. * * @private * @returns {number[]} Raw CFF bytes. */ _readCompactFontFormatTable(): number[]; /** * Initializes human readable font names from the name table records. * * @private * @param {_TrueTypeNameTable} nameTable - The name table to extract names from. * @returns {void} nothing. */ _initializeFontName(nameTable: _TrueTypeNameTable): void; /** * Retrieves table information by name from the directory. * * @private * @param {string} name - The sfnt table tag (e.g., 'head', 'cmap'). * @returns {_TrueTypeTableInfo} The table info entry. */ _getTable(name: string): _TrueTypeTableInfo; /** * Gets the width value for the specified glyph index with bounds protection. * * @private * @param {number} glyphCode - Glyph index. * @returns {number} The width for the glyph index. */ _getWidth(glyphCode: number): number; /** * Resolves the cmap encoding type from platform and encoding identifiers. * * @private * @param {number} platformID - Platform ID (e.g., Microsoft/Macintosh). * @param {number} encodingID - Encoding ID based on platform. * @returns {_TrueTypeCmapEncoding} The resolved cmap encoding. */ _getCmapEncoding(platformID: number, encodingID: number): _TrueTypeCmapEncoding; /** * Adds a glyph mapping to the internal collection for the active encoding. * * @private * @param {_TrueTypeGlyph} glyph - Glyph data to add. * @param {_TrueTypeCmapEncoding} encoding - Encoding bucket to target. * @returns {void} nothing. */ _addGlyph(glyph: _TrueTypeGlyph, encoding: _TrueTypeCmapEncoding): void; /** * Populates metrics values using multiple tables and computes scaled dimensions. * * @private * @param {_TrueTypeNameTable} nameTable - Name table. * @param {_TrueTypeHeadTable} headTable - Head table. * @param {_TrueTypeHorizontalHeaderTable} horizontalHeadTable - Horizontal header. * @param {_TrueTypeOS2Table} os2Table - OS/2 table. * @param {_TrueTypePostTable} postTable - Post table. * @param {_TrueTypeCmapSubTable[]} cmapTables - Parsed cmap subtables. * @returns {void} nothing. */ _initializeMetrics(nameTable: _TrueTypeNameTable, headTable: _TrueTypeHeadTable, horizontalHeadTable: _TrueTypeHorizontalHeaderTable, os2Table: _TrueTypeOS2Table, postTable: _TrueTypePostTable, cmapTables: _TrueTypeCmapSubTable[]): void; /** * Builds a width table for the first two hundred and fifty six codes using glyph lookup. * * @private * @returns {number[]} The width table for codes 0–255. */ _updateWidth(): number[]; /** * Returns the default glyph used when a mapping is missing. * * @private * @returns {_TrueTypeGlyph} The default (whitespace) glyph entry. */ _getDefaultGlyph(): _TrueTypeGlyph; /** * Converts a sequence of bytes to a string using simple code point mapping. * * @private * @param {number[]} byteToProcess - Bytes to convert. * @param {number} start - Start index within the array. * @param {number} length - Number of bytes to process. * @returns {string} The resulting string. */ _getString(byteToProcess: number[], start: number, length: number): string; /** * Sets the current read offset within the font data stream. * * @private * @param {number} offset - New absolute offset. * @returns {void} nothing. */ _setOffset(offset: number): void; /** * Produces a subset font program for the provided characters and returns raw bytes. * * @private * @param {Dictionary<string, string>} chars - Set of characters to include in subset. * @returns {number[]} The raw subset font program bytes. */ _readFontProgram(chars: Dictionary<string, string>): number[]; /** * Assembles a new glyf table and a new loca table for the subset glyph set. * * @private * @param {Dictionary<number, number>} glyphChars - Glyph indices to include. * @param {_TrueTypeLocaTable} locaTable - Source loca table. * @param {number[]} newLocaTable - Output loca table buffer (ignored on input). * @param {number[]} newGlyphTable - Output glyf table buffer (ignored on input). * @returns {{ glyphTableSize: number, newLocaTable: number[], newGlyphTable: number[] }} Sizes and buffers. */ _generateGlyphTable(glyphChars: Dictionary<number, number>, locaTable: _TrueTypeLocaTable, newLocaTable: number[], newGlyphTable: number[]): { glyphTableSize: number; newLocaTable: number[]; newGlyphTable: number[]; }; /** * Reads the loca table and returns glyph offsets in long or short form. * * @private * @param {boolean} bShort - When `true`, read short (uint16*2) offsets; otherwise long (uint32). * @returns {_TrueTypeLocaTable} The parsed loca table. */ _readLocaTable(bShort: boolean): _TrueTypeLocaTable; /** * Ensures the subset includes required composite glyph components. * * @private * @param {Dictionary<number, number>} glyphChars - The current glyph set for subsetting. * @param {_TrueTypeLocaTable} locaTable - The source loca table. * @returns {void} nothing. */ _updateGlyphChars(glyphChars: Dictionary<number, number>, locaTable: _TrueTypeLocaTable): void; /** * Processes a composite glyph and adds referenced child glyphs to the subset list. * * @private * @param {Dictionary<number, number>} glyphChars - The glyph set being built. * @param {number} glyph - The glyph index to process. * @param {_TrueTypeLocaTable} locaTable - Source loca table. * @returns {void} nothing. */ _processCompositeGlyph(glyphChars: Dictionary<number, number>, glyph: number, locaTable: _TrueTypeLocaTable): void; /** * Encodes the new loca table in short or long form and returns the byte array. * * @private * @param {number[]} newLocaTable - Raw absolute offsets to encode. * @param {boolean} bLocaIsShort - Whether to write short form. * @returns {{ newLocaUpdated: number[], newLocaSize: number }} Encoded table bytes and logical size. */ _updateLocaTable(newLocaTable: number[], bLocaIsShort: boolean): { newLocaUpdated: number[]; newLocaSize: number; }; /** * Aligns a byte count to a four byte boundary. * * @private * @param {number} value - Byte count to align. * @returns {number} The aligned byte count. */ _align(value: number): number; /** * Builds the final font program header, tables, checksums and data sections. * * @private * @param {number[]} newLocaTableOut - Encoded loca table bytes. * @param {number[]} newGlyphTable - Encoded glyf table bytes. * @param {number} glyphTableSize - Logical glyf table size. * @param {number} locaTableSize - Logical loca table size. * @returns {number[]} The final subset font program bytes. */ _getFontProgram(newLocaTableOut: number[], newGlyphTable: number[], glyphTableSize: number, locaTableSize: number): number[]; /** * Computes the total font program length and the number of included tables. * * @private * @param {number[]} newLocaTableOut - Encoded loca table. * @param {number[]} newGlyphTable - Encoded glyf table. * @param {number} table - Initial table count (ignored). * @returns {{ fontProgramLength: number, table: number }} Total length and table count. */ _getFontProgramLength(newLocaTableOut: number[], newGlyphTable: number[], table: number): { fontProgramLength: number; table: number; }; /** * Creates a mapping from glyph indices to source characters for subsetting. * * @private * @param {Dictionary<string, string>} chars - Character set to map. * @returns {Dictionary<number, number>} Map of glyph index to Unicode code point. */ _getGlyphChars(chars: Dictionary<string, string>): Dictionary<number, number>; /** * Writes table directory entries with checksums, offsets and lengths. * * @private * @param {_BigEndianWriter} writer - Writer to receive table directory. * @param {number} table - Total table count. * @param {number[]} newLocaTableOut - Encoded loca table bytes. * @param {number[]} newGlyphTable - Encoded glyf table bytes. * @param {number} glyphTableSize - Logical glyf table size. * @param {number} locaTableSize - Logical loca table size. * @returns {void} nothing. */ _writeCheckSums(writer: _BigEndianWriter, table: number, newLocaTableOut: number[], newGlyphTable: number[], glyphTableSize: number, locaTableSize: number): void; /** * Calculates a big endian checksum for a byte array. * * @private * @param {number[]} bytes - Bytes for checksum. * @returns {number} The checksum value. */ _calculateCheckSum(bytes: number[]): number; /** * Writes all included tables into the output font program in canonical order. * * @private * @param {_BigEndianWriter} writer - Destination writer. * @param {number[]} newLocaTable - Encoded loca table bytes. * @param {number[]} newGlyphTable - Encoded glyf table bytes. * @returns {void} nothing. */ _writeGlyphs(writer: _BigEndianWriter, newLocaTable: number[], newGlyphTable: number[]): void; /** * Reads a range of bytes from the font data into the provided buffer. * * @private * @param {number[]} buffer - Destination buffer to fill. * @param {number} index - Start index in destination buffer. * @param {number} count - Number of bytes to read. * @returns {{ buffer: number[], written: number }} The buffer and bytes written. */ _read(buffer: number[], index: number, count: number): { buffer: number[]; written: number; }; /** * Creates all reader internals by loading required tables and computing metrics. * * @private * @returns {void} nothing. */ _createInternals(): void; /** * Resolves a glyph either by numeric code or by character using active encodings. * * @private * @param {string} charCode - Character to resolve (overload). * @returns {_TrueTypeGlyph} The glyph entry. */ _getGlyph(charCode: string): _TrueTypeGlyph; _getGlyph(charCode: number): _TrueTypeGlyph; /** * Reads a string as latin or as unicode by stepping over high bytes. * * @private * @param {number} length - Number of bytes to read. * @returns {string} The decoded string. */ _readString(length: number): string; _readString(length: number, isUnicode: boolean): string; /** * Reads a fixed number value with integer and fractional parts. * * @private * @param {number} offset - Offset to read from. * @returns {number} The fixed-point value. */ _readFixed(offset: number): number; /** * Reads a signed thirty two bit integer in big endian order. * * @private * @param {number} offset - Offset to read from. * @returns {number} The signed 32-bit value. */ _readInt32(offset: number): number; /** * Reads an unsigned thirty two bit integer in big endian order. * * @private * @param {number} offset - Offset to read from. * @returns {number} The unsigned 32-bit value. */ _readUInt32(offset: number): number; /** * Reads a signed sixteen bit integer in big endian order with sign correction. * * @private * @param {number} offset - Offset to read from. * @returns {number} The signed 16-bit value. */ _readInt16(offset: number): number; /** * Reads a signed sixty four bit integer composed from two big endian words. * * @private * @param {number} offset - Offset to read from. * @returns {number} The signed 64-bit value (as JS number). */ _readInt64(offset: number): number; /** * Reads an unsigned sixteen bit integer in big endian order. * * @private * @param {number} offset - Offset to read from. * @returns {number} The unsigned 16-bit value. */ _readUInt16(offset: number): number; /** * Reads an array of unsigned sixteen bit integers of the given length. * * @private * @param {number} length - Number of entries to read. * @returns {number[]} The array of uint16 values. */ _readUShortArray(length: number): number[]; /** * Reads a sequence of bytes from the current offset and advances the cursor. * * @private * @param {number} length - Number of bytes to read. * @returns {number[]} The array of bytes read. */ _readBytes(length: number): number[]; /** * Reads a single byte at the given offset and advances the cursor by one. * * @private * @param {number} offset - Offset to read from. * @returns {number} The byte value. */ _readByte(offset: number): number; /** * Returns the width for a character using glyph mapping and default fallback. * * @private * @param {string} code - Character whose width is requested. * @returns {number} The character width. */ _getCharacterWidth(code: string): number; /** * Converts a text string to a glyph index string using current mappings. * * @private * @param {string} text - Text to convert. * @returns {string} A string where each char is the mapped glyph index. */ _convertString(text: string): string; } /** * Holds a single name record entry from the name table. * * @private */ export declare class _TrueTypeNameRecord { /** * Platform identifier (e.g., Macintosh, Microsoft). * * @private */ _platformID: number; /** * Platform-specific encoding identifier. * * @private */ _encodingID: number; /** * Language identifier code. * * @private */ _languageID: number; /** * Name record identifier. * * @private */ _nameID: number; /** * Length of the name string in bytes. * * @private */ _length: number; /** * Offset to the name string within the storage area. * * @private */ _offset: number; /** * Decoded name string value. * * @private */ _name: string; } /** * Stores computed truetype metrics including ascents descents widths and box data. * * @private */ export declare class _TrueTypeMetrics { /** * Gap between lines added to ascent and descent. * * @private */ _lineGap: number; /** * Indicates whether the font contains the requested character. * * @private */ _contains: boolean; /** * Indicates whether the font is a symbol font. * * @private */ _isSymbol: boolean; /** * Indicates whether the font is fixed pitch. * * @private */ _isFixedPitch: boolean; /** * Italic angle of the font in degrees. * * @private */ _italicAngle: number; /** * PostScript name of the font. * * @private */ _postScriptName: string; /** * Font family name. * * @private */ _fontFamily: string; /** * Cap height value of the font. * * @private */ _capHeight: number; /** * Leading (line spacing) value. * * @private */ _leading: number; /** * Macintosh ascent metric. * * @private */ _macAscent: number; /** * Macintosh descent metric. * * @private */ _macDescent: number; /** * Windows descent metric. * * @private */ _winDescent: number; /** * Windows ascent metric. * * @private */ _winAscent: number; /** * Stem thickness value (vertical). * * @private */ _stemV: number; /** * Table of glyph advance widths. * * @private */ _widthTable: number[]; /** * Macintosh style flags. * * @private */ _macStyle: number; /** * Subscript size scale factor. * * @private */ _subScriptSizeFactor: number; /** * Superscript size scale factor. * * @private */ _superscriptSizeFactor: number; /** * Font bounding box values [xMin, yMin, xMax, yMax]. * * @private */ _fontBox: number[]; /** * Indicates whether the font style includes italic according to mac style flags. * * @private * @returns {boolean} `true` if italic; otherwise `false`. */ readonly _isItalic: boolean; /** * Indicates whether the font style includes bold according to mac style flags. * * @private * @returns {boolean} `true` if bold; otherwise `false` */ readonly _isBold: boolean; } /** * Represents an entry of the horizontal metrics table with advance width and side bearing. * * @private */ export declare class _TrueTypeLongHorMetric { /** * Advance width for the glyph. * * @private */ _advanceWidth: number; /** * Left side bearing value. * * @private */ _lsb: number; } /** * Represents a glyph mapping with index width and source character code. * * @private */ export declare class _TrueTypeGlyph { /** * Glyph index in the font. * * @private */ _index: number; /** * Advance width of the glyph. * * @private */ _width: number; /** * Source character code mapped to this glyph. * * @private */ _charCode: number; /** * Indicates whether the glyph entry is empty with zero index width and code. * * @private * @returns {boolean} `true` if empty; otherwise `false`. */ readonly _empty: boolean; } /** * Holds the glyph location offsets used to address entries in the glyf table. * * @private */ export declare class _TrueTypeLocaTable { /** * Offsets to glyph data within the 'glyf' table. * * @private */ _offsets: number[]; } /** * Represents the glyph header fields for contour count and bounds. * * @private */ export declare class _TrueTypeGlyphHeader { numberOfContours: number; xMin: number; yMin: number; xMax: number; yMax: number; } /** * Writes big endian values into an internal buffer with position tracking. * * @private */ export declare class _BigEndianWriter { readonly int32Size: number; readonly int16Size: number; readonly int64Size: number; /** * Internal byte buffer for assembled output. * * @private */ _buffer: number[]; /** * Current length of the internal buffer. * * @private */ _bufferLength: number; /** * Current write position within the buffer. * * @private */ _internalPosition: number; /** * Gets the internal buffer extended to the reserved capacity with zero padding. * * @private * @returns {number[]} The internal byte buffer. */ readonly _data: number[]; /** * Gets the current write position within the internal buffer with lazy init. * * @private * @returns {number} The current write position. */ readonly _position: number; constructor(capacity: number); /** * Writes a sixteen bit signed value in big endian order to the buffer. * * @private * @param {number} value - Value to write. * @returns {void} nothing. */ _writeShort(value: number): void; /** * Writes a thirty two bit signed value in big endian order to the buffer. * * @private * @param {number} value - Value to write. * @returns {void} nothing. */ _writeInt(value: number): void; /** * Writes a thirty two bit unsigned value in big endian order to the buffer. * * @private * @param {number} value - Unsigned value to write. * @returns {void} nothing. */ _writeUInt(value: number): void; /** * Writes an ascii string as raw bytes to the buffer. * * @private * @param {string} value - ASCII string. * @returns {void} nothing. */ _writeString(value: string): void; /** * Writes a sequence of bytes to the buffer. * * @private * @param {number[]} value - Bytes to write. * @returns {void} nothing. */ _writeBytes(value: number[]): void; /** * Flushes the provided bytes into the internal buffer and advances the position. * * @private * @param {number[]} buff - Bytes to flush. * @returns {void} nothing. */ _flush(buff: number[]): void; }