mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Merge pull request #11059 from Snuffleupagus/boundingBox-more-validation
Fallback gracefully when encountering corrupt PDF files with empty /MediaBox and /CropBox entries
This commit is contained in:
commit
fbe8c6127c
6 changed files with 199 additions and 21 deletions
|
@ -95,24 +95,28 @@ class Page {
|
|||
this._getInheritableProperty('Resources') || Dict.empty);
|
||||
}
|
||||
|
||||
get mediaBox() {
|
||||
const mediaBox = this._getInheritableProperty('MediaBox',
|
||||
/* getArray = */ true);
|
||||
// Reset invalid media box to letter size.
|
||||
if (!Array.isArray(mediaBox) || mediaBox.length !== 4) {
|
||||
return shadow(this, 'mediaBox', LETTER_SIZE_MEDIABOX);
|
||||
_getBoundingBox(name) {
|
||||
const box = this._getInheritableProperty(name, /* getArray = */ true);
|
||||
|
||||
if (Array.isArray(box) && box.length === 4) {
|
||||
if ((box[2] - box[0]) !== 0 && (box[3] - box[1]) !== 0) {
|
||||
return box;
|
||||
}
|
||||
warn(`Empty /${name} entry.`);
|
||||
}
|
||||
return shadow(this, 'mediaBox', mediaBox);
|
||||
return null;
|
||||
}
|
||||
|
||||
get mediaBox() {
|
||||
// Reset invalid media box to letter size.
|
||||
return shadow(this, 'mediaBox',
|
||||
this._getBoundingBox('MediaBox') || LETTER_SIZE_MEDIABOX);
|
||||
}
|
||||
|
||||
get cropBox() {
|
||||
const cropBox = this._getInheritableProperty('CropBox',
|
||||
/* getArray = */ true);
|
||||
// Reset invalid crop box to media box.
|
||||
if (!Array.isArray(cropBox) || cropBox.length !== 4) {
|
||||
return shadow(this, 'cropBox', this.mediaBox);
|
||||
}
|
||||
return shadow(this, 'cropBox', cropBox);
|
||||
return shadow(this, 'cropBox',
|
||||
this._getBoundingBox('CropBox') || this.mediaBox);
|
||||
}
|
||||
|
||||
get userUnit() {
|
||||
|
@ -129,12 +133,18 @@ 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)) {
|
||||
return shadow(this, 'view', mediaBox);
|
||||
view = mediaBox;
|
||||
} else {
|
||||
const box = Util.intersect(cropBox, mediaBox, /* skipEmpty = */ true);
|
||||
if (box) {
|
||||
view = box;
|
||||
} else {
|
||||
warn('Empty /CropBox and /MediaBox intersection.');
|
||||
}
|
||||
}
|
||||
|
||||
const intersection = Util.intersect(cropBox, mediaBox);
|
||||
return shadow(this, 'view', intersection || mediaBox);
|
||||
return shadow(this, 'view', view || mediaBox);
|
||||
}
|
||||
|
||||
get rotate() {
|
||||
|
|
|
@ -748,7 +748,7 @@ var Util = (function UtilClosure() {
|
|||
// Returns a rectangle [x1, y1, x2, y2] corresponding to the
|
||||
// intersection of rect1 and rect2. If no intersection, returns 'false'
|
||||
// The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2]
|
||||
Util.intersect = function Util_intersect(rect1, rect2) {
|
||||
Util.intersect = function Util_intersect(rect1, rect2, skipEmpty = false) {
|
||||
function compare(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
@ -768,7 +768,7 @@ var Util = (function UtilClosure() {
|
|||
result[0] = orderedX[1];
|
||||
result[2] = orderedX[2];
|
||||
} else {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Y: first and second points belong to different rectangles?
|
||||
|
@ -778,9 +778,13 @@ var Util = (function UtilClosure() {
|
|||
result[1] = orderedY[1];
|
||||
result[3] = orderedY[2];
|
||||
} else {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (skipEmpty &&
|
||||
((result[2] - result[0]) === 0 || (result[3] - result[1]) === 0)) {
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue