From 538017f7a7fc466656cdb6416b685372e44f788a Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Wed, 22 Jul 2020 17:29:35 +0200 Subject: [PATCH 1/2] Add support for radios printing --- src/core/annotation.js | 9 +- src/display/annotation_layer.js | 26 +++++- test/unit/annotation_spec.js | 155 +++++++++++++++++++++++++------- 3 files changed, 150 insertions(+), 40 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index b37c889fd..8ec947ca9 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -1078,16 +1078,19 @@ class ButtonWidgetAnnotation extends WidgetAnnotation { if (!isDict(appearanceStates)) { return; } - const normalAppearanceState = appearanceStates.get("N"); - if (!isDict(normalAppearanceState)) { + const normalAppearance = appearanceStates.get("N"); + if (!isDict(normalAppearance)) { return; } - for (const key of normalAppearanceState.getKeys()) { + for (const key of normalAppearance.getKeys()) { if (key !== "Off") { this.data.buttonValue = key; break; } } + + this.checkedAppearance = normalAppearance.get(this.data.buttonValue); + this.uncheckedAppearance = normalAppearance.get("Off") || null; } _processPushButton(params) { diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index d209c595f..d87083445 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -584,15 +584,35 @@ 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.getOrCreateValue( + id, + data.fieldValue === data.buttonValue + ); const element = document.createElement("input"); - element.disabled = this.data.readOnly; + element.disabled = data.readOnly; element.type = "radio"; - element.name = this.data.fieldName; - if (this.data.fieldValue === this.data.buttonValue) { + element.name = data.fieldName; + if (value) { element.setAttribute("checked", true); } + element.addEventListener("change", function (event) { + const name = event.target.name; + for (const radio of document.getElementsByName(name)) { + if (radio !== event.target) { + storage.setValue( + radio.parentNode.getAttribute("data-annotation-id"), + false + ); + } + } + storage.setValue(id, event.target.checked); + }); + this.container.appendChild(element); return this.container; } diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index 0e8d7ca98..efb2c0462 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -1622,40 +1622,6 @@ describe("annotation", function () { }, done.fail); }); - it("should handle radio buttons with a field value", function (done) { - const parentDict = new Dict(); - parentDict.set("V", Name.get("1")); - - const normalAppearanceStateDict = new Dict(); - normalAppearanceStateDict.set("2", null); - - const appearanceStatesDict = new Dict(); - appearanceStatesDict.set("N", normalAppearanceStateDict); - - buttonWidgetDict.set("Ff", AnnotationFieldFlag.RADIO); - buttonWidgetDict.set("Parent", parentDict); - buttonWidgetDict.set("AP", appearanceStatesDict); - - const buttonWidgetRef = Ref.get(124, 0); - const xref = new XRefMock([ - { ref: buttonWidgetRef, data: buttonWidgetDict }, - ]); - - AnnotationFactory.create( - xref, - buttonWidgetRef, - pdfManagerMock, - idFactoryMock - ).then(({ data }) => { - expect(data.annotationType).toEqual(AnnotationType.WIDGET); - expect(data.checkBox).toEqual(false); - expect(data.radioButton).toEqual(true); - expect(data.fieldValue).toEqual("1"); - expect(data.buttonValue).toEqual("2"); - done(); - }, done.fail); - }); - it("should render checkboxes for printing", function (done) { const appearanceStatesDict = new Dict(); const exportValueOptionsDict = new Dict(); @@ -1712,6 +1678,40 @@ describe("annotation", function () { }, done.fail); }); + it("should handle radio buttons with a field value", function (done) { + const parentDict = new Dict(); + parentDict.set("V", Name.get("1")); + + const normalAppearanceStateDict = new Dict(); + normalAppearanceStateDict.set("2", null); + + const appearanceStatesDict = new Dict(); + appearanceStatesDict.set("N", normalAppearanceStateDict); + + buttonWidgetDict.set("Ff", AnnotationFieldFlag.RADIO); + buttonWidgetDict.set("Parent", parentDict); + buttonWidgetDict.set("AP", appearanceStatesDict); + + const buttonWidgetRef = Ref.get(124, 0); + const xref = new XRefMock([ + { ref: buttonWidgetRef, data: buttonWidgetDict }, + ]); + + AnnotationFactory.create( + xref, + buttonWidgetRef, + pdfManagerMock, + idFactoryMock + ).then(({ data }) => { + expect(data.annotationType).toEqual(AnnotationType.WIDGET); + expect(data.checkBox).toEqual(false); + expect(data.radioButton).toEqual(true); + expect(data.fieldValue).toEqual("1"); + expect(data.buttonValue).toEqual("2"); + done(); + }, done.fail); + }); + it("should handle radio buttons without a field value", function (done) { const normalAppearanceStateDict = new Dict(); normalAppearanceStateDict.set("2", null); @@ -1741,6 +1741,93 @@ describe("annotation", function () { done(); }, done.fail); }); + + it("should render radio buttons for printing", function (done) { + const appearanceStatesDict = new Dict(); + const exportValueOptionsDict = new Dict(); + const normalAppearanceDict = new Dict(); + const checkedAppearanceDict = new Dict(); + const uncheckedAppearanceDict = new Dict(); + + const checkedStream = new StringStream("0.1 0.2 0.3 rg"); + checkedStream.dict = checkedAppearanceDict; + + const uncheckedStream = new StringStream("0.3 0.2 0.1 rg"); + uncheckedStream.dict = uncheckedAppearanceDict; + + checkedAppearanceDict.set("BBox", [0, 0, 8, 8]); + checkedAppearanceDict.set("FormType", 1); + checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]); + normalAppearanceDict.set("Checked", checkedStream); + normalAppearanceDict.set("Off", uncheckedStream); + exportValueOptionsDict.set("Off", 0); + exportValueOptionsDict.set("Checked", 1); + appearanceStatesDict.set("D", exportValueOptionsDict); + appearanceStatesDict.set("N", normalAppearanceDict); + + buttonWidgetDict.set("Ff", AnnotationFieldFlag.RADIO); + buttonWidgetDict.set("AP", appearanceStatesDict); + + const buttonWidgetRef = Ref.get(124, 0); + const xref = new XRefMock([ + { ref: buttonWidgetRef, data: buttonWidgetDict }, + ]); + const task = new WorkerTask("test print"); + + AnnotationFactory.create( + xref, + buttonWidgetRef, + pdfManagerMock, + idFactoryMock + ) + .then(annotation => { + const annotationStorage = {}; + annotationStorage[annotation.data.id] = true; + return Promise.all([ + annotation, + annotation.getOperatorList( + partialEvaluator, + task, + false, + annotationStorage + ), + ]); + }, done.fail) + .then(([annotation, opList]) => { + expect(opList.argsArray.length).toEqual(3); + expect(opList.fnArray).toEqual([ + OPS.beginAnnotation, + OPS.setFillRGBColor, + OPS.endAnnotation, + ]); + expect(opList.argsArray[1]).toEqual( + new Uint8ClampedArray([26, 51, 76]) + ); + return annotation; + }, done.fail) + .then(annotation => { + const annotationStorage = {}; + annotationStorage[annotation.data.id] = false; + return annotation.getOperatorList( + partialEvaluator, + task, + false, + annotationStorage + ); + }, done.fail) + .then(opList => { + expect(opList.argsArray.length).toEqual(3); + expect(opList.fnArray).toEqual([ + OPS.beginAnnotation, + OPS.setFillRGBColor, + OPS.endAnnotation, + ]); + expect(opList.argsArray[1]).toEqual( + new Uint8ClampedArray([76, 51, 26]) + ); + done(); + }, done.fail); + }); }); describe("ChoiceWidgetAnnotation", function () { From f22e702ecc18a47417286368cf65e58bdd65d306 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 31 Jul 2020 14:39:11 +0200 Subject: [PATCH 2/2] Amend test for checkboxes printing to test the unchecked appearance --- test/unit/annotation_spec.js | 38 ++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index efb2c0462..cfec38789 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -1627,14 +1627,19 @@ describe("annotation", function () { const exportValueOptionsDict = new Dict(); const normalAppearanceDict = new Dict(); const checkedAppearanceDict = new Dict(); + const uncheckedAppearanceDict = new Dict(); - const stream = new StringStream("0.1 0.2 0.3 rg"); - stream.dict = checkedAppearanceDict; + const checkedStream = new StringStream("0.1 0.2 0.3 rg"); + checkedStream.dict = checkedAppearanceDict; + + const uncheckedStream = new StringStream("0.3 0.2 0.1 rg"); + uncheckedStream.dict = uncheckedAppearanceDict; checkedAppearanceDict.set("BBox", [0, 0, 8, 8]); checkedAppearanceDict.set("FormType", 1); checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]); - normalAppearanceDict.set("Checked", stream); + normalAppearanceDict.set("Checked", checkedStream); + normalAppearanceDict.set("Off", uncheckedStream); exportValueOptionsDict.set("Off", 0); exportValueOptionsDict.set("Checked", 1); appearanceStatesDict.set("D", exportValueOptionsDict); @@ -1657,6 +1662,31 @@ describe("annotation", function () { .then(annotation => { const annotationStorage = {}; annotationStorage[annotation.data.id] = true; + return Promise.all([ + annotation, + annotation.getOperatorList( + partialEvaluator, + task, + false, + annotationStorage + ), + ]); + }, done.fail) + .then(([annotation, opList]) => { + expect(opList.argsArray.length).toEqual(3); + expect(opList.fnArray).toEqual([ + OPS.beginAnnotation, + OPS.setFillRGBColor, + OPS.endAnnotation, + ]); + expect(opList.argsArray[1]).toEqual( + new Uint8ClampedArray([26, 51, 76]) + ); + return annotation; + }, done.fail) + .then(annotation => { + const annotationStorage = {}; + annotationStorage[annotation.data.id] = false; return annotation.getOperatorList( partialEvaluator, task, @@ -1672,7 +1702,7 @@ describe("annotation", function () { OPS.endAnnotation, ]); expect(opList.argsArray[1]).toEqual( - new Uint8ClampedArray([26, 51, 76]) + new Uint8ClampedArray([76, 51, 26]) ); done(); }, done.fail);