Skip to content

Commit

Permalink
Write the display flags in F entry when saving an annotation (issue 1…
Browse files Browse the repository at this point in the history
…8072)
  • Loading branch information
calixteman committed Oct 1, 2024
1 parent a7e1bf6 commit 7f841bd
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
72 changes: 65 additions & 7 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getModificationDate,
IDENTITY_MATRIX,
info,
isArrayEqual,
LINE_DESCENT_FACTOR,
LINE_FACTOR,
OPS,
Expand Down Expand Up @@ -723,6 +724,38 @@ class Annotation {
return !!(flags & flag);
}

_buildFlags(noView, noPrint) {
let { flags } = this;
if (noView === undefined) {
if (noPrint === undefined) {
return undefined;
}
if (noPrint) {
return flags & ~AnnotationFlag.PRINT;
}
return (flags & ~AnnotationFlag.HIDDEN) | AnnotationFlag.PRINT;
}

if (noView) {
flags |= AnnotationFlag.PRINT;
if (noPrint) {
// display === 1.
return (flags & ~AnnotationFlag.NOVIEW) | AnnotationFlag.HIDDEN;
}
// display === 3.
return (flags & ~AnnotationFlag.HIDDEN) | AnnotationFlag.NOVIEW;
}

flags &= ~(AnnotationFlag.HIDDEN | AnnotationFlag.NOVIEW);
if (noPrint) {
// display === 2.
return flags & ~AnnotationFlag.PRINT;
}

// display === 0.
return flags | AnnotationFlag.PRINT;
}

/**
* @private
*/
Expand Down Expand Up @@ -2073,10 +2106,15 @@ class WidgetAnnotation extends Annotation {

async save(evaluator, task, annotationStorage) {
const storageEntry = annotationStorage?.get(this.data.id);
const flags = this._buildFlags(storageEntry?.noView, storageEntry?.noPrint);
let value = storageEntry?.value,
rotation = storageEntry?.rotation;
if (value === this.data.fieldValue || value === undefined) {
if (!this._hasValueFromXFA && rotation === undefined) {
if (
!this._hasValueFromXFA &&
rotation === undefined &&
flags === undefined
) {
return null;
}
value ||= this.data.fieldValue;
Expand All @@ -2089,7 +2127,8 @@ class WidgetAnnotation extends Annotation {
Array.isArray(value) &&
Array.isArray(this.data.fieldValue) &&
value.length === this.data.fieldValue.length &&
value.every((x, i) => x === this.data.fieldValue[i])
isArrayEqual(value, this.data.fieldValue) &&
flags === undefined
) {
return null;
}
Expand All @@ -2106,7 +2145,7 @@ class WidgetAnnotation extends Annotation {
RenderingIntentFlag.SAVE,
annotationStorage
);
if (appearance === null) {
if (appearance === null && flags === undefined) {
// Appearance didn't change.
return null;
}
Expand Down Expand Up @@ -2134,6 +2173,15 @@ class WidgetAnnotation extends Annotation {
dict.set(key, originalDict.getRaw(key));
}
}
if (flags !== undefined) {
dict.set("F", flags);
if (appearance === null && !needAppearances) {
const ap = originalDict.getRaw("AP");
if (ap) {
dict.set("AP", ap);
}
}
}

const xfa = {
path: this.data.fieldName,
Expand Down Expand Up @@ -3019,10 +3067,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const flags = this._buildFlags(storageEntry?.noView, storageEntry?.noPrint);
let rotation = storageEntry?.rotation,
value = storageEntry?.value;

if (rotation === undefined) {
if (rotation === undefined && flags === undefined) {
if (value === undefined) {
return null;
}
Expand All @@ -3033,10 +3082,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
}

const dict = evaluator.xref.fetchIfRef(this.ref);
let dict = evaluator.xref.fetchIfRef(this.ref);
if (!(dict instanceof Dict)) {
return null;
}
dict = dict.clone();

if (rotation === undefined) {
rotation = this.rotation;
Expand All @@ -3054,6 +3104,9 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
dict.set("V", name);
dict.set("AS", name);
dict.set("M", `D:${getModificationDate()}`);
if (flags !== undefined) {
dict.set("F", flags);
}

const maybeMK = this._getMKDict(rotation);
if (maybeMK) {
Expand All @@ -3071,10 +3124,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const flags = this._buildFlags(storageEntry?.noView, storageEntry?.noPrint);
let rotation = storageEntry?.rotation,
value = storageEntry?.value;

if (rotation === undefined) {
if (rotation === undefined && flags === undefined) {
if (value === undefined) {
return null;
}
Expand All @@ -3085,10 +3139,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
}

const dict = evaluator.xref.fetchIfRef(this.ref);
let dict = evaluator.xref.fetchIfRef(this.ref);
if (!(dict instanceof Dict)) {
return null;
}
dict = dict.clone();

if (value === undefined) {
value = this.data.fieldValue === this.data.buttonValue;
Expand Down Expand Up @@ -3121,6 +3176,9 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {

dict.set("AS", name);
dict.set("M", `D:${getModificationDate()}`);
if (flags !== undefined) {
dict.set("F", flags);
}

const maybeMK = this._getMKDict(rotation);
if (maybeMK) {
Expand Down
3 changes: 0 additions & 3 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,6 @@ class Page {
return this._parsedAnnotations.then(function (annotations) {
const newRefsPromises = [];
for (const annotation of annotations) {
if (!annotation.mustBePrinted(annotationStorage)) {
continue;
}
newRefsPromises.push(
annotation
.save(partialEvaluator, task, annotationStorage)
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,4 @@
!bug1919513.pdf
!issue16038.pdf
!highlight_popup.pdf
!issue18072.pdf
Binary file added test/pdfs/issue18072.pdf
Binary file not shown.
54 changes: 54 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10524,5 +10524,59 @@
"md5": "47262993e04689c327b7ce85396bce99",
"rounds": 1,
"type": "eq"
},
{
"id": "issue18072-save-print",
"file": "pdfs/issue18072.pdf",
"md5": "231fe6f035da1f2ddf84f2a78ada20c5",
"rounds": 1,
"type": "eq",
"save": true,
"print": true,
"annotationStorage": {
"28R": {
"noView": true,
"noPrint": true
},
"29R": {
"noView": true,
"noPrint": false
},
"30R": {
"noView": false,
"noPrint": true
},
"31R": {
"noView": false,
"noPrint": false
}
}
},
{
"id": "issue18072-save-annotations",
"file": "pdfs/issue18072.pdf",
"md5": "231fe6f035da1f2ddf84f2a78ada20c5",
"rounds": 1,
"type": "eq",
"save": true,
"annotations": true,
"annotationStorage": {
"28R": {
"noView": true,
"noPrint": true
},
"29R": {
"noView": true,
"noPrint": false
},
"30R": {
"noView": false,
"noPrint": true
},
"31R": {
"noView": false,
"noPrint": false
}
}
}
]

0 comments on commit 7f841bd

Please sign in to comment.