import { PdfDictionary } from '../core/objects/pdf-dictionary.js'; import { PdfIndirectObject } from '../core/objects/pdf-indirect-object.js'; import { PdfName } from '../core/objects/pdf-name.js'; import { PdfNumber } from '../core/objects/pdf-number.js'; import { PdfArray } from '../core/objects/pdf-array.js'; import { PdfObjectReference } from '../core/objects/pdf-object-reference.js'; import type { FontDescriptor, UnicodeFontDescriptor, FontParser } from './types.js'; import type { ByteArray } from '../types.js'; type PdfFontDictionary = PdfDictionary<{ Type: PdfName<'Font'>; Subtype: PdfName<'Type1' | 'TrueType' | 'Type0'>; BaseFont: PdfName; FontDescriptor?: PdfObjectReference; Encoding?: PdfName; FirstChar?: PdfNumber; LastChar?: PdfNumber; Widths?: PdfArray; DescendantFonts?: PdfArray; ToUnicode?: PdfObjectReference; }>; type PdfStandardFontName = 'Helvetica' | 'Helvetica-Bold' | 'Helvetica-Oblique' | 'Helvetica-BoldOblique' | 'Times-Roman' | 'Times-Bold' | 'Times-Italic' | 'Times-BoldItalic' | 'Courier' | 'Courier-Bold' | 'Courier-Oblique' | 'Courier-BoldOblique' | 'Symbol' | 'ZapfDingbats'; /** * Represents an embedded font in a PDF document. * Extends PdfIndirectObject with a PdfDictionary content for the font dict. */ export declare class PdfFont extends PdfIndirectObject { /** * The PDF resource name used in content streams (e.g., 'F1', 'F2'). * This is the identifier used in PDF operators like `/F1 12 Tf`. */ resourceName: string; private static _resourceCounter; private static nextResourceName; /** * @internal * Font descriptor with metrics and properties. */ private _descriptor?; /** * @internal * Original font file bytes. */ private _fontData?; constructor(font: PdfIndirectObject); constructor(fontName: string); constructor(options: { fontName?: string; resourceName?: string; encoding?: string; descriptor?: FontDescriptor | UnicodeFontDescriptor; fontData?: ByteArray; }); get dict(): PdfFontDictionary; get fontName(): string | undefined; set fontName(name: string | undefined); get encoding(): string | undefined; set encoding(enc: string | undefined); /** * Gets the encoding map from the font's Encoding dictionary's Differences array. * Maps byte codes to Unicode characters for custom-encoded fonts. */ get encodingMap(): Map | null; /** * Gets the reverse encoding map (Unicode character → byte code). * Useful for encoding text back into the font's custom encoding. */ get reverseEncodingMap(): Map | undefined; /** * Whether this font uses Unicode (Type0/composite) encoding. */ get isUnicode(): boolean; /** * Gets the font descriptor with metrics and properties. * Available for embedded fonts, undefined for standard fonts or loaded fonts without descriptor. */ get descriptor(): FontDescriptor | UnicodeFontDescriptor | undefined; /** * Gets the original font file bytes. * Available for embedded fonts, undefined for standard fonts or loaded fonts. */ get fontData(): ByteArray | undefined; /** * Gets the font type (Subtype in PDF). */ get fontType(): 'Type1' | 'TrueType' | 'Type0' | 'MMType1' | 'Type3' | undefined; /** * Sets the font type (Subtype in PDF). */ set fontType(type: 'Type1' | 'TrueType' | 'Type0' | 'MMType1' | 'Type3' | undefined); /** * Gets the first character code in the Widths array. */ get firstChar(): number | undefined; /** * Sets the first character code in the Widths array. */ set firstChar(value: number | undefined); /** * Gets the last character code in the Widths array. */ get lastChar(): number | undefined; /** * Sets the last character code in the Widths array. */ set lastChar(value: number | undefined); /** * Gets the character widths array. */ get widths(): number[] | undefined; /** * Sets the character widths array. */ set widths(values: number[] | undefined); /** * Gets the raw character width (in 1000-unit em square) for a character code. * Returns null if the character is not in the font's width table. * * @param charCode - The character code to get the width for * @returns The raw character width or null if not found */ getRawCharacterWidth(charCode: number): number | null; /** * Gets the character width scaled to the specified font size. * Returns null if the character is not in the font's width table. * * @param charCode - The character code to get the width for * @param fontSize - The font size to scale to * @returns The scaled character width or null if not found */ getCharacterWidth(charCode: number, fontSize: number): number | null; /** * Checks if the font has width data for a character code. * * @param charCode - The character code to check * @returns True if width data is available, false otherwise */ hasCharacterWidth(charCode: number): boolean; /** * Gets character widths for all characters in a string. * Returns null for characters not in the font's width table. * * @param text - The text to get character widths for * @param fontSize - The font size to scale to * @returns Array of character widths (null for missing characters) */ getCharacterWidthsForString(text: string, fontSize: number): (number | null)[]; /** * Returns the resource name for string coercion. * This enables using PdfFont objects in template literals like: * ```typescript * const da = `/${font} 12 Tf 0 g` * ``` */ toString(): string; /** * @internal * Legacy property for backward compatibility with code that accesses fontRef. * Returns this font's reference since PdfFont IS the indirect object. */ get fontRef(): PdfObjectReference; /** * Creates a PdfFont from font file bytes. * Automatically detects the font format (TTF, OTF, WOFF) and parses it. * * @param data - The font file bytes * @returns A PdfFont instance ready to be written to the PDF */ static fromBytes(data: ByteArray): PdfFont; /** * Creates a PdfFont from a FontParser instance. * Extracts all necessary information from the parser including font name, * descriptor, and font data. * * @param parser - A FontParser instance (TtfParser, OtfParser, or WoffParser) * @returns A PdfFont instance ready to be written to the PDF */ static fromParser(parser: FontParser): PdfFont; /** * Creates a standard PDF Type1 font (one of the 14 built-in fonts). * These fonts don't require font data as they're built into PDF viewers. * * @param fontName - One of the 14 standard PDF fonts * @param widths - Optional AFM widths array (1/1000 em units) for chars 32–126 * @returns A PdfFont instance ready to be written to the PDF */ static fromStandardFont(fontName: PdfStandardFontName, widths?: number[]): PdfFont; private static readonly _HELVETICA_WIDTHS; private static readonly _HELVETICA_BOLD_WIDTHS; private static readonly _TIMES_ROMAN_WIDTHS; private static readonly _TIMES_BOLD_WIDTHS; private static readonly _TIMES_ITALIC_WIDTHS; private static readonly _TIMES_BOLD_ITALIC_WIDTHS; static readonly HELVETICA: PdfFont; static readonly HELVETICA_BOLD: PdfFont; static readonly HELVETICA_OBLIQUE: PdfFont; static readonly HELVETICA_BOLD_OBLIQUE: PdfFont; static readonly TIMES_ROMAN: PdfFont; static readonly TIMES_BOLD: PdfFont; static readonly TIMES_ITALIC: PdfFont; static readonly TIMES_BOLD_ITALIC: PdfFont; static readonly COURIER: PdfFont; static readonly COURIER_BOLD: PdfFont; static readonly COURIER_OBLIQUE: PdfFont; static readonly COURIER_BOLD_OBLIQUE: PdfFont; static readonly SYMBOL: PdfFont; static readonly ZAPF_DINGBATS: PdfFont; private static readonly BY_BASE_FONT; /** * Returns the static PdfFont instance for a standard font name, or null if not found. */ static getStandardFont(fontName: string): PdfFont | null; /** * Creates a TrueType font from font file data. * Uses WinAnsiEncoding for standard 8-bit character support. * * @param fontData - The TrueType font file bytes * @param fontName - The name to use for this font in the PDF * @param descriptor - Font metrics and properties * @returns A PdfFont instance ready to be written to the PDF */ static fromTrueTypeData(fontData: ByteArray, fontName: string, descriptor: FontDescriptor): PdfFont; /** * Creates a Type0 (composite) font with Unicode support. * Use this for fonts that need to display non-ASCII characters. * * @param fontData - The TrueType font file bytes * @param fontName - The name to use for this font in the PDF * @param descriptor - Unicode font descriptor with CID metrics * @param unicodeMappings - Optional map of CID to Unicode code point for ToUnicode CMap * @returns A PdfFont instance ready to be written to the PDF */ static fromType0Data(fontData: ByteArray, fontName: string, descriptor: UnicodeFontDescriptor, unicodeMappings?: Map): PdfFont; static fromFile(fontData: ByteArray, options?: { fontName?: string; unicode?: boolean; unicodeMappings?: Map; }): PdfFont; /** * Generates a ToUnicode CMap for mapping CIDs to Unicode code points. */ private static generateToUnicodeCMap; /** * Builds a CID width array for the /W entry in CIDFont dictionaries. */ private static buildCIDWidthArray; } export {};