1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 01:58:06 +02:00

Support textfield and choice widgets for printing

This commit is contained in:
Calixte Denizet 2020-08-05 14:40:31 +02:00
parent 63e33a5895
commit 1747d259f9
7 changed files with 409 additions and 46 deletions

View file

@ -30,7 +30,7 @@ import {
stringToUTF8String,
} from "../../src/shared/util.js";
import { createIdFactory, XRefMock } from "./test_utils.js";
import { Dict, Name, Ref } from "../../src/core/primitives.js";
import { Dict, Name, Ref, RefSetCache } from "../../src/core/primitives.js";
import { Lexer, Parser } from "../../src/core/parser.js";
import { PartialEvaluator } from "../../src/core/evaluator.js";
import { StringStream } from "../../src/core/stream.js";
@ -82,6 +82,7 @@ describe("annotation", function () {
handler: new HandlerMock(),
pageIndex: 0,
idFactory: createIdFactory(/* pageIndex = */ 0),
fontCache: new RefSetCache(),
});
done();
});
@ -1385,18 +1386,35 @@ describe("annotation", function () {
});
describe("TextWidgetAnnotation", function () {
let textWidgetDict;
let textWidgetDict, fontRefObj;
beforeEach(function (done) {
textWidgetDict = new Dict();
textWidgetDict.set("Type", Name.get("Annot"));
textWidgetDict.set("Subtype", Name.get("Widget"));
textWidgetDict.set("FT", Name.get("Tx"));
const helvDict = new Dict();
helvDict.set("BaseFont", Name.get("Helvetica"));
helvDict.set("Type", Name.get("Font"));
helvDict.set("Subtype", Name.get("Type1"));
const fontRef = Ref.get(314, 0);
fontRefObj = { ref: fontRef, data: helvDict };
const resourceDict = new Dict();
const fontDict = new Dict();
fontDict.set("Helv", fontRef);
resourceDict.set("Font", fontDict);
textWidgetDict.set("DA", "/Helv 5 Tf");
textWidgetDict.set("DR", resourceDict);
textWidgetDict.set("Rect", [0, 0, 32, 10]);
done();
});
afterEach(function () {
textWidgetDict = null;
textWidgetDict = fontRefObj = null;
});
it("should handle unknown text alignment, maximum length and flags", function (done) {
@ -1552,6 +1570,109 @@ describe("annotation", function () {
}
promise.then(done, done.fail);
});
it("should render regular text for printing", function (done) {
const textWidgetRef = Ref.get(271, 0);
const xref = new XRefMock([
{ ref: textWidgetRef, data: textWidgetDict },
fontRefObj,
]);
const task = new WorkerTask("test print");
partialEvaluator.xref = xref;
AnnotationFactory.create(
xref,
textWidgetRef,
pdfManagerMock,
idFactoryMock
)
.then(annotation => {
const id = annotation.data.id;
const annotationStorage = {};
annotationStorage[id] = "test\\print";
return annotation._getAppearance(
partialEvaluator,
task,
annotationStorage
);
}, done.fail)
.then(appearance => {
expect(appearance).toEqual(
"/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm" +
" 2.00 2.00 Td (test\\\\print) Tj ET Q EMC"
);
done();
}, done.fail);
});
it("should render auto-sized text for printing", function (done) {
textWidgetDict.set("DA", "/Helv 0 Tf");
const textWidgetRef = Ref.get(271, 0);
const xref = new XRefMock([
{ ref: textWidgetRef, data: textWidgetDict },
fontRefObj,
]);
const task = new WorkerTask("test print");
partialEvaluator.xref = xref;
AnnotationFactory.create(
xref,
textWidgetRef,
pdfManagerMock,
idFactoryMock
)
.then(annotation => {
const id = annotation.data.id;
const annotationStorage = {};
annotationStorage[id] = "test (print)";
return annotation._getAppearance(
partialEvaluator,
task,
annotationStorage
);
}, done.fail)
.then(appearance => {
expect(appearance).toEqual(
"/Tx BMC q BT /Helv 11 Tf 1 0 0 1 0 0 Tm" +
" 2.00 2.00 Td (test \\(print\\)) Tj ET Q EMC"
);
done();
}, done.fail);
});
it("should not render a password for printing", function (done) {
textWidgetDict.set("Ff", AnnotationFieldFlag.PASSWORD);
const textWidgetRef = Ref.get(271, 0);
const xref = new XRefMock([
{ ref: textWidgetRef, data: textWidgetDict },
fontRefObj,
]);
const task = new WorkerTask("test print");
partialEvaluator.xref = xref;
AnnotationFactory.create(
xref,
textWidgetRef,
pdfManagerMock,
idFactoryMock
)
.then(annotation => {
const id = annotation.data.id;
const annotationStorage = {};
annotationStorage[id] = "mypassword";
return annotation._getAppearance(
partialEvaluator,
task,
annotationStorage
);
}, done.fail)
.then(appearance => {
expect(appearance).toEqual(null);
done();
}, done.fail);
});
});
describe("ButtonWidgetAnnotation", function () {
@ -1861,18 +1982,35 @@ describe("annotation", function () {
});
describe("ChoiceWidgetAnnotation", function () {
let choiceWidgetDict;
let choiceWidgetDict, fontRefObj;
beforeEach(function (done) {
choiceWidgetDict = new Dict();
choiceWidgetDict.set("Type", Name.get("Annot"));
choiceWidgetDict.set("Subtype", Name.get("Widget"));
choiceWidgetDict.set("FT", Name.get("Ch"));
const helvDict = new Dict();
helvDict.set("BaseFont", Name.get("Helvetica"));
helvDict.set("Type", Name.get("Font"));
helvDict.set("Subtype", Name.get("Type1"));
const fontRef = Ref.get(314, 0);
fontRefObj = { ref: fontRef, data: helvDict };
const resourceDict = new Dict();
const fontDict = new Dict();
fontDict.set("Helv", fontRef);
resourceDict.set("Font", fontDict);
choiceWidgetDict.set("DA", "/Helv 5 Tf");
choiceWidgetDict.set("DR", resourceDict);
choiceWidgetDict.set("Rect", [0, 0, 32, 10]);
done();
});
afterEach(function () {
choiceWidgetDict = null;
choiceWidgetDict = fontRefObj = null;
});
it("should handle missing option arrays", function (done) {
@ -2128,6 +2266,40 @@ describe("annotation", function () {
done();
}, done.fail);
});
it("should render choice for printing", function (done) {
const choiceWidgetRef = Ref.get(271, 0);
const xref = new XRefMock([
{ ref: choiceWidgetRef, data: choiceWidgetDict },
fontRefObj,
]);
const task = new WorkerTask("test print");
partialEvaluator.xref = xref;
AnnotationFactory.create(
xref,
choiceWidgetRef,
pdfManagerMock,
idFactoryMock
)
.then(annotation => {
const id = annotation.data.id;
const annotationStorage = {};
annotationStorage[id] = "a value";
return annotation._getAppearance(
partialEvaluator,
task,
annotationStorage
);
}, done.fail)
.then(appearance => {
expect(appearance).toEqual(
"/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm" +
" 2.00 2.00 Td (a value) Tj ET Q EMC"
);
done();
}, done.fail);
});
});
describe("LineAnnotation", function () {

View file

@ -66,6 +66,10 @@ function buildGetDocumentParams(filename, options) {
class XRefMock {
constructor(array) {
this._map = Object.create(null);
this.stats = {
streamTypes: Object.create(null),
fontTypes: Object.create(null),
};
for (const key in array) {
const obj = array[key];

View file

@ -17,6 +17,7 @@ import {
bytesToString,
createPromiseCapability,
createValidAbsoluteUrl,
escapeString,
isArrayBuffer,
isBool,
isNum,
@ -314,4 +315,12 @@ describe("util", function () {
});
});
});
describe("escapeString", function () {
it("should escape (, ) and \\", function () {
expect(escapeString("((a\\a))(b(b\\b)b)")).toEqual(
"\\(\\(a\\\\a\\)\\)\\(b\\(b\\\\b\\)b\\)"
);
});
});
});