2013-03-21 17:04:44 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-06-18 07:43:33 +09:00
|
|
|
/* globals PDFJS, Util, isDict, isName, stringToPDFString, warn, Dict, Stream,
|
2015-07-06 18:58:26 +09:00
|
|
|
stringToBytes, Promise, isArray, ObjectLoader, OperatorList,
|
2014-12-26 04:24:28 +09:00
|
|
|
isValidUrl, OPS, createPromiseCapability, AnnotationType,
|
2015-07-15 23:59:25 +09:00
|
|
|
stringToUTF8String, AnnotationBorderStyleType, ColorSpace */
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-04-19 04:11:56 +09:00
|
|
|
var DEFAULT_ICON_SIZE = 22; // px
|
2014-03-07 23:48:42 +09:00
|
|
|
var SUPPORTED_TYPES = ['Link', 'Text', 'Widget'];
|
|
|
|
|
2013-03-21 17:04:44 +09:00
|
|
|
var Annotation = (function AnnotationClosure() {
|
|
|
|
// 12.5.5: Algorithm: Appearance streams
|
|
|
|
function getTransformMatrix(rect, bbox, matrix) {
|
|
|
|
var bounds = Util.getAxialAlignedBoundingBox(bbox, matrix);
|
|
|
|
var minX = bounds[0];
|
|
|
|
var minY = bounds[1];
|
|
|
|
var maxX = bounds[2];
|
|
|
|
var maxY = bounds[3];
|
|
|
|
|
|
|
|
if (minX === maxX || minY === maxY) {
|
|
|
|
// From real-life file, bbox was [0, 0, 0, 0]. In this case,
|
|
|
|
// just apply the transform for rect
|
|
|
|
return [1, 0, 0, 1, rect[0], rect[1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
var xRatio = (rect[2] - rect[0]) / (maxX - minX);
|
|
|
|
var yRatio = (rect[3] - rect[1]) / (maxY - minY);
|
|
|
|
return [
|
|
|
|
xRatio,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
yRatio,
|
|
|
|
rect[0] - minX * xRatio,
|
|
|
|
rect[1] - minY * yRatio
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultAppearance(dict) {
|
|
|
|
var appearanceState = dict.get('AP');
|
|
|
|
if (!isDict(appearanceState)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var appearance;
|
|
|
|
var appearances = appearanceState.get('N');
|
|
|
|
if (isDict(appearances)) {
|
|
|
|
var as = dict.get('AS');
|
|
|
|
if (as && appearances.has(as.name)) {
|
|
|
|
appearance = appearances.get(as.name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
appearance = appearances;
|
|
|
|
}
|
|
|
|
return appearance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Annotation(params) {
|
|
|
|
var dict = params.dict;
|
|
|
|
var data = this.data = {};
|
|
|
|
|
|
|
|
data.subtype = dict.get('Subtype').name;
|
|
|
|
data.annotationFlags = dict.get('F');
|
|
|
|
|
2015-07-21 05:01:47 +09:00
|
|
|
this.setRectangle(dict.get('Rect'));
|
|
|
|
data.rect = this.rectangle;
|
|
|
|
|
2015-07-15 23:59:25 +09:00
|
|
|
this.setColor(dict.get('C'));
|
|
|
|
data.color = this.color;
|
2013-03-26 18:41:56 +09:00
|
|
|
|
2014-12-26 04:44:16 +09:00
|
|
|
this.borderStyle = data.borderStyle = new AnnotationBorderStyle();
|
|
|
|
this.setBorderStyle(dict);
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
this.appearance = getDefaultAppearance(dict);
|
2013-08-23 04:29:06 +09:00
|
|
|
data.hasAppearance = !!this.appearance;
|
2014-04-01 20:45:00 +09:00
|
|
|
data.id = params.ref.num;
|
2013-03-21 17:04:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
Annotation.prototype = {
|
2015-07-21 05:01:47 +09:00
|
|
|
/**
|
|
|
|
* Set the rectangle.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof Annotation
|
|
|
|
* @param {Array} rectangle - The rectangle array with exactly four entries
|
|
|
|
*/
|
|
|
|
setRectangle: function Annotation_setRectangle(rectangle) {
|
|
|
|
if (isArray(rectangle) && rectangle.length === 4) {
|
|
|
|
this.rectangle = Util.normalizeRect(rectangle);
|
|
|
|
} else {
|
|
|
|
this.rectangle = [0, 0, 0, 0];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-15 23:59:25 +09:00
|
|
|
/**
|
|
|
|
* Set the color and take care of color space conversion.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof Annotation
|
|
|
|
* @param {Array} color - The color array containing either 0
|
|
|
|
* (transparent), 1 (grayscale), 3 (RGB) or
|
|
|
|
* 4 (CMYK) elements
|
|
|
|
*/
|
|
|
|
setColor: function Annotation_setColor(color) {
|
|
|
|
var rgbColor = new Uint8Array(3); // Black in RGB color space (default)
|
|
|
|
if (!isArray(color)) {
|
|
|
|
this.color = rgbColor;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (color.length) {
|
|
|
|
case 0: // Transparent, which we indicate with a null value
|
|
|
|
this.color = null;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // Convert grayscale to RGB
|
|
|
|
ColorSpace.singletons.gray.getRgbItem(color, 0, rgbColor, 0);
|
|
|
|
this.color = rgbColor;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: // Convert RGB percentages to RGB
|
|
|
|
ColorSpace.singletons.rgb.getRgbItem(color, 0, rgbColor, 0);
|
|
|
|
this.color = rgbColor;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: // Convert CMYK to RGB
|
|
|
|
ColorSpace.singletons.cmyk.getRgbItem(color, 0, rgbColor, 0);
|
|
|
|
this.color = rgbColor;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this.color = rgbColor;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-26 04:24:28 +09:00
|
|
|
/**
|
|
|
|
* Set the border style (as AnnotationBorderStyle object).
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof Annotation
|
|
|
|
* @param {Dict} borderStyle - The border style dictionary
|
|
|
|
*/
|
|
|
|
setBorderStyle: function Annotation_setBorderStyle(borderStyle) {
|
|
|
|
if (!isDict(borderStyle)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (borderStyle.has('BS')) {
|
|
|
|
var dict = borderStyle.get('BS');
|
|
|
|
var dictType;
|
|
|
|
|
|
|
|
if (!dict.has('Type') || (isName(dictType = dict.get('Type')) &&
|
|
|
|
dictType.name === 'Border')) {
|
|
|
|
this.borderStyle.setWidth(dict.get('W'));
|
|
|
|
this.borderStyle.setStyle(dict.get('S'));
|
|
|
|
this.borderStyle.setDashArray(dict.get('D'));
|
|
|
|
}
|
|
|
|
} else if (borderStyle.has('Border')) {
|
|
|
|
var array = borderStyle.get('Border');
|
|
|
|
if (isArray(array) && array.length >= 3) {
|
|
|
|
this.borderStyle.setHorizontalCornerRadius(array[0]);
|
|
|
|
this.borderStyle.setVerticalCornerRadius(array[1]);
|
|
|
|
this.borderStyle.setWidth(array[2]);
|
|
|
|
|
|
|
|
if (array.length === 4) { // Dash array available
|
|
|
|
this.borderStyle.setDashArray(array[3]);
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 21:48:59 +09:00
|
|
|
} else {
|
|
|
|
// There are no border entries in the dictionary. According to the
|
|
|
|
// specification, we should draw a solid border of width 1 in that
|
|
|
|
// case, but Adobe Reader did not implement that part of the
|
|
|
|
// specification and instead draws no border at all, so we do the same.
|
|
|
|
// See also https://github.com/mozilla/pdf.js/issues/6179.
|
|
|
|
this.borderStyle.setWidth(0);
|
2014-12-26 04:24:28 +09:00
|
|
|
}
|
|
|
|
},
|
2013-03-21 17:04:44 +09:00
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-03-21 17:04:44 +09:00
|
|
|
isViewable: function Annotation_isViewable() {
|
|
|
|
var data = this.data;
|
2014-03-07 23:48:42 +09:00
|
|
|
return !!(!this.isInvisible() &&
|
|
|
|
data &&
|
|
|
|
(!data.annotationFlags ||
|
|
|
|
!(data.annotationFlags & 0x22)) && // Hidden or NoView
|
2014-06-11 11:19:17 +09:00
|
|
|
data.rect); // rectangle is necessary
|
2014-03-07 23:48:42 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
isPrintable: function Annotation_isPrintable() {
|
|
|
|
var data = this.data;
|
|
|
|
return !!(!this.isInvisible() &&
|
|
|
|
data &&
|
|
|
|
data.annotationFlags && // Default: not printable
|
|
|
|
data.annotationFlags & 0x4 && // Print
|
2014-06-11 11:19:17 +09:00
|
|
|
!(data.annotationFlags & 0x2) && // Hidden
|
|
|
|
data.rect); // rectangle is necessary
|
2013-03-21 17:04:44 +09:00
|
|
|
},
|
|
|
|
|
2014-05-02 07:52:03 +09:00
|
|
|
loadResources: function Annotation_loadResources(keys) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
this.appearance.dict.getAsync('Resources').then(function (resources) {
|
|
|
|
if (!resources) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var objectLoader = new ObjectLoader(resources.map,
|
|
|
|
keys,
|
|
|
|
resources.xref);
|
|
|
|
objectLoader.load().then(function() {
|
|
|
|
resolve(resources);
|
|
|
|
}, reject);
|
|
|
|
}, reject);
|
2013-06-05 09:57:52 +09:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
getOperatorList: function Annotation_getOperatorList(evaluator) {
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
if (!this.appearance) {
|
2014-05-10 10:21:15 +09:00
|
|
|
return Promise.resolve(new OperatorList());
|
2013-03-21 17:04:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var data = this.data;
|
|
|
|
|
|
|
|
var appearanceDict = this.appearance.dict;
|
2013-06-05 09:57:52 +09:00
|
|
|
var resourcesPromise = this.loadResources([
|
|
|
|
'ExtGState',
|
|
|
|
'ColorSpace',
|
|
|
|
'Pattern',
|
|
|
|
'Shading',
|
|
|
|
'XObject',
|
|
|
|
'Font'
|
|
|
|
// ProcSet
|
|
|
|
// Properties
|
|
|
|
]);
|
2013-03-21 17:04:44 +09:00
|
|
|
var bbox = appearanceDict.get('BBox') || [0, 0, 1, 1];
|
|
|
|
var matrix = appearanceDict.get('Matrix') || [1, 0, 0, 1, 0 ,0];
|
|
|
|
var transform = getTransformMatrix(data.rect, bbox, matrix);
|
2014-05-10 10:21:15 +09:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return resourcesPromise.then(function(resources) {
|
|
|
|
var opList = new OperatorList();
|
|
|
|
opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
|
|
|
|
return evaluator.getOperatorList(self.appearance, resources, opList).
|
|
|
|
then(function () {
|
|
|
|
opList.addOp(OPS.endAnnotation, []);
|
|
|
|
self.appearance.reset();
|
|
|
|
return opList;
|
|
|
|
});
|
|
|
|
});
|
2013-03-21 17:04:44 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-26 07:32:47 +09:00
|
|
|
if (fieldType === 'Tx') {
|
|
|
|
return TextWidgetAnnotation;
|
|
|
|
} else {
|
|
|
|
return WidgetAnnotation;
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
} 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);
|
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
if (annotation.isViewable() || annotation.isPrintable()) {
|
2013-03-21 17:04:44 +09:00
|
|
|
return annotation;
|
|
|
|
} else {
|
2014-06-11 11:19:17 +09:00
|
|
|
if (SUPPORTED_TYPES.indexOf(subtype) === -1) {
|
|
|
|
warn('unimplemented annotation type: ' + subtype);
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-29 07:12:35 +09:00
|
|
|
Annotation.appendToOperatorList = function Annotation_appendToOperatorList(
|
2014-03-07 23:48:42 +09:00
|
|
|
annotations, opList, pdfManager, partialEvaluator, intent) {
|
2013-05-29 07:12:35 +09:00
|
|
|
|
|
|
|
function reject(e) {
|
2014-05-02 07:52:03 +09:00
|
|
|
annotationsReadyCapability.reject(e);
|
2013-05-29 07:12:35 +09:00
|
|
|
}
|
|
|
|
|
2014-05-02 07:52:03 +09:00
|
|
|
var annotationsReadyCapability = createPromiseCapability();
|
2013-05-29 07:12:35 +09:00
|
|
|
|
2013-06-05 09:57:52 +09:00
|
|
|
var annotationPromises = [];
|
2013-05-29 07:12:35 +09:00
|
|
|
for (var i = 0, n = annotations.length; i < n; ++i) {
|
2014-03-07 23:48:42 +09:00
|
|
|
if (intent === 'display' && annotations[i].isViewable() ||
|
|
|
|
intent === 'print' && annotations[i].isPrintable()) {
|
|
|
|
annotationPromises.push(
|
|
|
|
annotations[i].getOperatorList(partialEvaluator));
|
|
|
|
}
|
2013-05-29 07:12:35 +09:00
|
|
|
}
|
2013-06-05 09:57:52 +09:00
|
|
|
Promise.all(annotationPromises).then(function(datas) {
|
2013-11-14 04:43:38 +09:00
|
|
|
opList.addOp(OPS.beginAnnotations, []);
|
2013-06-05 09:57:52 +09:00
|
|
|
for (var i = 0, n = datas.length; i < n; ++i) {
|
2013-08-01 03:17:36 +09:00
|
|
|
var annotOpList = datas[i];
|
|
|
|
opList.addOpList(annotOpList);
|
2013-06-05 09:57:52 +09:00
|
|
|
}
|
2013-11-14 04:43:38 +09:00
|
|
|
opList.addOp(OPS.endAnnotations, []);
|
2014-05-02 07:52:03 +09:00
|
|
|
annotationsReadyCapability.resolve();
|
2013-05-29 07:12:35 +09:00
|
|
|
}, reject);
|
|
|
|
|
2014-05-02 07:52:03 +09:00
|
|
|
return annotationsReadyCapability.promise;
|
2013-05-29 07:12:35 +09:00
|
|
|
};
|
|
|
|
|
2013-03-21 17:04:44 +09:00
|
|
|
return Annotation;
|
|
|
|
})();
|
|
|
|
|
2014-12-26 04:11:23 +09:00
|
|
|
/**
|
|
|
|
* Contains all data regarding an annotation's border style.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
var AnnotationBorderStyle = (function AnnotationBorderStyleClosure() {
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function AnnotationBorderStyle() {
|
|
|
|
this.width = 1;
|
|
|
|
this.style = AnnotationBorderStyleType.SOLID;
|
|
|
|
this.dashArray = [3];
|
|
|
|
this.horizontalCornerRadius = 0;
|
|
|
|
this.verticalCornerRadius = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnotationBorderStyle.prototype = {
|
|
|
|
/**
|
|
|
|
* Set the width.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof AnnotationBorderStyle
|
|
|
|
* @param {integer} width - The width
|
|
|
|
*/
|
|
|
|
setWidth: function AnnotationBorderStyle_setWidth(width) {
|
|
|
|
if (width === (width | 0)) {
|
|
|
|
this.width = width;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the style.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof AnnotationBorderStyle
|
|
|
|
* @param {Object} style - The style object
|
|
|
|
* @see {@link shared/util.js}
|
|
|
|
*/
|
|
|
|
setStyle: function AnnotationBorderStyle_setStyle(style) {
|
|
|
|
if (!style) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (style.name) {
|
|
|
|
case 'S':
|
|
|
|
this.style = AnnotationBorderStyleType.SOLID;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
this.style = AnnotationBorderStyleType.DASHED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
this.style = AnnotationBorderStyleType.BEVELED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
|
|
|
this.style = AnnotationBorderStyleType.INSET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
this.style = AnnotationBorderStyleType.UNDERLINE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the dash array.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof AnnotationBorderStyle
|
|
|
|
* @param {Array} dashArray - The dash array with at least one element
|
|
|
|
*/
|
|
|
|
setDashArray: function AnnotationBorderStyle_setDashArray(dashArray) {
|
|
|
|
// We validate the dash array, but we do not use it because CSS does not
|
|
|
|
// allow us to change spacing of dashes. For more information, visit
|
|
|
|
// http://www.w3.org/TR/css3-background/#the-border-style.
|
|
|
|
if (isArray(dashArray) && dashArray.length > 0) {
|
|
|
|
// According to the PDF specification: the elements in a dashArray
|
|
|
|
// shall be numbers that are nonnegative and not all equal to zero.
|
|
|
|
var isValid = true;
|
|
|
|
var allZeros = true;
|
|
|
|
for (var i = 0, len = dashArray.length; i < len; i++) {
|
|
|
|
var element = dashArray[i];
|
|
|
|
var validNumber = (+element >= 0);
|
|
|
|
if (!validNumber) {
|
|
|
|
isValid = false;
|
|
|
|
break;
|
|
|
|
} else if (element > 0) {
|
|
|
|
allZeros = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isValid && !allZeros) {
|
|
|
|
this.dashArray = dashArray;
|
|
|
|
} else {
|
|
|
|
this.width = 0; // Adobe behavior when the array is invalid.
|
|
|
|
}
|
|
|
|
} else if (dashArray) {
|
|
|
|
this.width = 0; // Adobe behavior when the array is invalid.
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the horizontal corner radius (from a Border dictionary).
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof AnnotationBorderStyle
|
|
|
|
* @param {integer} radius - The horizontal corner radius
|
|
|
|
*/
|
|
|
|
setHorizontalCornerRadius:
|
|
|
|
function AnnotationBorderStyle_setHorizontalCornerRadius(radius) {
|
|
|
|
if (radius === (radius | 0)) {
|
|
|
|
this.horizontalCornerRadius = radius;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the vertical corner radius (from a Border dictionary).
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof AnnotationBorderStyle
|
|
|
|
* @param {integer} radius - The vertical corner radius
|
|
|
|
*/
|
|
|
|
setVerticalCornerRadius:
|
|
|
|
function AnnotationBorderStyle_setVerticalCornerRadius(radius) {
|
|
|
|
if (radius === (radius | 0)) {
|
|
|
|
this.verticalCornerRadius = radius;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return AnnotationBorderStyle;
|
|
|
|
})();
|
|
|
|
|
2013-03-21 17:04:44 +09:00
|
|
|
var WidgetAnnotation = (function WidgetAnnotationClosure() {
|
|
|
|
|
|
|
|
function WidgetAnnotation(params) {
|
|
|
|
Annotation.call(this, params);
|
|
|
|
|
|
|
|
var dict = params.dict;
|
|
|
|
var data = this.data;
|
|
|
|
|
|
|
|
data.fieldValue = stringToPDFString(
|
|
|
|
Util.getInheritableProperty(dict, 'V') || '');
|
|
|
|
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.fieldFlags = Util.getInheritableProperty(dict, 'Ff') || 0;
|
2014-03-26 23:07:38 +09:00
|
|
|
this.fieldResources = Util.getInheritableProperty(dict, 'DR') || Dict.empty;
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
// Building the full field name by collecting the field and
|
|
|
|
// its ancestors 'T' data and joining them using '.'.
|
|
|
|
var fieldName = [];
|
|
|
|
var namedItem = dict;
|
|
|
|
var ref = params.ref;
|
|
|
|
while (namedItem) {
|
|
|
|
var parent = namedItem.get('Parent');
|
|
|
|
var parentRef = namedItem.getRaw('Parent');
|
|
|
|
var name = namedItem.get('T');
|
|
|
|
if (name) {
|
|
|
|
fieldName.unshift(stringToPDFString(name));
|
2014-10-17 01:08:24 +09:00
|
|
|
} else if (parent && ref) {
|
2013-03-21 17:04:44 +09:00
|
|
|
// The field name is absent, that means more than one field
|
|
|
|
// with the same name may exist. Replacing the empty name
|
|
|
|
// with the '`' plus index in the parent's 'Kids' array.
|
|
|
|
// This is not in the PDF spec but necessary to id the
|
|
|
|
// the input controls.
|
|
|
|
var kids = parent.get('Kids');
|
|
|
|
var j, jj;
|
|
|
|
for (j = 0, jj = kids.length; j < jj; j++) {
|
|
|
|
var kidRef = kids[j];
|
2014-08-02 01:23:19 +09:00
|
|
|
if (kidRef.num === ref.num && kidRef.gen === ref.gen) {
|
2013-03-21 17:04:44 +09:00
|
|
|
break;
|
2014-03-04 00:03:23 +09:00
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
}
|
|
|
|
fieldName.unshift('`' + j);
|
|
|
|
}
|
|
|
|
namedItem = parent;
|
|
|
|
ref = parentRef;
|
|
|
|
}
|
|
|
|
data.fullName = fieldName.join('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent = Annotation.prototype;
|
|
|
|
Util.inherit(WidgetAnnotation, Annotation, {
|
|
|
|
isViewable: function WidgetAnnotation_isViewable() {
|
|
|
|
if (this.data.fieldType === 'Sig') {
|
2014-01-04 02:34:13 +09:00
|
|
|
warn('unimplemented annotation type: Widget signature');
|
2013-03-21 17:04:44 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent.isViewable.call(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return WidgetAnnotation;
|
|
|
|
})();
|
|
|
|
|
2013-03-26 07:32:47 +09:00
|
|
|
var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() {
|
|
|
|
function TextWidgetAnnotation(params) {
|
|
|
|
WidgetAnnotation.call(this, params);
|
|
|
|
|
|
|
|
this.data.textAlignment = Util.getInheritableProperty(params.dict, 'Q');
|
2014-06-18 07:43:33 +09:00
|
|
|
this.data.annotationType = AnnotationType.WIDGET;
|
|
|
|
this.data.hasHtml = !this.data.hasAppearance && !!this.data.fieldValue;
|
2013-03-26 07:32:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
Util.inherit(TextWidgetAnnotation, WidgetAnnotation, {
|
2013-05-29 10:16:21 +09:00
|
|
|
getOperatorList: function TextWidgetAnnotation_getOperatorList(evaluator) {
|
2013-08-23 04:29:06 +09:00
|
|
|
if (this.appearance) {
|
|
|
|
return Annotation.prototype.getOperatorList.call(this, evaluator);
|
|
|
|
}
|
2013-05-29 10:16:21 +09:00
|
|
|
|
2013-08-01 03:17:36 +09:00
|
|
|
var opList = new OperatorList();
|
2013-05-29 10:16:21 +09:00
|
|
|
var data = this.data;
|
2013-03-26 07:32:47 +09:00
|
|
|
|
|
|
|
// Even if there is an appearance stream, ignore it. This is the
|
|
|
|
// behaviour used by Adobe Reader.
|
2014-05-09 05:42:47 +09:00
|
|
|
if (!data.defaultAppearance) {
|
2014-05-02 07:52:03 +09:00
|
|
|
return Promise.resolve(opList);
|
2013-03-26 07:32:47 +09:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:42:47 +09:00
|
|
|
var stream = new Stream(stringToBytes(data.defaultAppearance));
|
2014-05-10 10:21:15 +09:00
|
|
|
return evaluator.getOperatorList(stream, this.fieldResources, opList).
|
|
|
|
then(function () {
|
|
|
|
return opList;
|
|
|
|
});
|
2013-03-26 07:32:47 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return TextWidgetAnnotation;
|
|
|
|
})();
|
|
|
|
|
2013-03-21 17:04:44 +09:00
|
|
|
var TextAnnotation = (function TextAnnotationClosure() {
|
|
|
|
function TextAnnotation(params) {
|
2015-07-06 18:58:26 +09:00
|
|
|
Annotation.call(this, params);
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
var dict = params.dict;
|
|
|
|
var data = this.data;
|
|
|
|
|
|
|
|
var content = dict.get('Contents');
|
|
|
|
var title = dict.get('T');
|
2014-06-18 07:43:33 +09:00
|
|
|
data.annotationType = AnnotationType.TEXT;
|
2013-03-21 17:04:44 +09:00
|
|
|
data.content = stringToPDFString(content || '');
|
|
|
|
data.title = stringToPDFString(title || '');
|
2015-07-06 18:58:26 +09:00
|
|
|
data.hasHtml = true;
|
2013-03-21 17:04:44 +09:00
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
if (data.hasAppearance) {
|
|
|
|
data.name = 'NoIcon';
|
|
|
|
} else {
|
2014-04-19 04:11:56 +09:00
|
|
|
data.rect[1] = data.rect[3] - DEFAULT_ICON_SIZE;
|
|
|
|
data.rect[2] = data.rect[0] + DEFAULT_ICON_SIZE;
|
2014-03-07 23:48:42 +09:00
|
|
|
data.name = dict.has('Name') ? dict.get('Name').name : 'Note';
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
|
2014-03-07 23:48:42 +09:00
|
|
|
if (dict.has('C')) {
|
|
|
|
data.hasBgColor = true;
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
|
2015-07-06 18:58:26 +09:00
|
|
|
Util.inherit(TextAnnotation, Annotation, { });
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
return TextAnnotation;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var LinkAnnotation = (function LinkAnnotationClosure() {
|
|
|
|
function LinkAnnotation(params) {
|
2015-07-06 18:58:26 +09:00
|
|
|
Annotation.call(this, params);
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
var dict = params.dict;
|
|
|
|
var data = this.data;
|
2014-06-18 07:43:33 +09:00
|
|
|
data.annotationType = AnnotationType.LINK;
|
2015-07-06 18:58:26 +09:00
|
|
|
data.hasHtml = true;
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
var action = dict.get('A');
|
2015-01-29 06:03:04 +09:00
|
|
|
if (action && isDict(action)) {
|
2013-03-21 17:04:44 +09:00
|
|
|
var linkType = action.get('S').name;
|
|
|
|
if (linkType === 'URI') {
|
2014-01-22 20:27:44 +09:00
|
|
|
var url = action.get('URI');
|
|
|
|
if (isName(url)) {
|
|
|
|
// Some bad PDFs do not put parentheses around relative URLs.
|
|
|
|
url = '/' + url.name;
|
2014-02-15 21:46:57 +09:00
|
|
|
} else if (url) {
|
2014-01-22 20:27:44 +09:00
|
|
|
url = addDefaultProtocolToUrl(url);
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
// TODO: pdf spec mentions urls can be relative to a Base
|
|
|
|
// entry in the dictionary.
|
2013-07-13 03:36:20 +09:00
|
|
|
if (!isValidUrl(url, false)) {
|
2013-03-21 17:04:44 +09:00
|
|
|
url = '';
|
|
|
|
}
|
2015-05-11 07:46:59 +09:00
|
|
|
// According to ISO 32000-1:2008, section 12.6.4.7,
|
|
|
|
// URI should to be encoded in 7-bit ASCII.
|
|
|
|
// Some bad PDFs may have URIs in UTF-8 encoding, see Bugzilla 1122280.
|
|
|
|
try {
|
|
|
|
data.url = stringToUTF8String(url);
|
|
|
|
} catch (e) {
|
|
|
|
// Fall back to a simple copy.
|
|
|
|
data.url = url;
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
} else if (linkType === 'GoTo') {
|
|
|
|
data.dest = action.get('D');
|
|
|
|
} else if (linkType === 'GoToR') {
|
|
|
|
var urlDict = action.get('F');
|
|
|
|
if (isDict(urlDict)) {
|
|
|
|
// We assume that the 'url' is a Filspec dictionary
|
|
|
|
// and fetch the url without checking any further
|
|
|
|
url = urlDict.get('F') || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: pdf reference says that GoToR
|
|
|
|
// can also have 'NewWindow' attribute
|
2013-07-13 03:36:20 +09:00
|
|
|
if (!isValidUrl(url, false)) {
|
2013-03-21 17:04:44 +09:00
|
|
|
url = '';
|
|
|
|
}
|
|
|
|
data.url = url;
|
|
|
|
data.dest = action.get('D');
|
2013-08-09 04:59:59 +09:00
|
|
|
} else if (linkType === 'Named') {
|
|
|
|
data.action = action.get('N').name;
|
2013-03-21 17:04:44 +09:00
|
|
|
} else {
|
2014-01-04 02:34:13 +09:00
|
|
|
warn('unrecognized link type: ' + linkType);
|
2013-03-21 17:04:44 +09:00
|
|
|
}
|
|
|
|
} else if (dict.has('Dest')) {
|
|
|
|
// simple destination link
|
|
|
|
var dest = dict.get('Dest');
|
|
|
|
data.dest = isName(dest) ? dest.name : dest;
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 07:12:23 +09:00
|
|
|
|
|
|
|
// Lets URLs beginning with 'www.' default to using the 'http://' protocol.
|
|
|
|
function addDefaultProtocolToUrl(url) {
|
2014-02-15 21:46:57 +09:00
|
|
|
if (url && url.indexOf('www.') === 0) {
|
2013-10-16 07:12:23 +09:00
|
|
|
return ('http://' + url);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2013-03-21 17:04:44 +09:00
|
|
|
|
2015-07-06 18:58:26 +09:00
|
|
|
Util.inherit(LinkAnnotation, Annotation, { });
|
2013-03-21 17:04:44 +09:00
|
|
|
|
|
|
|
return LinkAnnotation;
|
|
|
|
})();
|