Use more ES6 syntax in src/core/annotation.js

`let` is converted to `const` where possible.
This commit is contained in:
Tim van der Meij 2019-08-31 17:07:29 +02:00
parent 49018482dc
commit 2866c8a39e
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -47,18 +47,18 @@ class AnnotationFactory {
* @private * @private
*/ */
static _create(xref, ref, pdfManager, idFactory) { static _create(xref, ref, pdfManager, idFactory) {
let dict = xref.fetchIfRef(ref); const dict = xref.fetchIfRef(ref);
if (!isDict(dict)) { if (!isDict(dict)) {
return undefined; return undefined;
} }
let id = isRef(ref) ? ref.toString() : `annot_${idFactory.createObjId()}`; const id = isRef(ref) ? ref.toString() : `annot_${idFactory.createObjId()}`;
// Determine the annotation's subtype. // Determine the annotation's subtype.
let subtype = dict.get('Subtype'); let subtype = dict.get('Subtype');
subtype = isName(subtype) ? subtype.name : null; subtype = isName(subtype) ? subtype.name : null;
// Return the right annotation object based on the subtype and field type. // Return the right annotation object based on the subtype and field type.
let parameters = { const parameters = {
xref, xref,
dict, dict,
subtype, subtype,
@ -181,20 +181,16 @@ function getQuadPoints(dict, rect) {
function getTransformMatrix(rect, bbox, matrix) { function getTransformMatrix(rect, bbox, matrix) {
// 12.5.5: Algorithm: Appearance streams // 12.5.5: Algorithm: Appearance streams
let bounds = Util.getAxialAlignedBoundingBox(bbox, matrix); const [minX, minY, maxX, maxY] =
let minX = bounds[0]; Util.getAxialAlignedBoundingBox(bbox, matrix);
let minY = bounds[1];
let maxX = bounds[2];
let maxY = bounds[3];
if (minX === maxX || minY === maxY) { if (minX === maxX || minY === maxY) {
// From real-life file, bbox was [0, 0, 0, 0]. In this case, // From real-life file, bbox was [0, 0, 0, 0]. In this case,
// just apply the transform for rect // just apply the transform for rect
return [1, 0, 0, 1, rect[0], rect[1]]; return [1, 0, 0, 1, rect[0], rect[1]];
} }
let xRatio = (rect[2] - rect[0]) / (maxX - minX); const xRatio = (rect[2] - rect[0]) / (maxX - minX);
let yRatio = (rect[3] - rect[1]) / (maxY - minY); const yRatio = (rect[3] - rect[1]) / (maxY - minY);
return [ return [
xRatio, xRatio,
0, 0,
@ -355,7 +351,7 @@ class Annotation {
* 4 (CMYK) elements * 4 (CMYK) elements
*/ */
setColor(color) { setColor(color) {
let rgbColor = new Uint8ClampedArray(3); const rgbColor = new Uint8ClampedArray(3);
if (!Array.isArray(color)) { if (!Array.isArray(color)) {
this.color = rgbColor; this.color = rgbColor;
return; return;
@ -405,8 +401,8 @@ class Annotation {
return; return;
} }
if (borderStyle.has('BS')) { if (borderStyle.has('BS')) {
let dict = borderStyle.get('BS'); const dict = borderStyle.get('BS');
let dictType = dict.get('Type'); const dictType = dict.get('Type');
if (!dictType || isName(dictType, 'Border')) { if (!dictType || isName(dictType, 'Border')) {
this.borderStyle.setWidth(dict.get('W'), this.rectangle); this.borderStyle.setWidth(dict.get('W'), this.rectangle);
@ -414,7 +410,7 @@ class Annotation {
this.borderStyle.setDashArray(dict.getArray('D')); this.borderStyle.setDashArray(dict.getArray('D'));
} }
} else if (borderStyle.has('Border')) { } else if (borderStyle.has('Border')) {
let array = borderStyle.getArray('Border'); const array = borderStyle.getArray('Border');
if (Array.isArray(array) && array.length >= 3) { if (Array.isArray(array) && array.length >= 3) {
this.borderStyle.setHorizontalCornerRadius(array[0]); this.borderStyle.setHorizontalCornerRadius(array[0]);
this.borderStyle.setVerticalCornerRadius(array[1]); this.borderStyle.setVerticalCornerRadius(array[1]);
@ -444,13 +440,13 @@ class Annotation {
setAppearance(dict) { setAppearance(dict) {
this.appearance = null; this.appearance = null;
let appearanceStates = dict.get('AP'); const appearanceStates = dict.get('AP');
if (!isDict(appearanceStates)) { if (!isDict(appearanceStates)) {
return; return;
} }
// In case the normal appearance is a stream, then it is used directly. // In case the normal appearance is a stream, then it is used directly.
let normalAppearanceState = appearanceStates.get('N'); const normalAppearanceState = appearanceStates.get('N');
if (isStream(normalAppearanceState)) { if (isStream(normalAppearanceState)) {
this.appearance = normalAppearanceState; this.appearance = normalAppearanceState;
return; return;
@ -461,7 +457,7 @@ class Annotation {
// In case the normal appearance is a dictionary, the `AS` entry provides // In case the normal appearance is a dictionary, the `AS` entry provides
// the key of the stream in this dictionary. // the key of the stream in this dictionary.
let as = dict.get('AS'); const as = dict.get('AS');
if (!isName(as) || !normalAppearanceState.has(as.name)) { if (!isName(as) || !normalAppearanceState.has(as.name)) {
return; return;
} }
@ -473,8 +469,8 @@ class Annotation {
if (!resources) { if (!resources) {
return undefined; return undefined;
} }
let objectLoader = new ObjectLoader(resources, keys, resources.xref);
const objectLoader = new ObjectLoader(resources, keys, resources.xref);
return objectLoader.load().then(function() { return objectLoader.load().then(function() {
return resources; return resources;
}); });
@ -486,24 +482,22 @@ class Annotation {
return Promise.resolve(new OperatorList()); return Promise.resolve(new OperatorList());
} }
let data = this.data; const data = this.data;
let appearanceDict = this.appearance.dict; const appearanceDict = this.appearance.dict;
let resourcesPromise = this.loadResources([ const resourcesPromise = this.loadResources([
'ExtGState', 'ExtGState',
'ColorSpace', 'ColorSpace',
'Pattern', 'Pattern',
'Shading', 'Shading',
'XObject', 'XObject',
'Font', 'Font',
// ProcSet
// Properties
]); ]);
let bbox = appearanceDict.getArray('BBox') || [0, 0, 1, 1]; const bbox = appearanceDict.getArray('BBox') || [0, 0, 1, 1];
let matrix = appearanceDict.getArray('Matrix') || [1, 0, 0, 1, 0, 0]; const matrix = appearanceDict.getArray('Matrix') || [1, 0, 0, 1, 0, 0];
let transform = getTransformMatrix(data.rect, bbox, matrix); const transform = getTransformMatrix(data.rect, bbox, matrix);
return resourcesPromise.then((resources) => { return resourcesPromise.then((resources) => {
let opList = new OperatorList(); const opList = new OperatorList();
opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]); opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
return evaluator.getOperatorList({ return evaluator.getOperatorList({
stream: this.appearance, stream: this.appearance,
@ -624,9 +618,8 @@ class AnnotationBorderStyle {
// shall be numbers that are nonnegative and not all equal to zero. // shall be numbers that are nonnegative and not all equal to zero.
let isValid = true; let isValid = true;
let allZeros = true; let allZeros = true;
for (let i = 0, len = dashArray.length; i < len; i++) { for (const element of dashArray) {
let element = dashArray[i]; const validNumber = (+element >= 0);
let validNumber = (+element >= 0);
if (!validNumber) { if (!validNumber) {
isValid = false; isValid = false;
break; break;
@ -750,8 +743,8 @@ class WidgetAnnotation extends Annotation {
constructor(params) { constructor(params) {
super(params); super(params);
let dict = params.dict; const dict = params.dict;
let data = this.data; const data = this.data;
data.annotationType = AnnotationType.WIDGET; data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict); data.fieldName = this._constructFieldName(dict);
@ -759,7 +752,7 @@ class WidgetAnnotation extends Annotation {
getArray: true, }); getArray: true, });
data.alternativeText = stringToPDFString(dict.get('TU') || ''); data.alternativeText = stringToPDFString(dict.get('TU') || '');
data.defaultAppearance = getInheritableProperty({ dict, key: 'DA', }) || ''; data.defaultAppearance = getInheritableProperty({ dict, key: 'DA', }) || '';
let fieldType = getInheritableProperty({ dict, key: 'FT', }); const fieldType = getInheritableProperty({ dict, key: 'FT', });
data.fieldType = isName(fieldType) ? fieldType.name : null; data.fieldType = isName(fieldType) ? fieldType.name : null;
this.fieldResources = getInheritableProperty({ dict, key: 'DR', }) || this.fieldResources = getInheritableProperty({ dict, key: 'DR', }) ||
Dict.empty; Dict.empty;
@ -804,7 +797,7 @@ class WidgetAnnotation extends Annotation {
// Form the fully qualified field name by appending the partial name to // Form the fully qualified field name by appending the partial name to
// the parent's fully qualified name, separated by a period. // the parent's fully qualified name, separated by a period.
let fieldName = []; const fieldName = [];
if (dict.has('T')) { if (dict.has('T')) {
fieldName.unshift(stringToPDFString(dict.get('T'))); fieldName.unshift(stringToPDFString(dict.get('T')));
} }
@ -887,7 +880,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
return super.getOperatorList(evaluator, task, renderForms); return super.getOperatorList(evaluator, task, renderForms);
} }
let operatorList = new OperatorList(); const operatorList = new OperatorList();
// Even if there is an appearance stream, ignore it. This is the // Even if there is an appearance stream, ignore it. This is the
// behaviour used by Adobe Reader. // behaviour used by Adobe Reader.
@ -895,7 +888,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
return Promise.resolve(operatorList); return Promise.resolve(operatorList);
} }
let stream = new Stream(stringToBytes(this.data.defaultAppearance)); const stream = new Stream(stringToBytes(this.data.defaultAppearance));
return evaluator.getOperatorList({ return evaluator.getOperatorList({
stream, stream,
task, task,
@ -958,27 +951,26 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
// The parent field's `V` entry holds a `Name` object with the appearance // The parent field's `V` entry holds a `Name` object with the appearance
// state of whichever child field is currently in the "on" state. // state of whichever child field is currently in the "on" state.
let fieldParent = params.dict.get('Parent'); const fieldParent = params.dict.get('Parent');
if (isDict(fieldParent) && fieldParent.has('V')) { if (isDict(fieldParent) && fieldParent.has('V')) {
let fieldParentValue = fieldParent.get('V'); const fieldParentValue = fieldParent.get('V');
if (isName(fieldParentValue)) { if (isName(fieldParentValue)) {
this.data.fieldValue = fieldParentValue.name; this.data.fieldValue = fieldParentValue.name;
} }
} }
// The button's value corresponds to its appearance state. // The button's value corresponds to its appearance state.
let appearanceStates = params.dict.get('AP'); const appearanceStates = params.dict.get('AP');
if (!isDict(appearanceStates)) { if (!isDict(appearanceStates)) {
return; return;
} }
let normalAppearanceState = appearanceStates.get('N'); const normalAppearanceState = appearanceStates.get('N');
if (!isDict(normalAppearanceState)) { if (!isDict(normalAppearanceState)) {
return; return;
} }
let keys = normalAppearanceState.getKeys(); for (const key of normalAppearanceState.getKeys()) {
for (let i = 0, ii = keys.length; i < ii; i++) { if (key !== 'Off') {
if (keys[i] !== 'Off') { this.data.buttonValue = key;
this.data.buttonValue = keys[i];
break; break;
} }
} }
@ -1013,9 +1005,9 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
// inherit the options from a parent annotation (issue 8094). // inherit the options from a parent annotation (issue 8094).
this.data.options = []; this.data.options = [];
let options = getInheritableProperty({ dict: params.dict, key: 'Opt', }); const options = getInheritableProperty({ dict: params.dict, key: 'Opt', });
if (Array.isArray(options)) { if (Array.isArray(options)) {
let xref = params.xref; const xref = params.xref;
for (let i = 0, ii = options.length; i < ii; i++) { for (let i = 0, ii = options.length; i < ii; i++) {
let option = xref.fetchIfRef(options[i]); let option = xref.fetchIfRef(options[i]);
let isOptionArray = Array.isArray(option); let isOptionArray = Array.isArray(option);
@ -1094,16 +1086,15 @@ class PopupAnnotation extends Annotation {
this.data.annotationType = AnnotationType.POPUP; this.data.annotationType = AnnotationType.POPUP;
let dict = parameters.dict; let parentItem = parameters.dict.get('Parent');
let parentItem = dict.get('Parent');
if (!parentItem) { if (!parentItem) {
warn('Popup annotation has a missing or invalid parent annotation.'); warn('Popup annotation has a missing or invalid parent annotation.');
return; return;
} }
let parentSubtype = parentItem.get('Subtype'); const parentSubtype = parentItem.get('Subtype');
this.data.parentType = isName(parentSubtype) ? parentSubtype.name : null; this.data.parentType = isName(parentSubtype) ? parentSubtype.name : null;
const rawParent = dict.getRaw('Parent'); const rawParent = parameters.dict.getRaw('Parent');
this.data.parentId = isRef(rawParent) ? rawParent.toString() : null; this.data.parentId = isRef(rawParent) ? rawParent.toString() : null;
const rt = parentItem.get('RT'); const rt = parentItem.get('RT');
@ -1132,7 +1123,7 @@ class PopupAnnotation extends Annotation {
// that is most likely a bug. Fallback to inherit the flags from the parent // that is most likely a bug. Fallback to inherit the flags from the parent
// annotation (this is consistent with the behaviour in Adobe Reader). // annotation (this is consistent with the behaviour in Adobe Reader).
if (!this.viewable) { if (!this.viewable) {
let parentFlags = parentItem.get('F'); const parentFlags = parentItem.get('F');
if (this._isViewable(parentFlags)) { if (this._isViewable(parentFlags)) {
this.setFlags(parentFlags); this.setFlags(parentFlags);
} }
@ -1157,8 +1148,8 @@ class LineAnnotation extends MarkupAnnotation {
this.data.annotationType = AnnotationType.LINE; this.data.annotationType = AnnotationType.LINE;
let dict = parameters.dict; this.data.lineCoordinates =
this.data.lineCoordinates = Util.normalizeRect(dict.getArray('L')); Util.normalizeRect(parameters.dict.getArray('L'));
} }
} }
@ -1187,9 +1178,7 @@ class PolylineAnnotation extends MarkupAnnotation {
// The vertices array is an array of numbers representing the alternating // The vertices array is an array of numbers representing the alternating
// horizontal and vertical coordinates, respectively, of each vertex. // horizontal and vertical coordinates, respectively, of each vertex.
// Convert this to an array of objects with x and y coordinates. // Convert this to an array of objects with x and y coordinates.
let dict = parameters.dict; const rawVertices = parameters.dict.getArray('Vertices');
let rawVertices = dict.getArray('Vertices');
this.data.vertices = []; this.data.vertices = [];
for (let i = 0, ii = rawVertices.length; i < ii; i += 2) { for (let i = 0, ii = rawVertices.length; i < ii; i += 2) {
this.data.vertices.push({ this.data.vertices.push({
@ -1223,10 +1212,8 @@ class InkAnnotation extends MarkupAnnotation {
this.data.annotationType = AnnotationType.INK; this.data.annotationType = AnnotationType.INK;
let dict = parameters.dict;
const xref = parameters.xref; const xref = parameters.xref;
const originalInkLists = parameters.dict.getArray('InkList');
let originalInkLists = dict.getArray('InkList');
this.data.inkLists = []; this.data.inkLists = [];
for (let i = 0, ii = originalInkLists.length; i < ii; ++i) { for (let i = 0, ii = originalInkLists.length; i < ii; ++i) {
// The raw ink lists array contains arrays of numbers representing // The raw ink lists array contains arrays of numbers representing
@ -1308,7 +1295,7 @@ class FileAttachmentAnnotation extends MarkupAnnotation {
constructor(parameters) { constructor(parameters) {
super(parameters); super(parameters);
let file = new FileSpec(parameters.dict.get('FS'), parameters.xref); const file = new FileSpec(parameters.dict.get('FS'), parameters.xref);
this.data.annotationType = AnnotationType.FILEATTACHMENT; this.data.annotationType = AnnotationType.FILEATTACHMENT;
this.data.file = file.serializable; this.data.file = file.serializable;