Skip to content

Commit 0036303

Browse files
committed
Simplify acroform options mapping
1 parent 18b0baa commit 0036303

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

lib/mixins/acroform.js

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,36 @@ const FORMAT_DEFAULT = {
4040
},
4141
};
4242

43-
function mapType(type, pdfObject) {
43+
function mapTypeAndFlags(type, userOptions, pdfObject) {
44+
let flags = userOptions.Ff ?? 0;
45+
4446
if (type === 'text') {
4547
pdfObject.FT = 'Tx';
4648
} else if (type === 'pushButton') {
4749
pdfObject.FT = 'Btn';
48-
pdfObject.pushButton = true;
50+
flags |= FIELD_FLAGS.pushButton;
4951
} else if (type === 'radioButton') {
5052
pdfObject.FT = 'Btn';
51-
pdfObject.radioButton = true;
53+
flags |= FIELD_FLAGS.radioButton;
5254
} else if (type === 'checkbox') {
5355
pdfObject.FT = 'Btn';
5456
} else if (type === 'combo') {
5557
pdfObject.FT = 'Ch';
56-
pdfObject.combo = true;
58+
flags |= FIELD_FLAGS.combo;
5759
} else if (type === 'list') {
5860
pdfObject.FT = 'Ch';
59-
} else {
61+
} else if (type) {
6062
throw new Error(`Invalid form annotation type '${type}'`);
6163
}
62-
return pdfObject;
63-
}
6464

65-
function mapFlags(userOptions, pdfObject) {
66-
let result = userOptions.Ff ?? 0;
6765
Object.keys(userOptions).forEach((key) => {
68-
if (FIELD_FLAGS[key]) {
69-
if (userOptions[key]) {
70-
result |= FIELD_FLAGS[key];
71-
}
72-
}
73-
});
74-
Object.keys(pdfObject).forEach((key) => {
75-
if (FIELD_FLAGS[key]) {
76-
if (pdfObject[key]) {
77-
result |= FIELD_FLAGS[key];
78-
}
79-
delete pdfObject[key];
66+
if (FIELD_FLAGS[key] && userOptions[key]) {
67+
flags |= FIELD_FLAGS[key];
8068
}
8169
});
8270

83-
if (result !== 0) {
84-
pdfObject.Ff = result;
71+
if (flags !== 0) {
72+
pdfObject.Ff = flags;
8573
}
8674
}
8775

@@ -315,11 +303,7 @@ export default {
315303
}
316304
const pdfObject = {};
317305

318-
if (type) {
319-
mapType(type, pdfObject);
320-
}
321-
322-
mapFlags(options, pdfObject);
306+
mapTypeAndFlags(type, options, pdfObject);
323307
mapJustify(options, pdfObject);
324308
this._mapFont(options, pdfObject);
325309
mapStrings(options, pdfObject);

0 commit comments

Comments
 (0)