mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 22:58:07 +02:00
Pass the XRef
-instance explicitly to the StructTreeRoot
class
This avoids the current situation where we're accessing it through various dictionaries, since that's a somewhat brittle solution given that in the general case a `Dict`-instance may not have the `xref`-field set (see e.g. the empty-Dict).
This commit is contained in:
parent
d4c7aaa9d3
commit
3c93d63731
3 changed files with 17 additions and 22 deletions
|
@ -262,7 +262,7 @@ class Catalog {
|
|||
get structTreeRoot() {
|
||||
let structTree = null;
|
||||
try {
|
||||
structTree = this._readStructTreeRoot();
|
||||
structTree = this.#readStructTreeRoot();
|
||||
} catch (ex) {
|
||||
if (ex instanceof MissingDataException) {
|
||||
throw ex;
|
||||
|
@ -272,17 +272,14 @@ class Catalog {
|
|||
return shadow(this, "structTreeRoot", structTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_readStructTreeRoot() {
|
||||
#readStructTreeRoot() {
|
||||
const rawObj = this._catDict.getRaw("StructTreeRoot");
|
||||
const obj = this.xref.fetchIfRef(rawObj);
|
||||
if (!(obj instanceof Dict)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const root = new StructTreeRoot(obj, rawObj);
|
||||
const root = new StructTreeRoot(this.xref, obj, rawObj);
|
||||
root.init();
|
||||
return root;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,8 @@ const StructElementType = {
|
|||
};
|
||||
|
||||
class StructTreeRoot {
|
||||
constructor(rootDict, rootRef) {
|
||||
constructor(xref, rootDict, rootRef) {
|
||||
this.xref = xref;
|
||||
this.dict = rootDict;
|
||||
this.ref = rootRef instanceof Ref ? rootRef : null;
|
||||
this.roleMap = new Map();
|
||||
|
@ -158,7 +159,7 @@ class StructTreeRoot {
|
|||
}
|
||||
}
|
||||
|
||||
async canUpdateStructTree({ pdfManager, xref, newAnnotationsByPage }) {
|
||||
async canUpdateStructTree({ pdfManager, newAnnotationsByPage }) {
|
||||
if (!this.ref) {
|
||||
warn("Cannot update the struct tree: no root reference.");
|
||||
return false;
|
||||
|
@ -180,7 +181,7 @@ class StructTreeRoot {
|
|||
warn("Cannot update the struct tree: nums isn't an array.");
|
||||
return false;
|
||||
}
|
||||
const numberTree = new NumberTree(parentTree, xref);
|
||||
const numberTree = new NumberTree(parentTree, this.xref);
|
||||
|
||||
for (const pageIndex of newAnnotationsByPage.keys()) {
|
||||
const { pageDict } = await pdfManager.getPage(pageIndex);
|
||||
|
@ -201,7 +202,7 @@ class StructTreeRoot {
|
|||
const { pageDict } = await pdfManager.getPage(pageIndex);
|
||||
StructTreeRoot.#collectParents({
|
||||
elements,
|
||||
xref: this.dict.xref,
|
||||
xref: this.xref,
|
||||
pageDict,
|
||||
numberTree,
|
||||
});
|
||||
|
@ -233,9 +234,8 @@ class StructTreeRoot {
|
|||
}
|
||||
|
||||
async updateStructureTree({ newAnnotationsByPage, pdfManager, changes }) {
|
||||
const xref = this.dict.xref;
|
||||
const { ref: structTreeRootRef, xref } = this;
|
||||
const structTreeRoot = this.dict.clone();
|
||||
const structTreeRootRef = this.ref;
|
||||
const cache = new RefSetCache();
|
||||
cache.put(structTreeRootRef, structTreeRoot);
|
||||
|
||||
|
@ -541,6 +541,7 @@ class StructTreeRoot {
|
|||
class StructElementNode {
|
||||
constructor(tree, dict) {
|
||||
this.tree = tree;
|
||||
this.xref = tree.xref;
|
||||
this.dict = dict;
|
||||
this.kids = [];
|
||||
this.parseKids();
|
||||
|
@ -565,10 +566,7 @@ class StructElementNode {
|
|||
const kids = this.dict.get("K");
|
||||
if (Array.isArray(kids)) {
|
||||
for (const kid of kids) {
|
||||
const element = this.parseKid(
|
||||
pageObjId,
|
||||
this.dict.xref.fetchIfRef(kid)
|
||||
);
|
||||
const element = this.parseKid(pageObjId, this.xref.fetchIfRef(kid));
|
||||
if (element) {
|
||||
this.kids.push(element);
|
||||
}
|
||||
|
@ -657,7 +655,8 @@ class StructElement {
|
|||
class StructTreePage {
|
||||
constructor(structTreeRoot, pageDict) {
|
||||
this.root = structTreeRoot;
|
||||
this.rootDict = structTreeRoot ? structTreeRoot.dict : null;
|
||||
this.xref = structTreeRoot?.xref ?? null;
|
||||
this.rootDict = structTreeRoot?.dict ?? null;
|
||||
this.pageDict = pageDict;
|
||||
this.nodes = [];
|
||||
}
|
||||
|
@ -683,7 +682,7 @@ class StructTreePage {
|
|||
}
|
||||
|
||||
const map = new Map();
|
||||
const numberTree = new NumberTree(parentTree, this.rootDict.xref);
|
||||
const numberTree = new NumberTree(parentTree, this.xref);
|
||||
|
||||
for (const [elemId] of ids) {
|
||||
const obj = numberTree.getRaw(elemId);
|
||||
|
@ -710,14 +709,14 @@ class StructTreePage {
|
|||
}
|
||||
|
||||
const map = new Map();
|
||||
const numberTree = new NumberTree(parentTree, this.rootDict.xref);
|
||||
const numberTree = new NumberTree(parentTree, this.xref);
|
||||
|
||||
if (Number.isInteger(id)) {
|
||||
const parentArray = numberTree.get(id);
|
||||
if (Array.isArray(parentArray)) {
|
||||
for (const ref of parentArray) {
|
||||
if (ref instanceof Ref) {
|
||||
this.addNode(this.rootDict.xref.fetch(ref), map);
|
||||
this.addNode(this.xref.fetch(ref), map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -729,7 +728,7 @@ class StructTreePage {
|
|||
for (const [elemId, type] of ids) {
|
||||
const obj = numberTree.get(elemId);
|
||||
if (obj) {
|
||||
const elem = this.addNode(this.rootDict.xref.fetchIfRef(obj), map);
|
||||
const elem = this.addNode(this.xref.fetchIfRef(obj), map);
|
||||
if (
|
||||
elem?.kids?.length === 1 &&
|
||||
elem.kids[0].type === StructElementType.OBJECT
|
||||
|
|
|
@ -558,7 +558,6 @@ class WorkerMessageHandler {
|
|||
} else if (
|
||||
await _structTreeRoot.canUpdateStructTree({
|
||||
pdfManager,
|
||||
xref,
|
||||
newAnnotationsByPage,
|
||||
})
|
||||
) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue