Merge pull request #19719 from Snuffleupagus/structTree-xref
Pass the `XRef`-instance explicitly to the `StructTreeRoot` class, and simplify the `StructElementNode.prototype.role` getter
This commit is contained in:
commit
c6b9338f6f
@ -262,7 +262,7 @@ class Catalog {
|
|||||||
get structTreeRoot() {
|
get structTreeRoot() {
|
||||||
let structTree = null;
|
let structTree = null;
|
||||||
try {
|
try {
|
||||||
structTree = this._readStructTreeRoot();
|
structTree = this.#readStructTreeRoot();
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
if (ex instanceof MissingDataException) {
|
if (ex instanceof MissingDataException) {
|
||||||
throw ex;
|
throw ex;
|
||||||
@ -272,17 +272,14 @@ class Catalog {
|
|||||||
return shadow(this, "structTreeRoot", structTree);
|
return shadow(this, "structTreeRoot", structTree);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
#readStructTreeRoot() {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_readStructTreeRoot() {
|
|
||||||
const rawObj = this._catDict.getRaw("StructTreeRoot");
|
const rawObj = this._catDict.getRaw("StructTreeRoot");
|
||||||
const obj = this.xref.fetchIfRef(rawObj);
|
const obj = this.xref.fetchIfRef(rawObj);
|
||||||
if (!(obj instanceof Dict)) {
|
if (!(obj instanceof Dict)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const root = new StructTreeRoot(obj, rawObj);
|
const root = new StructTreeRoot(this.xref, obj, rawObj);
|
||||||
root.init();
|
root.init();
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,8 @@ const StructElementType = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class StructTreeRoot {
|
class StructTreeRoot {
|
||||||
constructor(rootDict, rootRef) {
|
constructor(xref, rootDict, rootRef) {
|
||||||
|
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.roleMap = new Map();
|
||||||
@ -158,7 +159,7 @@ class StructTreeRoot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async canUpdateStructTree({ pdfManager, xref, newAnnotationsByPage }) {
|
async canUpdateStructTree({ pdfManager, newAnnotationsByPage }) {
|
||||||
if (!this.ref) {
|
if (!this.ref) {
|
||||||
warn("Cannot update the struct tree: no root reference.");
|
warn("Cannot update the struct tree: no root reference.");
|
||||||
return false;
|
return false;
|
||||||
@ -180,7 +181,7 @@ class StructTreeRoot {
|
|||||||
warn("Cannot update the struct tree: nums isn't an array.");
|
warn("Cannot update the struct tree: nums isn't an array.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const numberTree = new NumberTree(parentTree, xref);
|
const numberTree = new NumberTree(parentTree, this.xref);
|
||||||
|
|
||||||
for (const pageIndex of newAnnotationsByPage.keys()) {
|
for (const pageIndex of newAnnotationsByPage.keys()) {
|
||||||
const { pageDict } = await pdfManager.getPage(pageIndex);
|
const { pageDict } = await pdfManager.getPage(pageIndex);
|
||||||
@ -201,7 +202,7 @@ class StructTreeRoot {
|
|||||||
const { pageDict } = await pdfManager.getPage(pageIndex);
|
const { pageDict } = await pdfManager.getPage(pageIndex);
|
||||||
StructTreeRoot.#collectParents({
|
StructTreeRoot.#collectParents({
|
||||||
elements,
|
elements,
|
||||||
xref: this.dict.xref,
|
xref: this.xref,
|
||||||
pageDict,
|
pageDict,
|
||||||
numberTree,
|
numberTree,
|
||||||
});
|
});
|
||||||
@ -233,9 +234,8 @@ class StructTreeRoot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateStructureTree({ newAnnotationsByPage, pdfManager, changes }) {
|
async updateStructureTree({ newAnnotationsByPage, pdfManager, changes }) {
|
||||||
const xref = this.dict.xref;
|
const { ref: structTreeRootRef, xref } = this;
|
||||||
const structTreeRoot = this.dict.clone();
|
const structTreeRoot = this.dict.clone();
|
||||||
const structTreeRootRef = this.ref;
|
|
||||||
const cache = new RefSetCache();
|
const cache = new RefSetCache();
|
||||||
cache.put(structTreeRootRef, structTreeRoot);
|
cache.put(structTreeRootRef, structTreeRoot);
|
||||||
|
|
||||||
@ -541,6 +541,7 @@ class StructTreeRoot {
|
|||||||
class StructElementNode {
|
class StructElementNode {
|
||||||
constructor(tree, dict) {
|
constructor(tree, dict) {
|
||||||
this.tree = tree;
|
this.tree = tree;
|
||||||
|
this.xref = tree.xref;
|
||||||
this.dict = dict;
|
this.dict = dict;
|
||||||
this.kids = [];
|
this.kids = [];
|
||||||
this.parseKids();
|
this.parseKids();
|
||||||
@ -550,10 +551,7 @@ class StructElementNode {
|
|||||||
const nameObj = this.dict.get("S");
|
const nameObj = this.dict.get("S");
|
||||||
const name = nameObj instanceof Name ? nameObj.name : "";
|
const name = nameObj instanceof Name ? nameObj.name : "";
|
||||||
const { root } = this.tree;
|
const { root } = this.tree;
|
||||||
if (root.roleMap.has(name)) {
|
return root.roleMap.get(name) ?? name;
|
||||||
return root.roleMap.get(name);
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parseKids() {
|
parseKids() {
|
||||||
@ -565,10 +563,7 @@ class StructElementNode {
|
|||||||
const kids = this.dict.get("K");
|
const kids = this.dict.get("K");
|
||||||
if (Array.isArray(kids)) {
|
if (Array.isArray(kids)) {
|
||||||
for (const kid of kids) {
|
for (const kid of kids) {
|
||||||
const element = this.parseKid(
|
const element = this.parseKid(pageObjId, this.xref.fetchIfRef(kid));
|
||||||
pageObjId,
|
|
||||||
this.dict.xref.fetchIfRef(kid)
|
|
||||||
);
|
|
||||||
if (element) {
|
if (element) {
|
||||||
this.kids.push(element);
|
this.kids.push(element);
|
||||||
}
|
}
|
||||||
@ -657,7 +652,8 @@ class StructElement {
|
|||||||
class StructTreePage {
|
class StructTreePage {
|
||||||
constructor(structTreeRoot, pageDict) {
|
constructor(structTreeRoot, pageDict) {
|
||||||
this.root = structTreeRoot;
|
this.root = structTreeRoot;
|
||||||
this.rootDict = structTreeRoot ? structTreeRoot.dict : null;
|
this.xref = structTreeRoot?.xref ?? null;
|
||||||
|
this.rootDict = structTreeRoot?.dict ?? null;
|
||||||
this.pageDict = pageDict;
|
this.pageDict = pageDict;
|
||||||
this.nodes = [];
|
this.nodes = [];
|
||||||
}
|
}
|
||||||
@ -683,7 +679,7 @@ class StructTreePage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const map = new Map();
|
const map = new Map();
|
||||||
const numberTree = new NumberTree(parentTree, this.rootDict.xref);
|
const numberTree = new NumberTree(parentTree, this.xref);
|
||||||
|
|
||||||
for (const [elemId] of ids) {
|
for (const [elemId] of ids) {
|
||||||
const obj = numberTree.getRaw(elemId);
|
const obj = numberTree.getRaw(elemId);
|
||||||
@ -710,14 +706,14 @@ class StructTreePage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const map = new Map();
|
const map = new Map();
|
||||||
const numberTree = new NumberTree(parentTree, this.rootDict.xref);
|
const numberTree = new NumberTree(parentTree, this.xref);
|
||||||
|
|
||||||
if (Number.isInteger(id)) {
|
if (Number.isInteger(id)) {
|
||||||
const parentArray = numberTree.get(id);
|
const parentArray = numberTree.get(id);
|
||||||
if (Array.isArray(parentArray)) {
|
if (Array.isArray(parentArray)) {
|
||||||
for (const ref of parentArray) {
|
for (const ref of parentArray) {
|
||||||
if (ref instanceof Ref) {
|
if (ref instanceof Ref) {
|
||||||
this.addNode(this.rootDict.xref.fetch(ref), map);
|
this.addNode(this.xref.fetch(ref), map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -729,7 +725,7 @@ class StructTreePage {
|
|||||||
for (const [elemId, type] of ids) {
|
for (const [elemId, type] of ids) {
|
||||||
const obj = numberTree.get(elemId);
|
const obj = numberTree.get(elemId);
|
||||||
if (obj) {
|
if (obj) {
|
||||||
const elem = this.addNode(this.rootDict.xref.fetchIfRef(obj), map);
|
const elem = this.addNode(this.xref.fetchIfRef(obj), map);
|
||||||
if (
|
if (
|
||||||
elem?.kids?.length === 1 &&
|
elem?.kids?.length === 1 &&
|
||||||
elem.kids[0].type === StructElementType.OBJECT
|
elem.kids[0].type === StructElementType.OBJECT
|
||||||
|
|||||||
@ -558,7 +558,6 @@ class WorkerMessageHandler {
|
|||||||
} else if (
|
} else if (
|
||||||
await _structTreeRoot.canUpdateStructTree({
|
await _structTreeRoot.canUpdateStructTree({
|
||||||
pdfManager,
|
pdfManager,
|
||||||
xref,
|
|
||||||
newAnnotationsByPage,
|
newAnnotationsByPage,
|
||||||
})
|
})
|
||||||
) {
|
) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user