import type { AnnotationBase, LinkAnnotation, PdfDestination, PdfLinkTarget, PdfCoordinates, } from '@embedpdf/engine-core/runtime'; import type { PdfFunctions, PdfRuntimeMemory, Ptr } from '@embedpdf/engine-runtime'; import type { AnnotationReadContext } from './annotationReadContext'; import { linkTargetFromActionTree } from '../../../destinations/linkTarget'; import { readDestination } from '../../../destinations/readDestination'; /** * Link reader: rect/flags/relationship ride the base (a link grouped to an * annotation is plain `inReplyTo` + `replyType: 'group'`); this module only * materialises the normalized target. * * One truth, one projection: the target is a pure function of the same * payload-carrying action tree the scripting plane reads * (`base.actions.activate`) — no second native read exists any more, so the * two action-shaped surfaces cannot drift by construction. * * `/A` precedence, pinned: an activate action that exists but cannot be * executed (an `incomplete` tree, a degraded/unreadable root) projects * `unsupported` — a broken action is a dead link, never an invitation to * guess. The direct `/Dest` (which is data, not an action) is consulted * only when no `/A` exists at all. */ export function readLink( fn: PdfFunctions, mem: PdfRuntimeMemory, annotPtr: Ptr, base: AnnotationBase, _rawSubtypeCode: number, ctx: AnnotationReadContext, ): LinkAnnotation { return { ...base, subtype: 'link', target: readLinkTarget(fn, mem, annotPtr, base, ctx) }; } function readLinkTarget( fn: PdfFunctions, mem: PdfRuntimeMemory, annotPtr: Ptr, base: AnnotationBase, ctx: AnnotationReadContext, ): PdfLinkTarget | null { const activate = base.actions?.activate; if (activate) return linkTargetFromActionTree(activate); // No /A at all → the direct `/Dest`, normalized onto `goto`. const linkPtr = fn.FPDFAnnot_GetLink(annotPtr); if (!linkPtr) return null; const destPtr = fn.FPDFLink_GetDest(ctx.docPtr, linkPtr); if (!destPtr) return null; // neither /A nor /Dest: a dead link, reported as-is const destination = readDestination(fn, mem, ctx.docPtr, destPtr); return destination ? { kind: 'goto', destination } : { kind: 'unsupported' }; }