annotation.js 36 KB

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