mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-24 16:07:22 +02:00
Simplify how the ObjectLoader is used
The `ObjectLoader.prototype.load` method has a fast-path, which avoids any lookup/parsing if the entire PDF document is already loaded. However, we still need to create an `ObjectLoader`-instance which seems unnecessary in that case. Hence we introduce a *static* `ObjectLoader.load` method, which will help avoid creating `ObjectLoader`-instances needlessly and also (slightly) shortens the call-sites. To ensure that the new method will be used, we extend the `no-restricted-syntax` ESLint rule to "forbid" direct usage of `new ObjectLoader()`.
This commit is contained in:
parent
ef1ad675c2
commit
62009ffa70
@ -303,6 +303,11 @@ export default [
|
|||||||
selector: "NewExpression[callee.name='Name']",
|
selector: "NewExpression[callee.name='Name']",
|
||||||
message: "Use `Name.get()` rather than `new Name()`.",
|
message: "Use `Name.get()` rather than `new Name()`.",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
selector: "NewExpression[callee.name='ObjectLoader']",
|
||||||
|
message:
|
||||||
|
"Use `ObjectLoader.load()` rather than `new ObjectLoader()`.",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
selector: "NewExpression[callee.name='Ref']",
|
selector: "NewExpression[callee.name='Ref']",
|
||||||
message: "Use `Ref.get()` rather than `new Ref()`.",
|
message: "Use `Ref.get()` rather than `new Ref()`.",
|
||||||
|
|||||||
@ -1158,15 +1158,12 @@ class Annotation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadResources(keys, appearance) {
|
async loadResources(keys, appearance) {
|
||||||
return appearance.dict.getAsync("Resources").then(resources => {
|
const resources = await appearance.dict.getAsync("Resources");
|
||||||
if (!resources) {
|
if (resources) {
|
||||||
return undefined;
|
await ObjectLoader.load(resources, keys, resources.xref);
|
||||||
}
|
}
|
||||||
|
return resources;
|
||||||
const objectLoader = new ObjectLoader(resources, keys, resources.xref);
|
|
||||||
return objectLoader.load().then(() => resources);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOperatorList(evaluator, task, intent, annotationStorage) {
|
async getOperatorList(evaluator, task, intent, annotationStorage) {
|
||||||
|
|||||||
@ -417,8 +417,7 @@ class Page {
|
|||||||
// TODO: add async `_getInheritableProperty` and remove this.
|
// TODO: add async `_getInheritableProperty` and remove this.
|
||||||
await (this.resourcesPromise ??= this.pdfManager.ensure(this, "resources"));
|
await (this.resourcesPromise ??= this.pdfManager.ensure(this, "resources"));
|
||||||
|
|
||||||
const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
|
await ObjectLoader.load(this.resources, keys, this.xref);
|
||||||
await objectLoader.load();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async #getMergedResources(streamDict, keys) {
|
async #getMergedResources(streamDict, keys) {
|
||||||
@ -430,8 +429,7 @@ class Page {
|
|||||||
if (!(localResources instanceof Dict && localResources.size)) {
|
if (!(localResources instanceof Dict && localResources.size)) {
|
||||||
return this.resources;
|
return this.resources;
|
||||||
}
|
}
|
||||||
const objectLoader = new ObjectLoader(localResources, keys, this.xref);
|
await ObjectLoader.load(localResources, keys, this.xref);
|
||||||
await objectLoader.load();
|
|
||||||
|
|
||||||
return Dict.merge({
|
return Dict.merge({
|
||||||
xref: this.xref,
|
xref: this.xref,
|
||||||
@ -1254,8 +1252,7 @@ class PDFDocument {
|
|||||||
if (!(resources instanceof Dict)) {
|
if (!(resources instanceof Dict)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const objectLoader = new ObjectLoader(resources, ["Font"], this.xref);
|
await ObjectLoader.load(resources, ["Font"], this.xref);
|
||||||
await objectLoader.load();
|
|
||||||
|
|
||||||
const fontRes = resources.get("Font");
|
const fontRes = resources.get("Font");
|
||||||
if (!(fontRes instanceof Dict)) {
|
if (!(fontRes instanceof Dict)) {
|
||||||
|
|||||||
@ -54,7 +54,7 @@ function addChildren(node, nodesToVisit) {
|
|||||||
* entire PDF document object graph to be traversed.
|
* entire PDF document object graph to be traversed.
|
||||||
*/
|
*/
|
||||||
class ObjectLoader {
|
class ObjectLoader {
|
||||||
refSet = null;
|
refSet = new RefSet();
|
||||||
|
|
||||||
constructor(dict, keys, xref) {
|
constructor(dict, keys, xref) {
|
||||||
this.dict = dict;
|
this.dict = dict;
|
||||||
@ -63,13 +63,7 @@ class ObjectLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
// Don't walk the graph if all the data is already loaded.
|
|
||||||
if (this.xref.stream.isDataLoaded) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { keys, dict } = this;
|
const { keys, dict } = this;
|
||||||
this.refSet = new RefSet();
|
|
||||||
// Setup the initial nodes to visit.
|
// Setup the initial nodes to visit.
|
||||||
const nodesToVisit = [];
|
const nodesToVisit = [];
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
@ -144,6 +138,16 @@ class ObjectLoader {
|
|||||||
await this.#walk(nodesToRevisit);
|
await this.#walk(nodesToRevisit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async load(obj, keys, xref) {
|
||||||
|
// Don't walk the graph if all the data is already loaded.
|
||||||
|
if (xref.stream.isDataLoaded) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
const objLoader = new ObjectLoader(obj, keys, xref);
|
||||||
|
await objLoader.load();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { ObjectLoader };
|
export { ObjectLoader };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user