Merge pull request #10233 from Snuffleupagus/PDFObjects-class

[api-minor] Convert `PDFObjects`, in `src/display/api.js`, to an ES6 class
This commit is contained in:
Tim van der Meij 2018-11-08 23:22:48 +01:00 committed by GitHub
commit 0dfedacc6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 98 deletions

View File

@ -447,8 +447,9 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
element.style.display = 'table-cell'; element.style.display = 'table-cell';
let font = null; let font = null;
if (this.data.fontRefName) { if (this.data.fontRefName &&
font = this.page.commonObjs.getData(this.data.fontRefName); this.page.commonObjs.has(this.data.fontRefName)) {
font = this.page.commonObjs.get(this.data.fontRefName);
} }
this._setTextStyle(element, font); this._setTextStyle(element, font);
} }

View File

@ -1885,7 +1885,7 @@ class WorkerTransport {
} }
const [id, type, exportedData] = data; const [id, type, exportedData] = data;
if (this.commonObjs.hasData(id)) { if (this.commonObjs.has(id)) {
return; return;
} }
@ -1937,7 +1937,7 @@ class WorkerTransport {
const [id, pageIndex, type, imageData] = data; const [id, pageIndex, type, imageData] = data;
const pageProxy = this.pageCache[pageIndex]; const pageProxy = this.pageCache[pageIndex];
if (pageProxy.objs.hasData(id)) { if (pageProxy.objs.has(id)) {
return; return;
} }
@ -2205,108 +2205,77 @@ class WorkerTransport {
} }
/** /**
* A PDF document and page is built of many objects. E.g. there are objects * A PDF document and page is built of many objects. E.g. there are objects for
* for fonts, images, rendering code and such. These objects might get processed * fonts, images, rendering code, etc. These objects may get processed inside of
* inside of a worker. The `PDFObjects` implements some basic functions to * a worker. This class implements some basic methods to manage these objects.
* manage these objects.
* @ignore * @ignore
*/ */
var PDFObjects = (function PDFObjectsClosure() { class PDFObjects {
function PDFObjects() { constructor() {
this.objs = Object.create(null); this._objs = Object.create(null);
} }
PDFObjects.prototype = { /**
/** * Ensures there is an object defined for `objId`.
* Internal function. * @private
* Ensures there is an object defined for `objId`. */
*/ _ensureObj(objId) {
ensureObj: function PDFObjects_ensureObj(objId) { if (this._objs[objId]) {
if (this.objs[objId]) { return this._objs[objId];
return this.objs[objId]; }
} return this._objs[objId] = {
capability: createPromiseCapability(),
data: null,
resolved: false,
};
}
var obj = { /**
capability: createPromiseCapability(), * If called *without* callback, this returns the data of `objId` but the
data: null, * object needs to be resolved. If it isn't, this method throws.
resolved: false, *
}; * If called *with* a callback, the callback is called with the data of the
this.objs[objId] = obj; * object once the object is resolved. That means, if you call this method
* and the object is already resolved, the callback gets called right away.
*/
get(objId, callback = null) {
// If there is a callback, then the get can be async and the object is
// not required to be resolved right now.
if (callback) {
this._ensureObj(objId).capability.promise.then(callback);
return null;
}
// If there isn't a callback, the user expects to get the resolved data
// directly.
const obj = this._objs[objId];
// If there isn't an object yet or the object isn't resolved, then the
// data isn't ready yet!
if (!obj || !obj.resolved) {
throw new Error(`Requesting object that isn't resolved yet ${objId}.`);
}
return obj.data;
}
return obj; has(objId) {
}, const obj = this._objs[objId];
return (obj ? obj.resolved : false);
}
/** /**
* If called *without* callback, this returns the data of `objId` but the * Resolves the object `objId` with optional `data`.
* object needs to be resolved. If it isn't, this function throws. */
* resolve(objId, data) {
* If called *with* a callback, the callback is called with the data of the const obj = this._ensureObj(objId);
* object once the object is resolved. That means, if you call this
* function and the object is already resolved, the callback gets called
* right away.
*/
get: function PDFObjects_get(objId, callback) {
// If there is a callback, then the get can be async and the object is
// not required to be resolved right now
if (callback) {
this.ensureObj(objId).capability.promise.then(callback);
return null;
}
// If there isn't a callback, the user expects to get the resolved data obj.resolved = true;
// directly. obj.data = data;
var obj = this.objs[objId]; obj.capability.resolve(data);
}
// If there isn't an object yet or the object isn't resolved, then the clear() {
// data isn't ready yet! this._objs = Object.create(null);
if (!obj || !obj.resolved) { }
throw new Error(`Requesting object that isn't resolved yet ${objId}`); }
}
return obj.data;
},
/**
* Resolves the object `objId` with optional `data`.
*/
resolve: function PDFObjects_resolve(objId, data) {
var obj = this.ensureObj(objId);
obj.resolved = true;
obj.data = data;
obj.capability.resolve(data);
},
isResolved: function PDFObjects_isResolved(objId) {
var objs = this.objs;
if (!objs[objId]) {
return false;
}
return objs[objId].resolved;
},
hasData: function PDFObjects_hasData(objId) {
return this.isResolved(objId);
},
/**
* Returns the data of `objId` if object exists, null otherwise.
*/
getData: function PDFObjects_getData(objId) {
var objs = this.objs;
if (!objs[objId] || !objs[objId].resolved) {
return null;
}
return objs[objId].data;
},
clear: function PDFObjects_clear() {
this.objs = Object.create(null);
},
};
return PDFObjects;
})();
/** /**
* Allows controlling of the rendering tasks. * Allows controlling of the rendering tasks.

View File

@ -810,7 +810,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
// If the promise isn't resolved yet, add the continueCallback // If the promise isn't resolved yet, add the continueCallback
// to the promise and bail out. // to the promise and bail out.
if (!objsPool.isResolved(depObjId)) { if (!objsPool.has(depObjId)) {
objsPool.get(depObjId, continueCallback); objsPool.get(depObjId, continueCallback);
return i; return i;
} }