Merge pull request #10833 from timvandermeij/annotation-fixes

[api-minor] Implement contents and creation date for the correct annotation types
This commit is contained in:
Tim van der Meij 2019-05-25 16:12:21 +02:00 committed by GitHub
commit dcf5393270
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 28 deletions

View File

@ -173,9 +173,9 @@ function getTransformMatrix(rect, bbox, matrix) {
class Annotation {
constructor(params) {
let dict = params.dict;
const dict = params.dict;
this.setCreationDate(dict.get('CreationDate'));
this.setContents(dict.get('Contents'));
this.setModificationDate(dict.get('M'));
this.setFlags(dict.get('F'));
this.setRectangle(dict.getArray('Rect'));
@ -188,7 +188,7 @@ class Annotation {
annotationFlags: this.flags,
borderStyle: this.borderStyle,
color: this.color,
creationDate: this.creationDate,
contents: this.contents,
hasAppearance: !!this.appearance,
id: params.id,
modificationDate: this.modificationDate,
@ -243,15 +243,16 @@ class Annotation {
}
/**
* Set the creation date.
* Set the contents.
*
* @public
* @memberof Annotation
* @param {string} creationDate - PDF date string that indicates when the
* annotation was originally created
* @param {string} contents - Text to display for the annotation or, if the
* type of annotation does not display text, a
* description of the annotation's contents
*/
setCreationDate(creationDate) {
this.creationDate = isString(creationDate) ? creationDate : null;
setContents(contents) {
this.contents = stringToPDFString(contents || '');
}
/**
@ -614,16 +615,30 @@ class AnnotationBorderStyle {
class MarkupAnnotation extends Annotation {
constructor(parameters) {
super(parameters);
const dict = parameters.dict;
const dict = parameters.dict;
if (!dict.has('C')) {
// Fall back to the default background color.
this.data.color = null;
}
this.setCreationDate(dict.get('CreationDate'));
this.data.creationDate = this.creationDate;
this.data.hasPopup = dict.has('Popup');
this.data.title = stringToPDFString(dict.get('T') || '');
this.data.contents = stringToPDFString(dict.get('Contents') || '');
}
/**
* Set the creation date.
*
* @public
* @memberof MarkupAnnotation
* @param {string} creationDate - PDF date string that indicates when the
* annotation was originally created
*/
setCreationDate(creationDate) {
this.creationDate = isString(creationDate) ? creationDate : null;
}
}
@ -975,13 +990,6 @@ class PopupAnnotation extends Annotation {
this.data.title = stringToPDFString(parentItem.get('T') || '');
this.data.contents = stringToPDFString(parentItem.get('Contents') || '');
if (!parentItem.has('CreationDate')) {
this.data.creationDate = null;
} else {
this.setCreationDate(parentItem.get('CreationDate'));
this.data.creationDate = this.creationDate;
}
if (!parentItem.has('M')) {
this.data.modificationDate = null;
} else {
@ -1165,4 +1173,5 @@ export {
Annotation,
AnnotationBorderStyle,
AnnotationFactory,
MarkupAnnotation,
};

View File

@ -14,7 +14,7 @@
*/
import {
Annotation, AnnotationBorderStyle, AnnotationFactory
Annotation, AnnotationBorderStyle, AnnotationFactory, MarkupAnnotation
} from '../../src/core/annotation';
import {
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
@ -131,18 +131,18 @@ describe('annotation', function() {
dict = ref = null;
});
it('should set and get a valid creation date', function() {
it('should set and get valid contents', function() {
const annotation = new Annotation({ dict, ref, });
annotation.setCreationDate('D:20190422');
annotation.setContents('Foo bar baz');
expect(annotation.creationDate).toEqual('D:20190422');
expect(annotation.contents).toEqual('Foo bar baz');
});
it('should set and get an invalid creation date', function() {
it('should not set and get invalid contents', function() {
const annotation = new Annotation({ dict, ref, });
annotation.setCreationDate(undefined);
annotation.setContents(undefined);
expect(annotation.creationDate).toEqual(null);
expect(annotation.contents).toEqual('');
});
it('should set and get a valid modification date', function() {
@ -152,7 +152,7 @@ describe('annotation', function() {
expect(annotation.modificationDate).toEqual('D:20190422');
});
it('should set and get an invalid modification date', function() {
it('should not set and get an invalid modification date', function() {
const annotation = new Annotation({ dict, ref, });
annotation.setModificationDate(undefined);
@ -317,6 +317,34 @@ describe('annotation', function() {
});
});
describe('MarkupAnnotation', function() {
let dict, ref;
beforeAll(function(done) {
dict = new Dict();
ref = new Ref(1, 0);
done();
});
afterAll(function() {
dict = ref = null;
});
it('should set and get a valid creation date', function() {
const markupAnnotation = new MarkupAnnotation({ dict, ref, });
markupAnnotation.setCreationDate('D:20190422');
expect(markupAnnotation.creationDate).toEqual('D:20190422');
});
it('should not set and get an invalid creation date', function() {
const markupAnnotation = new MarkupAnnotation({ dict, ref, });
markupAnnotation.setCreationDate(undefined);
expect(markupAnnotation.creationDate).toEqual(null);
});
});
describe('LinkAnnotation', function() {
it('should correctly parse a URI action', function(done) {
const actionDict = new Dict();
@ -1432,7 +1460,6 @@ describe('annotation', function() {
const parentDict = new Dict();
parentDict.set('Type', Name.get('Annot'));
parentDict.set('Subtype', Name.get('Text'));
parentDict.set('CreationDate', 'D:20190422');
parentDict.set('M', 'D:20190423');
parentDict.set('C', [0, 0, 1]);
@ -1449,7 +1476,6 @@ describe('annotation', function() {
AnnotationFactory.create(xref, popupRef, pdfManagerMock,
idFactoryMock).then(({ data, viewable, }) => {
expect(data.annotationType).toEqual(AnnotationType.POPUP);
expect(data.creationDate).toEqual('D:20190422');
expect(data.modificationDate).toEqual('D:20190423');
expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
done();
@ -1474,7 +1500,6 @@ describe('annotation', function() {
AnnotationFactory.create(xref, popupRef, pdfManagerMock,
idFactoryMock).then(({ data, viewable, }) => {
expect(data.annotationType).toEqual(AnnotationType.POPUP);
expect(data.creationDate).toEqual(null);
expect(data.modificationDate).toEqual(null);
expect(data.color).toEqual(null);
done();