[api-minor] Refactor the annotation code to be asynchronous

This commit is the first step towards implementing parsing for the
appearance streams of annotations.

Co-authored-by: Jonas Jenwald <jonas.jenwald@gmail.com>
Co-authored-by: Tim van der Meij <timvandermeij@gmail.com>
This commit is contained in:
dmitryskey 2018-03-20 20:43:40 -04:00 committed by Tim van der Meij
parent c8ee63319d
commit 3741becb9b
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
4 changed files with 482 additions and 432 deletions

View File

@ -26,13 +26,25 @@ import { Stream } from './stream';
class AnnotationFactory { class AnnotationFactory {
/** /**
* Create an `Annotation` object of the correct type for the given reference
* to an annotation dictionary. This yields a promise that is resolved when
* the `Annotation` object is constructed.
*
* @param {XRef} xref * @param {XRef} xref
* @param {Object} ref * @param {Object} ref
* @param {PDFManager} pdfManager * @param {PDFManager} pdfManager
* @param {Object} idFactory * @param {Object} idFactory
* @returns {Annotation} * @return {Promise} A promise that is resolved with an {Annotation} instance.
*/ */
static create(xref, ref, pdfManager, idFactory) { static create(xref, ref, pdfManager, idFactory) {
return pdfManager.ensure(this, '_create',
[xref, ref, pdfManager, idFactory]);
}
/**
* @private
*/
static _create(xref, ref, pdfManager, idFactory) {
let dict = xref.fetchIfRef(ref); let dict = xref.fetchIfRef(ref);
if (!isDict(dict)) { if (!isDict(dict)) {
return; return;

View File

@ -16,7 +16,7 @@
import { import {
assert, FormatError, getInheritableProperty, info, isArrayBuffer, isNum, assert, FormatError, getInheritableProperty, info, isArrayBuffer, isNum,
isSpace, isString, MissingDataException, OPS, shadow, stringToBytes, isSpace, isString, MissingDataException, OPS, shadow, stringToBytes,
stringToPDFString, Util stringToPDFString, Util, warn
} from '../shared/util'; } from '../shared/util';
import { Catalog, ObjectLoader, XRef } from './obj'; import { Catalog, ObjectLoader, XRef } from './obj';
import { Dict, isDict, isName, isStream, Ref } from './primitives'; import { Dict, isDict, isName, isStream, Ref } from './primitives';
@ -226,8 +226,7 @@ var Page = (function PageClosure() {
// Fetch the page's annotations and add their operator lists to the // Fetch the page's annotations and add their operator lists to the
// page's operator list to render them. // page's operator list to render them.
var annotationsPromise = this.pdfManager.ensure(this, 'annotations'); return Promise.all([pageListPromise, this._parsedAnnotations]).then(
return Promise.all([pageListPromise, annotationsPromise]).then(
function ([pageOpList, annotations]) { function ([pageOpList, annotations]) {
if (annotations.length === 0) { if (annotations.length === 0) {
pageOpList.flush(true); pageOpList.flush(true);
@ -292,30 +291,44 @@ var Page = (function PageClosure() {
}); });
}, },
getAnnotationsData: function Page_getAnnotationsData(intent) { getAnnotationsData(intent) {
var annotations = this.annotations; return this._parsedAnnotations.then(function(annotations) {
var annotationsData = []; let annotationsData = [];
for (var i = 0, n = annotations.length; i < n; ++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);
}
} }
} return annotationsData;
return annotationsData; });
}, },
get annotations() { get annotations() {
var annotations = []; return shadow(this, 'annotations',
var annotationRefs = this._getInheritableProperty('Annots') || []; this._getInheritableProperty('Annots') || []);
for (var i = 0, n = annotationRefs.length; i < n; ++i) { },
var annotationRef = annotationRefs[i];
var annotation = AnnotationFactory.create(this.xref, annotationRef, get _parsedAnnotations() {
this.pdfManager, const parsedAnnotations =
this.idFactory); this.pdfManager.ensure(this, 'annotations').then(() => {
if (annotation) { const annotationRefs = this.annotations;
annotations.push(annotation); const annotationPromises = [];
} for (let i = 0, ii = annotationRefs.length; i < ii; i++) {
} annotationPromises.push(AnnotationFactory.create(
return shadow(this, 'annotations', annotations); this.xref, annotationRefs[i], this.pdfManager, this.idFactory));
}
return Promise.all(annotationPromises).then(function(annotations) {
return annotations.filter(function isDefined(annotation) {
return !!annotation;
});
}, function(reason) {
warn(`_parsedAnnotations: "${reason}".`);
return [];
});
});
return shadow(this, '_parsedAnnotations', parsedAnnotations);
}, },
}; };

View File

@ -723,9 +723,9 @@ var WorkerMessageHandler = {
} }
); );
handler.on('GetAnnotations', function wphSetupGetAnnotations(data) { handler.on('GetAnnotations', function({ pageIndex, intent, }) {
return pdfManager.getPage(data.pageIndex).then(function(page) { return pdfManager.getPage(pageIndex).then(function(page) {
return pdfManager.ensure(page, 'getAnnotationsData', [data.intent]); return page.getAnnotationsData(intent);
}); });
}); });

File diff suppressed because it is too large Load Diff