Skip to content

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

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

Merged
merged 2 commits into from
Aug 25, 2021
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
12 changes: 9 additions & 3 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@
"description": "Whether to prevent the extension from reporting the extension and browser version to the extension developers.",
"default": false
},
"renderInteractiveForms": {
"type": "boolean",
"default": true
"annotationMode": {
"type": "integer",
"enum": [
0,
1,
2,
3
],
"default": 2
},
"enableScripting": {
"type": "boolean",
Expand Down
5 changes: 4 additions & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ class Page {
// page's operator list to render them.
return Promise.all([pageListPromise, this._parsedAnnotations]).then(
function ([pageOpList, annotations]) {
if (annotations.length === 0) {
if (
annotations.length === 0 ||
intent & RenderingIntentFlag.ANNOTATIONS_DISABLE
) {
pageOpList.flush(true);
return { length: pageOpList.totalLength };
}
Expand Down
18 changes: 9 additions & 9 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const DEFAULT_TAB_INDEX = 1000;
* @property {AnnotationStorage} [annotationStorage]
* @property {string} [imageResourcesPath] - Path for image resources, mainly
* for annotation icons. Include trailing slash.
* @property {boolean} renderInteractiveForms
* @property {boolean} renderForms
* @property {Object} svgFactory
* @property {boolean} [enableScripting]
* @property {boolean} [hasJSActions]
Expand Down Expand Up @@ -154,7 +154,7 @@ class AnnotationElement {
this.linkService = parameters.linkService;
this.downloadManager = parameters.downloadManager;
this.imageResourcesPath = parameters.imageResourcesPath;
this.renderInteractiveForms = parameters.renderInteractiveForms;
this.renderForms = parameters.renderForms;
this.svgFactory = parameters.svgFactory;
this.annotationStorage = parameters.annotationStorage;
this.enableScripting = parameters.enableScripting;
Expand Down Expand Up @@ -676,7 +676,7 @@ class WidgetAnnotationElement extends AnnotationElement {
class TextWidgetAnnotationElement extends WidgetAnnotationElement {
constructor(parameters) {
const isRenderable =
parameters.renderInteractiveForms ||
parameters.renderForms ||
(!parameters.data.hasAppearance && !!parameters.data.fieldValue);
super(parameters, { isRenderable });
}
Expand All @@ -700,7 +700,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
this.container.className = "textWidgetAnnotation";

let element = null;
if (this.renderInteractiveForms) {
if (this.renderForms) {
// NOTE: We cannot set the values using `element.value` below, since it
// prevents the AnnotationLayer rasterizer in `test/driver.js`
// from parsing the elements correctly for the reference tests.
Expand Down Expand Up @@ -952,7 +952,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {

class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
constructor(parameters) {
super(parameters, { isRenderable: parameters.renderInteractiveForms });
super(parameters, { isRenderable: parameters.renderForms });
}

render() {
Expand Down Expand Up @@ -1031,7 +1031,7 @@ class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {

class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
constructor(parameters) {
super(parameters, { isRenderable: parameters.renderInteractiveForms });
super(parameters, { isRenderable: parameters.renderForms });
}

render() {
Expand Down Expand Up @@ -1123,7 +1123,7 @@ class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {

class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
constructor(parameters) {
super(parameters, { isRenderable: parameters.renderInteractiveForms });
super(parameters, { isRenderable: parameters.renderForms });
}

render() {
Expand Down Expand Up @@ -2033,7 +2033,7 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
* @property {DownloadManager} downloadManager
* @property {string} [imageResourcesPath] - Path for image resources, mainly
* for annotation icons. Include trailing slash.
* @property {boolean} renderInteractiveForms
* @property {boolean} renderForms
* @property {boolean} [enableScripting] - Enable embedded script execution.
* @property {boolean} [hasJSActions] - Some fields have JS actions.
* The default value is `false`.
Expand Down Expand Up @@ -2076,7 +2076,7 @@ class AnnotationLayer {
linkService: parameters.linkService,
downloadManager: parameters.downloadManager,
imageResourcesPath: parameters.imageResourcesPath || "",
renderInteractiveForms: parameters.renderInteractiveForms !== false,
renderForms: parameters.renderForms !== false,
svgFactory: new DOMSVGFactory(),
annotationStorage:
parameters.annotationStorage || new AnnotationStorage(),
Expand Down
110 changes: 85 additions & 25 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import {
AbortException,
AnnotationMode,
assert,
createPromiseCapability,
getVerbosityLevel,
Expand Down Expand Up @@ -1135,9 +1136,18 @@ class PDFDocumentProxy {
* the `PDFPageProxy.getViewport` method.
* @property {string} [intent] - Rendering intent, can be 'display', 'print',
* or 'any'. The default value is 'display'.
* @property {boolean} [renderInteractiveForms] - Whether or not interactive
* form elements are rendered in the display layer. If so, we do not render
* them on the canvas as well. The default value is `false`.
* @property {number} [annotationMode] Controls which annotations are rendered
* onto the canvas, for annotations with appearance-data; the values from
* {@link AnnotationMode} should be used. The following values are supported:
* - `AnnotationMode.DISABLE`, which disables all annotations.
* - `AnnotationMode.ENABLE`, which includes all possible annotations (thus
* it also depends on the `intent`-option, see above).
* - `AnnotationMode.ENABLE_FORMS`, which excludes annotations that contain
* interactive form elements (those will be rendered in the display layer).
* - `AnnotationMode.ENABLE_STORAGE`, which includes all possible annotations
* (as above) but where interactive form elements are updated with data
* from the {@link AnnotationStorage}-instance; useful e.g. for printing.
* The default value is `AnnotationMode.ENABLE`.
* @property {Array<any>} [transform] - Additional transform, applied just
* before viewport transform.
* @property {Object} [imageLayer] - An object that has `beginLayout`,
Expand All @@ -1149,9 +1159,6 @@ class PDFDocumentProxy {
* <color> value, a `CanvasGradient` object (a linear or radial gradient) or
* a `CanvasPattern` object (a repetitive image). The default value is
* 'rgb(255,255,255)'.
* @property {boolean} [includeAnnotationStorage] - Render stored interactive
* form element data, from the {@link AnnotationStorage}-instance, onto the
* canvas itself; useful e.g. for printing. The default value is `false`.
* @property {Promise<OptionalContentConfig>} [optionalContentConfigPromise] -
* A promise that should resolve with an {@link OptionalContentConfig}
* created from `PDFDocumentProxy.getOptionalContentConfig`. If `null`,
Expand All @@ -1165,6 +1172,18 @@ class PDFDocumentProxy {
* @typedef {Object} GetOperatorListParameters
* @property {string} [intent] - Rendering intent, can be 'display', 'print',
* or 'any'. The default value is 'display'.
* @property {number} [annotationMode] Controls which annotations are included
* in the operatorList, for annotations with appearance-data; the values from
* {@link AnnotationMode} should be used. The following values are supported:
* - `AnnotationMode.DISABLE`, which disables all annotations.
* - `AnnotationMode.ENABLE`, which includes all possible annotations (thus
* it also depends on the `intent`-option, see above).
* - `AnnotationMode.ENABLE_FORMS`, which excludes annotations that contain
* interactive form elements (those will be rendered in the display layer).
* - `AnnotationMode.ENABLE_STORAGE`, which includes all possible annotations
* (as above) but where interactive form elements are updated with data
* from the {@link AnnotationStorage}-instance; useful e.g. for printing.
* The default value is `AnnotationMode.ENABLE`.
*/

/**
Expand Down Expand Up @@ -1280,7 +1299,7 @@ class PDFPageProxy {
* {Array} of the annotation objects.
*/
getAnnotations({ intent = "display" } = {}) {
const intentArgs = this._transport.getRenderingIntent(intent, {});
const intentArgs = this._transport.getRenderingIntent(intent);

let promise = this._annotationPromises.get(intentArgs.cacheKey);
if (!promise) {
Expand Down Expand Up @@ -1324,22 +1343,48 @@ class PDFPageProxy {
canvasContext,
viewport,
intent = "display",
renderInteractiveForms = false,
annotationMode = AnnotationMode.ENABLE,
transform = null,
imageLayer = null,
canvasFactory = null,
background = null,
includeAnnotationStorage = false,
optionalContentConfigPromise = null,
}) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
if (arguments[0]?.renderInteractiveForms !== undefined) {
deprecated(
"render no longer accepts the `renderInteractiveForms`-option, " +
"please use the `annotationMode`-option instead."
);
if (
arguments[0].renderInteractiveForms === true &&
annotationMode === AnnotationMode.ENABLE
) {
annotationMode = AnnotationMode.ENABLE_FORMS;
}
}
if (arguments[0]?.includeAnnotationStorage !== undefined) {
deprecated(
"render no longer accepts the `includeAnnotationStorage`-option, " +
"please use the `annotationMode`-option instead."
);
if (
arguments[0].includeAnnotationStorage === true &&
annotationMode === AnnotationMode.ENABLE
) {
annotationMode = AnnotationMode.ENABLE_STORAGE;
}
}
}

if (this._stats) {
this._stats.time("Overall");
}

const intentArgs = this._transport.getRenderingIntent(intent, {
renderForms: renderInteractiveForms === true,
includeAnnotationStorage: includeAnnotationStorage === true,
});
const intentArgs = this._transport.getRenderingIntent(
intent,
annotationMode
);
// If there was a pending destroy, cancel it so no cleanup happens during
// this call to render.
this.pendingCleanup = false;
Expand Down Expand Up @@ -1460,7 +1505,10 @@ class PDFPageProxy {
* @returns {Promise<PDFOperatorList>} A promise resolved with an
* {@link PDFOperatorList} object that represents the page's operator list.
*/
getOperatorList({ intent = "display" } = {}) {
getOperatorList({
intent = "display",
annotationMode = AnnotationMode.ENABLE,
} = {}) {
function operatorListChanged() {
if (intentState.operatorList.lastChunk) {
intentState.opListReadCapability.resolve(intentState.operatorList);
Expand All @@ -1469,9 +1517,11 @@ class PDFPageProxy {
}
}

const intentArgs = this._transport.getRenderingIntent(intent, {
isOpList: true,
});
const intentArgs = this._transport.getRenderingIntent(
intent,
annotationMode,
/* isOpList = */ true
);
let intentState = this._intentStates.get(intentArgs.cacheKey);
if (!intentState) {
intentState = Object.create(null);
Expand Down Expand Up @@ -1792,7 +1842,7 @@ class PDFPageProxy {
}
}
intentState.streamReader
.cancel(new AbortException(reason?.message))
.cancel(new AbortException(reason.message))
.catch(() => {
// Avoid "Uncaught promise" messages in the console.
});
Expand Down Expand Up @@ -2351,7 +2401,8 @@ class WorkerTransport {

getRenderingIntent(
intent,
{ renderForms = false, includeAnnotationStorage = false, isOpList = false }
annotationMode = AnnotationMode.ENABLE,
isOpList = false
) {
let renderingIntent = RenderingIntentFlag.DISPLAY; // Default value.
let lastModified = "";
Expand All @@ -2369,13 +2420,22 @@ class WorkerTransport {
warn(`getRenderingIntent - invalid intent: ${intent}`);
}

if (renderForms) {
renderingIntent += RenderingIntentFlag.ANNOTATIONS_FORMS;
}
if (includeAnnotationStorage) {
renderingIntent += RenderingIntentFlag.ANNOTATIONS_STORAGE;
switch (annotationMode) {
case AnnotationMode.DISABLE:
renderingIntent += RenderingIntentFlag.ANNOTATIONS_DISABLE;
break;
case AnnotationMode.ENABLE:
break;
case AnnotationMode.ENABLE_FORMS:
renderingIntent += RenderingIntentFlag.ANNOTATIONS_FORMS;
break;
case AnnotationMode.ENABLE_STORAGE:
renderingIntent += RenderingIntentFlag.ANNOTATIONS_STORAGE;

lastModified = this.annotationStorage.lastModified;
lastModified = this.annotationStorage.lastModified;
break;
default:
warn(`getRenderingIntent - invalid annotationMode: ${annotationMode}`);
}

if (isOpList) {
Expand Down
34 changes: 18 additions & 16 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ import {
RenderingCancelledException,
} from "./display/display_utils.js";
import {
build,
getDocument,
LoopbackPort,
PDFDataRangeTransport,
PDFWorker,
setPDFNetworkStreamFactory,
version,
} from "./display/api.js";
import {
AnnotationMode,
CMapCompressionType,
createObjectURL,
createPromiseCapability,
Expand All @@ -52,6 +44,15 @@ import {
Util,
VerbosityLevel,
} from "./shared/util.js";
import {
build,
getDocument,
LoopbackPort,
PDFDataRangeTransport,
PDFWorker,
setPDFNetworkStreamFactory,
version,
} from "./display/api.js";
import { AnnotationLayer } from "./display/annotation_layer.js";
import { GlobalWorkerOptions } from "./display/worker_options.js";
import { isNodeJS } from "./shared/is_node.js";
Expand Down Expand Up @@ -110,14 +111,8 @@ export {
PDFDateString,
RenderingCancelledException,
getXfaPageViewport,
// From "./display/api.js":
build,
getDocument,
LoopbackPort,
PDFDataRangeTransport,
PDFWorker,
version,
// From "./shared/util.js":
AnnotationMode,
CMapCompressionType,
createObjectURL,
createPromiseCapability,
Expand All @@ -133,6 +128,13 @@ export {
UNSUPPORTED_FEATURES,
Util,
VerbosityLevel,
// From "./display/api.js":
build,
getDocument,
LoopbackPort,
PDFDataRangeTransport,
PDFWorker,
version,
// From "./display/annotation_layer.js":
AnnotationLayer,
// From "./display/worker_options.js":
Expand Down
Loading