Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XFA - An image can be a stream in the pdf (bug 1718521) #13654

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,22 @@ class Catalog {
return shadow(this, "attachments", attachments);
}

get xfaImages() {
const obj = this._catDict.get("Names");
let xfaImages = null;

if (obj instanceof Dict && obj.has("XFAImages")) {
const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref);
for (const [key, value] of nameTree.getAll()) {
if (!xfaImages) {
xfaImages = new Dict(this.ref);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: this.ref -> this.xref

}
xfaImages.set(key, value);
}
}
return shadow(this, "xfaImages", xfaImages);
}

_collectJavaScript() {
const obj = this._catDict.get("Names");
let javaScript = null;
Expand Down
22 changes: 22 additions & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,28 @@ class PDFDocument {
return null;
}

async loadXfaImages() {
const xfaImagesDict = await this.pdfManager.ensureCatalog("xfaImages");
if (!xfaImagesDict) {
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 (!isStream(stream)) {
continue;
}
xfaImages.set(key, stream.getBytes());
}

this.xfaFactory.setImages(xfaImages);
}

async loadXfaFonts(handler, task) {
const acroForm = await this.pdfManager.ensureCatalog("acroForm");
if (!acroForm) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class BasePdfManager {
return this.pdfDocument.loadXfaFonts(handler, task);
}

loadXfaImages() {
return this.pdfDocument.loadXfaImages();
}

serializeXfaData(annotationStorage) {
return this.pdfDocument.serializeXfaData(annotationStorage);
}
Expand Down
15 changes: 9 additions & 6 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,15 @@ class WorkerMessageHandler {
if (isPureXfa) {
const task = new WorkerTask("loadXfaFonts");
startWorkerTask(task);
await pdfManager
.loadXfaFonts(handler, task)
.catch(reason => {
// Ignore errors, to allow the document to load.
})
.then(() => finishWorkerTask(task));
await Promise.all([
pdfManager
.loadXfaFonts(handler, task)
.catch(reason => {
// Ignore errors, to allow the document to load.
})
.then(() => finishWorkerTask(task)),
pdfManager.loadXfaImages(),
]);
}

const [numPages, fingerprint] = await Promise.all([
Expand Down
4 changes: 4 additions & 0 deletions src/core/xfa/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class XFAFactory {
return this.dims.length;
}

setImages(images) {
this.form[$globalData].images = images;
}

setFonts(fonts) {
this.form[$globalData].fontFinder = new FontFinder(fonts);
const missingFonts = [];
Expand Down
88 changes: 45 additions & 43 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2932,56 +2932,58 @@ class Image extends StringObject {
}

[$toHTML]() {
if (this.href || !this[$content]) {
// TODO: href can be a Name referring to an internal stream
// containing a picture.
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.
return HTMLResult.EMPTY;
}

// TODO: Firefox doesn't support natively tiff (and tif) format.
if (this.transferEncoding === "base64") {
const buffer = stringToBytes(atob(this[$content]));
const blob = new Blob([buffer], { type: this.contentType });
let style;
switch (this.aspect) {
case "fit":
case "actual":
// TODO: check what to do with actual.
// Normally we should return {auto, auto} for it but
// it implies some wrong rendering (see xfa_bug1716816.pdf).
break;
case "height":
style = {
width: "auto",
height: "100%",
};
break;
case "none":
style = {
width: "100%",
height: "100%",
};
break;
case "width":
style = {
width: "100%",
height: "auto",
};
break;
}
return HTMLResult.success({
name: "img",
attributes: {
class: ["xfaImage"],
style,
src: URL.createObjectURL(blob),
},
});
if (!buffer && this.transferEncoding === "base64") {
buffer = stringToBytes(atob(this[$content]));
}

return HTMLResult.EMPTY;
if (!buffer) {
return HTMLResult.EMPTY;
}

// TODO: Firefox doesn't support natively tiff (and tif) format.
const blob = new Blob([buffer], { type: this.contentType });
let style;
switch (this.aspect) {
case "fit":
case "actual":
// TODO: check what to do with actual.
// Normally we should return {auto, auto} for it but
// it implies some wrong rendering (see xfa_bug1716816.pdf).
break;
case "height":
style = {
width: "auto",
height: "100%",
};
break;
case "none":
style = {
width: "100%",
height: "100%",
};
break;
case "width":
style = {
width: "100%",
height: "auto",
};
break;
}
return HTMLResult.success({
name: "img",
attributes: {
class: ["xfaImage"],
style,
src: URL.createObjectURL(blob),
},
});
}
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/xfa_bug1718521_1.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9229396
1 change: 1 addition & 0 deletions test/pdfs/xfa_bug1718521_2.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9229409
1 change: 1 addition & 0 deletions test/pdfs/xfa_bug1718521_3.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9229430
24 changes: 24 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,30 @@
"lastPage": 2,
"type": "eq"
},
{ "id": "xfa_bug1718521_1",
"file": "pdfs/xfa_bug1718521_1.pdf",
"md5": "9b89dd9e6a4c6c3258ca24debd806863",
"link": true,
"rounds": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1718521_2",
"file": "pdfs/xfa_bug1718521_2.pdf",
"md5": "c23beb39e1c91a780da9d4b60cbd157f",
"link": true,
"rounds": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1718521_3",
"file": "pdfs/xfa_bug1718521_3.pdf",
"md5": "5cc6fb1a65515518e3e64baa81e7a21e",
"link": true,
"rounds": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1718735",
"file": "pdfs/xfa_bug1718735.pdf",
"md5": "1001f5c02c026943cbd37f646725d82f",
Expand Down