Re-factor the idFactory functionality, used in the core/-code, and move the fontID generation into it

Note how the `getFontID`-method in `src/core/fonts.js` is *completely* global, rather than properly tied to the current document. This means that if you repeatedly open and parse/render, and then close, even the *same* PDF document the `fontID`s will still be incremented continuously.

For comparison the `createObjId` method, on `idFactory`, will always create a *consistent* id, assuming of course that the document and its pages are parsed/rendered in the same order.

In order to address this inconsistency, it thus seems reasonable to add a new `createFontId` method on the `idFactory` and use that when obtaining `fontID`s. (When the current `getFontID` method was added the `idFactory` didn't actually exist yet, which explains why the code looks the way it does.)
*Please note:* Since the document id is (still) part of the `loadedName`, it's thus not possible for different documents to have identical font names.
This commit is contained in:
Jonas Jenwald 2020-07-07 16:00:05 +02:00
parent cf8daaf78b
commit 4cc6797f17
5 changed files with 56 additions and 30 deletions

View file

@ -587,7 +587,7 @@ class PartialEvaluator {
cacheGlobally = false;
if (this.parsingType3Font) {
objId = `${this.idFactory.getDocId()}_type3res_${objId}`;
objId = `${this.idFactory.getDocId()}_type3_${objId}`;
} else if (imageRef) {
cacheGlobally = this.globalImageCache.shouldCache(
imageRef,
@ -998,7 +998,7 @@ class PartialEvaluator {
var fontRefIsRef = isRef(fontRef),
fontID;
if (fontRefIsRef) {
fontID = fontRef.toString();
fontID = `f${fontRef.toString()}`;
}
if (hash && isDict(descriptor)) {
@ -1015,7 +1015,7 @@ class PartialEvaluator {
}
} else {
fontAliases[hash] = {
fontID: Font.getFontID(),
fontID: this.idFactory.createFontId(),
};
}
@ -1046,15 +1046,18 @@ class PartialEvaluator {
this.fontCache.put(fontRef, fontCapability.promise);
} else {
if (!fontID) {
fontID = this.idFactory.createObjId();
fontID = this.idFactory.createFontId();
}
this.fontCache.put(`id_${fontID}`, fontCapability.promise);
}
assert(fontID, 'The "fontID" must be defined.');
assert(
fontID && fontID.startsWith("f"),
'The "fontID" must be (correctly) defined.'
);
// Keep track of each font we translated so the caller can
// load them asynchronously before calling display on a page.
font.loadedName = `${this.idFactory.getDocId()}_f${fontID}`;
font.loadedName = `${this.idFactory.getDocId()}_${fontID}`;
font.translated = fontCapability.promise;