Ensure that *all* errors are handled in rasterizeTextLayer/rasterizeAnnotationLayer

Currently errors occurring within the `src/display/{text_layer, annotation_layer}.js` files are not being handled properly by the test-suite, and the tests simply time out rather than failing as intended.
This makes it *very* easy to accidentally overlook a certain type of errors, see e.g. https://github.com/mozilla/pdf.js/pull/13055#discussion_r589005041, which this patch will thus prevent.
This commit is contained in:
Jonas Jenwald 2021-03-12 14:00:12 +01:00
parent 56fd01bf8d
commit 39cd844243

View File

@ -74,32 +74,39 @@ var rasterizeTextLayer = (function rasterizeTextLayerClosure() {
div.className = "textLayer"; div.className = "textLayer";
foreignObject.appendChild(div); foreignObject.appendChild(div);
// Rendering text layer as HTML. stylePromise
var task = pdfjsLib.renderTextLayer({ .then(async cssRules => {
textContent, style.textContent = cssRules;
container: div,
viewport,
enhanceTextSelection,
});
Promise.all([stylePromise, task.promise]).then(function (results) {
task.expandTextDivs(true);
style.textContent = results[0];
svg.appendChild(foreignObject);
// We need to have UTF-8 encoded XML. // Rendering text layer as HTML.
var svg_xml = unescape( var task = pdfjsLib.renderTextLayer({
encodeURIComponent(new XMLSerializer().serializeToString(svg)) textContent,
); container: div,
var img = new Image(); viewport,
img.src = "data:image/svg+xml;base64," + btoa(svg_xml); enhanceTextSelection,
img.onload = function () { });
ctx.drawImage(img, 0, 0); await task.promise;
resolve();
}; task.expandTextDivs(true);
img.onerror = function (e) { svg.appendChild(foreignObject);
reject(new Error("Error rasterizing text layer " + e));
}; // We need to have UTF-8 encoded XML.
}); var svg_xml = unescape(
encodeURIComponent(new XMLSerializer().serializeToString(svg))
);
var img = new Image();
img.src = "data:image/svg+xml;base64," + btoa(svg_xml);
img.onload = function () {
ctx.drawImage(img, 0, 0);
resolve();
};
img.onerror = function (e) {
reject(new Error("Error rasterizing text layer " + e));
};
})
.catch(reason => {
reject(new Error(`rasterizeTextLayer: "${reason?.message}".`));
});
}); });
} }
@ -208,45 +215,46 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
div.className = "annotationLayer"; div.className = "annotationLayer";
// Rendering annotation layer as HTML. // Rendering annotation layer as HTML.
stylePromise.then(function (common, overrides) { stylePromise
style.textContent = common + overrides; .then(async (common, overrides) => {
style.textContent = common + overrides;
var annotation_viewport = viewport.clone({ dontFlip: true }); var annotation_viewport = viewport.clone({ dontFlip: true });
var parameters = { var parameters = {
viewport: annotation_viewport, viewport: annotation_viewport,
div, div,
annotations, annotations,
page, page,
linkService: new pdfjsViewer.SimpleLinkService(), linkService: new pdfjsViewer.SimpleLinkService(),
imageResourcesPath, imageResourcesPath,
renderInteractiveForms, renderInteractiveForms,
}; };
pdfjsLib.AnnotationLayer.render(parameters); pdfjsLib.AnnotationLayer.render(parameters);
// Inline SVG images from text annotations. // Inline SVG images from text annotations.
var images = div.getElementsByTagName("img"); var images = div.getElementsByTagName("img");
var imagePromises = inlineAnnotationImages(images); var imagePromises = inlineAnnotationImages(images);
var converted = 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;
});
foreignObject.appendChild(div); await Promise.all(imagePromises).then(function (data) {
svg.appendChild(foreignObject); 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;
});
// We need to have UTF-8 encoded XML. foreignObject.appendChild(div);
converted.then(function () { svg.appendChild(foreignObject);
// We need to have UTF-8 encoded XML.
var svg_xml = unescape( var svg_xml = unescape(
encodeURIComponent(new XMLSerializer().serializeToString(svg)) encodeURIComponent(new XMLSerializer().serializeToString(svg))
); );
@ -259,8 +267,10 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
img.onerror = function (e) { img.onerror = function (e) {
reject(new Error("Error rasterizing annotation layer " + e)); reject(new Error("Error rasterizing annotation layer " + e));
}; };
})
.catch(reason => {
reject(new Error(`rasterizeAnnotationLayer: "${reason?.message}".`));
}); });
});
}); });
} }