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
346 changes: 309 additions & 37 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
7 changes: 5 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,7 @@ class PartialEvaluator {
})
.then(translated => {
state.font = translated.font;
state.fontSize = fontSize;
translated.send(this.handler);
return translated.loadedName;
});
Expand Down Expand Up @@ -3801,4 +3804,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
52 changes: 45 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 @@ -1450,6 +1487,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
7 changes: 7 additions & 0 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
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,
calixteman marked this conversation as resolved.
Show resolved Hide resolved
imageResourcesPath = "",
renderInteractiveForms = false,
l10n = NullL10n
Expand All @@ -135,6 +139,7 @@ class DefaultAnnotationLayerFactory {
renderInteractiveForms,
linkService: new SimpleLinkService(),
l10n,
annotationStorage,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const defaultOptions = {
},
renderInteractiveForms: {
/** @type {boolean} */
value: false,
value: true,
calixteman marked this conversation as resolved.
Show resolved Hide resolved
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
},
sidebarViewOnLoad: {
Expand Down
1 change: 1 addition & 0 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ class BaseViewer {
renderInteractiveForms,
linkService: this.linkService,
downloadManager: this.downloadManager,
annotationStorage: this.pdfDocument.annotationStorage,
l10n,
});
}
Expand Down
3 changes: 2 additions & 1 deletion 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.getDict(),
};
return pdfPage.render(renderContext).promise;
})
Expand Down Expand Up @@ -109,7 +110,7 @@ PDFPrintServiceFactory.instance = {
return shadow(this, "supportsPrinting", value);
},

createPrintService(pdfDocument, pagesOverview, printContainer) {
createPrintService(pdfDocument, pagesOverview, printContainer, l10n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see why this change is necessary?

return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
},
};
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.getDict(),
};
return pdfPage.render(renderContext).promise;
})
Expand Down