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

Add an annotation storage in order to save annotation data in acroforms

This commit is contained in:
Calixte Denizet 2020-07-22 13:55:52 +02:00
parent d69fb446bf
commit 584902dbf8
13 changed files with 152 additions and 7 deletions

View file

@ -509,7 +509,7 @@ class Annotation {
});
}
getOperatorList(evaluator, task, renderForms) {
getOperatorList(evaluator, task, renderForms, annotationStorage) {
if (!this.appearance) {
return Promise.resolve(new OperatorList());
}
@ -877,13 +877,18 @@ class WidgetAnnotation extends Annotation {
return !!(this.data.fieldFlags & flag);
}
getOperatorList(evaluator, task, renderForms) {
getOperatorList(evaluator, task, renderForms, annotationStorage) {
// Do not render form elements on the canvas when interactive forms are
// enabled. The display layer is responsible for rendering them instead.
if (renderForms) {
return Promise.resolve(new OperatorList());
}
return super.getOperatorList(evaluator, task, renderForms);
return super.getOperatorList(
evaluator,
task,
renderForms,
annotationStorage
);
}
}
@ -920,9 +925,14 @@ class TextWidgetAnnotation extends WidgetAnnotation {
this.data.maxLen !== null;
}
getOperatorList(evaluator, task, renderForms) {
getOperatorList(evaluator, task, renderForms, annotationStorage) {
if (renderForms || this.appearance) {
return super.getOperatorList(evaluator, task, renderForms);
return super.getOperatorList(
evaluator,
task,
renderForms,
annotationStorage
);
}
const operatorList = new OperatorList();

View file

@ -238,7 +238,14 @@ class Page {
});
}
getOperatorList({ handler, sink, task, intent, renderInteractiveForms }) {
getOperatorList({
handler,
sink,
task,
intent,
renderInteractiveForms,
annotationStorage,
}) {
const contentStreamPromise = this.pdfManager.ensure(
this,
"getContentStream"
@ -301,7 +308,12 @@ class Page {
if (isAnnotationRenderable(annotation, intent)) {
opListPromises.push(
annotation
.getOperatorList(partialEvaluator, task, renderInteractiveForms)
.getOperatorList(
partialEvaluator,
task,
renderInteractiveForms,
annotationStorage
)
.catch(function (reason) {
warn(
"getOperatorList - ignoring annotation data during " +

View file

@ -532,6 +532,7 @@ class WorkerMessageHandler {
task,
intent: data.intent,
renderInteractiveForms: data.renderInteractiveForms,
annotationStorage: data.annotationStorage,
})
.then(
function (operatorListInfo) {

View file

@ -140,6 +140,7 @@ class AnnotationElement {
this.imageResourcesPath = parameters.imageResourcesPath;
this.renderInteractiveForms = parameters.renderInteractiveForms;
this.svgFactory = parameters.svgFactory;
this.annotationStorage = parameters.annotationStorage;
if (isRenderable) {
this.container = this._createContainer(ignoreBorder);
@ -1450,6 +1451,7 @@ class AnnotationLayer {
imageResourcesPath: parameters.imageResourcesPath || "",
renderInteractiveForms: parameters.renderInteractiveForms || false,
svgFactory: new DOMSVGFactory(),
annotationStorage: parameters.annotationStorage,
});
if (element.isRenderable) {
parameters.div.appendChild(element.render());

View file

@ -0,0 +1,57 @@
/* Copyright 2020 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class AnnotationStorage {
constructor() {
this._storage = {};
}
/**
* Get the value for a given key if it exists
* or store and return the default value
*
* @public
* @memberof AnnotationStorage
* @param {string} key
* @param {Object} defaultValue
* @returns {Object}
*/
getOrCreateValue(key, defaultValue) {
if (key in this._storage) {
return this._storage[key];
}
this._storage[key] = defaultValue;
return defaultValue;
}
/**
* Set the value for a given key
*
* @public
* @memberof AnnotationStorage
* @param {string} key
* @param {Object} value
*/
setValue(key, value) {
this._storage[key] = value;
}
getAll() {
return this._storage;
}
}
export { AnnotationStorage };

View file

@ -48,6 +48,7 @@ import {
} from "./display_utils.js";
import { FontFaceObject, FontLoader } from "./font_loader.js";
import { NodeCanvasFactory, NodeCMapReaderFactory } from "./node_utils.js";
import { AnnotationStorage } from "./annotation_storage.js";
import { apiCompatibilityParams } from "./api_compatibility.js";
import { CanvasGraphics } from "./canvas.js";
import { GlobalWorkerOptions } from "./worker_options.js";
@ -576,6 +577,14 @@ class PDFDocumentProxy {
constructor(pdfInfo, transport) {
this._pdfInfo = pdfInfo;
this._transport = transport;
this._annotationStorage = new AnnotationStorage();
}
/**
* @type {AnnotationStorage} Storage for annotation data in forms.
*/
get annotationStorage() {
return this._annotationStorage;
}
/**
@ -1004,6 +1013,7 @@ class PDFPageProxy {
imageLayer = null,
canvasFactory = null,
background = null,
annotationStorage = null,
}) {
if (this._stats) {
this._stats.time("Overall");
@ -1048,6 +1058,7 @@ class PDFPageProxy {
pageIndex: this._pageIndex,
intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true,
annotationStorage,
});
}