diff --git a/.gitignore b/.gitignore index 0b361641..40916d43 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ node_modules/ target/ .idea .npmrc - +package-lock.json diff --git a/base/shared/annotation.js b/base/shared/annotation.js index 8eb04215..978a352a 100755 --- a/base/shared/annotation.js +++ b/base/shared/annotation.js @@ -76,9 +76,9 @@ var Annotation = (function AnnotationClosure() { var data = this.data = {}; data.subtype = dict.get('Subtype').name; - var rect = dict.get('Rect'); - data.rect = Util.normalizeRect(rect); data.annotationFlags = dict.get('F'); + this.setRectangle(dict.get('Rect')); + data.rect = this.rectangle; var color = dict.get('C'); if (isArray(color) && color.length === 3) { @@ -104,6 +104,20 @@ var Annotation = (function AnnotationClosure() { } Annotation.prototype = { + /** + * Set the rectangle. + * + * @public + * @memberof Annotation + * @param {Array} rectangle - The rectangle array with exactly four entries + */ + setRectangle: function Annotation_setRectangle(rectangle) { + if (isArray(rectangle) && rectangle.length === 4) { + this.rectangle = Util.normalizeRect(rectangle); + } else { + this.rectangle = [0, 0, 0, 0]; + } + }, getData: function Annotation_getData() { return this.data; diff --git a/base/shared/util.js b/base/shared/util.js index 0cc03d94..4467d455 100755 --- a/base/shared/util.js +++ b/base/shared/util.js @@ -519,42 +519,33 @@ var Util = PDFJS.Util = (function UtilClosure() { }; // Returns a rectangle [x1, y1, x2, y2] corresponding to the - // intersection of rect1 and rect2. If no intersection, returns 'false' + // intersection of rect1 and rect2. If no intersection, returns 'null' // The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2] Util.intersect = function Util_intersect(rect1, rect2) { - function compare(a, b) { - return a - b; - } - - // Order points along the axes - var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare), - orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare), - result = []; - - rect1 = Util.normalizeRect(rect1); - rect2 = Util.normalizeRect(rect2); - - // X: first and second points belong to different rectangles? - if ((orderedX[0] === rect1[0] && orderedX[1] === rect2[0]) || - (orderedX[0] === rect2[0] && orderedX[1] === rect1[0])) { - // Intersection must be between second and third points - result[0] = orderedX[1]; - result[2] = orderedX[2]; - } else { - return false; + const xLow = Math.max( + Math.min(rect1[0], rect1[2]), + Math.min(rect2[0], rect2[2]) + ); + const xHigh = Math.min( + Math.max(rect1[0], rect1[2]), + Math.max(rect2[0], rect2[2]) + ); + if (xLow > xHigh) { + return null; } - - // Y: first and second points belong to different rectangles? - if ((orderedY[0] === rect1[1] && orderedY[1] === rect2[1]) || - (orderedY[0] === rect2[1] && orderedY[1] === rect1[1])) { - // Intersection must be between second and third points - result[1] = orderedY[1]; - result[3] = orderedY[2]; - } else { - return false; + const yLow = Math.max( + Math.min(rect1[1], rect1[3]), + Math.min(rect2[1], rect2[3]) + ); + const yHigh = Math.min( + Math.max(rect1[1], rect1[3]), + Math.max(rect2[1], rect2[3]) + ); + if (yLow > yHigh) { + return null; } - return result; + return [xLow, yLow, xHigh, yHigh]; }; Util.sign = function Util_sign(num) {