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] Implement API to get MarkInfo from the catalog. #12525

Merged
merged 2 commits into from
Oct 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,47 @@ class Catalog {
return shadow(this, "metadata", metadata);
}

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

/**
* @private
*/
_readMarkInfo() {
const obj = this._catDict.get("MarkInfo");
if (!isDict(obj)) {
return null;
}

const markInfo = Object.assign(Object.create(null), {
Marked: false,
UserProperties: false,
Suspects: false,
});
for (const key in markInfo) {
if (!obj.has(key)) {
continue;
}
const value = obj.get(key);
if (!isBool(value)) {
continue;
}
markInfo[key] = value;
}

return markInfo;
}

get toplevelPagesDict() {
const pagesObj = this._catDict.get("Pages");
if (!isDict(pagesObj)) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ class WorkerMessageHandler {
]);
});

handler.on("GetMarkInfo", function wphSetupGetMarkInfo(data) {
return pdfManager.ensureCatalog("markInfo");
});

handler.on("GetData", function wphSetupGetData(data) {
pdfManager.requestLoadedStream();
return pdfManager.onLoadedStream().then(function (stream) {
Expand Down
21 changes: 21 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,23 @@ class PDFDocumentProxy {
return this._transport.getMetadata();
}

/**
* @typedef {Object} MarkInfo
* Properties correspond to Table 321 of the PDF 32000-1:2008 spec.
* @property {boolean} Marked
* @property {boolean} UserProperties
* @property {boolean} Suspects
*/

/**
* @returns {Promise<MarkInfo | null>} A promise that is resolved with
* a {MarkInfo} object that contains the MarkInfo flags for the PDF
* document, or `null` when no MarkInfo values are present in the PDF file.
*/
getMarkInfo() {
return this._transport.getMarkInfo();
}

/**
* @returns {Promise<TypedArray>} A promise that is resolved with a
* {TypedArray} that has the raw data from the PDF.
Expand Down Expand Up @@ -2646,6 +2663,10 @@ class WorkerTransport {
});
}

getMarkInfo() {
return this.messageHandler.sendWithPromise("GetMarkInfo", null);
}

getStats() {
return this.messageHandler.sendWithPromise("GetStats", null);
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,24 @@ describe("api", function () {
.catch(done.fail);
});

it("gets markInfo", function (done) {
const loadingTask = getDocument(
buildGetDocumentParams("annotation-line.pdf")
);

loadingTask.promise
.then(function (pdfDoc) {
return pdfDoc.getMarkInfo();
})
.then(function (info) {
expect(info.Marked).toEqual(true);
expect(info.UserProperties).toEqual(false);
expect(info.Suspects).toEqual(false);
done();
})
.catch(done.fail);
});

it("gets data", function (done) {
var promise = pdfDocument.getData();
promise
Expand Down
32 changes: 32 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ const PDFViewerApplication = {
triggerDelayedFallback: null,
_saveInProgress: false,
_wheelUnusedTicks: 0,
_idleCallbacks: new Set(),

// Called once when the document is loaded.
async initialize(appConfig) {
Expand Down Expand Up @@ -738,6 +739,10 @@ const PDFViewerApplication = {
this.contentDispositionFilename = null;
this.triggerDelayedFallback = null;
this._saveInProgress = false;
for (const callback of this._idleCallbacks) {
window.cancelIdleCallback(callback);
}
this._idleCallbacks.clear();

this.pdfSidebar.reset();
this.pdfOutlineViewer.reset();
Expand Down Expand Up @@ -1329,12 +1334,39 @@ const PDFViewerApplication = {
pdfViewer.optionalContentConfigPromise.then(optionalContentConfig => {
this.pdfLayerViewer.render({ optionalContentConfig, pdfDocument });
});
if ("requestIdleCallback" in window) {
const callback = window.requestIdleCallback(
() => {
this._collectTelemetry(pdfDocument);
this._idleCallbacks.delete(callback);
},
{ timeout: 1000 }
);
this._idleCallbacks.add(callback);
}
});

this._initializePageLabels(pdfDocument);
this._initializeMetadata(pdfDocument);
},

/**
* A place to fetch data for telemetry after one page is rendered and the
* viewer is idle.
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
* @private
*/
async _collectTelemetry(pdfDocument) {
const markInfo = await this.pdfDocument.getMarkInfo();
if (pdfDocument !== this.pdfDocument) {
return; // Document was closed while waiting for mark info.
}
const tagged = markInfo?.Marked || false;
this.externalServices.reportTelemetry({
type: "tagged",
tagged,
});
},

/**
* @private
*/
Expand Down