/** * RenderContext — centralizes the object-numbering allocator and the * resource-registry maps used during PDF serialization. * * Background. Historically, `renderPdfPages` kept all of this state as * ad-hoc local variables and Maps inside one 1,000+ line function. That * made ordering invariants (object 1 = catalog, fonts before page, etc.) * implicit and made the file difficult to split into phase-specific * subsystems. `RenderContext` gathers the state into a single object so * the eventual refactor of `renderPdfPages` and the Phase 6 widget * extraction can share a uniform handle. * * This module has no callers yet — see M2.b in * docs/audit-0422/4-pdf-refactor-plan.md for the integration plan. * * Contract notes: * - `allocateRef()` returns monotonically increasing object numbers * starting at 1. Generation numbers are always 0 for newly-written * documents (PDF spec allows >0 only for incremental updates). * - `addObject(ref, value)` preserves insertion order. The writer * sorts by object number before emitting, so insertion order is * not load-bearing for output — but it is load-bearing for our * tests that snapshot the object stream. * - Resource registries are opt-in: the renderer may allocate a ref * via `allocateRef()` without also registering the resource if the * object isn't in a lookup-by-key path. */ import type { PDFDictionary, PDFStream } from "./pdf-objects.js"; import { PDFRef } from "./pdf-objects.js"; export interface RenderContextObject { ref: PDFRef; value: PDFDictionary | PDFStream; } export interface RenderContextSnapshot { /** All registered objects in insertion order. */ readonly objects: ReadonlyArray; /** The next unallocated object number (= last allocated + 1). */ readonly nextObjectNumber: number; } export declare class RenderContext { private nextObjectNumber; private readonly objects; private readonly fontRefs; private readonly imageRefs; private readonly imageAliases; private readonly formRefs; private readonly formAliases; private readonly extRefs; private readonly extAliases; private readonly shadingRefs; private readonly shadingAliases; allocateRef(): PDFRef; /** * Allocate a contiguous run of object refs in a single call. * * Used for compound objects that need adjacent numbering (e.g. an * embedded font emits 6 related objects — Type0, CIDFont, descriptor, * font-file stream, CIDToGIDMap stream, ToUnicode CMap — and the * renderer wires them up by offset). */ allocateRefBlock(count: number): PDFRef[]; addObject(ref: PDFRef, value: PDFDictionary | PDFStream): void; registerFont(key: string, ref: PDFRef): void; getFontRef(key: string): PDFRef | undefined; registerImage(key: string, ref: PDFRef, alias: string): void; getImageRef(key: string): PDFRef | undefined; getImageAlias(key: string): string | undefined; registerForm(key: string, ref: PDFRef, alias: string): void; getFormRef(key: string): PDFRef | undefined; getFormAlias(key: string): string | undefined; registerExtGState(key: string, ref: PDFRef, alias: string): void; getExtGStateRef(key: string): PDFRef | undefined; getExtGStateAlias(key: string): string | undefined; registerShading(key: string, ref: PDFRef, alias: string): void; getShadingRef(key: string): PDFRef | undefined; getShadingAlias(key: string): string | undefined; /** * Returns a frozen, read-only view of the current state. Intended for * handoff to the writer at end-of-render; further mutations to the * context are still possible but won't be reflected in this snapshot. */ snapshot(): RenderContextSnapshot; /** Iterate registered objects without allocating a snapshot array. */ iterateObjects(): IterableIterator; /** Iterate font (key, ref) pairs in registration order. */ iterateFonts(): IterableIterator<[string, PDFRef]>; /** Iterate image (key, ref, alias) triples in registration order. */ iterateImages(): IterableIterator<[string, PDFRef, string]>; /** Iterate form (key, ref, alias) triples in registration order. */ iterateForms(): IterableIterator<[string, PDFRef, string]>; /** Iterate ExtGState (key, ref, alias) triples in registration order. */ iterateExtGStates(): IterableIterator<[string, PDFRef, string]>; /** Iterate shading (key, ref, alias) triples in registration order. */ iterateShadings(): IterableIterator<[string, PDFRef, string]>; get objectCount(): number; }