Skip to content

Commit

Permalink
Widget annotations: simplify field flag handling
Browse files Browse the repository at this point in the history
Directly use the hexadecimal representation, just like the
`AnnotationFlags`, to avoid calculations and to improve readability.
This allows us to simplify the unit tests for text widget annotations as
well.
  • Loading branch information
timvandermeij committed Sep 21, 2016
1 parent 6100ab4 commit 375229d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 47 deletions.
7 changes: 3 additions & 4 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,13 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() {
*
* @public
* @memberof WidgetAnnotation
* @param {number} flag - Bit position, numbered from one instead of
* zero, to check
* @param {number} flag - Hexadecimal representation for an annotation
* field characteristic
* @return {boolean}
* @see {@link shared/util.js}
*/
hasFieldFlag: function WidgetAnnotation_hasFieldFlag(flag) {
var mask = 1 << (flag - 1);
return !!(this.data.fieldFlags & mask);
return !!(this.data.fieldFlags & flag);
},
});

Expand Down
38 changes: 19 additions & 19 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,25 @@ var AnnotationFlag = {
};

var AnnotationFieldFlag = {
READONLY: 1,
REQUIRED: 2,
NOEXPORT: 3,
MULTILINE: 13,
PASSWORD: 14,
NOTOGGLETOOFF: 15,
RADIO: 16,
PUSHBUTTON: 17,
COMBO: 18,
EDIT: 19,
SORT: 20,
FILESELECT: 21,
MULTISELECT: 22,
DONOTSPELLCHECK: 23,
DONOTSCROLL: 24,
COMB: 25,
RICHTEXT: 26,
RADIOSINUNISON: 26,
COMMITONSELCHANGE: 27,
READONLY: 0x0000001,
REQUIRED: 0x0000002,
NOEXPORT: 0x0000004,
MULTILINE: 0x0001000,
PASSWORD: 0x0002000,
NOTOGGLETOOFF: 0x0004000,
RADIO: 0x0008000,
PUSHBUTTON: 0x0010000,
COMBO: 0x0020000,
EDIT: 0x0040000,
SORT: 0x0080000,
FILESELECT: 0x0100000,
MULTISELECT: 0x0200000,
DONOTSPELLCHECK: 0x0400000,
DONOTSCROLL: 0x0800000,
COMB: 0x1000000,
RICHTEXT: 0x2000000,
RADIOSINUNISON: 0x2000000,
COMMITONSELCHANGE: 0x4000000,
};

var AnnotationBorderStyleType = {
Expand Down
35 changes: 11 additions & 24 deletions test/unit/annotation_layer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,10 @@ describe('Annotation layer', function() {

it('should set valid text alignment, maximum length and flags',
function() {
var flags = 0;
flags |= 1 << (AnnotationFieldFlag.READONLY - 1);
flags |= 1 << (AnnotationFieldFlag.MULTILINE - 1);

textWidgetDict.set('Q', 1);
textWidgetDict.set('MaxLen', 20);
textWidgetDict.set('Ff', flags);
textWidgetDict.set('Ff', AnnotationFieldFlag.READONLY +
AnnotationFieldFlag.MULTILINE);

var textWidgetRef = new Ref(84, 0);
var xref = new XRefMock([
Expand All @@ -526,10 +523,7 @@ describe('Annotation layer', function() {
});

it('should reject comb fields without a maximum length', function() {
var flags = 0;
flags |= 1 << (AnnotationFieldFlag.COMB - 1);

textWidgetDict.set('Ff', flags);
textWidgetDict.set('Ff', AnnotationFieldFlag.COMB);

var textWidgetRef = new Ref(46, 0);
var xref = new XRefMock([
Expand All @@ -541,11 +535,8 @@ describe('Annotation layer', function() {
});

it('should accept comb fields with a maximum length', function() {
var flags = 0;
flags |= 1 << (AnnotationFieldFlag.COMB - 1);

textWidgetDict.set('MaxLen', 20);
textWidgetDict.set('Ff', flags);
textWidgetDict.set('Ff', AnnotationFieldFlag.COMB);

var textWidgetRef = new Ref(46, 0);
var xref = new XRefMock([
Expand All @@ -558,20 +549,16 @@ describe('Annotation layer', function() {

it('should only accept comb fields when the flags are valid', function() {
var invalidFieldFlags = [
AnnotationFieldFlag.MULTILINE,
AnnotationFieldFlag.PASSWORD,
AnnotationFieldFlag.MULTILINE, AnnotationFieldFlag.PASSWORD,
AnnotationFieldFlag.FILESELECT
];

// The field may not use combs until all invalid flags are unset.
for (var i = 0, ii = invalidFieldFlags.length; i <= ii; i++) {
var flags = 0;
flags |= 1 << (AnnotationFieldFlag.COMB - 1);

for (var j = 0, jj = invalidFieldFlags.length; j < jj; j++) {
flags |= 1 << (invalidFieldFlags[j] - 1);
}
// Start with all invalid flags set and remove them one by one.
// The field may only use combs when all invalid flags are unset.
var flags = AnnotationFieldFlag.COMB + AnnotationFieldFlag.MULTILINE +
AnnotationFieldFlag.PASSWORD + AnnotationFieldFlag.FILESELECT;

for (var i = 0, ii = invalidFieldFlags.length; i <= ii; i++) {
textWidgetDict.set('MaxLen', 20);
textWidgetDict.set('Ff', flags);

Expand All @@ -588,7 +575,7 @@ describe('Annotation layer', function() {

// Remove the last invalid flag for the next iteration.
if (!valid) {
invalidFieldFlags.splice(-1, 1);
flags -= invalidFieldFlags.splice(-1, 1);
}
}
});
Expand Down

0 comments on commit 375229d

Please sign in to comment.