Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] TypeError: Cannot read properties of undefined (reading 'slice') at at Function.Util_normalizeRect [as normalizeRect] #298

Merged
merged 7 commits into from
Mar 26, 2023
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ node_modules/
target/
.idea
.npmrc

package-lock.json
18 changes: 16 additions & 2 deletions base/shared/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
53 changes: 22 additions & 31 deletions base/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down