Skip to content

Commit

Permalink
[WIP] Truncate ridiculously large Annotation widths to the Rect e…
Browse files Browse the repository at this point in the history
…ntry, to prevent the `annotationLayer` from rendering it over the surrounding document (bug 1552113)

*TODO:* Add tests once the general approach has been accepted.

Fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1552113
  • Loading branch information
Snuffleupagus committed May 31, 2019
1 parent 209e420 commit 049f003
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import {
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
AnnotationType, isString, OPS, stringToBytes, stringToPDFString, Util, warn
AnnotationType, assert, isString, OPS, stringToBytes, stringToPDFString, Util,
warn
} from '../shared/util';
import { Catalog, FileSpec, ObjectLoader } from './obj';
import { Dict, isDict, isName, isRef, isStream } from './primitives';
Expand Down Expand Up @@ -361,6 +362,11 @@ class Annotation {
* @param {Dict} borderStyle - The border style dictionary
*/
setBorderStyle(borderStyle) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(this.rectangle, 'setRectangle must have been called previously.');
}

this.borderStyle = new AnnotationBorderStyle();
if (!isDict(borderStyle)) {
return;
Expand All @@ -370,7 +376,7 @@ class Annotation {
let dictType = dict.get('Type');

if (!dictType || isName(dictType, 'Border')) {
this.borderStyle.setWidth(dict.get('W'));
this.borderStyle.setWidth(dict.get('W'), this.rectangle);
this.borderStyle.setStyle(dict.get('S'));
this.borderStyle.setDashArray(dict.getArray('D'));
}
Expand All @@ -379,7 +385,7 @@ class Annotation {
if (Array.isArray(array) && array.length >= 3) {
this.borderStyle.setHorizontalCornerRadius(array[0]);
this.borderStyle.setVerticalCornerRadius(array[1]);
this.borderStyle.setWidth(array[2]);
this.borderStyle.setWidth(array[2], this.rectangle);

if (array.length === 4) { // Dash array available
this.borderStyle.setDashArray(array[3]);
Expand Down Expand Up @@ -497,16 +503,39 @@ class AnnotationBorderStyle {
*
* @public
* @memberof AnnotationBorderStyle
* @param {integer} width - The width
* @param {integer} width - The width.
* @param {Array} rect - The annotation `Rect` entry.
*/
setWidth(width) {
setWidth(width, rect = [0, 0, 0, 0]) {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(Array.isArray(rect) && rect.length === 4,
'A valid `rect` parameter must be provided.');
}

// Some corrupt PDF generators may provide the width as a `Name`,
// rather than as a number (fixes issue 10385).
if (isName(width)) {
this.width = 0; // This is consistent with the behaviour in Adobe Reader.
return;
}
if (Number.isInteger(width)) {
if (width > 0) {
const maxWidth = (rect[2] - rect[0]) / 2;
const maxHeight = (rect[3] - rect[1]) / 2;

// Truncate ridiculously large `width`s to the `Rect` entry of the
// annotation, to prevent the `annotationLayer` from rendering it
// over the surrounding document (fixes bug1552113.pdf).
//
// This is consistent with the behaviour in Adobe Reader.
if ((maxWidth > 0 && maxHeight > 0) &&
(width > maxWidth || width > maxHeight)) {
warn('AnnotationBorderStyle.setWidth - truncating large width: ' +
width);
width = Math.min(maxWidth, maxHeight);
}
}
this.width = width;
}
}
Expand Down

0 comments on commit 049f003

Please sign in to comment.