import type { AnnotationFlattenResult, AnnotationRef, MutationMeta, PageFlattenUsage, PageObjectNumber, } from '@embedpdf/engine-core/runtime'; import { ANNOTATION_RESOURCE_ROLES, EngineError, EngineErrorCode, subtypeFromCode, toPageRef, } from '@embedpdf/engine-core/runtime'; import type { PdfRuntimeModule, Ptr } from '@embedpdf/engine-runtime'; import { resolveAnnotPtr } from './internal/identity/resolveAnnotationPointer'; import { exportUnturnedAppearance } from './internal/read/exportUnturnedAppearance'; import { promoteInlineAnnotations } from './internal/write/promoteInlineAnnotations'; import { saveDocumentToBuffer } from './internal/saveDocumentToBuffer'; import type { DocumentSession } from '../../document-session/DocumentSession'; import { withScratch } from '../../runtime/memory/scratch'; import { throwIfAborted } from '../../shared/abort'; // `FLATTEN_*` / `EPDF_FLATTEN_STATUS_*` from public/fpdf_flatten.h. const FLATTEN_FAIL = 0; const FLATTEN_SUCCESS = 1; const STATUS_APPLIED = 0; const STATUS_SKIPPED = 1; const STATUS_NOT_ON_PAGE = 2; /** * The annotation-plane flatten verbs — `pages.flatten` for a chosen set: * in place (`flatten`) or into a new single-page document * (`exportAppearance`). Both resolve refs to native handles on the page, * hand the set to the fork (one candidate plan, one placement writer — the * same code whole-page flatten runs), and release every handle afterwards. */ export class AnnotationFlattener { constructor( private readonly runtime: PdfRuntimeModule, private readonly session: DocumentSession, ) {} flatten( pageObjectNumber: PageObjectNumber, refs: AnnotationRef[], usage: PageFlattenUsage, signal: AbortSignal, ): AnnotationFlattenResult { throwIfAborted(signal); this.requireRefs('annotations.flatten', pageObjectNumber, refs); const { fn, mem } = this.runtime; // A flatten removes entries from /Annots; promotion keeps every position. const promoted = promoteInlineAnnotations(this.runtime, this.session, pageObjectNumber); const pool = this.session.pagePool(); const pagePtr = pool.acquire(pageObjectNumber); try { const annotPtrs = this.resolveAll(pagePtr, refs); let code: number; let statuses: number[]; try { [code, statuses] = this.withHandleArray(annotPtrs, (arrayPtr) => withScratch(mem, 4 * refs.length, (statusPtr) => { const rc = fn.EPDFPage_FlattenAnnotations( pagePtr, arrayPtr, refs.length, usage === 'print' ? 1 : 0, statusPtr, ); const out: number[] = []; for (let i = 0; i < refs.length; i++) { out.push(Number(mem.peek(statusPtr, 'i32', 4 * i))); } return [rc, out] as const; }), ); } finally { for (const annotPtr of annotPtrs) fn.FPDFPage_CloseAnnot(annotPtr); } if (code === FLATTEN_FAIL) { // Every ref resolved on this page (resolveAll), so a fail is the // fork disagreeing with /Annots or a catalog write failing. const foreign = statuses.findIndex((status) => status === STATUS_NOT_ON_PAGE); throw new EngineError( foreign >= 0 ? EngineErrorCode.InvalidArg : EngineErrorCode.Unknown, foreign >= 0 ? `annotations.flatten: ref ${foreign} is not an annotation of page ${pageObjectNumber}` : 'native annotation flatten failed after preflight', ); } const results = refs.map((ref, i) => ({ ref, status: statuses[i] === STATUS_APPLIED ? ('applied' as const) : ('unchanged' as const), })); // A promotion with nothing to flatten still changed the page. if (code !== FLATTEN_SUCCESS && !promoted) { const meta: MutationMeta = { affectedPages: [], cacheDelta: null, ...this.session.writeStamp() }; return { page: toPageRef(pageObjectNumber), usage, results, meta }; } // Content and annotations changed on this page. this.session.invalidateDerived(); const meta: MutationMeta = { affectedPages: [toPageRef(pageObjectNumber)], cacheDelta: null, ...this.session.writeStamp(), }; return { page: toPageRef(pageObjectNumber), usage, results, meta }; } finally { pool.release(pageObjectNumber); } } exportAppearance( pageObjectNumber: PageObjectNumber, refs: AnnotationRef[], signal: AbortSignal, ): { bytes: ArrayBuffer; size: number } { throwIfAborted(signal); this.requireRefs('annotations.exportAppearance', pageObjectNumber, refs); const { fn } = this.runtime; const pool = this.session.pagePool(); const pagePtr = pool.acquire(pageObjectNumber); let exportedPtr: Ptr | null = null; try { const annotPtrs = this.resolveAll(pagePtr, refs); try { exportedPtr = this.withHandleArray(annotPtrs, (arrayPtr) => fn.EPDFPage_ExportAnnotationsAsDocument(pagePtr, arrayPtr, refs.length), ); } finally { for (const annotPtr of annotPtrs) fn.FPDFPage_CloseAnnot(annotPtr); } if (!exportedPtr) { // All-or-nothing by contract: a ref without a usable normal // appearance (hidden, popup, no /AP) refuses the whole export. throw new EngineError( EngineErrorCode.InvalidArg, 'annotations.exportAppearance: every ref must be a visible annotation of this page with a normal appearance', ); } return saveDocumentToBuffer( this.runtime.fn, this.runtime.mem, exportedPtr, 'exported appearances', ); } finally { if (exportedPtr) fn.FPDF_CloseDocument(exportedPtr); pool.release(pageObjectNumber); } } /** * An annotation's `appearance` resource: what its normal appearance draws * apart from what its data describes, as a one-page PDF * (`EPDFAnnot_ExportAppearance`). Only a kind that takes an appearance * resource has one; a copy is created from these bytes and the data. */ readAppearance( pageObjectNumber: PageObjectNumber, ref: AnnotationRef, signal: AbortSignal, ): { bytes: ArrayBuffer; size: number } { throwIfAborted(signal); const { fn } = this.runtime; const pool = this.session.pagePool(); const pagePtr = pool.acquire(pageObjectNumber); let exportedPtr: Ptr | null = null; try { const annotPtr = resolveAnnotPtr(this.runtime, this.session, pagePtr, ref); try { const subtype = subtypeFromCode(fn.FPDFAnnot_GetSubtype(annotPtr)); if (ANNOTATION_RESOURCE_ROLES[subtype]?.appearance === undefined) { throw new EngineError( EngineErrorCode.InvalidArg, `a ${subtype} annotation has no 'appearance' resource`, ); } exportedPtr = exportUnturnedAppearance(fn, this.runtime.mem, annotPtr); } finally { fn.FPDFPage_CloseAnnot(annotPtr); } if (!exportedPtr) { throw new EngineError( EngineErrorCode.InvalidArg, 'the annotation has no normal appearance to read', ); } return saveDocumentToBuffer(this.runtime.fn, this.runtime.mem, exportedPtr, 'the appearance'); } finally { if (exportedPtr) fn.FPDF_CloseDocument(exportedPtr); pool.release(pageObjectNumber); } } /** Resolve every ref on `pagePtr` (NotFound from the resolver on caller * error); on any failure, close what was opened. */ private resolveAll(pagePtr: Ptr, refs: AnnotationRef[]): Ptr[] { const { fn } = this.runtime; const annotPtrs: Ptr[] = []; try { for (const ref of refs) { annotPtrs.push(resolveAnnotPtr(this.runtime, this.session, pagePtr, ref)); } return annotPtrs; } catch (error) { for (const annotPtr of annotPtrs) fn.FPDFPage_CloseAnnot(annotPtr); throw error; } } /** Marshal an `FPDF_ANNOTATION*` array for the call's duration. */ private withHandleArray(annotPtrs: Ptr[], body: (arrayPtr: Ptr) => T): T { const { mem } = this.runtime; const isWasm = this.runtime.kind === 'wasm'; const ptrSize = isWasm ? 4 : 8; return withScratch(mem, Math.max(1, annotPtrs.length * ptrSize), (arrayPtr) => { annotPtrs.forEach((ptr, i) => isWasm ? mem.poke(arrayPtr, 'i32', Number(ptr), i * 4) : mem.poke(arrayPtr, 'i64', ptr, i * 8), ); return body(arrayPtr); }); } private requireRefs(op: string, pageObjectNumber: PageObjectNumber, refs: AnnotationRef[]): void { if (refs.length === 0) { throw new EngineError(EngineErrorCode.InvalidArg, `${op} requires at least one ref`); } for (const ref of refs) { if (ref.page.objectNumber !== pageObjectNumber) { throw new EngineError( EngineErrorCode.InvalidArg, `${op} refs must all target page ${pageObjectNumber}; got ref on page ${ref.page.objectNumber}`, ); } } } }