Skip to content

Commit

Permalink
XFA - Implement aspect property on image element
Browse files Browse the repository at this point in the history
  - it aims to fix issue #13634;
  - move some img-related functions in test/drivers.js in order to have images in xfa reftests.
  • Loading branch information
calixteman committed Jun 27, 2021
1 parent d7f8a0e commit f8b7823
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 44 deletions.
29 changes: 28 additions & 1 deletion src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2829,11 +2829,38 @@ class Image extends StringObject {
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: {},
style,
src: URL.createObjectURL(blob),
},
});
Expand Down
90 changes: 47 additions & 43 deletions test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,51 @@ function writeSVG(svgElement, ctx, resolve, reject) {
};
}

function inlineImages(images) {
const imagePromises = [];
for (let i = 0, ii = images.length; i < ii; i++) {
const imagePromise = new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = function () {
const reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.onerror = function (e) {
reject(new Error("Error fetching inline image " + e));
};
xhr.open("GET", images[i].src);
xhr.send();
});
imagePromises.push(imagePromise);
}
return imagePromises;
}

async function resolveImages(node) {
const images = node.getElementsByTagName("img");
const imagePromises = inlineImages(images);

await Promise.all(imagePromises).then(function (data) {
const loadedPromises = [];
for (let i = 0, ii = data.length; i < ii; i++) {
images[i].src = data[i];
loadedPromises.push(
new Promise(function (resolveImage, rejectImage) {
images[i].onload = resolveImage;
images[i].onerror = function (e) {
rejectImage(new Error("Error loading image " + e));
};
})
);
}
return loadedPromises;
});
}

/**
* @class
*/
Expand Down Expand Up @@ -164,30 +209,6 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
return loadStyles(styles);
}

function inlineAnnotationImages(images) {
var imagePromises = [];
for (var i = 0, ii = images.length; i < ii; i++) {
var imagePromise = new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = function () {
var reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.onerror = function (e) {
reject(new Error("Error fetching inline annotation image " + e));
};
xhr.open("GET", images[i].src);
xhr.send();
});
imagePromises.push(imagePromise);
}
return imagePromises;
}

// eslint-disable-next-line no-shadow
function rasterizeAnnotationLayer(
ctx,
Expand Down Expand Up @@ -233,25 +254,7 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
pdfjsLib.AnnotationLayer.render(parameters);

// Inline SVG images from text annotations.
var images = div.getElementsByTagName("img");
var imagePromises = inlineAnnotationImages(images);

await Promise.all(imagePromises).then(function (data) {
var loadedPromises = [];
for (var i = 0, ii = data.length; i < ii; i++) {
images[i].src = data[i];
loadedPromises.push(
new Promise(function (resolveImage, rejectImage) {
images[i].onload = resolveImage;
images[i].onerror = function (e) {
rejectImage(new Error("Error loading image " + e));
};
})
);
}
return loadedPromises;
});

await resolveImages(div);
foreignObject.appendChild(div);
svg.appendChild(foreignObject);

Expand Down Expand Up @@ -312,6 +315,7 @@ var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
viewport: viewport.clone({ dontFlip: true }),
});

await resolveImages(div);
svg.appendChild(foreignObject);

writeSVG(svg, ctx, resolve, reject);
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/xfa_issue13634.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://web.archive.org/web/20210510183636/https://guichet.public.lu/dam-assets/catalogue-formulaires/tva/tva-option-aic-pers-morales/tva-option-aic-pers-morale-DE.pdf
8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,14 @@
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_issue13634",
"file": "pdfs/xfa_issue13634.pdf",
"md5": "459a04045470811cbab6671772929d3d",
"link": true,
"rounds": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_issue13556",
"file": "pdfs/xfa_issue13556.pdf",
"md5": "197e93a010763c3b6f9845595ee66c70",
Expand Down
2 changes: 2 additions & 0 deletions web/xfa_layer_builder.css
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@
}

.xfaImage {
object-position: left top;
object-fit: contain;
width: 100%;
height: 100%;
}
Expand Down

0 comments on commit f8b7823

Please sign in to comment.