2
0

annotation.js 33 KB

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