Skip to content

[api-minor] Add an annotation storage in order to save annotation data in acroforms #12106

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 1 commit into from
Jul 24, 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
20 changes: 15 additions & 5 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class Annotation {
});
}

getOperatorList(evaluator, task, renderForms) {
getOperatorList(evaluator, task, renderForms, annotationStorage) {
if (!this.appearance) {
return Promise.resolve(new OperatorList());
}
Expand Down Expand Up @@ -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
);
}
}

Expand Down Expand Up @@ -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();
Expand Down
16 changes: 14 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 " +
Expand Down
1 change: 1 addition & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ class WorkerMessageHandler {
task,
intent: data.intent,
renderInteractiveForms: data.renderInteractiveForms,
annotationStorage: data.annotationStorage,
})
.then(
function (operatorListInfo) {
Expand Down
2 changes: 2 additions & 0 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
57 changes: 57 additions & 0 deletions src/display/annotation_storage.js
Original file line number Diff line number Diff line change
@@ -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 };
11 changes: 11 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -1004,6 +1013,7 @@ class PDFPageProxy {
imageLayer = null,
canvasFactory = null,
background = null,
annotationStorage = null,
}) {
if (this._stats) {
this._stats.time("Overall");
Expand Down Expand Up @@ -1048,6 +1058,7 @@ class PDFPageProxy {
pageIndex: this._pageIndex,
intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true,
annotationStorage,
});
}

Expand Down
42 changes: 42 additions & 0 deletions test/unit/annotation_storage_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* 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.
*/

import { AnnotationStorage } from "../../src/display/annotation_storage.js";

describe("AnnotationStorage", function () {
describe("GetOrCreateValue", function () {
it("should get and set a new value in the annotation storage", function (done) {
const annotationStorage = new AnnotationStorage();
let value = annotationStorage.getOrCreateValue("123A", "hello world");
expect(value).toEqual("hello world");

// the second argument is the default value to use
// if the key isn't in the storage
value = annotationStorage.getOrCreateValue("123A", "an other string");
expect(value).toEqual("hello world");
done();
});
});

describe("SetValue", function () {
it("should set a new value in the annotation storage", function (done) {
const annotationStorage = new AnnotationStorage();
annotationStorage.setValue("123A", "an other string");
const value = annotationStorage.getAll()["123A"];
expect(value).toEqual("an other string");
done();
});
});
});
1 change: 1 addition & 0 deletions test/unit/clitests.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"spec_files": [
"annotation_spec.js",
"annotation_storage_spec.js",
"api_spec.js",
"bidi_spec.js",
"cff_parser_spec.js",
Expand Down
1 change: 1 addition & 0 deletions test/unit/jasmine-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function initializePDFJS(callback) {
"pdfjs/display/fetch_stream.js",
"pdfjs/shared/is_node.js",
"pdfjs-test/unit/annotation_spec.js",
"pdfjs-test/unit/annotation_storage_spec.js",
"pdfjs-test/unit/api_spec.js",
"pdfjs-test/unit/bidi_spec.js",
"pdfjs-test/unit/cff_parser_spec.js",
Expand Down
5 changes: 5 additions & 0 deletions web/annotation_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AnnotationLayerBuilder {
pdfPage,
linkService,
downloadManager,
annotationStorage,
imageResourcesPath = "",
renderInteractiveForms = false,
l10n = NullL10n,
Expand All @@ -49,6 +50,7 @@ class AnnotationLayerBuilder {
this.imageResourcesPath = imageResourcesPath;
this.renderInteractiveForms = renderInteractiveForms;
this.l10n = l10n;
this.annotationStorage = annotationStorage;

this.div = null;
this._cancelled = false;
Expand All @@ -73,6 +75,7 @@ class AnnotationLayerBuilder {
renderInteractiveForms: this.renderInteractiveForms,
linkService: this.linkService,
downloadManager: this.downloadManager,
annotationStorage: this.annotationStorage,
};

if (this.div) {
Expand Down Expand Up @@ -124,6 +127,7 @@ class DefaultAnnotationLayerFactory {
createAnnotationLayerBuilder(
pageDiv,
pdfPage,
annotationStorage,
imageResourcesPath = "",
renderInteractiveForms = false,
l10n = NullL10n
Expand All @@ -135,6 +139,7 @@ class DefaultAnnotationLayerFactory {
renderInteractiveForms,
linkService: new SimpleLinkService(),
l10n,
annotationStorage,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,7 @@ class BaseViewer {
renderInteractiveForms,
linkService: this.linkService,
downloadManager: this.downloadManager,
annotationStorage: this.pdfDocument.annotationStorage,
l10n,
});
}
Expand Down
1 change: 1 addition & 0 deletions web/firefox_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function composePage(pdfDocument, pageNumber, size, printContainer) {
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }),
intent: "print",
annotationStorage: pdfDocument.annotationStorage.getAll(),
};
return pdfPage.render(renderContext).promise;
})
Expand Down
1 change: 1 addition & 0 deletions web/pdf_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) {
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }),
intent: "print",
annotationStorage: pdfDocument.annotationStorage.getAll(),
};
return pdfPage.render(renderContext).promise;
})
Expand Down