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 9, 2021
1 parent 6429ccc commit fc9501a
Show file tree
Hide file tree
Showing 22 changed files with 911 additions and 14 deletions.
20 changes: 20 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,22 @@ class Page {
task,
resources: this.resources,
normalizeWhitespace,
includeMarkedContent,
combineTextItems,
sink,
});
});
}

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 +620,10 @@ class PDFDocument {
static createObjId() {
unreachable("Abstract method `createObjId` called.");
}

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

Expand Down
39 changes: 38 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] instanceof Dict ? args[1].get("MCID") : null,
];

break;
case OPS.beginMarkedContent:
Expand Down Expand Up @@ -1973,6 +1976,7 @@ class PartialEvaluator {
stateManager = null,
normalizeWhitespace = false,
combineTextItems = false,
includeMarkedContent = false,
sink,
seenStyles = new Set(),
}) {
Expand Down Expand Up @@ -2573,6 +2577,7 @@ class PartialEvaluator {
stateManager: xObjStateManager,
normalizeWhitespace,
combineTextItems,
includeMarkedContent,
sink: sinkWrapper,
seenStyles,
})
Expand Down Expand Up @@ -2650,6 +2655,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 fc9501a

Please sign in to comment.