[Form] Don't use field appearances when /NeedAppearances is set to true (bug 1796741)

When a form isn't changed, we used the appearances we had in the file, but when
/NeedAppearances is true, all the appearances have to be regenerated whatever they're.
This commit is contained in:
Calixte Denizet 2022-10-24 15:26:00 +02:00
parent 22225a1eaa
commit 9f95a14e91
4 changed files with 44 additions and 17 deletions

View File

@ -126,6 +126,8 @@ class AnnotationFactory {
let subtype = dict.get("Subtype"); let subtype = dict.get("Subtype");
subtype = subtype instanceof Name ? subtype.name : null; subtype = subtype instanceof Name ? subtype.name : null;
const acroFormDict = acroForm instanceof Dict ? acroForm : Dict.empty;
// Return the right annotation object based on the subtype and field type. // Return the right annotation object based on the subtype and field type.
const parameters = { const parameters = {
xref, xref,
@ -134,10 +136,12 @@ class AnnotationFactory {
subtype, subtype,
id, id,
pdfManager, pdfManager,
acroForm: acroForm instanceof Dict ? acroForm : Dict.empty, acroForm: acroFormDict,
attachments, attachments,
xfaDatasets, xfaDatasets,
collectFields, collectFields,
needAppearances:
!collectFields && acroFormDict.get("NeedAppearances") === true,
pageIndex, pageIndex,
}; };
@ -508,6 +512,7 @@ class Annotation {
} }
this._fallbackFontDict = null; this._fallbackFontDict = null;
this._needAppearances = false;
} }
/** /**
@ -1483,6 +1488,7 @@ class WidgetAnnotation extends Annotation {
const dict = params.dict; const dict = params.dict;
const data = this.data; const data = this.data;
this.ref = params.ref; this.ref = params.ref;
this._needAppearances = params.needAppearances;
data.annotationType = AnnotationType.WIDGET; data.annotationType = AnnotationType.WIDGET;
if (data.fieldName === undefined) { if (data.fieldName === undefined) {
@ -1535,6 +1541,12 @@ class WidgetAnnotation extends Annotation {
this._defaultAppearance this._defaultAppearance
); );
data.hasAppearance =
(this._needAppearances &&
data.fieldValue !== undefined &&
data.fieldValue !== null) ||
data.hasAppearance;
const fieldType = getInheritableProperty({ dict, key: "FT" }); const fieldType = getInheritableProperty({ dict, key: "FT" });
data.fieldType = fieldType instanceof Name ? fieldType.name : null; data.fieldType = fieldType instanceof Name ? fieldType.name : null;
@ -1909,18 +1921,25 @@ class WidgetAnnotation extends Annotation {
rotation = storageEntry.rotation; rotation = storageEntry.rotation;
} }
if (rotation === undefined && value === undefined) { if (
rotation === undefined &&
value === undefined &&
!this._needAppearances
) {
if (!this._hasValueFromXFA || this.appearance) { if (!this._hasValueFromXFA || this.appearance) {
// The annotation hasn't been rendered so use the appearance. // The annotation hasn't been rendered so use the appearance.
return null; return null;
} }
} }
// Empty or it has a trailing whitespace.
const colors = this.getBorderAndBackgroundAppearances(annotationStorage);
if (value === undefined) { if (value === undefined) {
// The annotation has its value in XFA datasets but not in the V field. // The annotation has its value in XFA datasets but not in the V field.
value = this.data.fieldValue; value = this.data.fieldValue;
if (!value) { if (!value) {
return ""; return `/Tx BMC q ${colors}Q EMC`;
} }
} }
@ -1934,7 +1953,7 @@ class WidgetAnnotation extends Annotation {
if (value === "") { if (value === "") {
// the field is empty: nothing to render // the field is empty: nothing to render
return ""; return `/Tx BMC q ${colors}Q EMC`;
} }
if (rotation === undefined) { if (rotation === undefined) {
@ -2024,9 +2043,6 @@ class WidgetAnnotation extends Annotation {
); );
} }
// Empty or it has a trailing whitespace.
const colors = this.getBorderAndBackgroundAppearances(annotationStorage);
if (alignment === 0 || alignment > 2) { if (alignment === 0 || alignment > 2) {
// Left alignment: nothing to do // Left alignment: nothing to do
return ( return (
@ -3020,18 +3036,21 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
return super._getAppearance(evaluator, task, annotationStorage); return super._getAppearance(evaluator, task, annotationStorage);
} }
if (!annotationStorage) { let exportedValue, rotation;
return null; const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
if (storageEntry) {
rotation = storageEntry.rotation;
exportedValue = storageEntry.value;
} }
const storageEntry = annotationStorage.get(this.data.id); if (
if (!storageEntry) { rotation === undefined &&
return null; exportedValue === undefined &&
} !this._needAppearances
) {
const rotation = storageEntry.rotation;
let exportedValue = storageEntry.value;
if (rotation === undefined && exportedValue === undefined) {
// The annotation hasn't been rendered so use the appearance // The annotation hasn't been rendered so use the appearance
return null; return null;
} }

View File

@ -548,3 +548,4 @@
!issue15340.pdf !issue15340.pdf
!bug1795263.pdf !bug1795263.pdf
!issue15597.pdf !issue15597.pdf
!bug1796741.pdf

BIN
test/pdfs/bug1796741.pdf Executable file

Binary file not shown.

View File

@ -6960,5 +6960,12 @@
"md5": "af708acbb22c6c9d268240dcf4a39809", "md5": "af708acbb22c6c9d268240dcf4a39809",
"rounds": 1, "rounds": 1,
"type": "eq" "type": "eq"
},
{ "id": "bug1796741-print",
"file": "pdfs/bug1796741.pdf",
"md5": "d5167d1b0d1b840c9aa258b98ce4fa62",
"rounds": 1,
"type": "eq",
"print": true
} }
] ]