Include the catalog instance in the annotationGlobals data

The `FileAttachmentAnnotation` and `MediaAnnotation` code needs to (synchronously) access a `catalog` method, which leads to unnecessarily verbose code.
This can be avoided by including the `catalog` instance in the `annotationGlobals` data, which is safe since it already includes data that's fetched asynchronously from the `catalog` instance.
This commit is contained in:
Jonas Jenwald 2026-06-23 10:10:40 +02:00
parent 04eeeec4a4
commit f07a106529
2 changed files with 10 additions and 17 deletions

View File

@ -81,10 +81,6 @@ import { parseMarkedContentProps } from "./evaluator_utils.js";
import { StringStream } from "./stream.js"; import { StringStream } from "./stream.js";
import { XFAFactory } from "./xfa/factory.js"; import { XFAFactory } from "./xfa/factory.js";
/**
* @import { Catalog } from "./catalog.js";
*/
class AnnotationFactory { class AnnotationFactory {
static createGlobals(pdfManager) { static createGlobals(pdfManager) {
return Promise.all([ return Promise.all([
@ -108,6 +104,7 @@ class AnnotationFactory {
globalColorSpaceCache, globalColorSpaceCache,
]) => ({ ]) => ({
pdfManager, pdfManager,
catalog: pdfManager.pdfDocument.catalog,
acroForm: acroForm instanceof Dict ? acroForm : Dict.empty, acroForm: acroForm instanceof Dict ? acroForm : Dict.empty,
xfaDatasets, xfaDatasets,
structTreeRoot, structTreeRoot,
@ -5421,10 +5418,6 @@ class FileAttachmentAnnotation extends MarkupAnnotation {
const { annotationGlobals, dict } = params; const { annotationGlobals, dict } = params;
const fsDict = dict.get("FS"); const fsDict = dict.get("FS");
const file = new FileSpec(fsDict);
/** @type {{catalog?: Catalog}} */
const { catalog } = annotationGlobals.pdfManager.pdfDocument;
// Encode the embedded content's reference in the id so it can be // Encode the embedded content's reference in the id so it can be
// re-fetched from the xref on demand (see `Catalog.attachmentContent`) // re-fetched from the xref on demand (see `Catalog.attachmentContent`)
// instead of being cached where `cleanup` would wipe it. The file-spec is // instead of being cached where `cleanup` would wipe it. The file-spec is
@ -5440,14 +5433,15 @@ class FileAttachmentAnnotation extends MarkupAnnotation {
); );
} }
if (contentRef instanceof Ref) { if (contentRef instanceof Ref) {
fileId = catalog?.getAttachmentIdForAnnotation(contentRef); fileId =
annotationGlobals.catalog.getAttachmentIdForAnnotation(contentRef);
} }
} }
this.data.hasOwnCanvas = this.data.noRotate; this.data.hasOwnCanvas = this.data.noRotate;
this.data.noHTML = false; this.data.noHTML = false;
this.data.fileId = fileId; this.data.fileId = fileId;
this.data.file = file.serializable; this.data.file = new FileSpec(fsDict).serializable;
const name = dict.get("Name"); const name = dict.get("Name");
this.data.name = this.data.name =
@ -5490,7 +5484,7 @@ class MediaAnnotation extends Annotation {
* when `assetRef` isn't itself a reference. * when `assetRef` isn't itself a reference.
* @param {string} asset.filename * @param {string} asset.filename
* @param {string} asset.contentType * @param {string} asset.contentType
* @param {Catalog} [catalog] * @param {Catalog} catalog
*/ */
_setMediaData({ assetRef, assetDict, filename, contentType }, catalog) { _setMediaData({ assetRef, assetDict, filename, contentType }, catalog) {
let contentRef = assetRef; let contentRef = assetRef;
@ -5502,7 +5496,7 @@ class MediaAnnotation extends Annotation {
} }
const fileId = const fileId =
contentRef instanceof Ref contentRef instanceof Ref
? catalog?.getAttachmentIdForAnnotation(contentRef) ? catalog.getAttachmentIdForAnnotation(contentRef)
: undefined; : undefined;
this.data.noHTML = false; this.data.noHTML = false;
@ -5577,8 +5571,6 @@ class RichMediaAnnotation extends MediaAnnotation {
super(params); super(params);
const { dict, xref, annotationGlobals } = params; const { dict, xref, annotationGlobals } = params;
/** @type {{catalog?: Catalog}} */
const { catalog } = annotationGlobals.pdfManager.pdfDocument;
const content = dict.get("RichMediaContent"); const content = dict.get("RichMediaContent");
if (!(content instanceof Dict)) { if (!(content instanceof Dict)) {
@ -5590,8 +5582,7 @@ class RichMediaAnnotation extends MediaAnnotation {
warn("RichMedia annotation has no playable asset."); warn("RichMedia annotation has no playable asset.");
return; return;
} }
this._setMediaData(asset, annotationGlobals.catalog);
this._setMediaData(asset, catalog);
} }
/** /**
@ -5675,7 +5666,7 @@ class ScreenAnnotation extends MediaAnnotation {
// a /Movie); such ones simply render their appearance, so don't warn. // a /Movie); such ones simply render their appearance, so don't warn.
return; return;
} }
this._setMediaData(asset, annotationGlobals.pdfManager.pdfDocument.catalog); this._setMediaData(asset, annotationGlobals.catalog);
} }
/** /**

View File

@ -73,6 +73,8 @@ describe("document", function () {
const pdfDocument = new PDFDocument(pdfManager, stream); const pdfDocument = new PDFDocument(pdfManager, stream);
pdfDocument.xref = xref; pdfDocument.xref = xref;
pdfDocument.catalog = catalog; pdfDocument.catalog = catalog;
pdfManager.pdfDocument = pdfDocument;
return pdfDocument; return pdfDocument;
} }