Browse Source

Improve the structure for widget annotations

Currently, we only support text widget annotations (field type 'Tx')
partially. However, the current code does not make this entirely clear
and does not provide a warning when an unsupported field type is
encountered, making it harder to determine why rendering fails.

Moreover, in the display layer we make no distinction between the
various types of widget annotations, causing the code for text widget
annotations to also be executed for other types of widget annotations in
a fallback situation.

This patch improves the structure of the widget annotation code. In the
core layer, we use the same structure we use for non-widget annotations
in the factory and provide a clear warning when an unsupported type is
encountered. In the display layer, we do the same and split the
`WidgetAnnotationElement` class into two classes, namely
`TextWidgetAnnotationElement` for text widget annotations and
`WidgetAnnotationElement` for other unsupported annotations as a
fallback. From this it clear that we only support text widget
annotations and nothing else.
Tim van der Meij 9 years ago
parent
commit
576f742047
  1. 12
      src/core/annotation.js
  2. 43
      src/display/annotation_layer.js

12
src/core/annotation.js

@ -98,9 +98,14 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ { @@ -98,9 +98,14 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ {
case 'Widget':
var fieldType = Util.getInheritableProperty(dict, 'FT');
if (isName(fieldType, 'Tx')) {
fieldType = isName(fieldType) ? fieldType.name : null;
switch (fieldType) {
case 'Tx':
return new TextWidgetAnnotation(parameters);
}
warn('Unimplemented widget field type "' + fieldType + '", ' +
'falling back to base field type.');
return new WidgetAnnotation(parameters);
case 'Popup':
@ -615,13 +620,12 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() { @@ -615,13 +620,12 @@ var WidgetAnnotation = (function WidgetAnnotationClosure() {
data.alternativeText = stringToPDFString(dict.get('TU') || '');
data.defaultAppearance = Util.getInheritableProperty(dict, 'DA') || '';
var fieldType = Util.getInheritableProperty(dict, 'FT');
data.fieldType = isName(fieldType) ? fieldType.name : '';
data.fieldType = isName(fieldType) ? fieldType.name : null;
data.fieldFlags = Util.getInheritableProperty(dict, 'Ff') || 0;
this.fieldResources = Util.getInheritableProperty(dict, 'DR') || Dict.empty;
// Hide unsupported Widget signatures.
// Hide signatures because we cannot validate them.
if (data.fieldType === 'Sig') {
warn('unimplemented annotation type: Widget signature');
this.setFlags(AnnotationFlag.HIDDEN);
}

43
src/display/annotation_layer.js

@ -69,6 +69,12 @@ AnnotationElementFactory.prototype = @@ -69,6 +69,12 @@ AnnotationElementFactory.prototype =
return new TextAnnotationElement(parameters);
case AnnotationType.WIDGET:
var fieldType = parameters.data.fieldType;
switch (fieldType) {
case 'Tx':
return new TextWidgetAnnotationElement(parameters);
}
return new WidgetAnnotationElement(parameters);
case AnnotationType.POPUP:
@ -392,9 +398,7 @@ var TextAnnotationElement = (function TextAnnotationElementClosure() { @@ -392,9 +398,7 @@ var TextAnnotationElement = (function TextAnnotationElementClosure() {
*/
var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() {
function WidgetAnnotationElement(parameters) {
var isRenderable = !parameters.data.hasAppearance &&
!!parameters.data.fieldValue;
AnnotationElement.call(this, parameters, isRenderable);
AnnotationElement.call(this, parameters, true);
}
Util.inherit(WidgetAnnotationElement, AnnotationElement, {
@ -406,6 +410,33 @@ var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() { @@ -406,6 +410,33 @@ var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() {
* @returns {HTMLSectionElement}
*/
render: function WidgetAnnotationElement_render() {
// Show only the container for unsupported field types.
return this.container;
}
});
return WidgetAnnotationElement;
})();
/**
* @class
* @alias TextWidgetAnnotationElement
*/
var TextWidgetAnnotationElement = (
function TextWidgetAnnotationElementClosure() {
function TextWidgetAnnotationElement(parameters) {
WidgetAnnotationElement.call(this, parameters);
}
Util.inherit(TextWidgetAnnotationElement, WidgetAnnotationElement, {
/**
* Render the text widget annotation's HTML element in the empty container.
*
* @public
* @memberof TextWidgetAnnotationElement
* @returns {HTMLSectionElement}
*/
render: function TextWidgetAnnotationElement_render() {
var content = document.createElement('div');
content.textContent = this.data.fieldValue;
var textAlignment = this.data.textAlignment;
@ -427,10 +458,10 @@ var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() { @@ -427,10 +458,10 @@ var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() {
* @private
* @param {HTMLDivElement} element
* @param {Object} font
* @memberof WidgetAnnotationElement
* @memberof TextWidgetAnnotationElement
*/
_setTextStyle:
function WidgetAnnotationElement_setTextStyle(element, font) {
function TextWidgetAnnotationElement_setTextStyle(element, font) {
// TODO: This duplicates some of the logic in CanvasGraphics.setFont().
var style = element.style;
style.fontSize = this.data.fontSize + 'px';
@ -452,7 +483,7 @@ var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() { @@ -452,7 +483,7 @@ var WidgetAnnotationElement = (function WidgetAnnotationElementClosure() {
}
});
return WidgetAnnotationElement;
return TextWidgetAnnotationElement;
})();
/**

Loading…
Cancel
Save