diff --git a/src/core/catalog.js b/src/core/catalog.js index 30fead7ad..b12abee83 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -354,15 +354,11 @@ class Catalog { } #readStructTreeRoot() { - const rawObj = this.#catDict.getRaw("StructTreeRoot"); - const obj = this.xref.fetchIfRef(rawObj); - if (!(obj instanceof Dict)) { - return null; - } - - const root = new StructTreeRoot(this.xref, obj, rawObj); - root.init(); - return root; + const rawObj = this.#catDict.getRaw("StructTreeRoot"), + obj = this.xref.fetchIfRef(rawObj); + return obj instanceof Dict + ? new StructTreeRoot(this.xref, obj, rawObj) + : null; } get toplevelPagesDict() { diff --git a/src/core/struct_tree.js b/src/core/struct_tree.js index 8bfa3f186..722927a8b 100644 --- a/src/core/struct_tree.js +++ b/src/core/struct_tree.js @@ -37,14 +37,31 @@ const StructElementType = { }; class StructTreeRoot { + kidRefToPosition = undefined; + + parentTree = null; + + roleMap = new Map(); + + structParentIds = null; + constructor(xref, rootDict, rootRef) { this.xref = xref; this.dict = rootDict; this.ref = rootRef instanceof Ref ? rootRef : null; - this.roleMap = new Map(); - this.structParentIds = null; - this.kidRefToPosition = undefined; - this.parentTree = null; + + const roleMap = rootDict.get("RoleMap"); + if (roleMap instanceof Dict) { + for (const [key, value] of roleMap) { + if (value instanceof Name) { + this.roleMap.set(key, value.name); + } + } + } + const parentTree = rootDict.getRaw("ParentTree"); + if (parentTree) { + this.parentTree = new NumberTree(parentTree, xref); + } } getKidPosition(kidRef) { @@ -71,15 +88,6 @@ class StructTreeRoot { : -1; } - init() { - this.readRoleMap(); - const parentTree = this.dict.get("ParentTree"); - if (!parentTree) { - return; - } - this.parentTree = new NumberTree(parentTree, this.xref); - } - #addIdToPage(pageRef, id, type) { if (!(pageRef instanceof Ref) || id < 0) { return; @@ -97,18 +105,6 @@ class StructTreeRoot { this.#addIdToPage(pageRef, id, StructElementType.ANNOTATION); } - readRoleMap() { - const roleMapDict = this.dict.get("RoleMap"); - if (!(roleMapDict instanceof Dict)) { - return; - } - for (const [key, value] of roleMapDict) { - if (value instanceof Name) { - this.roleMap.set(key, value.name); - } - } - } - static async canCreateStructureTree({ catalogRef, pdfManager,