mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Replace direct lookup of uniquePrefix
/idCounters
, in Page
instances, with an idFactory
containing an createObjId
method instead
We're currently making use of `uniquePrefix`/`idCounters` in multiple files, to create unique object id's, and adding a new occurrence of them requires some care to ensure that an object id isn't accidentally reused. Furthermore, having to pass around multiple parameters as we currently do seem like something you want to avoid. Instead, this patch adds a factory which means that there's only *one* thing that needs to be passed around. And since it's now only necessary to call a method in order to obtain a unique object id, the details are thus abstracted away at the call-sites which avoids accidental reuse of object id's. To test that this works as expected a very simple `Page` unit-test is added, and the existing `Annotation layer` tests are also adjusted slightly.
This commit is contained in:
parent
e259bc2c16
commit
642d8621ef
7 changed files with 252 additions and 155 deletions
|
@ -65,18 +65,15 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ {
|
|||
* @param {XRef} xref
|
||||
* @param {Object} ref
|
||||
* @param {PDFManager} pdfManager
|
||||
* @param {string} uniquePrefix
|
||||
* @param {Object} idCounters
|
||||
* @param {Object} idFactory
|
||||
* @returns {Annotation}
|
||||
*/
|
||||
create: function AnnotationFactory_create(xref, ref, pdfManager,
|
||||
uniquePrefix, idCounters) {
|
||||
create: function AnnotationFactory_create(xref, ref, pdfManager, idFactory) {
|
||||
var dict = xref.fetchIfRef(ref);
|
||||
if (!isDict(dict)) {
|
||||
return;
|
||||
}
|
||||
var id = isRef(ref) ? ref.toString() :
|
||||
'annot_' + (uniquePrefix || '') + (++idCounters.obj);
|
||||
var id = isRef(ref) ? ref.toString() : 'annot_' + idFactory.createObjId();
|
||||
|
||||
// Determine the annotation's subtype.
|
||||
var subtype = dict.get('Subtype');
|
||||
|
|
|
@ -78,12 +78,18 @@ var Page = (function PageClosure() {
|
|||
this.xref = xref;
|
||||
this.ref = ref;
|
||||
this.fontCache = fontCache;
|
||||
this.uniquePrefix = 'p' + this.pageIndex + '_';
|
||||
this.idCounters = {
|
||||
obj: 0
|
||||
};
|
||||
this.evaluatorOptions = pdfManager.evaluatorOptions;
|
||||
this.resourcesPromise = null;
|
||||
|
||||
var uniquePrefix = 'p' + this.pageIndex + '_';
|
||||
var idCounters = {
|
||||
obj: 0,
|
||||
};
|
||||
this.idFactory = {
|
||||
createObjId: function () {
|
||||
return uniquePrefix + (++idCounters.obj);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Page.prototype = {
|
||||
|
@ -240,8 +246,7 @@ var Page = (function PageClosure() {
|
|||
|
||||
var partialEvaluator = new PartialEvaluator(pdfManager, this.xref,
|
||||
handler, this.pageIndex,
|
||||
this.uniquePrefix,
|
||||
this.idCounters,
|
||||
this.idFactory,
|
||||
this.fontCache,
|
||||
this.evaluatorOptions);
|
||||
|
||||
|
@ -308,8 +313,7 @@ var Page = (function PageClosure() {
|
|||
var contentStream = data[0];
|
||||
var partialEvaluator = new PartialEvaluator(pdfManager, self.xref,
|
||||
handler, self.pageIndex,
|
||||
self.uniquePrefix,
|
||||
self.idCounters,
|
||||
self.idFactory,
|
||||
self.fontCache,
|
||||
self.evaluatorOptions);
|
||||
|
||||
|
@ -345,8 +349,7 @@ var Page = (function PageClosure() {
|
|||
var annotationRef = annotationRefs[i];
|
||||
var annotation = annotationFactory.create(this.xref, annotationRef,
|
||||
this.pdfManager,
|
||||
this.uniquePrefix,
|
||||
this.idCounters);
|
||||
this.idFactory);
|
||||
if (annotation) {
|
||||
annotations.push(annotation);
|
||||
}
|
||||
|
|
|
@ -169,13 +169,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
};
|
||||
|
||||
function PartialEvaluator(pdfManager, xref, handler, pageIndex,
|
||||
uniquePrefix, idCounters, fontCache, options) {
|
||||
idFactory, fontCache, options) {
|
||||
this.pdfManager = pdfManager;
|
||||
this.xref = xref;
|
||||
this.handler = handler;
|
||||
this.pageIndex = pageIndex;
|
||||
this.uniquePrefix = uniquePrefix;
|
||||
this.idCounters = idCounters;
|
||||
this.idFactory = idFactory;
|
||||
this.fontCache = fontCache;
|
||||
this.options = options || DefaultPartialEvaluatorOptions;
|
||||
}
|
||||
|
@ -391,8 +390,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
|
||||
// If there is no imageMask, create the PDFImage and a lot
|
||||
// of image processing can be done here.
|
||||
var uniquePrefix = (this.uniquePrefix || '');
|
||||
var objId = 'img_' + uniquePrefix + (++this.idCounters.obj);
|
||||
var objId = 'img_' + this.idFactory.createObjId();
|
||||
operatorList.addDependency(objId);
|
||||
args = [objId, w, h];
|
||||
|
||||
|
@ -733,7 +731,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
this.fontCache.put(fontRef, fontCapability.promise);
|
||||
} else {
|
||||
if (!fontID) {
|
||||
fontID = (this.uniquePrefix || 'F_') + (++this.idCounters.obj);
|
||||
fontID = this.idFactory.createObjId();
|
||||
}
|
||||
this.fontCache.put('id_' + fontID, fontCapability.promise);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue