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

Add form printing support #12076

Closed
wants to merge 14 commits into from
411 changes: 364 additions & 47 deletions src/core/annotation.js

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,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 @@ -302,7 +309,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
10 changes: 8 additions & 2 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,12 @@ class PartialEvaluator {

handleSetFont(resources, fontArgs, fontRef, operatorList, task, state) {
// TODO(mack): Not needed?
var fontName;
var fontName,
fontSize = 0;
if (fontArgs) {
fontArgs = fontArgs.slice();
fontName = fontArgs[0].name;
fontSize = fontArgs[1];
}

return this.loadFont(fontName, fontRef, resources)
Expand Down Expand Up @@ -783,6 +785,8 @@ class PartialEvaluator {
})
.then(translated => {
state.font = translated.font;
state.fontSize = fontSize;
state.fontName = fontName;
translated.send(this.handler);
return translated.loadedName;
});
Expand Down Expand Up @@ -3518,6 +3522,8 @@ class EvalState {
constructor() {
this.ctm = new Float32Array(IDENTITY_MATRIX);
this.font = null;
this.fontSize = 0;
this.fontName = null;
this.textRenderingMode = TextRenderingMode.FILL;
this.fillColorSpace = ColorSpace.singletons.gray;
this.strokeColorSpace = ColorSpace.singletons.gray;
Expand Down Expand Up @@ -3801,4 +3807,4 @@ class EvaluatorPreprocessor {
}
}

export { PartialEvaluator };
export { EvalState, PartialEvaluator };
2 changes: 2 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ class WorkerMessageHandler {
"GetOperatorList",
function wphSetupRenderPage(data, sink) {
var pageIndex = data.pageIndex;
const annotationStorage = data.annotationStorage;
pdfManager.getPage(pageIndex).then(function (page) {
var task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
startWorkerTask(task);
Expand All @@ -532,6 +533,7 @@ class WorkerMessageHandler {
task,
intent: data.intent,
renderInteractiveForms: data.renderInteractiveForms,
annotationStorage,
})
.then(
function (operatorListInfo) {
Expand Down
61 changes: 54 additions & 7 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 @@ -438,6 +439,8 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
*/
render() {
const TEXT_ALIGNMENT = ["left", "center", "right"];
const storage = this.annotationStorage;
const id = this.data.id;

this.container.className = "textWidgetAnnotation";

Expand All @@ -446,15 +449,21 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
// 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.
const textContent = storage.getOrCreate(id, this.data.fieldValue);

if (this.data.multiLine) {
element = document.createElement("textarea");
element.textContent = this.data.fieldValue;
element.textContent = textContent;
} else {
element = document.createElement("input");
element.type = "text";
element.setAttribute("value", this.data.fieldValue);
element.setAttribute("value", textContent);
}

element.addEventListener("change", function (event) {
storage.setValue(id, event.target.value);
});

element.disabled = this.data.readOnly;
element.name = this.data.fieldName;

Expand Down Expand Up @@ -542,15 +551,26 @@ class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
*/
render() {
this.container.className = "buttonWidgetAnnotation checkBox";
const storage = this.annotationStorage;
const data = this.data;
const id = data.id;
const value = storage.getOrCreate(
id,
data.fieldValue && data.fieldValue !== "Off"
);

const element = document.createElement("input");
element.disabled = this.data.readOnly;
element.disabled = data.readOnly;
element.type = "checkbox";
element.name = this.data.fieldName;
if (this.data.fieldValue && this.data.fieldValue !== "Off") {
element.setAttribute("checked", true);
element.name = data.fieldName;
if (value) {
element.setAttribute("checked", value);
}

element.addEventListener("change", function (event) {
storage.setValue(id, event.target.checked);
});

this.container.appendChild(element);
return this.container;
}
Expand All @@ -571,15 +591,32 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
*/
render() {
this.container.className = "buttonWidgetAnnotation radioButton";
const storage = this.annotationStorage;
const data = this.data;
const id = data.id;
const value = storage.getOrCreate(id, data.fieldValue === data.buttonValue);

const element = document.createElement("input");
element.disabled = this.data.readOnly;
element.type = "radio";
element.name = this.data.fieldName;
if (this.data.fieldValue === this.data.buttonValue) {
if (value) {
element.setAttribute("checked", true);
}

element.addEventListener("change", function (e) {
calixteman marked this conversation as resolved.
Show resolved Hide resolved
const name = e.target.name;
for (const el of document.getElementsByName(name)) {
if (el !== e.target) {
storage.setValue(
el.parentNode.getAttribute("data-annotation-id"),
false
);
}
}
storage.setValue(id, e.target.checked);
});

this.container.appendChild(element);
return this.container;
}
Expand Down Expand Up @@ -623,6 +660,8 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
const selectElement = document.createElement("select");
selectElement.disabled = this.data.readOnly;
selectElement.name = this.data.fieldName;
const storage = this.annotationStorage;
const id = this.data.id;

if (!this.data.combo) {
// List boxes have a size and (optionally) multiple selection.
Expand All @@ -643,6 +682,13 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
selectElement.appendChild(optionElement);
}

selectElement.addEventListener("change", function (event) {
const options = event.target.options;
const value = options[options.selectedIndex].text;

storage.setValue(id, value);
});

this.container.appendChild(selectElement);
return this.container;
}
Expand Down Expand Up @@ -1450,6 +1496,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 2014 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(storage = {}) {
this._storage = storage;
}

/**
* Get the value for a given key if it doesn't exist
* or store and return the defaultValue
*
* @public
* @memberof AnnotationStorage
* @param {String} key
* @param {Object} defaultValue
* @returns {Object}
*/
getOrCreate(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;
}

getDict() {
return this._storage;
}
}

export { AnnotationStorage };
12 changes: 12 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 annotations.
*/
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 @@ -1044,10 +1054,12 @@ class PDFPageProxy {
if (this._stats) {
this._stats.time("Page Request");
}

this._pumpOperatorList({
pageIndex: this._pageIndex,
intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true,
annotationStorage,
});
}

Expand Down
11 changes: 9 additions & 2 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ describe("annotation", function () {
class PDFManagerMock {
constructor(params) {
this.docBaseUrl = params.docBaseUrl || null;
this.pdfDocument = {
acroForm: new Dict(),
};
}

ensure(obj, prop, args) {
Expand All @@ -49,6 +52,10 @@ describe("annotation", function () {
}
});
}

ensureDoc(prop, args) {
return this.ensure(this.pdfDocument, prop, args);
}
}

let pdfManagerMock, idFactoryMock;
Expand Down Expand Up @@ -1384,7 +1391,7 @@ describe("annotation", function () {
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.textAlignment).toEqual(null);
expect(data.textAlignment).toEqual(0);
expect(data.maxLen).toEqual(null);
expect(data.readOnly).toEqual(false);
expect(data.multiLine).toEqual(false);
Expand All @@ -1408,7 +1415,7 @@ describe("annotation", function () {
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.textAlignment).toEqual(null);
expect(data.textAlignment).toEqual(0);
expect(data.maxLen).toEqual(null);
expect(data.readOnly).toEqual(false);
expect(data.multiLine).toEqual(false);
Expand Down
Loading