1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

[api-minor] Introduce a new annotationMode-option, in PDFPageProxy.{render, getOperatorList}

*This is a follow-up to PRs 13867 and 13899.*

This patch is tagged `api-minor` for the following reasons:
 - It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.

 - For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.

 - It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.

---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
This commit is contained in:
Jonas Jenwald 2021-08-08 14:36:28 +02:00
parent 56e7bb626c
commit 41efa3c071
16 changed files with 272 additions and 134 deletions

View file

@ -17,6 +17,16 @@
"use strict";
const {
AnnotationLayer,
AnnotationMode,
getDocument,
GlobalWorkerOptions,
renderTextLayer,
XfaLayer,
} = pdfjsLib;
const { SimpleLinkService } = pdfjsViewer;
const WAITING_TIME = 100; // ms
const PDF_TO_CSS_UNITS = 96.0 / 72.0;
const CMAP_URL = "/build/generic/web/cmaps/";
@ -162,7 +172,7 @@ var rasterizeTextLayer = (function rasterizeTextLayerClosure() {
style.textContent = cssRules;
// Rendering text layer as HTML.
var task = pdfjsLib.renderTextLayer({
var task = renderTextLayer({
textContent,
container: div,
viewport,
@ -219,7 +229,7 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
annotations,
page,
imageResourcesPath,
renderInteractiveForms
renderForms = false
) {
return new Promise(function (resolve, reject) {
// Building SVG with size of the viewport.
@ -250,11 +260,11 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
div,
annotations,
page,
linkService: new pdfjsViewer.SimpleLinkService(),
linkService: new SimpleLinkService(),
imageResourcesPath,
renderInteractiveForms,
renderForms,
};
pdfjsLib.AnnotationLayer.render(parameters);
AnnotationLayer.render(parameters);
// Inline SVG images from text annotations.
await resolveImages(div);
@ -319,7 +329,7 @@ var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
.then(async cssRules => {
style.textContent = fontRules + "\n" + cssRules;
pdfjsLib.XfaLayer.render({
XfaLayer.render({
xfa,
div,
viewport: viewport.clone({ dontFlip: true }),
@ -365,7 +375,7 @@ var Driver = (function DriverClosure() {
// eslint-disable-next-line no-shadow
function Driver(options) {
// Configure the global worker options.
pdfjsLib.GlobalWorkerOptions.workerSrc = WORKER_SRC;
GlobalWorkerOptions.workerSrc = WORKER_SRC;
// Set the passed options
this.inflight = options.inflight;
@ -494,7 +504,7 @@ var Driver = (function DriverClosure() {
.appendChild(xfaStyleElement);
}
const loadingTask = pdfjsLib.getDocument({
const loadingTask = getDocument({
url: absoluteUrl,
password: task.password,
cMapUrl: CMAP_URL,
@ -752,12 +762,13 @@ var Driver = (function DriverClosure() {
var renderContext = {
canvasContext: ctx,
viewport,
renderInteractiveForms: renderForms,
optionalContentConfigPromise: task.optionalContentConfigPromise,
};
if (renderPrint) {
if (renderForms) {
renderContext.annotationMode = AnnotationMode.ENABLE_FORMS;
} else if (renderPrint) {
if (task.annotationStorage) {
renderContext.includeAnnotationStorage = true;
renderContext.annotationMode = AnnotationMode.ENABLE_STORAGE;
}
renderContext.intent = "print";
}

View file

@ -14,11 +14,7 @@
*/
import {
buildGetDocumentParams,
DefaultFileReaderFactory,
TEST_PDFS_PATH,
} from "./test_utils.js";
import {
AnnotationMode,
createPromiseCapability,
FontType,
ImageKind,
@ -30,6 +26,11 @@ import {
PermissionFlag,
StreamType,
} from "../../src/shared/util.js";
import {
buildGetDocumentParams,
DefaultFileReaderFactory,
TEST_PDFS_PATH,
} from "./test_utils.js";
import {
DefaultCanvasFactory,
getDocument,
@ -1739,6 +1740,56 @@ describe("api", function () {
await loadingTask.destroy();
});
it("gets operator list, with `annotationMode`-option", async function () {
const loadingTask = getDocument(buildGetDocumentParams("evaljs.pdf"));
const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(2);
pdfDoc.annotationStorage.setValue("30R", { value: "test" });
pdfDoc.annotationStorage.setValue("31R", { value: true });
const opListAnnotDisable = await pdfPage.getOperatorList({
annotationMode: AnnotationMode.DISABLE,
});
expect(opListAnnotDisable.fnArray.length).toEqual(0);
expect(opListAnnotDisable.argsArray.length).toEqual(0);
expect(opListAnnotDisable.lastChunk).toEqual(true);
const opListAnnotEnable = await pdfPage.getOperatorList({
annotationMode: AnnotationMode.ENABLE,
});
expect(opListAnnotEnable.fnArray.length).toBeGreaterThan(150);
expect(opListAnnotEnable.argsArray.length).toBeGreaterThan(150);
expect(opListAnnotEnable.lastChunk).toEqual(true);
const opListAnnotEnableForms = await pdfPage.getOperatorList({
annotationMode: AnnotationMode.ENABLE_FORMS,
});
expect(opListAnnotEnableForms.fnArray.length).toBeGreaterThan(40);
expect(opListAnnotEnableForms.argsArray.length).toBeGreaterThan(40);
expect(opListAnnotEnableForms.lastChunk).toEqual(true);
const opListAnnotEnableStorage = await pdfPage.getOperatorList({
annotationMode: AnnotationMode.ENABLE_STORAGE,
});
expect(opListAnnotEnableStorage.fnArray.length).toBeGreaterThan(170);
expect(opListAnnotEnableStorage.argsArray.length).toBeGreaterThan(170);
expect(opListAnnotEnableStorage.lastChunk).toEqual(true);
// Sanity check to ensure that the `annotationMode` is correctly applied.
expect(opListAnnotDisable.fnArray.length).toBeLessThan(
opListAnnotEnableForms.fnArray.length
);
expect(opListAnnotEnableForms.fnArray.length).toBeLessThan(
opListAnnotEnable.fnArray.length
);
expect(opListAnnotEnable.fnArray.length).toBeLessThan(
opListAnnotEnableStorage.fnArray.length
);
await loadingTask.destroy();
});
it("gets document stats after parsing page", async function () {
const stats = await page.getOperatorList().then(function () {
return pdfDocument.getStats();