Simplify the loadXfaImages method and related code

Currently we create an intermediate `Dict` during parsing, however that seems unnecessary since (note especially the second point):
 - The `NameOrNumberTree.prototype.getAll` method will already resolve any references, as needed, during parsing.
 - The `Catalog.prototype.xfaImages` getter is invoked, via the `BasePdfManager`-instance, such that any `MissingDataException`s are already handled correctly.
This commit is contained in:
Jonas Jenwald 2025-05-02 11:31:45 +02:00
parent 91bfe12f38
commit 122822a750
3 changed files with 10 additions and 22 deletions

View File

@ -1062,11 +1062,13 @@ class Catalog {
if (obj instanceof Dict && obj.has("XFAImages")) {
const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref);
for (const [key, value] of nameTree.getAll()) {
xfaImages ??= new Dict(this.xref);
xfaImages.set(
stringToPDFString(key, /* keepEscapeSequence = */ true),
value
);
if (value instanceof BaseStream) {
xfaImages ??= new Map();
xfaImages.set(
stringToPDFString(key, /* keepEscapeSequence = */ true),
value.getBytes()
);
}
}
}
return shadow(this, "xfaImages", xfaImages);

View File

@ -1238,23 +1238,10 @@ class PDFDocument {
}
async loadXfaImages() {
const xfaImagesDict = await this.pdfManager.ensureCatalog("xfaImages");
if (!xfaImagesDict) {
const xfaImages = await this.pdfManager.ensureCatalog("xfaImages");
if (!xfaImages) {
return;
}
const keys = xfaImagesDict.getKeys();
const objectLoader = new ObjectLoader(xfaImagesDict, keys, this.xref);
await objectLoader.load();
const xfaImages = new Map();
for (const key of keys) {
const stream = xfaImagesDict.get(key);
if (stream instanceof BaseStream) {
xfaImages.set(key, stream.getBytes());
}
}
this.xfaFactory.setImages(xfaImages);
}

View File

@ -3412,8 +3412,7 @@ class Image extends StringObject {
return HTMLResult.EMPTY;
}
let buffer =
this[$globalData].images && this[$globalData].images.get(this.href);
let buffer = this[$globalData].images?.get(this.href);
if (!buffer && (this.href || !this[$content])) {
// In general, we don't get remote data and use what we have
// in the pdf itself, so no picture for non null href.