1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

[JS] Take into account all the required fields for some computations

- Fix Field::getArray in order to collect only the fields which have a value;
- Fix AFSimple_Calculate:
  * allow to have a string with a list of field names as argument;
  * since a field can be non-terminal, use Field::getArray to collect
    the field under it and then apply the calculation on all the descendants.
This commit is contained in:
Calixte Denizet 2022-10-13 15:48:01 +02:00
parent c6cc7c6e6a
commit e756bb69e4
4 changed files with 154 additions and 10 deletions

View file

@ -500,14 +500,18 @@ class AForm {
const event = globalThis.event;
const values = [];
cFields = this.AFMakeArrayFromList(cFields);
for (const cField of cFields) {
const field = this._document.getField(cField);
if (!field) {
continue;
}
const number = this.AFMakeNumber(field.value);
if (number !== null) {
values.push(number);
for (const child of field.getArray()) {
const number = this.AFMakeNumber(child.value);
if (number !== null) {
values.push(number);
}
}
}

View file

@ -889,6 +889,24 @@ class Doc extends PDFObject {
return children;
}
_getTerminalChildren(fieldName) {
// Get all the descendants which have a value.
const children = [];
const len = fieldName.length;
for (const [name, field] of this._fields.entries()) {
if (name.startsWith(fieldName)) {
const finalPart = name.slice(len);
if (
field.obj._hasValue &&
(finalPart === "" || finalPart.startsWith("."))
) {
children.push(field.wrapped);
}
}
}
return children;
}
getIcon() {
/* Not implemented */
}

View file

@ -76,6 +76,7 @@ class Field extends PDFObject {
this._fillColor = data.fillColor || ["T"];
this._isChoice = Array.isArray(data.items);
this._items = data.items || [];
this._hasValue = data.hasOwnProperty("value");
this._page = data.page || 0;
this._strokeColor = data.strokeColor || ["G", 0];
this._textColor = data.textColor || ["G", 0];
@ -393,15 +394,32 @@ class Field extends PDFObject {
}
getArray() {
// Gets the array of terminal child fields (that is, fields that can have
// a value for this Field object, the parent field).
if (this._kidIds) {
return this._kidIds.map(id => this._appObjects[id].wrapped);
const array = [];
const fillArrayWithKids = kidIds => {
for (const id of kidIds) {
const obj = this._appObjects[id];
if (!obj) {
continue;
}
if (obj.obj._hasValue) {
array.push(obj.wrapped);
}
if (obj.obj._kidIds) {
fillArrayWithKids(obj.obj._kidIds);
}
}
};
fillArrayWithKids(this._kidIds);
return array;
}
if (this._children === null) {
this._children = this._document.obj
._getChildren(this._fieldPath)
.map(child => child.wrapped);
this._children = this._document.obj._getTerminalChildren(this._fieldPath);
}
return this._children;
}