Skip to content

Commit

Permalink
Implement support for Popup annotations
Browse files Browse the repository at this point in the history
Most code for Popup annotations is already present for Text annotations.
This patch extracts the popup creation logic from the Text annotation
code so it can be reused for Popup annotations.

Not only does this add support for Popup annotations, the Text
annotation code is also considerably easier. If a `Popup` entry is
available for a Text annotation, it will not be more than an image. The
popup will be handled by the Popup annotation. However, it is also
possible for Text annotations to not have a separate Popup annotation,
in which case the Text annotation handles the popup creation itself.
  • Loading branch information
timvandermeij committed Dec 24, 2015
1 parent 05b9d37 commit 7a681ed
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 161 deletions.
68 changes: 48 additions & 20 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 @@ -160,7 +161,7 @@ var Annotation = (function AnnotationClosure() {

// Expose public properties using a data object.
this.data = {};
this.data.id = params.ref.num;
this.data.id = params.ref.toString();
this.data.subtype = dict.get('Subtype').name;
this.data.annotationFlags = this.flags;
this.data.rect = this.rectangle;
Expand Down Expand Up @@ -639,29 +640,29 @@ 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';
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';
}

if (dict.has('C')) {
data.hasBgColor = true;
this.data.popup = !!dict.get('Popup');
if (!this.data.popup) {
// There is no associated Popup annotation, so the Text annotation
// must create its own popup.
this.data.title = stringToPDFString(dict.get('T') || '');
this.data.contents = stringToPDFString(dict.get('Contents') || '');
}
}

Expand Down Expand Up @@ -746,6 +747,33 @@ 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;
var parentItem = dict.get('Parent');
if (!parentItem) {
warn('Popup annotation has a missing or invalid parent annotation.');
return;
}

this.data.parentId = dict.getRaw('Parent').toString();
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 7a681ed

Please sign in to comment.