Convert the Page class in src/core/document.js to ES6 syntax

This commit is contained in:
Tim van der Meij 2018-12-29 15:46:22 +01:00
parent 85363f4566
commit 612fc9fcc2
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -28,17 +28,16 @@ import { OperatorList } from './operator_list';
import { PartialEvaluator } from './evaluator'; import { PartialEvaluator } from './evaluator';
import { PDFFunctionFactory } from './function'; import { PDFFunctionFactory } from './function';
var Page = (function PageClosure() { const DEFAULT_USER_UNIT = 1.0;
const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
var DEFAULT_USER_UNIT = 1.0;
var LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
function isAnnotationRenderable(annotation, intent) { function isAnnotationRenderable(annotation, intent) {
return (intent === 'display' && annotation.viewable) || return (intent === 'display' && annotation.viewable) ||
(intent === 'print' && annotation.printable); (intent === 'print' && annotation.printable);
} }
function Page({ pdfManager, xref, pageIndex, pageDict, ref, fontCache, class Page {
constructor({ pdfManager, xref, pageIndex, pageDict, ref, fontCache,
builtInCMapCache, pdfFunctionFactory, }) { builtInCMapCache, pdfFunctionFactory, }) {
this.pdfManager = pdfManager; this.pdfManager = pdfManager;
this.pageIndex = pageIndex; this.pageIndex = pageIndex;
@ -51,8 +50,8 @@ var Page = (function PageClosure() {
this.evaluatorOptions = pdfManager.evaluatorOptions; this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null; this.resourcesPromise = null;
var uniquePrefix = 'p' + this.pageIndex + '_'; const uniquePrefix = `p${this.pageIndex}_`;
var idCounters = { const idCounters = {
obj: 0, obj: 0,
}; };
this.idFactory = { this.idFactory = {
@ -62,12 +61,11 @@ var Page = (function PageClosure() {
}; };
} }
Page.prototype = {
/** /**
* @private * @private
*/ */
_getInheritableProperty(key, getArray = false) { _getInheritableProperty(key, getArray = false) {
let value = getInheritableProperty({ dict: this.pageDict, key, getArray, const value = getInheritableProperty({ dict: this.pageDict, key, getArray,
stopWhenFound: false, }); stopWhenFound: false, });
if (!Array.isArray(value)) { if (!Array.isArray(value)) {
return value; return value;
@ -76,124 +74,123 @@ var Page = (function PageClosure() {
return value[0]; return value[0];
} }
return Dict.merge(this.xref, value); return Dict.merge(this.xref, value);
}, }
get content() { get content() {
return this.pageDict.get('Contents'); return this.pageDict.get('Contents');
}, }
get resources() { get resources() {
// For robustness: The spec states that a \Resources entry has to be // For robustness: The spec states that a \Resources entry has to be
// present, but can be empty. Some document omit it still, in this case // present, but can be empty. Some documents still omit it; in this case
// we return an empty dictionary. // we return an empty dictionary.
return shadow(this, 'resources', return shadow(this, 'resources',
this._getInheritableProperty('Resources') || Dict.empty); this._getInheritableProperty('Resources') || Dict.empty);
}, }
get mediaBox() { get mediaBox() {
var mediaBox = this._getInheritableProperty('MediaBox', const mediaBox = this._getInheritableProperty('MediaBox',
/* getArray = */ true); /* getArray = */ true);
// Reset invalid media box to letter size. // Reset invalid media box to letter size.
if (!Array.isArray(mediaBox) || mediaBox.length !== 4) { if (!Array.isArray(mediaBox) || mediaBox.length !== 4) {
return shadow(this, 'mediaBox', LETTER_SIZE_MEDIABOX); return shadow(this, 'mediaBox', LETTER_SIZE_MEDIABOX);
} }
return shadow(this, 'mediaBox', mediaBox); return shadow(this, 'mediaBox', mediaBox);
}, }
get cropBox() { get cropBox() {
var cropBox = this._getInheritableProperty('CropBox', const cropBox = this._getInheritableProperty('CropBox',
/* getArray = */ true); /* getArray = */ true);
// Reset invalid crop box to media box. // Reset invalid crop box to media box.
if (!Array.isArray(cropBox) || cropBox.length !== 4) { if (!Array.isArray(cropBox) || cropBox.length !== 4) {
return shadow(this, 'cropBox', this.mediaBox); return shadow(this, 'cropBox', this.mediaBox);
} }
return shadow(this, 'cropBox', cropBox); return shadow(this, 'cropBox', cropBox);
}, }
get userUnit() { get userUnit() {
var obj = this.pageDict.get('UserUnit'); let obj = this.pageDict.get('UserUnit');
if (!isNum(obj) || obj <= 0) { if (!isNum(obj) || obj <= 0) {
obj = DEFAULT_USER_UNIT; obj = DEFAULT_USER_UNIT;
} }
return shadow(this, 'userUnit', obj); return shadow(this, 'userUnit', obj);
}, }
get view() { get view() {
// From the spec, 6th ed., p.963: // From the spec, 6th ed., p.963:
// "The crop, bleed, trim, and art boxes should not ordinarily // "The crop, bleed, trim, and art boxes should not ordinarily
// extend beyond the boundaries of the media box. If they do, they are // extend beyond the boundaries of the media box. If they do, they are
// effectively reduced to their intersection with the media box." // effectively reduced to their intersection with the media box."
var mediaBox = this.mediaBox, cropBox = this.cropBox; const mediaBox = this.mediaBox, cropBox = this.cropBox;
if (mediaBox === cropBox) { if (mediaBox === cropBox) {
return shadow(this, 'view', mediaBox); return shadow(this, 'view', mediaBox);
} }
var intersection = Util.intersect(cropBox, mediaBox);
const intersection = Util.intersect(cropBox, mediaBox);
return shadow(this, 'view', intersection || mediaBox); return shadow(this, 'view', intersection || mediaBox);
}, }
get rotate() { get rotate() {
var rotate = this._getInheritableProperty('Rotate') || 0; let rotate = this._getInheritableProperty('Rotate') || 0;
// Normalize rotation so it's a multiple of 90 and between 0 and 270
// Normalize rotation so it's a multiple of 90 and between 0 and 270.
if (rotate % 90 !== 0) { if (rotate % 90 !== 0) {
rotate = 0; rotate = 0;
} else if (rotate >= 360) { } else if (rotate >= 360) {
rotate = rotate % 360; rotate = rotate % 360;
} else if (rotate < 0) { } else if (rotate < 0) {
// The spec doesn't cover negatives, assume its counterclockwise // The spec doesn't cover negatives. Assume it's counterclockwise
// rotation. The following is the other implementation of modulo. // rotation. The following is the other implementation of modulo.
rotate = ((rotate % 360) + 360) % 360; rotate = ((rotate % 360) + 360) % 360;
} }
return shadow(this, 'rotate', rotate); return shadow(this, 'rotate', rotate);
}, }
getContentStream() {
const content = this.content;
let stream;
getContentStream: function Page_getContentStream() {
var content = this.content;
var stream;
if (Array.isArray(content)) { if (Array.isArray(content)) {
// fetching items // Fetching the individual streams from the array.
var xref = this.xref; const xref = this.xref;
var i, n = content.length; const streams = [];
var streams = []; for (const stream of content) {
for (i = 0; i < n; ++i) { streams.push(xref.fetchIfRef(stream));
streams.push(xref.fetchIfRef(content[i]));
} }
stream = new StreamsSequenceStream(streams); stream = new StreamsSequenceStream(streams);
} else if (isStream(content)) { } else if (isStream(content)) {
stream = content; stream = content;
} else { } else {
// replacing non-existent page content with empty one // Replace non-existent page content with empty content.
stream = new NullStream(); stream = new NullStream();
} }
return stream; return stream;
}, }
loadResources: function Page_loadResources(keys) { loadResources(keys) {
if (!this.resourcesPromise) { if (!this.resourcesPromise) {
// TODO: add async `_getInheritableProperty` and remove this. // TODO: add async `_getInheritableProperty` and remove this.
this.resourcesPromise = this.pdfManager.ensure(this, 'resources'); this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
} }
return this.resourcesPromise.then(() => { return this.resourcesPromise.then(() => {
let objectLoader = new ObjectLoader(this.resources, keys, this.xref); const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
return objectLoader.load(); return objectLoader.load();
}); });
}, }
getOperatorList({ handler, task, intent, renderInteractiveForms, }) { getOperatorList({ handler, task, intent, renderInteractiveForms, }) {
var contentStreamPromise = this.pdfManager.ensure(this, const contentStreamPromise = this.pdfManager.ensure(this,
'getContentStream'); 'getContentStream');
var resourcesPromise = this.loadResources([ const resourcesPromise = this.loadResources([
'ExtGState', 'ExtGState',
'ColorSpace', 'ColorSpace',
'Pattern', 'Pattern',
'Shading', 'Shading',
'XObject', 'XObject',
'Font' 'Font',
// ProcSet
// Properties
]); ]);
var partialEvaluator = new PartialEvaluator({ const partialEvaluator = new PartialEvaluator({
pdfManager: this.pdfManager, pdfManager: this.pdfManager,
xref: this.xref, xref: this.xref,
handler, handler,
@ -205,15 +202,16 @@ var Page = (function PageClosure() {
pdfFunctionFactory: this.pdfFunctionFactory, pdfFunctionFactory: this.pdfFunctionFactory,
}); });
var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]); const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
var pageListPromise = dataPromises.then(([contentStream]) => { const pageListPromise = dataPromises.then(([contentStream]) => {
var opList = new OperatorList(intent, handler, this.pageIndex); const opList = new OperatorList(intent, handler, this.pageIndex);
handler.send('StartRenderPage', { handler.send('StartRenderPage', {
transparency: partialEvaluator.hasBlendModes(this.resources), transparency: partialEvaluator.hasBlendModes(this.resources),
pageIndex: this.pageIndex, pageIndex: this.pageIndex,
intent, intent,
}); });
return partialEvaluator.getOperatorList({ return partialEvaluator.getOperatorList({
stream: contentStream, stream: contentStream,
task, task,
@ -235,40 +233,39 @@ var Page = (function PageClosure() {
// Collect the operator list promises for the annotations. Each promise // Collect the operator list promises for the annotations. Each promise
// is resolved with the complete operator list for a single annotation. // is resolved with the complete operator list for a single annotation.
var i, ii, opListPromises = []; const opListPromises = [];
for (i = 0, ii = annotations.length; i < ii; i++) { for (const annotation of annotations) {
if (isAnnotationRenderable(annotations[i], intent)) { if (isAnnotationRenderable(annotation, intent)) {
opListPromises.push(annotations[i].getOperatorList( opListPromises.push(annotation.getOperatorList(
partialEvaluator, task, renderInteractiveForms)); partialEvaluator, task, renderInteractiveForms));
} }
} }
return Promise.all(opListPromises).then(function(opLists) { return Promise.all(opListPromises).then(function(opLists) {
pageOpList.addOp(OPS.beginAnnotations, []); pageOpList.addOp(OPS.beginAnnotations, []);
for (i = 0, ii = opLists.length; i < ii; i++) { for (const opList of opLists) {
pageOpList.addOpList(opLists[i]); pageOpList.addOpList(opList);
} }
pageOpList.addOp(OPS.endAnnotations, []); pageOpList.addOp(OPS.endAnnotations, []);
pageOpList.flush(true); pageOpList.flush(true);
return pageOpList; return pageOpList;
}); });
}); });
}, }
extractTextContent({ handler, task, normalizeWhitespace, extractTextContent({ handler, task, normalizeWhitespace, sink,
sink, combineTextItems, }) { combineTextItems, }) {
var contentStreamPromise = this.pdfManager.ensure(this, const contentStreamPromise = this.pdfManager.ensure(this,
'getContentStream'); 'getContentStream');
var resourcesPromise = this.loadResources([ const resourcesPromise = this.loadResources([
'ExtGState', 'ExtGState',
'XObject', 'XObject',
'Font' 'Font',
]); ]);
var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]); const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
return dataPromises.then(([contentStream]) => { return dataPromises.then(([contentStream]) => {
var partialEvaluator = new PartialEvaluator({ const partialEvaluator = new PartialEvaluator({
pdfManager: this.pdfManager, pdfManager: this.pdfManager,
xref: this.xref, xref: this.xref,
handler, handler,
@ -289,11 +286,11 @@ var Page = (function PageClosure() {
sink, sink,
}); });
}); });
}, }
getAnnotationsData(intent) { getAnnotationsData(intent) {
return this._parsedAnnotations.then(function(annotations) { return this._parsedAnnotations.then(function(annotations) {
let annotationsData = []; const annotationsData = [];
for (let i = 0, ii = annotations.length; i < ii; i++) { for (let i = 0, ii = annotations.length; i < ii; i++) {
if (!intent || isAnnotationRenderable(annotations[i], intent)) { if (!intent || isAnnotationRenderable(annotations[i], intent)) {
annotationsData.push(annotations[i].data); annotationsData.push(annotations[i].data);
@ -301,12 +298,12 @@ var Page = (function PageClosure() {
} }
return annotationsData; return annotationsData;
}); });
}, }
get annotations() { get annotations() {
return shadow(this, 'annotations', return shadow(this, 'annotations',
this._getInheritableProperty('Annots') || []); this._getInheritableProperty('Annots') || []);
}, }
get _parsedAnnotations() { get _parsedAnnotations() {
const parsedAnnotations = const parsedAnnotations =
@ -329,11 +326,8 @@ var Page = (function PageClosure() {
}); });
return shadow(this, '_parsedAnnotations', parsedAnnotations); return shadow(this, '_parsedAnnotations', parsedAnnotations);
}, }
}; }
return Page;
})();
/** /**
* The `PDFDocument` holds all the data of the PDF file. Compared to the * The `PDFDocument` holds all the data of the PDF file. Compared to the