2
0

annotation.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.getQuadPoints = getQuadPoints;
  27. exports.MarkupAnnotation = exports.AnnotationFactory = exports.AnnotationBorderStyle = exports.Annotation = void 0;
  28. var _util = require("../shared/util.js");
  29. var _obj = require("./obj.js");
  30. var _primitives = require("./primitives.js");
  31. var _colorspace = require("./colorspace.js");
  32. var _core_utils = require("./core_utils.js");
  33. var _operator_list = require("./operator_list.js");
  34. var _stream = require("./stream.js");
  35. var _writer = require("./writer.js");
  36. class AnnotationFactory {
  37. static create(xref, ref, pdfManager, idFactory) {
  38. return pdfManager.ensureCatalog("acroForm").then(acroForm => {
  39. return pdfManager.ensure(this, "_create", [xref, ref, pdfManager, idFactory, acroForm]);
  40. });
  41. }
  42. static _create(xref, ref, pdfManager, idFactory, acroForm) {
  43. const dict = xref.fetchIfRef(ref);
  44. if (!(0, _primitives.isDict)(dict)) {
  45. return undefined;
  46. }
  47. const id = (0, _primitives.isRef)(ref) ? ref.toString() : `annot_${idFactory.createObjId()}`;
  48. let subtype = dict.get("Subtype");
  49. subtype = (0, _primitives.isName)(subtype) ? subtype.name : null;
  50. const parameters = {
  51. xref,
  52. ref,
  53. dict,
  54. subtype,
  55. id,
  56. pdfManager,
  57. acroForm: acroForm instanceof _primitives.Dict ? acroForm : _primitives.Dict.empty
  58. };
  59. switch (subtype) {
  60. case "Link":
  61. return new LinkAnnotation(parameters);
  62. case "Text":
  63. return new TextAnnotation(parameters);
  64. case "Widget":
  65. let fieldType = (0, _core_utils.getInheritableProperty)({
  66. dict,
  67. key: "FT"
  68. });
  69. fieldType = (0, _primitives.isName)(fieldType) ? fieldType.name : null;
  70. switch (fieldType) {
  71. case "Tx":
  72. return new TextWidgetAnnotation(parameters);
  73. case "Btn":
  74. return new ButtonWidgetAnnotation(parameters);
  75. case "Ch":
  76. return new ChoiceWidgetAnnotation(parameters);
  77. }
  78. (0, _util.warn)('Unimplemented widget field type "' + fieldType + '", ' + "falling back to base field type.");
  79. return new WidgetAnnotation(parameters);
  80. case "Popup":
  81. return new PopupAnnotation(parameters);
  82. case "FreeText":
  83. return new FreeTextAnnotation(parameters);
  84. case "Line":
  85. return new LineAnnotation(parameters);
  86. case "Square":
  87. return new SquareAnnotation(parameters);
  88. case "Circle":
  89. return new CircleAnnotation(parameters);
  90. case "PolyLine":
  91. return new PolylineAnnotation(parameters);
  92. case "Polygon":
  93. return new PolygonAnnotation(parameters);
  94. case "Caret":
  95. return new CaretAnnotation(parameters);
  96. case "Ink":
  97. return new InkAnnotation(parameters);
  98. case "Highlight":
  99. return new HighlightAnnotation(parameters);
  100. case "Underline":
  101. return new UnderlineAnnotation(parameters);
  102. case "Squiggly":
  103. return new SquigglyAnnotation(parameters);
  104. case "StrikeOut":
  105. return new StrikeOutAnnotation(parameters);
  106. case "Stamp":
  107. return new StampAnnotation(parameters);
  108. case "FileAttachment":
  109. return new FileAttachmentAnnotation(parameters);
  110. default:
  111. if (!subtype) {
  112. (0, _util.warn)("Annotation is missing the required /Subtype.");
  113. } else {
  114. (0, _util.warn)('Unimplemented annotation type "' + subtype + '", ' + "falling back to base annotation.");
  115. }
  116. return new Annotation(parameters);
  117. }
  118. }
  119. }
  120. exports.AnnotationFactory = AnnotationFactory;
  121. function getQuadPoints(dict, rect) {
  122. if (!dict.has("QuadPoints")) {
  123. return null;
  124. }
  125. const quadPoints = dict.getArray("QuadPoints");
  126. if (!Array.isArray(quadPoints) || quadPoints.length % 8 > 0) {
  127. return null;
  128. }
  129. const quadPointsLists = [];
  130. for (let i = 0, ii = quadPoints.length / 8; i < ii; i++) {
  131. quadPointsLists.push([]);
  132. for (let j = i * 8, jj = i * 8 + 8; j < jj; j += 2) {
  133. const x = quadPoints[j];
  134. const y = quadPoints[j + 1];
  135. if (x < rect[0] || x > rect[2] || y < rect[1] || y > rect[3]) {
  136. return null;
  137. }
  138. quadPointsLists[i].push({
  139. x,
  140. y
  141. });
  142. }
  143. }
  144. return quadPointsLists;
  145. }
  146. function getTransformMatrix(rect, bbox, matrix) {
  147. const [minX, minY, maxX, maxY] = _util.Util.getAxialAlignedBoundingBox(bbox, matrix);
  148. if (minX === maxX || minY === maxY) {
  149. return [1, 0, 0, 1, rect[0], rect[1]];
  150. }
  151. const xRatio = (rect[2] - rect[0]) / (maxX - minX);
  152. const yRatio = (rect[3] - rect[1]) / (maxY - minY);
  153. return [xRatio, 0, 0, yRatio, rect[0] - minX * xRatio, rect[1] - minY * yRatio];
  154. }
  155. class Annotation {
  156. constructor(params) {
  157. const dict = params.dict;
  158. this.setContents(dict.get("Contents"));
  159. this.setModificationDate(dict.get("M"));
  160. this.setFlags(dict.get("F"));
  161. this.setRectangle(dict.getArray("Rect"));
  162. this.setColor(dict.getArray("C"));
  163. this.setBorderStyle(dict);
  164. this.setAppearance(dict);
  165. this.data = {
  166. annotationFlags: this.flags,
  167. borderStyle: this.borderStyle,
  168. color: this.color,
  169. contents: this.contents,
  170. hasAppearance: !!this.appearance,
  171. id: params.id,
  172. modificationDate: this.modificationDate,
  173. rect: this.rectangle,
  174. subtype: params.subtype
  175. };
  176. }
  177. _hasFlag(flags, flag) {
  178. return !!(flags & flag);
  179. }
  180. _isViewable(flags) {
  181. return !this._hasFlag(flags, _util.AnnotationFlag.INVISIBLE) && !this._hasFlag(flags, _util.AnnotationFlag.HIDDEN) && !this._hasFlag(flags, _util.AnnotationFlag.NOVIEW);
  182. }
  183. _isPrintable(flags) {
  184. return this._hasFlag(flags, _util.AnnotationFlag.PRINT) && !this._hasFlag(flags, _util.AnnotationFlag.INVISIBLE) && !this._hasFlag(flags, _util.AnnotationFlag.HIDDEN);
  185. }
  186. get viewable() {
  187. if (this.flags === 0) {
  188. return true;
  189. }
  190. return this._isViewable(this.flags);
  191. }
  192. get printable() {
  193. if (this.flags === 0) {
  194. return false;
  195. }
  196. return this._isPrintable(this.flags);
  197. }
  198. setContents(contents) {
  199. this.contents = (0, _util.stringToPDFString)(contents || "");
  200. }
  201. setModificationDate(modificationDate) {
  202. this.modificationDate = (0, _util.isString)(modificationDate) ? modificationDate : null;
  203. }
  204. setFlags(flags) {
  205. this.flags = Number.isInteger(flags) && flags > 0 ? flags : 0;
  206. }
  207. hasFlag(flag) {
  208. return this._hasFlag(this.flags, flag);
  209. }
  210. setRectangle(rectangle) {
  211. if (Array.isArray(rectangle) && rectangle.length === 4) {
  212. this.rectangle = _util.Util.normalizeRect(rectangle);
  213. } else {
  214. this.rectangle = [0, 0, 0, 0];
  215. }
  216. }
  217. setColor(color) {
  218. const rgbColor = new Uint8ClampedArray(3);
  219. if (!Array.isArray(color)) {
  220. this.color = rgbColor;
  221. return;
  222. }
  223. switch (color.length) {
  224. case 0:
  225. this.color = null;
  226. break;
  227. case 1:
  228. _colorspace.ColorSpace.singletons.gray.getRgbItem(color, 0, rgbColor, 0);
  229. this.color = rgbColor;
  230. break;
  231. case 3:
  232. _colorspace.ColorSpace.singletons.rgb.getRgbItem(color, 0, rgbColor, 0);
  233. this.color = rgbColor;
  234. break;
  235. case 4:
  236. _colorspace.ColorSpace.singletons.cmyk.getRgbItem(color, 0, rgbColor, 0);
  237. this.color = rgbColor;
  238. break;
  239. default:
  240. this.color = rgbColor;
  241. break;
  242. }
  243. }
  244. setBorderStyle(borderStyle) {
  245. this.borderStyle = new AnnotationBorderStyle();
  246. if (!(0, _primitives.isDict)(borderStyle)) {
  247. return;
  248. }
  249. if (borderStyle.has("BS")) {
  250. const dict = borderStyle.get("BS");
  251. const dictType = dict.get("Type");
  252. if (!dictType || (0, _primitives.isName)(dictType, "Border")) {
  253. this.borderStyle.setWidth(dict.get("W"), this.rectangle);
  254. this.borderStyle.setStyle(dict.get("S"));
  255. this.borderStyle.setDashArray(dict.getArray("D"));
  256. }
  257. } else if (borderStyle.has("Border")) {
  258. const array = borderStyle.getArray("Border");
  259. if (Array.isArray(array) && array.length >= 3) {
  260. this.borderStyle.setHorizontalCornerRadius(array[0]);
  261. this.borderStyle.setVerticalCornerRadius(array[1]);
  262. this.borderStyle.setWidth(array[2], this.rectangle);
  263. if (array.length === 4) {
  264. this.borderStyle.setDashArray(array[3]);
  265. }
  266. }
  267. } else {
  268. this.borderStyle.setWidth(0);
  269. }
  270. }
  271. setAppearance(dict) {
  272. this.appearance = null;
  273. const appearanceStates = dict.get("AP");
  274. if (!(0, _primitives.isDict)(appearanceStates)) {
  275. return;
  276. }
  277. const normalAppearanceState = appearanceStates.get("N");
  278. if ((0, _primitives.isStream)(normalAppearanceState)) {
  279. this.appearance = normalAppearanceState;
  280. return;
  281. }
  282. if (!(0, _primitives.isDict)(normalAppearanceState)) {
  283. return;
  284. }
  285. const as = dict.get("AS");
  286. if (!(0, _primitives.isName)(as) || !normalAppearanceState.has(as.name)) {
  287. return;
  288. }
  289. this.appearance = normalAppearanceState.get(as.name);
  290. }
  291. loadResources(keys) {
  292. return this.appearance.dict.getAsync("Resources").then(resources => {
  293. if (!resources) {
  294. return undefined;
  295. }
  296. const objectLoader = new _obj.ObjectLoader(resources, keys, resources.xref);
  297. return objectLoader.load().then(function () {
  298. return resources;
  299. });
  300. });
  301. }
  302. getOperatorList(evaluator, task, renderForms, annotationStorage) {
  303. if (!this.appearance) {
  304. return Promise.resolve(new _operator_list.OperatorList());
  305. }
  306. const appearance = this.appearance;
  307. const data = this.data;
  308. const appearanceDict = appearance.dict;
  309. const resourcesPromise = this.loadResources(["ExtGState", "ColorSpace", "Pattern", "Shading", "XObject", "Font"]);
  310. const bbox = appearanceDict.getArray("BBox") || [0, 0, 1, 1];
  311. const matrix = appearanceDict.getArray("Matrix") || [1, 0, 0, 1, 0, 0];
  312. const transform = getTransformMatrix(data.rect, bbox, matrix);
  313. return resourcesPromise.then(resources => {
  314. const opList = new _operator_list.OperatorList();
  315. opList.addOp(_util.OPS.beginAnnotation, [data.rect, transform, matrix]);
  316. return evaluator.getOperatorList({
  317. stream: appearance,
  318. task,
  319. resources,
  320. operatorList: opList
  321. }).then(() => {
  322. opList.addOp(_util.OPS.endAnnotation, []);
  323. appearance.reset();
  324. return opList;
  325. });
  326. });
  327. }
  328. async save(evaluator, task, annotationStorage) {
  329. return null;
  330. }
  331. }
  332. exports.Annotation = Annotation;
  333. class AnnotationBorderStyle {
  334. constructor() {
  335. this.width = 1;
  336. this.style = _util.AnnotationBorderStyleType.SOLID;
  337. this.dashArray = [3];
  338. this.horizontalCornerRadius = 0;
  339. this.verticalCornerRadius = 0;
  340. }
  341. setWidth(width, rect = [0, 0, 0, 0]) {
  342. if ((0, _primitives.isName)(width)) {
  343. this.width = 0;
  344. return;
  345. }
  346. if (Number.isInteger(width)) {
  347. if (width > 0) {
  348. const maxWidth = (rect[2] - rect[0]) / 2;
  349. const maxHeight = (rect[3] - rect[1]) / 2;
  350. if (maxWidth > 0 && maxHeight > 0 && (width > maxWidth || width > maxHeight)) {
  351. (0, _util.warn)(`AnnotationBorderStyle.setWidth - ignoring width: ${width}`);
  352. width = 1;
  353. }
  354. }
  355. this.width = width;
  356. }
  357. }
  358. setStyle(style) {
  359. if (!(0, _primitives.isName)(style)) {
  360. return;
  361. }
  362. switch (style.name) {
  363. case "S":
  364. this.style = _util.AnnotationBorderStyleType.SOLID;
  365. break;
  366. case "D":
  367. this.style = _util.AnnotationBorderStyleType.DASHED;
  368. break;
  369. case "B":
  370. this.style = _util.AnnotationBorderStyleType.BEVELED;
  371. break;
  372. case "I":
  373. this.style = _util.AnnotationBorderStyleType.INSET;
  374. break;
  375. case "U":
  376. this.style = _util.AnnotationBorderStyleType.UNDERLINE;
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. setDashArray(dashArray) {
  383. if (Array.isArray(dashArray) && dashArray.length > 0) {
  384. let isValid = true;
  385. let allZeros = true;
  386. for (const element of dashArray) {
  387. const validNumber = +element >= 0;
  388. if (!validNumber) {
  389. isValid = false;
  390. break;
  391. } else if (element > 0) {
  392. allZeros = false;
  393. }
  394. }
  395. if (isValid && !allZeros) {
  396. this.dashArray = dashArray;
  397. } else {
  398. this.width = 0;
  399. }
  400. } else if (dashArray) {
  401. this.width = 0;
  402. }
  403. }
  404. setHorizontalCornerRadius(radius) {
  405. if (Number.isInteger(radius)) {
  406. this.horizontalCornerRadius = radius;
  407. }
  408. }
  409. setVerticalCornerRadius(radius) {
  410. if (Number.isInteger(radius)) {
  411. this.verticalCornerRadius = radius;
  412. }
  413. }
  414. }
  415. exports.AnnotationBorderStyle = AnnotationBorderStyle;
  416. class MarkupAnnotation extends Annotation {
  417. constructor(parameters) {
  418. super(parameters);
  419. const dict = parameters.dict;
  420. if (dict.has("IRT")) {
  421. const rawIRT = dict.getRaw("IRT");
  422. this.data.inReplyTo = (0, _primitives.isRef)(rawIRT) ? rawIRT.toString() : null;
  423. const rt = dict.get("RT");
  424. this.data.replyType = (0, _primitives.isName)(rt) ? rt.name : _util.AnnotationReplyType.REPLY;
  425. }
  426. if (this.data.replyType === _util.AnnotationReplyType.GROUP) {
  427. const parent = dict.get("IRT");
  428. this.data.title = (0, _util.stringToPDFString)(parent.get("T") || "");
  429. this.setContents(parent.get("Contents"));
  430. this.data.contents = this.contents;
  431. if (!parent.has("CreationDate")) {
  432. this.data.creationDate = null;
  433. } else {
  434. this.setCreationDate(parent.get("CreationDate"));
  435. this.data.creationDate = this.creationDate;
  436. }
  437. if (!parent.has("M")) {
  438. this.data.modificationDate = null;
  439. } else {
  440. this.setModificationDate(parent.get("M"));
  441. this.data.modificationDate = this.modificationDate;
  442. }
  443. this.data.hasPopup = parent.has("Popup");
  444. if (!parent.has("C")) {
  445. this.data.color = null;
  446. } else {
  447. this.setColor(parent.getArray("C"));
  448. this.data.color = this.color;
  449. }
  450. } else {
  451. this.data.title = (0, _util.stringToPDFString)(dict.get("T") || "");
  452. this.setCreationDate(dict.get("CreationDate"));
  453. this.data.creationDate = this.creationDate;
  454. this.data.hasPopup = dict.has("Popup");
  455. if (!dict.has("C")) {
  456. this.data.color = null;
  457. }
  458. }
  459. }
  460. setCreationDate(creationDate) {
  461. this.creationDate = (0, _util.isString)(creationDate) ? creationDate : null;
  462. }
  463. }
  464. exports.MarkupAnnotation = MarkupAnnotation;
  465. class WidgetAnnotation extends Annotation {
  466. constructor(params) {
  467. super(params);
  468. const dict = params.dict;
  469. const data = this.data;
  470. this.ref = params.ref;
  471. data.annotationType = _util.AnnotationType.WIDGET;
  472. data.fieldName = this._constructFieldName(dict);
  473. const fieldValue = (0, _core_utils.getInheritableProperty)({
  474. dict,
  475. key: "V",
  476. getArray: true
  477. });
  478. data.fieldValue = this._decodeFormValue(fieldValue);
  479. data.alternativeText = (0, _util.stringToPDFString)(dict.get("TU") || "");
  480. data.defaultAppearance = (0, _core_utils.getInheritableProperty)({
  481. dict,
  482. key: "DA"
  483. }) || params.acroForm.get("DA") || "";
  484. const fieldType = (0, _core_utils.getInheritableProperty)({
  485. dict,
  486. key: "FT"
  487. });
  488. data.fieldType = (0, _primitives.isName)(fieldType) ? fieldType.name : null;
  489. this.fieldResources = (0, _core_utils.getInheritableProperty)({
  490. dict,
  491. key: "DR"
  492. }) || params.acroForm.get("DR") || _primitives.Dict.empty;
  493. data.fieldFlags = (0, _core_utils.getInheritableProperty)({
  494. dict,
  495. key: "Ff"
  496. });
  497. if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
  498. data.fieldFlags = 0;
  499. }
  500. data.readOnly = this.hasFieldFlag(_util.AnnotationFieldFlag.READONLY);
  501. if (data.fieldType === "Sig") {
  502. data.fieldValue = null;
  503. this.setFlags(_util.AnnotationFlag.HIDDEN);
  504. }
  505. }
  506. _constructFieldName(dict) {
  507. if (!dict.has("T") && !dict.has("Parent")) {
  508. (0, _util.warn)("Unknown field name, falling back to empty field name.");
  509. return "";
  510. }
  511. if (!dict.has("Parent")) {
  512. return (0, _util.stringToPDFString)(dict.get("T"));
  513. }
  514. const fieldName = [];
  515. if (dict.has("T")) {
  516. fieldName.unshift((0, _util.stringToPDFString)(dict.get("T")));
  517. }
  518. let loopDict = dict;
  519. while (loopDict.has("Parent")) {
  520. loopDict = loopDict.get("Parent");
  521. if (!(0, _primitives.isDict)(loopDict)) {
  522. break;
  523. }
  524. if (loopDict.has("T")) {
  525. fieldName.unshift((0, _util.stringToPDFString)(loopDict.get("T")));
  526. }
  527. }
  528. return fieldName.join(".");
  529. }
  530. _decodeFormValue(formValue) {
  531. if (Array.isArray(formValue)) {
  532. return formValue.filter(item => (0, _util.isString)(item)).map(item => (0, _util.stringToPDFString)(item));
  533. } else if ((0, _primitives.isName)(formValue)) {
  534. return (0, _util.stringToPDFString)(formValue.name);
  535. } else if ((0, _util.isString)(formValue)) {
  536. return (0, _util.stringToPDFString)(formValue);
  537. }
  538. return null;
  539. }
  540. hasFieldFlag(flag) {
  541. return !!(this.data.fieldFlags & flag);
  542. }
  543. getOperatorList(evaluator, task, renderForms, annotationStorage) {
  544. if (renderForms) {
  545. return Promise.resolve(new _operator_list.OperatorList());
  546. }
  547. if (!this._hasText) {
  548. return super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  549. }
  550. return this._getAppearance(evaluator, task, annotationStorage).then(content => {
  551. if (this.appearance && content === null) {
  552. return super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  553. }
  554. const operatorList = new _operator_list.OperatorList();
  555. if (!this.data.defaultAppearance || content === null) {
  556. return operatorList;
  557. }
  558. const matrix = [1, 0, 0, 1, 0, 0];
  559. const bbox = [0, 0, this.data.rect[2] - this.data.rect[0], this.data.rect[3] - this.data.rect[1]];
  560. const transform = getTransformMatrix(this.data.rect, bbox, matrix);
  561. operatorList.addOp(_util.OPS.beginAnnotation, [this.data.rect, transform, matrix]);
  562. const stream = new _stream.StringStream(content);
  563. return evaluator.getOperatorList({
  564. stream,
  565. task,
  566. resources: this.fieldResources,
  567. operatorList
  568. }).then(function () {
  569. operatorList.addOp(_util.OPS.endAnnotation, []);
  570. return operatorList;
  571. });
  572. });
  573. }
  574. async save(evaluator, task, annotationStorage) {
  575. if (this.data.fieldValue === annotationStorage[this.data.id]) {
  576. return null;
  577. }
  578. let appearance = await this._getAppearance(evaluator, task, annotationStorage);
  579. if (appearance === null) {
  580. return null;
  581. }
  582. const dict = evaluator.xref.fetchIfRef(this.ref);
  583. if (!(0, _primitives.isDict)(dict)) {
  584. return null;
  585. }
  586. const bbox = [0, 0, this.data.rect[2] - this.data.rect[0], this.data.rect[3] - this.data.rect[1]];
  587. const newRef = evaluator.xref.getNewRef();
  588. const AP = new _primitives.Dict(evaluator.xref);
  589. AP.set("N", newRef);
  590. const value = annotationStorage[this.data.id];
  591. const encrypt = evaluator.xref.encrypt;
  592. let originalTransform = null;
  593. let newTransform = null;
  594. if (encrypt) {
  595. originalTransform = encrypt.createCipherTransform(this.ref.num, this.ref.gen);
  596. newTransform = encrypt.createCipherTransform(newRef.num, newRef.gen);
  597. appearance = newTransform.encryptString(appearance);
  598. }
  599. dict.set("V", value);
  600. dict.set("AP", AP);
  601. dict.set("M", `D:${(0, _util.getModificationDate)()}`);
  602. const appearanceDict = new _primitives.Dict(evaluator.xref);
  603. appearanceDict.set("Length", appearance.length);
  604. appearanceDict.set("Subtype", _primitives.Name.get("Form"));
  605. appearanceDict.set("Resources", this.fieldResources);
  606. appearanceDict.set("BBox", bbox);
  607. const bufferOriginal = [`${this.ref.num} ${this.ref.gen} obj\n`];
  608. (0, _writer.writeDict)(dict, bufferOriginal, originalTransform);
  609. bufferOriginal.push("\nendobj\n");
  610. const bufferNew = [`${newRef.num} ${newRef.gen} obj\n`];
  611. (0, _writer.writeDict)(appearanceDict, bufferNew, newTransform);
  612. bufferNew.push(" stream\n");
  613. bufferNew.push(appearance);
  614. bufferNew.push("\nendstream\nendobj\n");
  615. return [{
  616. ref: this.ref,
  617. data: bufferOriginal.join("")
  618. }, {
  619. ref: newRef,
  620. data: bufferNew.join("")
  621. }];
  622. }
  623. async _getAppearance(evaluator, task, annotationStorage) {
  624. const isPassword = this.hasFieldFlag(_util.AnnotationFieldFlag.PASSWORD);
  625. if (!annotationStorage || isPassword) {
  626. return null;
  627. }
  628. const value = annotationStorage[this.data.id];
  629. if (value === "") {
  630. return "";
  631. }
  632. const defaultPadding = 2;
  633. const hPadding = defaultPadding;
  634. const totalHeight = this.data.rect[3] - this.data.rect[1];
  635. const totalWidth = this.data.rect[2] - this.data.rect[0];
  636. const fontInfo = await this._getFontData(evaluator, task);
  637. const [font, fontName] = fontInfo;
  638. let fontSize = fontInfo[2];
  639. fontSize = this._computeFontSize(font, fontName, fontSize, totalHeight);
  640. let descent = font.descent;
  641. if (isNaN(descent)) {
  642. descent = 0;
  643. }
  644. const vPadding = defaultPadding + Math.abs(descent) * fontSize;
  645. const defaultAppearance = this.data.defaultAppearance;
  646. const alignment = this.data.textAlignment;
  647. if (this.data.comb) {
  648. return this._getCombAppearance(defaultAppearance, value, totalWidth, hPadding, vPadding);
  649. }
  650. if (this.data.multiLine) {
  651. return this._getMultilineAppearance(defaultAppearance, value, font, fontSize, totalWidth, totalHeight, alignment, hPadding, vPadding);
  652. }
  653. if (alignment === 0 || alignment > 2) {
  654. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 ${hPadding} ${vPadding} Tm (${(0, _util.escapeString)(value)}) Tj` + " ET Q EMC";
  655. }
  656. const renderedText = this._renderText(value, font, fontSize, totalWidth, alignment, hPadding, vPadding);
  657. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 0 0 Tm ${renderedText}` + " ET Q EMC";
  658. }
  659. async _getFontData(evaluator, task) {
  660. const operatorList = new _operator_list.OperatorList();
  661. const initialState = {
  662. fontSize: 0,
  663. font: null,
  664. fontName: null,
  665. clone() {
  666. return this;
  667. }
  668. };
  669. await evaluator.getOperatorList({
  670. stream: new _stream.StringStream(this.data.defaultAppearance),
  671. task,
  672. resources: this.fieldResources,
  673. operatorList,
  674. initialState
  675. });
  676. return [initialState.font, initialState.fontName, initialState.fontSize];
  677. }
  678. _computeFontSize(font, fontName, fontSize, height) {
  679. if (fontSize === null || fontSize === 0) {
  680. const em = font.charsToGlyphs("M", true)[0].width / 1000;
  681. const capHeight = 0.7 * em;
  682. fontSize = Math.max(1, Math.floor(height / (1.5 * capHeight)));
  683. let fontRegex = new RegExp(`/${fontName}\\s+[0-9\.]+\\s+Tf`);
  684. if (this.data.defaultAppearance.search(fontRegex) === -1) {
  685. fontRegex = new RegExp(`/${fontName}\\s+Tf`);
  686. }
  687. this.data.defaultAppearance = this.data.defaultAppearance.replace(fontRegex, `/${fontName} ${fontSize} Tf`);
  688. }
  689. return fontSize;
  690. }
  691. _renderText(text, font, fontSize, totalWidth, alignment, hPadding, vPadding) {
  692. const glyphs = font.charsToGlyphs(text);
  693. const scale = fontSize / 1000;
  694. let width = 0;
  695. for (const glyph of glyphs) {
  696. width += glyph.width * scale;
  697. }
  698. let shift;
  699. if (alignment === 1) {
  700. shift = (totalWidth - width) / 2;
  701. } else if (alignment === 2) {
  702. shift = totalWidth - width - hPadding;
  703. } else {
  704. shift = hPadding;
  705. }
  706. shift = shift.toFixed(2);
  707. vPadding = vPadding.toFixed(2);
  708. return `${shift} ${vPadding} Td (${(0, _util.escapeString)(text)}) Tj`;
  709. }
  710. }
  711. class TextWidgetAnnotation extends WidgetAnnotation {
  712. constructor(params) {
  713. super(params);
  714. this._hasText = true;
  715. const dict = params.dict;
  716. if (!(0, _util.isString)(this.data.fieldValue)) {
  717. this.data.fieldValue = "";
  718. }
  719. let alignment = (0, _core_utils.getInheritableProperty)({
  720. dict,
  721. key: "Q"
  722. });
  723. if (!Number.isInteger(alignment) || alignment < 0 || alignment > 2) {
  724. alignment = null;
  725. }
  726. this.data.textAlignment = alignment;
  727. let maximumLength = (0, _core_utils.getInheritableProperty)({
  728. dict,
  729. key: "MaxLen"
  730. });
  731. if (!Number.isInteger(maximumLength) || maximumLength < 0) {
  732. maximumLength = null;
  733. }
  734. this.data.maxLen = maximumLength;
  735. this.data.multiLine = this.hasFieldFlag(_util.AnnotationFieldFlag.MULTILINE);
  736. this.data.comb = this.hasFieldFlag(_util.AnnotationFieldFlag.COMB) && !this.hasFieldFlag(_util.AnnotationFieldFlag.MULTILINE) && !this.hasFieldFlag(_util.AnnotationFieldFlag.PASSWORD) && !this.hasFieldFlag(_util.AnnotationFieldFlag.FILESELECT) && this.data.maxLen !== null;
  737. }
  738. _getCombAppearance(defaultAppearance, text, width, hPadding, vPadding) {
  739. const combWidth = (width / this.data.maxLen).toFixed(2);
  740. const buf = [];
  741. for (const character of text) {
  742. buf.push(`(${(0, _util.escapeString)(character)}) Tj`);
  743. }
  744. const renderedComb = buf.join(` ${combWidth} 0 Td `);
  745. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 ${hPadding} ${vPadding} Tm ${renderedComb}` + " ET Q EMC";
  746. }
  747. _getMultilineAppearance(defaultAppearance, text, font, fontSize, width, height, alignment, hPadding, vPadding) {
  748. const lines = text.split(/\r\n|\r|\n/);
  749. const buf = [];
  750. const totalWidth = width - 2 * hPadding;
  751. for (const line of lines) {
  752. const chunks = this._splitLine(line, font, fontSize, totalWidth);
  753. for (const chunk of chunks) {
  754. const padding = buf.length === 0 ? hPadding : 0;
  755. buf.push(this._renderText(chunk, font, fontSize, width, alignment, padding, -fontSize));
  756. }
  757. }
  758. const renderedText = buf.join("\n");
  759. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 0 ${height} Tm ${renderedText}` + " ET Q EMC";
  760. }
  761. _splitLine(line, font, fontSize, width) {
  762. if (line.length <= 1) {
  763. return [line];
  764. }
  765. const scale = fontSize / 1000;
  766. const whitespace = font.charsToGlyphs(" ", true)[0].width * scale;
  767. const chunks = [];
  768. let lastSpacePos = -1,
  769. startChunk = 0,
  770. currentWidth = 0;
  771. for (let i = 0, ii = line.length; i < ii; i++) {
  772. const character = line.charAt(i);
  773. if (character === " ") {
  774. if (currentWidth + whitespace > width) {
  775. chunks.push(line.substring(startChunk, i));
  776. startChunk = i;
  777. currentWidth = whitespace;
  778. lastSpacePos = -1;
  779. } else {
  780. currentWidth += whitespace;
  781. lastSpacePos = i;
  782. }
  783. } else {
  784. const charWidth = font.charsToGlyphs(character, false)[0].width * scale;
  785. if (currentWidth + charWidth > width) {
  786. if (lastSpacePos !== -1) {
  787. chunks.push(line.substring(startChunk, lastSpacePos + 1));
  788. startChunk = i = lastSpacePos + 1;
  789. lastSpacePos = -1;
  790. currentWidth = 0;
  791. } else {
  792. chunks.push(line.substring(startChunk, i));
  793. startChunk = i;
  794. currentWidth = charWidth;
  795. }
  796. } else {
  797. currentWidth += charWidth;
  798. }
  799. }
  800. }
  801. if (startChunk < line.length) {
  802. chunks.push(line.substring(startChunk, line.length));
  803. }
  804. return chunks;
  805. }
  806. }
  807. class ButtonWidgetAnnotation extends WidgetAnnotation {
  808. constructor(params) {
  809. super(params);
  810. this.checkedAppearance = null;
  811. this.uncheckedAppearance = null;
  812. this.data.checkBox = !this.hasFieldFlag(_util.AnnotationFieldFlag.RADIO) && !this.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  813. this.data.radioButton = this.hasFieldFlag(_util.AnnotationFieldFlag.RADIO) && !this.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  814. this.data.pushButton = this.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  815. if (this.data.checkBox) {
  816. this._processCheckBox(params);
  817. } else if (this.data.radioButton) {
  818. this._processRadioButton(params);
  819. } else if (this.data.pushButton) {
  820. this._processPushButton(params);
  821. } else {
  822. (0, _util.warn)("Invalid field flags for button widget annotation");
  823. }
  824. }
  825. getOperatorList(evaluator, task, renderForms, annotationStorage) {
  826. if (this.data.pushButton) {
  827. return super.getOperatorList(evaluator, task, false, annotationStorage);
  828. }
  829. if (annotationStorage) {
  830. const value = annotationStorage[this.data.id] || false;
  831. let appearance;
  832. if (value) {
  833. appearance = this.checkedAppearance;
  834. } else {
  835. appearance = this.uncheckedAppearance;
  836. }
  837. if (appearance) {
  838. const savedAppearance = this.appearance;
  839. this.appearance = appearance;
  840. const operatorList = super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  841. this.appearance = savedAppearance;
  842. return operatorList;
  843. }
  844. return Promise.resolve(new _operator_list.OperatorList());
  845. }
  846. return super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  847. }
  848. async save(evaluator, task, annotationStorage) {
  849. if (this.data.checkBox) {
  850. return this._saveCheckbox(evaluator, task, annotationStorage);
  851. }
  852. if (this.data.radioButton) {
  853. return this._saveRadioButton(evaluator, task, annotationStorage);
  854. }
  855. return super.save(evaluator, task, annotationStorage);
  856. }
  857. async _saveCheckbox(evaluator, task, annotationStorage) {
  858. const defaultValue = this.data.fieldValue && this.data.fieldValue !== "Off";
  859. const value = annotationStorage[this.data.id];
  860. if (defaultValue === value) {
  861. return null;
  862. }
  863. const dict = evaluator.xref.fetchIfRef(this.ref);
  864. if (!(0, _primitives.isDict)(dict)) {
  865. return null;
  866. }
  867. const name = _primitives.Name.get(value ? this.data.exportValue : "Off");
  868. dict.set("V", name);
  869. dict.set("AS", name);
  870. dict.set("M", `D:${(0, _util.getModificationDate)()}`);
  871. const encrypt = evaluator.xref.encrypt;
  872. let originalTransform = null;
  873. if (encrypt) {
  874. originalTransform = encrypt.createCipherTransform(this.ref.num, this.ref.gen);
  875. }
  876. const buffer = [`${this.ref.num} ${this.ref.gen} obj\n`];
  877. (0, _writer.writeDict)(dict, buffer, originalTransform);
  878. buffer.push("\nendobj\n");
  879. return [{
  880. ref: this.ref,
  881. data: buffer.join("")
  882. }];
  883. }
  884. async _saveRadioButton(evaluator, task, annotationStorage) {
  885. const defaultValue = this.data.fieldValue === this.data.buttonValue;
  886. const value = annotationStorage[this.data.id];
  887. if (defaultValue === value) {
  888. return null;
  889. }
  890. const dict = evaluator.xref.fetchIfRef(this.ref);
  891. if (!(0, _primitives.isDict)(dict)) {
  892. return null;
  893. }
  894. const name = _primitives.Name.get(value ? this.data.buttonValue : "Off");
  895. let parentBuffer = null;
  896. const encrypt = evaluator.xref.encrypt;
  897. if (value) {
  898. if ((0, _primitives.isRef)(this.parent)) {
  899. const parent = evaluator.xref.fetch(this.parent);
  900. let parentTransform = null;
  901. if (encrypt) {
  902. parentTransform = encrypt.createCipherTransform(this.parent.num, this.parent.gen);
  903. }
  904. parent.set("V", name);
  905. parentBuffer = [`${this.parent.num} ${this.parent.gen} obj\n`];
  906. (0, _writer.writeDict)(parent, parentBuffer, parentTransform);
  907. parentBuffer.push("\nendobj\n");
  908. } else if ((0, _primitives.isDict)(this.parent)) {
  909. this.parent.set("V", name);
  910. }
  911. }
  912. dict.set("AS", name);
  913. dict.set("M", `D:${(0, _util.getModificationDate)()}`);
  914. let originalTransform = null;
  915. if (encrypt) {
  916. originalTransform = encrypt.createCipherTransform(this.ref.num, this.ref.gen);
  917. }
  918. const buffer = [`${this.ref.num} ${this.ref.gen} obj\n`];
  919. (0, _writer.writeDict)(dict, buffer, originalTransform);
  920. buffer.push("\nendobj\n");
  921. const newRefs = [{
  922. ref: this.ref,
  923. data: buffer.join("")
  924. }];
  925. if (parentBuffer !== null) {
  926. newRefs.push({
  927. ref: this.parent,
  928. data: parentBuffer.join("")
  929. });
  930. }
  931. return newRefs;
  932. }
  933. _processCheckBox(params) {
  934. const customAppearance = params.dict.get("AP");
  935. if (!(0, _primitives.isDict)(customAppearance)) {
  936. return;
  937. }
  938. const normalAppearance = customAppearance.get("N");
  939. if (!(0, _primitives.isDict)(normalAppearance)) {
  940. return;
  941. }
  942. const exportValues = normalAppearance.getKeys();
  943. if (!exportValues.includes("Off")) {
  944. exportValues.push("Off");
  945. }
  946. if (exportValues.length !== 2) {
  947. return;
  948. }
  949. this.data.exportValue = exportValues[0] === "Off" ? exportValues[1] : exportValues[0];
  950. this.checkedAppearance = normalAppearance.get(this.data.exportValue);
  951. this.uncheckedAppearance = normalAppearance.get("Off") || null;
  952. }
  953. _processRadioButton(params) {
  954. this.data.fieldValue = this.data.buttonValue = null;
  955. const fieldParent = params.dict.get("Parent");
  956. if ((0, _primitives.isDict)(fieldParent) && fieldParent.has("V")) {
  957. const fieldParentValue = fieldParent.get("V");
  958. if ((0, _primitives.isName)(fieldParentValue)) {
  959. this.parent = params.dict.getRaw("Parent");
  960. this.data.fieldValue = this._decodeFormValue(fieldParentValue);
  961. }
  962. }
  963. const appearanceStates = params.dict.get("AP");
  964. if (!(0, _primitives.isDict)(appearanceStates)) {
  965. return;
  966. }
  967. const normalAppearance = appearanceStates.get("N");
  968. if (!(0, _primitives.isDict)(normalAppearance)) {
  969. return;
  970. }
  971. for (const key of normalAppearance.getKeys()) {
  972. if (key !== "Off") {
  973. this.data.buttonValue = key;
  974. break;
  975. }
  976. }
  977. this.checkedAppearance = normalAppearance.get(this.data.buttonValue);
  978. this.uncheckedAppearance = normalAppearance.get("Off") || null;
  979. }
  980. _processPushButton(params) {
  981. if (!params.dict.has("A")) {
  982. (0, _util.warn)("Push buttons without action dictionaries are not supported");
  983. return;
  984. }
  985. _obj.Catalog.parseDestDictionary({
  986. destDict: params.dict,
  987. resultObj: this.data,
  988. docBaseUrl: params.pdfManager.docBaseUrl
  989. });
  990. }
  991. }
  992. class ChoiceWidgetAnnotation extends WidgetAnnotation {
  993. constructor(params) {
  994. super(params);
  995. this.data.options = [];
  996. const options = (0, _core_utils.getInheritableProperty)({
  997. dict: params.dict,
  998. key: "Opt"
  999. });
  1000. if (Array.isArray(options)) {
  1001. const xref = params.xref;
  1002. for (let i = 0, ii = options.length; i < ii; i++) {
  1003. const option = xref.fetchIfRef(options[i]);
  1004. const isOptionArray = Array.isArray(option);
  1005. this.data.options[i] = {
  1006. exportValue: this._decodeFormValue(isOptionArray ? xref.fetchIfRef(option[0]) : option),
  1007. displayValue: this._decodeFormValue(isOptionArray ? xref.fetchIfRef(option[1]) : option)
  1008. };
  1009. }
  1010. }
  1011. if ((0, _util.isString)(this.data.fieldValue)) {
  1012. this.data.fieldValue = [this.data.fieldValue];
  1013. } else if (!this.data.fieldValue) {
  1014. this.data.fieldValue = [];
  1015. }
  1016. this.data.combo = this.hasFieldFlag(_util.AnnotationFieldFlag.COMBO);
  1017. this.data.multiSelect = this.hasFieldFlag(_util.AnnotationFieldFlag.MULTISELECT);
  1018. this._hasText = true;
  1019. }
  1020. }
  1021. class TextAnnotation extends MarkupAnnotation {
  1022. constructor(parameters) {
  1023. const DEFAULT_ICON_SIZE = 22;
  1024. super(parameters);
  1025. const dict = parameters.dict;
  1026. this.data.annotationType = _util.AnnotationType.TEXT;
  1027. if (this.data.hasAppearance) {
  1028. this.data.name = "NoIcon";
  1029. } else {
  1030. this.data.rect[1] = this.data.rect[3] - DEFAULT_ICON_SIZE;
  1031. this.data.rect[2] = this.data.rect[0] + DEFAULT_ICON_SIZE;
  1032. this.data.name = dict.has("Name") ? dict.get("Name").name : "Note";
  1033. }
  1034. if (dict.has("State")) {
  1035. this.data.state = dict.get("State") || null;
  1036. this.data.stateModel = dict.get("StateModel") || null;
  1037. } else {
  1038. this.data.state = null;
  1039. this.data.stateModel = null;
  1040. }
  1041. }
  1042. }
  1043. class LinkAnnotation extends Annotation {
  1044. constructor(params) {
  1045. super(params);
  1046. this.data.annotationType = _util.AnnotationType.LINK;
  1047. const quadPoints = getQuadPoints(params.dict, this.rectangle);
  1048. if (quadPoints) {
  1049. this.data.quadPoints = quadPoints;
  1050. }
  1051. _obj.Catalog.parseDestDictionary({
  1052. destDict: params.dict,
  1053. resultObj: this.data,
  1054. docBaseUrl: params.pdfManager.docBaseUrl
  1055. });
  1056. }
  1057. }
  1058. class PopupAnnotation extends Annotation {
  1059. constructor(parameters) {
  1060. super(parameters);
  1061. this.data.annotationType = _util.AnnotationType.POPUP;
  1062. let parentItem = parameters.dict.get("Parent");
  1063. if (!parentItem) {
  1064. (0, _util.warn)("Popup annotation has a missing or invalid parent annotation.");
  1065. return;
  1066. }
  1067. const parentSubtype = parentItem.get("Subtype");
  1068. this.data.parentType = (0, _primitives.isName)(parentSubtype) ? parentSubtype.name : null;
  1069. const rawParent = parameters.dict.getRaw("Parent");
  1070. this.data.parentId = (0, _primitives.isRef)(rawParent) ? rawParent.toString() : null;
  1071. const rt = parentItem.get("RT");
  1072. if ((0, _primitives.isName)(rt, _util.AnnotationReplyType.GROUP)) {
  1073. parentItem = parentItem.get("IRT");
  1074. }
  1075. if (!parentItem.has("M")) {
  1076. this.data.modificationDate = null;
  1077. } else {
  1078. this.setModificationDate(parentItem.get("M"));
  1079. this.data.modificationDate = this.modificationDate;
  1080. }
  1081. if (!parentItem.has("C")) {
  1082. this.data.color = null;
  1083. } else {
  1084. this.setColor(parentItem.getArray("C"));
  1085. this.data.color = this.color;
  1086. }
  1087. if (!this.viewable) {
  1088. const parentFlags = parentItem.get("F");
  1089. if (this._isViewable(parentFlags)) {
  1090. this.setFlags(parentFlags);
  1091. }
  1092. }
  1093. this.data.title = (0, _util.stringToPDFString)(parentItem.get("T") || "");
  1094. this.data.contents = (0, _util.stringToPDFString)(parentItem.get("Contents") || "");
  1095. }
  1096. }
  1097. class FreeTextAnnotation extends MarkupAnnotation {
  1098. constructor(parameters) {
  1099. super(parameters);
  1100. this.data.annotationType = _util.AnnotationType.FREETEXT;
  1101. }
  1102. }
  1103. class LineAnnotation extends MarkupAnnotation {
  1104. constructor(parameters) {
  1105. super(parameters);
  1106. this.data.annotationType = _util.AnnotationType.LINE;
  1107. this.data.lineCoordinates = _util.Util.normalizeRect(parameters.dict.getArray("L"));
  1108. }
  1109. }
  1110. class SquareAnnotation extends MarkupAnnotation {
  1111. constructor(parameters) {
  1112. super(parameters);
  1113. this.data.annotationType = _util.AnnotationType.SQUARE;
  1114. }
  1115. }
  1116. class CircleAnnotation extends MarkupAnnotation {
  1117. constructor(parameters) {
  1118. super(parameters);
  1119. this.data.annotationType = _util.AnnotationType.CIRCLE;
  1120. }
  1121. }
  1122. class PolylineAnnotation extends MarkupAnnotation {
  1123. constructor(parameters) {
  1124. super(parameters);
  1125. this.data.annotationType = _util.AnnotationType.POLYLINE;
  1126. const rawVertices = parameters.dict.getArray("Vertices");
  1127. this.data.vertices = [];
  1128. for (let i = 0, ii = rawVertices.length; i < ii; i += 2) {
  1129. this.data.vertices.push({
  1130. x: rawVertices[i],
  1131. y: rawVertices[i + 1]
  1132. });
  1133. }
  1134. }
  1135. }
  1136. class PolygonAnnotation extends PolylineAnnotation {
  1137. constructor(parameters) {
  1138. super(parameters);
  1139. this.data.annotationType = _util.AnnotationType.POLYGON;
  1140. }
  1141. }
  1142. class CaretAnnotation extends MarkupAnnotation {
  1143. constructor(parameters) {
  1144. super(parameters);
  1145. this.data.annotationType = _util.AnnotationType.CARET;
  1146. }
  1147. }
  1148. class InkAnnotation extends MarkupAnnotation {
  1149. constructor(parameters) {
  1150. super(parameters);
  1151. this.data.annotationType = _util.AnnotationType.INK;
  1152. const xref = parameters.xref;
  1153. const originalInkLists = parameters.dict.getArray("InkList");
  1154. this.data.inkLists = [];
  1155. for (let i = 0, ii = originalInkLists.length; i < ii; ++i) {
  1156. this.data.inkLists.push([]);
  1157. for (let j = 0, jj = originalInkLists[i].length; j < jj; j += 2) {
  1158. this.data.inkLists[i].push({
  1159. x: xref.fetchIfRef(originalInkLists[i][j]),
  1160. y: xref.fetchIfRef(originalInkLists[i][j + 1])
  1161. });
  1162. }
  1163. }
  1164. }
  1165. }
  1166. class HighlightAnnotation extends MarkupAnnotation {
  1167. constructor(parameters) {
  1168. super(parameters);
  1169. this.data.annotationType = _util.AnnotationType.HIGHLIGHT;
  1170. const quadPoints = getQuadPoints(parameters.dict, this.rectangle);
  1171. if (quadPoints) {
  1172. this.data.quadPoints = quadPoints;
  1173. }
  1174. }
  1175. }
  1176. class UnderlineAnnotation extends MarkupAnnotation {
  1177. constructor(parameters) {
  1178. super(parameters);
  1179. this.data.annotationType = _util.AnnotationType.UNDERLINE;
  1180. const quadPoints = getQuadPoints(parameters.dict, this.rectangle);
  1181. if (quadPoints) {
  1182. this.data.quadPoints = quadPoints;
  1183. }
  1184. }
  1185. }
  1186. class SquigglyAnnotation extends MarkupAnnotation {
  1187. constructor(parameters) {
  1188. super(parameters);
  1189. this.data.annotationType = _util.AnnotationType.SQUIGGLY;
  1190. const quadPoints = getQuadPoints(parameters.dict, this.rectangle);
  1191. if (quadPoints) {
  1192. this.data.quadPoints = quadPoints;
  1193. }
  1194. }
  1195. }
  1196. class StrikeOutAnnotation extends MarkupAnnotation {
  1197. constructor(parameters) {
  1198. super(parameters);
  1199. this.data.annotationType = _util.AnnotationType.STRIKEOUT;
  1200. const quadPoints = getQuadPoints(parameters.dict, this.rectangle);
  1201. if (quadPoints) {
  1202. this.data.quadPoints = quadPoints;
  1203. }
  1204. }
  1205. }
  1206. class StampAnnotation extends MarkupAnnotation {
  1207. constructor(parameters) {
  1208. super(parameters);
  1209. this.data.annotationType = _util.AnnotationType.STAMP;
  1210. }
  1211. }
  1212. class FileAttachmentAnnotation extends MarkupAnnotation {
  1213. constructor(parameters) {
  1214. super(parameters);
  1215. const file = new _obj.FileSpec(parameters.dict.get("FS"), parameters.xref);
  1216. this.data.annotationType = _util.AnnotationType.FILEATTACHMENT;
  1217. this.data.file = file.serializable;
  1218. }
  1219. }