mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 09:38:06 +02:00
JS - Handle correctly hierarchy of fields (#13133)
* JS - Handle correctly hierarchy of fields - it aims to fix #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.
This commit is contained in:
parent
75a6b2fa13
commit
84d7cccb1d
13 changed files with 337 additions and 106 deletions
|
@ -64,10 +64,11 @@ class AnnotationFactory {
|
|||
* @param {Object} ref
|
||||
* @param {PDFManager} pdfManager
|
||||
* @param {Object} idFactory
|
||||
* @param {boolean} collectFields
|
||||
* @returns {Promise} A promise that is resolved with an {Annotation}
|
||||
* instance.
|
||||
*/
|
||||
static create(xref, ref, pdfManager, idFactory) {
|
||||
static create(xref, ref, pdfManager, idFactory, collectFields) {
|
||||
return pdfManager.ensureCatalog("acroForm").then(acroForm => {
|
||||
return pdfManager.ensure(this, "_create", [
|
||||
xref,
|
||||
|
@ -75,6 +76,7 @@ class AnnotationFactory {
|
|||
pdfManager,
|
||||
idFactory,
|
||||
acroForm,
|
||||
collectFields,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
@ -82,7 +84,7 @@ class AnnotationFactory {
|
|||
/**
|
||||
* @private
|
||||
*/
|
||||
static _create(xref, ref, pdfManager, idFactory, acroForm) {
|
||||
static _create(xref, ref, pdfManager, idFactory, acroForm, collectFields) {
|
||||
const dict = xref.fetchIfRef(ref);
|
||||
if (!isDict(dict)) {
|
||||
return undefined;
|
||||
|
@ -103,6 +105,7 @@ class AnnotationFactory {
|
|||
id,
|
||||
pdfManager,
|
||||
acroForm: acroForm instanceof Dict ? acroForm : Dict.empty,
|
||||
collectFields,
|
||||
};
|
||||
|
||||
switch (subtype) {
|
||||
|
@ -178,15 +181,17 @@ class AnnotationFactory {
|
|||
return new FileAttachmentAnnotation(parameters);
|
||||
|
||||
default:
|
||||
if (!subtype) {
|
||||
warn("Annotation is missing the required /Subtype.");
|
||||
} else {
|
||||
warn(
|
||||
'Unimplemented annotation type "' +
|
||||
subtype +
|
||||
'", ' +
|
||||
"falling back to base annotation."
|
||||
);
|
||||
if (!collectFields) {
|
||||
if (!subtype) {
|
||||
warn("Annotation is missing the required /Subtype.");
|
||||
} else {
|
||||
warn(
|
||||
'Unimplemented annotation type "' +
|
||||
subtype +
|
||||
'", ' +
|
||||
"falling back to base annotation."
|
||||
);
|
||||
}
|
||||
}
|
||||
return new Annotation(parameters);
|
||||
}
|
||||
|
@ -345,6 +350,31 @@ class Annotation {
|
|||
subtype: params.subtype,
|
||||
};
|
||||
|
||||
if (params.collectFields) {
|
||||
// Fields can act as container for other fields and have
|
||||
// some actions even if no Annotation inherit from them.
|
||||
// Those fields can be referenced by CO (calculation order).
|
||||
const kids = dict.get("Kids");
|
||||
if (Array.isArray(kids)) {
|
||||
const kidIds = [];
|
||||
for (const kid of kids) {
|
||||
if (isRef(kid)) {
|
||||
kidIds.push(kid.toString());
|
||||
}
|
||||
}
|
||||
if (kidIds.length !== 0) {
|
||||
this.data.kidIds = kidIds;
|
||||
}
|
||||
}
|
||||
|
||||
this.data.actions = collectActions(
|
||||
params.xref,
|
||||
dict,
|
||||
AnnotationActionEventType
|
||||
);
|
||||
this.data.fieldName = this._constructFieldName(dict);
|
||||
}
|
||||
|
||||
this._fallbackFontDict = null;
|
||||
}
|
||||
|
||||
|
@ -644,6 +674,15 @@ class Annotation {
|
|||
* @returns {Object | null}
|
||||
*/
|
||||
getFieldObject() {
|
||||
if (this.data.kidIds) {
|
||||
return {
|
||||
id: this.data.id,
|
||||
actions: this.data.actions,
|
||||
name: this.data.fieldName,
|
||||
type: "",
|
||||
kidIds: this.data.kidIds,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -670,6 +709,65 @@ class Annotation {
|
|||
stream.reset();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the (fully qualified) field name from the (partial) field
|
||||
* names of the field and its ancestors.
|
||||
*
|
||||
* @private
|
||||
* @memberof Annotation
|
||||
* @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(".");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -995,8 +1093,16 @@ class WidgetAnnotation extends Annotation {
|
|||
this.ref = params.ref;
|
||||
|
||||
data.annotationType = AnnotationType.WIDGET;
|
||||
data.fieldName = this._constructFieldName(dict);
|
||||
data.actions = collectActions(params.xref, dict, AnnotationActionEventType);
|
||||
if (data.fieldName === undefined) {
|
||||
data.fieldName = this._constructFieldName(dict);
|
||||
}
|
||||
if (data.actions === undefined) {
|
||||
data.actions = collectActions(
|
||||
params.xref,
|
||||
dict,
|
||||
AnnotationActionEventType
|
||||
);
|
||||
}
|
||||
|
||||
const fieldValue = getInheritableProperty({
|
||||
dict,
|
||||
|
@ -1059,65 +1165,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.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue