Skip to content

Commit

Permalink
JS - Handle correctly hierarchy of fields
Browse files Browse the repository at this point in the history
  - it aims to fix mozilla#13132;
  - annotations can inherit their actions from the parent field;
  - there are some fields which act as a container for other fields:
    - they can be access through js so need to add them with an empty type (nothing in the spec about that but checked in Acrobat);
    - calculation order list (CO) can reference them so need make them through this.getField;
    - getArray method must return kids.
  - field values are number, string, ... depending of their type but nothing in the spec on how to know what's the type:
    - according to the comment for Canonical Format: https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf#page=461
    - it seems that this "type" can be guessed from js action Format (when setting a type in Acrobat DC, the only affected thing is this action).
  - util.scand with an empty string returns the current date.
  • Loading branch information
calixteman committed Mar 23, 2021
1 parent d426ffd commit 63f5f8e
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 89 deletions.
142 changes: 81 additions & 61 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ class Annotation {
this._streams.push(this.appearance);
}

const kids = dict.getRaw("Kids");
const kidsId = [];
if (Array.isArray(kids)) {
for (const kid of kids) {
if (isRef(kid)) {
kidsId.push(kid.toString());
}
}
}

// Expose public properties using a data object.
this.data = {
annotationFlags: this.flags,
Expand All @@ -343,6 +353,9 @@ class Annotation {
modificationDate: this.modificationDate,
rect: this.rectangle,
subtype: params.subtype,
actions: collectActions(params.xref, dict, AnnotationActionEventType),
fieldName: this._constructFieldName(dict),
kidsId,
};

this._fallbackFontDict = null;
Expand Down Expand Up @@ -644,6 +657,15 @@ class Annotation {
* @returns {Object | null}
*/
getFieldObject() {
if (this.data.kidsId.length > 0) {
return {
id: this.data.id,
actions: this.data.actions,
name: this.data.fieldName,
type: "",
kidsId: this.data.kidsId,
};
}
return null;
}

Expand All @@ -670,6 +692,65 @@ class Annotation {
stream.reset();
}
}

/**
* Construct the (fully qualified) field name from the (partial) field
* names of the field and its ancestors.
*
* @private
* @memberof WidgetAnnotation
* @param {Dict} dict - Complete widget annotation dictionary
* @returns {string}
*/
_constructFieldName(dict) {
// Both the `Parent` and `T` fields are optional. While at least one of
// them should be provided, bad PDF generators may fail to do so.
if (!dict.has("T") && !dict.has("Parent")) {
warn("Unknown field name, falling back to empty field name.");
return "";
}

// If no parent exists, the partial and fully qualified names are equal.
if (!dict.has("Parent")) {
return stringToPDFString(dict.get("T"));
}

// Form the fully qualified field name by appending the partial name to
// the parent's fully qualified name, separated by a period.
const fieldName = [];
if (dict.has("T")) {
fieldName.unshift(stringToPDFString(dict.get("T")));
}

let loopDict = dict;
const visited = new RefSet();
if (dict.objId) {
visited.put(dict.objId);
}
while (loopDict.has("Parent")) {
loopDict = loopDict.get("Parent");
if (
!(loopDict instanceof Dict) ||
(loopDict.objId && visited.has(loopDict.objId))
) {
// Even though it is not allowed according to the PDF specification,
// bad PDF generators may provide a `Parent` entry that is not a
// dictionary, but `null` for example (issue 8143).
//
// If parent has been already visited, it means that we're
// in an infinite loop.
break;
}
if (loopDict.objId) {
visited.put(loopDict.objId);
}

if (loopDict.has("T")) {
fieldName.unshift(stringToPDFString(loopDict.get("T")));
}
}
return fieldName.join(".");
}
}

/**
Expand Down Expand Up @@ -995,8 +1076,6 @@ class WidgetAnnotation extends Annotation {
this.ref = params.ref;

data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict);
data.actions = collectActions(params.xref, dict, AnnotationActionEventType);

const fieldValue = getInheritableProperty({
dict,
Expand Down Expand Up @@ -1059,65 +1138,6 @@ class WidgetAnnotation extends Annotation {
}
}

/**
* Construct the (fully qualified) field name from the (partial) field
* names of the field and its ancestors.
*
* @private
* @memberof WidgetAnnotation
* @param {Dict} dict - Complete widget annotation dictionary
* @returns {string}
*/
_constructFieldName(dict) {
// Both the `Parent` and `T` fields are optional. While at least one of
// them should be provided, bad PDF generators may fail to do so.
if (!dict.has("T") && !dict.has("Parent")) {
warn("Unknown field name, falling back to empty field name.");
return "";
}

// If no parent exists, the partial and fully qualified names are equal.
if (!dict.has("Parent")) {
return stringToPDFString(dict.get("T"));
}

// Form the fully qualified field name by appending the partial name to
// the parent's fully qualified name, separated by a period.
const fieldName = [];
if (dict.has("T")) {
fieldName.unshift(stringToPDFString(dict.get("T")));
}

let loopDict = dict;
const visited = new RefSet();
if (dict.objId) {
visited.put(dict.objId);
}
while (loopDict.has("Parent")) {
loopDict = loopDict.get("Parent");
if (
!(loopDict instanceof Dict) ||
(loopDict.objId && visited.has(loopDict.objId))
) {
// Even though it is not allowed according to the PDF specification,
// bad PDF generators may provide a `Parent` entry that is not a
// dictionary, but `null` for example (issue 8143).
//
// If parent has been already visited, it means that we're
// in an infinite loop.
break;
}
if (loopDict.objId) {
visited.put(loopDict.objId);
}

if (loopDict.has("T")) {
fieldName.unshift(stringToPDFString(loopDict.get("T")));
}
}
return fieldName.join(".");
}

/**
* Decode the given form value.
*
Expand Down
33 changes: 20 additions & 13 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,26 @@ function _collectJS(entry, xref, list, parents) {

function collectActions(xref, dict, eventType) {
const actions = Object.create(null);
if (dict.has("AA")) {
const additionalActions = dict.get("AA");
for (const key of additionalActions.getKeys()) {
const action = eventType[key];
if (!action) {
continue;
}
const actionDict = additionalActions.getRaw(key);
const parents = new RefSet();
const list = [];
_collectJS(actionDict, xref, list, parents);
if (list.length > 0) {
actions[action] = list;
const additionalActionsDicts = getInheritableProperty({
dict,
key: "AA",
stopWhenFound: false,
});
if (additionalActionsDicts) {
for (let i = additionalActionsDicts.length - 1; i >= 0; i--) {
const additionalActions = additionalActionsDicts[i];
for (const key of additionalActions.getKeys()) {
const action = eventType[key];
if (!action) {
continue;
}
const actionDict = additionalActions.getRaw(key);
const parents = new RefSet();
const list = [];
_collectJS(actionDict, xref, list, parents);
if (list.length > 0) {
actions[action] = list;
}
}
}
}
Expand Down
34 changes: 33 additions & 1 deletion src/scripting_api/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
* limitations under the License.
*/

const FieldType = {
none: 0,
number: 1,
percent: 2,
date: 3,
time: 4,
};

function createActionsMap(actions) {
const actionsMap = new Map();
if (actions) {
Expand All @@ -23,4 +31,28 @@ function createActionsMap(actions) {
return actionsMap;
}

export { createActionsMap };
function getFieldType(actions) {
let format = actions.get("Format");
if (!format) {
return FieldType.none;
}

format = format[0];

format = format.trim();
if (format.startsWith("AFNumber_")) {
return FieldType.number;
}
if (format.startsWith("AFPercent_")) {
return FieldType.percent;
}
if (format.startsWith("AFDate_")) {
return FieldType.date;
}
if (format.startsWith("AFTime__")) {
return FieldType.time;
}
return FieldType.none;
}

export { createActionsMap, FieldType, getFieldType };
15 changes: 13 additions & 2 deletions src/scripting_api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,27 @@ class EventDispatcher {
continue;
}

event.value = null;
const target = this._objects[targetId];
this.runActions(source, target, event, "Calculate");
if (!event.rc) {
continue;
}
if (event.value !== null) {
target.wrapped.value = event.value;
}

event.value = target.obj.value;
this.runActions(target, target, event, "Validate");
if (!event.rc) {
continue;
}

target.wrapped.value = event.value;
event.value = target.obj.value;
this.runActions(target, target, event, "Format");
target.wrapped.valueAsString = event.value;
if (event.value !== null) {
target.wrapped.valueAsString = event.value;
}
}
}
}
Expand Down
27 changes: 25 additions & 2 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* limitations under the License.
*/

import { createActionsMap, FieldType, getFieldType } from "./common.js";
import { Color } from "./color.js";
import { createActionsMap } from "./common.js";
import { PDFObject } from "./pdf_object.js";

class Field extends PDFObject {
Expand Down Expand Up @@ -82,8 +82,11 @@ class Field extends PDFObject {
this._textColor = data.textColor || ["G", 0];
this._value = data.value || "";
this._valueAsString = data.valueAsString;
this._kidsId = data.kidsId || null;
this._fieldType = getFieldType(this._actions);

this._globalEval = data.globalEval;
this._appObjects = data.appObjects;
}

get currentValueIndices() {
Expand Down Expand Up @@ -200,7 +203,23 @@ class Field extends PDFObject {
}

set value(value) {
this._value = value;
if (value === "") {
this._value = "";
} else if (typeof value === "string") {
switch (this._fieldType) {
case FieldType.number:
case FieldType.percent:
value = parseFloat(value);
if (!isNaN(value)) {
this._value = value;
}
break;
default:
this._value = value;
}
} else {
this._value = value;
}
if (this._isChoice) {
if (this.multipleSelection) {
const values = new Set(value);
Expand Down Expand Up @@ -332,6 +351,10 @@ class Field extends PDFObject {
}

getArray() {
if (this._kidsId) {
return this._kidsId.map(id => this._appObjects[id].wrapped);
}

if (this._children === null) {
this._children = this._document.obj._getChildren(this._fieldPath);
}
Expand Down
Loading

0 comments on commit 63f5f8e

Please sign in to comment.