/** * Annotation `/F` flags — the one interpretation of ISO 32000-2 Table 167. * * A record's annotation carries the named booleans as the engine reads them; every * behavioral question (can I see it? click it? move it? edit its text?) is * answered here and nowhere else, so rendering, hit-testing, chrome, and the * reducer can never disagree about what a flag means. * * The flag→behavior split, in one line each: * hidden — gone everywhere (screen, interaction, print) * noView — gone on screen, may still print * toggleNoView — reveals a noView annotation while engaged (v1: selected) * readOnly — visible but inert (ignored for widget kinds, per spec — * the form layer owns field-level ReadOnly) * locked — selectable but frozen: no move/resize/rotate/delete/restyle * lockedContents — `/Contents` text cannot change; geometry still can * print — include when printing (carried for the print pipeline) * noZoom/noRotate — screen-anchored body (see anchor.ts) * invisible — hides only a type that isn't standard; meaningless on * a standard annotation */ import { NO_ANNOTATION_FLAGS, PdfAnnotationSubtypeCode, type Annotation, type AnnotationFlags, } from '@embedpdf/engine-core/runtime'; import { kindOf } from './record/identity'; export type { AnnotationFlags }; export { NO_ANNOTATION_FLAGS }; /** The `/F` every freshly drawn annotation starts with: `print` set (Acrobat * parity — without it the annotation silently disappears when printed). */ export const DRAWN_FLAGS: AnnotationFlags = { ...NO_ANNOTATION_FLAGS, print: true }; /** All ten flags, for iteration/equality — kept in the primitives' order. */ export const FLAG_KEYS = Object.keys(NO_ANNOTATION_FLAGS) as ReadonlyArray; export const flagsEqual = (left: AnnotationFlags, right: AnnotationFlags): boolean => FLAG_KEYS.every((flag) => left[flag] === right[flag]); /** Merge a partial write over the current flags. */ export const mergeFlags = ( base: AnnotationFlags, patch: Partial, ): AnnotationFlags => ({ ...base, ...patch, }); /** * Does `invisible` leave this annotation unpainted? Only when its type isn't a * standard one, which no handler draws (ISO 32000-2 12.5.3); on a standard * annotation the flag means nothing. The engine's renderer and flatten follow * the same rule. A type the engine doesn't model but PDFium knows (Sound, * Screen, 3D…) is standard, and so is Projection, which PDFium doesn't know. */ const hiddenByInvisibleFlag = (annotation: Annotation): boolean => annotation.invisible && annotation.subtype === 'unsupported' && annotation.rawSubtypeCode === PdfAnnotationSubtypeCode.UNKNOWN && annotation.rawSubtypeName !== 'Projection'; /** * On-screen visibility. `hidden` beats everything, and `invisible` hides a type * that isn't standard; `noView` hides unless `toggleNoView` and the annotation * is engaged — the spec says hover/selection, and v1 uses selection (the model * tracks no hover). */ export const viewable = (annotation: Annotation, engaged = false): boolean => !annotation.hidden && !hiddenByInvisibleFlag(annotation) && (!annotation.noView || (annotation.toggleNoView && engaged)); /** Any pointer interaction (click-to-select, hover). ReadOnly kills it. */ export const interactive = (flags: AnnotationFlags): boolean => !flags.hidden && !flags.noView && !flags.readOnly; /** The part of a ModelAnnotation these predicates read. */ export interface FlagBearer { /** The record's annotation: its kind and `/F` flags are read off it. */ annotation: Annotation; /** Session authority projected at ingest (permissions.md). Absent = * unstamped (drafts, wildcard local engines, tests) = allowed. */ authority?: { update: boolean; delete: boolean }; } /** Interaction gate for a concrete annotation: widget kinds ignore `readOnly` * (ISO 32000 — a ReadOnly form field must still be movable by a form designer; * the form-filling layer enforces field ReadOnly itself). */ export const annotInteractive = (record: FlagBearer): boolean => kindOf(record.annotation).caps.ignoresReadOnly ? !record.annotation.hidden && !record.annotation.noView : interactive(record.annotation); /** * Geometry/style mutations — `locked` freezes the object, not its contents * (that's `lockedContents`), and the session must hold update authority over * the record. One predicate for hit-test, chrome, and props alike, so a * record you may not edit renders and behaves exactly like a locked one. */ export const annotTransformable = (record: FlagBearer): boolean => annotInteractive(record) && !record.annotation.locked && (record.authority?.update ?? true); /** Deletion — the delete half of the authority split (a narrowed grant can * allow update but not delete, or vice versa). Flags gate like transforms. */ export const annotDeletable = (record: FlagBearer): boolean => annotInteractive(record) && !record.annotation.locked && (record.authority?.delete ?? true); /** `/Contents` text edits — the contents counterpart of `locked`. */ export const annotContentsEditable = (record: FlagBearer): boolean => annotInteractive(record) && !record.annotation.lockedContents && (record.authority?.update ?? true); /** An annotation's text: `lockedContents` guards it, not `locked`. */ const TEXT_FIELDS: ReadonlySet = new Set(['contents', 'richText']); /** * May this session change the record's engine `field` now? The one table * every write of engine fields asks (`withValues`): * * a flag always, given update authority: unlocking a locked * annotation, or showing a hidden one, must work * contents, richText unless `lockedContents`: its text * anything else unless `locked`: the annotation itself */ export function mayWrite(record: FlagBearer, field: string): boolean { if ((FLAG_KEYS as readonly string[]).includes(field)) return record.authority?.update ?? true; return TEXT_FIELDS.has(field) ? annotContentsEditable(record) : annotTransformable(record); }