annotation.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.AnnotationFactory = exports.AnnotationBorderStyle = exports.Annotation = undefined;
  20. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  21. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  22. var _util = require('../shared/util');
  23. var _obj = require('./obj');
  24. var _primitives = require('./primitives');
  25. var _colorspace = require('./colorspace');
  26. var _evaluator = require('./evaluator');
  27. var _stream = require('./stream');
  28. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  29. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  31. var AnnotationFactory = function () {
  32. function AnnotationFactory() {
  33. _classCallCheck(this, AnnotationFactory);
  34. }
  35. _createClass(AnnotationFactory, null, [{
  36. key: 'create',
  37. value: function create(xref, ref, pdfManager, idFactory) {
  38. var dict = xref.fetchIfRef(ref);
  39. if (!(0, _primitives.isDict)(dict)) {
  40. return;
  41. }
  42. var id = (0, _primitives.isRef)(ref) ? ref.toString() : 'annot_' + idFactory.createObjId();
  43. var subtype = dict.get('Subtype');
  44. subtype = (0, _primitives.isName)(subtype) ? subtype.name : null;
  45. var parameters = {
  46. xref: xref,
  47. dict: dict,
  48. ref: (0, _primitives.isRef)(ref) ? ref : null,
  49. subtype: subtype,
  50. id: id,
  51. pdfManager: pdfManager
  52. };
  53. switch (subtype) {
  54. case 'Link':
  55. return new LinkAnnotation(parameters);
  56. case 'Text':
  57. return new TextAnnotation(parameters);
  58. case 'Widget':
  59. var fieldType = _util.Util.getInheritableProperty(dict, 'FT');
  60. fieldType = (0, _primitives.isName)(fieldType) ? fieldType.name : null;
  61. switch (fieldType) {
  62. case 'Tx':
  63. return new TextWidgetAnnotation(parameters);
  64. case 'Btn':
  65. return new ButtonWidgetAnnotation(parameters);
  66. case 'Ch':
  67. return new ChoiceWidgetAnnotation(parameters);
  68. }
  69. (0, _util.warn)('Unimplemented widget field type "' + fieldType + '", ' + 'falling back to base field type.');
  70. return new WidgetAnnotation(parameters);
  71. case 'Popup':
  72. return new PopupAnnotation(parameters);
  73. case 'Line':
  74. return new LineAnnotation(parameters);
  75. case 'Square':
  76. return new SquareAnnotation(parameters);
  77. case 'Circle':
  78. return new CircleAnnotation(parameters);
  79. case 'Highlight':
  80. return new HighlightAnnotation(parameters);
  81. case 'Underline':
  82. return new UnderlineAnnotation(parameters);
  83. case 'Squiggly':
  84. return new SquigglyAnnotation(parameters);
  85. case 'StrikeOut':
  86. return new StrikeOutAnnotation(parameters);
  87. case 'FileAttachment':
  88. return new FileAttachmentAnnotation(parameters);
  89. default:
  90. if (!subtype) {
  91. (0, _util.warn)('Annotation is missing the required /Subtype.');
  92. } else {
  93. (0, _util.warn)('Unimplemented annotation type "' + subtype + '", ' + 'falling back to base annotation.');
  94. }
  95. return new Annotation(parameters);
  96. }
  97. }
  98. }]);
  99. return AnnotationFactory;
  100. }();
  101. function getTransformMatrix(rect, bbox, matrix) {
  102. var bounds = _util.Util.getAxialAlignedBoundingBox(bbox, matrix);
  103. var minX = bounds[0];
  104. var minY = bounds[1];
  105. var maxX = bounds[2];
  106. var maxY = bounds[3];
  107. if (minX === maxX || minY === maxY) {
  108. return [1, 0, 0, 1, rect[0], rect[1]];
  109. }
  110. var xRatio = (rect[2] - rect[0]) / (maxX - minX);
  111. var yRatio = (rect[3] - rect[1]) / (maxY - minY);
  112. return [xRatio, 0, 0, yRatio, rect[0] - minX * xRatio, rect[1] - minY * yRatio];
  113. }
  114. var Annotation = function () {
  115. function Annotation(params) {
  116. _classCallCheck(this, Annotation);
  117. var dict = params.dict;
  118. this.setFlags(dict.get('F'));
  119. this.setRectangle(dict.getArray('Rect'));
  120. this.setColor(dict.getArray('C'));
  121. this.setBorderStyle(dict);
  122. this.setAppearance(dict);
  123. this.data = {
  124. annotationFlags: this.flags,
  125. borderStyle: this.borderStyle,
  126. color: this.color,
  127. hasAppearance: !!this.appearance,
  128. id: params.id,
  129. rect: this.rectangle,
  130. subtype: params.subtype
  131. };
  132. }
  133. _createClass(Annotation, [{
  134. key: '_hasFlag',
  135. value: function _hasFlag(flags, flag) {
  136. return !!(flags & flag);
  137. }
  138. }, {
  139. key: '_isViewable',
  140. value: function _isViewable(flags) {
  141. return !this._hasFlag(flags, _util.AnnotationFlag.INVISIBLE) && !this._hasFlag(flags, _util.AnnotationFlag.HIDDEN) && !this._hasFlag(flags, _util.AnnotationFlag.NOVIEW);
  142. }
  143. }, {
  144. key: '_isPrintable',
  145. value: function _isPrintable(flags) {
  146. return this._hasFlag(flags, _util.AnnotationFlag.PRINT) && !this._hasFlag(flags, _util.AnnotationFlag.INVISIBLE) && !this._hasFlag(flags, _util.AnnotationFlag.HIDDEN);
  147. }
  148. }, {
  149. key: 'setFlags',
  150. value: function setFlags(flags) {
  151. this.flags = Number.isInteger(flags) && flags > 0 ? flags : 0;
  152. }
  153. }, {
  154. key: 'hasFlag',
  155. value: function hasFlag(flag) {
  156. return this._hasFlag(this.flags, flag);
  157. }
  158. }, {
  159. key: 'setRectangle',
  160. value: function setRectangle(rectangle) {
  161. if (Array.isArray(rectangle) && rectangle.length === 4) {
  162. this.rectangle = _util.Util.normalizeRect(rectangle);
  163. } else {
  164. this.rectangle = [0, 0, 0, 0];
  165. }
  166. }
  167. }, {
  168. key: 'setColor',
  169. value: function setColor(color) {
  170. var rgbColor = new Uint8Array(3);
  171. if (!Array.isArray(color)) {
  172. this.color = rgbColor;
  173. return;
  174. }
  175. switch (color.length) {
  176. case 0:
  177. this.color = null;
  178. break;
  179. case 1:
  180. _colorspace.ColorSpace.singletons.gray.getRgbItem(color, 0, rgbColor, 0);
  181. this.color = rgbColor;
  182. break;
  183. case 3:
  184. _colorspace.ColorSpace.singletons.rgb.getRgbItem(color, 0, rgbColor, 0);
  185. this.color = rgbColor;
  186. break;
  187. case 4:
  188. _colorspace.ColorSpace.singletons.cmyk.getRgbItem(color, 0, rgbColor, 0);
  189. this.color = rgbColor;
  190. break;
  191. default:
  192. this.color = rgbColor;
  193. break;
  194. }
  195. }
  196. }, {
  197. key: 'setBorderStyle',
  198. value: function setBorderStyle(borderStyle) {
  199. this.borderStyle = new AnnotationBorderStyle();
  200. if (!(0, _primitives.isDict)(borderStyle)) {
  201. return;
  202. }
  203. if (borderStyle.has('BS')) {
  204. var dict = borderStyle.get('BS');
  205. var dictType = dict.get('Type');
  206. if (!dictType || (0, _primitives.isName)(dictType, 'Border')) {
  207. this.borderStyle.setWidth(dict.get('W'));
  208. this.borderStyle.setStyle(dict.get('S'));
  209. this.borderStyle.setDashArray(dict.getArray('D'));
  210. }
  211. } else if (borderStyle.has('Border')) {
  212. var array = borderStyle.getArray('Border');
  213. if (Array.isArray(array) && array.length >= 3) {
  214. this.borderStyle.setHorizontalCornerRadius(array[0]);
  215. this.borderStyle.setVerticalCornerRadius(array[1]);
  216. this.borderStyle.setWidth(array[2]);
  217. if (array.length === 4) {
  218. this.borderStyle.setDashArray(array[3]);
  219. }
  220. }
  221. } else {
  222. this.borderStyle.setWidth(0);
  223. }
  224. }
  225. }, {
  226. key: 'setAppearance',
  227. value: function setAppearance(dict) {
  228. this.appearance = null;
  229. var appearanceStates = dict.get('AP');
  230. if (!(0, _primitives.isDict)(appearanceStates)) {
  231. return;
  232. }
  233. var normalAppearanceState = appearanceStates.get('N');
  234. if ((0, _primitives.isStream)(normalAppearanceState)) {
  235. this.appearance = normalAppearanceState;
  236. return;
  237. }
  238. if (!(0, _primitives.isDict)(normalAppearanceState)) {
  239. return;
  240. }
  241. var as = dict.get('AS');
  242. if (!(0, _primitives.isName)(as) || !normalAppearanceState.has(as.name)) {
  243. return;
  244. }
  245. this.appearance = normalAppearanceState.get(as.name);
  246. }
  247. }, {
  248. key: '_preparePopup',
  249. value: function _preparePopup(dict) {
  250. if (!dict.has('C')) {
  251. this.data.color = null;
  252. }
  253. this.data.hasPopup = dict.has('Popup');
  254. this.data.title = (0, _util.stringToPDFString)(dict.get('T') || '');
  255. this.data.contents = (0, _util.stringToPDFString)(dict.get('Contents') || '');
  256. }
  257. }, {
  258. key: 'loadResources',
  259. value: function loadResources(keys) {
  260. return this.appearance.dict.getAsync('Resources').then(function (resources) {
  261. if (!resources) {
  262. return;
  263. }
  264. var objectLoader = new _obj.ObjectLoader(resources, keys, resources.xref);
  265. return objectLoader.load().then(function () {
  266. return resources;
  267. });
  268. });
  269. }
  270. }, {
  271. key: 'getOperatorList',
  272. value: function getOperatorList(evaluator, task, renderForms) {
  273. var _this = this;
  274. if (!this.appearance) {
  275. return Promise.resolve(new _evaluator.OperatorList());
  276. }
  277. var data = this.data;
  278. var appearanceDict = this.appearance.dict;
  279. var resourcesPromise = this.loadResources(['ExtGState', 'ColorSpace', 'Pattern', 'Shading', 'XObject', 'Font']);
  280. var bbox = appearanceDict.getArray('BBox') || [0, 0, 1, 1];
  281. var matrix = appearanceDict.getArray('Matrix') || [1, 0, 0, 1, 0, 0];
  282. var transform = getTransformMatrix(data.rect, bbox, matrix);
  283. return resourcesPromise.then(function (resources) {
  284. var opList = new _evaluator.OperatorList();
  285. opList.addOp(_util.OPS.beginAnnotation, [data.rect, transform, matrix]);
  286. return evaluator.getOperatorList({
  287. stream: _this.appearance,
  288. task: task,
  289. resources: resources,
  290. operatorList: opList
  291. }).then(function () {
  292. opList.addOp(_util.OPS.endAnnotation, []);
  293. _this.appearance.reset();
  294. return opList;
  295. });
  296. });
  297. }
  298. }, {
  299. key: 'viewable',
  300. get: function get() {
  301. if (this.flags === 0) {
  302. return true;
  303. }
  304. return this._isViewable(this.flags);
  305. }
  306. }, {
  307. key: 'printable',
  308. get: function get() {
  309. if (this.flags === 0) {
  310. return false;
  311. }
  312. return this._isPrintable(this.flags);
  313. }
  314. }]);
  315. return Annotation;
  316. }();
  317. var AnnotationBorderStyle = function () {
  318. function AnnotationBorderStyle() {
  319. _classCallCheck(this, AnnotationBorderStyle);
  320. this.width = 1;
  321. this.style = _util.AnnotationBorderStyleType.SOLID;
  322. this.dashArray = [3];
  323. this.horizontalCornerRadius = 0;
  324. this.verticalCornerRadius = 0;
  325. }
  326. _createClass(AnnotationBorderStyle, [{
  327. key: 'setWidth',
  328. value: function setWidth(width) {
  329. if (Number.isInteger(width)) {
  330. this.width = width;
  331. }
  332. }
  333. }, {
  334. key: 'setStyle',
  335. value: function setStyle(style) {
  336. if (!style) {
  337. return;
  338. }
  339. switch (style.name) {
  340. case 'S':
  341. this.style = _util.AnnotationBorderStyleType.SOLID;
  342. break;
  343. case 'D':
  344. this.style = _util.AnnotationBorderStyleType.DASHED;
  345. break;
  346. case 'B':
  347. this.style = _util.AnnotationBorderStyleType.BEVELED;
  348. break;
  349. case 'I':
  350. this.style = _util.AnnotationBorderStyleType.INSET;
  351. break;
  352. case 'U':
  353. this.style = _util.AnnotationBorderStyleType.UNDERLINE;
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. }, {
  360. key: 'setDashArray',
  361. value: function setDashArray(dashArray) {
  362. if (Array.isArray(dashArray) && dashArray.length > 0) {
  363. var isValid = true;
  364. var allZeros = true;
  365. for (var i = 0, len = dashArray.length; i < len; i++) {
  366. var element = dashArray[i];
  367. var validNumber = +element >= 0;
  368. if (!validNumber) {
  369. isValid = false;
  370. break;
  371. } else if (element > 0) {
  372. allZeros = false;
  373. }
  374. }
  375. if (isValid && !allZeros) {
  376. this.dashArray = dashArray;
  377. } else {
  378. this.width = 0;
  379. }
  380. } else if (dashArray) {
  381. this.width = 0;
  382. }
  383. }
  384. }, {
  385. key: 'setHorizontalCornerRadius',
  386. value: function setHorizontalCornerRadius(radius) {
  387. if (Number.isInteger(radius)) {
  388. this.horizontalCornerRadius = radius;
  389. }
  390. }
  391. }, {
  392. key: 'setVerticalCornerRadius',
  393. value: function setVerticalCornerRadius(radius) {
  394. if (Number.isInteger(radius)) {
  395. this.verticalCornerRadius = radius;
  396. }
  397. }
  398. }]);
  399. return AnnotationBorderStyle;
  400. }();
  401. var WidgetAnnotation = function (_Annotation) {
  402. _inherits(WidgetAnnotation, _Annotation);
  403. function WidgetAnnotation(params) {
  404. _classCallCheck(this, WidgetAnnotation);
  405. var _this2 = _possibleConstructorReturn(this, (WidgetAnnotation.__proto__ || Object.getPrototypeOf(WidgetAnnotation)).call(this, params));
  406. var dict = params.dict;
  407. var data = _this2.data;
  408. data.annotationType = _util.AnnotationType.WIDGET;
  409. data.fieldName = _this2._constructFieldName(dict);
  410. data.fieldValue = _util.Util.getInheritableProperty(dict, 'V', true);
  411. data.alternativeText = (0, _util.stringToPDFString)(dict.get('TU') || '');
  412. data.defaultAppearance = _util.Util.getInheritableProperty(dict, 'DA') || '';
  413. var fieldType = _util.Util.getInheritableProperty(dict, 'FT');
  414. data.fieldType = (0, _primitives.isName)(fieldType) ? fieldType.name : null;
  415. _this2.fieldResources = _util.Util.getInheritableProperty(dict, 'DR') || _primitives.Dict.empty;
  416. data.fieldFlags = _util.Util.getInheritableProperty(dict, 'Ff');
  417. if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
  418. data.fieldFlags = 0;
  419. }
  420. data.readOnly = _this2.hasFieldFlag(_util.AnnotationFieldFlag.READONLY);
  421. if (data.fieldType === 'Sig') {
  422. _this2.setFlags(_util.AnnotationFlag.HIDDEN);
  423. }
  424. return _this2;
  425. }
  426. _createClass(WidgetAnnotation, [{
  427. key: '_constructFieldName',
  428. value: function _constructFieldName(dict) {
  429. if (!dict.has('T') && !dict.has('Parent')) {
  430. (0, _util.warn)('Unknown field name, falling back to empty field name.');
  431. return '';
  432. }
  433. if (!dict.has('Parent')) {
  434. return (0, _util.stringToPDFString)(dict.get('T'));
  435. }
  436. var fieldName = [];
  437. if (dict.has('T')) {
  438. fieldName.unshift((0, _util.stringToPDFString)(dict.get('T')));
  439. }
  440. var loopDict = dict;
  441. while (loopDict.has('Parent')) {
  442. loopDict = loopDict.get('Parent');
  443. if (!(0, _primitives.isDict)(loopDict)) {
  444. break;
  445. }
  446. if (loopDict.has('T')) {
  447. fieldName.unshift((0, _util.stringToPDFString)(loopDict.get('T')));
  448. }
  449. }
  450. return fieldName.join('.');
  451. }
  452. }, {
  453. key: 'hasFieldFlag',
  454. value: function hasFieldFlag(flag) {
  455. return !!(this.data.fieldFlags & flag);
  456. }
  457. }, {
  458. key: 'getOperatorList',
  459. value: function getOperatorList(evaluator, task, renderForms) {
  460. if (renderForms) {
  461. return Promise.resolve(new _evaluator.OperatorList());
  462. }
  463. return _get(WidgetAnnotation.prototype.__proto__ || Object.getPrototypeOf(WidgetAnnotation.prototype), 'getOperatorList', this).call(this, evaluator, task, renderForms);
  464. }
  465. }]);
  466. return WidgetAnnotation;
  467. }(Annotation);
  468. var TextWidgetAnnotation = function (_WidgetAnnotation) {
  469. _inherits(TextWidgetAnnotation, _WidgetAnnotation);
  470. function TextWidgetAnnotation(params) {
  471. _classCallCheck(this, TextWidgetAnnotation);
  472. var _this3 = _possibleConstructorReturn(this, (TextWidgetAnnotation.__proto__ || Object.getPrototypeOf(TextWidgetAnnotation)).call(this, params));
  473. _this3.data.fieldValue = (0, _util.stringToPDFString)(_this3.data.fieldValue || '');
  474. var alignment = _util.Util.getInheritableProperty(params.dict, 'Q');
  475. if (!Number.isInteger(alignment) || alignment < 0 || alignment > 2) {
  476. alignment = null;
  477. }
  478. _this3.data.textAlignment = alignment;
  479. var maximumLength = _util.Util.getInheritableProperty(params.dict, 'MaxLen');
  480. if (!Number.isInteger(maximumLength) || maximumLength < 0) {
  481. maximumLength = null;
  482. }
  483. _this3.data.maxLen = maximumLength;
  484. _this3.data.multiLine = _this3.hasFieldFlag(_util.AnnotationFieldFlag.MULTILINE);
  485. _this3.data.comb = _this3.hasFieldFlag(_util.AnnotationFieldFlag.COMB) && !_this3.hasFieldFlag(_util.AnnotationFieldFlag.MULTILINE) && !_this3.hasFieldFlag(_util.AnnotationFieldFlag.PASSWORD) && !_this3.hasFieldFlag(_util.AnnotationFieldFlag.FILESELECT) && _this3.data.maxLen !== null;
  486. return _this3;
  487. }
  488. _createClass(TextWidgetAnnotation, [{
  489. key: 'getOperatorList',
  490. value: function getOperatorList(evaluator, task, renderForms) {
  491. if (renderForms || this.appearance) {
  492. return _get(TextWidgetAnnotation.prototype.__proto__ || Object.getPrototypeOf(TextWidgetAnnotation.prototype), 'getOperatorList', this).call(this, evaluator, task, renderForms);
  493. }
  494. var operatorList = new _evaluator.OperatorList();
  495. if (!this.data.defaultAppearance) {
  496. return Promise.resolve(operatorList);
  497. }
  498. var stream = new _stream.Stream((0, _util.stringToBytes)(this.data.defaultAppearance));
  499. return evaluator.getOperatorList({
  500. stream: stream,
  501. task: task,
  502. resources: this.fieldResources,
  503. operatorList: operatorList
  504. }).then(function () {
  505. return operatorList;
  506. });
  507. }
  508. }]);
  509. return TextWidgetAnnotation;
  510. }(WidgetAnnotation);
  511. var ButtonWidgetAnnotation = function (_WidgetAnnotation2) {
  512. _inherits(ButtonWidgetAnnotation, _WidgetAnnotation2);
  513. function ButtonWidgetAnnotation(params) {
  514. _classCallCheck(this, ButtonWidgetAnnotation);
  515. var _this4 = _possibleConstructorReturn(this, (ButtonWidgetAnnotation.__proto__ || Object.getPrototypeOf(ButtonWidgetAnnotation)).call(this, params));
  516. _this4.data.checkBox = !_this4.hasFieldFlag(_util.AnnotationFieldFlag.RADIO) && !_this4.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  517. if (_this4.data.checkBox) {
  518. if (!(0, _primitives.isName)(_this4.data.fieldValue)) {
  519. return _possibleConstructorReturn(_this4);
  520. }
  521. _this4.data.fieldValue = _this4.data.fieldValue.name;
  522. }
  523. _this4.data.radioButton = _this4.hasFieldFlag(_util.AnnotationFieldFlag.RADIO) && !_this4.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  524. if (_this4.data.radioButton) {
  525. _this4.data.fieldValue = _this4.data.buttonValue = null;
  526. var fieldParent = params.dict.get('Parent');
  527. if ((0, _primitives.isDict)(fieldParent) && fieldParent.has('V')) {
  528. var fieldParentValue = fieldParent.get('V');
  529. if ((0, _primitives.isName)(fieldParentValue)) {
  530. _this4.data.fieldValue = fieldParentValue.name;
  531. }
  532. }
  533. var appearanceStates = params.dict.get('AP');
  534. if (!(0, _primitives.isDict)(appearanceStates)) {
  535. return _possibleConstructorReturn(_this4);
  536. }
  537. var normalAppearanceState = appearanceStates.get('N');
  538. if (!(0, _primitives.isDict)(normalAppearanceState)) {
  539. return _possibleConstructorReturn(_this4);
  540. }
  541. var keys = normalAppearanceState.getKeys();
  542. for (var i = 0, ii = keys.length; i < ii; i++) {
  543. if (keys[i] !== 'Off') {
  544. _this4.data.buttonValue = keys[i];
  545. break;
  546. }
  547. }
  548. }
  549. return _this4;
  550. }
  551. return ButtonWidgetAnnotation;
  552. }(WidgetAnnotation);
  553. var ChoiceWidgetAnnotation = function (_WidgetAnnotation3) {
  554. _inherits(ChoiceWidgetAnnotation, _WidgetAnnotation3);
  555. function ChoiceWidgetAnnotation(params) {
  556. _classCallCheck(this, ChoiceWidgetAnnotation);
  557. var _this5 = _possibleConstructorReturn(this, (ChoiceWidgetAnnotation.__proto__ || Object.getPrototypeOf(ChoiceWidgetAnnotation)).call(this, params));
  558. _this5.data.options = [];
  559. var options = _util.Util.getInheritableProperty(params.dict, 'Opt');
  560. if (Array.isArray(options)) {
  561. var xref = params.xref;
  562. for (var i = 0, ii = options.length; i < ii; i++) {
  563. var option = xref.fetchIfRef(options[i]);
  564. var isOptionArray = Array.isArray(option);
  565. _this5.data.options[i] = {
  566. exportValue: isOptionArray ? xref.fetchIfRef(option[0]) : option,
  567. displayValue: isOptionArray ? xref.fetchIfRef(option[1]) : option
  568. };
  569. }
  570. }
  571. if (!Array.isArray(_this5.data.fieldValue)) {
  572. _this5.data.fieldValue = [_this5.data.fieldValue];
  573. }
  574. _this5.data.combo = _this5.hasFieldFlag(_util.AnnotationFieldFlag.COMBO);
  575. _this5.data.multiSelect = _this5.hasFieldFlag(_util.AnnotationFieldFlag.MULTISELECT);
  576. return _this5;
  577. }
  578. return ChoiceWidgetAnnotation;
  579. }(WidgetAnnotation);
  580. var TextAnnotation = function (_Annotation2) {
  581. _inherits(TextAnnotation, _Annotation2);
  582. function TextAnnotation(parameters) {
  583. _classCallCheck(this, TextAnnotation);
  584. var DEFAULT_ICON_SIZE = 22;
  585. var _this6 = _possibleConstructorReturn(this, (TextAnnotation.__proto__ || Object.getPrototypeOf(TextAnnotation)).call(this, parameters));
  586. _this6.data.annotationType = _util.AnnotationType.TEXT;
  587. if (_this6.data.hasAppearance) {
  588. _this6.data.name = 'NoIcon';
  589. } else {
  590. _this6.data.rect[1] = _this6.data.rect[3] - DEFAULT_ICON_SIZE;
  591. _this6.data.rect[2] = _this6.data.rect[0] + DEFAULT_ICON_SIZE;
  592. _this6.data.name = parameters.dict.has('Name') ? parameters.dict.get('Name').name : 'Note';
  593. }
  594. _this6._preparePopup(parameters.dict);
  595. return _this6;
  596. }
  597. return TextAnnotation;
  598. }(Annotation);
  599. var LinkAnnotation = function (_Annotation3) {
  600. _inherits(LinkAnnotation, _Annotation3);
  601. function LinkAnnotation(params) {
  602. _classCallCheck(this, LinkAnnotation);
  603. var _this7 = _possibleConstructorReturn(this, (LinkAnnotation.__proto__ || Object.getPrototypeOf(LinkAnnotation)).call(this, params));
  604. _this7.data.annotationType = _util.AnnotationType.LINK;
  605. _obj.Catalog.parseDestDictionary({
  606. destDict: params.dict,
  607. resultObj: _this7.data,
  608. docBaseUrl: params.pdfManager.docBaseUrl
  609. });
  610. return _this7;
  611. }
  612. return LinkAnnotation;
  613. }(Annotation);
  614. var PopupAnnotation = function (_Annotation4) {
  615. _inherits(PopupAnnotation, _Annotation4);
  616. function PopupAnnotation(parameters) {
  617. _classCallCheck(this, PopupAnnotation);
  618. var _this8 = _possibleConstructorReturn(this, (PopupAnnotation.__proto__ || Object.getPrototypeOf(PopupAnnotation)).call(this, parameters));
  619. _this8.data.annotationType = _util.AnnotationType.POPUP;
  620. var dict = parameters.dict;
  621. var parentItem = dict.get('Parent');
  622. if (!parentItem) {
  623. (0, _util.warn)('Popup annotation has a missing or invalid parent annotation.');
  624. return _possibleConstructorReturn(_this8);
  625. }
  626. var parentSubtype = parentItem.get('Subtype');
  627. _this8.data.parentType = (0, _primitives.isName)(parentSubtype) ? parentSubtype.name : null;
  628. _this8.data.parentId = dict.getRaw('Parent').toString();
  629. _this8.data.title = (0, _util.stringToPDFString)(parentItem.get('T') || '');
  630. _this8.data.contents = (0, _util.stringToPDFString)(parentItem.get('Contents') || '');
  631. if (!parentItem.has('C')) {
  632. _this8.data.color = null;
  633. } else {
  634. _this8.setColor(parentItem.getArray('C'));
  635. _this8.data.color = _this8.color;
  636. }
  637. if (!_this8.viewable) {
  638. var parentFlags = parentItem.get('F');
  639. if (_this8._isViewable(parentFlags)) {
  640. _this8.setFlags(parentFlags);
  641. }
  642. }
  643. return _this8;
  644. }
  645. return PopupAnnotation;
  646. }(Annotation);
  647. var LineAnnotation = function (_Annotation5) {
  648. _inherits(LineAnnotation, _Annotation5);
  649. function LineAnnotation(parameters) {
  650. _classCallCheck(this, LineAnnotation);
  651. var _this9 = _possibleConstructorReturn(this, (LineAnnotation.__proto__ || Object.getPrototypeOf(LineAnnotation)).call(this, parameters));
  652. _this9.data.annotationType = _util.AnnotationType.LINE;
  653. var dict = parameters.dict;
  654. _this9.data.lineCoordinates = _util.Util.normalizeRect(dict.getArray('L'));
  655. _this9._preparePopup(dict);
  656. return _this9;
  657. }
  658. return LineAnnotation;
  659. }(Annotation);
  660. var SquareAnnotation = function (_Annotation6) {
  661. _inherits(SquareAnnotation, _Annotation6);
  662. function SquareAnnotation(parameters) {
  663. _classCallCheck(this, SquareAnnotation);
  664. var _this10 = _possibleConstructorReturn(this, (SquareAnnotation.__proto__ || Object.getPrototypeOf(SquareAnnotation)).call(this, parameters));
  665. _this10.data.annotationType = _util.AnnotationType.SQUARE;
  666. _this10._preparePopup(parameters.dict);
  667. return _this10;
  668. }
  669. return SquareAnnotation;
  670. }(Annotation);
  671. var CircleAnnotation = function (_Annotation7) {
  672. _inherits(CircleAnnotation, _Annotation7);
  673. function CircleAnnotation(parameters) {
  674. _classCallCheck(this, CircleAnnotation);
  675. var _this11 = _possibleConstructorReturn(this, (CircleAnnotation.__proto__ || Object.getPrototypeOf(CircleAnnotation)).call(this, parameters));
  676. _this11.data.annotationType = _util.AnnotationType.CIRCLE;
  677. _this11._preparePopup(parameters.dict);
  678. return _this11;
  679. }
  680. return CircleAnnotation;
  681. }(Annotation);
  682. var HighlightAnnotation = function (_Annotation8) {
  683. _inherits(HighlightAnnotation, _Annotation8);
  684. function HighlightAnnotation(parameters) {
  685. _classCallCheck(this, HighlightAnnotation);
  686. var _this12 = _possibleConstructorReturn(this, (HighlightAnnotation.__proto__ || Object.getPrototypeOf(HighlightAnnotation)).call(this, parameters));
  687. _this12.data.annotationType = _util.AnnotationType.HIGHLIGHT;
  688. _this12._preparePopup(parameters.dict);
  689. return _this12;
  690. }
  691. return HighlightAnnotation;
  692. }(Annotation);
  693. var UnderlineAnnotation = function (_Annotation9) {
  694. _inherits(UnderlineAnnotation, _Annotation9);
  695. function UnderlineAnnotation(parameters) {
  696. _classCallCheck(this, UnderlineAnnotation);
  697. var _this13 = _possibleConstructorReturn(this, (UnderlineAnnotation.__proto__ || Object.getPrototypeOf(UnderlineAnnotation)).call(this, parameters));
  698. _this13.data.annotationType = _util.AnnotationType.UNDERLINE;
  699. _this13._preparePopup(parameters.dict);
  700. return _this13;
  701. }
  702. return UnderlineAnnotation;
  703. }(Annotation);
  704. var SquigglyAnnotation = function (_Annotation10) {
  705. _inherits(SquigglyAnnotation, _Annotation10);
  706. function SquigglyAnnotation(parameters) {
  707. _classCallCheck(this, SquigglyAnnotation);
  708. var _this14 = _possibleConstructorReturn(this, (SquigglyAnnotation.__proto__ || Object.getPrototypeOf(SquigglyAnnotation)).call(this, parameters));
  709. _this14.data.annotationType = _util.AnnotationType.SQUIGGLY;
  710. _this14._preparePopup(parameters.dict);
  711. return _this14;
  712. }
  713. return SquigglyAnnotation;
  714. }(Annotation);
  715. var StrikeOutAnnotation = function (_Annotation11) {
  716. _inherits(StrikeOutAnnotation, _Annotation11);
  717. function StrikeOutAnnotation(parameters) {
  718. _classCallCheck(this, StrikeOutAnnotation);
  719. var _this15 = _possibleConstructorReturn(this, (StrikeOutAnnotation.__proto__ || Object.getPrototypeOf(StrikeOutAnnotation)).call(this, parameters));
  720. _this15.data.annotationType = _util.AnnotationType.STRIKEOUT;
  721. _this15._preparePopup(parameters.dict);
  722. return _this15;
  723. }
  724. return StrikeOutAnnotation;
  725. }(Annotation);
  726. var FileAttachmentAnnotation = function (_Annotation12) {
  727. _inherits(FileAttachmentAnnotation, _Annotation12);
  728. function FileAttachmentAnnotation(parameters) {
  729. _classCallCheck(this, FileAttachmentAnnotation);
  730. var _this16 = _possibleConstructorReturn(this, (FileAttachmentAnnotation.__proto__ || Object.getPrototypeOf(FileAttachmentAnnotation)).call(this, parameters));
  731. var file = new _obj.FileSpec(parameters.dict.get('FS'), parameters.xref);
  732. _this16.data.annotationType = _util.AnnotationType.FILEATTACHMENT;
  733. _this16.data.file = file.serializable;
  734. _this16._preparePopup(parameters.dict);
  735. return _this16;
  736. }
  737. return FileAttachmentAnnotation;
  738. }(Annotation);
  739. exports.Annotation = Annotation;
  740. exports.AnnotationBorderStyle = AnnotationBorderStyle;
  741. exports.AnnotationFactory = AnnotationFactory;