Skip to content

Commit

Permalink
Merge pull request mozilla#6273 from timvandermeij/annotation-factory
Browse files Browse the repository at this point in the history
Refactor annotation code to use a factory
  • Loading branch information
Snuffleupagus committed Jul 29, 2015
2 parents d08895d + 4f920ad commit e876dd8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 75 deletions.
123 changes: 51 additions & 72 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,54 @@
'use strict';

var DEFAULT_ICON_SIZE = 22; // px
var SUPPORTED_TYPES = ['Link', 'Text', 'Widget'];

/**
* @constructor
*/
function AnnotationFactory() {}
AnnotationFactory.prototype = {
/**
* @param {XRef} xref
* @param {Object} ref
* @returns {Annotation}
*/
create: function AnnotationFactory_create(xref, ref) {
var dict = xref.fetchIfRef(ref);
if (!isDict(dict)) {
return;
}

// Determine the annotation's subtype.
var subtype = dict.get('Subtype');
subtype = isName(subtype) ? subtype.name : '';

// Return the right annotation object based on the subtype and field type.
var parameters = {
dict: dict,
ref: ref
};

switch (subtype) {
case 'Link':
return new LinkAnnotation(parameters);

case 'Text':
return new TextAnnotation(parameters);

case 'Widget':
var fieldType = Util.getInheritableProperty(dict, 'FT');
if (isName(fieldType) && fieldType.name === 'Tx') {
return new TextWidgetAnnotation(parameters);
}
return new WidgetAnnotation(parameters);

default:
warn('Unimplemented annotation type "' + subtype + '", ' +
'falling back to base annotation');
return new Annotation(parameters);
}
}
};

var Annotation = (function AnnotationClosure() {
// 12.5.5: Algorithm: Appearance streams
Expand Down Expand Up @@ -193,13 +240,9 @@ var Annotation = (function AnnotationClosure() {

isInvisible: function Annotation_isInvisible() {
var data = this.data;
if (data && SUPPORTED_TYPES.indexOf(data.subtype) !== -1) {
return false;
} else {
return !!(data &&
data.annotationFlags && // Default: not invisible
data.annotationFlags & 0x1); // Invisible
}
return !!(data &&
data.annotationFlags && // Default: not invisible
data.annotationFlags & 0x1); // Invisible
},

isViewable: function Annotation_isViewable() {
Expand Down Expand Up @@ -275,70 +318,6 @@ var Annotation = (function AnnotationClosure() {
}
};

Annotation.getConstructor =
function Annotation_getConstructor(subtype, fieldType) {

if (!subtype) {
return;
}

// TODO(mack): Implement FreeText annotations
if (subtype === 'Link') {
return LinkAnnotation;
} else if (subtype === 'Text') {
return TextAnnotation;
} else if (subtype === 'Widget') {
if (!fieldType) {
return;
}

if (fieldType === 'Tx') {
return TextWidgetAnnotation;
} else {
return WidgetAnnotation;
}
} else {
return Annotation;
}
};

Annotation.fromRef = function Annotation_fromRef(xref, ref) {

var dict = xref.fetchIfRef(ref);
if (!isDict(dict)) {
return;
}

var subtype = dict.get('Subtype');
subtype = isName(subtype) ? subtype.name : '';
if (!subtype) {
return;
}

var fieldType = Util.getInheritableProperty(dict, 'FT');
fieldType = isName(fieldType) ? fieldType.name : '';

var Constructor = Annotation.getConstructor(subtype, fieldType);
if (!Constructor) {
return;
}

var params = {
dict: dict,
ref: ref,
};

var annotation = new Constructor(params);

if (annotation.isViewable() || annotation.isPrintable()) {
return annotation;
} else {
if (SUPPORTED_TYPES.indexOf(subtype) === -1) {
warn('unimplemented annotation type: ' + subtype);
}
}
};

Annotation.appendToOperatorList = function Annotation_appendToOperatorList(
annotations, opList, pdfManager, partialEvaluator, intent) {

Expand Down
9 changes: 6 additions & 3 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
isStream, NullStream, ObjectLoader, PartialEvaluator, Promise,
OperatorList, Annotation, error, assert, XRef, isArrayBuffer, Stream,
isString, isName, info, Linearization, MissingDataException, Lexer,
Catalog, stringToPDFString, stringToBytes, calculateMD5 */
Catalog, stringToPDFString, stringToBytes, calculateMD5,
AnnotationFactory */

'use strict';

Expand Down Expand Up @@ -264,10 +265,12 @@ var Page = (function PageClosure() {
get annotations() {
var annotations = [];
var annotationRefs = this.getInheritedPageProp('Annots') || [];
var annotationFactory = new AnnotationFactory();
for (var i = 0, n = annotationRefs.length; i < n; ++i) {
var annotationRef = annotationRefs[i];
var annotation = Annotation.fromRef(this.xref, annotationRef);
if (annotation) {
var annotation = annotationFactory.create(this.xref, annotationRef);
if (annotation &&
(annotation.isViewable() || annotation.isPrintable())) {
annotations.push(annotation);
}
}
Expand Down

0 comments on commit e876dd8

Please sign in to comment.