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:
commit
dcf5393270
@ -173,9 +173,9 @@ function getTransformMatrix(rect, bbox, matrix) {
|
|||||||
|
|
||||||
class Annotation {
|
class Annotation {
|
||||||
constructor(params) {
|
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.setModificationDate(dict.get('M'));
|
||||||
this.setFlags(dict.get('F'));
|
this.setFlags(dict.get('F'));
|
||||||
this.setRectangle(dict.getArray('Rect'));
|
this.setRectangle(dict.getArray('Rect'));
|
||||||
@ -188,7 +188,7 @@ class Annotation {
|
|||||||
annotationFlags: this.flags,
|
annotationFlags: this.flags,
|
||||||
borderStyle: this.borderStyle,
|
borderStyle: this.borderStyle,
|
||||||
color: this.color,
|
color: this.color,
|
||||||
creationDate: this.creationDate,
|
contents: this.contents,
|
||||||
hasAppearance: !!this.appearance,
|
hasAppearance: !!this.appearance,
|
||||||
id: params.id,
|
id: params.id,
|
||||||
modificationDate: this.modificationDate,
|
modificationDate: this.modificationDate,
|
||||||
@ -243,15 +243,16 @@ class Annotation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the creation date.
|
* Set the contents.
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
* @memberof Annotation
|
* @memberof Annotation
|
||||||
* @param {string} creationDate - PDF date string that indicates when the
|
* @param {string} contents - Text to display for the annotation or, if the
|
||||||
* annotation was originally created
|
* type of annotation does not display text, a
|
||||||
|
* description of the annotation's contents
|
||||||
*/
|
*/
|
||||||
setCreationDate(creationDate) {
|
setContents(contents) {
|
||||||
this.creationDate = isString(creationDate) ? creationDate : null;
|
this.contents = stringToPDFString(contents || '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -614,16 +615,30 @@ class AnnotationBorderStyle {
|
|||||||
class MarkupAnnotation extends Annotation {
|
class MarkupAnnotation extends Annotation {
|
||||||
constructor(parameters) {
|
constructor(parameters) {
|
||||||
super(parameters);
|
super(parameters);
|
||||||
const dict = parameters.dict;
|
|
||||||
|
|
||||||
|
const dict = parameters.dict;
|
||||||
if (!dict.has('C')) {
|
if (!dict.has('C')) {
|
||||||
// Fall back to the default background color.
|
// Fall back to the default background color.
|
||||||
this.data.color = null;
|
this.data.color = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.setCreationDate(dict.get('CreationDate'));
|
||||||
|
this.data.creationDate = this.creationDate;
|
||||||
|
|
||||||
this.data.hasPopup = dict.has('Popup');
|
this.data.hasPopup = dict.has('Popup');
|
||||||
this.data.title = stringToPDFString(dict.get('T') || '');
|
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.title = stringToPDFString(parentItem.get('T') || '');
|
||||||
this.data.contents = stringToPDFString(parentItem.get('Contents') || '');
|
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')) {
|
if (!parentItem.has('M')) {
|
||||||
this.data.modificationDate = null;
|
this.data.modificationDate = null;
|
||||||
} else {
|
} else {
|
||||||
@ -1165,4 +1173,5 @@ export {
|
|||||||
Annotation,
|
Annotation,
|
||||||
AnnotationBorderStyle,
|
AnnotationBorderStyle,
|
||||||
AnnotationFactory,
|
AnnotationFactory,
|
||||||
|
MarkupAnnotation,
|
||||||
};
|
};
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Annotation, AnnotationBorderStyle, AnnotationFactory
|
Annotation, AnnotationBorderStyle, AnnotationFactory, MarkupAnnotation
|
||||||
} from '../../src/core/annotation';
|
} from '../../src/core/annotation';
|
||||||
import {
|
import {
|
||||||
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
|
AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag,
|
||||||
@ -131,18 +131,18 @@ describe('annotation', function() {
|
|||||||
dict = ref = null;
|
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, });
|
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, });
|
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() {
|
it('should set and get a valid modification date', function() {
|
||||||
@ -152,7 +152,7 @@ describe('annotation', function() {
|
|||||||
expect(annotation.modificationDate).toEqual('D:20190422');
|
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, });
|
const annotation = new Annotation({ dict, ref, });
|
||||||
annotation.setModificationDate(undefined);
|
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() {
|
describe('LinkAnnotation', function() {
|
||||||
it('should correctly parse a URI action', function(done) {
|
it('should correctly parse a URI action', function(done) {
|
||||||
const actionDict = new Dict();
|
const actionDict = new Dict();
|
||||||
@ -1432,7 +1460,6 @@ describe('annotation', function() {
|
|||||||
const parentDict = new Dict();
|
const parentDict = new Dict();
|
||||||
parentDict.set('Type', Name.get('Annot'));
|
parentDict.set('Type', Name.get('Annot'));
|
||||||
parentDict.set('Subtype', Name.get('Text'));
|
parentDict.set('Subtype', Name.get('Text'));
|
||||||
parentDict.set('CreationDate', 'D:20190422');
|
|
||||||
parentDict.set('M', 'D:20190423');
|
parentDict.set('M', 'D:20190423');
|
||||||
parentDict.set('C', [0, 0, 1]);
|
parentDict.set('C', [0, 0, 1]);
|
||||||
|
|
||||||
@ -1449,7 +1476,6 @@ describe('annotation', function() {
|
|||||||
AnnotationFactory.create(xref, popupRef, pdfManagerMock,
|
AnnotationFactory.create(xref, popupRef, pdfManagerMock,
|
||||||
idFactoryMock).then(({ data, viewable, }) => {
|
idFactoryMock).then(({ data, viewable, }) => {
|
||||||
expect(data.annotationType).toEqual(AnnotationType.POPUP);
|
expect(data.annotationType).toEqual(AnnotationType.POPUP);
|
||||||
expect(data.creationDate).toEqual('D:20190422');
|
|
||||||
expect(data.modificationDate).toEqual('D:20190423');
|
expect(data.modificationDate).toEqual('D:20190423');
|
||||||
expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
|
expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
|
||||||
done();
|
done();
|
||||||
@ -1474,7 +1500,6 @@ describe('annotation', function() {
|
|||||||
AnnotationFactory.create(xref, popupRef, pdfManagerMock,
|
AnnotationFactory.create(xref, popupRef, pdfManagerMock,
|
||||||
idFactoryMock).then(({ data, viewable, }) => {
|
idFactoryMock).then(({ data, viewable, }) => {
|
||||||
expect(data.annotationType).toEqual(AnnotationType.POPUP);
|
expect(data.annotationType).toEqual(AnnotationType.POPUP);
|
||||||
expect(data.creationDate).toEqual(null);
|
|
||||||
expect(data.modificationDate).toEqual(null);
|
expect(data.modificationDate).toEqual(null);
|
||||||
expect(data.color).toEqual(null);
|
expect(data.color).toEqual(null);
|
||||||
done();
|
done();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user