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() {
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() {

View File

@ -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,