Inline the init method in the StructTreeRoot constructor

Currently the constructor only set various class fields and the class instance thus needs to be "manually" initialized, which seems unnecessary.
Given how short/simple the `init` and `readRoleMap` methods are we can just inline their code in the constructor, thus simplifying the code overall.
This commit is contained in:
Jonas Jenwald 2026-06-26 14:14:18 +02:00
parent 86ffd68c05
commit 82324408cd
2 changed files with 26 additions and 34 deletions

View File

@ -354,15 +354,11 @@ class Catalog {
} }
#readStructTreeRoot() { #readStructTreeRoot() {
const rawObj = this.#catDict.getRaw("StructTreeRoot"); const rawObj = this.#catDict.getRaw("StructTreeRoot"),
const obj = this.xref.fetchIfRef(rawObj); obj = this.xref.fetchIfRef(rawObj);
if (!(obj instanceof Dict)) { return obj instanceof Dict
return null; ? new StructTreeRoot(this.xref, obj, rawObj)
} : null;
const root = new StructTreeRoot(this.xref, obj, rawObj);
root.init();
return root;
} }
get toplevelPagesDict() { get toplevelPagesDict() {

View File

@ -37,14 +37,31 @@ const StructElementType = {
}; };
class StructTreeRoot { class StructTreeRoot {
kidRefToPosition = undefined;
parentTree = null;
roleMap = new Map();
structParentIds = null;
constructor(xref, rootDict, rootRef) { constructor(xref, rootDict, rootRef) {
this.xref = xref; this.xref = xref;
this.dict = rootDict; this.dict = rootDict;
this.ref = rootRef instanceof Ref ? rootRef : null; this.ref = rootRef instanceof Ref ? rootRef : null;
this.roleMap = new Map();
this.structParentIds = null; const roleMap = rootDict.get("RoleMap");
this.kidRefToPosition = undefined; if (roleMap instanceof Dict) {
this.parentTree = null; 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) { getKidPosition(kidRef) {
@ -71,15 +88,6 @@ class StructTreeRoot {
: -1; : -1;
} }
init() {
this.readRoleMap();
const parentTree = this.dict.get("ParentTree");
if (!parentTree) {
return;
}
this.parentTree = new NumberTree(parentTree, this.xref);
}
#addIdToPage(pageRef, id, type) { #addIdToPage(pageRef, id, type) {
if (!(pageRef instanceof Ref) || id < 0) { if (!(pageRef instanceof Ref) || id < 0) {
return; return;
@ -97,18 +105,6 @@ class StructTreeRoot {
this.#addIdToPage(pageRef, id, StructElementType.ANNOTATION); 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({ static async canCreateStructureTree({
catalogRef, catalogRef,
pdfManager, pdfManager,