Skip to content

Commit

Permalink
Add support for basic structure tree for accessibility.
Browse files Browse the repository at this point in the history
When a PDF is "marked" we now generate a separate DOM that represents
the structure tree from the PDF.  This DOM is inserted into the <canvas>
element and allows screen readers to walk the tree and have more
information about headings, images, links, etc. To link the structure
tree DOM (which is empty) to the text layer aria-owns is used. This
required modifying the text layer creation so that marked items are
now tracked.
  • Loading branch information
brendandahl committed Apr 8, 2021
1 parent 6429ccc commit fccd302
Show file tree
Hide file tree
Showing 22 changed files with 908 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { calculateMD5 } from "./crypto.js";
import { Linearization } from "./parser.js";
import { OperatorList } from "./operator_list.js";
import { PartialEvaluator } from "./evaluator.js";
import { StructTreePage } from "./struct_tree.js";
import { XFAFactory } from "./xfa/factory.js";

const DEFAULT_USER_UNIT = 1.0;
Expand Down Expand Up @@ -104,6 +105,10 @@ class Page {
static createObjId() {
return `p${pageIndex}_${++idCounters.obj}`;
}

static getPageObjId() {
return `page${ref.toString()}`;
}
};
}

Expand Down Expand Up @@ -406,6 +411,7 @@ class Page {
handler,
task,
normalizeWhitespace,
includeMarkedContent,
sink,
combineTextItems,
}) {
Expand Down Expand Up @@ -437,12 +443,23 @@ class Page {
task,
resources: this.resources,
normalizeWhitespace,
includeMarkedContent,
combineTextItems,
sink,
pageObjId: this.ref.toString(),
});
});
}

async getStructTree() {
const structTreeRoot = await this.pdfManager.ensureCatalog(
"structTreeRoot"
);
const tree = new StructTreePage(structTreeRoot, this.pageDict);
tree.parse();
return tree;
}

getAnnotationsData(intent) {
return this._parsedAnnotations.then(function (annotations) {
const annotationsData = [];
Expand Down Expand Up @@ -604,6 +621,10 @@ class PDFDocument {
static createObjId() {
unreachable("Abstract method `createObjId` called.");
}

static getPageObjId() {
unreachable("Abstract method `getPageObjId` called.");
}
};
}

Expand Down
41 changes: 40 additions & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,10 @@ class PartialEvaluator {
return;
}
// Other marked content types aren't supported yet.
args = [args[0].name];
args = [
args[0].name,
args[1] && isDict(args[1]) ? args[1].get("MCID") : null,
];

break;
case OPS.beginMarkedContent:
Expand Down Expand Up @@ -1973,8 +1976,10 @@ class PartialEvaluator {
stateManager = null,
normalizeWhitespace = false,
combineTextItems = false,
includeMarkedContent = false,
sink,
seenStyles = new Set(),
pageObjId,
}) {
// Ensure that `resources`/`stateManager` is correctly initialized,
// even if the provided parameter is e.g. `null`.
Expand Down Expand Up @@ -2573,8 +2578,10 @@ class PartialEvaluator {
stateManager: xObjStateManager,
normalizeWhitespace,
combineTextItems,
includeMarkedContent,
sink: sinkWrapper,
seenStyles,
pageObjId,
})
.then(function () {
if (!sinkWrapper.enqueueInvoked) {
Expand Down Expand Up @@ -2650,6 +2657,38 @@ class PartialEvaluator {
})
);
return;
case OPS.beginMarkedContent:
if (includeMarkedContent) {
textContent.items.push({
type: "beginMarkedContent",
tag: isName(args[0]) ? args[0].name : null,
});
}
break;
case OPS.beginMarkedContentProps:
if (includeMarkedContent) {
flushTextContentItem();
let mcid = null;
if (isDict(args[1])) {
mcid = args[1].get("MCID");
}
textContent.items.push({
type: "beginMarkedContentProps",
id: Number.isInteger(mcid)
? `${self.idFactory.getPageObjId()}_mcid${mcid}`
: null,
tag: isName(args[0]) ? args[0].name : null,
});
}
break;
case OPS.endMarkedContent:
if (includeMarkedContent) {
flushTextContentItem();
textContent.items.push({
type: "endMarkedContent",
});
}
break;
} // switch
if (textContent.items.length >= sink.desiredSize) {
// Wait for ready, if we reach highWaterMark.
Expand Down
29 changes: 28 additions & 1 deletion src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { CipherTransformFactory } from "./crypto.js";
import { ColorSpace } from "./colorspace.js";
import { GlobalImageCache } from "./image_utils.js";
import { MetadataParser } from "./metadata_parser.js";
import { StructTreeRoot } from "./struct_tree.js";

function fetchDestination(dest) {
return isDict(dest) ? dest.get("D") : dest;
Expand Down Expand Up @@ -200,6 +201,32 @@ class Catalog {
return markInfo;
}

get structTreeRoot() {
let structTree = null;
try {
structTree = this._readStructTreeRoot();
} catch (ex) {
if (ex instanceof MissingDataException) {
throw ex;
}
warn("Unable read to structTreeRoot info.");
}
return shadow(this, "structTreeRoot", structTree);
}

/**
* @private
*/
_readStructTreeRoot() {
const obj = this._catDict.get("StructTreeRoot");
if (!isDict(obj)) {
return null;
}
const root = new StructTreeRoot(obj);
root.init();
return root;
}

get toplevelPagesDict() {
const pagesObj = this._catDict.get("Pages");
if (!isDict(pagesObj)) {
Expand Down Expand Up @@ -2626,4 +2653,4 @@ const ObjectLoader = (function () {
return ObjectLoader;
})();

export { Catalog, FileSpec, ObjectLoader, XRef };
export { Catalog, FileSpec, NumberTree, ObjectLoader, XRef };

0 comments on commit fccd302

Please sign in to comment.