From 5f8598abb78dcd7bcabbc608b7db01c4cf0ec0a1 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 2 Dec 2022 10:09:35 +0100 Subject: [PATCH] [api-minor] Normalize the `view`-getter on the worker-thread *Please note:* I don't really expect that this is will be an observable change, since virtually all PDF documents already order e.g. /MediaBox and /CropBox entries correctly. By normalizing boundingBoxes already on the worker-thread, we can be sure that even a corrupt document won't cause issues. Note how we're passing the `view`-getter to the `PartialEvaluator.getTextContent` method, in order to detect textContent which is outside of the page, hence it makes sense to ensure that it's formatted as expected. Furthermore, by normalizing this once on the worker-tread we should no longer have to worry about a possibly negative width/height in the `PageViewport` constructor. Finally, the patch also simplifies the `view`-getter a little bit. --- src/core/document.js | 23 ++++++++++------------- src/display/display_utils.js | 8 ++++---- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 37a1597c43e44..e733d881cdb1d 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -146,14 +146,14 @@ class Page { if (this.xfaData) { return this.xfaData.bbox; } - - const box = this._getInheritableProperty(name, /* getArray = */ true); + let box = this._getInheritableProperty(name, /* getArray = */ true); if (Array.isArray(box) && box.length === 4) { - if (box[2] - box[0] !== 0 && box[3] - box[1] !== 0) { + box = Util.normalizeRect(box); + if (box[2] - box[0] > 0 && box[3] - box[1] > 0) { return box; } - warn(`Empty /${name} entry.`); + warn(`Empty, or invalid, /${name} entry.`); } return null; } @@ -190,18 +190,15 @@ class Page { // extend beyond the boundaries of the media box. If they do, they are // effectively reduced to their intersection with the media box." const { cropBox, mediaBox } = this; - let view; - if (cropBox === mediaBox || isArrayEqual(cropBox, mediaBox)) { - view = mediaBox; - } else { + + if (cropBox !== mediaBox && !isArrayEqual(cropBox, mediaBox)) { const box = Util.intersect(cropBox, mediaBox); - if (box && box[2] - box[0] !== 0 && box[3] - box[1] !== 0) { - view = box; - } else { - warn("Empty /CropBox and /MediaBox intersection."); + if (box && box[2] - box[0] > 0 && box[3] - box[1] > 0) { + return shadow(this, "view", box); } + warn("Empty /CropBox and /MediaBox intersection."); } - return shadow(this, "view", view || mediaBox); + return shadow(this, "view", mediaBox); } get rotate() { diff --git a/src/display/display_utils.js b/src/display/display_utils.js index f6d245741dd34..4b9c41164e3de 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -224,13 +224,13 @@ class PageViewport { if (rotateA === 0) { offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX; offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY; - width = Math.abs(viewBox[3] - viewBox[1]) * scale; - height = Math.abs(viewBox[2] - viewBox[0]) * scale; + width = (viewBox[3] - viewBox[1]) * scale; + height = (viewBox[2] - viewBox[0]) * scale; } else { offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX; offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY; - width = Math.abs(viewBox[2] - viewBox[0]) * scale; - height = Math.abs(viewBox[3] - viewBox[1]) * scale; + width = (viewBox[2] - viewBox[0]) * scale; + height = (viewBox[3] - viewBox[1]) * scale; } // creating transform for the following operations: // translate(-centerX, -centerY), rotate and flip vertically,