mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +02:00
Implement a single getInheritableProperty
utility function
This function combines the logic of two separate methods into one. The loop limit is also a good thing to have for the calls in `src/core/annotation.js`. Moreover, since this is important functionality, a set of unit tests and documentation is added.
This commit is contained in:
parent
4e5eb59a33
commit
f308d73d40
4 changed files with 189 additions and 57 deletions
|
@ -15,7 +15,8 @@
|
|||
|
||||
import {
|
||||
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
|
||||
AnnotationType, OPS, stringToBytes, stringToPDFString, Util, warn
|
||||
AnnotationType, getInheritableProperty, OPS, stringToBytes, stringToPDFString,
|
||||
Util, warn
|
||||
} from '../shared/util';
|
||||
import { Catalog, FileSpec, ObjectLoader } from './obj';
|
||||
import { Dict, isDict, isName, isRef, isStream } from './primitives';
|
||||
|
@ -60,7 +61,7 @@ class AnnotationFactory {
|
|||
return new TextAnnotation(parameters);
|
||||
|
||||
case 'Widget':
|
||||
let fieldType = Util.getInheritableProperty(dict, 'FT');
|
||||
let fieldType = getInheritableProperty({ dict, key: 'FT', });
|
||||
fieldType = isName(fieldType) ? fieldType.name : null;
|
||||
|
||||
switch (fieldType) {
|
||||
|
@ -580,15 +581,16 @@ class WidgetAnnotation extends Annotation {
|
|||
|
||||
data.annotationType = AnnotationType.WIDGET;
|
||||
data.fieldName = this._constructFieldName(dict);
|
||||
data.fieldValue = Util.getInheritableProperty(dict, 'V',
|
||||
/* getArray = */ true);
|
||||
data.fieldValue = getInheritableProperty({ dict, key: 'V',
|
||||
getArray: true, });
|
||||
data.alternativeText = stringToPDFString(dict.get('TU') || '');
|
||||
data.defaultAppearance = Util.getInheritableProperty(dict, 'DA') || '';
|
||||
let fieldType = Util.getInheritableProperty(dict, 'FT');
|
||||
data.defaultAppearance = getInheritableProperty({ dict, key: 'DA', }) || '';
|
||||
let fieldType = getInheritableProperty({ dict, key: 'FT', });
|
||||
data.fieldType = isName(fieldType) ? fieldType.name : null;
|
||||
this.fieldResources = Util.getInheritableProperty(dict, 'DR') || Dict.empty;
|
||||
this.fieldResources = getInheritableProperty({ dict, key: 'DR', }) ||
|
||||
Dict.empty;
|
||||
|
||||
data.fieldFlags = Util.getInheritableProperty(dict, 'Ff');
|
||||
data.fieldFlags = getInheritableProperty({ dict, key: 'Ff', });
|
||||
if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
|
||||
data.fieldFlags = 0;
|
||||
}
|
||||
|
@ -675,18 +677,20 @@ class TextWidgetAnnotation extends WidgetAnnotation {
|
|||
constructor(params) {
|
||||
super(params);
|
||||
|
||||
const dict = params.dict;
|
||||
|
||||
// The field value is always a string.
|
||||
this.data.fieldValue = stringToPDFString(this.data.fieldValue || '');
|
||||
|
||||
// Determine the alignment of text in the field.
|
||||
let alignment = Util.getInheritableProperty(params.dict, 'Q');
|
||||
let alignment = getInheritableProperty({ dict, key: 'Q', });
|
||||
if (!Number.isInteger(alignment) || alignment < 0 || alignment > 2) {
|
||||
alignment = null;
|
||||
}
|
||||
this.data.textAlignment = alignment;
|
||||
|
||||
// Determine the maximum length of text in the field.
|
||||
let maximumLength = Util.getInheritableProperty(params.dict, 'MaxLen');
|
||||
let maximumLength = getInheritableProperty({ dict, key: 'MaxLen', });
|
||||
if (!Number.isInteger(maximumLength) || maximumLength < 0) {
|
||||
maximumLength = null;
|
||||
}
|
||||
|
@ -814,7 +818,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
|
|||
// inherit the options from a parent annotation (issue 8094).
|
||||
this.data.options = [];
|
||||
|
||||
let options = Util.getInheritableProperty(params.dict, 'Opt');
|
||||
let options = getInheritableProperty({ dict: params.dict, key: 'Opt', });
|
||||
if (Array.isArray(options)) {
|
||||
let xref = params.xref;
|
||||
for (let i = 0, ii = options.length; i < ii; i++) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue