Skip to content

Commit

Permalink
Implement support for Popup annotations
Browse files Browse the repository at this point in the history
We already had most code ready for supporting Popup annotations, but it
was only available for Text annotations. This patch implements the
separation between Text (or any other annotation type for that matter)
and Popup annotations. According to the specification they are two very
separate entities and should thus be implemented that way.

The main reason for this change is because Popup annotations are used
with many other annotation types too. In order to support those, we
first need to have proper support for Popup annotations. With this patch
in place, implementing other annotation types will be much easier.

A consequence of this patch is that the Text annotation code is greatly
simplified. Essentially, a Text annotation is only a container with an
image. The Popup annotation takes care of the rest.

Finally, this change allows for a small bit of refactoring. We no longer
need a separate wrapper as the Popup annotation has its own container,
which acts as the wrapper. This means we can also remove the strange
sanity check that belonged with it.
  • Loading branch information
timvandermeij committed Dec 22, 2015
1 parent 05b9d37 commit 991e501
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 151 deletions.
66 changes: 45 additions & 21 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var ColorSpace = coreColorSpace.ColorSpace;
var ObjectLoader = coreObj.ObjectLoader;
var OperatorList = coreEvaluator.OperatorList;

var DEFAULT_ICON_SIZE = 22; // px

/**
* @class
* @alias AnnotationFactory
Expand Down Expand Up @@ -95,6 +93,9 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ {
}
return new WidgetAnnotation(parameters);

case 'Popup':
return new PopupAnnotation(parameters);

default:
warn('Unimplemented annotation type "' + subtype + '", ' +
'falling back to base annotation');
Expand Down Expand Up @@ -639,29 +640,21 @@ var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() {
})();

var TextAnnotation = (function TextAnnotationClosure() {
function TextAnnotation(params) {
Annotation.call(this, params);
var DEFAULT_ICON_SIZE = 22; // px

var dict = params.dict;
var data = this.data;
function TextAnnotation(parameters) {
Annotation.call(this, parameters);

var content = dict.get('Contents');
var title = dict.get('T');
data.annotationType = AnnotationType.TEXT;
data.content = stringToPDFString(content || '');
data.title = stringToPDFString(title || '');
data.hasHtml = true;
this.data.annotationType = AnnotationType.TEXT;
this.data.hasHtml = true;

if (data.hasAppearance) {
data.name = 'NoIcon';
var dict = parameters.dict;
if (this.data.hasAppearance) {
this.data.name = 'NoIcon';
} else {
data.rect[1] = data.rect[3] - DEFAULT_ICON_SIZE;
data.rect[2] = data.rect[0] + DEFAULT_ICON_SIZE;
data.name = dict.has('Name') ? dict.get('Name').name : 'Note';
}

if (dict.has('C')) {
data.hasBgColor = true;
this.data.rect[1] = this.data.rect[3] - DEFAULT_ICON_SIZE;
this.data.rect[2] = this.data.rect[0] + DEFAULT_ICON_SIZE;
this.data.name = dict.has('Name') ? dict.get('Name').name : 'Note';
}
}

Expand Down Expand Up @@ -746,6 +739,37 @@ var LinkAnnotation = (function LinkAnnotationClosure() {
return LinkAnnotation;
})();

var PopupAnnotation = (function PopupAnnotationClosure() {
function PopupAnnotation(parameters) {
Annotation.call(this, parameters);

this.data.annotationType = AnnotationType.POPUP;

var dict = parameters.dict;
if (!dict.has('Parent')) {
warn('Popup annotations without a parent annotation are not supported.');
return;
}

var parentItem = dict.get('Parent');
if (!parentItem) {
warn('Popup annotation has an invalid parent annotation.');
return;
}
this.data.parentId = dict.map.Parent.num;
this.data.title = stringToPDFString(parentItem.get('T') || '');
this.data.contents = stringToPDFString(parentItem.get('Contents') || '');
this.setColor(parentItem.get('C'));
this.data.color = this.color;

this.data.hasHtml = (this.data.title && this.data.contents);
}

Util.inherit(PopupAnnotation, Annotation, {});

return PopupAnnotation;
})();

exports.Annotation = Annotation;
exports.AnnotationBorderStyle = AnnotationBorderStyle;
exports.AnnotationFactory = AnnotationFactory;
Expand Down

0 comments on commit 991e501

Please sign in to comment.