Skip to content

Commit

Permalink
Merge pull request #13171 from brendandahl/struct-tree
Browse files Browse the repository at this point in the history
[api-minor] Add support for basic structure tree for accessibility.
  • Loading branch information
timvandermeij committed Apr 9, 2021
2 parents b0473eb + fc9501a commit 03c8c89
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 03c8c89

Please sign in to comment.