Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] A couple of smaller PDFPageProxy.getStructTree fixes (PR 13171 follow-up) #13221

Merged
merged 2 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ class Page {
const structTreeRoot = await this.pdfManager.ensureCatalog(
"structTreeRoot"
);
return this.pdfManager.ensure(this, "_parseStructTree", [structTreeRoot]);
}

/**
* @private
*/
_parseStructTree(structTreeRoot) {
const tree = new StructTreePage(structTreeRoot, this.pageDict);
tree.parse();
return tree;
Expand Down
4 changes: 4 additions & 0 deletions src/core/struct_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ class StructTreePage {
}
nodeToSerializable(child, root);
}

if (root.children.length === 0) {
return null;
}
return root;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,8 @@ class PDFPageProxy {

/**
* @returns {Promise<StructTreeNode>} A promise that is resolved with a
* {@link StructTreeNode} object that represents the page's structure tree.
* {@link StructTreeNode} object that represents the page's structure tree,
* or `null` when no structure tree is present for the current page.
*/
getStructTree() {
return (this._structTreePromise ||= this._transport.getStructTree(
Expand Down
63 changes: 63 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,69 @@ describe("api", function () {
.catch(done.fail);
});

it("gets empty structure tree", async function () {
const tree = await page.getStructTree();

expect(tree).toEqual(null);
});
it("gets simple structure tree", async function () {
const loadingTask = getDocument(
buildGetDocumentParams("structure_simple.pdf")
);
const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);
const tree = await pdfPage.getStructTree();

expect(tree).toEqual({
role: "Root",
children: [
{
role: "Document",
children: [
{
role: "H1",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid0" }],
},
],
},
{
role: "P",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid1" }],
},
],
},
{
role: "H2",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid2" }],
},
],
},
{
role: "P",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid3" }],
},
],
},
],
},
],
});

await loadingTask.destroy();
});

it("gets operator list", function (done) {
const promise = page.getOperatorList();
promise
Expand Down
3 changes: 3 additions & 0 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ class PDFPageView {
this.eventBus._off("textlayerrendered", this._onTextLayerRendered);
this._onTextLayerRendered = null;
this.pdfPage.getStructTree().then(tree => {
if (!tree) {
return;
}
const treeDom = this.structTreeLayer.render(tree);
treeDom.classList.add("structTree");
this.canvas.appendChild(treeDom);
Expand Down